📄 os_cpu_c.c
字号:
} else if (eventType==hInterruptEvent[1]) { interruptTable[1](); } else if (eventType==hInterruptEvent[2]) { interruptTable[2](); } else if (eventType==hInterruptEvent[3]) { interruptTable[3](); } else if (eventType==hInterruptEvent[4]) { interruptTable[4](); } else if (eventType==hInterruptEvent[5]) { interruptTable[5](); } else if (eventType==hInterruptEvent[6]) { interruptTable[6](); } else if (eventType==hInterruptEvent[7]) { interruptTable[7](); } else { perror("OSInterruptThread(): Unsupported interrupt\n"); } DBGPRINT(0x00000001, "--- OSIntExit\n"); OSIntExit(); LeaveCriticalSection(&criticalSection); }}/********************************************************************************************************** uCOS-II Functions**********************************************************************************************************/// OSStopThreads *************************************************************// Stop all threads before exiting the programvoid OSStopThreads(void){ DBGPRINT(0x00000001, "--- Sending signal SIGKILL to PID=%u\n", hMainThread); kill(hMainThread, SIGKILL);}// OSTimeTickInit ************************************************************// Initialize the LINUX multimedia timer to simulate time tick interruptsvoid OSTimeTickInit(){ ITIMERVAL timerVal; DBGPRINT(0x00000008, "*** OSTimeTickInit\n"); signal(SIGALRM, OSTimeTickCallback); timerVal.it_interval.tv_sec = 0; timerVal.it_interval.tv_usec = 1000000/ OS_TICKS_PER_SEC; timerVal.it_value=timerVal.it_interval; if (setitimer(ITIMER_REAL,&timerVal, NULL)) { printf("uCOS-II ERROR: Timer could not be installed 1\n"); exit(-1); }}// OSTaskStkInit *************************************************************// This function does initialize the stack of a task (this is only a dummy function// for compatibility reasons, because this stack is not really used (except to refer// to the task parameter *pdata)OS_STK *OSTaskStkInit(void (*task) (void *pd), void *pdata, OS_STK *ptos, INT16U opt){ OS_STK *stk; DBGPRINT(0x00000010, "*** OSTaskStkInit\n"); stk = (OS_STK *) ptos; // Load stack pointer *--stk = (INT32U) pdata; // Push pdata on the stack *--stk = (INT32U) task; // Push the task start address on the stack DBGPRINT(0x00000010, "*** OSTaskStkInit end\n"); return stk;}/******************************************************************************** Internal Hook functions (used by the LINUX port, not user hookable)********************************************************************************/// OSInitHookBegin ***********************************************************// This hook is invoked at the beginning of the OS initialization. MUST NOT BE DEFINED BY THE APPLICATION.void OSInitHookBegin(){ int i; char temp[256]; void *p, *q; p=malloc(TASKSTACKSIZE); p=p+TASKSTACKSIZE; q=malloc(TASKSTACKSIZE); q=q+TASKSTACKSIZE; DBGPRINT(0x00000010, "*** OSInitHookBegin PID=%u TID=%u\n", getpid(), gettid()); hMainThread=getpid(); hScheduleEvent = SIGCONT; signal(hScheduleEvent, OSScheduleEventHandler); sigemptyset(&scheduleSignalSet); sigaddset(&scheduleSignalSet, hScheduleEvent); sigemptyset(&interruptSignalSet); sigaddset(&interruptSignalSet, hScheduleEvent); for (i=0; i< NINTERRUPTS; i++) { interruptTable[i] = OSDummyISR; if (i==0) //Seems that Linux kernel 2.6.x does not support hInterruptEvent[i]=SIGUSR1; //real time signals SIGRTMIN ... ??? else if (i==1) hInterruptEvent[i]=SIGUSR2; else hInterruptEvent[i] = SIGRTMIN+10+i; signal(hInterruptEvent[i], OSInterruptEventHandler); sigaddset(&interruptSignalSet, hInterruptEvent[i]); } InitializeCriticalSection(&criticalSection); hScheduleThread=clone((LPTHREAD_START_ROUTINE) OSScheduleThread, p, //Start the schedule thread CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_VM, NULL); hInterruptThread=clone((LPTHREAD_START_ROUTINE) OSInterruptThread, q, //Start the Interrupt thread CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_VM, NULL); interruptTable[0] = OSTimeTick; atexit(OSStopThreads); DBGPRINT(0x00000010, "--- PID schedule thread = %u interrupt thread = %u\n", hScheduleThread, hInterruptThread); if (setpriority(PRIO_PROCESS, hScheduleThread, -10)) //Set the scheduler and interrupt threads to maximum LINUX priority DBGPRINT(0x00000010, "Setting hScheduleThread priority failed\n"); if (setpriority(PRIO_PROCESS, hInterruptThread, -10)) DBGPRINT(0x00000010, "Setting hInterruptThread priority failed\n"); signal(SIGINT, CtrlBreakHandler); signal(SIGTSTP, CtrlBreakHandler); sched_yield(); //Give Linux a chance to start the scheduler and interrupt thread //ADD YOUR OWN HOOK CODE HERE IF NECESSARY}typedef struct { BOOLEAN flag; LPTHREAD_START_ROUTINE routine; void *param; INT16U prio;} TASKSTART;TASKSTART taskStart;// OSGenericTaskRoutine ******************************************************// Used to suspend a task after creationint OSGenericTaskRoutine(void *param){ TASKSTART localTaskStart; //Flag, that the new task has been started memcpy(&localTaskStart, param, sizeof(TASKSTART)); taskStart.flag=TRUE; DBGPRINT(0x00000004, "--- Sending signal SIGSTOP to self %u ---\n", taskStart.prio); kill(getpid(), SIGSTOP); //Suspend the task DBGPRINT(0x00000004, "--- Continuing task %u ---\n", localTaskStart.prio); localTaskStart.routine(localTaskStart.param); //Continue the task}// OSTCBInitHook// This hook is invoked during TCB initializationvoid OSTCBInitHook(OS_TCB * pTcb){ int i; void *p; DBGPRINT(0x00000004, "*** OSTaskCreateHook %u\n", pTcb->OSTCBPrio); p=malloc(TASKSTACKSIZE); p=p+TASKSTACKSIZE; taskStart.flag=FALSE; taskStart.routine=(LPTHREAD_START_ROUTINE) *(pTcb->OSTCBStkPtr); taskStart.param =(void *) *(pTcb->OSTCBStkPtr + 1); taskStart.prio = pTcb->OSTCBPrio; hTaskThread[pTcb->OSTCBPrio]=clone((LPTHREAD_START_ROUTINE) OSGenericTaskRoutine, p, //Map uCOS-II task to LINUX thread CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_VM, (void *) &taskStart); DBGPRINT(0x00000004, "--- PID new Task = %u\n", hTaskThread[pTcb->OSTCBPrio]); while(taskStart.flag==FALSE) sched_yield(); taskSuspended[pTcb->OSTCBPrio]=1; //Create thread in LINUX suspended state/* Does not work ??? if (OS_BOOST_LINUX_PRIORITY && (pTcb->OSTCBPrio!=OS_LOWEST_PRIO)) //Boost LINUX thread priorites (except idle thread) setpriority(PRIO_PROCESS, hTaskThread[pTcb->OSTCBPrio], -5);*/ //ADD YOUR OWN HOOK CODE HERE IF NECESSARY}/******************************************************************************** Hook functions (user hookable)********************************************************************************/#if OS_CPU_HOOKS_EN > 0/*void OSInitHookBegin() MUST NOT BE DEFINED BY THE APPLICATION, see above.*/// OSInitHookEnd *************************************************************// This hook is invoked at the end of the OS initialization.void OSInitHookEnd(){}// OSTaskCreateHook **********************************************************// This hook is invoked during task creation.void OSTaskCreateHook(OS_TCB *pTcb){}// OSTaskDelHook *************************************************************// This hook is invoked during task deletion.void OSTaskDelHook(OS_TCB * pTcb){}/*void OSTCBInitHook(OS_TCB *pTcb) MUST NOT BE DEFINED BY THE APPLICATION, see above*/// OSTaskIdleHook ************************************************************// This hook is invoked from the idle task. MUST NOT BE DEFINED BY THE APPLICATION.void OSTaskIdleHook(){ if (idleTrigger) //Issue a debug message, each time the idle task is reinvoked { DBGPRINT(0x00000020, "*** OSTaskIdleHook\n"); idleTrigger = FALSE; } //ADD YOUR OWN HOOK CODE HERE IF NECESSARY OS_SLEEP(); //Give Linux a chance to run other applications to when uCOS-II idles}// OSTaskStatHook ************************************************************// This hook is invoked by the statistical task every second.void OSTaskStatHook(){}// OSTimeTickHook ************************************************************// This hook is invoked during a time tick.void OSTimeTickHook(){}// OSTaskSwHook **************************************************************// This hook is invoked during a task switch.// OSTCBCur points to the current task (being switched out).// OSTCBHighRdy points on the new task (being switched in).void OSTaskSwHook(){}#else#pragma message("INFO: Hook functions must be defined in the application")#endif/******************************************************************************** Internal Task switch functions********************************************************************************/// OSStartHighRdy *************************************************************// Start first taskvoid OSStartHighRdy(void){ //OSTaskSwHook(); //Call the task switch hook function OSTimeTickInit(); //Initialize time ticks // Increment OSRunning by 1 OSRunning++; DBGPRINT(0x00000002, "*** OSStartHighRdy from %2u to %2u\n", OSPrioCur, OSPrioHighRdy); DBGPRINT(0x00000002, "--- Sending signal hScheduleEvent to PID=%u\n", hScheduleThread); kill(hScheduleThread,hScheduleEvent); //Trigger scheduling thread while(OSRunning) //A redundant idle thread, in case the uCOS { //usleep(1000); //scheduler does not invoke the uCOS idle task// sched_yield(); OS_SLEEP(); } printf("ERROR: Primary thread killed - exiting %d\n");}// OSCtxSw ********************************************************************// Task context switchvoid OSCtxSw(void){ DBGPRINT(0x00000002, "*** OSCtxSw from %2u to %2u\n", OSPrioCur, OSPrioHighRdy); kill(hScheduleThread,hScheduleEvent); //Trigger scheduling thread}// OSIntCtxSw *****************************************************************// Interrupt context switchvoid OSIntCtxSw(void){ DBGPRINT(0x00000002, "*** OSCIntCtxSw from %2u to %2u\n", OSPrioCur, OSPrioHighRdy); kill(hScheduleThread,hScheduleEvent); //Trigger scheduling thread}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -