+/rtos
git clone https://git.julia.blue/+/rtos
9ff5279
master
1#include "timer.h" 2#include <stdint.h> 3 4#pragma once 5 6#define TRACK_JITTER 7 8struct TCB ; 9typedef struct { 10int32_t value ;// >=0 means free, negative means busy 11struct TCB * blocked_head ; 12}Sema4 ; 13 14// initialize OS controlled IO, timers, and heap 15// disables interrupts until OS_Launch 16void OS_Init (void ); 17 18// start the scheduler, enable interrupts 19// time_slice is the number of cycles between context switches 20void OS_Launch (uint32_t time_slice ); 21 22// add a foregound task to the scheduler 23// name is a static string label for the thread used for debugging 24// stack_size is the number of bytes to allocate for the task's stack 25// lower numbers = higher priority 26// returns false if this thread can not be added 27bool OS_AddThread (void (* task )(void ),const char * name ,uint32_t stack_size , 28uint32_t priority ); 29 30// add a background periodic task 31// the task can't block, but it can call OS_Signal or OS_AddThread 32// period is in cycles 33// lower numbers = higher priority 34// returns false if this thread can not be added 35bool OS_AddPeriodicThread (void (* task )(void ),uint32_t period , 36uint32_t priority ); 37 38void OS_ReportJitter (void );// print jitter stats for periodic threads 39 40// add a background task to run whenever the SW1 (PF4) button is pushed 41// the task can't block, but it can call OS_Signal or OS_AddThread 42void OS_AddSW1Task (void (* task )(void )); 43 44// add a background task to run whenever the SW2 (PF0) button is pushed 45// the task can't block, but it can call OS_Signal or OS_AddThread 46void OS_AddSW2Task (void (* task )(void )); 47 48uint32_t OS_Id (void );// returns a unique id for the current_thread 49uint32_t OS_Time (void );// return the system time in cycles 50void OS_ClearTime (void );// sets the system time to zero 51 52// suspend the current thread for AT LEAST a given number of cycles 53void OS_Sleep (uint32_t time ); 54void OS_Suspend (void );// suspend the current thread 55void OS_Kill (void );// kill the current thread, releasing its stack 56 57// set the starting value for a semaphore, 0 or more means available 58void OS_InitSemaphore (Sema4 * semaPt ,int32_t value ); 59void OS_Wait (Sema4 * semaPt );// block until a semaphore is available 60void OS_Signal (Sema4 * semaPt );// increment semaphore value (may wake thread) 61 62// These are used to dynamically load user code 63bool OS_AddProcess (void (* entry )(void ),void * text ,void * data , 64uint32_t stack_size ,uint32_t priority ); 65void OS_LoadProgram (char * name ); 66 67typedef enum { 68UART = 1 , 69ESP = 2 , 70FS = 4 , 71SCREEN = 8 , 72}OutputDevice ; 73 74void OS_RedirectOutput (OutputDevice device ); 75void OS_RedirectString (const char * str ); 76void OS_RedirectChar (char c );