Setting up a C++ Toolchain for STM32 Microcontrollers

From Double Jump Electric Wiki
Jump to navigation Jump to search

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


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


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!

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

all: encoder_example.elf

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

	rm -f *.o *.elf

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