STMicroelectronics.X-CUBE-AI - Sine Approximator

Preparing Model

See this Google Colab Notebookarrow-up-right.

STM32CubeIDE Setup

Go to Help -> Manage Embedded Software Packages

Install the latest Artificial Intelligence package under STMicroelectronics -> X-CUBE-AI tab

Create a new STM32 project. To see list only the MCU supported by the AI package, check the Enable checkbox under MIDDLEWARE -> Artificial Intelligence in the project selector.

STM32F446RET6 is supported.

After creating the project, configure the System Core and UART as normal. Then, in the ioc configuration page, select Software Packs -> Select Components

Enable the Artificial Intelligence pack, and select Application as Application Template

After enabling the pack, there will be a new configurable field STMicroelectronics.X-CUBE-AI under Software Packs.

When clicking it, it will prompt the following message if the clock is in default setting. Accept the change, as it will set the system clock to the highest frequency (180MHz for STM32F446).

Click "Add Network", set the model name to desired name, model type to "TFLite", and load the .tflite model file we exported in the Jupyter Notebook. Finally, click Analyze.

We can click the "Show graph" button to visualize the model

Generate code by saving the .ioc file.

The AI pack related files will be put under "X-CUBE-AI" folder.

Our model weights are loaded in the sine_model_data_params.c file.

Because we do not have printf redirected, we need to change the printf lines in the generated file to sprintf and UART_Transmit:

Before:

After:

And also grab the huart definition from main with extern

User Code

We will put our user code in the acquire_and_process_data and post_process function. For demo, we will just use a constant 2.0 as the input to the neural network, and see if the result is close to sin(2.0) = 0.9092974268256817.

Running the code, we get the following output.

Last updated