Baremetal programming of the STM32f042 from ST Microelectronics


ST Microelectronics recently (late 2015) released a Nucleo board in the form of an Arduino Nano containing an STM32F042 microcontroller. This makes for a great little development environment:
It has plenty of RAM and ROM
It has lots of timers
It has an ADC
It can be plugged into a breadboard.
The board is mbed enabled which means that it behaves like a mass storage device when attached to your computer. You can download the output of the mbed compiler to it simply by dragging and dropping the executable onto the "disk" that it emulates. This site however takes a different approach and uses the built-in STLink-V2 debug interface to program and debug the device.
All work was carried out on Fedora 23 starting late in 2015. Openocd (version 0.11 snapshot) was used to connect the board to the ARM GNU debugger (arm-none-eabi-gdb). GCC and GDB cross compilers for ARM were obtained from
openocd was launched as follows:
sudo /usr/local/bin/openocd -f /usr/local/share/openocd/scripts/board/st_nucleo_f030r8.cfg 
Note the board type is not quite the same as the Nucleo '042 but they are both Cortex M0's and both include the same debug interface so this works out fine.
As usual with this site, all programming examples are written to be as simple as possible and be completely self contained in a single directory.

Sample Code

Blinky. This program simply flashes the attached LED using a software delay
Debug Test. This program shows how you can use semihosting to trap unhandled interrupts. It generates a hard fault by writing to unconnected memory. A default interrupt handler is then run which prints the IPSR (exception number) and offending address using semihosting over the SWD interface - follow the instructions at the top of main.c to make this work
Serial communications. This program uses the built-in serial interface in the nucleo board to communicate with a host PC. The serial interface connects to USART2 on the STM32F042 MCU.
Driving a WS2812B LED using SPI.. This project uses SPI to send data to a WS2812B LED. It is described in detail over at my blog
Using DMA with SPI to drive WS2812B's

Back to my ARM project page