In this tutorial I describe how to build and deploy a simple “Hello World!” application on the popular ARM-based STM32F4 Discovery board using open source tools (sneak peak, here).
This tutorial is suitable for total beginners! First, you will install the development environment with no hassle in few minutes via apt-get command. I use public and well-maintained repositories whenever possible. Second, in just 3 commands you will retrieve the project from the public repository, build it and deploy the executable on the STM32F4 Discovery board. Finally, you may explore the references to learn technologies in greater depth, try to play with the existing code or read another STM32F4 post.
Enjoy your coding! 🙂
- Ubuntu 14.04 LTS (x86 architecture).
- STM32F4 Discovery Board (ARM architecture, costs less than 20 EUR).
2. Install Software Dependencies
- Install GNU ARM Embedded Toolchain. You’ll need this cross-toolchain to build executables for the ARM target on the x86 host.
sudo apt-get install build-essential git \ binutils-arm-none-eabi \ gcc-arm-none-eabi \ libnewlib-arm-none-eabi
- Install OpenOCD package to enable STM32F4 programming & remote debugging.
sudo apt-get install openocd
- Download STM32F4 examples.
- Clone my git repository. I recommend this options.
git clone https://github.com/istarc/stm32.git cd stm32/STM32F4-Discovery_FW_V1.1.0/Project cd Peripheral_Examples/IO_Toogle
- Use the official source (but you are on your own now).
# Download & Extract mkdir -p ~/stm32f4 && cd ~/stm32f4 # Download stsw-stm32068 file wget http://bit.ly/1ryC8Tp unzip stsw-stm32068.zip cd STM32F4-Discovery_FW_V1.1.0/Project cd Peripheral_Examples/IO_Toogle
- Clone my git repository. I recommend this options.
3. Build the IO Toggle Project
Now, it’s perfect time to get familiar with GNU make. It’s a great utility to automate a building process. Create a new file in the IO Toggle directory and name it “Makefile”. Copy the code below and paste it inside the Makefile. Skip this step if you cloned the git repository.
# GNU ARM Embedded Toolchain CC=arm-none-eabi-gcc LD=arm-none-eabi-ld AR=arm-none-eabi-ar AS=arm-none-eabi-as CP=arm-none-eabi-objcopy OD=arm-none-eabi-objdump # Build Parameters: MCU Flags, Definitions, Includes, # Compile Flags, Linker Script, Linker Flags MCFLAGS=-mcpu=cortex-m4 -mthumb -mlittle-endian \ -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb-interwork DEFS=-DUSE_STDPERIPH_DRIVER -DSTM32F4XX INCLUDES=-I. \ -I../../../Libraries/CMSIS/ST/STM32F4xx/Include \ -I../../../Utilities/STM32F4-Discovery \ -I../../../Libraries/CMSIS/Include \ -I../../../Libraries/STM32F4xx_StdPeriph_Driver/inc CFLAGS=-c $(MCFLAGS) $(DEFS) $(INCLUDES) LDSCRIPT = TrueSTUDIO/IO_Toggle/stm32_flash.ld LDFLAGS=-T $(LDSCRIPT) --specs=nosys.specs $(MCFLAGS) # Inputs: C Sources, Assembler Sources SOURCES=main.c stm32f4xx_it.c system_stm32f4xx.c \ ../../../Utilities/STM32F4-Discovery/stm32f4_discovery.c \ ../../../Libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_syscfg.c \ ../../../Libraries/STM32F4xx_StdPeriph_Driver/src/misc.c \ ../../../Libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_gpio.c \ ../../../Libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_rcc.c \ ../../../Libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_exti.c ASMSOURCES=../../../Libraries/CMSIS/ST/STM32F4xx/Source/Templates/gcc_ride7/startup_stm32f4xx.s # Outputs: Object Files, ELF Executable & # Converted ELF Executable to Intel HEX format OBJECTS=$(SOURCES:%.c=%.o) OBJECTS+=$(ASMSOURCES:%.s=%.o) EXECUTABLE=iotoggle.elf TARGET=iotoggle.hex # Build Rules .PHONY: release release: $(TARGET) .PHONY: all all: $(TARGET) .PHONY: debug debug: CFLAGS+=-g # Add debug flag debug: $(TARGET) $(TARGET): $(EXECUTABLE) $(CP) -O ihex $< $@ @echo "Objcopy from ELF to IHEX complete!\n" $(EXECUTABLE): $(OBJECTS) $(CC) $(LDFLAGS) $(OBJECTS) -o $@ @echo "Linking complete!\n" %.o: %.c $(CC) $(CFLAGS) $< -o $@ @echo "Compiled "$<"!\n" %.o: %.s $(CC) $(CFLAGS) $< -o $@ @echo "Assambled "$<"!\n" .PHONY: clean clean: rm -f $(OBJECTS) $(EXECUTABLE) $(TARGET)
Now, you’ll use this Makefile to automagically build the project (you could also do it manually, but it’s a tedious work!).
make clean && make
4. Deploy the IO Toggle Project
Deploy the iotoggle executable using OpenOCD (don’t forget to plug-in the STM32F4 board via USB). The OpenOCD driver will automatically detect the appropriate USB port.
sudo openocd \ -f /usr/share/openocd/scripts/board/stm32f4discovery.cfg \ -c "program iotoggle.elf verify reset"
If everything goes well, you should see the output text below and blinking LEDs on the STM32F4 board.
Info : STLINK v2 JTAG v14 API v2 SWIM v0 VID 0x0483 PID 0x3748
Info : Target voltage: 2.900177
Info : stm32f4x.cpu: hardware has 6 breakpoints, 4 watchpoints
target state: halted
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x08002060 msp: 0x20020000
** Programming Started **
auto erase enabled
Info : device id = 0x10016413
Info : flash size = 1024kbytes
wrote 16384 bytes from file iotoggle.elf in 0.844525s (18.946 KiB/s)
** Programming Finished **
** Verify Started **
verified 10384 bytes in 0.116926s (86.727 KiB/s)
** Verified OK **
** Resetting Target **
If you want to change the LED blinking order or delay, you can make appropriate changes in the main.c file. Then rebuild and deploy the project (use section 3 and 4, respectively).
Many things can go wrong in software development. One of them are software bugs.Once they are discovered, they can be effectively mitigated by using STM32F4 in-circuit debugger, but that’s another story (click here).
Q: Deployment fails repeatedly due to some OpenOCD issue. 😦 Is there a workaround?
A: Yes, the current official Ubuntu package (Aug 2014) contains a prehistoric OpenOCD version. You should build a newer version from scratch. I provide step-by-step instructions here (2nd section, it takes less than 3 minutes to build it). When you are done, just return here and continue as nothing happened. It will work out of the box. 🙂
This tutorial is based on Benjamin’s robotics blog on “Get started with the STM32F4 on Ubuntu Linux“. The Makefile is based on Nabil’s blog on “Programming the STM32F4 DISCOVERY with the Bus Blaster“. The Homer Simpson Whoohoo cover is from FB Cover Street.