How to deploy your AI model on mainstream STM32

This tutorial details the steps to generate an AI application running on a mainstream STM32, embedding an internal flash memory and without AI accelerator. For this tutorial, choice is made to use a STM32U5 microcontroller, in which AI inference is performed in software on its Cortex-M33 core. This tutorial uses the B-U585I-IOT02A as the reference board.

Objective

Generate a working AI inference application for the B-U585I-IOT02A by converting your AI model into C-code with STM32Cube AI Studio, configuring board peripherals and generating firmware project in STM32CubeMX, integrating model files and flashing the board.

Important

For a fast model evaluation on your board, STM32Cube AI Studio can directly generate a complete firmware project, ready to build and flash. Steps described in Parts 2 and 3 of this tutorial aim to detail required project configurations to implement in your own application.

Summary

Part 1 — Generate AI model C-code with STM32Cube AI Studio

  1. Getting the model

  2. Create a project in STM32Cube AI Studio

  3. Generate AI model code in STM32Cube AI Studio

Part 2 — Create the base firmware project with STM32CubeMX

  1. Create a project from the B-U585I-IOT02A board selector

  2. Configure Connectivity: USART1

  3. Configure Clock

  4. Configure Project Manager: appli structure, STM32CubeIDE

  5. Generate code and open the firmware project

Part 3 — Deploy AI model in firmware project with STM32CubeIDE

  1. Copy AI files to the firmware project

  2. Configure the project settings: include paths, link library, source location

  3. Edit main.c: add AI init and process calls

  4. Flash and run

0. Prerequisites

Component

Minimum version

Purpose

STM32CubeMX

6.17.0

Project configuration and code generation

STM32CubeU5 package

1.8.0

STM32U5 MCU package (HAL drivers)

STM32CubeIDE

2.0.0

IDE and compiler

STM32Cube AI Studio

1.2.0

AI model validation and C code generation

ST Edge AI Core

4.0.0

AI model validation and C code generation (core component)

You also need a B-U585I-IOT02A board with a micro-USB cable.

Part 1 — Generate AI model C-code with STM32Cube AI Studio

1. Getting the model

For this tutorial, we will download an Arc Fault Detection model in .tflite format from the STM32 Model Zoo. The STM32 Model Zoo is an open-source repository of pre-trained, STM32-optimized AI models for various applications including predictive maintenance, audio, and more. Each model comes with training scripts and configuration files.

The chosen model is a 1D convolutional neural network that detects arc faults in electrical installations from time-series current samples, post-training quantized to INT8 for efficient deployment on STM32.

Download it from the STM32 Model Zoo: st_conv_time_1channel_512_int8.tflite

2. Create a project in STM32Cube AI Studio

At this step, we will create an STM32Cube AI Studio project to convert our .tflite model to C code.

STM32Cube AI Studio converts your model ( .tflite or .onnx) into C code optimized for STM32 microcontrollers. AI model inferences run entirely in software on the Cortex-M33 core. The Application variant produces not only the raw network files but also the middlewares and the application source code to run the model.

  1. Open STM32Cube AI Studio.

  2. Create a new project clicking + Project on the top right.

  3. Select From Scratch project creation option.

  4. Set a name for your project (Test_CubeAIStudio_ArcFault_U5 in this tutorial). Click Next.

  5. Set the target development board: B-U585I-IOT02A. Click Next.

  6. Select the toolchain you will use for building your AI project. For this tutorial, select STM32CubeIDE. Click Create Project. Your STM32Cube AI project is now created:

    cubeai-studio-blank-project

