Setting up a C++ Toolchain for STM32 Microcontrollers

From Double Jump Electric Wiki
Revision as of 01:30, 18 November 2020 by Poofjunior (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Much of this guide is in great debt to the 1bitsy and libopencm3 projects.

Background

C++ offers the flexibility of object oriented programming, a bump over C when it comes to working with microcontrollers.


Setup

On Ubuntu, install the arm compiler with:

sudo apt-get install gcc-arm-embedded

On other systems, you can download it manually from the Arm Developer webpage.

Clone/Download libopencm3.

Compile libopencm3. Depending on your architecture, you may need to add additional specs to ask for hardware-based floating point arithmetic, but Cortex M4 defaults to using it if you simply invoke make with no arguments.


Write some code! When youre ready, you can compile it with a simple makefile like this one:

# libopencm3 must be installed and compiled first!
LIBOPENCM3_DIR=../../libopencm3


CXX=arm-none-eabi-g++
CXXFLAGS=-g3 -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 \
    --specs=rdimon.specs -I$(LIBOPENCM3_DIR)/include -DSTM32F4 -nostartfiles \
    -std=c++11
LDFLAGS=-L$(LIBOPENCM3_DIR)/lib -Wl,-T,1bitsy.ld -lopencm3_stm32f4

all: encoder_example.elf

%.elf: %.o
	${CXX} ${CXXFLAGS} $< ${LDFLAGS} -o $@

clean:
	rm -f *.o *.elf

(Note that, in the above Makefile, some indentations are tab characters, not spaces.)

References