Welcome readers, In this blog, we will be going through step by step procedure for programming STM32 using Simulink. Before going directly to the steps, let's discuss why we need to program STM32 using Simulink? STM32 uses ARM cortex M architecture. It is used in many applications because of its lower cost and good performance. Programming STM32 using Arduino IDE is pretty straightforward as there are many libraries developed by the people. But when you start developing complex embedded or control systems, writing bare-metal code using Arduino IDE is a hectic thing. Here comes the Simulink to make this process easier. You can develop the complex systems using blocks and test them using Simulink functionalities and then generate a "c" code which can be compiled and generate a binary file so that this binary file can be directly flashed into the STM32. That's it done now the complex system is deployed at the physical level. In this blog, we will be using Simulink to develop the model and embedded coder to generate the "c" code for the same model, STM32CubeMX, for developing a configuration file (.ioc) and MDK-ARM from Keil to build the project and flash it into the controller. We will be using STM32f103c8t6, also known as Blue Pill, as a target MCU.
Let's go through the required software and packages:
- From MATLAB:
- MATLAB Coder (Add-On)
- Simulink Coder (Add-On)
- Embedded Code (Add-On)
- From STMicroelectronics:
Update: [Feb 2022]
STM32-MAT/TARGET has been removed from the respective website. We have uploaded the file used and provided by Author in Google Drive.
- Any one of the following Toolchain:
- STM32CubeIDE from STMicroelectronics
- MDK-ARM from Keil
- EWARM from IAR
- STM32F103C8T6 (Blue pill) microcontroller
- STLink v2
- 4 × female to male jumpers
Let's go through the process in a step-by-step manner.
Step-1: Set STM32-MAT software path in MATLAB
After installing all the software and add-ons, open MATLAB, select the "set path" option and select the folder created at the location "c:/MATLAB/STM32-MAT/STM" during the installation of the STM32-MAT/TARGET toolkit.
Note: If you didn't find the STM32 folder at the above-specified path, then ensure that STM32-MAT/TARGET toolkit is installed correctly. Click here to download the utility.
Step-2: Create a project in STM32CubeMX
Now open STM32cubeMX and create a new project by selecting "ACCESS TO MCU SELECTOR".
In MCU/MPU selector, enter the part number as "STM32F103C8T6" and select the "STM32F103C8TX" option in the list, and click start project.
Step-3: Configure the clock and GPIO pins
After clicking start project, you will be getting Pinout view. Now set up the clock. In system core, select the "RCC" option and then at the "High-Speed Clock" section select "Crystal/Ceramic Resonator".
Now set the GPIOs. Select "PC13" and set it as "GPIO_Output".
Now select the GPIO option in the System Core section and then select the GPIO and check the modified check box. Now change the Pin configurations as shown in the below image.
Step-4: Project Manager & Generate the Code
Now open Project Manager, edit the details as shown in the figure, and click generate the code.
Close the opened dialogue box that is popped out after clicking the code generation tab and open MATLAB.
Step-5: Create a model in the Simulink
Create a blank model in the same folder where the "blink.ioc" file is saved, as shown in the below figure.
Now open the Model Settings from the simulation tab and change the parameters in the solver section, as shown in the below figure.
Open the Code Generation tab and select the "STM32.tlc" option as a System target file using the browse option. Now select STM32 Options under the Code Generation section.
Check the "STM32CubeMx path update" check box and select the "Update installed path" option. Now apply the changes and close the Model Settings dialog box.
Step-6: Insert STM32_Config block in the model
Now open "Library Browser" and select "STM32_Config" block, add this block to the model.
Note: If you didn't find the "Target Support Package – STM32 Adapter" option, then ensure that STM32-MAT/TARGET toolkit is installed correctly. Click here to download & install it properly.
Double click on the "STM32_Config" block, select the "Select STM32 configuration file" option in the opened dialog box. Now select the ".ioc" file that is created in step 4.
Step-7: Create the model
Complete modeling by selecting and arranging the same blocks as shown in the below figure.
Following are the pulse block parameters.
Step-8: Compile and generate the code
After completing modeling, compile the model by selecting the "Update Model" option under the "Modeling" tab.
After compilation, go to the "APPS" section and select Embedded Coder.
Now Build the project (it might take some time). In the second box shown in the below figure is the code report generated by the Embedded coder.
Step-9: Upload the code into the hardware
After code report generation, open "STM32CubeMx" and select the "Generate Code" option, Now a dialogue box options. Select the "Open Project" option.
Before opening MDK-Keil IDE, let's go through the hardware.
Before flashing the binaries into the STM32 toggle the "Boot0" pin of the STM32 to" HIGH". Connect the connections between STM32 and the STLink v2 as shown in the below figure.
Now open MDK-Keil IDE and Build the project. After this, you will be getting '0' errors and '0' warnings. Now Load the model into the STM32. You will be getting a "finished loading" message in the console.
Now toggle back the "Boot0" back to "LOW" and press the reset button as shown in the bellow figure.
Now the Inbuilt LED starts blinking.
"Simulink" and "Embedded Coder" made prototyping complex embedded systems and control systems easier. We can test the system or model in Simulink for the errors and performance before deploying the binaries of the model into the hardware.
Get instant access to the code, model, or application of the video or article you found helpful! Simply purchase the specific title, if available, and receive the download link right away! #MATLABHelper #CodeMadeEasy
Ready to take your MATLAB skills to the next level? Look no further! At MATLAB Helper, we've got you covered. From free community support to expert help and training, we've got all the resources you need to become a pro in no time. If you have any questions or queries, don't hesitate to reach out to us. Simply post a comment below or send us an email at [email protected].
And don't forget to connect with us on LinkedIn, Facebook, and Subscribe to our YouTube Channel! We're always sharing helpful tips and updates, so you can stay up-to-date on everything related to MATLAB. Plus, if you spot any bugs or errors on our website, just let us know and we'll make sure to fix it ASAP.
Ready to get started? Book your expert help with Research Assistance plan today and get personalized assistance tailored to your needs. Or, if you're looking for more comprehensive training, join one of our training modules and get hands-on experience with the latest techniques and technologies. The choice is yours – start learning and growing with MATLAB Helper today!
Education is our future. MATLAB is our feature. Happy MATLABing!
it is easier for us by reading this article for research work
It is very helpful. I need some help on how to work with I2C,SPI devices?