Nezha
July 12, 2022, 8:23pm
1
Hi everyone,
I am trying to use the qwiic openLog with my artemis global tracker, but it create some Mbed OS errors. I'm building my code over Example15_BetterTracker.
I wonder what might be the problem. I know that the artemis global tracker has other i2c devices (barometric sensor and iridium) connected. In the examples for the openlog we need to use Wire and those other device use Twowire.
Is it possible to use Wire and TwoWire instances at the same time?
#include <Wire.h> // Needed for I2C
const byte PIN_AGTWIRE_SCL = 8;
const byte PIN_AGTWIRE_SDA = 9;
TwoWire agtWire(PIN_AGTWIRE_SDA, PIN_AGTWIRE_SCL); //Create an I2C port using pads 8 (SCL) and 9 (SDA)
#include "SparkFun_Qwiic_OpenLog_Arduino_Library.h"
OpenLog myLog; //Create instance
void setup()
{
// Start the console serial port
Serial.begin(115200);
Wire.begin();
myLog.begin();
Whenever I use myLog.println(someString) the program crashes. But when I use only the examples of the openlog qwiic library it works without problem. So I wonder, if the problem comes from using Wire and twoWire at the same time.
Here's the error i'm getting:
++ MbedOS Fault Handler ++
FaultType: HardFault
Context:
R0: 10009FFC
R1: 186A0
R2: 1123456
R3: 50008000
R4: 0
R5: 10009FFC
R6: 10007B2C
R7: 39
R8: 10001404
R9: 10001088
R10: 10007AC8
R11: 10007A74
R12: 30679
SP : 10007958
LR : 31D79
PC : 35498
xPSR : 1000000
PSP : 100078F0
MSP : 1005FF70
CPUID: 410FC241
HFSR : 40000000
MMFSR: 0
BFSR : 82
UFSR : 0
DFSR : 0
AFSR : 0
BFAR : 50008214
Mode : Thread
Priv : Privileged
Stack: PSP
-- MbedOS Fault Handler --
++ MbedOS Error Info ++
Error Status: 0x80FF013D Code: 317 Module: 255
Error Message: Fault exception
Location: 0x35498
Error Value: 0x10006BB8
Current Thread: main Id: 0x10004F94 Entry: 0x2FA09 StackSize: 0x1000 StackMem: 0x10006C18 SP: 0x10007958
For more info, visit: https://mbed.com/s/error?error=0x80FF013D&tgt=SFE_ARTEMIS_ATP
-- MbedOS Error Info --
I wanted to check the disassembly file to see where the error is triggered, but I don't know how to convert .axf file to disassembly file.
Let me know if you need more info.
Nezha
July 13, 2022, 3:19pm
2
I found the solution over here.
opened 02:25AM - 23 Mar 22 UTC
bug
### Subject of the issue
use-after-free error in Wire library.
The relevant … methods are `Wire.begin`:
https://github.com/sparkfun/Arduino_Apollo3/blob/4e24a0275c4b84b0f3162e811162899dae4a1c62/libraries/Wire/src/Wire.cpp#L14-L19
and `Wire.end`:
https://github.com/sparkfun/Arduino_Apollo3/blob/4e24a0275c4b84b0f3162e811162899dae4a1c62/libraries/Wire/src/Wire.cpp#L31-L40
With the sequence:
```
Wire.begin();
Wire.end();
Wire.begin();
//I2C Activity
```
After the call to Wire.end, master is pointing to the location of the deleted `mbed::I2C` object.
### Your workbench
* RedBoard-Artemis-ATP
* Sparkfun Arduino_Apollo3 2.2.0, Arduino 1.8.13
* [TMP117](https://www.sparkfun.com/products/15805) connected via QWIIC connector.
* Powered via the USB-C plug.
### Steps to reproduce
#### Example Code
```c++
#include "Wire.h"
arduino::MbedI2C *myWire;
void setup() {
Serial.begin(115200);
Serial.println("Wire Use-After-Free");
Wire.end();
myWire = new arduino::MbedI2C(40, 39);
}
void loop() {
Serial.println("Loop");
myWire->begin();
Serial.println("Begin succeeded!");
Serial.println("Starting I2C transmission");
myWire->beginTransmission(0x48); //TMP117
myWire->endTransmission();
Serial.println("I2C transmission succeeded!");
myWire->end();
}
```
#### Example Code Output
```
Wire Use-After-Free
Loop
Begin succeeded!
Starting I2C transmission
I2C transmission succeeded!
Loop
Begin succeeded!
Starting I2C transmission
++ MbedOS Fault Handler ++
FaultType: HardFault
Context:
R0: 1000A750
R1: 90
R2: 1000A584
R3: A3009150
R4: 1000A750
R5: 0
R6: 0
R7: 0
R8: 0
R9: 0
R10: 0
R11: 0
R12: 23949
SP : 10006E48
LR : 10F2D
PC : A3009150
xPSR : 0
PSP : 10006DE0
MSP : 1005FF70
CPUID: 410FC241
HFSR : 40000000
MMFSR: 1
BFSR : 0
UFSR : 0
DFSR : 0
AFSR : 0
Mode : Thread
Priv : Privileged
Stack: PSP
-- MbedOS Fault Handler --
++ MbedOS Error Info ++
Error Status: 0x80FF013D Code: 317 Module: 255
Error Message: Fault exception
Location: 0xA3009150
Error Value: 0x10005E30
Current Thread: main Id: 0x10004248 Entry: 0x22C75 StackSize: 0x1000 StackMem: 0x10005E90 SP: 0x10006E48
For more info, visit: https://mbed.com/s/error?error=0x80FF013D&tgt=SFE_ARTEMIS_ATP
-- MbedOS Error Info --
```
#### Trace of I2C Output
First I2C transmission definitely worked:

### Possible solution
This code would stop the use-after-free. Maybe more is needed.
```c++
void arduino::MbedI2C::end() {
if (master != NULL) {
delete master;
master = NULL;
}
#ifdef DEVICE_I2CSLAVE
if (slave != NULL) {
delete slave;
slave = NULL;
}
#endif
}
```
system
Closed
January 9, 2023, 3:20pm
3
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.