Skip to content

Instantly share code, notes, and snippets.

@GGn0
Last active April 8, 2023 09:19
Show Gist options
  • Save GGn0/c3b8eedce9fe9d6e8b3fc8f4e4be28bb to your computer and use it in GitHub Desktop.
Save GGn0/c3b8eedce9fe9d6e8b3fc8f4e4be28bb to your computer and use it in GitHub Desktop.
Programming STM32 Nucleo-64 with PlatformIO

Programming STM32 Nucleo-64 with PlatformIO

Make sure to install the correct drivers for the stlink programmer.

⚠️ If you are on Windows, you may want to reboot the system after installing drivers or even PlatformIO

Specifications

  • OS: Windows 10 Pro 20H2 build 19042.1052
  • VSCode: v1.53.2
  • PlatformIO: Core v5.1.1
  • Board: Nucleo-STM32L476RG
  • Programmer: Mini ST-LinkV2

The programmer looks like a small USB stick and was bought from az-delivery

Configuring Platform IO

Install the STM32Cube Framework

Open the extension page, click on Platforms > Embedded and look for STM32Cube

Create new project

From PIO home click Projects > + Create New Project
Set the project name and the target board (e.g. Nucleo L476RG)
You can also set an installation directory if it's the first time using the extension
Make sure to set the framework to STM32Cube

The extension will generate a directory for the project and a configuration file platformio.ini.
Other settings can be added following the platform specific guide https://platformio.org/platforms/ststm32/boards

Add some code

Add some code to the folder src
Check out main.c and main.h in this Gist
Or the linked GitHub repo

Verify the connections

Read the pinout of the programmer and the board and connect 4 pins on the SWD connector:

  • RST
  • SWCLK
  • SWDIO
  • GND
  • 3.3V

Keep the ST-link jumpers on the board
Connect the usb cable to power up the board

Now verify that the programmer is correctly recognised by PlatformIO:
Open the device tab from the PIO extension and check the device list.
If the programmer is not present in the list you can try:

  • Rebooting the system
  • Reinstalling the ST-Link drivers (and rebooting the system)
  • Reinstalling PlatformIO (First close VSCode then delete the .platformio folder, usually in %userprofile% on windows)

Building and Uploading

Building
First of all check that the code builds correctly.
You can use one of the following methods:

  • Open the PlatformIO extension and click on Project Tasks > nucleo_l476rg > General > Build
  • Open the command palette (CTRL + P) and search for PlatformIO: Build
  • Click the checkmark ✔️ symbol on the status bar below If VSCode asks to configure a build task, relaunch the IDE then try to build the project.
    Make sure to open the project from PatformIO!

Uploading
Specify the protocol in platformio.ini

upload_protocol = stlink

To be able to upload your code, you may have to configure openocd config files
The files are located under %userprofile%/.platformio/packages/tool-openocd/scripts

In particular for this combination of board + mini usb programmer, it has been necessary to edit the file
boards/st_nucleo_l4.cfg

...
# use hardware reset
# reset_config srst_only srst_nogate
reset_config trst_only

You can then upload your code on the board using one of the following methods:

  • Open the PlatformIO extension and click on Project Tasks > nucleo_l476rg > General > Upload
  • Open the command palette (CTRL + P) and search for PlatformIO: Upload
  • Click the arrow ➡️ symbol on the status bar below

Debugging

Add the the following line of code to platformio.ini

debug_tool = stlink

Now you can start debugging your code in multiple ways:

  • Open the PlatformIO extension and click on Quick Access > Debug > Start Debugging
  • Open the command palette (CTRL + P) and search for PlatformIO: Start Debugging

VSCode will reupload the code on the board and open the debugging view.

#include "main.h"
void LED_Init();
int main(void) {
HAL_Init();
LED_Init();
while (1)
{
HAL_GPIO_TogglePin(LED_GPIO_PORT, LED_PIN);
HAL_Delay(1000);
}
}
void LED_Init() {
LED_GPIO_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Pin = LED_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
HAL_GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct);
}
void SysTick_Handler(void) {
HAL_IncTick();
}
#ifndef MAIN_H
#define MAIN_H
#include "stm32l4xx_hal.h"
#define LED_PIN GPIO_PIN_5
#define LED_GPIO_PORT GPIOA
#define LED_GPIO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
#endif // MAIN_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment