Lesson plan

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.

4 chapters, 7 lessons, about 7 hours, on the Arduino Uno R3. Every lesson runs in the browser, with nothing to install.

The unit

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.

Chapter 1. About 2 h 10 min

Tasks and switching

What a task is, and how the processor moves from one to another.

LessonTimeWhat 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
  1. Run it: under FreeRTOS
  2. The task control block
  3. A frame that was never saved
  4. Launch
  5. Running on its own stack
  6. Launch the other one
  7. Why a stack each
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
  1. Run it: three tasks take turns
  2. Save
  3. Swap
  4. Restore
  5. What a switch costs
  6. A task that will not share
  7. Read your switch

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".

Chapter 2. About 1 h 50 min

Time and preemption

A tick of the kernel’s own, and a scheduler that takes the processor back.

LessonTimeWhat 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
  1. Run it: Timer1 ticks
  2. Leave Timer0 alone
  3. Two clocks agree
  4. Wait in ticks
  5. Double the tick rate
  6. Choosing a tick rate
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
  1. Run it: tasks that never yield
  2. A naked interrupt
  3. See the slices
  4. Fair shares
  5. Longer slices
  6. Every stack pays

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".

Chapter 3. About 2 h

Waiting and asking

Tasks that block on semaphores, and one doorway into the kernel.

LessonTimeWhat 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
  1. Run it: a dock with two bays
  2. Blocked
  3. Handed over
  4. Counting
  5. One bay
  6. A semaphore is not a mutex
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
  1. Run it: tasks that ask
  2. Sleep, in milliseconds
  3. Why a doorway
  4. Return means exit
  5. The idle load
  6. Speed up red
  7. Design a syscall

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".

Chapter 4. About 1 h 5 min

Boss project

Three tasks on one Uno, on the kernel you built.

LessonTimeWhat 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
  1. Run it: your kernel
  2. The beacon
  3. Sensor to reporter
  4. Turn the knob
  5. How much stack is left
  6. Overflow it
  7. From here to FreeRTOS

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".

Your class

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.