Eplankton / mos-stm32

πŸ“Ί Simple RTOS on STM32F4

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Introduction πŸš€

 A_A       _    MOS Real-Time Operating System
o'' )_____//    Simple RTOS on Cortex-M
 `_/  MOS  )    Developed with C/C++
 (_(_/--(_/     [Apache License Version 2.0]

Repository 🌏

Architecture πŸ”

USR/src

.
β”œβ”€β”€ πŸ“ vendor              // Hardware Abstraction Layer (SPL/HAL/LL/...)
└── πŸ“ src
    β”œβ”€β”€ πŸ“ driver          // Interface Compatibility Layer
    β”‚   β”œβ”€β”€ πŸ“ stm32f4xx   // STM32F4xx On-Chip Peripherals (USART, I2C, SPI, ...)
    β”‚   └── πŸ“ device      // Other Hardware Components (LED, LCD, SD, ...)
    β”‚
    β”œβ”€β”€ πŸ“ mos
    β”‚   β”œβ”€β”€ config.h             // System Configuration
    β”‚   β”œβ”€β”€ πŸ“ arch              // Architecture Specific
    β”‚   β”‚   └── cpu.hpp          // Init/Context Switch
    β”‚   β”‚
    β”‚   β”œβ”€β”€ πŸ“ kernel            // Kernel (Arch Independent)
    β”‚   β”‚   β”œβ”€β”€ macro.hpp        // Kernel Constants Macros
    β”‚   β”‚   β”œβ”€β”€ type.hpp         // Basic Types
    β”‚   β”‚   β”œβ”€β”€ concepts.hpp     // Type Constraints (Optional)
    β”‚   β”‚   β”œβ”€β”€ data_type.hpp    // Basic Data Structures
    β”‚   β”‚   β”œβ”€β”€ alloc.hpp        // Memory Management
    β”‚   β”‚   β”œβ”€β”€ global.hpp       // Kernel Global Variables
    β”‚   β”‚   β”œβ”€β”€ printf.h/.c      // Thread-Safe printf(*)
    β”‚   β”‚   β”œβ”€β”€ task.hpp         // Task Control
    β”‚   β”‚   β”œβ”€β”€ sync.hpp         // Synchronization Primitives
    β”‚   β”‚   β”œβ”€β”€ scheduler.hpp    // Scheduler
    β”‚   β”‚   β”œβ”€β”€ ipc.hpp          // Inter-Process Communication
    β”‚   β”‚   └── utils.hpp        // Other Utilities
    β”‚   β”‚
    β”‚   β”œβ”€β”€ kernel.hpp           // Kernel Module
    β”‚   └── shell.hpp            // Command Line Shell
    β”‚
    β”œβ”€β”€ πŸ“ user
    β”‚   β”œβ”€β”€ πŸ“ gui               // Graphical System
    β”‚   β”‚   β”œβ”€β”€ GuiLite.h        // GuiLite Framework
    β”‚   β”‚   └── UICode.cpp       // Custom UI
    β”‚   β”‚
    β”‚   β”œβ”€β”€ global.hpp           // User Global Variables
    β”‚   β”œβ”€β”€ bsp.hpp              // Board Support Package
    β”‚   β”œβ”€β”€ app.hpp              // User Tasks
    β”‚   β”œβ”€β”€ fatfs.hpp            // File System
    β”‚   └── test.hpp             // Test Code
    β”‚
    β”œβ”€β”€ main.cpp                 // System Entry Function
    └── stm32f4xx_it.cpp         // Interrupt Handler Routine

Example 🍎

Shell Test shell_demo

Mutex Test(Priority Ceiling Protocol) mutex_test

LCD Driver & GUI Demo

Concurrent Task Period & Time Sequence

// MOS Kernel & Shell
#include "mos/kernel.hpp"
#include "mos/shell.hpp"

// HAL and Device 
#include "drivers/stm32f4xx/hal.hpp"
#include "drivers/device/led.hpp"
namespace MOS::User::Global
{
    using namespace HAL::STM32F4xx;
    using namespace Driver::Device;
    using namespace DataType;

    // Shell I/O UART and Buffer
    auto& stdio = STM32F4xx::convert(USARTx);
    DataType::SyncRxBuf_t<16> io_buf;

    // LED red, green, blue
    Device::LED_t leds[] = {...};
}
namespace MOS::User::BSP
{
    using namespace Driver;
    using namespace Global;

    void LED_Config()
    {
        for (auto& led: leds) {
            led.init();
        }
    }

    void USART_Config()
    {
        stdio.init(9600-8-1-N)
             .rx_config(PXa)  // RX -> PXa
             .tx_config(PYb)  // TX -> PYb
             .it_enable(RXNE) // Enable RXNE interrupt
             .enable();       // Enable UART
    }
    ...
}
namespace MOS::User::App
{
    Sync::Barrier_t bar {2}; // Set Barrier to sync tasks

    void led1(Device::LED_t leds[])
    {
        bar.wait();
        for (auto _: Range(0, 20)) {
           leds[1].toggle(); // green
           Task::delay(250_ms);
        }
        kprintf(
            "%s exits...\n",
            Task::current()->get_name()
        );
    }

    void led0(Device::LED_t leds[])
    {
        Task::create(
            led1, 
            leds, 
            Task::current()->get_pri(),
            "led1"
        );
        bar.wait();
        while (true) {
            leds[0].toggle(); // red
            Task::delay(500_ms);
        }
    }
    ...
}
int main()
{
    using namespace MOS;
    using namespace Kernel;
    using namespace User;
    using namespace User::Global;

    BSP::config(); // Init hardware and clocks

    Task::create( // Create Calendar with RTC
        App::time_init, nullptr, 0, "time/init"
    );

    Task::create( // Create Shell with buffer
        Shell::launch, &stdio.buf, 1, "shell"
    );

    /* User Tasks */
    Task::create(App::led_init, &leds, 2, "led/init");
    ...

    /* Test examples */
    Test::MutexTest();
    Test::MsgQueueTest();
    ...
    
    // Start scheduling, never return
    Scheduler::launch();
}

Boot Up ⚑

 A_A       _   Version @ x.x.x(...)
o'' )_____//   Build   @ TIME, DATE
 `_/  MOS  )   Chip    @ MCU, ARCH
 (_(_/--(_/    2023-2024 Copyright by Eplankton

 Tid   Name   Priority   Status    Mem%
----------------------------------------
 #0    idle      15      READY      10%
 #1    shell      1      BLOCKED    21%
 #2    led0       2      RUNNING     9%
----------------------------------------

Version 🧾

πŸ“¦ v0.1

βœ… Done:

  • Basic data structures, scheduler, and task control, memory management

πŸ“Œ Planned:

  • Timers, round-robin scheduling
  • Inter-Process Communication (IPC), pipes, message queues
  • Process synchronization (Sync), semaphores, mutexes
  • Porting a simple Shell
  • Variable page sizes, memory allocator
  • SPI driver, porting GuiLite/LVGL graphics libraries
  • Porting to other boards/arch, e.g., ESP32-C3 (RISC-V)

πŸ“¦ v0.2

βœ… Done:

  • Synchronization primitives Sync::{Sema_t, Lock_t, Mutex_t<T>, CondVar_t, Barrier_t}
  • Scheduler::Policy::PreemptPri with round-robin RoundRobin scheduling for same priority levels
  • Task::terminate implicitly called upon task exit to reclaim resources
  • Simple command-line interaction Shell::{Command, CmdCall, launch}
  • HAL::STM32F4xx::SPI_t and Driver::Device::ST7735S_t, porting the GuiLite graphics library
  • Blocking delay with Kernel::Global::os_ticks and Task::delay
  • Refactored project organization into {kernel, arch, drivers}
  • Support for GCC compilation, compatible with STM32Cube HAL
  • Real-time calendar HAL::STM32F4xx::RTC_t, CmdCall::date_cmd, App::Calendar
  • idle uses Kernel::Global::zombie_list to reclaim inactive pages
  • Three basic page allocation policies Page_t::Policy::{POOL, DYNAMIC, STATIC}

πŸ“¦ v0.3

βœ… Done:

  • Mapping Tids to BitMap_t
  • Message queue IPC::MsgQueue_t
  • Task::create allows generic function signatures as void fn(auto argv) with type checker
  • Added ESP32-C3 as a WiFi component
  • Added Driver::Device::SD_t, SD card driver, porting FatFs file system
  • Added Shell::usr_cmds for user-registered commands
  • [Experimental] Atomic types <stdatomic.h>
  • [Experimental] Utils::IntrGuard_t, nested interrupt critical sections
  • [Experimental] Simple formal verification of Scheduler + Mutex

πŸ“Œ Planned:

  • Inter-Process Communication: pipes/channels
  • FPU hardware float support
  • Performance benchmarking
  • Error handling with Result<T, E>, Option<T>
  • DMA_t DMA Driver
  • Software/hardware timers Timer
  • [Experimental] Adding POSIX support
  • [Experimental] Asynchronous stackless coroutines Async::{Future_t, async/await}
  • [Experimental] More real-time scheduling algorithms

References πŸ›Έ

There's a movie on TV.
Four boys are walking on railroad tracks...
I better go, too.

About

πŸ“Ί Simple RTOS on STM32F4

License:Apache License 2.0


Languages

Language:C 96.1%Language:C++ 3.5%Language:Assembly 0.4%Language:GDB 0.0%