Build a tiny kernel lesson plan
Your own preemptive kernel on the Uno: task control blocks and hand-built stacks, a context switch in inline assembly, a Timer1 tick, preemption, sleep and semaphores, syscalls, and stack canaries under three tasks at once.
Objectives and materials
By the end, students can
- What a task is, and how the processor moves from one to another.
- A tick of the kernel’s own, and a scheduler that takes the processor back.
- Tasks that block on semaphores, and one doorway into the kernel.
- Three tasks on one Uno, on the kernel you built.
Materials for the whole unit
- Arduino Uno R3
- Character LCD with I2C backpack
- Full-size breadboard
- LED x 3
- Logic analyzer, four channels
- Potentiometer
- Pushbutton
- Resistor x 3
Every part is already on the simulator’s bench, so there is nothing to buy or hand out. The counts are the most any one lesson uses, if you build it on real breadboards too.
Tasks and switching
What a task is, and how the processor moves from one to another.
| Lesson | Time | What students do |
|---|---|---|
| Tasks and stacksWhat a task really is: a function, a stack of its own and a task control block. Build two by hand, read the fake frame on each stack, and launch one by loading its stack pointer and returning into it. | 65 min |
|
| Context switch in assemblyThe heart of every kernel: push all 32 registers and SREG, store SP in the TCB, pick the next task, load its SP, pop and return. Three tasks share the Uno by handing the processor on themselves. | 65 min |
|
Materials
- Arduino Uno R3
- Character LCD with I2C backpack
- Full-size breadboard
- LED x 3
- Potentiometer
- Pushbutton
- Resistor x 3
Check for understanding
Each student finishes "Context switch in assembly" on their own, without opening the hints. The simulator checks 7 of its 7 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Read your switch".
Time and preemption
A tick of the kernel’s own, and a scheduler that takes the processor back.
| Lesson | Time | What students do |
|---|---|---|
| A timer tickA clock of the kernel’s own: Timer1 in CTC mode interrupts at 100 Hz, leaving Timer0 to millis(). The register math, an atomic read of a two byte counter, and tasks that wait in ticks. | 55 min |
|
| PreemptionMake the tick switch tasks: a naked Timer1 interrupt saves the interrupted task, counts the tick and restores the next one. Tasks that never yield still share, on the logic analyzer, a slice at a time. | 55 min |
|
Materials
- Arduino Uno R3
- Character LCD with I2C backpack
- Full-size breadboard
- LED x 3
- Logic analyzer, four channels
- Potentiometer
- Pushbutton
- Resistor x 3
Check for understanding
Each student finishes "Preemption" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Every stack pays".
Waiting and asking
Tasks that block on semaphores, and one doorway into the kernel.
| Lesson | Time | What students do |
|---|---|---|
| SemaphoresWaiting without spinning: tasks that sleep, tasks that block, and a counting semaphore that hands a unit straight to the next task in line. Three rovers share a charging dock with two bays. | 55 min |
|
| SyscallsOne numbered doorway into the kernel: yield, sleep, ticks, write and exit through a table of functions. A task that returns is ended by SYS_EXIT, and the idle loop measures the load. | 65 min |
|
Materials
- Arduino Uno R3
- Character LCD with I2C backpack
- Full-size breadboard
- LED x 3
- Potentiometer
- Pushbutton
- Resistor x 3
Check for understanding
Each student finishes "Syscalls" on their own, without opening the hints. The simulator checks 7 of its 7 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Design a syscall".
Boss project
Three tasks on one Uno, on the kernel you built.
| Lesson | Time | What students do |
|---|---|---|
| Boss: three tasks on one UnoYour whole kernel at work: a beacon, a sensor and a reporter on a 100 Hz preemptive tick, with sleep, a semaphore between sensor and reporter, painted stacks and canaries. Then overflow a stack and watch the kernel catch it. | 65 min |
|
Materials
- Arduino Uno R3
- Full-size breadboard
- LED x 2
- Potentiometer
- Resistor x 2
Check for understanding
Each student finishes "Boss: three tasks on one Uno" on their own, without opening the hints. The simulator checks 7 of its 7 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "From here to FreeRTOS".
Teach this unit as a class
Start a class from this course and its 7 lessons are assigned in one go, in order. Students join with a six-character code or printed login cards, and each one’s work comes back to you.
The class tools are free for 30 days for one class of up to 20 students, with no card. The lessons themselves are free either way.