STM32, Mac, and VSCode
Posted On March 24, 2023
As I am searching for debuggers and possible future replacements for PSOC5 (since Infineon appears to dislike Cypress innovation), I stumbled across STMicro. STMicro has a development environment called STM32CubeIDE. There is also a STM32CubeMX application. Get both of them.
On the Macintosh, you can download that from st.com with a free account and it will install. When run the first time, the STM32CubeIDE will also install everything necessary to do development. It has embedded a version of ST32MCubeMX as a plugin in the Eclipse IDE. That setup works for the STM32CubeIDE, but not for generating an external build for systems such as VSCode.
BluePill Versions and YouTube
I followed a video on YouTube, allowing for differences between Window and Mac, using a STLink Clone and BluePill from Amazon. The video is here: https://www.youtube.com/watch?v=kXg467nVd_A. The Amazon package I purchased is here: https://www.amazon.com/dp/B0B18KDLCG?psc=1&ref=ppx_yo2ov_dt_b_product_details. I purchased that package of 2 STlinks and 2 bluepills so I could make mistakes and burn something up. The STM32F103C6T6 on that board has less flash memory (32K) than the STM32F103C8T6 (64K), so it may not be able to run FreeRTOS or BlackMagic, but is good to play around with at the first.
How to determine the STM part? Easy, I use a flashlight and shine it sideways across the surface of the part. The part number is printed on the IC. It is hard to read, printed in a weird font, but it is there.
Start Out With the CubeIDE to Prove It Works
The CubeIDE will let you build and debug a small project. I followed the previously mentioned BluePill video to get the first build and debug environment using a clone STLink. Their setup is clunky, but it works, and following a video is the best way to shortcut the “ST Way” learning experience. ST’s rendition of Eclipse IDE leaves much to be desired. It supports dark mode, but normal IDE goodies that VSCode provides are clunky or missing.
To use the STLink V2 clone under the CubeIDE, I had to use OpenOCD as the debug type. Once I did that, the STLink upgrade option showed up. However, it could not upgrade the debugger without some extra steps. It could not put it into the programming mode. After (google) research, I did it again, and while the programmer window was up I unplugged and plugged in the clone STLink and voila! it programmed with updated firmware. Debugging worked after that.
VSCode and STM32CubeIDE
If you want to use the CubeIDE to generate a project that will work with VSCode, too bad. The ST32MCubeMX plugin is broken for generating an external makefile project. Eclipse is also broken for code completion (called intellisense in VSCode), and that deficiency cannot be fixed through configuration dialogs. Give Up, move on.
VSCode and STM32CubeMX
You can’t install the downloaded STM32CubeMX on the Macintosh by double clicking on the app icon. However, if you open a terminal change to the directory where the installer app is, you can get into the executable directory and run it like this:
Then it will install.
I moved the installed app and the CubeIDE into an STMicro directory in my Applications folder. Just personal preference.
When you use the CubeMX application and select Makefile type of application you are pretty well setup to use the VSCode as a build and debug IDE, but only if you have the stm32-for-vscode extension installed.
Default use of the CubeMX project builder is to save the .ioc file created during your processor setup into your development directory for that project. I called my directory blink. Make sure to click the button to generate files into that directory. If it freezes during file generation, it is because the directory already exists. That implies your file generation has to be done at the same time as the project is first created, or you have to get very creative.
The CubeMX automatic code generation will save you a lot of time. It is similar in scope to the PSOC Creator application’s schematic build. It makes using the STM32 processors fairly simple once you get used to their way of doing things.
Install VSCode Extension
If you install the PSOC extensions listed in my previous posts (Intellisense, arm assembly, cortex-debug, open in application, output colorizer, Tasks) then you should have the basic ones you want installed.
Now install stm32-for-vscode into your VSCode extension list from the extension market place. I found it worked alongside the current PSOC used extensions without a problem. If it cannot find your current ARM GCC compiler system, it will prompt you to install it. Since all the ARM apps are installed, it left me alone. You may wish to install Cortex-Debug: Device Support Pack – STM32F1, but it is not needed.
Prepare For Using VSCode
Using CubeMX (NOT CubeIDE), follow the video and select the options for the project. Be sure to select ceramic resonator, debug Serial Wire, and set up the clock properly. Select PC13 as GPIO_out. (I am recreating the blinky project from the video, and will walk through the VSCode part.) On Project manager, on the ToolChain/ IDE, select Makefile. Click on the Code Generator Blue Button area on the left and select “Copy all used libraries into project folder.” I set all free pins as analog, Generate peripherals as separate c/h files (aids in adding on later, your mileage may vary).
Click on the upper-right “GENERATE CODE” button. Again, if it freezes during code generation, that is because the directory already exists. Delete the directory and try again, after quitting the CubeMx app.
Open the directory (blink in my case) using VSCode. If everything is installed properly, then a new icon will show in the left hand bar, a square with an ST in it. Click on it. Then click on the Build line under the “STM32 FOR VSCODE:…” heading. This will trigger the STM extension to setup openocd.cfg, a .yaml file and a .vscode directory, and will build your project. The extension has a link to its github directory for more details of the files involved.
In the VSCode explorer, the Core->Src directory has main.c. If you find during your working with VSCoide that “__IO is not defined” shows up in VSCode intellisense, then go into Drivers->Core->Device->Include in the VSCode Explorer window and open stm32f1xx.h and uncomment the #define around line 71 so that #define STM32F103x6 is available to VSCode and the compiler. This should be the default in the .vscode->c_cpp_properties.json file. That file should be followed by VSCode, but sometimes VSCode gets a headache.
Once you have done your first build, click on the ST Icon again and click Debug STM32. The system will automatically select your STLink you programmed and set up under the CubeIDE previous to this point. It will also activate the task bar at the bottom. That more or less finishes the setup for the STM32 debugging, at least with STLink.
The application will begin running. The Green Led will come on on the Blue Pill board. Now follow the You Tube Video and add the HAL_GPIO_WritePin() code and HAL_Delay() code and watch your board blink. To add a breakpoint, click to the left of the line numbers, just like with the PSOC.
Enjoy playing with STM32! I suspect they will give Infineon a run for their money, since Infineon does not understand the usefulness of embedded FPGA technology, and ST’s auto build code actually creates useful stuff that compiles properly. Their prices are also lower.
ST code also works properly the first time, at least for the F1 series of processors. That was the other strength of the PSOC Creator which was unique to Cypress. An MCU maker paid attention. Too bad Infineon has not.