A custom RTOS kernel built from scratch for ARM Cortex-M4.
I'm building a real-time operating system kernel to understand how these systems work under the hood. Starting with the basics: task scheduling, memory management, and inter-process communication.
Current status: Task implementation complete, working on task scheduling.
na-rtos/
├── kernel/
│ ├── inc/ # Public headers
│ └── src/ # Implementation
├── port/arm-cortex-m4/ # Hardware-specific code
├── examples/ # Demo applications
└── tests/ # Unit tests
# Native build (for testing)
make
# ARM build (for hardware)
make TARGET=stm32f4
# Run tests
make testMemory management: Static allocation only. The kernel doesn't do dynamic allocation - that's the application's job.
Error handling: Functions return status codes, use output parameters for data. No exceptions or global error state.
Circular buffer optimization: Using bit masking instead of modulo for index wrapping. Requires power-of-2 buffer sizes but avoids expensive division operations.
// Instead of: index = (index + 1) % capacity
// We do: index = (index + 1) & maskAPI design: Keep it simple. cb_* functions for initialization, self parameter for operations on existing objects.
Generic circular buffer that works with any data type. Used for message queues, UART buffers, etc.
Key features:
- Generic void* design
- Power-of-2 size optimization
- Complete API: put, get, peek, clear, size checks
- Proper error handling and memory safety
- Task Control Blocks and task lifecycle management
- Priority-based scheduler with round-robin
- Context switching (ARM assembly)
- Basic kernel services
- Traffic light demo to tie it all together
STM32F4 Discovery board (ARM Cortex-M4). Planning to keep the hardware abstraction clean so porting to other Cortex-M chips should be straightforward.
Mostly curiosity. RTOSes like many things, don't make sense to me, so I thought what better way to understand than to build one. Plus, understanding scheduler internals and memory management at this level is useful even for application development.
MIT