11 Tutorial 1: From pinout to project C code generation using an MCU of the STM32F4 series

Support of additional software components using CMSIS-Pack standard

Figure 590. Project generated with CMSIS-Pack software component

image610

11 Tutorial 1: From pinout to project C code generation

using an MCU of the STM32F4 series

This section describes the configuration and C code generation process. It takes as an example a simple LED toggling application running on the STM32F4DISCOVERY board.

11.1 Creating a new STM32CubeMX project

  1. Select File > New project from the main menu bar or New project from the Home page.

  2. Select the MCU/MPU Selector tab and filter down the STM32 portfolio by selecting STM32F4 as ‘Series’, STM32F407 as ‘Lines’, and LQFP100 as ‘Package’ (see Figure 591).

  3. Select the STM32F407VGTx from the MCU list and click OK.

Figure 591. MCU selection

image611

STM32CubeMX views are then populated with the selected MCU database ( Figure 592). Optionally, remove the MCUs Selection bottom window by deselecting Window > Outputs submenu (see Figure 593).

image612

11.2 Configuring the MCU pinout

For a detailed description of menus, advanced actions and conflict resolutions, refer to Section 4 and Appendix A.

  1. By default, STM32CubeMX shows the Pinout view.

  2. By default, image613 is unchecked, allowing STM32CubeMX to

move the peripheral functions around and to find the optimal pin allocation (the one that accommodates the maximum number of peripheral modes).

Since the MCU pin configurations must match the STM32F4DISCOVERY board,

enable image614 for STM32CubeMX to maintain the peripheral

function allocation (mapping) to a given pin. This setting is saved as a user preference, to be restored when reopening the tool or when loading another project.

  1. Select the required peripherals and peripheral modes:

  1. Configure the GPIO to output the signal on the STM32F4DISCOVERY green LED by right-clicking PD12 from the Pinout view, then select GPIO_output:

Figure 594. GPIO pin configuration

image615

  1. Enable a timer to be used as timebase for toggling the LED. This is done by selecting Internal Clock as TIM3 clock source from the peripheral tree (see Figure 595).

image616

  1. You can also configure the RCC to use an external oscillator as clock source (see Figure 596).

image617

This completes the pinout configuration for this example.

Note: Starting with STM32CubeMX 4.2, the user can skip the pinout configuration by directly loading ST Discovery board configuration from the Board selector tab.

11.3 Saving the project

  1. Click image618 to save the project.

    When saving for the first time, select a destination folder and filename for the project. The .ioc extension is added automatically to indicate that this is an STM32CubeMX configuration file.

    image619

11.4 Generating the report

Reports can be generated at any time during the configuration:

  1. Click image620 to generate .pdf and .txt reports.

    If a project file has not been created yet, a warning prompts the user to save the project first and requests a project name and a destination folder (see Figure 598). An .ioc file is then generated for the project along with a .pdf and .txt reports with the same name.

image621

Answering No will require to provide a name and location for the report only.

As shown in Figure 599, a confirmation message is displayed when the operation is successful.

image622

  1. Open the .pdf report using Adobe Reader or the .txt report using your favorite text editor. The reports summarize all the settings and MCU configuration performed for the project.

11.5 Configuring the MCU clock tree

The following sequence describes how to configure the clocks required by the application based on an STM32F4 MCU.

STM32CubeMX automatically generates the system, CPU and AHB/APB bus frequencies from the clock sources and prescalers selected by the user. Wrong settings are detected and highlighted in fuchsia through a dynamic validation of minimum and maximum conditions. Useful tooltips provide a detailed description of the actions to undertake when the settings are unavailable or wrong. User frequency selection can influence some peripheral parameters (e.g. UART baud rate limitation).

STM32CubeMX uses the clock settings defined in the Clock tree view to generate the initialization C code for each peripheral clock. Clock settings are performed in the generated C code as part of RCC initialization within the project main.c and in stm32f4xx_hal_conf.h (HSE, HSI and external clock values expressed in Hertz).

Follow the sequence below to configure the MCU clock tree:

  1. Click the Clock Configuration tab to display the clock tree (see Figure 600).

The internal (HSI, LSI), system (SYSCLK) and peripheral clock frequency fields cannot be edited. The system and peripheral clocks can be adjusted by selecting a clock source, and optionally by using the PLL, prescalers and multipliers.

Figure 600. Clock tree view

image623

  1. Select the clock source (HSE, HSI or PLLCLK) that will drive the system clock.

In the example taken for the tutorial, select HSI to use the internal 16 MHz clock (see Figure 601).

image624

To use an external clock source (HSE or LSE), the RCC peripheral must be configured in the Pinout view, as pins will be used to connect the external clock crystals (see Figure 602).

image625

Other clock configuration options for the STM32F4DISCOVERY board:

  • Select the external HSE source and enter 8 in the HSE input frequency box since an 8 MHz crystal is connected on the discovery board:

Figure 603. HSE clock source enabled

image626

  • Select the external PLL clock source and the HSI or HSE as the PLL input clock source.

Figure 604. External PLL clock source enabled

image627

  1. Keep the core and peripheral clocks to 16 MHz using HSI, no PLL and no prescaling.

Note: Optionally, further adjust the system and peripheral clocks using PLL, prescalers and multipliers:

Other clock sources independent from the system clock can be configured as follows:

  • USB OTG FS, RNG and SDIO clocks are driven by an independent PLL output.

  • I2S peripherals come with their own internal clock (PLLI2S), alternatively derived by an independent external clock source.

  • USB OTG HS and Ethernet clocks are derived from an external source.

  1. Optionally, configure the prescaler for the Microcontroller Clock Output (MCO) pins that allow to output two clocks to the external circuit.

  2. Click image628 to save the project.

  3. Go to the Configuration tab to proceed with the project configuration.

11.6 Configuring the MCU initialization parameters

Caution: The C code generated by STM32CubeMX covers the initialization of the MCU peripherals and middlewares using the STM32Cube firmware libraries.

From the Pinout & Configuration tab, select and configure (one by one) every component (peripheral, middleware, additional software) required by the application using the Mode and Configuration panels (see Figure 605).

Tooltips and warning messages are displayed when peripherals are not properly configured (see Section 4 for details).

Note: The RCC peripheral initialization uses the parameter configuration done in this view as well as the configuration done in the Clock tree view (clock source, frequencies, prescaler values).

Figure 605. Pinout & Configuration view

image629

Each peripheral instance corresponds to a dedicated button in the main panel. Some peripheral modes have no configurable parameters, as illustrated below.

Figure 606. Case of Peripheral and Middleware without configuration parameters

image630

Follow the steps below to proceed with peripheral configuration:

  1. Click the peripheral button to open the corresponding configuration window.

In our example

  1. click TIM3 to open the timer configuration window.

Figure 607. Timer 3 configuration window

image631

  1. with a 16 MHz APB clock (Clock tree view), set the prescaler to 16000 and the counter period to 1000 to make the LED blink every millisecond.

Figure 608. Timer 3 configuration

image632

  1. Optionally, and when available, select:

    • The NVIC Settings tab to display the NVIC configuration and enable interruptions for this peripheral.

    • The DMA Settings tab to display the DMA configuration and to configure DMA transfers for this peripheral.

In the tutorial example, the DMA is not used and the GPIO settings remain unchanged. The interrupt is enabled, as shown in Figure 609.

  • The GPIO Settings tab to display the GPIO configuration and to configure the GPIOs for this peripheral.

  • Insert an item:

  • The User Constants tab to specify constants to be used in the project.

Figure 609. Enabling Timer 3 interrupt

image633

The user can adjust all pin configurations from this window. A small icon along with a tooltip indicates the configuration status.

Figure 610. GPIO configuration color scheme and tooltip

image634

Follow the sequence below to configure the GPIOs:

  1. Click the GPIO button in the Configuration view to open the Pin Configuration window.

  2. The first tab shows pins that have been assigned a GPIO mode, but not for a dedicated peripheral and middleware. Select Pin Name to open the configuration for that pin.

In the tutorial example, select PD12 and configure it in output push-pull mode to drive the STM32F4DISCOVERY LED (see Figure 611).

image635

This is not required for this example. It is recommended to use DMA transfers to offload the CPU. The DMA Configuration window provides a fast and easy way to configure the DMAs (see Figure 612):

  1. add a new DMA request and select among a list of possible configurations.

  2. select among the available streams.

  3. select the Direction: Memory to Peripheral or Peripheral to Memory.

  4. select a Priority.

  5. enable the FIFO.

Note: Configuring the DMA for a given peripheral and middleware can also be performed using the Peripheral and Middleware configuration window.

Figure 612. DMA parameters configuration window

image636

This is not required for the example taken for the tutorial.

If a peripheral is required for a middleware mode, the peripheral must be configured in the Pinout view for the middleware mode to become available. A tooltip can guide the user as shown below.

image637

  1. Configure the USB peripheral from the Pinout view.

image638

  1. Select MSC_FS class from USB Host middleware.

  2. Select the checkbox to enable FatFs USB mode in the tree panel.

Figure 615. FatFs over USB mode enabled

image639

  1. Select the Configuration view. FatFs and USB buttons are then displayed.

image640

  1. FatFs and USB using default settings are already marked as configured image641. Click FatFs and USB buttons to display default configuration settings. You can also change them by following the guidelines provided at the bottom of the window.

    Figure 617. FatFs define statements

image642

11.7 Generating a complete C project

Default project settings can be adjusted prior to C code generation as shown in Figure 618.

  1. Select the Project Manager view to update project settings and generation options.

  2. Select the Project Tab and choose a Project name, location, a toolchain and a toolchain version to generate the project (see Figure 618).

image643

  1. Select the Code Generator tab to choose various C code generation options:

  • The library files copied to Projects folder.

  • C code regeneration (e.g. what is kept or backed up during C code regeneration).

  • HAL specific action (for example, set all free pins as analog I/Os to reduce power consumption).

In the tutorial example, select the settings as displayed in Figure 619, and click OK.

Note: A dialog window appears when the firmware package is missing. Go to next section for explanation on how to download the firmware package.

image644

  1. Click image645 to generate the C code.

    During the generation, STM32CubeMX copies files from the relevant STM32Cube MCU package into the project folder so that the project can be compiled. When generating a project for the first time, the firmware package is not available on the user PC and a warning is displayed.

image646

  1. STM32CubeMX offers to download the relevant firmware package or to go on. Click Download to obtain a complete project, ready to use in the selected IDE.

By clicking Continue, only Inc and Src folders are created, holding STM32CubeMX generated initialization files. The necessary firmware and middleware libraries must be copied manually to obtain a complete project. If the download fails, an error message is displayed.

Figure 621. Error during download

image647

To solve this issue, execute the next two steps. Skip them otherwise.

  1. Select Help > Connection & Updates, a new window appears.

  2. Go to the Connection Parameters tab and adjust the parameters to match your network configuration.

  3. Click Check connection. The check mark turns green once the connection is established.

Figure 622. Updated settings with connection

image648

  1. Once the connection is functional, click image649 to generate the C code.

The progress is displayed (see next figures).

Figure 623. Downloading the firmware package

image650

  1. Finally, a confirmation message is displayed to indicate that the C code generation has been successful.

image651

  1. Click Open Folder to display the generated project contents or click Open Project to open the project directly in your IDE. Then proceed with Section 11.8.

Figure 626. C code generation output folder

image652

The generated project contains:

  • The STM32CubeMX .ioc project file located in the root folder. It contains the project user configuration and settings generated through STM32CubeMX user interface.

  • The Drivers and Middlewares folders hold copies of the firmware package files relevant for the user configuration.

  • The Projects folder contains IDE specific folders with all the files required for the project development and debug within the IDE.

  • The Inc and Src folders contain STM32CubeMX generated files for middleware, peripheral and GPIO initialization, including the main.c file. The STM32CubeMX generated files contain user-dedicated sections allowing to insert user-defined C code.

Caution: C code written within the user sections is preserved at next C code generation, while C code written outside these sections is overwritten.

User C code is lost if user sections are moved or if user sections delimiters are renamed.

STM32CubeMX includes a feature that allows users to pause and resume the download actions. This capability is particularly useful in scenarios where network connectivity is unstable or frequently interrupted. This also offers the ability to verify network issue, and connection speed.

