Mutex 0x200102BC, Not allowed in ISR context - Crash when receiving BLE serial data from Android.
I'm getting a system crash when receiving data from Android BLE serial connection using SerialPassthrough example. It is easily reproducible as follows:
Load SerialPassthrough example in Arduino IDE.
To get it to work with Android app, change the device name by changing line #9 to
if (!bleSerial.beginAndSetupBLE("Pixl.js")) {
Compile and load onto Arduino Nano 33 BLE.
On Android phone, install GadgetBridge app (note: version in Google Play store may be out of date) https://f-droid.org/en/packages/nodomain.freeyourgadget.gadgetbridge/
Click + button, select Pixl.js device. Select "do not pair". Phone should connect successfully to Arduino. Click menu button (top left), then Debug. Press "Set music info" about 10+ times quickly. Ardunio should have crashed and LED will be blinking 3 short, 3 long blinks.
Crash info (available from TX1) will be:
++ MbedOS Error Info ++
Error Status: 0x80010133 Code: 307 Module: 1
Error Message: Mutex 0x200102BC, Not allowed in ISR context
Location: 0x4682F
Error Value: 0x200102BC
Current Thread: main Id: 0x200044C4 Entry: 0x4675D StackSize: 0x8000 StackMem: 0x20008288 SP: 0x2003FE54
For more info, visit: https://mbed.com/s/error?error=0x80010133&tgt=ARDUINO_NANO33BLE
-- MbedOS Error Info --
I've managed to narrow it down now, it is crashing at line #28
while (bleSerial.available() > 0) {
Serial.write(bleSerial.read());
}
If I replace the above section with the following then it no longer crashes:
if(bleSerial.availableLines() > 0) {
bleSerial.readLine(lineBuffer, 1024);
Serial.println(lineBuffer);
}
Further work has shown it has to do with the ble.poll() not being called often enough to process all the data coming thought. The readLine and println functions are much quicker at processing the data so it can get back to polling quicker.