3. Generate AI model code in STM32Cube AI Studio

  1. On the right bar in Model section, click Model file field to select your model (st_conv_time_1channel_512_int8.tflite).

  2. You will now validate your model to check its compatibility for C-code conversion and memory size. For this, click Run:

    cubeai-studio-run-validation
  3. When the validation is successful, click the gear icon on the top right to open Board settings. Check the default settings. The UART interface will be used to set the communication between the board and the host PC. Then, click Save.

    • UART interface: USART1

    • Baudrate: 115200

    • RX pin: PA10

    • TX pin: PA9

    cubeai-studio-board-settings
  4. Click Generate code to generate your neural network embedded C code that you will add to your application.

    cubeai-studio-generate-code
  5. Select the Application variant, keep Generate C code, then click Generate. On top of the AI part, the Application variant adds a top level code running an inference each 5 seconds and outputting its execution time.

    cubeai-studio-application-variant
  6. After the generation, click Download your C model. A .zip archive (named Test_CubeAIStudio_ArcFault_U5-run-1-model-network-ApplicationTemplate-code.zip in this tutorial) is produced containing C-code model files and dependencies. Model integration in firmware project will be done in Part 3 of this tutorial.

Part 2 — Create the base firmware project with STM32CubeMX

In this part we will use STM32CubeMX to initialize board peripherals and generate the firmware project for STM32CubeIDE.

A ready-to-use .ioc file with all the settings described below is available for download. You can use it directly in STM32CubeMX instead of configuring the project from scratch. All the steps of this Part 2 are nonetheless detailed below so you can understand how to proceed by yourself and adapt the configuration to your own application.

1. Create a project from the B-U585I-IOT02A board selector

Starting from the board selector pre-fills all board-specific hardware and software settings allowing to run your AI application. Only the needed peripherals will be enabled.

  1. Open STM32CubeMX and click Access to Board Selector.

    cubemx-board-selector
  2. In the Commercial Part Number field, enter B-U585I-IOT02A. Select the item in the Board List.

    cubemx-create-project-board-selector
  3. Click Start Project.

  4. When asked to initialize all peripherals with their default mode, click No.

Note

Declining auto-initialization avoids enabling peripherals that are not needed and that could create pin conflicts.

  1. When asked for the TrustZone feature, select without TrustZone activated and click OK.

Note

TrustZone is a security feature that allows to separate the application into two domains: secure and non-secure. It is not needed for the simple AI inference application described in this tutorial.

2. Configure Connectivity: USART1

USART1 is used to print inference results to a serial terminal through Virtual COM Port (115200 bauds).

  1. In Connectivity, select USART1.

  2. Set the mode as Asynchronous.

    cubemx-usart1-asynchronous-mode

3. Configure Clock

Set the system clock to the maximum frequency for best inference throughput.

Switch to the Clock Configuration tab and check that Cortex System timer clock is set at maximum frequency ( 160 MHz):

cubemx-clock-configuration

4. Configure Project Manager: appli structure, STM32CubeIDE

  1. Switch to the Project Manager tab.

  2. Set a Project Name (Test_CubeAIStudio_ArcFault_U5 in this tutorial).

  3. Set Application Structure to Basic.

  4. Set Toolchain / IDE to STM32CubeIDE.

    cubemx-project-manager

5. Generate code and open the firmware project

Click Generate Code, then click Open Project in the information window. It imports and opens your firmware project in STM32CubeIDE.

Part 3 — Deploy AI model in firmware project with STM32CubeIDE

In this part, we will integrate the AI model C-code generated in Part 1 into the firmware project generated in Part 2. We will also configure the project settings and edit the main codes to run inferences on the STM32N6570-DK board.

1. Copy AI files to the firmware project

In your file explorer, unzip the archive generated by STM32Cube AI Studio in Part 1 of this tutorial (named Test_CubeAIStudio_ArcFault_U5-run-1-model-network-ApplicationTemplate-code.zip). The folder contains three folders:

  • ai_generated_network/ — low-level model C code (network weights, operator calls)

  • AI/App/ — application-level code (init, process, I/O wrappers)

  • Middlewares/ST/AI/ — AI software inference runtime library