The user does not need to restart large downloads from scratch after a brief Internet hiccup. To stop the download, click the Pause button.

Figure 627. Pause button

image653

Once the Pause button has been clicked, a temporary file is created under the path

~/STM32Cube/Repository, to keep the download context

Figure 628. Creating the .tmp file

image654

To restart the download, click the Resume button.

Figure 629. Resume button

image655

The button is grayed if the download has been completed, and the unzip of the downloaded folder is started.

Figure 630. Resume button during the unzipping of downloaded files

image656

When closing the download dialog box an additional window appears, prompting the user to decide whether to keep the temporary file:

  • If the user clicks Yes, the system retains the temporary file to facilitate the download.

  • If the user clicks No, the system deletes the temporary file.

Figure 631. Closing the download window without clicking the Resume button

image657

When the Cancel button is clicked, all procedures are canceled. The system stops the download and deletes the temporary file.

image658

11.8 Building and updating the C code project

This example explains how to use the generated initialization C code and complete the project, within IAR™ EWARM toolchain, to have the LED blink according to the TIM3 frequency.

A folder is available for the toolchains selected for C code generation: the project can be generated for more than one toolchain by choosing a different toolchain from the Project Manager menu and clicking Generate code once again.

  1. Open the project directly in the IDE toolchain by clicking Open Project from the dialog window or by double-clicking the relevant IDE file available in the toolchain folder under STM32CubeMX generated project directory (see Figure 625).

image659

  1. As an example, select .eww file to load the project in the IAR™ EWARM IDE.

image660

  1. Select the main.c file to open in editor.

image661

The htim3 structure handler, system clock, GPIO and TIM3 initialization functions are defined. The initialization functions are called in the main.c. For now the user C code sections are empty.

  1. In the IAR™ IDE, right-click the project name and select Options.

Figure 636. IAR™ options

image662

  1. Click the ST-LINK category and make sure SWD is selected to communicate with the STM32F4DISCOVERY board. Click OK.

image663

  1. Select Project > Rebuild all. Check if the project building has succeeded.

Figure 638. Project building log

image664

  1. Add user C code in the dedicated user sections only.

Note: The main while(1) loop is placed in a user section.

For example:

  1. Edit the main.c file.

  2. To start timer 3, update User Section 2 with the following C code:

Figure 639. User Section 2

image665

  1. Then, add the following C code in User Section 4:

Figure 640. User Section 4

image666

This C code implements the weak callback function defined in the HAL timer driver (stm32f4xx_hal_tim.h) to toggle the GPIO pin driving the green LED when the timer counter period has elapsed.

  1. Rebuild and program your board using image667. Make sure the SWD ST-LINK option is checked as a Project options otherwise board programming will fail.

  2. Launch the program using image668. The green LED on the STM32F4DISCOVERY board will blink every second.

  3. To change the MCU configuration, go back to STM32CubeMX user interface, implement the changes and regenerate the C code. The project will be updated,

preserving the C code in the user sections if image669 option in

Project Manager’s Code Generator tab is enabled.

11.9 Switching to another MCU

STM32CubeMX allows loading a project configuration on an MCU of the same series.

Proceed as follows:

  1. Select File > New Project.

  2. Select an MCU belonging to the same series. As an example, you can select the STM32F429ZITx that is the core MCU of the 32F429IDISCOVERY board.

  3. Select File > Import project. In the Import project window, browse to the .ioc file to load. A message warns you that the currently selected MCU (STM32F429ZITx) differs from the one specified in the .ioc file (STM32F407VGTx). Several import options are proposed (see Figure 641).

  4. Click the Try Import button and check the import status to verify if the import has been successful.

  5. Click OK to really import the project. An output tab is then displayed to report the import results.

  6. The green LED on 32F429IDISCOVERY board is connected to PG13: CTRL+ right click PD12 and drag and drop it on PG13.

  7. From Project Manager project tab configure the new project name and folder location. Click Generate icon to save the project and generate the code.

  8. Select Open the project from the dialog window, update the user sections with the user code, making sure to update the GPIO settings for PG13. Build the project and flash the board. Launch the program and check that LED blinks once per second.