Phase 2 of 4 · Study weeks 5-8
Real-time control and an RTOS
This phase puts a controller on top of the Phase 1 sampling and lets you decide, with a number, what runs in an interrupt and what runs in a task.
Topics every week
- Week 58 h
- A discrete PI controller, anti-windup, identifying the plant from a step capture, float on a Cortex-M4F (single-precision FPU, lazy stacking)
Hand inA Python model of the plant and the PI with the same constants, overlaid on a measured step response (reading: the PID chapter of Feedback Systems by Åström and Murray, 2nd edition, free online)
- Week 68 h
- Interrupts: NVIC priority bits (16 levels on this chip), tail-chaining, stack frame,
BASEPRI, sharing data between an ISR andmain
Hand inThe loop in an ISR that writes the new duty first and does its bookkeeping after, a cycle budget table, and a double buffer with a sequence counter
- Interrupts: NVIC priority bits (16 levels on this chip), tail-chaining, stack frame,
- Week 78 h
- FreeRTOS on Cortex-M4F: tasks, queues, notifications, priority grouping (all bits preempt) and
configMAX_SYSCALL_INTERRUPT_PRIORITY, mutex against binary semaphore, stack high-water marks
Hand inTasks for commands and telemetry, with the loop ISR handing data over, and a measured priority-inversion demo
- FreeRTOS on Cortex-M4F: tasks, queues, notifications, priority grouping (all bits preempt) and
- Week 88 h
- Tuning and proof: disturbance rejection, CPU-load measurement, seeded faults, the three-way latency comparison
Hand inCheckpoint 2
Practice projects
- Priority inversion lab: a low, a medium and a high task share a binary semaphore, then a mutex. Done when the blocking time of the high task is captured for both and the difference is explained.
- PI tuning: three tunings in one table (rise time, overshoot, settling, noise). Done when the model predicts each overshoot within 3 percentage points.
- CPU-load meter (idle hook plus DWT). Done when it matches a known busy loop within 2%.
GitHub repos to study
| Repo | What to read | Week |
|---|---|---|
| FreeRTOS/FreeRTOS-Kernel | portable/GCC/ARM_CM4F/port.c (context switch, BASEPRI) and tasks.c; the repo has tags up to V11.3.3 | 6, 7 |
| STMicroelectronics/STM32CubeG4 | Projects/NUCLEO-G474RE/Examples/TIM and Middlewares/Third_Party/FreeRTOS | 5, 7 |
| memfault/interrupt | _posts: the NVIC, RTOS context switching and stack-usage posts | 6, 7 |
Milestone checkpoint
Add the loop, the tasks and the seeded faults to the Phase 1 harness. Done when the Checkpoint 2 row passes with the Phase 1 harness still reporting zero missed samples. Evidence: step-response and disturbance captures, the latency table for three designs, stack high-water marks and ADR-2 and ADR-3 (float against fixed-point).
Check yourself
Answer each question in your head first, then open it to check.
Why must an ISR that calls
xQueueSendFromISRhave a priority value numerically at or above the syscall limit?On Cortex-M a lower number means a higher priority. FreeRTOS critical sections setBASEPRIto the limit, which masks interrupts whose priority value is at or above it. Only those interrupts are safe to call the API; one with a lower value is never masked and could corrupt the kernel's lists.What is priority inversion, and which FreeRTOS object mitigates it?
A high task waits for a low one holding a resource while a medium task runs. A mutex raises the holder's priority; a binary semaphore does not.What does lazy FPU stacking cost on interrupt entry?
Nothing until the ISR uses the FPU, then the hardware saves the FPU registers, which adds cycles. Measure it in Week 6.What does integral windup do, and how do you stop it?
The integrator keeps growing while the output is saturated, causing overshoot after. Clamp or back-calculate the integrator.Why can a high-priority task still miss a deadline when no other task is ready?
The tick interrupt, critical sections, and higher-priority interrupts can all run first.