cancel
Showing results for 
Search instead for 
Did you mean: 

How to use VS Code with STM32 Microcontrollers

B_Subramanian
ST Employee

Summary

This article is an introduction to STM32 MCU development through the VS Code IDE.

In this article, we cover the following:

  • Setting up your VS Code environment to interface with STM32 MCUs
  • Creating a new project through the embedded CubeMX platform
  • Programming and debugging your board
  • Built-in VS Code debugging windows

Introduction

When developing with STM32 microcontrollers, developers can choose between various IDEs to create their application and program/debug their device. VS Code is a great option because many developers are familiar with it, it’s easy to use, and it’s a lightweight application. VS Code can easily be configured to develop STM32 MCU applications and used to program and debug devices as well.

1. Prerequisites

1.1. Hardware

1.2. Software

2. Development

2.1. Setting up the STM32 VS Code Extension

First, you’ll need to download the STM32VSCode extension. This is done through the VS Code application with your PC connected to the internet.

1. In the VS Code application, open [Extensions] (Ctrl + Shift + X).

B_Subramanian_0-1731514123729.png

2. Install the [STM32 VS Code Extension]. It will look like this picture below.

B_Subramanian_1-1731514123731.png

Next, we’ll configure the STM32 VS Code extension and add the paths of the STM32 software that you downloaded from the ST website.

3. Navigate to the [Extensions] tab (Ctrl + Shift +X)

4. Select the cogwheel [Settings] icon for the VS Code Extension.

cogwheel.png

5. After selecting the icon, select [Settings] from the dropdown menu.

B_Subramanian_3-1731514123733.png

6. Add the paths of the below applications installed on your PC.

  • STM32CubeCLT_1.15.0
  • STM32CubeMX.exe
  • STMCUFinder.exe

B_Subramanian_4-1731514123735.png

Now, you have completed configuring your VS Code IDE for development with STM32 MCU.  You only have to configure this one time. Moving forward, your settings are saved.

2.2. Creating a new project in STM32CubeMX

Now, that your VS Code IDE is configured for STM32 development, you can create your first project.
1. Open STM32CubeMX from VS Code by selecting [Project Manager] → [Launch STM32CubeMX].

launch STM32CubeMX.png

2. Select the board that you would like to develop with. In this article, we use the NUCLEO-G071RB.

B_Subramanian_6-1731514546352.png

3. Configure the peripherals for your application. You won’t need to change any configurations for this demonstration but note that PA5 is connected to the green LED.

B_Subramanian_7-1731514546354.png

 

4. Once you have finished configuring your peripherals, select the [Project Manager] tab. 
Here, you can name and designate a folder for your project. Make sure to select [CMake] for [Toolchain/IDE].

5. To generate the files needed for the project, select [Generate Code].

B_Subramanian_8-1731514546356.png

6. Once you’ve generated the CMake project, return to the VS Code IDE. Under the STM32 VS Code extension, select [Import CMake Project] and select the folder you created in STM32CubeMX.

import cmake project.png

7. After selecting the folder, you’ll have to specify your [Project type] as [CMake] and specify the hardware as NUCLEO-G071RB. Then you can select [Import Project] to finish importing the CMake project to VS Code.

B_Subramanian_10-1731514546357.png

Now that you’ve imported the base project into VS Code IDE, you can add in your own user code for your application. In this demo, we make the board LED start blinking.

8. Navigate to Core > Src > main.c. This is where you’ll add the user code for your application.

B_Subramanian_11-1731514546358.png

9. Add the following code to the /* USER CODE BEGIN WHILE */ section

/* USER CODE BEGIN WHILE */

  while (1)

  {

      HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);

      HAL_Delay(100);


    /* USER CODE END WHILE */

2.3. Programming and debugging your board

1. Connect your NUCLEO-G071RB board to your PC using a Micro-USB Cable. You can set breakpoints by clicking to the left of the line number.

B_Subramanian_12-1731514546358.png

2. To start debugging, select [Run] > [Start Debugging].

B_Subramanian_13-1731514546359.png

Now, you should see the LED on your board blinking.

Congratulations! You have now created an STM32 project using the VS Code IDE and modified the code to blink an LED.

2.4. VS Code debugging windows

VS Code also provides many helpful debugging windows that can help you debug more complex applications.

The [Variables] window can help you keep track of the values of different variables while debugging.

B_Subramanian_0-1731524734972.png

The [Call Stack] window shows what functions have been called and is helpful in tracing back your function calls.

B_Subramanian_1-1731524746039.png

The [Breakpoints] window displays all the breakpoints that have been set in your code so that you can keep track while debugging.

B_Subramanian_2-1731524910661.png

The [Xperipherals] window allows you to monitor peripherals by displaying their configuration registers and address space.

B_Subramanian_3-1731524969234.png

The [Memory Viewer] allows you to monitor memory contents while debugging.

B_Subramanian_18-1731514546370.png

Conclusion

In this article, we covered setting up your VS Code environment, creating a new project, programming and debugging your board. Additionally, we covered some useful built-in debugging windows in the VS Code environment. From this point, you can configure more peripherals, add additional user code, and create a more complex custom application.


Stay tuned for a separate article that covers useful add-ons for VS Code that are helpful when developing an application for your STM32 MCU. 

Related links

Version history
Last update:
‎2024-11-27 01:45 AM
Updated by: