Skip to content

lingzolabs/dumyos

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dumyOS - Lightweight Real-Time Operating System for Cortex-M3/M4

📖 中文文档: README_CN.md

📖 Project Overview

dumyOS is a lightweight real-time operating system (RTOS) designed for ARM Cortex-M3 and Cortex-M4 microcontrollers. It provides essential multitasking capabilities including task scheduling, priority management, and task delays, making it ideal for learning embedded systems development and small-scale projects across various MCU platforms.

✨ Key Features

  • 🚀 Lightweight Design - Clean code with minimal memory footprint
  • 📋 Preemptive Scheduling - Priority-based task scheduling
  • Task Delays - Millisecond-precision task delays
  • 🔄 Task Yielding - Support for voluntary CPU yielding
  • 💾 Static Memory Management - All task stacks are statically allocated
  • 🎯 Hardware Abstraction - Optimized for Cortex-M3 core

🛠️ Hardware Requirements

  • MCU: ARM Cortex-M3 or Cortex-M4 based microcontrollers
    • STM32F1xx, STM32F4xx series
    • NXP LPC series
    • Atmel SAM series
    • TI Tiva/Stellaris series
    • And other Cortex-M3/M4 based MCUs
  • Flash: Minimum 16KB
  • RAM: Minimum 8KB
  • Peripherals: SysTick timer, PendSV exception

📁 Project Structure

dumyOS/
├── dumyos/              # Operating system core code
│   ├── os.h            # System header file
│   └── os.c            # System implementation
├── src/                # Application code
│   ├── main.c          # Main program file
│   └── system_stm32f1xx.c # System initialization
├── startup/            # Startup code
├── linker/             # Linker scripts
├── lib/                # CMSIS library files
├── inc/                # Header file directory
└── Makefile           # Build configuration

🚀 Quick Start

Build Environment

Ensure the following tools are installed:

  • arm-none-eabi-gcc - ARM cross-compiler
  • make - Build tool
  • openocd - Programming tool (optional)

Building the Project

# Clone the project
git clone <repository-url>
cd dumyOS

# Build the project
make

# Check binary size
make size

# Clean build artifacts
make clean

Flashing to Development Board

# Flash using OpenOCD (requires ST-Link)
make flash

💻 API Reference

System Initialization

void OS_Init(void);                    // Initialize the operating system
void OS_Start(void);                   // Start the scheduler

Task Management

int OS_CreateTask(void (*task)(void *), // Task function pointer
                  void *param,          // Task parameter
                  uint8_t priority,     // Task priority (0-31, lower number = higher priority)
                  uint32_t *stack_mem,  // Task stack memory
                  uint32_t stack_size_words); // Stack size in words

void OS_Delay(uint32_t ticks);         // Task delay (milliseconds)
void OS_Yield(void);                   // Voluntarily yield CPU

Task States

typedef enum {
    TASK_STATE_READY = 0,     // Ready state
    TASK_STATE_RUNNING,       // Running state
    TASK_STATE_SLEEPING,      // Sleeping state
    TASK_STATE_UNUSED         // Unused state
} task_state_t;

📋 Usage Example

#include "dumyos/os.h"

// Define task stacks
static uint32_t task1_stack[128];
static uint32_t task2_stack[128];

// Task function 1 - High priority task
void high_priority_task(void *param) {
    while (1) {
        // Critical task operations
        // GPIO toggle or other time-sensitive operations
        OS_Delay(100);                  // Delay 100ms
    }
}

// Task function 2 - Low priority task
void low_priority_task(void *param) {
    while (1) {
        // Background task operations
        // Data processing, logging, etc.
        OS_Delay(1000);                 // Delay 1 second
    }
}

int main(void) {
    // System initialization (MCU specific)
    SystemClock_Config();   // Configure system clock
    peripheral_init();      // Initialize peripherals

    // Operating system initialization
    OS_Init();

    // Create tasks
    OS_CreateTask(high_priority_task, NULL, 2, task1_stack, 128);  // High priority
    OS_CreateTask(low_priority_task, NULL, 4, task2_stack, 128);   // Low priority

    // Start scheduler
    OS_Start();

    // Never reached
    while(1);
}

⚙️ System Configuration

The following parameters can be configured in os.h:

#define OS_MAX_TASKS 16    // Maximum number of tasks
#define OS_MAX_PRIO 32     // Maximum priority levels (0-31)

📊 Scheduling Algorithm

dumyOS uses preemptive priority scheduling:

  1. Priority Scheduling: Lower numbers have higher priority (0 is highest)
  2. Preemptive: High-priority tasks can preempt low-priority tasks
  3. Same Priority: Uses FIFO (First In, First Out) strategy
  4. Idle Task: System automatically creates lowest priority idle task

🔧 Technical Implementation

Task Scheduling

  • Uses SysTick timer for system clock interrupts (1ms) - available on all Cortex-M3/M4
  • Uses PendSV exception for context switching - standard ARM Cortex-M feature
  • Fast task selection algorithm based on priority bitmap

Memory Management

  • All task stack space is statically allocated by user
  • Task Control Blocks (TCB) managed using fixed-size arrays
  • Ready queue uses linked list structure, supporting O(1) insertion and deletion

Context Switching

  • Hardware automatically saves/restores R0-R3, R12, LR, PC, PSR
  • Software saves/restores R4-R11
  • Based on ARM Cortex-M3/M4 PendSV mechanism

🚨 Important Notes

  1. Stack Size: Ensure sufficient stack space for each task to avoid stack overflow
  2. Priority: Avoid priority inversion issues, design task priorities carefully
  3. Critical Sections: Use __disable_irq() and __enable_irq() when modifying shared resources
  4. Task Functions: Task functions should be infinite loops and should not return
  5. Platform Specific: Adapt system initialization and peripheral access for your specific MCU

🐛 Debugging Tips

  1. Stack Overflow Detection: Fill stack bottom with special values, check periodically for corruption
  2. Task State Monitoring: Observe task state changes in debugger
  3. System Load: Monitor idle task runtime to assess system load

📈 Performance Metrics

  • Context Switch Time: < 5μs (typical at 72-100MHz)
  • Memory Usage:
    • System code: ~2KB Flash
    • System RAM: ~1KB
    • Per task: 32 bytes TCB + user-defined stack space
  • Supported Platforms: Any Cortex-M3/M4 based microcontroller

🔮 Future Plans

  • Add Mutex support
  • Implement Semaphores
  • Add message queue functionality
  • Support software timers
  • Memory pool management
  • Task statistics

📄 License

This project is licensed under the MIT License - see the LICENSE file for details

🤝 Contributing

Issues and Pull Requests are welcome!

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

📞 Contact

For questions or suggestions, please contact us through Issues.


⭐ If this project helps you, please give it a Star!

About

dumyOS 是一个专为 ARM Cortex-M3 和 Cortex-M4 微控制器设计的轻量级实时操作系统(RTOS)。它提供了基本的多任务调度、任务优先级管理、任务延时等功能,适用于学习嵌入式系统开发和各种MCU平台的小型项目应用。

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors