Booting A Cortex M with NO IDE : like a caveman : Part 1 Toolchain

 


I like to jump into things and learn as I go so ill do the same for you. Ill explain what is necessary when it becomes relevant. However, I also dont explain simple things like how to extract a file or how to add paths to your system variables, this can be googled. 

Add path to windows environment

Add path to linux environment

I am going to proceed under the assumption that you have already installed one of the following: STM32CubeIDE, STM32CubeMX, or STM32CubeCLT. These software packages from STMicroelectronics come bundled with their own versions of OpenOCD and the ARM GNU Toolchain, both of which are specifically tailored to work seamlessly with STM32 microcontrollers.

Now, you might be wondering why these packages are relevant, especially if we’re not using the integrated development environment (IDE) itself. We need OpenOCD and ARM GNU tools to compile and debug our application. When you're working with a newly released STM32 chip, the publicly, or mainstream, available versions of OpenOCD might not yet support it. In such cases, the configuration files and toolchains provided by STMicroelectronics in their software packages are invaluable, as they ensure compatibility with the latest STM32 devices. However, this post is about booting a Cortex M MCU that does not necessarily have to be an STM32

At this point, you have three options: 

  1. Follow the Steps Provided (recommended to follow this post): You can continue with the instructions I will provide, which involve downloading and setting up the required tools independently from mainstream sources, which is fine unless you're using a new STM32 released yesterday or something, you lucky dog!! The good thing about this approach is that you'll have an OpenOcd version tailored for most microcontrollers not just STM32
  2. Use the Pre-Installed Tools: Alternatively, if you already have STM32CubeIDE, STM32CubeMX installed, you can skip the downloading steps. Instead, you can use the versions of OpenOCD and the ARM GNU Toolchain included in these packages. To do this, you’ll simply need to locate the relevant files within your installation directory.
  3. Use STM32CubeCLT : Download STM32CubeCLT from the ST website it contains STLinkGDBServer (OpenOcd alternative) and ARM GNU Toolchain.

Toolchain setup

First order of business is to download the ARM GNU toolchain, which includes our cross-compiler, linker, and debugger. It also packs a bunch of other helpful utilities. Just a heads-up: these are all command-line tools—no GUIs here. What do you think this is, 2024?

I like to use xPack-dev-tools because they keep up with their work.
Download the toolchain relevant to your operating system at the link below and extract it somewhere, I prefer a path close to C:/ on Windows or close to home on Linux

Location from STM32CubeIDE installation:
C:\ST\STM32CubeIDE_1.15.0\STM32CubeIDE\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.12.3.rel1.win32_1.0.200.202406191623\tools\bin

Next download xPack's OpenOCD application as well, same procedure for extracting and keeping its path short. Stick to  the latest version. If you're on Linux you can just install it from your apt manager

Location from STM32CubeIDE installation:
C:\ST\STM32CubeIDE_1.15.0\STM32CubeIDE\plugins\com.st.stm32cube.ide.mcu.externaltools.openocd.win32_2.3.200.202404091248\tools\bin

Now that you have the necessary applications to build an executable you should add them to your environment variables / path so that you can call them from the command line no matter what directory your shell is in. You will add the path to the bin folder of openocd and the bin folder of ARM GNU toolchain. Additonally you will want to add the 

Once you do that open up your shell and type in : openocd --version


Do the same with the ARM GNU tools, note the command for the ARM GNU toolchain is kind of ugly looking and long but thats how it is.



Perfect, I hope! Next you need something pretty to type in, I like to use VSCode because its easy to customize and all the cool kids are using it. So go ahead and download that and get it installed. 

We should also install make so we can automate building our project because manually building things is only fun once when reading an online tutorial and then never again.

You can get make from the link below. You should also add its bin folder to your environment path


Next we need to get some premade files like the device header with all the register definition for the microcontroller that will be used. I am using an STM32G071RB Nucleo board.  We will also need to grab some CMSIS files made by ARM that describe the Core registers. If you poke around enough you will find some assembly start up files but ignore those since we will write out own in C instead.
We can get the files we need from ST's github page

Do not bother cloning the entire repo just grab the following files;
From the following directory on ST's page you will need

STM32CubeG0/Drivers/CMSIS/Include/
  • core_cm0.h: This is the primary header file for the Cortex-M0 core. It contains the core definitions, register structures, and intrinsic functions necessary for programming the Cortex-M0.
  • cmsis_compiler.h: This file provides the necessary compiler-specific definitions that are compatible across different toolchains. It's used to ensure that the CMSIS (Cortex Microcontroller Software Interface Standard) code works across various compilers.
  • cmsis_gcc.h: Since we are using the GNU ARM toolchain (GCC), this file contains GCC-specific macros and definitions that you'll need.
  • cmsis_version.h: This file contains the CMSIS version information and is generally included in CMSIS projects for consistency.
Usually the CMSIS repo will also link to the specific device addressed by that repo, simply follow the link and head over to the device repo for you chip because we need to get something from there as well.


From the STM32G0 Repo
We will grab the device header file because it has definitions for all the registers, this fill is generated by the chip vendor and you probably dont want to type this out by hand, heck they dont even do that, these are probably generated from SVD files.
Grab the following files: 

cmsis_device_g0/Include

  • stm32g071xx.h: device header file with register definitions
  • stm32g0xx.h: This is the generic header file for the STM32G0 series of microcontrollers. It provides common definitions and macros that apply to the entire STM32G0 family, ensuring that your code can be easily ported across different STM32G0 devices
  • system_stm32g0xx.h: This file contains the system configuration functions and setup routines for the STM32G0 series. It typically includes the system clock configuration and other critical low-level initialization code that needs to be executed before your main application can run.
Finally below are some helpful links to documentation that will be helpful in this journey. 

First up is the manual for ARM GCC which is our compiler, this will help use figure out which flags to pass in order to compile our code for our specific chip.

Next are the reference manual and datasheet for your specific chip

That is it for now. In the next post we finally open up VSCode and start out project.

Comments

Share your comments with me

Archive

Contact Form

Send