RobotGuide

Hardware & electrical · topic E2 · 60 hours

MCU development (STM32)

All real-time work above 1kHz lives on an MCU. No matter how fast the host computer, it cannot do this job.

Core stage · Intermediate
System software · Aware Embedded & controls · Proficient Mechanical · Aware

Tools
Tools

What to learn

  • Clock tree, interrupt priorities, DMA: the three places that show real skill
  • Common peripherals: timers and PWM, ADC sampling, CAN/FDCAN, encoder interface
  • Division of labor between interrupts and tasks: what goes in an ISR, what goes in a task (RTOS scheduling details get their own lesson, E11)
  • Bootloader and OTA: how to roll back a failed update

The lesson · plain words

STM32 is the robotics industry's lingua franca: motor driver boards, sensor boards, safety boards — nearly all carry an STM32. Learning it is not about memorizing registers; it is about mastering the three weapons: timers, interrupts, DMA.

Once these three are fluent, the MCU stops being a black box and becomes a tool in your hands.

Timers and PWM: the MCU's heart

The timer counts up to ARR (auto-reload value), resets to zero, repeats; the compare value CCR decides when the output flips — that is PWM. Watch the animation: bigger CCR means higher duty cycle; bigger ARR means finer resolution but lower frequency.

Motor speed control, LED breathing, servo control — all PWM. Two things must be computed: frequency (motor PWM is commonly 10–20kHz, above the audible range) and resolution (ARR decides how many levels the duty cycle can take).

Interactive figure · drag it and see

What this figure shows: the counter's sawtooth ramp (yellow) crossing the compare value CCR (red dashed line) — while the counter is below CCR the PWM output stays high, and sliders for CCR (duty) and ARR (resolution) display duty cycle = CCR/ARR with the resulting average voltage on a 3.3 V system. Takeaway: CCR sets the duty cycle, ARR sets the resolution-vs-frequency trade-off.

Interrupts and DMA: don't hog the CPU

Interrupts: an event arrives (timer overflow, UART data received) and the main loop pauses to run the ISR. There is only one rule: keep the ISR short — set a flag or move data, leave the heavy work to the main loop. printf inside an ISR is beginner sin number one.

DMA: peripherals and memory move data directly, with no CPU involvement at all. Continuous ADC sampling and bulk UART transfers should both use DMA. The interrupt+DMA combination takes CPU usage from 60% down to 5%.

CubeMX and HAL: the toolchain reality

CubeMX configures pins/clocks/peripherals graphically and generates init code; the HAL library provides a uniform API. Veteran engineers mock it as «slow», but it buys you productive in two weeks versus six months grinding registers. Engineering advice: start with HAL, drop down to LL/bare registers only where performance bottlenecks appear.

You must understand what it did for you: the clock tree (where the core frequency comes from via the PLL), GPIO alternate functions (which function owns a multi-function pin), interrupt priority grouping. Miss these three and every problem is a black box.

How to learn, and how to check yourself

One Nucleo board (a few dozen dollars) plus the official tutorials: GPIO → timer PWM → interrupts → DMA → UART. Then do a small project: encoder reading + PWM speed control + UART reporting, chaining all the weapons together in one go.

Pass criteria: without the tutorial, configure a 1kHz interrupt + 4-channel PWM + DMA UART; the PWM frequency and duty cycle measured on a scope match the configuration.

Three questions — do you really get it?

1. With the PWM frequency fixed, what does increasing ARR do?

  • AResolution gets finer but frequency drops (unless the prescaler is raised to compensate)
  • BDuty cycle gets bigger
  • CNo effect
Show answer

A. Frequency = Fclk/(PSC×ARR), resolution ≈ ARR. The two are opposite ends of the same slider.

2. What is the thing you must never do inside an ISR?

  • ASet a flag
  • Bprintf logging / long loops of processing
  • CRead one register
Show answer

B. A long ISR blocks other interrupts and jitters real-time behavior. Heavy work belongs in the main loop or a task.

3. For continuous high-speed ADC-to-memory sampling, what should you use?

  • ACPU polling
  • BAn interrupt per sample
  • CDirect DMA transfer
Show answer

C. DMA leaves the CPU free. Interrupt-per-sample at high sampling rates eats the CPU alive.

Keep going deeper

Links without a marker stay on this site; the rest point to the Chinese edition until its translation batch lands.

How it fails in the field

  • Doing long operations inside an ISR (printing, floating-point math), dragging the main loop to a crawl
  • Not using DMA: multi-channel ADC sampling drops points and the data looks «randomly jittery»

You have learned enough when

You can write a complete small system — 1kHz fixed-rate task + multi-channel ADC via DMA + CAN send/receive — and measure the period jitter.

The Chinese edition carries the full topic page for MCU development (STM32), including the complete outline, depth requirements per career track, and further derivations: robotguide.dev/k/E2/.