cancel
Showing results for 
Search instead for 
Did you mean: 

Helpful tips on Using VS Code with STM32

B_Subramanian
ST Employee

Summary

This article highlights some tips on using VS Code with STM32.

In this article, we cover the following:

  • Performing a clean build of your project and deleting the cache
  • Adding source files, directories, and include files to your VS Code project
  • Importing projects from STM32CubeIDE to VS Code
  • Using the Serial Monitor Extension within VS Code

Introduction

In this article How to use VS Code with STM32 microcontrollers, we discussed setting up your VS Code environment, creating a VS Code project, programming, and debugging through VS Code. If you have not already set up your VS Code environment, follow the steps in that article before exploring the content of this article. This article discusses common functions that can be helpful when developing your application through VS Code.  

1. Prerequisites

1.1 Hardware

1.2 Software

2. Development

2.1 Clean, rebuild, and delete cache

When developing your application, you may come across the need to perform a clean build of your project. To remove files that are generated by the makefile, you could use the CMake: Clean or CMake: Clean Rebuild functions to remove the previously generated files.

This can be done by typing >CMake: Clean into the search bar at the top of your screen and selecting the command.

B_Subramanian_0-1733431777905.png

 

 Similarly, when changing configuration files, it is useful to use CMake Delete Cache and Reconfigure.

To perform this command, type >CMake Delete Cache into the search bar at the top of your screen, and once the command pops up, you can select it.

B_Subramanian_1-1733431777909.png

 

2.2 Configuring your project through CMakeLists.txt

Another important thing to know when using VS Code to develop your application is how to add directories, source files, and include files. All of those files can be added through the CMakeLists.txt file. This file is created when your project code is generated, and you should be able to access it from your file explorer on the left panel.

cmakelists.png

Below you can see the locations where you can add project paths which are commented in the file. In addition to directories, source files, and include files, you can also define the build type, change compiler settings, add linked libraries, and make many other configurations through this file.

B_Subramanian_3-1733431777914.png

 

3.3 Using the Embedded Tools extension to convert STM32CubeIDE projects to VS Code projects

As you develop your STM32 applications in VS Code, you may desire to convert some of your existing STM32CubeIDE projects into VS Code projects. This is accomplished through the Embedded Tools Extension in VS Code.

  1. Open [Extensions] (Ctrl + Shift + X) from within the VS Code application
  2. Download the Embedded Tools extension

B_Subramanian_4-1733431777916.png

 

  1. Once the extension is installed, you can type >Embedded Tools: Create Project into the search bar

B_Subramanian_5-1733431777918.png

 

  1. Once you select that option from the dropdown menu, you can select the .cproject file from your existing STM32CubeIDE project.

B_Subramanian_6-1733431777920.png

 

  1. Now, you have successfully opened your existing STM32CubeIDE project in VS Code.

3.4 Using the Serial Monitor extension in VS Code

When developing your application, you may want to use a Serial Monitor to see any USART communication. VS Code offers a Serial Monitor extension, so you can view USART communication within the IDE itself. This avoids the need for a third-party Serial Monitor tool.

  1. Open [Extensions] (Ctrl + Shift + X) from within the VS Code application
  2. Download the Serial Monitor extension

B_Subramanian_7-1733431777922.png

 

  1. Once it is downloaded, you should be able to open it and see the terminal below.

B_Subramanian_8-1733431777924.png

 

  1. Create a new project. When configuring your project in STM32CubeMX, note that USART2 is enabled on PA2 and PA3. We use USART2 to output a message onto the Serial Monitor.

B_Subramanian_9-1733431777930.png

 

  1. Navigate to Core>Src>main.c and paste these two lines of code after   /* USER CODE BEGIN 2 */

    uint8_t UARTbuffer[] = "Hello World!\r\n";
    HAL_UART_Transmit(&huart2, &UARTbuffer, sizeof(UARTbuffer), 1000);​

 

  1. Connect your NUCLEO-G071RB board to your computer with a USB micro-B cable. Make sure to select the correct port on the Serial Monitor and set the correct baud rate configured in STM32CubeMX (default setting is 115200).

 

  1. Click Start Monitoring and Build and Run your code. You should see “Hello World!” appear in the serial terminal.

 

Conclusion

In this article, we discussed many useful points that may be helpful to you when using VS Code to develop your STM32 application. From this article you have learned to perform a clean rebuild, delete the project’s cache, and add source files and dependencies to your project. Additionally, how to create a VS Code project from an existing STM32CubeIDE project, and using the serial terminal extension.

References

 

Version history
Last update:
‎2025-01-13 07:02 AM
Updated by:
Contributors