🛠 MicroPython sur la STM32F4DISCOVERY : REPL interactif et pilotage des LED
Objective
Turn the STM32F4DISCOVERY into an interactive Python interpreter using the official STM32F4DISC port of MicroPython. The REPL runs over USB (CDC serial on the OTG FS, enabled by MICROPY_HW_USB_FS in the board configuration): unlike Arduino mode, the USB serial port works immediately here, without an external cable. The port configuration also enables the DAC, the hardware random number generator, and servos.
Hardware
- 1 STM32F4DISCOVERY board + micro-USB data cable
- This time, plug the cable into the USB OTG FS micro-AB connector (pins PA11/PA12): this is the one that exposes the REPL
- Flashing tool: STM32CubeProgrammer or ST-LINK Utility via the second port (ST-LINK/V2-A)
Installation
- Get the
STM32F4DISCfirmware (published builds or compilation inports/stm32withmake), official source:ports/stm32/boards/STM32F4DISCfrom the micropython repository. - Flash it into the 1024 KB flash of the STM32F407VGT6 via the embedded ST-LINK.
- Reconnect to the OTG port: a USB serial port appears; open it at 115200 baud (Thonny, mpremote, picocom...).
Complete code
# Dans le REPL (Ctrl-D puis coller) ou dans un fichier main.py :
from machine import Pin
import time
# Broche verifiee dans ports/stm32/boards/STM32F4DISC/mpconfigboard.h
leds = [Pin('PD12', Pin.OUT), # verte
Pin('PD13', Pin.OUT), # orange
Pin('PD14', Pin.OUT), # rouge
Pin('PD15', Pin.OUT)] # bleue
user = Pin('PA0', Pin.IN) # bouton USER, actif a l'etat haut
while True:
for led in leds:
led.on; time.sleep_ms(120); led.off
print("btn:", user.value)
Two hardware specifics to keep in mind from the start
- PA0 is shared: the USER button occupies the same pin as UART4 TX/CTS and the Wake-Up function. MicroPython documents removing the surface-mount resistor SB20 (bottom side) to free up PA0 if you need UART4.
- PC7 is connected to the MCLK input of the CS43L22 audio codec: using it as a UART output only makes sense if the codec is completely unused (comment from the MicroPython port).
Expected verification
MicroPython v... on ...; STM32F407 with STM32F407VG is displayed when connecting the OTG port. The program runs the four LEDs and displays btn: 0 or btn: 1 depending on the button state. Press Ctrl-C to interrupt the loop and return to the prompt.
Common errors
- Looking for the REPL on the ST-LINK port: MicroPython's CDC serial is on the OTG FS port (micro-AB), the other connector.
- Writing
Pin(12)thinking of PD12: the MicroPython STM32 port accepts literal pin names (Pin('PD12')), which are more explicit than Arduino numbers. - Coding a real-time loop in pure Python: the interpreter runs on an M4F at 168 MHz, which is comfortable but not suitable for hard audio control — for that, see the CS43L22 tutorial in C.
- Forgetting that the board is officially obsolete (labeled as such by Wikipedia): MicroPython, Zephyr, CubeF4, and the Arduino core still support it, but there is no new hardware in production.
Other tutorials for this board
- Premier téléversement sur la STM32F4DISCOVERY avec l'Arduino STM32duino
- Zephyr RTOS sur la STM32F4DISCOVERY : west build et west flash
- 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