on
2024-11-25
06:00 AM
- edited on
2024-11-27
01:45 AM
by
Laurids_PETERSE
This article is an introduction to STM32 MCU development through the VS Code IDE.
In this article, we cover the following:
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.
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).
2. Install the [STM32 VS Code Extension]. It will look like this picture below.
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.
5. After selecting the icon, select [Settings] from the dropdown menu.
6. Add the paths of the below applications installed on your PC.
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.
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].
2. Select the board that you would like to develop with. In this article, we use the NUCLEO-G071RB.
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.
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].
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.
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.
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.
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 */
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.
2. To start debugging, select [Run] > [Start Debugging].
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.
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.
The [Call Stack] window shows what functions have been called and is helpful in tracing back your function calls.
The [Breakpoints] window displays all the breakpoints that have been set in your code so that you can keep track while debugging.
The [Xperipherals] window allows you to monitor peripherals by displaying their configuration registers and address space.
The [Memory Viewer] allows you to monitor memory contents while debugging.
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.