Bare-metal programming

Bare-metal programming deals with programming directly on the ARM microcontroller without an operating system or a library. It runs entirely standalone. This way, there is no need to use hardware abstraction such as HAL and external libraries, nor CubeIDE.

Arm GNU Toolchain for linux hosts

To proceed, we need just a C compiler and a few tools (linker, flasher, …).

The most popular tool for compiling assembly code for arm is the GAS compiler from the GNU project, which is part of the Arm GNU Toolchain. So, let’s install this set of tools.

To do this, go to the official website of the Arm company at https://developer.arm.com/downloads/-/arm-gnu-toolchain-downloads.

GNU Arm Embedded Toolchain for ARM32

You just need to download the toolchain with the right host (here x86_64 linux) and the right target (here AArch32 bare-metal).

linux hosted

ABI means Application Binary Interface. The E stands for Embedded.

Now it’s time to decompress the tar.xz file:

unzip

In the bin directory, we find:

eabi programs

Let’s check the ARM as (assembler) version in the bin directory with the following command

arm-none-eabi-as --version
GNU assembler (Arm GNU Toolchain 13.2.rel1 (Build arm-13.7)) 2.41.0.20231009
Copyright (C) 2023 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or later.
This program has absolutely no warranty.
This assembler was configured for a target of `arm-none-eabi'.

The following version command is no longer listed in the help and remains blocked:

arm-none-eabi-as -v

Execute programs from anywhere in Linux

Now, we have a problem. We would like to run the assembler from anywhere in the Linux host. But the eabi programs are unknown outside of the bin directory. To execute programs from anywhere in Linux, users need to set the PATH environment variable, which contains the locations where Linux searches for executable files. The directories that contain the programs in Linux generally include /usr/bin, /usr/local/bin, and /sbin.

Open the ~/.bashrc file. At the end of the file, insert the following instruction:

export PATH=$PATH:$HOME/TOOLCHAIN/bin:$PATH

Microcontroller selection

A STM32L476 microcontroller will be used.

We select the Nucleo-L476RG board with a STM32L476RG microcontroller

alternate text

Microcontroller documentation

The first source of information is the STM32L476 datasheet. This document contains all the needed hardware informations about L476 microcontrollers.

You will probably find that an important document is the STM32L476 reference manual RM0351.

In particular, you can find in it the memory addresses of the periphery registers.

The programming manual PM0214 may be useful, not really for programming, but for getting useful information, in particular about interrupts (NVIC, SysTick timer, timers).

You can find others documents, such as Application Notes, at the STM site documentation tab.

warning

Take a look at the Errata sheet