1. STM32 Microcontrollers
The STM32 series is a popular, affordable, and high-performance family of microcontrollers. They enjoy extensive support from various development software suites tailored for microcontrollers. STM32 microcontrollers provide numerous peripherals that can be connected to a wide range of electronic components, such as sensors, displays, and electric motors.
The performance range of STM32 microcontrollers is quite broad. The most basic models, like the STM32F0 and STM32F1 series, start with a clock frequency of 24 MHz and are available in packages with as few as 16 pins. In contrast, the STM32H7 series can operate at speeds of up to 400 MHz and comes in packages with 240 pins. Additionally, the STM32L series is specifically designed for low-power applications that run on small batteries.
To develop code, program the microcontroller, and debug applications, development software is necessary. This software suite should include a compiler, a debugger, and an in-circuit serial programmer (ICSP).
2. PlatformIO
PlatformIO supports a wide range of platforms, architectures, and frameworks, offering modern development capabilities. It is available as an extension in VSCode, making it easy to install and configure with just a few clicks.
To install it, launch VSCode and navigate to the Extensions screen by pressing Ctrl+Shift+X. In the marketplace search bar, type "platformio." It will appear at the top of the search results. Click the Install button, and you're all set!
After a few minutes, the installation is complete, and we now have PlatformIO installed in the VSCode IDE. PlatformIO offers some unique features, the most notable being its declarative development environment. With PlatformIO, we only need to specify the components we will use in our project, including the chip type, the framework, the version of that framework, and any additional libraries with their version constraints. We'll explore the meaning of these components and how to configure a project shortly. Additionally, PlatformIO provides all the essential tools needed for developing embedded projects, such as debugging, unit testing, static code analysis, and firmware memory inspection.
3. ST-LINK
The ST-LINK debugger is a versatile in-circuit debugger and programmer designed for STM32 microcontrollers, developed by STMicroelectronics. It provides a seamless interface for debugging and programming STM32 devices through the SWD (Serial Wire Debug) or JTAG interfaces. The ST-LINK is widely used in development environments due to its compatibility with popular IDEs such as STM32CubeIDE and Keil MDK. It offers features like real-time debugging, breakpoints, and flash programming. Its compact design and ease of use make it an essential tool for developers working with STM32 microcontrollers, facilitating efficient development and troubleshooting of embedded applications.
3.1. Prepare ST-LINK on Linux
In order for OpenOCD to properly access and communicate with USB devices on Linux, it’s essential to add a custom udev rule. By default, OpenOCD may not have the necessary permissions to interact with USB devices like ST-Link. The udev rule grants the appropriate permissions to these devices, ensuring that OpenOCD can successfully connect to them. To do this, you need to create a udev rule file in the /etc/udev/rules.d/ directory, specifying the correct vendor and product IDs of the connected debugger.
By executing the command below, we can find information about our device, including the vendor ID and product ID.
lsusb
We now utilize the device information to create a rule in udev with the following content.
file: /etc/udev/rules.d/99-openocd.rules
SUBSYSTEM=="usb", ATTR{idVendor}=="0483", ATTR{idProduct}=="3748", MODE="0666"
4. Simple LED Blink and USART Project
The blinking LED is often referred to as the “Hello World” of embedded systems because it allows you to verify the basic functionality of the hardware while also providing immediate, visual feedback. When your LED blinks correctly, it means the system is initialized properly and you have control over the microcontroller’s peripherals.
4.1. Go to the PlatformIO Home screen.
4.2. Click on the New Project button on the right of the same screen.
4.3. PlatformIO Project Wizard
A pop-up window appears. Set the project name, select BlackPill F103C8 (Generic) for the board, and specify the framework as STM32Cube Development Framework. You can choose a directory for the project or leave it at the PlatformIO default. Click on Finish to let PlatformIO do its job.
4.4. Directory Structure
When the project is created, we have the following directory structure.
4.5. Peripheral Configuration in STM32CubeMX
STM32CubeMX is an essential tool for anyone working with STM32 microcontrollers. It greatly simplifies the process of configuring microcontroller peripherals and generating initialization code. Without STM32CubeMX, you would have to manually configure each peripheral, set up the clock system, manage pin mappings, and handle low-level register manipulations, all of which can be error-prone and time-consuming.
With STM32CubeMX, you get a graphical interface that streamlines the configuration process, making it easier to start your development work.
4.5.1. Clock Configuration
The Black Pill STM32F103C8 board, which is based on the STM32F103C8 microcontroller, provides several clock sources to operate the system clock and its peripherals.
To achieve a system clock frequency of 72 MHz on the STM32F103C8, you should utilize the Phase-Locked Loop (PLL) along with the High-Speed External (HSE) oscillator as the input clock source.
Now in the clock configuration choose 72 MHz.
4.5.2. Pin Configuration
For this project we used PB7 GPIO pin.
4.5.3. USART Configuration
For this project we used USART1 to sending message from microcontroller. You should Configuring PA9 and PA10 for RX and TX.
After Configuring PA9 and PA10 you should select USART mode.
4.5.4. Code Generating
After configuring the microcontroller, you can generate the necessary initialization code. This code typically involves setting up the clock configuration registers to select the HSE as the PLL source and configuring the PLL multiplier.
4.5.5. Generated Code Structure
The generated code structure is something like the following figure.
For the PlatformIO project, we only need the Inc
and Src
folders in the Core
directory. Copy them from the current location to the corresponding directory in the PlatformIO project structure.
4.6. Coding
The generated code includes multiple comments that guide us on how to modify and add our own code. To achieve our goal, we need to locate the main function, which signifies the start of the program. Inside the main function, we should find the while loop and incorporate the LED toggling logic within it, along with a delay to blink the LED connected to pin B7. Additionally, before entering the while loop, we should send the message "Hello dev.to" at the beginning of the program.
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_USART1_UART_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Transmit(&huart1, (uint8_t *)"Hello dev.to", 13, 1000);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_7);
HAL_Delay(250);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
HAL_GPIO_TogglePin: This function toggles the state of the LED pin (turning the LED on and off).
HAL_Delay: This introduces a 500ms delay between toggles, giving the LED a blinking effect.
HAL_UART_Transmit: This transmits the “Hello dev.to” string over USART1 to a connected serial terminal. The huart1 variable is automatically generated in STM32CubeMX and refers to the USART1 handler.
4.7. Build and Run
To build the project, you can use the build button, and to run it, you can click on the Upload button in the PlatformIO extension. Alternatively, you can use the following commands in the PlatformIO terminal emulator inside VSCode.
pio run --environment blackpill_f103c8
pio run --target upload --environment blackpill_f103c8
Resources
Pakdel, M. (2020). Advanced Programming with STM32 Microcontrollers. Elektor.
- Oner, V. O. (2021). Developing IoT Projects with ESP32: Automate your home or business with inexpensive Wi-Fi devices. Packt Publishing Ltd.
Top comments (0)