How to use the STM32CubeMX Framework (API)

uVision5 provides two routes for users to configure their RTE. The first option, called Classic (used for all the recipes in Chapters 2-8), configures the STM's Hardware Abstraction Layer (HAL) using the RTE_Device.h header file. This option allows users to quickly configure the RTE for most CMSIS-enabled devices. The second option uses STM's graphical configuration tool, STM32Cube MX, to perform low-level configuration of the HAL directly. Example projects using both approaches are shipped with recent versions of Device Family Packs (for example, DFP 2.6.0). This recipe (named ARM_STM32CubeMX_Blinky_c9v0) shows you how to build a Blinky project using STM's tool.

How to do it…

  1. Create a new project named STM32CubeMX_Blinky. Choose the STM32F407IGHx device.
  2. Configure the RTE for the MCBSTM32F400 board. Check the Board SupportLED (API) and DeviceSTM32Cube Framework (API)STM32CubeMX options. Then, select Resolve and OK.
  3. If you haven't installed STM32CubeMX yet, you will be prompted to do so. It is freely available from www.st.com (search for STM32Cube initialization code generator).
  4. If you have installed STM32CubeMX, then you should see this window asking you to launch the program:
    How to do it…
  5. Once STM32CubeMX is launched, you should see the initial welcome screen. Choose New Project:
    How to do it…
  6. You should now see the microcontroller part rendered on the screen, as in the following screenshot:
    How to do it…
  7. Select pin [G1] (left mouse button) and use the drop-down menu to configure the pin as RCC OSC IN, as in the following screenshot:
    How to do it…
  8. Similarly, configure pin [H1] as RCC OSC OUT.
  9. Expand PeripheralsRCC and use the drop-down menu to configure the HSE to use a Crystal/Ceramic Resonator:
    How to do it…
  10. Open the Clock Configuration tab and configure the clock tree to use a 25 MHz input (crystal), set the clock divider, and select PLLCLK to give a SYSCLK frequency of 168 MHz. Also, set the AHB, APB1, and APB2 Prescalers:
    How to do it…
  11. Select ProjectGenerate Code.
  12. Select FileSave Project. Note that the Toolchain / IDE is EWARM:
    How to do it…
  13. Select OK; then, quit STM32CubeMX by navigating to FileExit.
  14. We should see the following message when we return to uVision. Select Yes to import the code that we've just generated:
    How to do it…
  15. Open the Project tab and check whether we have successfully imported the code:
    How to do it…
  16. Open the file, main.c (found in folder STM32CubeMX:Common Sources), navigate to the main() function definition, and add this statement in the section identified by the /* USER CODE BEGIN 2 */ comment:
    LED_Initialize ( );
  17. Add this code fragment in the section identified by the /* Infinite loop */ comment:
    LED_On(0);
    for (i=0; i<1000000; i++)
      ;
       LED_Off(0);
    for (i=0; i<1000000; i++)
      ;
  18. Remember to declare the loop variables: i and #include "Board_LED.h".
  19. Build, download, and run the program.

How it works…

We've used STM32CubeMX to generate a very basic runtime environment. We're still using the Board Support API to provide functions to configure GPIO and drive LEDs. STM32CubeMX is much more powerful, and we've only illustrated a very basic configuration. More details and further tutorials can be found at www.st.com.

There's more…

We can also use STM32CubeMX to configure the GPIO pins that are used to drive the LEDs. We illustrate this in the ARM_STM32CubeMX_Blinky_c9v1:

  1. After configuring the oscillator (Step 7), select each of the GPIO pins that are connected to the LEDs (GPIO PG6,7,8, PH2,3,6,7, PI10) and configure them as outputs, as in the following screenshot:
    There's more…
  2. Then, select the GPIO menu in the configuration tab to set the other GPIO pin parameters (GPIO Mode, Pull-up, and so on.):
    There's more…
  3. Use STMCubeMX, as we did before, to generate the code. When we open the main.c file, we should now find that STM32CubeMX has added code to configure the GPIO pins in the MX_GPIO_Init() function, as follows:
    void MX_GPIO_Init(void)
    {
    
      GPIO_InitTypeDef GPIO_InitStruct;
    
      /* GPIO Ports Clock Enable */
      __GPIOI_CLK_ENABLE();
      __GPIOH_CLK_ENABLE();
      __GPIOG_CLK_ENABLE();
    
      /*Configure GPIO pin : LED_3_Pin */
      GPIO_InitStruct.Pin = LED_3_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
      HAL_GPIO_Init(LED_3_GPIO_Port, &GPIO_InitStruct);
    
      /*Configure GPIO pins : LED_7_Pin 
                          LED_0_Pin LED_1_Pin LED_2_Pin */
      GPIO_InitStruct.Pin =
                  LED_7_Pin|LED_0_Pin|LED_1_Pin|LED_2_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
      HAL_GPIO_Init(GPIOH, &GPIO_InitStruct);
    
      /*Configure GPIO pins : LED_6_Pin LED_5_Pin LED_4_Pin */
      GPIO_InitStruct.Pin = LED_6_Pin|LED_5_Pin|LED_4_Pin;
      GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
      HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
    
    }
  4. The MX_GPIO_Init() function that was generated by STM32CubeMX is almost identical to that of LED_Initialize(). As such, there is no need to call LED_Initialize () before calling LED_On() and LED_Off().
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.134.151