bare-metal motor controller
why bare metal
An OS is great at hiding hardware — which is exactly what I didn't want. Feedback control depends on sampling and acting at precise, regular intervals. Running directly on the Cortex-M3 meant configuring timers, PWM, and interrupts myself, and knowing exactly when each piece of code runs.
how it works
The controller measures motor speed, compares it to the target, and a PID controller computes a correction. That correction sets the duty cycle of a 20 kHz PWM signal driving the motor.

timing
Two rates matter. The PWM runs at 20 kHz — fast enough that the motor sees a smooth average voltage, and above the range of audible whine. The PID loop runs at 100 Hz, a steady cadence for measuring and correcting. If a loop iteration runs late, the controller is effectively reacting to stale data; across testing there were zero missed real-time deadlines.
results
The controller operated across roughly ±3,000 RPM in both directions, held steady-state error below 1%, and settled in 2.1 seconds. The PWM ran at 20 kHz, the PID loop at 100 Hz, and I recorded no missed deadlines during testing. The settling time reflects the tuning tradeoff between reaching the target quickly and limiting overshoot.

what I learned
The control math was only part of the job. I also had to make sure each iteration ran when expected, since a correct PID update arriving late still acts on stale data.