STM32F0 CubeMX Tutorial 1 – GPIO

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).

Step 1:
Open STM32 CubeMX and create a new project selecting STM32F051R8Tx

Step 2:
Set PC8 and PC9 as outputs, along with PA0 as an input

Step 3:
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)

Step 4:
Once the GPIO settings have been confirmed, save the project ensuring the toolchain is set to SW4STM32

Step 5:
You have now completed the CubeMX portion of this tutorial! Open SW4STM32 and import your project into the workspace


Step 6:
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!

Step 7:
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!

Step 8:
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!

Advertisements

2 responses to “STM32F0 CubeMX Tutorial 1 – GPIO

  1. I enjoy your choice.
    First i started with Coocox but now, i think STM32CubeMX with SW4STM32 is more greater, with a large community.
    I prepare a tutorial (in French) with many screen shoot.
    This PDF file is in work, you can have a look at
    onebus.fr/dl/stm32workbench.pdf
    You can easily translate it with google translate.
    Best regards.
    Yves

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