In STM32CubeIDE, in the firmware project (named Test_CubeAIStudio_ArcFault_U5 in this tutorial):

  1. Directly in Test_CubeAIStudio_ArcFault_U5, create a folder named AI.

  2. In this new AI folder, copy paste .c/.h network files from Test_CubeAIStudio_ArcFault_U5-run-1-model-network-ApplicationTemplate-code/ai_generated_network:

    • network.c

    • network.h

    • network_data.c

    • network_data.h

    • network_details.h

  3. In Src, copy paste .c files located in Test_CubeAIStudio_ArcFault_U5-run-1-model-network-ApplicationTemplate-code/AI/App:

    • app_x-cube-ai.c

    • network_weights.c

    • user_init.c

  4. In Inc, copy paste .h files located in Test_CubeAIStudio_ArcFault_U5-run-1-model-network-ApplicationTemplate-code/AI/App:

    • app_config.h

    • app_x-cube-ai.h

    • bsp_ai.h

    • network_weights.h

    • user_init.h

  5. Directly in Test_CubeAIStudio_ArcFault_U5, copy paste Middlewares folder from Test_CubeAIStudio_ArcFault_U5-run-1-model-network-ApplicationTemplate-code.

  6. The resulting project tree should look like:

3. Edit main.c: add AI init and process calls

Open Src/main.c and add:

  • In /* USER CODE BEGIN Includes */:

    #include "app_x-cube-ai.h"
    
  • In /* USER CODE BEGIN 2 */:

    // Load model weights into memory and configure the AI runtime
    STM32CubeAI_Studio_AI_Init();
    
  • In /* USER CODE BEGIN WHILE */:

    // Run one complete inference per main-loop iteration and output the result over USART1.
    STM32CubeAI_Studio_AI_Process();
    

You can now build the project (Project → Build Project).

4. Flash and run

You can use STM32CubeIDE ’s built-in run/debug functionality to flash and run the application.

  1. Connect the board to your PC using the micro-USB cable on the STLK USB port.

  2. Right-click the project → Run As → STM32 C/C++ Application.

  3. When the launch configuration dialog opens, click OK. STM32CubeIDE flashes the binary and starts the application.

  4. Open a serial terminal and connect to the board through Virtual COM Port (speed: 115200 bauds). Inference output is displayed on the serial console every 5s (inference time and number of CPU cycles).

Next Steps

  • Validate model accuracy — in STM32Cube AI Studio, run an on-target validation to compare C model performance metrics against the original model and measure inference time.

  • Explore other AI models — download pre-trained STM32-optimized models from the STM32 Model Zoo

  • Customize the application — add sensor input for a full end-to-end inference pipeline

Troubleshooting

  • Build error — missing headers: Verify that all files from ai_generated_network/ were copied to AI and that ../AI is in the include paths and added to source locations.

  • Runtime library not found at link time: Verify that ../Middlewares/ST/AI/Lib is set as a Library search path in the project linker settings, and that the library filename listed in Libraries matches the actual .a file in Middlewares/ST/AI/Lib.

  • ``STM32CubeAI_Studio_AI_Init`` undefined reference: Confirm all files from Test_CubeAIStudio_ArcFault_U5-run-1-model-network-ApplicationTemplate-code/AI/App/ (app_x-cube-ai.c, etc.) were copied to Src and Inc and that ../AI is in the include paths and added to source locations.

  • Board not detected by STM32CubeIDE: Verify the USB cable is connected to the STLK USB port and that the ST-LINK firmware is up to date (use STM32CubeProgrammer to update it if needed).

  • Application does not print inference results: Confirm the correct COM port is selected in the serial terminal and that the baud rate is set to 115200.

Assets

  • Arc_Fault_Detection_U5.ioc - download the .ioc configuration file and open it with STM32CubeMX.

  • AI model (.tflite) — obtain the model from the STM32 Model Zoo. This tutorial was validated with STM32Cube AI Studio v1.2.0.