Lessons and the learning game

Bare-metal STM32

Program an STM32F411 through its registers: clocks, GPIO, SysTick, a USART and timer PWM by hand, and a cubesat Morse beacon with no driver but yours.

Take the course at /learn/path/bare-metal-stm32. It is an advanced course: it assumes the STM32 course and Registers and assembly.

The bench

A Black Pill with its on-board LED on PC13 (active low) and KEY button on PA0, a red LED behind 220 ohms on PB12, and on PA6 (TIM3 channel 1) a second LED, or a piezo for the boss, with a frequency counter.

The lessons

The reference manual. Registers read by base plus offset: PLLCFGR's reset value, a port that reads 0 with its clock off, and PC13 blinked through BSRR by address.

Clocks and RCC. A lost write before GPIOCEN, then HSE, PLLCFGR for 84 MHz from the 25 MHz crystal, the APB1 divider and the switch confirmed by SWS.

GPIO registers. PUPDR for KEY's pull-up, MODER for two outputs, IDR read and masked, BSRR writes, active-high and active-low LEDs.

SysTick. LOAD for a 1 ms tick, the 24-bit limit, COUNTFLAG polled, and an LED blinked by counting ticks.

USART by hand. AF7 on PA9 and PA10, BRR for 115200 and 9600 baud, TXE and RXNE polled: a line echo with no Serial.

Timer PWM by hand. TIM3's PSC, ARR, CCR1, CCMR1, CCER and CR1 for 1 kHz on PA6, checked by the counter; a breathing LED; 2 kHz.

Boss: a cubesat Morse beacon. Every peripheral by hand: a non-blocking Morse state machine keying an 800 Hz tone and the LED, with new messages uplinked over the USART.

The sketches

Sketch What it does
bmsmanual Registers by address, and an LED blinked through a pointer
bmsclock Clock gates, HSE, the PLL and the system clock switch
bmsgpio KEY on PA0 mirrored to PC13 and PB12 through BSRR
bmssystick SysTick at 1 ms, polled
bmsuart A polled USART1 driver with a line echo
bmspwm TIM3 PWM on PA6, breathing
bmsbeacon The driver-free Morse beacon

What the simulation leaves out

The clock tree is not modeled: the chip runs at 84 MHz from power-on, ready bits come up as soon as their ON bits are set, and RCC stores what it is told, so the PLL lesson's sequence is right for a real board but changes nothing here. FLASH_ACR is not modeled, so the flash wait-state step is described rather than written. Clock enable bits are real gates. Floating inputs read noise. The register subset modeled is listed in the board's contract, docs/stm32f411.md.

The STM32F411 Black Pill.