📄 core.c
字号:
/* Add new TCB to ready queue */ add_tcb_rdy(tcb); add_node_list_rear(&task, &tcb->task_link); return TRUE;}/** * sysinit - Initialize global varialbles and data structs * @return: * * @notes: */void sysinit(void) { uword_t i; /* Initialize all global variables */ idle_cnt = 0; pcurtcb = NULL; int_cnt = 0; pglobal = NULL; highprio = 0; sysclock = 0; int_flag = FALSE; psleep.next = &psleep; psleep.prev = &psleep; pdelay.next = &pdelay; pdelay.prev = &pdelay; /* initialize ptask[MAX_PRIO], make it point to itself. * ptask[i] points to the head and rear of ready queue, * all the priority of TCBs is 'i' in ready queue. */ for(i = 0; i < MAX_PRIO; i++) { ptask[i].next = &ptask[i]; ptask[i].prev = &ptask[i]; } /* Initialize prio_rdy_grup and prio_rdy[]. * * we can easily get the TCB of the max priority task in * ready queue. This method accelerates seeking speed for * the max priority task. */ prio_rdy_grup = 0; for(i = 0; i < READY_NUM; i++) { prio_rdy[i] = 0; } initmem(); /* Initialize memory management section */ initsem(); /* Initialize semaphore section */ initirq(); /* Initialize interruption */ task.next = &task; task.prev = &task; /* Create Init and idle task */ create_task(idle_task, &idle_tcb, IDLE_TASK_ID, IDLE_TASK_PRIO, 5, idle_stk, IDLE_TASK_STACK_SIZE); create_task(Init, &init_tcb, INIT_TASK_ID, INIT_TASK_PRIO, 20, init_stk, INIT_TASK_STACK_SIZE); init_tick(); /* Initialize time tick */ scheduler(); /* OK, Bycore is running! */}/** * osCreateTask - Creation a new task. * @pTask: Pointer to start addrees of task. * @pTcb: Pointer to TCB of task * @TaskID: Task ID. * @Prio: The priority of task. * @Time: Slice time. * @pStk: Pointer to stack space. * @StkSize: Stack size. * @return: * * @notes: */void osCreateTask(void(*pTask)(), tcb_t *pTcb, uword_t TaskID, uword_t Prio, uword_t Time, stk_t *pStk, uword_t StkSize) { mac_disable_irq(); create_task(pTask, pTcb, TaskID, Prio, Time, (word_t*)pStk, StkSize); mac_enable_irq(); scheduler();}/** * osInitTask - Creation a new task. * @pTask: Pointer to start addrees of task. * @TaskID: Task ID. * @Prio: The priority of task. * @Time: Slice time. * @StkSize: Stack size. * @return: * * @notes: */void osInitTask(void(*pTask)(), uword_t TaskID, uword_t Prio, uword_t Time, uword_t StkSize) { tcb_t *ptcb; stk_t *stk; mac_disable_irq(); if(Prio >= MAX_PRIO) { return ; } if((ptcb = (tcb_t*)allocate(sizeof(tcb_t))) == NULL) { mac_enable_irq(); return ; } if((stk = (stk_t*)allocate(StkSize *sizeof(stk_t))) == NULL) { free(ptcb); mac_enable_irq(); return ; } if((create_task(pTask, ptcb, TaskID, Prio, Time, stk, StkSize) == FALSE)) { free(ptcb); free(stk); mac_enable_irq(); return; } mac_enable_irq(); scheduler();}/** * ChgPrio - Change the priority of a task. * @NewPrio - The new priority. * @return: * * @notes: */void osChgPrio(uword_t NewPrio) { if(NewPrio == pcurtcb->prio || NewPrio >= MAX_PRIO) { return ; } mac_disable_irq(); del_tcb_rdy(pcurtcb); pcurtcb->prio = NewPrio; add_tcb_rdy(pcurtcb); mac_enable_irq(); scheduler();}/** * Sleep - Make a task to sleep. * @return: * * @notes: */void osSleep(void) { list_t *plist; mac_disable_irq(); pcurtcb->status = T_SLEEP; plist = del_tcb_rdy(pcurtcb); if(plist == NULL) { mac_enable_irq(); return ; } add_node_list_rear(&psleep, plist); mac_enable_irq(); scheduler();}/** * osWakeUp - Wake up a sleep task. * @TaskID - The ID of sleep task. * @return: * * @notes: */void osWakeUp(uword_t TaskID) { tcb_t *ptcb; list_t *plist = psleep.next; mac_disable_irq(); do { ptcb = mac_find_entry(plist, tcb_t, link); if((ptcb->id == TaskID) && (ptcb->status == T_SLEEP)) { ptcb->status = T_READY; plist = del_node_list(&psleep, &ptcb->link); add_tcb_rdy(mac_find_entry(plist, tcb_t, link)); mac_enable_irq(); scheduler(); break; } plist = plist->next; } while(plist != psleep.next);}/** * osWait - Make a task to been delayed for while. * @DelayTime - How long the task will be delayed. * @return: * * @notes: */void osWait(uword_t DelayTime) { list_t *plist; if(DelayTime > 0) { mac_disable_irq(); pcurtcb->status = T_DELAY; pcurtcb->delay_time = DelayTime; plist = del_tcb_rdy(pcurtcb); if(plist != NULL) { add_node_list_rear(&pdelay, plist); mac_enable_irq(); scheduler(); } }}/** * osKill - Finish a life of a task. * @return: * * @notes: */void osKill(void) { release_sem(pcurtcb); mac_disable_irq(); del_tcb_rdy(pcurtcb); del_node_list(&task, &pcurtcb->task_link); /* Task was created by osInitTask() */ if((word_t)pcurtcb->pstart >= MEM_START_ADDR) { free(pcurtcb->pstart); free(pcurtcb); } pcurtcb = NULL; mac_enable_irq(); scheduler();}/** * osGetTaskHead - Get the head of task list. * @return: The head of task list. * * @notes: */list_t *osGetTaskHead(void) { return &task;}/** * osVersion - Get the head sleep queue. * @return: The head sleep queue. * * @notes: */list_t *osGetSleepHead(void) { return &psleep;}/** * osVersion - Get the head delay queue. * @return: The head delay queue. * * @notes: */list_t *osGetDelayHead(void) { return &pdelay;}/** * osVersion - Return the current version of Bycore. * @return: The current version of Bycore. * * @notes: */uword_t osVersion(void) { return VERSION;}/** * systick - The heart of Bycore, handle the system tick. * @return: * * @notes: It only refreshes deleyed time of all delayed tasks In this routines. */void systick(void) { tcb_t *ptcb; list_t *plist; list_t *plistnext; sysclock++; if(pcurtcb != NULL && (pcurtcb->status == T_READY)) { pcurtcb->exe_time++; } plist = pdelay.next; if(plist != &pdelay) { do { ptcb = mac_find_entry(plist, tcb_t, link); if(--ptcb->delay_time <= 0) { ptcb->status = T_READY; plistnext = plist->next; plist = del_node_list(&pdelay, plist); add_tcb_rdy(ptcb); if(plistnext != plist) { plist = plistnext; } else { plist = &pdelay; } } else { plist = plist->next; } } while((plist != pdelay.next) && (plist != &pdelay)); }}/** * if_isr - To determine whether the current context * is in interruption mode or not. * @return: The number of interruption nesting. * * @notes: */uword_t if_isr(void) { return int_cnt;}/** * get_clock - Get the number of clock count. * @return: The number of clock count. * * @notes: */uword_t get_clock(void) { return sysclock;}/** * current - Get the TCB of current task. * @return: The TCB of current task. * * @notes: */tcb_t *current(void) { return pcurtcb;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -