0
0
SimulinkHow-ToBeginner · 4 min read

How to Deploy Simulink Model to STM32 Microcontroller

To deploy a Simulink model to an STM32 microcontroller, first install the STM32 support package and Embedded Coder. Then configure the model for code generation targeting STM32 hardware, build the code, and flash it to the device using the provided tools.
📐

Syntax

Deploying a Simulink model to STM32 involves these main steps:

  • Install Support Packages: STM32 hardware support and Embedded Coder.
  • Configure Model: Set hardware board to STM32, enable code generation.
  • Build Code: Generate C code from the model.
  • Deploy: Flash the generated code to STM32 using the programmer.
matlab
%% Step 1: Open Simulink model
open_system('your_model_name')

%% Step 2: Set hardware board
set_param('your_model_name', 'HardwareBoard', 'STMicroelectronics->STM32')

%% Step 3: Configure code generation
set_param('your_model_name', 'SystemTargetFile', 'ert.tlc')

%% Step 4: Build and deploy
rtwbuild('your_model_name')
💻

Example

This example shows how to configure and deploy a simple Simulink model named stm32_blinky that toggles an LED on STM32.

matlab
%% Open the model
open_system('stm32_blinky')

%% Set STM32 as hardware board
set_param('stm32_blinky', 'HardwareBoard', 'STMicroelectronics->STM32')

%% Use Embedded Coder target
set_param('stm32_blinky', 'SystemTargetFile', 'ert.tlc')

%% Build and deploy to STM32
rtwbuild('stm32_blinky')
Output
### Build Summary for stm32_blinky ### Build completed successfully. Code generated and flashed to STM32 device.
⚠️

Common Pitfalls

  • Missing Support Packages: Not installing STM32 hardware support or Embedded Coder causes build errors.
  • Incorrect Hardware Board: Setting wrong board in model config leads to incompatible code.
  • Code Generation Errors: Model blocks not supported for code generation can fail the build.
  • Flashing Issues: STM32 device not connected or drivers missing prevents deployment.
matlab
%% Wrong way: No hardware board set
set_param('stm32_blinky', 'HardwareBoard', '')
rtwbuild('stm32_blinky')

%% Right way: Set correct STM32 board
set_param('stm32_blinky', 'HardwareBoard', 'STMicroelectronics->STM32')
rtwbuild('stm32_blinky')
Output
Error: Hardware board not specified. ### After correction ### Build completed successfully.
📊

Quick Reference

StepActionDescription
1Install Support PackagesSTM32 hardware support and Embedded Coder from MATLAB Add-Ons
2Configure ModelSet Hardware Board to STM32 and System Target File to ert.tlc
3Build ModelRun rtwbuild to generate and compile code
4Deploy CodeFlash generated code to STM32 using built-in programmer

Key Takeaways

Install STM32 hardware support and Embedded Coder before deployment.
Set the Simulink model hardware board to STM32 for correct code generation.
Use rtwbuild to generate and flash code to the STM32 device.
Check for unsupported blocks to avoid code generation errors.
Ensure STM32 device is connected and drivers are installed for flashing.