🛠 Zephyr RTOS sur la STM32F4DISCOVERY : west build et west flash
Objective
Set up a professional development workflow on the STM32F4DISCOVERY with Zephyr RTOS, which officially supports the board under the name stm32f4_disco. You will compile the blinky example, flash it via the onboard ST-LINK/V2-A, and understand why the serial console does not work by default.
Hardware
- 1 STM32F4DISCOVERY board + micro-USB data cable (ST-LINK port)
- PC running Linux, Windows, or macOS with the
westtool installed (see Zephyr's "Getting Started" documentation) and STM32CubeProgrammer (or OpenOCD)
Compilation and flashing
# Une fois l'espace de travail Zephyr installé (west init / west update) :
west build -b stm32f4_disco samples/basic/blinky
west flash
The default runner is STM32CubeProgrammer, which communicates with the board's ST-LINK/V2-A. Documented alternatives: west flash --runner openocd (OpenOCD provides a ready-to-use board file, stm32f4discovery.cfg, using SWD with srst_only) or --runner jlink.
The code
/* blinky — build: west build -b stm32f4_disco samples/basic/blinky */
#include <zephyr/kernel.h>
#include <zephyr/drivers/gpio.h>
#define LED_NODE DT_ALIAS(led0)
int main(void)
{
const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED_NODE, gpios);
gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE);
while (1) {
gpio_pin_toggle_dt(&led);
k_msleep(500);
}
return 0;
}
Zephyr uses the Device Tree: DT_ALIAS(led0) refers to the user LED defined in the board description, not a hard-coded pin number. The four physical LEDs are LD4 (green, PD12), LD3 (orange, PD13), LD5 (red, PD14), and LD6 (blue, PD15) — the board has eight LEDs in total, including LD1 (USB), LD2 (3.3 V power), and LD7/LD8 (VBUS/OTG overvoltage).
The console: a pitfall to know
Zephyr's default serial settings are 115200 8N1, but the board documentation warns: the ST-LINK's virtual serial port is not connected to the chip's serial port. For console output, plan for an external serial cable on pins UART2, PA2/PA3. Without this, printk goes nowhere — this is the number one point of confusion for beginners on this board.
Expected verification
After west flash, the LED blinks at 1 Hz (500 ms ON / 500 ms OFF), and the command displays Halting target CPU... followed by Successfully programmed. The board runs at 168 MHz (system clock configured from the 8 MHz HSE crystal via the board configuration).
Common errors
- Writing
-b stm32f4discoveryor-b disco_f407vg: the exact Zephyr name isstm32f4_disco. - Plugging the cable into the OTG micro-USB port expecting a flash: debugging goes through the ST-LINK/V2-A port.
- Looking for the console on the ST-LINK's virtual serial port: not wired on stock boards (see above).
- Forgetting udev rules/USB permissions for the ST-LINK under Linux before the first
west flash.
Other tutorials for this board
- Premier téléversement sur la STM32F4DISCOVERY avec l'Arduino STM32duino
- MicroPython sur la STM32F4DISCOVERY : REPL interactif et pilotage des LED
- STM32Cube HAL et BSP officiel : LED, bouton et accéléromètre avec détection automatique
- Lecture audio sur le codec CS43L22 : I2S, classe D et les limitations du BSP
- USB OTG FS sur le port micro-AB : se faire passer pour un périphérique classe CDC
- Acquérir le micro PDM MP45DT02 : I2S2, filtre PDM-vers-PCM et contraintes de sortie