Helpful tips on using VS Code with STM32
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Email to a Friend
- Printer Friendly Page
- Report Inappropriate Content
on
2025-01-15
8:00 AM
- edited on
2025-01-28
4:58 AM
by
Laurids_PETERSE
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 a previous 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
- NUCLEO-G071RE
- 1 USB Cable Type micro-B
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.
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.
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.
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.
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.
- Open [Extensions] (Ctrl + Shift + X) from within the VS Code application
- Download the Embedded Tools extension
- Once the extension is installed, you can type >Embedded Tools: Create Project into the search bar
- Once you select that option from the dropdown menu, you can select the .cproject file from your existing STM32CubeIDE project.
- 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.
- Open [Extensions] (Ctrl + Shift + X) from within the VS Code application
- Download the Serial Monitor extension
- Once it is downloaded, you should be able to open it and see the terminal below.
- 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.
- 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);
- 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).
- 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
- VS Code application
- Serial Monitor extension
- Embedded Tools extension
- Knowledge article: How to use VS Code with STM32 microcontrollers