Lessons and the learning game

Build a tiny kernel

Write a preemptive kernel for the Uno's ATmega328P: stacks, a context switch in assembly, a timer tick, semaphores, syscalls and stack canaries.

The bench

Every lesson opens the bench of the RTOS course: lamps on pins 13, 12 and 11, a button on pin 2, a knob on A0 and a 16x2 I2C LCD. The preemption lesson adds a logic analyzer on pins 13 and 12 to show the time slices.

The course follows Scheduling and RTOS: that course uses FreeRTOS, and this one builds what is underneath it. Take it at /learn/path/build-a-kernel on /learn.

The lessons

  1. Tasks and stacks. A TCB whose first field is the saved stack pointer, a stack built by hand to look like a switched-out task (return address, SREG 0x80, r1 at zero, the other registers), and a launch that loads SP and returns into the task.
  2. Context switch in assembly. SAVE_CONTEXT pushes r0, SREG and r1 to r31 and stores SP in the TCB; round robin picks the next task; RESTORE_CONTEXT pops it back. A cooperative kernel, and the task that breaks it.
  3. A timer tick. Timer1 in CTC mode at clk/64 with OCR1A 2499: 100 Hz. Timer0 is left alone because it runs millis(). An atomic read of a two byte counter, and tasks that wait in ticks.
  4. Preemption. A naked Timer1 compare handler that saves the interrupted task, counts the tick and switches round robin, so tasks that never yield still share. Slices on the logic analyzer, and a scheduler lock for the serial port.
  5. Semaphores. Sleeping and blocked states, and a counting semaphore that hands a unit straight to the next waiter: three rovers, two charging bays.
  6. Syscalls. One numbered doorway into the kernel (yield, sleep, ticks, write, exit), sleeps rounded up to whole ticks, a task that returns and is ended by SYS_EXIT, and the idle load.
  7. Boss: three tasks on one Uno. A beacon, a sensor and a reporter with sleep and a semaphore, painted stacks with a high water report, and canaries that catch a stack overflow.

What each sketch does

Sketch What it does
kntask Builds two TCBs and stacks, prints the first frame, launches a task.
knswitch A cooperative kernel with the context switch in inline assembly.
kntick Adds a 100 Hz Timer1 tick; tasks wait in ticks.
knslice The preemptive kernel: the tick switches tasks round robin.
knsem Sleep, blocking and a counting semaphore.
knsys A syscall table, SYS_EXIT for a returning task, idle load.
knboss The whole kernel with three tasks, stack painting and canaries.

Each sketch is a whole kernel in one file, with no library, so every line can be read and changed.

Honest limits

  • The kernels run on the simulated ATmega328P instruction for instruction, so the context switch, the tick and the stacks behave as on a real Uno. Timing in the lessons is quoted from the simulation; a real board agrees to within the crystal's tolerance.
  • The kernel takes Timer1, so analogWrite() on pins 9 and 10 and the Servo library cannot be used alongside it.
  • The AVR has no privileged mode or memory protection, so syscalls are a convention and a canary catches an overflow only after it happens. The course says where bigger chips do better.
  • These are teaching kernels: round robin with no priorities, mutexes or queues. FreeRTOS, taught in the RTOS course, adds those.

LoRa long-range radio, another advanced embedded course on the Uno.