Simulink code generation for STM32: how to generate the .bin file?

Hi everyone,
I have bought a STM32F4-Discovery Board DTM32F429I-Disc1, and my intention is to work in Matlab-Simulink.
This is the process I have followed:
  1. INSTALLATION: Matlab/Simulink R2018b 64bit with toolboxes like Embedded Coder
  2. INSTALLATION: Matlab/Simulink HW support package "Embedded Coder Support Package for STMicroelectronics Discovery Boards"
  3. INSTALLATION: STM32-MAT/TARGET from ST
  4. INSTALLATION: STM32CubeMX from ST
  5. INSTALLATION: STM32CubeIDE_1.3.0 from ST
Within STM32CubeMX, generate the .ioc file for the right board/MCU in order to blink the green LED:
  1. Give it the name "BlinkLED"
  2. PG13 as GPIO_Output and with a name "GreenLED"
  3. Select Toolchain\IDE = STM32CubeIDE (Project Manager \ Code Generator)
Within Matlab-Simulink:
  1. Add the STM32-MAT/TARGET path to Matlab's path (pathtool)
  2. Create a new simulink model
  3. Within the configuration parameters:
  4. Solver: Fixed-step, stop time = inf, step time = 0.001
  5. Code generation: Select the target file as "stm32.tlc" (This takes a while...)
  6. Code generation: Within the STM32 options, update the STM32CubeMx path
  7. Open the Simulink library and at the bottom should be the library for "Target support package - STM32 adapter"
  8. Select block "STM32_Config" and link to the .ioc file generated within STM32CubeMx
  9. Select block GPIO_Write and configure the pin 13
  10. Build (Ctrl+B)
  11. SUCCESSFUL!
However, I can not find a .bin file that can programmed on the board using the "ST-Link Utility". Any ideas?
Other option is to open the .project generated within STM32CubeIDE, but there are seem to be some files missing?
Thanks a lot

3 Comments

Hello everyone,
Same problem here (see second paragraph).
@Dionisio Cagigas Fojaco: I don't think MATBLAB Embedded Coder or any of the required additional tools within MATLAB generate binary files that can be flashed onto the microcontroller. You need to compile the project with an IDE first. That will create the .bin file.
I experience the exact same problem with the STM32CubeIDE missing files. Now what I have occasionally had luck with is to manually move files. In detail:
  • Within your root project folder go to the directory "<projectName>/Src" (these files habe been created by MATLAB, without STM32CubeMX doing anything to them)
  • Copy all those files to the default "src" folder of the STM32CubeIDE project.
  • Now hit "Run" again and it should compile with no errors.
However as soon as interrupts (from what I can tell at least with external interrupts) are coded into the programm, the whole thing does not work anymore. At least that is the behaviour I observed so far. To make sure STM32CubeIDE can find all necessary files while compiling I double checked the include paths. All necessary paths are stated but files can, for what ever reason, not be found anyways.
Does anyone know how to properly set up the toolchain MATLAB Simulink -> STM32CubeMX -> STM32CubeIDE? I think neither Dionisio Cagigas Fojaco nor me have had problems with the first two.
Help is greatly appreciated! Thanks! :)
Hello,
I also found problems to complete the generated code, as reported by @Pascal Weller. The code generated by simulink was produced separately from the codes generated by STM32cubeMx and another by simulink, for example two main files were generated. I used STM32cubeMx version 6.1.1
Would you remember which version you used @Pascal Weller was?
To solve the problem I revert the version of STM32CubeMx to version 5.6.0. The .c and .h files are generated correctly, but in the STM32CubeIDE interface it is necessary to add to the project the folders generated by the simulink code generator. After that the compilation takes place correctly.
Maybe you already solved the problem, but this solution can help others maybe...
Hello,
Unfortunately, I do not remember which version I used back then. Anyways, thanks for your input on the topic.
Best,
Pascal

Sign in to comment.

Answers (2)

Hi everyone and @Pascal Weller,
I have managed to get the LED blinking.
These are the things I had to modify:
STM32CubeMX
  • Select: ProjectManager\Project\Toolchain\IDE = STM32CubeIDE
  • Select: Pinout&Configuration\SystemCore\NVIC\CodeGeneration\TimeBase:SystemTickTimer -> TICK ON "Generate IRQ handler"
Matlab\Simulink
  • Before launching the build of the Simulink model (Ctrl+B), create a separate folder, and go there (make it your working directory within Matlab)
  • Now, go to Simulink, and build (Ctrl+B)
  • This will prevent files getting mixed-up
  • If the build is successful, within the folder in which the .slx model is, a .project file will be generated. This is the project to be opened in STM32CubeIDE
STM32CubeIDE
  • Within the project tree (on the left hand side), navigate to the sub-folder of the project called like the simulink .slx model (it should contain a "Inc" and "Src" sub-folders)
  • Right click -> Properties -> C\C++ build -> Un-tick the option "exclude resource from build"
  • Connect the board
  • Within the project tree, go to the top level, right-click and "Debug-as"
  • This will generate the "Debug" folder, and within it, the .bin\.elf files
Hope it helps

4 Comments

First of all thank you for that rocket-fast answer.
It has indeed been the "Exclude resource from build" checkbox that was giving me headaches for a good two days now. With that the first and probably most severe problem is solved. The whole toolchain now works as intendet. Excellent!
However, I still can not get the built-in LED (NUCLEO-F334R8) to toggle whenever I press the built-in user button (even though the IRQ handler is activated in the STM32CubeMX program - I am using the NUCLEO-F334R8 preset). Below pictures of the Simulink model (left) and the function call subsystem (right). The checkbox "Toggle Pin 5" in the GPIO_Write block parameters is checked.
What do I do wrong?
Thank you for helping out!
Hi again, I would suggest just trying to blink the LED, without the push-button and the "toggle" functionality. Something like this:
Within STM32CubeMX, make sure you configure the GPIO like this (not sure if it is also your default):
In my case the Green LED is in the PG13.
Hi Dionisio Cagigas Fojaco, I am using Nucleo F746ZG LQFP144. I followed the same procedure as you mentioned. But the problem is simulink is creating the .c and .h files but they are not generating on my ide.(Keil or stm32cubeide). Please help how to run those files(.c and .h) on the ide.
Thanks alot .
similar problem here:
my procedure is this
1)Open CubeMX and create a simple project with atollic truestudio as toolchain, then i don't create the project just save the .ioc file in my workspace
2)open matlab, create a folder in the main workspace and in it i create an slx model
3)open simulink, in model configuration parameters:
stop dime=inf
fixed step
sample time= 1e-3
code generation:
system target file= stm32.slc
STM32 options:
tick on stm32cubeMX path
4)stm32_Config block i select the .ioc file
5)make a simple model of a blinking led
6)save
7)apps->Embedded Coder->Custom Target and select again stm32.slc
8)Build
9) at this point i got a project with the main.c exactly as cubeMX had create it with the while(1) empty
10)now i can build and debug but of course the code don't do anything, because the function in the .c file with the simulink progect is never called
EDIT:
i 've solved selecting BASIC from application structure in cubeMX
actyally you need just to select BASIC from application structure, even if you have teh latest version of cubeMX, at leas it worked for me.

Sign in to comment.

Products

Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!