Discovery: Code, Part 1

Last week we were introduced to ST’s CubeMX (Cube) software as a way to quickly generate code to initialize and configure the processor for a project. We looked at the various screens within Cube, what they are used for, and how to how to set up the clocks. This week, we are going to look at how to get Cube to generate code for us.

Fire up Cube and choose New Project. Select your processor board, I will be using the STM32F407G-DISC1, so I use the Board Selector tab and cut down my choices by choosing my Type of Board to be Discovery, and my MCU Series to be STM32F4.

If you have a different ST dev board, please use Cube to generate a project for your board. They should not sit under your bed with your Bowflex and your 3D printer. Get them to blink an LED at a bare minimum.

Next in the Project menu choose the Settings… option, give your project a name. Let’s look at some of the options here:

Hit Ok and Cube will create a project directory for you.

The interesting bit on this page is the Toolchain/IDE selector. Cube can generate project files for various different  integrated development environments (IDE). An IDE is nothing more than a single program that gives you a text editor, hands your code off to a compiler with error messages that show up in the editor, and then gives you a debugger that steps through the original source code with buttons rather than at a command line. (I talked about that awhile ago in a more general sense.)

When you choose a toolchain, all of the C code that gets generated is exactly the same, only the project file that is read by the IDE will change.

In the next two posts we will take a look at some of the code, I’ll give a rundown of the various IDEs and you can choose your favourite.

A Useful Setting

By default, Cube will put all of the peripheral specific startup code in the main.c file. I find that this clutters the file. If you want to see what the code does to initialize a certain peripheral, you have to sift through one single function that does everything.

If your check the “Generate peripheral initialization as a pair of ‘.c/.h’ files per peripherals” box, Cube will split out the initialization code for each peripheral into its own file. That way, if you have a problem with a peripheral, you can go straight to its initialization file and see how it was configured.

Generating Code

Generating code is very straight forward; under the Project menu, we find the Generate Code item. This starts the process, which takes very little time, and it will finally ask you if you wish to open the project folder.

In the next post we will look at the code that gets generated.  Since we’re starting to get to the interesting bits, my next post will be early. But in the meantime, get Cube running, play with some of the controls, generate some code, and see what you get. You can always delete your test projects.

This post is part of a series. Please see the other posts here.