Friday, May 17, 2024
HomeMatlabWeblog | Programming STM32 Utilizing Simulink

Weblog | Programming STM32 Utilizing Simulink


Introduction

Welcome readers, On this weblog, we will probably be going via step-by-step process for programming STM32 utilizing Simulink. Earlier than going on to the steps, let's talk about why we have to program STM32 utilizing Simulink? STM32 makes use of ARM cortex M structure. It's utilized in many functions due to its decrease price and good efficiency. Programming STM32 utilizing Arduino IDE is fairly easy as there are lots of libraries developed by the individuals. However while you begin creating advanced embedded or management programs, writing bare-metal code utilizing Arduino IDE is a busy factor. Right here comes the Simulink to make this course of simpler. You possibly can develop the advanced programs utilizing blocks and take a look at them utilizing Simulink functionalities after which generate a "c" code which might be compiled and generate a binary file in order that this binary file might be instantly flashed into the STM32. That is it achieved now the advanced system is deployed on the bodily stage. On this weblog, we will probably be utilizing Simulink to develop the mannequin and embedded coder to generate the "c" code for a similar mannequin, STM32CubeMX, for creating a configuration file (.ioc) and MDK-ARM from Keil to construct the undertaking and flash it into the controller. We will probably be utilizing STM32f103c8t6, also called Blue Capsule, as a goal MCU.

Supplies Required

Let's undergo the required software program and packages:

  • From MATLAB:
    • MATLAB Coder (Add-On)
    • Simulink Coder (Add-On)
    • Embedded Code (Add-On)
  • From STMicroelectronics:

STM32-MAT/TARGET has been faraway from the respective web site. Now we have uploaded the file used and supplied by Creator in Google Drive.

  • Any one of many following Toolchain:

{Hardware} Required:

  • STM32F103C8T6 (Blue tablet) microcontroller
  • STLink v2
  • Breadboard
  • 4 × feminine to male jumpers

Let's undergo the method in a step-by-step method.

Step-1: Set STM32-MAT software program path in MATLAB

After putting in all of the software program and add-ons, open MATLAB, choose the "set path" choice and choose the folder created on the location "c:/MATLAB/STM32-MAT/STM" through the set up of the STM32-MAT/TARGET toolkit.

Set path for STM32-MAT files

Set path for STM32-MAT information in MATLAB

Select STM32-MAT software files

Choose STM32-MAT software program information

Notice: When you did not discover the STM32 folder on the above-specified path, then be sure that STM32-MAT/TARGET toolkit is put in accurately. Click on right here to obtain the utility.

Step-2: Create a undertaking in STM32CubeMX

Now open STM32cubeMX and create a brand new undertaking by choosing "ACCESS TO MCU SELECTOR".

Create a project in STM32CubeMX

Create a undertaking in STM32CubeMX

In MCU/MPU selector, enter the half quantity as "STM32F103C8T6" and choose the "STM32F103C8TX" choice within the checklist, and click on begin undertaking.

Select the MCU

Choose the MCU

Step-3: Configure the clock and GPIO pins

After clicking begin undertaking, you may be getting Pinout view. Now arrange the clock. In system core, choose the "RCC" choice after which on the "Excessive-Velocity Clock" part choose "Crystal/Ceramic Resonator".

Set the clock as Crystal/Ceramic Resonator

Set the clock as Crystal/Ceramic Resonator

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

Select the GPIOs and set as  output

Choose the GPIOs and set as output

Now choose the GPIO choice within the System Core part after which choose the GPIO and verify the modified verify field. Now change the Pin configurations as proven within the under picture.

Modify the GPIO configurations

Modify the GPIO configurations

Step-4: Venture Supervisor & Generate the Code

Now open Venture Supervisor, edit the main points as proven within the determine, and click on generate the code.

Project Manager

Venture Supervisor

Shut the opened dialogue field that's popped out after clicking the code technology tab and open MATLAB.

Close the dialogue box

Shut the dialogue field

Step-5: Create a mannequin within the Simulink

Create a clean mannequin in the identical folder the place the "blink.ioc" file is saved, as proven within the under determine.

Create a blank Simulink model

Create a clean Simulink mannequin

Now open the Mannequin Settings from the simulation tab and alter the parameters within the solver part, as proven within the under determine.

