.st0{fill:#FFFFFF;}

Programming STM32 using Simulink 

 September 3, 2021

By  Addepalli Dolendra Vikas

Introduction

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.

Materials Required

Let's go through the required software and packages:

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:

Hardware Required:

  • STM32F103C8T6 (Blue pill) microcontroller
  • STLink v2
  • Breadboard
  • 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.

Set path for STM32-MAT files

Set path for STM32-MAT files in MATLAB

Select STM32-MAT software files

Select STM32-MAT software files

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".

Create a project in STM32CubeMX

Create a project in STM32CubeMX

In MCU/MPU selector, enter the part number as "STM32F103C8T6" and select the "STM32F103C8TX" option in the list, and click start project.

Select the MCU

Select the MCU

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".

Set the clock as Crystal/Ceramic Resonator

Set the clock as Crystal/Ceramic Resonator

Now set the GPIOs. Select "PC13" and set it as "GPIO_Output".

Select the GPIOs and set as  output

Select the GPIOs and set as 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.

Modify the GPIO configurations

Modify the GPIO configurations

Step-4: Project Manager & Generate the Code

Now open Project Manager, edit the details as shown in the figure, and click generate the code.

Project Manager

Project Manager

Close the opened dialogue box that is popped out after clicking the code generation tab and open MATLAB.

Close the dialogue box

Close the dialogue box

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.

Create a blank Simulink model

Create a blank Simulink model

Now open the Model Settings from the simulation tab and change the parameters in the solver section, as shown in the below figure.

Configure solver parameters

Configure solver parameters

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.

Change the system target file

Change the system target file

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.

Update the STM32CubeMX path

Update the STM32CubeMX path

Step-6: Insert STM32_Config block in the model

Now open "Library Browser" and select "STM32_Config" block, add this block to the model.

Select the STM32_Config block

Select the STM32_Config block

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.

Select the STM32 configuration file

Select the STM32 configuration file

Step-7: Create the model

Complete modeling by selecting and arranging the same blocks as shown in the below figure.

Select GPIO and Pulse Generator block

Select GPIO and Pulse Generator block

Following are the pulse block parameters.

Pulse Generator block

Pulse Generator block

Step-8: Compile and generate the code

After completing modeling, compile the model by selecting the "Update Model" option under the "Modeling" tab.

Compile the model

Compile the model

After compilation, go to the "APPS" section and select Embedded Coder.

Launch embedded coder app

Launch embedded coder app

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.

Build the model

Build the model

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.

Generate Code

Generate Code

Before opening MDK-Keil IDE, let's go through the hardware.

STM32F103C8T6

STM32F103C8T6

 STLINK-V2

STLINK-V2

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.

Connections between STM32 and STLink

Connections between STM32 and STLink

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.

Build the project and load the code into the STM32

Build the project and load the code into the STM32

Now toggle back the "Boot0" back to "LOW" and press the reset button as shown in the bellow figure.

Toggle back the"boot0" pin

Toggle back the"boot0" pin

Now the Inbuilt LED starts blinking.

Conclusion

"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!

About the author 

Addepalli Dolendra Vikas

I’m an electronics hobbyist and tech enthusiast. I keep roaming around the stuff that are related to embedded systems, IoT and robotics. I love prototyping stuff and make it work.

  • tanu jain says:

    it is easier for us by reading this article for research work

  • Semachew Fasika says:

    It is very helpful. I need some help on how to work with I2C,SPI devices?

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

    MATLAB Helper ®

    Follow: YouTube Channel, LinkedIn Company, Facebook Page, Instagram Page

    Join Community of MATLAB Enthusiasts: Facebook Group, Telegram, LinkedIn Group

    Use Website Chat or WhatsApp at +91-8104622179

    >