How to configure VS Code to build and debug STM32 projects using cubeMX - Windows 10 - stm32

I am new to the realm of STM32 programming and have been trying to find a suitable IDE for quite a while now. I know of all the other IDE's like Keil and IAR but the cost of buying them just to learn is far to steep for me at this point in time.
I have started using VS Code for a growing amount of my development work and I though it would be a good IDE to use for STM32 development. I have found many examples online over the past few days on how to configure the IDE to build STM32 projects but they all seem to be missing important information that I need to properly get the project to compile. It is rather frustrating,
I was wondering if there is anyone that can point me to a complete setup guide on how to set up VS code to work with cubeMX and the arm tool-chain, or if you are feeling really kind, send me a sample project that I can use as a base learn from.
Just some background information, I know how to use cubeMX to generate the base project as well as the associated makefile, I also have the latest GNU-Tools-Arm-Embedded installed.
Thank you in advance for your help

Install GNU Arm Embedded toolchain and add its bin folder to your PATH environment variable.
You will also need a make to execute your makefiles so download Make for Windows. Easiest way is to download the binaries and extract it somewhere on your system. Add it (C:\make-3.81-bin\bin) to your PATH as well.
Create an STM32CubeMX project and select Makefile as Toolchain/IDE.
At this point you will be able to build the generated project by simply using make in the project's root folder.
If you open the project in VS Code you can build using its terminal or you can create a VS Code task to execute the make command. You can bind your task to a hotkey as well to spare some time.
To debug, the easiest way is to install Cortex-Debug VS Code extension. Follow the instructions to configure your debug sessions.

A while ago I had the same question, but did not find anything that I really liked. So I created STM32 for VSCode, it is an extension for VSCode which works with STM32CubeMX generated files and sets up building and debugging for you.

There is a library of python scripts that does just this, it has been released recently with excellent documentation and after testing I can say it works as advertised.
VSCode STM32 IDE
The process is quite straight forward:
Export the files using STM32CubeMX
Cpen the VSCode folder and save it as workspace
Copy the scripts "ideScripts" directory to your project folder
Run update.py
Here is a video on how it works:
VSCode STM32 IDE - Getting Started

There is already very good answer by #Bence Kaulics, based on it add my recent findings.
make command somehow did not work for me in VS Code Terminal. To solve this I installed "Makefile Tools" extension from Microsoft.
instruction link does not work, therefore I add steps how to configure debugging for J-Link.
-> Install Cortex-Debug Extention. -> Download and install J-Link Software from Segger. -> Get SVD file if you want to see peripheral registers. -> Edit launch.json file (see code below). -> Set your executable, paths and device.
{
"version": "0.2.0",
"configurations": [
{
"cwd": "${workspaceRoot}",
"executable": "./build/STM32F103RBT6_Test1.elf",
"name": "Debug Microcontroller",
"request": "launch",
"type": "cortex-debug",
"servertype": "jlink",
"serverpath": "C:/Program Files/SEGGER/JLink/JLinkGDBServerCL.exe",
"armToolchainPath": "C:/Program Files (x86)/GNU Arm Embedded Toolchain/10 2021.10/bin",
"device": "STM32F103RB",
"interface": "swd",
//"serialNumber": "", // if Multiple Debuggers attached
"runToMain": true,
"svdFile": "${workspaceRoot}/device/STM32F103xx.svd",
}
]
}

Related

Cannot find Arduino IDE path. In Visual Studio Code

