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. | Components | Distributor | Link To Buy |
1 | STM32 Nucleo | Amazon | Buy 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. | Components | Distributor | Link To Buy |
1 | STM32 Nucleo | Amazon | Buy 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:
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.
Introduction to STM32 Family
In this guide, we will explain step by step to start programming on the STMicroelectronics (STM) platform, especially the STM32 family.
The term, "STM32" refers to a family of 32-bit microcontroller integrated circuits based on the ARM® Cortex®M processor. The architecture of these CPUs (Central Processing Unit) is ARM (Advanced Risk Machine) which is a particular family of Reduced Instruction Set Computing (RISC). RISC architecture differs from Complex Instruction Set Computing (CISC) for its simplicity that allows you to create processors capable of executing instruction sets with shorter times. Why use STM32? The advantages are many, and now we will list a part of them:
- ST offers a wide portfolio of solutions depending on the developer's needs. We can find products that combine different advanced features while maintaining a high level of integration. In fact, we can choose products with high performance, real-time processing, digital signal processing, and low consumption.
- Thanks to the availability of different development tools and available support material, the development of a simple or complex project is quite simple and fast, reducing the time to market if you want to develop a product.
- Each microcontroller has an integrated processor core, static RAM, flash memory, debug interface, and various peripherals such as GPIO, ADC, DAC, Timer, SPI, UART-USART, I2C, etc.
- For every MCU, ST provides the STM32 Nucleo Board that helps anyone who wants to fastly build and test prototypes for new projects with any STM32 MCU. STM32 Nucleo boards share the same connectors and can be easily expanded with many specialized application hardware add-ons (Nucleo-64 includes ST Morpho and Arduino Uno Rev3 connectors, while Nucleo-32 includes Arduino Nano connectors). Another and not insignificant advantage is the cheap cost of these development boards.
In the next paragraph, it will be illustrated how the STM32 is divided to easily identify the one used for your purposes.
Where To Buy? |
---|
No. | Components | Distributor | Link To Buy |
1 | STM32 Nucleo | Amazon | Buy Now |
STM32 Family
- To date, the STM32 family has 16 series of microcontrollers divided into four groups in order to cover all the needs of developers.
- The four groups are Mainstream, Ultra-Low-Power, High-Performance Wireless.
STM32 Mainstream
The STM32 Mainstream has been designed to offer solutions for a wide range of applications where costs, time to market, reliability, and availability are fundamental requirements. They are widely used in real-time control signal processing applications.
There are 5 series in this group:
- STM32F1 are microcontrollers based on the ARM Cortex-M3 core. It was launched in 2007 and evolved over time in terms of maximum clock rate, memory depth, and peripherals. In fact, the maximum clock rate has gone from 24 MHz to 72 MHz, static RAM up to 96 kB, and Flash up to 1024 kB. It also supports Thumb-1 and Thumb-2 instruction sets.
- STM32F0 are microcontrollers based on the ARM Cortex-M0 core. It was launched in 2012. The maximum clock rate is 48 MHz and includes the SysTick timer. Static RAM up to 32 kB, and Flash up to 256 kB. It also supports Thumb-1 and Thumb-2 instruction sets.
- STM32F3 are microcontrollers based on the ARM Cortex-M0 core. It was launched in 2012. The maximum clock rate is 72 MHz and includes the SysTick timer. Static RAM up to 40 kB, and Flash up to 256 kB. It also supports Thumb-1, Thumb-2, Saturated, DSP, FPU instruction sets.
- STM32G0 are microcontrollers based on the Cortex-M0/M0+ core. It was launched in 2018. The maximum clock rate is 64 MHz. Static RAM up to 128 kB, and Flash up to 512 kB. It also supports Thumb-1 and Thumb-2 instruction sets. Compared to the older F0 series, it presents improvements in terms of efficiency and performance.
- STM32G4 are microcontrollers based on the Cortex-M4F core. It was launched in 2019. The maximum clock rate is 170 MHz. Static RAM up to 128 kB, and Flash up to 512 kB. Thumb-1, Thumb-2, Saturated, DSP, FPU instruction sets. Compared to the older F3/F4 series, it presents improvements in terms of efficiency and performance and higher performance compared to the L4 series.
STM32 Ultra-Low-Power
The STM32 Ultra-Low-Power has been designed to meet the need to develop devices with low energy consumption (such as portable and wearable devices) but maintaining a good compromise with performance. There are 6 series in this group:
- STM32L0 are microcontrollers based on the ARM Cortex-M0+ core. It was launched in 2014. The maximum clock is 32 MHz, static RAM is of 8 kB, and Flash is up to 64 kB. It also supports Thumb-1 and Thumb-2 instruction sets.
- STM32L1 are microcontrollers based on the ARM Cortex-M3 core. It was launched in 2010. The maximum clock rate is 32 MHz, static RAM up to 80 kB, and Flash up to 512 kB. It also supports Thumb-1 and Thumb-2 instruction sets.
- STM32L4 are microcontrollers based on the ARM Cortex-M4 core. It was launched in 2015. The maximum clock rate is 80 MHz, static RAM is of 64 kB, and Flash is of 1024 kB. It also supports Thumb-1 and Thumb-2, Saturated, DSP, FPU instruction sets.
- STM32L4+ are microcontrollers based on the ARM Cortex-M4 core. It was launched in 2016. The maximum clock rate is 120 MHz, static SRAM up to 640 kB, and Flash up to 2048 kB. It also supports Thumb-1 and Thumb-2, Saturated, DSP, FPU instruction sets. It has been enriched with advanced peripherals such as a TFT-LCD controller, Camera interface, etc.
- STM32L5 are microcontrollers based on the ARM Cortex-M33F core. It was launched in 2018. The maximum clock rate is 110 MHz, static SRAM up to 640 kB, and Flash up to 2048 kB. It also supports Thumb-1 and Thumb-2, Saturated, DSP, FPU instruction sets.
- STM32U5 is the last ultra-low-power series launched (in 2021). It is an evolution of the L series and is based on the ARM Cortex-M33F core. the new 40 nm silicon technology allows to further reduce energy consumption, it also includes advanced cyber security features and graphics accelerators. The maximum clock rate is 160 MHz, static SRAM up to 640 kB, and Flash up to 2048 kB. It also supports Thumb-1 and Thumb-2, Saturated, DSP, FPU instruction sets.
STM32 High-Performance
The STM32 High-Performance has been designed for data processing and data transfer. It also has a high level of memory integration. There are 5 series in this group:
- STM32H7 are microcontrollers based on the ARM Cortex-M7F core. It was launched in 2017. The maximum clock is 480 MHz, static RAM is up to 1.4 MB, and Flash is up to 128 kB. It includes Ethernet and some advanced features such as dual Octo-SPI, JPEG codec, etc. It supports Thumb-1 and Thumb-2, Saturated, DSP, FPU instruction sets.
- STM32F7 are microcontrollers based on the ARM Cortex-M7F core. It was launched in 2014. The maximum clock is 216 MHz, static RAM is up to 1.4 MB, and Flash is up to 128 kB. It is fully pin-compatible with F4-series. It supports Thumb-1 and Thumb-2, Saturated, DSP, FPU instruction sets.
- STM32F4 was the first series of microcontrollers based on the ARM Cortex-M4F core. It was launched in 2011. The maximum clock is up to 180 MHz. It is the first series to have DSP and FPU. It also has faster ADCs, full-duplex I2S and an improved real-time clock. It supports Thumb-1 and Thumb-2, Saturated, DSP, FPU instruction sets.
- STM32F2 are microcontrollers based on the ARM Cortex-M3 core. It was launched in 2010. The maximum clock is 120 MHz, static RAM is up to 128 kB, and Flash is up to 1024 kB. It is fully pin-compatible with the F2 series. It supports Thumb-1, Thumb-2 and Saturated instruction sets.
STM32 Wireless
With STM32 Wireless ST adds in the portfolio a platform for wireless connectivity to the portfolio. It has a broad spectrum of frequencies and is used in various industrial and consumer applications.
It has features compatible with multiple protocols which allows it to communicate with different devices in real-time. Now, only two series belong to this group:
- STM32WB provides Bluetooth®LE 5.2 and IEEE 802.15.4 communication protocols, Zigbee® and Thread, which can work simultaneously or individually.
- STM32WL is the first series that support LoRa® communication.
So, that was all for today. I hope you have enjoyed today's lecture. In the next lecture, I am going to focus on the Nucleo Development board, as we are going to use that in our upcoming tutorials. Thanks for reading. Take care !!! :)