It’s certainly been a while since I wrote a tutorial, a lot of the STM32 ecosystem has changed and its probably time I changed with it. CubeMX was developed by ST to streamline application design and creation. It allows for a visual method of generating initialization code without needing to know the ins and outs of the internal microcontroller structure. ST also offer an IDE which is compatible with the code produced by CubeMX called SW4STM32 – an acronym of System Workbench for STM32. This IDE is based around Eclipse and CubeMX can generate projects directly for it.
This tutorial makes these assumptions:
- The reader has already installed both CubeMX and SW4STM32
- An SW4STM32 workspace has already been created
- The STM32 ST Link drivers have been installed
- The reader has an STM32F0 Discovery board (based on the STM32F051R8)
If all these are true, we can proceed! In this tutorial, we will be writing a simple program that toggles the onboard LEDs, PC8 and PC9 dependent on the user push button (PA0).
Open STM32 CubeMX and create a new project selecting STM32F051R8Tx
Set PC8 and PC9 as outputs, along with PA0 as an input
Go to the configuration tab and select GPIO. Ensure the settings are correct (PC8 and PC9 should be low speed outputs and PA0 should be an input with no pullup/down resistors)
Once the GPIO settings have been confirmed, save the project ensuring the toolchain is set to SW4STM32
You have now completed the CubeMX portion of this tutorial! Open SW4STM32 and import your project into the workspace
Now the project has been imported, code writing can begin! Navigate through your project to main.c (Application -> User -> main.c) and add the small amount of code shown. Once the code has been written, build the project!
Hopefully the build was successful and you can now upload to your board. Click the more arrow on the run symbol on y our taskbar and navigate to Run Configurations… Once in the run configurations, find Ac6 STM32 Debugging and select your project name. Finally, click Run and this should upload your project to your board!
If all has gone well, you should now see the blue LED (PC8) illuminated on your board. As you press the user button (PA0), the blue LED should turn off and the green LED should turn on (PC9). Voila! Your project is now running!
In a few steps, you can get a simple GPIO sketch running which should realistically take no more than 10-15 minutes. From within CubeMX, you can change system parameters and regenerate the code note however that regenerating the CubeMX code will overwrite any code you have written in the files originally generated potentially erasing any work done so bare that in mind! Hopefully I’ll be able to get a few more tutorials on using CubeMX soon.
The entire project for this tutorial can be found on my github!