Main Content

Getting Started with STMicroelectronics STM32 Processor Based Boards

This example shows how to use the Embedded Coder® Support Package for STMicroelectronics® STM32 Processors to run a Simulink® model on an STMicroelectronics STM32 Processor Based hardware board.

Introduction

The support package enables you to use Simulink blocks to access peripherals during run time and use the STM32CubeMX tool to configure peripherals on the processor.

In this example, you will learn how to configure a Simulink model with single and varying rates to generate code and periodically switch the blue LED light on a STMicroelectronics NUCLEO-F429ZI board.

Prerequisite

Complete the following tutorials:

Required Hardware

To run this example you need the following hardware:

  • STMicroelectronics NUCLEO-F429ZI board

  • Micro USB cable

Run Hardware Board Setup

Once you have installed the support package, perform the hardware board setup process to register the STMicroelectronics software tools required to generate code and deploy code to the connected STMicroelectronics STM32 hardware board. If you have already installed the support package, you can start the hardware setup by using the Add-On Manager. Alternatively you can run the following command to launch the setup from MATLAB® Command window.

stm32cube.tools.launchHardwareSetup

Task 1 - Create a STM32CubeMX Project to Configure Peripherals

Support for STM32 Processor Based Board with Simulink uses STM32CubeMX graphical tool. STM32CubeMX tool is a graphical interface that you can use to configure peripherals of STM32 processors and generate peripheral initialization code for the STM32 processors that you select. For more information on the STM32CubeMX tool, see STM32CubeMX overview.

You can choose an existing STM32CubeMX project or create a new one. You can launch the project from your Simulink model. Follow the following instructions to create a model to support your STM32 processor based board:

1. Open the Modeling tab and press CTRL+E to open the Configuration Parameters dialog box. Go to Hardware Implementation > Hardware board.

2. Select STM32 Processor Based board.

3. Go to Build options and click Browse to select an existing STM32CubeMX project file.

4. Click Create to create a new STM32CubeMX project file.

  • Provide a project name with an .ioc extension.

  • Browse to the desired folder location and save the project.

  • Select the STM32 hardware and click Apply and OK.

You can also create a new project by selecting custom STM32 Processor Based hardware.

Task 2 - Configure STMicroelectronics STM32CubeMX Project in Simulink to Make LED Blink at Single Rate

In this task, configure the target model to make the blue LED light blink at a single sample rate that you specify in the model.

1. Open the stm32_gettingstarted.

2. Configure the GPIO block. Double-click the block to open the block parameter dialog box.

  • Blue LED is connected to pin 7 of the GPIOB port.

3. Create a new STM32CubeMX project or browse to an existing STM32CubeMX project. Launch the STM32CubeMX project in the STM32CubeMX tool. For details, see Task 1.

4. Configure the same pin (PB7) as GPIO_Output in your STM32CubeMX project to toggle user LED.

5. In your STM32CubeMX project, ensure to perform the following configurations:

  • Enable Do not generate the main() under Project Manager > Project.

  • Disable Generate under root under Project Manager > Project.

  • Under Project Manager > Advanced Settings > Driver Selector, select low level (LL) drivers for the peripherals.

  • Under Project Manager > Advanced Settings > Generate Functions Calls, deselect Do Not Generate Function Calls for all the peripheral initialization function calls.

  • Under Project Manager > Advanced Settings > Generate Function Calls, deselect Visibility (Static) for all the peripheral initialization function calls.

  • Save the project.

Generate Code and Load on Hardware Board from Simulink Model

1. To generate the code for the model, press Ctrl+B or click Build, Deploy & Start.

2. Follow the build process by opening the diagnostic viewer using the link provided at the bottom of the model canvas. After you load the code on the board, a blue LED blinks on the hardware board, indicating that the code is running.

Task 3 - Configure STMicroelectronics STM32CubeMX Project in Simulink Modeling to Make LED Blink at Varying Rate

In this task you will configure the target model to blink a blue LED at a varying rates with different sample times. You will learn how to model in the multitasking mode.

1. Open the stm32_multitasking_gettingstarted target model.

2. Open the Modeling tab and press CTRL+E to open Configuration Parameters dialog box. Go to Solver and enable the Treat each discrete rate as separate task and Automatically handle rate transition for data transfer parameters.

3. Configure the blocks. Double-click the block to open the block parameter dialog box.

  • User button (blue button) is connected to Pin 13 of GPIOC port (PC13).

  • The blue LED is connected to pin 7 of GPIOB port (PB7).

4. Create a new STM32CubeMX project or browse to an existing STM32CubeMX project. Launch the STM32CubeMX project in the STM32CubeMX tool. For details, see Task 1.

5. Configure PC13 pin as GPIO_Input or GPIO_EXTI functionality in the STM32CubeMX project just as you did in the single rate model. For details, see Task 2.

6. In your STM32CubeMX project, ensure to perform the following configurations:

  • Enable Do not generate the main() under Project Manager > Project.

  • Disable Generate under root under Project Manager > Project.

  • Under Project Manager > Advanced Settings > Driver Selector, select low level (LL) drivers for the peripherals.

  • Under Project Manager > Advanced Settings > Generate Functions Calls, deselect Do Not Generate Function Calls for all the peripheral initialization function calls.

  • Under Project Manager > Advanced Settings > Generate Function Calls, deselect Visibility (Static) for all the peripheral initialization function calls.

  • Save the project.

Generate Code and Load on Hardware Board from Simulink Model

1. To generate the code for the model, press Ctrl+B or click Build, Deploy & Start.

2. Follow the build process by opening the diagnostic viewer using the link provided at the bottom of the model canvas. After you load the code on the board, a blue LED blinks on the hardware board, indicating that the code is running.

Review Block Library for Your Board

The Embedded Coder Support Package for STMicroelectronics STM32 Processors enables you to create algorithms that use peripherals supported with dedicated blocks that can be added to your Simulink model. Use the STM32 Processor Based driver blocks to configure the associated sensors and actuators and read data from a peripheral or send data to a peripheral.

Run slLibraryBrowser in MATLAB Command window to open the Simulink Library Browser. In the opened Simulink Library Browser, you can find STM32 Processor Based Board driver library under Embedded Coder Support Package for STMicroelectronics STM32 Processors.

Other Things to Try

  • Try to read the push button at slower rates and analyze the behavior.

  • Run the example with the Hardware Interrupt block.

More about