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 |
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.
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.”).
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:
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:
Another simple way is to use the function
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.
Note that GPIOE is not available on all devices.
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.
This parameter can be one of the GPIO_PinState enum values:
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.
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:
Where To Buy? | ||||
---|---|---|---|---|
No. | Components | Distributor | Link To Buy | |
1 | STM32 Nucleo | Amazon | Buy Now |
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
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).
The STM32 Nucleo-64 board has 8 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 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 carry out these activities we basically need:
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.