First Project using STM32 in STM32CubeIDE

We will use for our examples STM32CubeIDE released by ST and completely free. STM32CubeIDE is a development tool and supports multi operative system (SO), which is part of the STM32Cube software ecosystem. STM32CubeIDE allows using a single platform to configure peripherals, to generate/compile/debug the code for STM32 microcontrollers and microprocessors. The framework used is Eclipse®/CDT, as tool-chain for the development is used GCC toolchain and GDB for the debugging.

To start the project, you must first select the MCU and then initialize and configure the peripherals the user wants to use. At this point, the initialization code is generated. At any time, the user can go back and change initializations and configurations, without affecting the user code. We will dive into these steps with a simple example in the next paragraph.

Where To Buy?
No.ComponentsDistributorLink To Buy
1STM32 NucleoAmazonBuy Now

First Project in STM using STM32CubeIDE

  • First of all, you have to install on your PC the STM32CubeIDE. In order to do it, you have to go on the ST site and after registered in it you can navigate to "https://www.st.com/en/development-tools/stm32cubeide.html" to download it.
  • At this point, you can install it, if you'll find problems you can check the "UM2563 - STM32CubeIDE installation guide".
  • STM32 Nucleo board (in our example we will use NUCLEO-L053R8, but you can use the one you prefer, as already said all Nucleo boards are pinout compatible);
  • USB 2.0 Cable - A-Male to Mini-B Cord to connect PC to ST-LINK;
  • Breadboard to hold and connect the simple electrical components;
  • Jumper Cables Kit to connect the Nucleo board to breadboard or other components;
  • Various basic electrical components such as Resistors (THT), Capacitors (THT), Buzzers, LEDs, etc.
Now let’s start!

Blinking LED using STM32 Nucleo Board

  • Turning a LED on and off is a basic and classical experiment when dealing with NUCLEO for the first time.
  • First, let's turn on the led, to do this we have to connect the LED to the power supply (on Nucleo we can find 3.3V and 5V).
Assuming:
  • the power supply 5V;
  • LED forward voltage of LED 7 Volt;
  • LED maximum forward current 20mA;

We must connect in series to LED a resistor. What resistance value must be considered to limit the current to 20 mA (see the formula below)?

R > 5 - 1.7/0.02 , where R > 165 ?

A good value of R is 220 ?.

Now we must be able to turn off the led, to do it we need a switch to connect and disconnect the power supply as shown below.

In this case when the switch is closed the Led is ON, when the switch is open the Led is OFF. This can be easily done with a Nucleo board by configuring a GPIO (General Purpose Input Output) pin.

The steps required to configure a core board are explained below. Obviously, this is a very simple practical example, but peripherals, communication protocols and anything else can be configured in a similar way.

Steps to generate the config. files from STM32CubeMX

As already said, we will use for our examples a NUCLEO-L053R8 board. For our examples, so also a Windows PC is required. The ST-Link USB connector is used both for serial data communications, and firmware downloading and debugging on the MCU. A Type-A to mini-B USB cable must be connected between the board and the computer. The USART2 peripheral uses PA2 and PA3 pins, which are wired to the ST-Link connector. In addition, USART2 is selected to communicate with the PC via the ST-Link Virtual COM Port. A tool that emulates the serial communication terminal is necessary to view received messages and send them. You can download many open-source tools as Tera Term. In this way, you can display on PC the messages received from the board over the virtual communication Port and the other way around.

In this paragraph, we will initialize the peripherals using STM32CubeMX. The STM32CubeMX tool to create the necessary config. files to enable drivers of peripheral used (for more detail read “UM1718 - User manual STM32CubeMX for STM32 configuration and initialization C code generation.”).

Steps to Follow

  • Select the NUCLEO-L053R8 board looking in a selection bar within the New Project menu.
  • Select the required features (debug, GPIOs, peripherals, timer) from the Pinout view: peripheral operating modes as well as assignment of relevant signals on pins.
  • Configure the MCU clock tree from the Clock Configuration view.
  • Configure the peripheral parameters from the Configuration view.
  • Configure the project settings in the Project Manager menu and generation of the project (initialization code only).
  • Update the project with the user application code corresponding to the Led blinking example.
  • Compile, and execute the project on the board.

Creating a new STM32CubeMX project

  • Select File -> New project from the main menu bar to open the New Project window.
  • Go to the Board selector tab and filter on STM32L0 Series.
  • Select NUCLEO-L053R8 board and then click on OK button to confirm. In this way the board is loaded within the STM32CubeMX user interface.
  • Insert the project name, in this case, "BlinkLed" and click finish.
  • Then the tool will open the pinout view.

Selecting the features from the Pinout view

  • Select Debug Serial Wire under SYS, for do it click on System Core (on the topo right) and then select SYS and finally flag on “Debug Serial Wire”.
 
  • Select Internal Clock as clock source under TIM2 peripheral. To do this click on Timers and then select TIM2. Now go to clock source and select through the drop-down menu “internal clock”.
  • Select one of the available pins (basically anyone!) as GPIO output, for example PA9 (the last pin of CN5)
Check that the signals are properly assigned on pins:
  • SYS_SWDIO on PA13
  • SYS_SWCLK on PA14
  • GPIO OUTPUT on PA5

Configuring the MCU clock tree

  • Go to the Clock Configuration tab and in this project, there is no need to change the configuration.

Configuring the peripheral parameters

  • Come back to Pinout&Configuration and select System Core -> GPIO to open the peripheral Parameter Settings window and in this case no change the configuration.
  • Select Timers -> TIM2 and change the Prescaler to 16000 and the Counter Period to 1000.
  • Go to the NVIC Settings tab and flag TIM2 global interrupt to enable the interrupt related to TIM2.

Configuring the project settings and generating the project

  • In the Project Manager tab, configure the code to be generated and click OK to generate the code.

Updating the project with the user application code

  • Our project has been initialized by STCubeMX.
  • In /BlinkLed/Core/Src/main.c we will find our main where we will write the main body of our program.

STM32 Programming Code for Led Blinking

The user code must be inserted between the "USER CODE BEGIN" and "USER CODE END" so that if you go to regenerate the initializations that part of the code is not deleted or overwritten. As shown in the code below, the simplest way is to use the function:

  • HAL_GPIO_TogglePin(GPIOx, GPIO_Pin) in the infinite loop ( while (1) ) with a chosen delay, for example of 1 second. To do this we use the function HAL_Delay(Delay). The declarations of these two functions with their parameters are described below
  • HAL_GPIO_TogglePin(GPIOx, GPIO_Pin): Toggles the specified GPIO pins.
  • GPIOx: Where x can be (A..E and H) to select the GPIO peripheral for STM32L0xx family devices. Note that GPIOE is not available on all devices. All port bits are not necessarily available on all GPIOs.
  • GPIO_Pin: Specifies the pins to be toggled.
  • HAL_Delay(Delay): This function provides minimum delay (in milliseconds) based on variable incremented. In the default implementation, SysTick timer is the source of time base. It is used to generate interrupts at regular time intervals where uwTick is incremented.

Note This function is declared as __weak to be overwritten in case of other implementations in a user file.

- Delay: specifies the delay time length, in milliseconds.

So, in our case GPIOx is GPIOA and the GPIO_PIN is GPIO_PIN_9 (see below).

int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_TIM2_Init(); /* USER CODE BEGIN 2 */ /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_9); HAL_Delay(1000); /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ }

Now we are ready to compile and run the project:

  • Compile the project within IDE.
  • Download it to the board.
  • Run the program.

Another simple way is to use the function

  • HAL_GPIO_WritePin(GPIOx, GPIO_Pin, PinState).
  • HAL_GPIO_WritePin(GPIOx, GPIO_Pin, PinState): Sets or clears the selected data port bit.

Note This function uses GPIOx_BSRR register to allow atomic read/modify accesses. In this way, there is no risk of an IRQ occurring between the read and the modified access.

  • GPIOx: where x can be (A..E and H) to select the GPIO peripheral for STM32L0xx family devices.

Note that GPIOE is not available on all devices.

  • GPIO_Pin: specifies the port bit to be written.

This parameter can be one of GPIO_PIN_x where x can be (0..15).

All port bits are not necessarily available on all GPIOs.

  • PinState: specifies the value to be written to the selected bit.

This parameter can be one of the GPIO_PinState enum values:

  • GPIO_PIN_RESET: to clear the port pin
  • GPIO_PIN_SET: to set the port pin

So, in our case GPIOx is GPIOA and the GPIO_PIN is GPIO_PIN_9 and the pin state changes between 0 (LOW or CLEAR) and 1 (HIGH or SET).

In this case, the code is the following:

while (1) { HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9,0); HAL_Delay(1000); HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9,1); /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ }

There are many other ways to blink an Led such as PWM, Interrupts etc. and will discuss it in the upcoming lectures. Thanks for reading.

Introduction to Nucleo Development Board

To become familiar with the world of microcontrollers it is necessary to have a development board (also known as a kit), which generally allows you to start working on it easily. Fortunately, the ST provides a wide portfolio of development boards. In this guide, we will describe and use the Nucleo board.

The Nucleo has been introduced a few years ago and its line is divided into three main groups:

  • Nucleo-32;
  • Nucleo-64;
  • Nucleo-144.
Where To Buy?
No.ComponentsDistributorLink To Buy
1STM32 NucleoAmazonBuy Now

Nucleo-32 Development Board

The number of pins available, so the package, gives the name to the board: Nucleo-32 uses an LQFP-32 package; Nucleo-64 and LQFP-64; Nucleo-144 an LQFP-144. The Nucleo-64 was the first line introduced and counts 16 different boards.

The Nucleo boards have interesting advantages compared to the Discovery. First, is the cheaper cost, you can buy it for around 15-25 dollars. Now in 2021 due to the lack of processed semiconductors, it is very difficult to find them on the normal distribution channels and costs are rising. A return to normal is expected from 2023. Furthermore, Nucleo boards are designed to be pin-to-pin compatible with each other. It is a very important advantage, in fact, if I start to develop my firmware on generic Nucleo later then I can adapt my code to another one.

In the next paragraphs, we will see the main structure of STM32-64

STM32 Nucleo-64 parts

The Nucleo-64 is composed of two parts:

  • ST-LINK part
  • MCU part

The part with the mini-USB connector is an ST-LINK 2.1 integrated debugger. It needs to upload the firmware on the target MCU and run the debugging. Furthermore, the ST-LINK interface provides a Virtual COM Port (VCP), which can be used to exchange data and messages with the host PC. The ST-LINK interface can be used as a stand-alone ST-LINK programmer, in fac,t can be easily cuttable to reduce board size.

To program the STM32 on board, simply plug in the two jumpers on CN4, as shown in the figure below in pink, but do not use the CN11 connector as that may disturb communication with the STM32 microcontroller of the Nucleo.

However, the ST-LINK provides an optional SWD interface which can be used to program another board without detaching the ST-LINK interface from the Nucleo by removing the two jumpers labeled ST-LINK (CN4).

The rest of the board, MCU part, contains the target MCU (the microcontroller we will use to develop our applications), a RESET button, a user-programmable push button (switch), and an LED. It is possible to mount an external high-speed crystal (HSE) through X3 pads (see figure below). Generally, the Nucleo boards, especially the most recent ones, provide a low-speed crystal (LSE).

Finally, the board has several pin headers we will look at in the next paragraph.

STM32 Nucleo-64 connectors

The STM32 Nucleo-64 board has 8 connectors:

  • CN2: ST-LINK USB connector on ST-LINK (already described in the previous paragraph);
  • CN11: SWD connector in ST-LINK part (already described in the previous paragraph);
  • CN7 and CN10: ST morpho connectors;
  • CN5, CN6, CN8, and CN9: ARDUINO® Uno V3 connectors;

The CN7 and CN10 ST morpho connectors are male pin headers (2x19, 2.54mm male pin headers) accessible on both sides of the STM32 Nucleo-64 board (see the figure below). All signals and power pins can be probed by an oscilloscope, logical analyzer, or voltmeter through the ST morpho connectors. They are two. They are called Morpho connectors and are a convenient way to access most of the MCU pins.

  • The figure below is shown the CN7 pinout (the STM32 peripherals and GPIOs associated with the Morpho connector) for NUCLEO-L053R8.
  • In the next figure, is shown the CN10 pinout for NUCLEO-L053R8.
  • The previous figure showed the pinout for NUCLEO STM32L053R8, but I remember you the Nucleo is pinout compatible with each other.
  • Another important feature of the Nucleo board is the compatibility with ARDUINO® Uno V3 through the CN5, CN6, CN8, and CN9 connectors (see figure below).
  • CN6 and CN8 are directly connected to CN7 and CN5 and CN9 to CN10.

Setting-Up the Tool-Chain

The first step in developing an application on the STM32 platform is to fully set up the tool-chain. A tool-chain is a set of programs, compilers, and tools that allows us:

  • to write our code and to browse the source files of our application;
  • to browse inside the application code, in order to examine variables, definitions, function declarations, and etc;
  • to compile our code using a cross-platform compiler;
  • to upload and debug our application on the development board (or a custom board).

To carry out these activities we basically need:

  • an IDE with integrated source editor and navigator;
  • a cross-platform compiler able to compile source code for our platform;
  • a debugger that executes the debugging of firmware on our board;
  • a tool that interacts with the ST-LINK interface.

There are several complete tool-chain for the STM32 Cortex-M family, both free and commercial. The most used tools are: IAR for Cortex-M, ARM Keil, and AC6 SW4STM32.

They can integrate everything necessary for the development of applications on STM32 to simplify and accelerate their development. The first two are commercial solutions and therefore have a price to the public that may be too high for those approaching the first time.

So, that was all for today. I hope you have enjoyed today's lecture and have understood this Nucleo Development Board. In the next lecture, we will design our first project in STM32CubeIDE. Thanks for reading.
Syed Zain Nasir

I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Share
Published by
Syed Zain Nasir