Program STM32 using VScode
If you are like me, then you will find it terrible to use ten different IDE’s which will all have their own pros and cons. For me this was the reason I never started with the STM32. However it did come with an form of intellisense, I found it a hell to use. If you want a suggestion you needed to hit ctrl+enter every single time which is just awfull for a STM32 beginner. Well in VScode this isn’t a problem anymore, and it made enjoy programming again.
You could skip the first 8 steps and let the stm32-for-vscode extention, install all the nesserary files. But using the method I adopted from Shawn Hymel and his tutorial on installing the toolchain, compiler and sdk for the PI pico, you know which part is in which location. This way you could easily use the same software for more ARM based mcu’s. If you allready installed the arm toolchain and mingw using Shawn Hymel’s tutorial, you could skip the first 6 steps.
step 1: Create a folder on you C drive and name this folder: “VSARM”
step 2: Create two folders in the “VSARM” folder, called: “armcc” and “mingw”. You should now have the following folder structure:
step 3: Go to: https://developer.arm.com/downloads/-/gnu-rm and download the latest ARM GNU toolchain for windows, at the time of writing this was 10.3-2021.10 and I downloaded the windows 32-bit installer.
step 4: When running the installer you will be ask to choose the location to which the files will be installed. Make sure you select the armcc folder that we have just created, and click install. After the installation is completed you will see some checkboxes, make sure you checked the “Add path to environment variable” box.
step 5: Now it is time to install MinGW. Go to: https://sourceforge.net/projects/mingw-w64/files/ and download the: “i686- posix-sjlj” version. After you downloaded this extract the files to our mingw folder. If you did it right you should now have the following path: “C:\VSARM \mingw\mingw32”
step 6: Open windows command prompt and enter the following into the prompt (you could skip this possibly):
echo mingw32-make %* > C:\VSARM\mingw\mingw32\bin\make.bat
step 7: Now download openocd from: “https://github.com/xpack-dev-tools/openocd-xpack/releases/, you should download the “win32-x64.zip” and extract all the files to our openocd folder that you have created.
step 8: To be able to execute the tool we just installed we need to add some environment variables, in dutch this is called: “omgevingsvariabelen”. To do this press start and start typing: “environment variables” and hit ENTER. If it doesn’t work open the windows 10 settings and navigate to system > info, on the right side you will see blue text, one of them is: “Advanced system settings”, click on that one. Now you will see a new window, at the right bottom of this window, just above cancel, you will find a button: “Environment variables” click on this button and you will get again a new window.
Navigate to “User variables for ” and select “Path” now click “Edit”, now you wil get a new window, again. Make sure that the following paths are listed:
If this is not the case, add them by clicking “New” and fill in the correct location.
step 9: Now it is time to install VScode, so go to: https://code.visualstudio.com/#alt-downloads and download the latest stable system installer for your OS (windows 64 bits in our case).
step 10: Go through the first installation steps, when the setup askes you which additional tasks should be preformed, make sure you have selected: “Add to PATH (requires shell restart)”.
step 11: Finish the install of Visual Studio Code, when it is finised launch VS code.
step 12: When you have launched VS code, go to the blocks icon on the left side of the window, hit: “CTRL + SHIFT + X” or press ctrl+shift+P to open the command pallete, type in the command pallete:
View: Show Extensions
step 13: Now that you have opened the extentions tab search for: “stm32-for-vscode”, this (https://marketplace.visualstudio.com/items?itemName=bmd.stm32-for-vscode) extentions is created by: “Bureau Moeilijke Dingen”. When you found the right extention install the extension by clicking the install button on the extention page.
step 14: If you skipped the first 9 steps you could also skip this step. If you followed the first 9 steps, you need to setup the paths so the “stm32-for-vscode” extension knows where to search the ARM toolchain, our MinGW compiler and OpenOcd. To do this navigate to the extension settings by clicking the cogwheel on the extension page inside VS code. A menu will popup, in this menu click: “Extension settings”. A new window will open, in this window you need to enter paths as follows:
-Stm32-for-vscode: Arm Toolchain Path: “C:\VSARM\ARMCC\\BIN”
-Stm32-for-vscode: Make Path: “C:\VSARM\MINGW\MINGW32\BIN\MAKE.BAT”
-Stm32-for-vscode: Open OCDPath: “C:\VSARM\OPENOCD\…”
after you filled out the right paths close the settings window.
step 15: After you installed the extention “stm32-for-vscode”, search in the search bar for the extention: “Cortex-Debug”, this (https://marketplace.visualstudio.com/items?itemName=marus25.cortex-debug&ssr=false#overview) extension is created by: “marus25”. Just like with the earlier extention install this one.
step 16: Last but not least install the extention: “C/C++” this (https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools) extension created by: “Microsoft”.
step 17: Now it is time to head over to the st download page (https://www.st.com/en/development-tools/stm32cubemx.html#get-software) to download STM32CubeMX for windows, to do so you need to sign up with you email adres. After signing up you will get an email with the download link, this could take along, long time.
step 18: Well when you finally received the email just install STM32CubeMX, it is mostly self explainatory.
step 19: If you skipped the first 9 steps you need to install the toolchain, compiler and OpenOCD. STM32-for-vscode could do this for you. To do so create a project using STM32CubeMX, and open the freshly created project folder in vscode. If everything is right the STM32-for-vscode extension should also load, when it is loaded click on the extention icon in the left bar. If the extention couldn’t find the toolchain, compiler or OpenOCD, you will see three blue buttons, you could choose to enter the Paths manualy or let the extention install the components, it thinks that is’s missing.
step 20: You should be ready to build flash and debug a STM microcontroller. If you get error 0xC0000142 from arm-none-eabi-gcc.exe, check if you have Winavr installed, because this could trigger the error. If you don’t use winavr anymore remove it from your PC.
If I missed anything please let me know, and I will change it.
Leave a ReplyWant to join the discussion?
Feel free to contribute!