Difference between revisions of "Setting up a C++ Toolchain for STM32 Microcontrollers"
Poofjunior (talk | contribs) (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...") |
Poofjunior (talk | contribs) |
||
(5 intermediate revisions by the same user not shown) | |||
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 lang="bash"> | ||
+ | # 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> | ||
+ | (Note that, in the above Makefile, some indentations are '''tab characters''', not spaces.) | ||
+ | |||
+ | == References == | ||
+ | * [https://hackaday.com/2020/11/17/bare-metal-stm32-from-power-up-to-hello-world/ Bare Metal STM32 From Power Up to Hello World] | ||
+ | * [http://galexander.org/stm32/ Minimal setup from galexander] |
Latest revision as of 01:30, 18 November 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
(Note that, in the above Makefile, some indentations are tab characters, not spaces.)