I wants to use Arduino in vs code Visual Studio Code.
I installed arduino IDE on my local machine on C:\Program Files\Arduino IDE I also added arduino path my Visual Studio Code's setting .json
"arduino.path": "C:\\Program Files\\Arduino IDE",
setting .json
I am using arduino extension.
This is the pic of extension
Any suggestion or recommendations would greatly aid me. I've been struggling to find a solution for this problem for 6 hours.
This was giving me issues for the longest time, too. It seems that the Arduino 2.X IDE doesn't work well with VSCode.
To resolve, use the legacy IDE (Arduino 1.8.X). First, uninstall the 2.X version of Arduino. As of February 2023, you can download the legacy version by going to the Arduino Downloads page and scrolling down to the "Legacy IDE (1.8.X)" section. There's a few different download options. I opted for the one labeled "Windows Win 7 and Newer" since it's a neatly contained executable to install Arduino.
From here, run the executable and install normally. I also reinstalled the Arduino plugin in the Visual Studio marketplace (not sure if necessary, but it doesn't hurt). There's at least three places you could potentially enter the arduino.path:
The one in YOUR PROJECT FILEPATH/.vscode/settings.json
The one in the VS Code User Settings (File>Preferences>Settings> User tab> Search for arduino.path in the search bar)
Same as above, but on the "Workspace" tab instead of "User"
For me, leaving all three empty worked fine. I believe that's because the installer added Arduino to the Windows Path variable. Here's a related post, though, where someone had to configure the path variable in case that doesn't work for you.

vscode debugger does not launch debug console

I am so frustrated that my vscode debugger does not work on the remote servers suddenly today.
Here is the situation.
When I use the starred interpreter by vscode (the recommended one), it works smoothly. However, when I want to use other interpreters, the ones stored in personal folder on the server, the debugger doesn't launch a debug console and dose not enter the file.
Vscode can find and recognize the interpreters and I can use those interpreters to run scripts but can NOT DEBUG.(on the remote server) However, it works fine on my local machine with multiple python interpreter.
Can anyone give me some help? I have tried uninstall vscode completely on my end but still not work. Thank you so much!!
The Python extension debugger in VSCode requires a minimum Python version to run properly. However, this is not documented at all in the changelog at https://github.com/microsoft/vscode-python/releases, but you have to find out for yourself. For example, at the time of writing, version 2022.12.0 doesn't harmonize with Python 3.6.8.
As a workaround you have 2 options
Upgrade your interpreter to a newer version
Downgrade the Python extension to an older version
For the latter, go to the extensions page and under the Uninstall button you will find Install another version.... This makes it easy to try out what works best for you.
Just in case stumbles upon this question and the accepted answer doesn't work for you:
Try downgrading the Python extension version in VSCode. That fixed the issue for me. I picked a random version from ~2 months ago (when I wasn't having the issue). You might need to try a few versions though.
This workaround might work for you: use the interpreter that works for you but in the launch.json configuration file add a
"python": "/path/to/the/python/you/want",
I share the same experience. It seems that vscode fails to start the remote debugger with the interpreter I want but succeeds with the system interpreter.
For example:
"configurations": [
{
"name": "config_name",
"type": "python",
"python": "/path/to/desired/python",
"request": "launch",
"program": "/path/to/python/script.py",
"console": "integratedTerminal",
},
]

NIOS II IDE unsuably slow

I've installed Quartus and NIOS II IDE on my Linux machine. Originally I tried importing an existing NIOS II Project into Eclipse, but it just sits there spinning away and eventually tells me it can't import the project because it already exists.
I tried installing everything on a Win10 machine and the project imported OK. I did notice that some of the paths in the orignal project have backslashes. So, for example:
#include "..\subfolder\include_file.h"
Kind of thing. I wonder whether maybe it was that was causing issues.
So, I then tried creating an hello world NIOS Processor in Quartus and making a NIOS II project from scratch. Every step along the way with Eclipse was grindingly slow, but eventually worked up until the point I was able to hit Finish at which point it's just hanging with the little circle with red and blue arrows spinning round and round.
I also have a pop up window with a long list of:
Remote System Explorer Opertion
lines, and at the top it says:
The user operation is waiting for background work to complete
It seems like everything is installed correctly, I can open the NIOS II Eclipse IDE from Quartus for example. Quartus itself works nicely.
I used these instructions to installed Eclipse:
Is anyone able to give me any pointers as to why this is so incredibly slow please? My Linux machine is pretty high spec and flies. Nothing else whatsoever even vaguely struggles on it.
If there's anything I can try to give diagnostic info, am more than happy to supply. Thanks!
I found a solution here
https://askubuntu.com/questions/761604/eclipse-not-working-in-16-04
To test if this fix works, try opening a terminal, and doing
$export SWT_GTK3=0
then run eclipse-nios2. That is, do something like
$ ~/intelFPGA_lite/20.1/nios2eds/bin/eclipse-nios2
If that fixes the problem then to make the fix permanent edit eclipse.ini which for me is in intelFPGA_lite/20.1/nios2eds/bin/eclipse_nios2
In that file, between the lines openFile and --launcher.appendVmargs
insert
--launcher.GTK_version
2
So it reads:
openFile
--launcher.GTK_version
2
--launcher.appendVmargs
and save.
Worked for me!
Try using Visual Studio Code instead. It can do (almost) everything that Eclipse can do, with the added benefit that you can debug remote. E.g. in my setup, I develop exclusively using macOS. I have Quartus installed in a Ubuntu 20.04 VM and VSCODE allows me to develop and debug as if I'm running Quartus natively in macOS.
I'll provide a rough 'how-to' below.
I strongly suggest using a Ubuntu environment for Quartus (other linux environments should work too: I've tested Arch and Manjaro). The Windows install is a royal pain to get working, regardless of IDE choice. I haven't tried the Windows version since 20.1 but nothing worked out of the box and it took many hours or messing with config files to fix it. Also, the Windows version isn't native anyway - it's actually running in Ubuntu via WSL.
Make sure you have installed your required version of Quartus - this should work for Pro, Standard and Lite editions. You can skip the manual installation of NiosII Build Tools for Eclipse if you'd like but I would actually recommend installing it. It can still be useful, if only for creating a new project, BSP etc.
Install VSCODE along with the C/C++ Extension provided by Microsoft (and the Visual Studio Code Remote - SSH extension if you intend to use that feature)
Create a new project using Eclipse (or skip, if you already have a project)
Open VSCODE, select open and navigate to the software directory of your project.
VSCODE will open your project and you should see your application directory and BSP directory.
From here, you can work on your code with full linting, auto completion etc.
If you need to regenerate your BSP, you can either do that using Eclipse or just run the command-line tool instead.
Compiling
If you allowed Eclipse to create your project for you, it will have produced a makefile in your application directory. Compiling is as simple as running 'make' from the application directory. I like to keep a terminal window open within VSCODE so I can run various tools as I work so this is usually how I can compile code. If, instead, you'd like to integrate this into VSCODE, you can define a task (https://code.visualstudio.com/docs/editor/tasks).
Eclipse automatically adds new sources that you create to the makefile. VSCODE won't do this - however, I personally don't think this is a bad thing. I don't like IDEs messing with my makefiles, I want fully control over them myself. So when you create a new source file, just remember to add it to the makefile. There is probably a way to get VSCODE to add sources files automatically (you can run commands on save etc) - but I have no motivation to try to figure this out.
Debugging
It is also possible to debug Nios2 software live on the target via the USB-Blaster using VSCODE. To do this, open the launch.json file in the .vscode folder of your project. If this file does not exist, just create it. The path should be:
<project root dir>/.vscode/launch.json
Copy the following into the launch.json file:
{
"version": "0.2.0",
"configurations": [
{
"name": "app",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/app/app.elf",
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"MIMode": "gdb",
"miDebuggerServerAddress": "localhost:2334",
"miDebuggerPath": "/PATH/TO/intelFPGA/20.1/nios2eds/bin/gnu/H-x86_64-pc-linux-gnu/bin/nios2-elf-gdb",
"debugServerPath": "/PATH/TO/intelFPGA/20.1/quartus/bin/nios2-gdb-server",
"debugServerArgs": "--tcpport 2334 --reset-target --tcppersist",
}
]
}
This file is a list of debug targets and settings. You can add any number of configurations here, but easiest to just start with one for now. The configuration above is called 'app', you can change this to suit your own project if you wish.
Update the "program" entry with the path to your applications elf
file
Update the "miDebuggerPath" path with the path the nios2-elf-gdb on
your system. This is the gdb client for Nios2
Update the "debugServerPath" path with the path the nios2-gdb-server.
This is the gdb server for Nios2
Save the file and now click on the "run and debug" button on the left
side control panel in VSCODE
You'll see play button at the top of the screen next to the configuration you just defined in launch.json. Hit the play button - this will start an instance of the gdb server, followed by the gdb client and connect the two. If all goes well, your target will run and stop in main.
VSCODE will provide you with a set of debug tools for stepping, watching variables, call stack - all that good stuff. On the right hand side, you can view the debug console which will allow you to enter commands directly into the gdb console. Note, you have to prepend your commands with -exec. E.g. to print the value of a variable x, you would type:
-exec print x
Caveat
There is a problem with this debugging method: nios2-gdb-server doesn't always exit cleanly and as a result the port doesn't close. If you try to start another debug session, the server won't start because the port is in use. Eclipse solves this problem by randomizing the port everytime it launches the gdb. I have not found a way to do that VSCODE yet.
I find easiest way around this is to run the gdb server manually when I need it. Comment out the two 'debugServer' lines above. Open a second terminal in VSCODE and run:
nios2-gdb-server --tcpport 2334 --reset-target --tcppersist
This will keep the server running even when you stop debugging. If you start another debug session, it will reconnect to the same server instance.
Update: rioV8 provided a helpful solution to this problem which involves using a vscode extension to generate a random environment variable which can used be used in place of the port numbers. The solution is here.

How to debug Visual Studio Code extensions?

What are the best ways to troubleshoot and debug Visual Studio Code?
I have encountered conflicts in some snippet/suggestion extensions while editing in Visual Studio Code and want to try to find the root cause.
P.S. I would appreciate any experience in resolving conflicts between extensions. Feel free to chime in if you have encountered this issue before (built-in suggestions completely overwriting extension suggestions after a couple of seconds)
Taken from https://code.visualstudio.com/docs/extensions/developing-extensions:
Running and debugging your extension
You can easily run your extension under the debugger by pressing F5.
This opens a new VS Code window with your extension loaded. Output
from your extension shows up in the Debug Console. You can set break
points, step through your code, and inspect variables either in the
Debug view or the Debug Console.
To debug installed Visual Studio Code extensions, first navigate to the installed extension's project folder.
%USERPROFILE%\.vscode\extension\${PublisherName}.${ExtensionName}-${VersionNumber}\
This folder is contained in your user profile or root folder. It may also be called .vscode-insiders depending on the version of Visual Studio Code you have installed.
This project folder should already have the debugger set up and you can just press F5 in a project source file to open the [Extension Development Host] as originally assumed.
For more information you can check the <projectroot>/.vscode/launch.json to find the launch configurations detailing the use of the [Extension Development Host] if you need to fine-tune these settings.
Example taken from auto-generated extension debugger settings launch.json:
// A launch configuration that compiles the extension and then opens it inside a new window
{
"version": "0.1.0",
"configurations": [{
"name": "Launch Extension",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": ["--extensionDevelopmentPath=${workspaceRoot}"]
}]
}
From there it's just a simple(~) matter of adding breakpoints and/or console logs to work out the cause of extension-related issues.
~ Edit: I have enough rep to embed images now 😘
For more information on general development of Visual Studio Code extensions see the official docs here:
https://code.visualstudio.com/docs/extensions/developing-extensions#_creating-your-own-extension
To view the errors for someone else's extension you installed normally:
In the menu, select View > Output
Select the extension in a small dropdown at the top right of the output window

How to configure Visual Studio Code to build a project with IAR compiler?

I want to use Visual Studio Code as my main IDE, but I want to compile my code with IAR's compiler.
How could I configure VSCode ?
I found this extension but the documentation is too poor.
Yes, the extension politoleo.iar is indeed NOT good.
-> I found a better one (will try it in future by myself):
VSCode plugin: pluyckx.iar-vsc
IAR For Visual Studio Code - Visual Studio Marketplace
Good document
Welcome to IAR-VSC’s documentation! — IAR-VSC 1.2 documentation
doc's Github source: pluyckx/iar-vsc
IAR related official Technical Note
Using Visual Studio Code with IAR Embedded Workbench
Enjoy it !
There are now two official IAR plugins for VSCode which make things much easier:
For building and development: https://marketplace.visualstudio.com/items?itemName=iarsystems.iar-build
For debugging with C-Spy: https://marketplace.visualstudio.com/items?itemName=iarsystems.iar-debug
https://www.iar.com/knowledge/support/technical-notes/general/using-visual-studio-code-with-iar-embedded-workbench/
What I did is setting some environment variables in settings.json;
then in tasks.json I created shell tasks that call the IarBuild.exe & IarIdePm.exe with appropriate values (see the documentation on the command line options)...
So now, I can use VSCode for navigation & editing, but the compilation is done by the EWARM.
The only thing that I haven't figure out yet is how to make C-Spy work from within VSCode...
Currently when I want to debug, I just launch the EWARM and debug from there...
here is a screenshot:Screenshot of settings.json and tasks.json
For this, you can port you project to the custom build system (non iarbuild), e.g. a best choose is the Qbs. The Qbs support a lot of architectures and the toolchains (include IAR, KEIL and so on). In this case you can then use any IDE which support the Qbs, e.g. such as VSCode or QtCreator, and to open your project in any of these IDEs.
Both IDEs support only the GDB debugging, because the C-Spy debuger is a proptietary technology, which has not the public datasheets.