53

Blog | Programming STM32 Using Simulink | MATLAB Helper

 3 years ago
source link: https://matlabhelper.com/blog/simulink/programming-stm32-using-simulink/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
.st0{fill:#FFFFFF;}

Programming STM32 using Simulink 

 September 3, 2021

By  Addepalli Dolendra Vikas

Need Urgent Help?

Our experts assist in all MATLAB & Simulink fields with communication options from live sessions to offline work.

testimonials

Philippa E. / PhD Fellow

I STRONGLY recommend MATLAB Helper to EVERYONE interested in doing a successful project & research work! MATLAB Helper has completely surpassed my expectations. Just book their service and forget all your worries.

Yogesh Mangal / Graduate Trainee

MATLAB Helper provide training and internship in MATLAB. It covers many topics of MATLAB. I have received my training from MATLAB Helper with the best experience. It also provide many webinar which is helpful to learning in MATLAB.

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:

  • From MATLAB:
    • MATLAB Coder (Add-On)
    • Simulink Coder (Add-On)
    • Embedded Code (Add-On)
  • From STMicroelectronics:
  • 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 in MATLAB

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

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

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

Now set the GPIOs. Select "PC13" and set it as "GPIO_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

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

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

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

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

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

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

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

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

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

Following are the pulse block parameters.

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

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

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

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

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

STM32F103C8T6

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

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

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

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.

Loved our Blog Post? Give us your valuable feedback through comments!

Thank you for reading this blog. Do share this blog if you found it helpful. If you have any queries, post them in the comments or get in touch with us by emailing your questions to [email protected]. Follow us on LinkedInFacebook, and Subscribe to our YouTube Channel. 

We will not share any code or resource which the author has not provided. If you are looking for free help, you can post your comment on the blog or video & wait for any community member to respond, which is not guaranteed. You can book Expert Help, a paid service, and get assistance in your requirement. If your timeline allows, we would recommend you to book the Research Assistance plan. If you want to get trained in MATLAB or Simulink, you may join one of our Training modules. 

If you are ready for the paid service, share your requirement with necessary attachments & inform us about any Service preference along with the timeline. Once evaluated, we will revert to you with more details and the next suggested step.

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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK