Difference between revisions of "Setting up a C++ Toolchain for STM32 Microcontrollers"

From Double Jump Electric Wiki
Jump to navigation Jump to search
(Created page with "Much of this guide is in great debt to the [https://1bitsy.org/ 1bitsy] and [https://libopencm3.org/ libopencm3] projects. ==Background== C++ offers the flexibility of object...")
 
Line 16: Line 16:
 
Clone/Download [https://github.com/libopencm3/libopencm3 libopencm3].
 
Clone/Download [https://github.com/libopencm3/libopencm3 libopencm3].
  
Compile 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 <code>make</code> with no arguments.
 +
 
 +
 
 +
Write some code! When youre ready, you can compile it with a simple makefile like this one:
 +
 
 +
<source>
 +
# 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
 +
</source>

Revision as of 13:59, 19 April 2020

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