Configure solver parameters

Configure solver parameters

Open the Code Era tab and choose the "STM32.tlc" choice as a System goal file utilizing the browse choice. Now choose STM32 Choices beneath the Code Era part.

Change the system target file

Change the system goal file

Verify the "STM32CubeMx path replace" verify field and choose the "Replace put in path" choice. Now apply the modifications and shut the Mannequin Settings dialog field.

Update the STM32CubeMX path

Replace the STM32CubeMX path

Step-6: Insert STM32_Config block within the mannequin

Now open "Library Browser" and choose "STM32_Config" block, add this block to the mannequin.

Select the STM32_Config block

Choose the STM32_Config block

Notice: When you did not discover the "Goal Assist Package deal – STM32 Adapter" choice, then be sure that STM32-MAT/TARGET toolkit is put in accurately. Click on right here to obtain & set up it correctly.

Double click on on the "STM32_Config" block, choose the "Choose STM32 configuration file" choice within the opened dialog field. Now choose the ".ioc" file that's created in step 4.

Select the STM32 configuration file

Choose the STM32 configuration file

Step-7: Create the mannequin

Full modeling by choosing and arranging the identical blocks as proven within the under determine.

Select GPIO and Pulse Generator block

Choose GPIO and Pulse Generator block

Following are the heartbeat block parameters.

Pulse Generator block

Pulse Generator block

Step-8: Compile and generate the code

After finishing modeling, compile the mannequin by choosing the "Replace Mannequin" choice beneath the "Modeling" tab.

Compile the model

Compile the mannequin

After compilation, go to the "APPS" part and choose Embedded Coder.

Launch embedded coder app

Launch embedded coder app

Now Construct the undertaking (it'd take a while). Within the second field proven within the under determine is the code report generated by the Embedded coder.

Build the model

Construct the mannequin

Step-9: Add the code into the {hardware}

After code report technology, open "STM32CubeMx" and choose the "Generate Code" choice, Now a dialogue field choices. Choose the "Open Venture" choice.

Generate Code

Generate Code

Earlier than opening MDK-Keil IDE, let's undergo the {hardware}.

STM32F103C8T6

STM32F103C8T6

 STLINK-V2

STLINK-V2

Earlier than flashing the binaries into the STM32 toggle the "Boot0" pin of the STM32 to" HIGH". Join the connections between STM32 and the STLink v2 as proven within the under determine.

Connections between STM32 and STLink

Connections between STM32 and STLink

Now open MDK-Keil IDE and Construct the undertaking. After this, you may be getting '0' errors and '0' warnings. Now Load the mannequin into the STM32. You may be getting a "completed loading" message within the console.

Build the project and load the code into the STM32

Construct the undertaking and cargo the code into the STM32

Now toggle again the "Boot0" again to "LOW" and press the reset button as proven within the bellow determine.

Toggle back the"boot0" pin

Toggle again the"boot0" pin

Now the Inbuilt LED begins blinking.

Conclusion

"Simulink" and "Embedded Coder" made prototyping advanced embedded programs and management programs simpler. We are able to take a look at the system or mannequin in Simulink for the errors and efficiency earlier than deploying the binaries of the mannequin into the {hardware}.

Did you discover some useful content material from our video or article and now searching for its code, mannequin, or software? You possibly can buy the precise Title, if out there, and immediately get the obtain hyperlink.

Thanks for studying this weblog. Do share this weblog if you happen to discovered it useful. When you have any queries, publish them within the feedback or contact us by emailing your inquiries to [email protected]. Comply with us on LinkedIn Fb, and Subscribe to our YouTube Channel. When you discover any bug or error on this or another web page on our web site, please inform us & we are going to right it.

If you're searching for free assist, you may publish your remark under & await any group member to reply, which isn't assured. You possibly can e book Skilled Assist, a paid service, and get help in your requirement. In case your timeline permits, we advocate you e book the Analysis Help plan. If you wish to get skilled in MATLAB or Simulink, it's possible you'll be part of one in all our coaching modules. 

If you're prepared for the paid service, share your requirement with vital attachments & inform us about any Service choice together with the timeline. As soon as evaluated, we are going to revert to you with extra particulars and the following prompt step.

Schooling is our future. MATLAB is our function. Pleased MATLABing!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments