Add Cooperative scheduler

This commit is contained in:
Ridha Noomane 2024-12-26 10:47:08 +01:00
parent c657fe17e7
commit f7cd7387dc
5 changed files with 164 additions and 164 deletions

View File

@ -15,12 +15,8 @@ typedef struct tControlBlock
typedef void (*StaticTask)(void); typedef void (*StaticTask)(void);
#endif #endif
uint8_t ridhaOsAddThreads(StaticTask fn0, StaticTask fn1, StaticTask fn2); uint8_t ridhaOsAddThreads(StaticTask fn0, StaticTask fn1, StaticTask fn2);
void ridhaOsStart(void); void ridhaOsStart(void);
#endif /* __R_KERNEL_H__ */ #endif /* __R_KERNEL_H__ */

View File

@ -3,7 +3,6 @@
#include "ridhaOs.h" #include "ridhaOs.h"
#include "RIDHAOS_CONF.h" #include "RIDHAOS_CONF.h"
#include <stdint.h> #include <stdint.h>
@ -18,6 +17,6 @@ inline void ridhaOsSchedulerStart(void)
} }
void ridhaOsSchedulerLaunch(void); void ridhaOsSchedulerLaunch(void);
void ridhaOsSchedulerThreadYield(void);
#endif /* __R_SCHEDULER_H__ */ #endif /* __R_SCHEDULER_H__ */

View File

@ -3,12 +3,10 @@
#warning Be Sure to configure this file, when you start your project. #warning Be Sure to configure this file, when you start your project.
/* Here Define your MCU REGISTER MAP FILE */ /* Here Define your MCU REGISTER MAP FILE */
#include "fsl_device_registers.h" #include "fsl_device_registers.h"
#define CPU_ARM_VERSION 33 #define CPU_ARM_VERSION 33
/* SysTick Configuration for TimeBase */ /* SysTick Configuration for TimeBase */
extern uint32_t SystemCoreClock; extern uint32_t SystemCoreClock;
@ -16,14 +14,12 @@ extern uint32_t SystemCoreClock;
#define MAX_DELAY 0xFFFFFFFFU // Max Wayt Delay #define MAX_DELAY 0xFFFFFFFFU // Max Wayt Delay
#define TICK_RATE_HZ 1000U // Getting SystemCoreClock divide by 1000 to get interrupt every 1ms #define TICK_RATE_HZ 1000U // Getting SystemCoreClock divide by 1000 to get interrupt every 1ms
#define MAX_DELAY 0xFFFFFFFFU // MAX DELAY #define MAX_DELAY 0xFFFFFFFFU // MAX DELAY
#define ROUND_ROBIN_QUANTA 10U // QUANTA For round robin schedular change tasks. #define ROUND_ROBIN_QUANTA 10U // QUANTA in MS For round robin schedular change tasks.
/* Thread Configuration Size and Number Max for TCB */ /* Thread Configuration Size and Number Max for TCB */
#define USE_STATIC_THREAD 1 #define USE_STATIC_THREAD 1
#define USE_DYNAMIC_THREAD 0 #define USE_DYNAMIC_THREAD 0
#define NUM_OF_THREADS 3 #define NUM_OF_THREADS 3
#define STACKSIZE 100 #define STACKSIZE 700 // As memory addressing orgonize by 4 so you need to mulitple by 4
#endif /* __R_CONFIGURATION_H__ */ #endif /* __R_CONFIGURATION_H__ */

View File

@ -81,5 +81,5 @@ void ridhaOsStart(void)
/* Launch schedular */ /* Launch schedular */
ridhaOsSchedulerLaunch(); ridhaOsSchedulerLaunch();
} }

View File

@ -36,8 +36,9 @@ void ridhaOsSchedulerDelayMS(uint32_t delay)
wait += (uint32_t)(tick_freq); wait += (uint32_t)(tick_freq);
} }
while((ridhaOsSchedulerGetTick() - tickstart) < wait){} while ((ridhaOsSchedulerGetTick() - tickstart) < wait)
{
}
} }
uint32_t ridhaOsSchedulerGetTick(void) uint32_t ridhaOsSchedulerGetTick(void)
@ -54,8 +55,7 @@ void ridhaOsSchedulerInit(void)
ridhaOsSchedulerReset(); ridhaOsSchedulerReset();
/* Reload the timer with number of MS */ /* Reload the timer with number of MS */
SysTick->LOAD = ((SystemCoreClock / TICK_RATE_HZ) SysTick->LOAD = ((SystemCoreClock / TICK_RATE_HZ) * ROUND_ROBIN_QUANTA) - 1;
* ROUND_ROBIN_QUANTA) - 1;
/* Clear Systick current value register */ /* Clear Systick current value register */
SysTick->VAL = 0x00; SysTick->VAL = 0x00;
@ -68,10 +68,8 @@ void ridhaOsSchedulerInit(void)
/* Enable SysTick interrupt */ /* Enable SysTick interrupt */
SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk; SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;
} }
void ridhaOsSchedulerLaunch(void) void ridhaOsSchedulerLaunch(void)
{ {
/* Load Address of currentPt into R0 */ /* Load Address of currentPt into R0 */
@ -106,10 +104,10 @@ void ridhaOsSchedulerLaunch(void)
/* Return from exception and restore r0 to r3, lr, pc, psr */ /* Return from exception and restore r0 to r3, lr, pc, psr */
__asm("BX LR"); __asm("BX LR");
} }
__attribute__((naked)) void SysTick_Handler(void){ __attribute__((naked)) void SysTick_Handler(void)
{
/* SUSPEND CURRENT THREAD */ /* SUSPEND CURRENT THREAD */
/* DISABLE GLOBAL INTERRUPTS */ /* DISABLE GLOBAL INTERRUPTS */
@ -136,5 +134,16 @@ __attribute__((naked)) void SysTick_Handler(void){
__asm("CPSIE I"); __asm("CPSIE I");
/* Return from exception and restore r0 to r3, lr, pc, psr */ /* Return from exception and restore r0 to r3, lr, pc, psr */
__asm("BX LR"); __asm("BX LR");
}
void ridhaOsSchedulerThreadYield(void)
{
/* Clear Systick current value register */
SysTick->VAL = 0;
/* Trigger Systick */
SCB->ICSR |= SCB_ICSR_PENDSTSET_Msk;
} }