This is an old revision of the document!
Here is a rewritten DokuWiki page with the new TFT screen, runtime stack pool, explicit stack size API, profiling, and datasheet/resource links.
```dokuwiki
athreads is a small preemptive scheduling library for 8-bit AVR microcontrollers, developed and demonstrated on the ATmega2560.
The project implements a lightweight threading runtime in C and AVR assembly. It supports timer-driven preemption, context switching, per-thread time quanta, sleeping threads, runtime stack allocation from a stack pool, and basic CPU usage accounting.
The original idea was to understand how operating systems schedule tasks, but in a constrained embedded environment where there is no operating system underneath. Instead of only simulating scheduling on a PC, this project runs directly on a microcontroller and exposes scheduler behavior through hardware UI and serial profiling.
The project is useful as an educational low-level systems project because it shows how a preemptive runtime can be built from interrupts, stacks, registers, timers, and explicit context switching.
The project is centered around the athreads scheduler library. The scheduler runs on the ATmega2560 and uses a hardware timer interrupt to periodically preempt the currently running thread.
Each thread has:
The demo application adds:
+-------------------------------------------------------------+
| PC / Laptop |
| |
| +-------------------------------+ |
| | Python CPU Task Manager | |
| | - reads USART packets | |
| | - plots per-thread CPU usage | |
| +---------------^---------------+ |
| | USB Serial |
+------------------|------------------------------------------+
|
+------------------v------------------------------------------+
| Arduino Mega 2560 |
| |
| +-------------------------------------------------------+ |
| | athreads Scheduler | |
| | | |
| | - thread table | |
| | - runtime stack pool | |
| | - per-thread stack/context | |
| | - round-robin scheduling | |
| | - per-thread quantum | |
| | - sleeping thread wakeup | |
| | - CPU tick accounting | |
| +-----------^--------------------^----------------------+ |
| | | |
| Timer1 interrupt Timer2 uptime tick |
| preemption sleep/encoder timing |
| |
| +------------------+ +------------------+ |
| | TFT UI Thread | | Encoder Thread | |
| | process menu | | input handling | |
| +--------^---------+ +--------^---------+ |
| | | |
| SPI ST7735 TFT Rotary Encoder |
| |
| +-------------------------------------------------------+ |
| | Worker Threads | |
| | synthetic workloads used for profiling/demo purposes | |
| +-------------------------------------------------------+ |
+-------------------------------------------------------------+
The hardware used for the demo consists of:
| Component | Documentation / Datasheet |
|---|---|
| Arduino Mega 2560 Rev3 | Arduino Mega 2560 documentation |
| Arduino Mega 2560 Rev3 board datasheet | Arduino Mega 2560 Rev3 datasheet PDF |
| Arduino Mega 2560 schematic | Arduino Mega 2560 schematic PDF |
| ATmega2560 microcontroller | Microchip ATmega2560 documentation and datasheet |
| ST7735R TFT controller | Sitronix ST7735R datasheet |
| KY-040 rotary encoder module | KY-040 rotary encoder datasheet PDF |
The current display is a 1.8 inch SPI TFT color display using an ST7735 controller.
| TFT Pin | Arduino Mega 2560 Pin |
|---|---|
| SCL / SCK / CLK | D52 / SCK |
| SDA / MOSI / DIN | D51 / MOSI |
| RST / RES | D48 |
| DC / A0 | D49 |
| CS | D53 |
| VCC | 5V |
| GND | GND |
| BL / LED | 5V |
The module used in this project is compatible with 5V systems, so it can be connected directly to the Arduino Mega 2560 pins.
INVON, and the driver uses pre-inverted RGB565 color constants so that the physical display appears in dark mode correctly.
| Encoder Pin | Arduino Mega 2560 Pin |
|---|---|
| SW | D47 |
| DT | D45 |
| CLK | D43 |
| + | 5V |
| GND | GND |
The TFT display and rotary encoder are not required by the scheduler library itself. They are part of the demonstration layer.
The TFT shows the currently running threads and their quantum values. The encoder allows the user to navigate through the thread list, enter edit mode, and modify the selected thread's quantum while the scheduler is running.
The project is developed using:
include/ athread/ Public scheduler API and generated AVR structure offsets platform/ USART, uptime, and debug headers profiling/ CPU statistics, tracing, and worker demo headers ui/ TFT display and encoder headers src/ athread/ Scheduler implementation and AVR context switch assembly platform/ USART and millisecond uptime support profiling/ CPU sampling, trace hooks, and demo workloads ui/ ST7735 SPI TFT driver and rotary encoder input main.c Demo firmware entry point tools/ cpu_task_manager.py Live CPU profiling viewer gen_offsets.py PlatformIO pre-build offset generator gen_athread_offsets.c Offset generator source
The scheduler keeps a table of thread descriptors. Each descriptor stores:
Threads are created with an explicit stack size. The scheduler allocates stack memory from a static stack pool at runtime.
This avoids hardcoding one global array per thread, such as:
static uint8_t main_stack[MAIN_STACK_SIZE]; static uint8_t worker_stack[WORK_STACK_SIZE];
Instead, the scheduler owns a single pool:
static uint8_t stack_pool[ATHREAD_STACK_POOL_SIZE]; static uint16_t stack_pool_used;
When a thread is created, the scheduler reserves a slice of this pool and stores the stack boundaries in the thread descriptor.
Timer1 is used for preemption. When the Timer1 compare interrupt fires, the scheduler updates the running thread's quantum counter. If the quantum expires, the current thread context is saved and another ready thread is selected.
Timer2 is used for millisecond uptime and periodic support work, including sleeping-thread wakeups and encoder debouncing.
| Function | Description |
|---|---|
athread_init() | Initializes scheduler state, creates the idle thread, and resets the stack pool. |
athread_start() | Starts the scheduler. After this call, execution is controlled by the scheduler and the function does not normally return. |
athread_create(entry, info, stack_size) | Creates a new thread, allocates stack_size bytes from the scheduler stack pool, initializes its context, and returns the thread ID or ATHREAD_INVALID_TID on failure. |
athread_yield() | Voluntarily gives up the CPU and allows another ready thread to run. |
athread_sleep_ticks(ticks) | Puts the current thread to sleep for a number of scheduler ticks. |
athread_set_quantum(tid, quantum_ticks) | Changes the time quantum of a thread at runtime. |
athread_get_quantum(tid) | Returns the configured time quantum of a thread. |
athread_get_thread_count() | Returns the number of allocated thread IDs, including the idle thread. |
athread_get_cpu_ticks(out_ticks, max_ticks) | Copies per-thread CPU tick counters for profiling and diagnostics. |
athread_get_current_tid() | Returns the ID of the currently running thread. |
athread_tick() | Advances scheduler timing state, including sleeping-thread wakeups. Used by the platform timer code. |
athread_bootstrap() | Internal startup wrapper used when a thread begins execution. |
Example thread creation:
uint8_t tid = athread_create(worker_thread, worker_info, 512);
A quantum is the amount of scheduler time a thread is allowed to run before it can be preempted.
In this project, the quantum is measured in scheduler timer ticks. For example, if the scheduler tick is 5 ms, a quantum of 4 allows a thread to run for approximately 20 ms before the scheduler may switch to another thread.
Changing a thread's quantum affects responsiveness and CPU distribution:
The TFT display shows a dark-mode process menu. It displays thread IDs, names, quantum values, and small quantum bars.
The rotary encoder is used as follows:
The display driver uses ST7735 SPI commands and RGB565 color values. Because this module required inversion mode, the driver enables INVON and uses pre-inverted colors internally.
The profiling system is optional and is built on top of the scheduler's CPU counters.
The firmware periodically reads per-thread CPU tick counters and sends compact packets over USART. On the PC, a Python program reads the serial stream and displays a live graph of CPU usage per thread.
The viewer can be started with:
python ./tools/cpu_task_manager.py --port COM3
If the default Python installation does not include Tkinter, a Python installation with Tkinter support must be used.
The demo firmware creates several threads to show scheduling behavior:
| Thread | Name | Purpose |
|---|---|---|
| T0 | IDLE | Idle thread created by the scheduler |
| T1 | MAIN | Main application coordinator |
| T2 | ENC | Rotary encoder input handling |
| T3 | TFT | TFT process menu |
| T4 | WRK1 | Synthetic worker workload |
| T5 | WRK2 | Synthetic worker workload |
| T6 | WRK3 | Synthetic worker workload |
| T7 | WRK4 | More dynamic worker workload |
The project successfully implements a working preemptive scheduler on the ATmega2560.
The main achieved results are:
The profiling viewer makes it possible to observe how scheduling decisions affect CPU distribution between threads.
This project helped me understand preemptive scheduling from the hardware level upward. Implementing context switching on a microcontroller made the relationship between stacks, registers, interrupts, timers, and scheduling much clearer than a high-level simulation would have.
The most challenging parts were the AVR assembly context switch, stack initialization for newly created threads, runtime stack pool management, and making profiling work without disturbing the system too much.
The final result is a small but functional preemptive scheduling library, with optional profiling and hardware visualization tools that make the runtime behavior visible and easier to reason about.
Possible future improvements include:
The project source code, firmware, tools, and documentation are available in the repository:
Build command:
pio run
Upload command:
pio run -t upload
Run the CPU profiler:
python ./tools/cpu_task_manager.py --port COM3