Welcome to the series. This is a multipart series created partially to guide students of the Tallinn University of Technology Robotics Club through the programming of a simple STM32. Our end goal with this series is to write the software for a simple folkrace robot, PID controller and all. But, we’ll be starting from the beginning. And the beginning is: tools, configuration, code structure.
Assumptions
This course assumes that you have the following physical items:
- a Nucleo F303k8,
- USB micro cable for connecting the Nucleo to your PC,
- a few female-to-female jumper wires,
- a through-hole LED with an appropriate through-hole resistor.
Software wise, two pieces of software are needed at a minimum:
- STM32CubeIDE,
- PuTTY.
- minicom as a PuTTY alternative, if you’re on MacOS or Linux.
Project Creation
Our base will be an STM32CubeIDE project. CubeIDE is an Eclipse based editor which comes with an integrated compiler, debugger, and microcontroller configuration software (STM32CubeMX). The benefits, specially for beginners, is that STM32CubeMX lets us configure the microcontroller graphically, and will generate a standard hardware abstraction library (HAL) implementation to load the configuration at microcontroller’s startup.
When you boot STM32CubeIDE for the first time, or following an update, you’ll be given the “Information Center” panel. You can close it from the top left area, it won’t be needed.
Click “File” from the top left, the “New” selection, and “New STM32 Project”. This will open the “Target Selection” menu.
Important: we are working with the Nucleo development board. Ergo, select the second tab at the top of the “Target Selection” menu called “Board Selector”.
To quickly locate our board, type “NUCLEO-F303K8” into the “Commercial Part Number” field, and select the only option in the “Boards List” menu. Once highlighted, click “Next” at the bottom.
Give the project a name and leave everything else as is.
Press “Finish”. You will be asked if you want to initialize all peripherals with their default mode, click “Yes”.
The project is now generated and you will be brought into the STM32CubeMX configuration view.
MCU Configuration
Our goal is to configure the MCU as follows:
- We will have one digital input, called IN_BTN.
- We will have one digital output, called OUT_LED.
- We will use USART2 as a virtual COM port (VCP) to print data back to us.
To do this, first, we must remove the erroneous MCO function of the PF0 pin. Left click the green PF0 pin, and select “Reset state”.
Then we have to configure the digital inputs and outputs. As will be explained later, digital inputs and outputs are handled via the GPIO (General Purpose Input-Output) functionality of a pin. The Nucleo board has on onboard LED tied to pin PB3. So we’ll start from there.
Click PB3, click “GPIO_Output”. This assigns the pin as a digital output. Further, we should name the pin so that we could reference it in code later. Right click PB3, select “Enter user label”, and type in “OUT_LED”.
For the input, we have to choose a random pin. For the purposes of our setup, we’ll choose PB0 as our input. Click it, set it as a “GPIO_Input”, and name it as “IN_BTN”.
For purposes that we’ll explain later, we need to set this pin as a pull-up as well. We do this from the left side panel of the STM32CubeMX window. From there, select “GPIO” under the “System Core” section, select “PB0” and configure it as a “Pull up” from the “GPIO Pull-up/Pull-down” drop down.
With this, our GPIOs are configured. We now need to check the VCP and USART2. By default, USART2 is enabled on pins PA15 and
PA2. They should be named as “VCP_RX” and “VCP_TX” respectively. What we need to do is check and modify the baud rate for this
subsystem. Again, from the left side panel, look for the “Connectivity” section, select “USART2”, and look for the “Baud Rate”
menu in the “Parameter Settings” area. Modify it to be 115200
.
With this, our setup is done! Save the file with “Ctrl-S” or from the “File” menu. It will ask if you want to generate the project, press “Yes”. Alternatively, click the shaft and cog button on the top left.
After this, you will be taken to the main.c
file. If the program asks for a perspective shift, press “Yes”.
Code
STM32Cube will now generate code for you. This is all the code that’s necessary for initialization of the microcontroller hardware (clocks, interrupts, etc.) and peripherals (GPIOs, UART, etc.) This allows us to skip a bunch of menial code writing ourselves.
The code itself is structured in a bit of a weird way, if you’re faced with something like this for the first time. Namely, it has a bunch of commented sections which say “USER CODE BEGIN/END”. There’s a few rules regarding this code structure:
- Generated files get written over and adjusted if you regenerate the code again.
- Write code between a set of BEGIN/END comments, and your code will be fine.
- Do not delete or move these comments.
- Any files you create yourself are unaffected by the generator.
Structure
For the purpose of this guide, we’ll be interested in the main.c
file, located in the Core\Src\
folder.
In there, locate the int main(void)
function and make note of the following sections before and after the line:
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
and
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
From here, head to the next post to figure out how to write data to our PC and view it.