Getting started with the STM32F4-Discovery board and ChibiOS/RT

This article will explain how to get started with the recently released STM32F4-Discovery board using ChibiOS/RT. This board is really cheap (below 20$) and is an excellent evaluation platform for the STMicroelectronics Hi-Performance & DSP STM32F4 family.

Board features


This board is impressive when compared to the cost.

  • STM32F407VGT6 Hi-Performance & DSP 32bits MCU, 168MHz Cortex-M4 CPU, 1MB flash, 192KB RAM, 4KB backup RAM.
  • 16 advanced DMA channels, 6 USART, 3 SPI, 3 I2C, 3 ADC, 2 DAC, RTC, CRC unit, Ethernet, 2 USB OTG, lots of timers, two independent watchdogs.
  • Integrated ST-Link/V2 USB debugger/programmer unit. The debugger unit is also usable separately.
  • 8MHz HSE crystal.
  • 32768Hz LSE crystal (not fitted but installable).
  • USB power LED (red).
  • USB communication LED (red/green).
  • Four user LEDs (green, blue, red, orange).
  • VBUS active and over current LEDs.
  • Reset button.
  • User button.
  • All the STM32 I/O pins are conveniently available on the board pin strips, many free I/O lines available.
  • On-board MEMS digital accelerometer.
  • On-board MEMS digital microphone.
  • On-board audio DAC with class-D speaker driver.
  • USB OTG micro-AB connector.

Required Software

  • ChibiOS/RT 2.3.4 or newer.
  • A GNU GCC toolchain, for example YAGARTO or CodeSourcery. The demo can also be compiled using the free IAR and Keil ARM compilers (projects included).
  • ST Toolset 4.2.1p2 or newer for flash programming.

Required Hardware


  • An STM32F4-Discovery board.
  • A 10KΩ resistive trimmer or linear potentiometer.
  • Some jumper wires to connect the various parts.
  • An RS232 levels translator. It is required only if you want to capture the test report from the USART1 port using a terminal emulator, for example the (in)famous HyperTerminal. I bought the one in the picture here.
  • An SPI Analyzer or a Logic Analyzer or an Oscilloscope. It is only required if you want to see the SPI output.

ChibiOS/RT Components

ChibiOS/RT Logo

ChibiOS/RT offers an excellent support for the STM32F4 platform. The application demonstrates the use of the following components:

  • Cortex-M4 startup support.
  • Linker scripts for STM32F4.
  • STM32F4-Discovery specific board files.
  • RTOS kernel for multi-threading and synchronization.
  • HAL driver for the STM32F4 platform initialization (clock tree).
  • PAL driver (I/O ports abstraction).
  • DMA driver (shared DMA-abstractor helper driver).
  • ADC driver.
  • SPI driver.
  • PWM driver.
  • Serial driver.
  • Test Suite for self test and reporting.

All drivers are “native” in order to minimize the memory footprint, the bulky ST library has not been used (but is supported).

Relevant documents

The Demo Application

The demo application samples two analog channels (AN11 and Temperature Sensor) using the ADC driver and modulates two PWM channels using the average of four samples per channel. The sampled data is also transmitted over the SPI bus (SPI1, CPOL=0, CPHA=0, 16bits frames, MSb first).
The PWM is programmed with a cycle of exactly one second, the ADC samples modulate the two output channels with a duty cycle between 0 and 100 percent. The PWM outputs are connected to the board green and blue LEDs so you don't need any instrument to see the output.
By pressing the user button the test suite is activated and the output is transmitted over the serial port 1 (38400-8-N-1).

The application demonstrates the following tasks:

  • STM32F4 platform startup and initialization.
  • Clock tree initialization using the HAL driver.
  • RTOS initialization and operations.
  • I/O pins programming and redirection.
  • DMA-assisted ADC sampling and callbacks.
  • DMA-assisted SPI transactions and callbacks.
  • PWM operations and callbacks.
  • Interrupt driven buffered serial driver.
  • Multithreading.

The demo code can be previewed here.

Board Setup

Now let's connect the various parts:

  1. Connect the STM32F4-Discovery PA3 (RX) pin to the RS232 board receiver output.
  2. Connect the STM32F4-Discovery PA2 (TX) pin to the RS232 board transmitter input.
  3. Connect the STM32F4-Discovery VDD pin to the RS232 board VCC pin.
  4. Connect the STM32F4-Discovery VDD pin to the potentiometer pin 1.
  5. Connect the STM32F4-Discovery GND pin to the RS232 board GND pin.
  6. Connect the STM32F4-Discovery GND pin to the potentiometer pin 3.
  7. Connect the STM32F4-Discovery PC1 pin to the potentiometer pin 2 (center).
  8. Connect your analyzer to the SPI port pins (NSS=PB12, SCK=PB13, MOSI=PB15).
  9. Connect the RS232 board to a PC, run a terminal emulator setting the parameters 38400-8-N-1.
  10. Connect the STM32F4-Discovery board to a PC using an USB cable.

Building the Demo

The demo is located under ./demos/ARMCM4-STM32F407-DISCOVERY and can be compiled by simply giving the “make” command. There are several guides you may find useful:

Uploading the Demo

stvp_model_f4.jpg stvp_open.jpg stvp_program.jpg

If you don't need to debug but just want to upload the code on the board you may use the ST Visual Programmer tool. Its use is very simple:

  1. Launch the tool.
  2. Select the device STM32F407xG.
  3. Load the demo image file ”./build/ch.hex”.
  4. Program the STM32 flash memory with the demo application binary image.

If you are using the IAR Workbench or Keil uVision you can flash and debug the STM32F4-Discovery directly from you IDE (select ST-Link as probe and configure it in SWD mode).

Running the Demo

The demo should run immediately after loading, if not then try disconnecting and reconnecting the board USB cable in order to reset it (older versions of ST visual programmer do not run the program automatically).

You will see both user LEDs flashing with an one second period, the green LED duty cycle can be adjusted using the potentiometer, the blue LED duty cycle is determined by the temperature and is driven by the STM32 internal sensor.

The various components assembled and working

Using a Logic Analyzer you can monitor the SPI output, the demo periodically transmits eight 16 bits words, each one representing a sample taken by the ADC.

SPI output on the Logic Analyzer

By pressing the user button the OS will execute the test suite, you can capture the USART2 output using a terminal emulator in order to examine the test report and the benchmark results.

Test Report on HyperTerminal

And now?

Now you have a complete working RTOS supporting the STM32F4, strip the test suite code from the Makefile (you won't need it) and start experimenting modifying the file main.c.

chibios/articles/stm32f4_discovery.txt · Last modified: 2012/01/06 21:42 by giovanni
Except where otherwise noted, content on this wiki is licensed under the following license:GNU Free Documentation License 1.3