Code Generation For Stm32 Mcus Using Matlab and Simulink: March 2020
Code Generation For Stm32 Mcus Using Matlab and Simulink: March 2020
simulink®
March 2020
Systems check
• Mandatory Software
• From Mathworks
• MATLAB®, Simulink and Embedded Coder (2018B)
• From STMicroelectronics
• STM32CubeMX (5.6.0)
• STM32-MAT/TARGET toolkit to develop STM32 applications (STM32MatTarget_5.6.0_setup.zip)
• Hardware
• Any STM32 based electronic application board with SWD/JTAG connection.
• STLinkV2/V3 or 3rd party debug probe
• Virtual Com Port implemented between STM32 and Computer
2
Key information
• Arm®/ST/MathWorks initiative
• Optimized Cortex-M code for Embedded Coder based on Arm’s CMSIS DSP SW library
• Simulink Peripheral blockset provided by ST based on STM32Cube abstraction layers supporting most of
STM32 portfolio
• Compiler / IDE integration for MATLAB, based on Arm Keil , IAR and STM32CubeIDE
3
Why matlab/simulink on STM32 ?
4
Quick description of tools
Toolchain
5
Using scenarios and tools
• Step 1: Pure simulation MATLAB/
• Everything done on the PC Simulink
6
Step 1: pure simulation
MATLAB® & simulink
• Not a subject for this presentation : You do your model ready, simulate and test it
on the PC.
8
Step 2: processor-in-the-loop
PIL overview
• PIL is used to run MATLAB® algorithm on STM32 device
• To see if STM32’s computational results are numerically
equivalent to PC-based simulation results.
• To measure directly the time to execute the Simulink
model steps (profiling)
• PIL simulation uses serial port to send data from Simulink to
STM32 and receive Simulink processed data back from
STM32
• PIL simulation doesn’t run in «real-time»
• Simulink is «master» sending asynchronously data to STM32 through
serial port.
• STM32 device waits for data from Simulink, process data (in real-time)
and sends result back to Simulink through serial-port.
10
Simulink PIL start
• Set simulation duration time and click run simulation green button
• Example: Simulate 1 second (given 1000 values from testdata, one for each 1 ms)
11
Code generation overview
• PIL referenced model runs into STM32 target while the simulation model runs on
PC
• Automatic conversion of PIL model to .c/.h files
• Automatic call to STM32CubeMX to create project
• Manually build and download project to STM32 target from selected IDE
12
Step 3: everything on STM32
Handle data through real peripherals
14
Build application
15
MATLAB® code generation
16
STM32CubeMX code generation
• STM32CubeMX process
• STM32CubeMX is automatically called from MATLAB®
when «Download Application» has been selected
from the Model Configuration window.
• STM32CubeMX adds necessary library files. .c/.h library files from HAL STM32 libraries.
• STM32CubeMX generates toolchain project including files generated from MATLAB® Click « Open Project » to
automatically open project
using selected toolchain.
17
Step 4: external mode
Simulink external mode
• Monitoring
• As long as STM32 application is running, Simulink model parameters can be edited
• Monitoring means that only parameters can be modified. C generated code can't be modified. For
example, it is not possible to replace x with + as x C code function has been generated.
19
Summary
20
Summary
Thank you