Getting Started with STM32
Table of Contents
- 1 Intro to STM32
- 2 The Nucleo Board
- 3 Developing Software for STM32
- 4 Example Beginner Projects
- 4.1 Hello World: Onboard GPIO LED Blinking
- 4.2 First Input: GPIO Push Button Input
- 4.3 Debug Expansion with Analog: Reading ADC with the Debugger
- 4.4 Always Reading: ADC with DMA
- 4.5 Reactive system: NVIC
- 4.6 Basic Communication: Arduino to STM32 USART
- 4.7 Not Brainless Design: Scheduler
- 4.8 Advanced Communications: CAN
1 Intro to STM32
Getting started with STM32 microcontrollers for FSAE applications presents a step up from platforms like Arduino, offering more advanced features and capabilities. While these microcontrollers share similar programming concepts with Arduino, they come with a steep learning curve to implement STM32s' superior processing power and flexibility.
The STM32 is a family of 32-bit microcontrollers based on ARM Cortex-M processors, developed by STMicroelectronics. What sets it apart from other microcontrollers, like those in the Arduino series, is its advanced features: STM32 offers higher processing power, greater memory capacity, and more peripherals, like built-in USB controllers, ADCs (analog-to-digital converters), and communication interfaces.
2 The Nucleo Board
STM32 Nucleo boards are a series of development boards designed around the STM32 microcontrollers. These boards come in different sizes, mirroring the STM32 microcontroller's diversity:
- Nucleo-32 (compact size).
- Nucleo-64 (medium size).
- Nucleo-144 (largest with advanced features and connectivity).
Examples include the Nucleo-F401RE for high-performance applications, Nucleo-L432KC for power-efficient designs, and Nucleo-G071RB for general-purpose use. Similar to Arduino boards in user-friendliness and community support, STM32 Nucleo boards are also Arduino-compatible, allowing them to use existing Arduino shields, which makes them accessible for Arduino enthusiasts looking to upgrade to more powerful and feature-rich microcontrollers.
Our department currently uses the Nucleo-L432KC as our primary development Nucleo. The primary reasons are:
- CAN support.
- Variety of peripherals.
- Hits all the bare minimum needed specs.
- Small form factor & wide PCB hat board availability.
2.1 Nucleo-L432KC Pinout
- Pictures from os.mbed.com
Notice that the board uses Arduino style connector names (A0, D1, ...). When actually programming all pin references will be to the MCU pin (PA_1, PB_3, ...). The first "PA" of PA_1 means peripheral port A and "1" means pin 1.
2.2 STM32L432KC Block Diagram
Block diagrams are a simplified graphical representation of a system, breaking down complex structures into individual components connected by lines that indicate relationships or flows. They depict the functional workings of a system, highlighting how different parts interact without delving into detailed specifics. Similar to flowcharts, block diagrams are essential for conceptualizing, designing, and troubleshooting systems.
- Picture from ST STM32L432KC Datasheet.
3 Developing Software for STM32
3.1 GitHub Version Control
3.2 Integrated Development Environment
- Recommended for beginners: stm32_cubeide_setup.md.
- Advanced: stm32_clion_setup.md.
4 Example Beginner Projects
4.1 GPIO Output
4.1 Hello World: Onboard GPIO LED Blinking
The typical hello world project for embedded systems is to get a blinking LED going. In this module we will work to get an LED that is already included on Nucleo boards running.
Materials:
- Nucleo-L432KC.
- Programming cable (USB micro B).
- Your computer (this will be assumed going forwards).
Additional Resources:
Starting Steps:
- Pick an LED that you want to get blinking.
- Figure out which MCU pin you need to use.
- Take a look at the Nucleo datasheet and the pinout.
- Create a new project for the Nucleo-L432KC.
- Find the main while loop.
- Follow the additional resources to see code snippets.
4.2 GPIO Input
4.2 First Input: GPIO Push Button Input
Now that we've figure out how to output with GPIO, Let's try getting an input. We'll use a simple button or switch to generate an input. Further we can an LED turn on or off with the push of the button.
Materials:
- Nucleo-L432KC.
- Programming cable (USB micro B).
- Push button or switch.
- Breadboard.
- General hookup wire or jumper wires.
Additional Resources:
Starting Steps:
- Pick an LED that you want to get blinking.
- Figure out which MCU pin you need to use for LED output and one for GPIO
input.
- Take a look at the Nucleo datasheet and the pinout.
- Wire the breadboard to accept the input of the button or switch.
- Follow the additional resources to see code snippets.
4.3 ADC + Debugger
4.3 Debug Expansion with Analog: Reading ADC with the Debugger
GPIO as discussed is about digital values, the other side is analog values. In this module the goal is to read analog values and display them using a debugger in your IDE.
Materials:
- Nucleo-L432KC.
- Programming cable (USB micro B).
- Potentiometer.
- Breadboard.
- General hookup wire or jumper wires.
Additional Resources:
Starting Steps:
- Figure out which MCU pin you need to use for ADC input.
- Take a look at the Nucleo datasheet and the pinout.
- Wire the breadboard to accept the input of the potentiometer.
- Follow the additional resources to see code snippets.
4.4 ADC + DMA
4.4 Always Reading: ADC with DMA
Calling the HAL to get the ADC value everytime can be code complex and take up unnecessary resources. DMA allows for constant updating of values to at the hardware level, reducing resource use.
Materials:
- Nucleo-L432KC.
- Programming cable (USB micro B).
- Potentiometer.
- Breadboard.
- General hookup wire or jumper wires.
Additional Resources:
Starting Steps:
- Based on the previous module's work, change the code to implement DMA to get the ADC value.
4.5 NVIC with GPIO Input
4.5 Reactive system: NVIC
In the previous GPIO Input module, we needed to poll every so often to check if a button was pressed. Instead, we can use interrupts.
Materials:
- Nucleo-L432KC.
- Programming cable (USB micro B).
- Push button or switch.
- Breadboard.
- General hookup wire or jumper wires.
Starting Steps:
- Based on the previous module's work with GPIO inputs, change the code to use the NVIC to turn an LED on and off.
4.7 Scheduler
4.7 Not Brainless Design: Scheduler
Additional Resources:
4.8 CAN
4.8 Advanced Communications: CAN
Additional Resources:
Dev Envs: