Welcome to the Cube

So continuing my adventure with the new STM32Nucleo development board I began playing around with the STM32CubeMX code generator today. STM32CubeMX is a graphical software configuration tool that allows you to generate initialization code using a graphical wizard.

STM32CubeMXWith today’s microcontrollers becoming more and more advanced and offering multiple functionality it can often take a substantial amount of time just getting up and running. It seems most manufacturers these days offer some form of graphical code generation tool. Texas Instruments have their Pinmux utility,  Renesas have their Appilet tool and ST are no exception with their offering for the STM32 range STM32CubeMX.

The tool is fairly intuitive but like any other tool there is always a learning curve. I found few areas where I came slightly unstuck which was not necessarily any fault of the tool but with configuring the resulting project.

Generating and building a demo project

As a quick guide I intend going through creating an example project with STM32CubeMX and running it on the STM32Nucleo development board. I’ll keep the project simple and in keeping with recent projects we will simply toggle the LED. Since FreeRTOS is a middleware option we will use that as well.

  1. Download and install STM32CubeMX from the link here.
  2. Launch STM32CubeMX and you will be greeted with something similar to that show above.
  3. Click “New Project” project. You can select your processor you wish to use. You can use the drop down boxes to narrow your search or simply check the peripherals you require under the “Peripheral Selection” pane and only MCUs supporting those peripherals will be shown.
  4. Since we will be using the ST32NucleoF401RE board then it’s far simpler just to select it from the “Board Selector” tab.STM32CubeMX_edit
  5. The main window will now show a diagram of your chosen MCU along with the supported peripherals. You will notice that some of the pins have already been assigned signals. Example PA5 the GPIO pin LED LD2 is connected. Also PC13 which is connected to the blue push button. Other pins such as PA2 and PA3 are coloured orange. These pins are are used by USART2 which by default has no mode configured. Some peripherals may have symbols next to them indicating there is currently a conflict or issue with them. Hovering over these peripherals should show more information.
  6. Expand “USART2” by clicking on the “+” next to it. Set the mode to “Asynchronous”. Leave flow control disabled.
  7. Expand “RCC”. Set both the High and Low Speed Clocks to “Crystal/Ceramic Resonator”.
  8. Expand “FREERTOS”  and enable it.
  9. Click the “Clock Configuration” tab. The default configuration should be fine. The clock source should be the 16 MHz HSI RC resonator connected through the PLL Source Mux. /M divide by 16, *N multiply by 336, /P divide by 4. The System Clock Mux should be set to PLLCLK resulting in a SSYCLK of 84 Mhz. AHB Prescaler should be divide by 1, APB1 should be set to divide by 2 and APB2 divide by 1. The end result being an FCLK of 84 MHz.
  10. Next click the “Configuration” tab. You should have FREERTOS, RCC, SYS, and USART2 all enabled.
  11. Save the project settings by either clicking the disk icon in the tool bar.
  12. Start the code generator by clicking the gear icon in the tool bar.
  13. Enter a project name and choose a suitable location to save the generated files.
  14. Select the “MDK-ARM” as the desired tool chain.
  15. Click on “OK” start code generation. When complete close STM32CubeMX.
  16. Open Keil uVision and then load your newly generated project.
  17. Select “Select Device for Target…” from the Project menu.
  18. Type “STM32F401RET” into the search box. The only MCU shown appear is the “STM32F401RET”. Select it and press “OK”.
  19. Select “Rebuild all target files” from the Project menu to rebuild the entire project. It should build successfully with no errors and no warnings.

Modifying the demo project to do something

STM32CubeMX generates quite a few files. The tool embeds a series specific software platform which includes an STM32 abstraction layer to provide an API for accessing the STM32 peripherals. This is all added to the project when created. Again this appears to be something most micro controller manufacturers are doing these days.

So getting back to the code. Referring to the main() function in main.c the functions starts with a number of initialisation functions, the creation of a new thread “StartThread”, followed by a call to start the scheduler (FreeRTOS) and finally an infinite loop. At this point the scheduler is now in control of the program flow.

We only have one thread in this example “StartThread” so let’s modify it to toggle the LED LD2 every 500ms. Make the following changes to “StartThread” function as shown below :-

 static void StartThread(void const * argument)  
    /* USER CODE BEGIN 5 */  
    /* Infinite loop */  
       HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);  
    /* USER CODE END 5 */   

This function now contains all call to HAL_GPIO_TogglePin() which is API hardware call that as its name suggests simply toggles an GPIO pin. In this case pin 5 (GPIO_PIN_5) on general purpose I/O port A (GPIOA). After toggling the pin the function simply waits for 500 ms before repeating the process. Build the target and it should build successfully with no errors and no warnings.

Configuring the debugger

By default ST-Link is not the default debugger for generated project so you will need to configure it before you can successfully execute the project on target.

  1. Connect the STM32Nucleo board to your PC via CN1.
  2. Select “Options For Target” from the Project menu.
  3. Click the “Debug” tab to configure the debugger.target_config1
  4. Select “ST-Link Debugger” as the required debugger.
  5. Click the “Settings” next to the debugger selection.
  6. Select “SW” under the Port setting. The device should now be shown in the SWDIO box.target_config2
  7. Change the “Max Clock” to 4MHz.
  8. Click the “Flash Download” tab.target_config3
  9. Click the “Add” button.
  10. Select “STM32F4xx 512kB Flash” and press “Add”.
  11. OK all open dialogs to return back to the IDE main page.
  12. Select “Start/Stop Debug Session” from the Debug menu.
  13. The project will be download to the STM32Nucleo board and the debugger started. The debugger will then break on the first instruction in the function main().
  14. Press F5 project will begin executing and with any luck LED LD2 should be flashing away.

So there you have it concept to working prototype in next to no time. Well not quite but you can see how STM32CubeMX takes a lot of the initial pain out of configuring your hardware when starting from scratch. The resulting code may be bloated using the HAL and there may not be an abundance of information detailing the use of the HAL but it sure beats trudging through endless pages of the datasheet.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s