Add Cooperative scheduler
This commit is contained in:
parent
c657fe17e7
commit
f7cd7387dc
@ -6,21 +6,17 @@
|
||||
/* This Struct used to restore next thread and save the running thread */
|
||||
typedef struct tControlBlock
|
||||
{
|
||||
int32_t * stackPt;
|
||||
struct tControlBlock * nextPt;
|
||||
}tControlBlock_t;
|
||||
int32_t *stackPt;
|
||||
struct tControlBlock *nextPt;
|
||||
} tControlBlock_t;
|
||||
|
||||
/* Define typdef of static task to execute in the future */
|
||||
#if (USE_STATIC_THREAD)
|
||||
typedef void(*StaticTask)(void);
|
||||
typedef void (*StaticTask)(void);
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
uint8_t ridhaOsAddThreads(StaticTask fn0, StaticTask fn1, StaticTask fn2);
|
||||
|
||||
void ridhaOsStart(void);
|
||||
|
||||
|
||||
|
||||
#endif /* __R_KERNEL_H__ */
|
||||
@ -3,7 +3,6 @@
|
||||
|
||||
#include "ridhaOs.h"
|
||||
|
||||
|
||||
#include "RIDHAOS_CONF.h"
|
||||
#include <stdint.h>
|
||||
|
||||
@ -18,6 +17,6 @@ inline void ridhaOsSchedulerStart(void)
|
||||
}
|
||||
|
||||
void ridhaOsSchedulerLaunch(void);
|
||||
|
||||
void ridhaOsSchedulerThreadYield(void);
|
||||
|
||||
#endif /* __R_SCHEDULER_H__ */
|
||||
@ -3,12 +3,10 @@
|
||||
|
||||
#warning Be Sure to configure this file, when you start your project.
|
||||
|
||||
|
||||
/* Here Define your MCU REGISTER MAP FILE */
|
||||
#include "fsl_device_registers.h"
|
||||
#define CPU_ARM_VERSION 33
|
||||
|
||||
|
||||
/* SysTick Configuration for TimeBase */
|
||||
extern uint32_t SystemCoreClock;
|
||||
|
||||
@ -16,14 +14,12 @@ extern uint32_t SystemCoreClock;
|
||||
#define MAX_DELAY 0xFFFFFFFFU // Max Wayt Delay
|
||||
#define TICK_RATE_HZ 1000U // Getting SystemCoreClock divide by 1000 to get interrupt every 1ms
|
||||
#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 */
|
||||
#define USE_STATIC_THREAD 1
|
||||
#define USE_DYNAMIC_THREAD 0
|
||||
#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__ */
|
||||
@ -4,7 +4,7 @@
|
||||
tControlBlock_t tcbs[NUM_OF_THREADS];
|
||||
|
||||
/* Define current Pointer to point to first of the list of Stack*/
|
||||
tControlBlock_t * currentPt;
|
||||
tControlBlock_t *currentPt;
|
||||
|
||||
/* Each Thread will have STACKSIZE * 4. */
|
||||
int32_t TCB_STACK[NUM_OF_THREADS][STACKSIZE];
|
||||
@ -14,26 +14,26 @@ void ridhaOsStackInit(int i)
|
||||
tcbs[i].stackPt = &TCB_STACK[i][STACKSIZE - 16];
|
||||
|
||||
/* Set bit24 Thumb state bit to one, operate in thumb mode */
|
||||
TCB_STACK[i][STACKSIZE-1] = (1U << 24); // PSR register
|
||||
TCB_STACK[i][STACKSIZE - 1] = (1U << 24); // PSR register
|
||||
|
||||
#ifdef DEBUG
|
||||
/* @Note: Block below need to delete after for debug purpose */
|
||||
/* Dummy stack content */
|
||||
TCB_STACK[i][STACKSIZE-3] = 0xAAAAAAAA; // R14(LR)
|
||||
TCB_STACK[i][STACKSIZE-4] = 0xAAAAAAAA; // R12
|
||||
TCB_STACK[i][STACKSIZE-5] = 0xAAAAAAAA; // R3
|
||||
TCB_STACK[i][STACKSIZE-6] = 0xAAAAAAAA; // R2
|
||||
TCB_STACK[i][STACKSIZE-7] = 0xAAAAAAAA; // R1
|
||||
TCB_STACK[i][STACKSIZE-8] = 0xAAAAAAAA; // R0
|
||||
TCB_STACK[i][STACKSIZE - 3] = 0xAAAAAAAA; // R14(LR)
|
||||
TCB_STACK[i][STACKSIZE - 4] = 0xAAAAAAAA; // R12
|
||||
TCB_STACK[i][STACKSIZE - 5] = 0xAAAAAAAA; // R3
|
||||
TCB_STACK[i][STACKSIZE - 6] = 0xAAAAAAAA; // R2
|
||||
TCB_STACK[i][STACKSIZE - 7] = 0xAAAAAAAA; // R1
|
||||
TCB_STACK[i][STACKSIZE - 8] = 0xAAAAAAAA; // R0
|
||||
|
||||
TCB_STACK[i][STACKSIZE-9] = 0xAAAAAAAA; // R11
|
||||
TCB_STACK[i][STACKSIZE-10] = 0xAAAAAAAA; // R10
|
||||
TCB_STACK[i][STACKSIZE-11] = 0xAAAAAAAA; // R9
|
||||
TCB_STACK[i][STACKSIZE-12] = 0xAAAAAAAA; // R8
|
||||
TCB_STACK[i][STACKSIZE-13] = 0xAAAAAAAA; // R6
|
||||
TCB_STACK[i][STACKSIZE-14] = 0xAAAAAAAA; // R7
|
||||
TCB_STACK[i][STACKSIZE-15] = 0xAAAAAAAA; // R5
|
||||
TCB_STACK[i][STACKSIZE-16] = 0xAAAAAAAA; // R4
|
||||
TCB_STACK[i][STACKSIZE - 9] = 0xAAAAAAAA; // R11
|
||||
TCB_STACK[i][STACKSIZE - 10] = 0xAAAAAAAA; // R10
|
||||
TCB_STACK[i][STACKSIZE - 11] = 0xAAAAAAAA; // R9
|
||||
TCB_STACK[i][STACKSIZE - 12] = 0xAAAAAAAA; // R8
|
||||
TCB_STACK[i][STACKSIZE - 13] = 0xAAAAAAAA; // R6
|
||||
TCB_STACK[i][STACKSIZE - 14] = 0xAAAAAAAA; // R7
|
||||
TCB_STACK[i][STACKSIZE - 15] = 0xAAAAAAAA; // R5
|
||||
TCB_STACK[i][STACKSIZE - 16] = 0xAAAAAAAA; // R4
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -50,19 +50,19 @@ uint8_t ridhaOsAddThreads(StaticTask fn0, StaticTask fn1, StaticTask fn2)
|
||||
ridhaOsStackInit(0);
|
||||
|
||||
/* Init Program counter (PC) to task functions */
|
||||
TCB_STACK[0][STACKSIZE-2] = (int32_t)(fn0);
|
||||
TCB_STACK[0][STACKSIZE - 2] = (int32_t)(fn0);
|
||||
|
||||
/* Init Stacks for first thread */
|
||||
ridhaOsStackInit(1);
|
||||
|
||||
/* Init Program counter (PC) to task functions */
|
||||
TCB_STACK[1][STACKSIZE-2] = (int32_t)(fn1);
|
||||
TCB_STACK[1][STACKSIZE - 2] = (int32_t)(fn1);
|
||||
|
||||
/* Init Stacks for first thread */
|
||||
ridhaOsStackInit(2);
|
||||
|
||||
/* Init Program counter (PC) to task functions */
|
||||
TCB_STACK[2][STACKSIZE-2] = (int32_t)(fn2);
|
||||
TCB_STACK[2][STACKSIZE - 2] = (int32_t)(fn2);
|
||||
|
||||
/* Start from thread 0 */
|
||||
currentPt = &tcbs[0];
|
||||
@ -81,5 +81,5 @@ void ridhaOsStart(void)
|
||||
|
||||
/* Launch schedular */
|
||||
ridhaOsSchedulerLaunch();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
#include "ridhaOsScheduler.h"
|
||||
|
||||
/* Define current Pointer to point to first of the list of Stack*/
|
||||
extern tControlBlock_t * currentPt;
|
||||
extern tControlBlock_t *currentPt;
|
||||
volatile uint32_t g_curr_tick;
|
||||
volatile uint32_t g_curr_tick_p;
|
||||
volatile uint32_t tick_freq = 1;
|
||||
@ -31,13 +31,14 @@ void ridhaOsSchedulerDelayMS(uint32_t delay)
|
||||
{
|
||||
uint32_t tickstart = ridhaOsSchedulerGetTick();
|
||||
uint32_t wait = delay;
|
||||
if(wait < MAX_DELAY)
|
||||
if (wait < MAX_DELAY)
|
||||
{
|
||||
wait += (uint32_t)(tick_freq);
|
||||
}
|
||||
|
||||
while((ridhaOsSchedulerGetTick() - tickstart) < wait){}
|
||||
|
||||
while ((ridhaOsSchedulerGetTick() - tickstart) < wait)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t ridhaOsSchedulerGetTick(void)
|
||||
@ -54,8 +55,7 @@ void ridhaOsSchedulerInit(void)
|
||||
ridhaOsSchedulerReset();
|
||||
|
||||
/* Reload the timer with number of MS */
|
||||
SysTick->LOAD = ((SystemCoreClock / TICK_RATE_HZ)
|
||||
* ROUND_ROBIN_QUANTA) - 1;
|
||||
SysTick->LOAD = ((SystemCoreClock / TICK_RATE_HZ) * ROUND_ROBIN_QUANTA) - 1;
|
||||
|
||||
/* Clear Systick current value register */
|
||||
SysTick->VAL = 0x00;
|
||||
@ -68,10 +68,8 @@ void ridhaOsSchedulerInit(void)
|
||||
|
||||
/* Enable SysTick interrupt */
|
||||
SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;
|
||||
|
||||
}
|
||||
|
||||
|
||||
void ridhaOsSchedulerLaunch(void)
|
||||
{
|
||||
/* Load Address of currentPt into R0 */
|
||||
@ -106,10 +104,10 @@ void ridhaOsSchedulerLaunch(void)
|
||||
|
||||
/* Return from exception and restore r0 to r3, lr, pc, psr */
|
||||
__asm("BX LR");
|
||||
|
||||
}
|
||||
|
||||
__attribute__((naked)) void SysTick_Handler(void){
|
||||
__attribute__((naked)) void SysTick_Handler(void)
|
||||
{
|
||||
|
||||
/* SUSPEND CURRENT THREAD */
|
||||
/* DISABLE GLOBAL INTERRUPTS */
|
||||
@ -136,5 +134,16 @@ __attribute__((naked)) void SysTick_Handler(void){
|
||||
__asm("CPSIE I");
|
||||
/* Return from exception and restore r0 to r3, lr, pc, psr */
|
||||
__asm("BX LR");
|
||||
}
|
||||
|
||||
|
||||
void ridhaOsSchedulerThreadYield(void)
|
||||
{
|
||||
/* Clear Systick current value register */
|
||||
SysTick->VAL = 0;
|
||||
|
||||
/* Trigger Systick */
|
||||
SCB->ICSR |= SCB_ICSR_PENDSTSET_Msk;
|
||||
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user