//--------------Includes----------------- #include #include #include #include #include"scheduler.h" #include"error.h" #include"heap.h" #include"printf.h" #include"ossettings.h" #include"../platform/porting.h" /* TCBs */ static osTCB_t tasks[MAX_NUMBER_OF_TASKS]; static uint8_t task_cnt = 0; /* Scheduler */ static uint8_t scheduler_initialized = 0; static osSchedulerState_t state = S_INIT; static osHeapNode_t current = NULL; /* Timing */ static uint32_t sys_clk = 0; /* Priority queues */ static osHeapNode_t ready_q[HEAP_SIZE]; uint32_t osSchedulerGetSysT(void) { return sys_clk; } /** Get the task which is running. * * @retval Pointer to TCB of the running task. */ static inline osTCB_t* osSchedulerGetRunningT(void) { for(int i=0; ifnc_ptr = NULL; } void osTaskDelay(uint8_t idelay) { osTCB_t* task_running = osSchedulerGetRunningT(); if( task_running == NULL ) THROW_ERROR(E_NULL_FORBIDDEN); task_running->state = BLOCKED; task_running->wake_up = sys_clk + idelay; } void osTaskDelayUntil(uint32_t iwakeup_time, uint8_t idelay) { osTCB_t* task_running = osSchedulerGetRunningT(); if( task_running == NULL ) THROW_ERROR(E_NULL_FORBIDDEN); task_running->state = BLOCKED; task_running->wake_up = iwakeup_time + idelay; } void osRunScheduler(void) { uint32_t sys_clk_old = sys_clk; /* The Cortex-M3 system timer for instance * can not handle a very high period needed * e.g. for debugging as the register is * simply too small. */ // #if (SYS_TICK_MS >= 100) // static uint16_t cnt = 0; // if(cnt != SYS_TICK_MS/SYS_TICK_PERIOD_MS) // { // /* Don't execute scheduler. */ // cnt++; // return; // } // else // cnt = 0; // #endif ++sys_clk; DEBUG_MSG("System clock: %d\n", sys_clk); if( (sys_clk % ALIVE_PULSE_LENGTH) == 0) toggleAliveLED(); if(sys_clk < sys_clk_old) THROW_WARNING(W_SYS_TIMER_OVERFLOW); /* Are any tasks becoming ready? */ for(int i=0; istate = RUNNING; state = S_EXECUTING_TASK; (*(current->fnc_ptr))(current->arguments); state = S_EXECUTING_NO_TASK; break; case S_IDELING: DEBUG_MSG("S_IDELING\n"); if(!osHeapIsEmpty(ready_q)) state = S_EXECUTING_NO_TASK; break; } } void osPrintTask(uint8_t iindex) { //DEBUG_MSG("Function Pointer:\t%d\n", tasks[iindex].fnc_ptr); DEBUG_MSG("Name:\t\t\t%s\n", tasks[iindex].name); //DEBUG_MSG("Arguments Pointer:\t%d\n", tasks[iindex].fnc_ptr); //DEBUG_MSG("Priority:\t\t%d\n", tasks[iindex].priority); //DEBUG_MSG("State:\t\t\t%d\n", tasks[iindex].state); DEBUG_MSG("Wake up:\t\t%d\n", tasks[iindex].wake_up); } void osPrintAllTasks(void) { for(int i=0; i