Skip to content

Eplankton/mos-stm32

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

MOS-STM32 πŸ§€

Introduction πŸš€

English | δΈ­ζ–‡

 A_A       _
o'' )_____//    [MOS-STM32]
 `_/  MOS  )    Mini RTOS on Cortex-M
 (_(_/--(_/     MOS <=> Mini-RTOS

- Board: NUCLEO-144 F429ZI
- MCU:   STM32F429ZIT6 (256KB SRAM, 2MB FLASH)

Repository 🌏

GitHub | Gitee

Architecture πŸ”

USR/src

.
β”œβ”€β”€ vendor              // Hardware Abstraction Layer(SPL/HAL/LL/...)
└── src
    β”œβ”€β”€ drivers         // Hardware Driver Portable Interface
    β”‚   β”œβ”€β”€ stm32f4xx   // STM32F4xx On-Chip Peripherals(USART, I2C, SPI, ...)
    β”‚   └── device      // Other components(LED, LCD, ...)
    β”‚
    β”œβ”€β”€ mos
    β”‚   β”œβ”€β”€ config.h             // System Configuration
    β”‚   β”œβ”€β”€ arch                 // Arch-related
    β”‚   β”‚   └── cpu.hpp          // asm for init/context_switch
    β”‚   β”‚
    β”‚   β”œβ”€β”€ kernel               // Kernel(Arch-independent)
    β”‚   β”‚   β”œβ”€β”€ macro.hpp        // Kernel Constant Macros
    β”‚   β”‚   β”œβ”€β”€ type.hpp         // Basic Types
    β”‚   β”‚   β”œβ”€β”€ concepts.hpp     // Type Constraints(Optional)
    β”‚   β”‚   β”œβ”€β”€ data_type.hpp    // Basic Data Structures
    β”‚   β”‚   β”œβ”€β”€ alloc.hpp        // Static/Dynamic Allocator
    β”‚   β”‚   β”œβ”€β”€ global.hpp       // Kernel Globals
    β”‚   β”‚   β”œβ”€β”€ printf.h/.c      // Thread-safe printf (by mpaland)
    β”‚   β”‚   β”œβ”€β”€ task.hpp         // Task control
    β”‚   β”‚   β”œβ”€β”€ sync.hpp         // Sync primitives
    β”‚   β”‚   β”œβ”€β”€ scheduler.hpp    // Scheduler and Policy
    β”‚   β”‚   β”œβ”€β”€ ipc.hpp          // Inter-Process Communication
    β”‚   β”‚   └── utils.hpp        // Utils
    β”‚   β”‚
    β”‚   β”œβ”€β”€ kernel.hpp           // Import Kernel Modules
    β”‚   └── shell.hpp            // Simple Shell
    β”‚
    β”œβ”€β”€ user
    β”‚   β”œβ”€β”€ gui                  // GUI-related
    β”‚   β”‚   β”œβ”€β”€ GuiLite.h        // GuiLite Framework
    β”‚   β”‚   └── UICode.cpp       // User Interface
    β”‚   β”‚
    β”‚   β”œβ”€β”€ global.hpp           // User Globals
    β”‚   β”œβ”€β”€ bsp.hpp              // Board Support Package
    β”‚   β”œβ”€β”€ app.hpp              // User Applications
    β”‚   └── test.hpp             // Test
    β”‚
    β”œβ”€β”€ main.cpp                 // Entry main()
    └── stm32f4xx_it.cpp         // Interrput SubRoutine(Partly)

Example 🍎

Shell shell_demo

MutexTest mutex_test

LCD Driver & GUI Demo

Concurrent Task Period & Time Sequence

Async Executor

// 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()
    {
        // Simplified
        stdio.init(9600-8-1-N)
             .rx_config(PXa)  // RX -> PXa
             .tx_config(PYb)  // TX -> PYb
             .it_enable(RXNE) // Enable RXNE interrupt
             .enable();
    }
    ...
}
namespace MOS::User::App
{
    Sync::Barrier_t bar {2};

    void LED_1(Device::LED_t leds[])
    {
        bar.wait();
        for (auto _: Range(0, 20)) {
           leds[1].toggle(); // green
           Task::delay(250_ms);
        }
        kprintf("L1 exits...\n");
    }

    void LED_0(Device::LED_t leds[])
    {
        Task::create(
            LED_1, 
            leds, 
            Task::current()->get_pri(),
            "L1"
        );
        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::Calendar, nullptr, 0, "Calendar"
    );

    Task::create( // Create Shell with io_buf
        Shell::launch, &io_buf, 1, "Shell"
    );

    /* User Tasks */
    Task::create(App::LED_0, &leds, 2, "L0");
    ...

    /* 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   Stack%
-----------------------------------------
 #0    idle      15      READY       10%
 #1    Shell      1      BLOCKED     21%
 #2    L0         2      RUNNING      9%
-----------------------------------------

Version 🧾


πŸ“¦ v0.1

βœ… Done

  • Basic Scheduler and Task control, memory management

πŸ“Œ Plan

  • Timers, RoundRobin
  • Inter-process communication IPC, pipes, message queues
  • Sync, semaphore, mutex, lock
  • Porting simple shells
  • Mutable page size, memory allocator
  • SPI driver and LVGL library
  • Port to other platform like ESP32-C3(RISC-V)

πŸ“¦ v0.2

βœ… Done

  • Sync::{Sema_t, Lock_t, Mutex_t<T>, CondVar_t, Barrier_t}, where Mutex_t adopts Priority Ceiling Protocol
  • Scheduler::Policy::PreemptPri, under same priority -> RoundRobin
  • Task::terminate will be implicitly called when task exits
  • Shell::{Command, CmdCall, launch}
  • HAL::STM32F4xx::SPI_t and Driver::ST7735S_t, support GuiLite
  • Kernel::Global::os_ticks and Task::delay for blocking delay
  • Refactor the project into {kernel, arch, drivers}
  • Support GCC and STM32CubeMX HAL
  • HAL::STM32F4xx::RTC_t, CmdCall::date_cmd and App::Calendar
  • idle uses Kernel::Global::zombie_list to recycle inactive pages
  • Three basic page allocator policies, Page_t::Policy::{POOL, DYNAMIC, STATIC}

πŸ“¦ v0.3

βœ… Done

  • Tids from BitMap_t
  • (Experimental) Task::Async::{Future_t, async/await}
  • IPC::MsgQueue_t, Message Queue
  • Task::create allows generic fn signature as void fn(auto argv) with type check
  • Add ESP32C3 as WiFi Module
  • (Experimental) Atomic Type in <stdatomic.h>
  • (Experimental) Utils::IntrGuard_t, Nested Interrupt Lock Guard
  • Add Driver::Device::SD_t driver with SPI mode
  • Add FatFs as File System
  • Add Shell::usr_cmds,User Register Service

πŸ“Œ Plan

  • IPC::pipe/channel
  • Soft/Hardware Timers
  • (Experimental) Async Stackless Coroutine Async::{Future_t, async/await}
  • (Experimental) Basic Formal Verification on Scheduler
  • DMA_t Driver
  • More real-time scheduling algorithms
  • FPU support
  • Result<T, E>, Option<T>
  • Add POSIX support
  • Performance Benchmark

References πŸ›Έ

Wake up, Neo...
The Matrix has you...
Follow the white rabbit.
Knock, knock, Neo.

Releases

No releases published

Packages

No packages published

Languages