Incompatible with ArduinoBLE?

Hi,

I am deploying to Arduino Nano 33 BLE Sense board and am using the example Sketch given when the Arduino Library deployment is selected.

All was working fine until I tried to add BLE to the sketch. As soon as I do that I am unable to use the Arduino Desktop IDE Serial Monitor. When I select it from the IDE menu it is not shown and the IDE freezes until I disconnect the USB from the device.

On reconnecting and re-running the IDE I am unable to upload another Sketch until doing a bootloader reset (double press on the device button).

I get this problem even if I only have the following BLE code outside of setup() and loop() without going on to setup the BLE:

#include <ArduinoBLE.h>
...
BLEService soundsService("180C");  // User defined service

BLEStringCharacteristic soundCharacteristic("2A56",  // standard 16-bit characteristic UUID
    BLERead, 13); // remote clients will only be able to read this

I have tried finding the code that is in the example Sketch that causes things to go wrong by progressively commenting out the code. It isn’t the sound recording, it’s running the inference engine. It stops working as soon as I uncomment:

EI_IMPULSE_ERROR r = run_classifier(&signal, &result, debug_nn);
    if (r != EI_IMPULSE_OK) {
        ei_printf("ERR: Failed to run classifier (%d)\n", r);
        return;
    }

Note that after compilation it says that Sketch uses 438616 bytes (44%) of program space.
Global variables use 71120 bytes (27%) of dynamic memory, Leaving 191024 bytes for local variables.

Are there known issues? Do you have an example of it working with BLE?
Thanks

@jasonbsteele, that behavior indicates a hardfault (the built-in LED should also blink in an SOS pattern). Unfortunately you cannot see the error message on the Arduino Nano 33 BLE Sense when that happens without an external debugger. A way to see startup errors on this board is by adding:

    for (size_t ix = 0; ix < 10; ix++) {
      ei_printf("waiting %lu\n", ix);
      delay(1000);
    }

to the setup() function. Then you have time to attach :slight_smile:

I’ve experimented a bit and I see the the hardfault happening in PopulateTfLiteTensorArrayFromFlatbuffer but I don’t have the actual fault context…

OK I have no idea why, but if you change:

const unsigned char trained_tflite[] = {

into:

unsigned char trained_tflite[] = {

In tflite-trained.h this starts working…

1 Like

Excellent! Working for me too! … but the fix is bizarre! My knowledge of C/C++ embedded is very low (much more comfortable in Azure with C#!) so I have no idea what I’m talking about… but could it be something to do with where that array will be in memory?

Yeah when declared const the array will be in flash, and when declared non-const will be placed in ram. But it doesn’t make any sense to me why this would make any difference when loading the BLE library. Maybe some different linker settings that affect how the code works? It’s a real mystery here… will ask the Arduino folks when I run into them!

@dansitu could you ask the TF team whether the tflite model should be ran from flash or could be required to run from ram in certain conditions. I saw many todo comments for Pete Warden in the code here so would be interested in his comments.

Filed a bug report with the TFLite team here: https://github.com/tensorflow/tensorflow/issues/41271

2 Likes

Hi,

where I can find tflite-trained.h to patch?

thanks

@wallax, it’s in the Arduino libraries folder under the name of your project, e.g. on macOS:

~/Documents/Arduino/libraries/ei-hello-world-keywords-arduino-1.0.9/src/tflite-model/

(where ei-hello-keywords-arduino is my library name).

https://www.arduino.cc/en/hacking/libraries <-- locations on other operating systems.

@janjongboom thank you so much

Ric

I guess const put the bytes on the flash instead of SRAM.

@naveen That’s correct.

Hi,
I have the same problem and I don´t find tflite-model.h in my folder. I have trained_model_compiled.h or trained_model_ops_define.h in the tflite-model folder. What I have to change in this .h to solve the problem?

Thanks

Hi @Txomin,

“trained_model_compiled.h” means you have EON compiler enabled. I recommend disabling it in your project and exporting as a regular TFLM model. That should give you the “tflite-trained.h” that you can try patching.