Build C++ Library with STM32CubeIDE

Hi, here’s a quick guide on how to build the C++ Library using STM32CubeIDE.

  1. Export as C++ Library from the Deployment tab in Edge Impulse.
  2. Add the edge-impulse-sdk, tflite-model and model-parameters folder to the Core directory of your project [1]
  3. Right click on the project, select Properties > C/C++ Build > Settings > Tool Settings and add these lines to the ‘Include paths’ for both the GCC and G++ Compiler:
"${workspace_loc:/${ProjName}/Core}"
"${workspace_loc:/${ProjName}/Core/edge-impulse-sdk}"
"${workspace_loc:/${ProjName}/Core/edge-impulse-sdk/CMSIS/Core/Include}"
"${workspace_loc:/${ProjName}/Core/edge-impulse-sdk/third_party/flatbuffers/include}"
"${workspace_loc:/${ProjName}/Core/edge-impulse-sdk/CMSIS/DSP/Include}"
"${workspace_loc:/${ProjName}/Core/edge-impulse-sdk/CMSIS/DSP/PrivateInclude}"
"${workspace_loc:/${ProjName}/Core/edge-impulse-sdk/CMSIS/NN/Include}"
"${workspace_loc:/${ProjName}/Core/edge-impulse-sdk/third_party}"
"${workspace_loc:/${ProjName}/Core/edge-impulse-sdk/third_party/flatbuffers}"
"${workspace_loc:/${ProjName}/Core/edge-impulse-sdk/third_party/gemmlowp}"
"${workspace_loc:/${ProjName}/Core/edge-impulse-sdk/third_party/ruy}"
"${workspace_loc:/${ProjName}/Core/edge-impulse-sdk/third_party}"

That’s it. You can now build as usual.

[1] Because I have no f@&* clue how to mark a folder as ‘contains source code that you need to compile’

7 Likes

[1] Me neither, which is part of my dislike for Eclipse :slight_smile:

I also recommend adding those includes to Debug and Release configurations as well. The Debug configuration will automatically add the -DDEBUG flag, which causes tflite inference to run much more slowly.

3 Likes

Thanks a lot @janjongboom …yeah [1] did the trick its compiling fine now. thanks a lot for your help

How i can compile library in keil?

@AlexKr best bet would be to just use C++ Library export. This is a list of includes that you’ll need to add: https://github.com/edgeimpulse/example-standalone-inferencing/blob/3e002222b905d519dac51cae194e659527eb836a/Makefile.tflite#L8

If there’s any GCC specific code in the library that prevents us from compiling with ARMCC let us know (I don’t think there’s a lot) though - I don’t have a KEIL license.

Current version of STM32CubeIDE does not seem to have such folder tree that you reference in [3].

?

@ArtoK is this a project that you’re inspecting rather than a folder? It should have an icon like this:

image

Here’s how I open the project properties menu. Is that right?

@ArtoK This looks like just a folder, not a project. Could you import the directory via Import > Existing project into workspace ? You should have a similar icon as I had afterwards…

I think I was able to add that libraray now in my project.
Is there any guidance how can I use the library in my project? How to launch the algorithm and where to get results?

Yes, https://docs.edgeimpulse.com/docs/using-cubeai this tutorial still applies. E.g. @ShawnHymel has used it to do keyword spotting from CubeIDE: https://github.com/ShawnHymel/ei-keyword-spotting

1 Like

I followed that tutorial but as I cannot do the ‘Adding the CMSIS-PACK’ part because


I followed Build C++ Library with STM32CubeIDE to add created C++ libraries.
After that I continued the tutorial from ’ Configuring printf’ but after adding those lines in the main.cpp I get compilation error
In file included from …/Core/Src/main.cpp:73:0:
C:/Users/ett14229/STM32CubeIDE/workspace_1.5.0/ML_WachingMachine/Core/edge-impulse-sdk/classifier/ei_run_classifier.h:89:10: fatal error: tflite-model/trained_model_compiled.h: No such file or directory
#include “tflite-model/trained_model_compiled.h”
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
How should I continue to get my libraries in use.

@ArtoK have you updated your include settings? Could you post a screenshot of Properties > C/C++ Build > Settings > Tool settings > G++ Compiler > Includes ?

@ArtoK where did you put the tflite-model folder? Also under Core/?

You are correct. It was there but not anymore. Strange. I copied it back and not it is going further.
Stucks here.
…/Core/Src/main.cpp:80:28: error: ‘huart1’ was not declared in this scope
HAL_UART_Transmit(&huart1, (uint8_t*)string, strlen(string), 0xffffff); // send message via UART
^~~~~~
I’ll check .ioc file next.
Thanks.

1 Like

For some reason the last part of the guide does not work. ‘Seeing the output’.
I’m using Putty and similar setup with it gives me console output if I update the firmware.

@ArtoK

  1. Are you setting the correct baud rate (try both 115,200 and 9,600?
  2. Have you configured the right UART peripheral, and are writing to the right peripheral?
  3. Can you print anything from your application and get output via normal STM32HAL functions?

Got this working. the STM IDE set the UART configuration to the wrong pins. Had to force it to correct pins.
image

Next question is that how can I gather the raw data with HW so that I dont have to copy and paste it from the cloud?