📄 os_cpu_c.c
字号:
if (virtualInterruptFlag==FALSE) DBGPRINT(0x00000001, "*** virtualInteruptFlag disabled when calling OSInterruptThread XXX %d\n", virtualInterruptFlag); DBGPRINT(0x00000001, "--- OSIntEnter\n"); OSIntEnter(); DBGPRINT(0x00000001, "--- OSTimeTick\n"); if (eventType==hInterruptEvent[0]) //Call uCOS' interrupt processing { interruptTable[0](); //IRQ0 reserved vor time tick: OSTimeTick(); } 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; int rc=0; DBGPRINT(0x00000008, "*** OSTimeTickInit\n"); signal(SIGALRM, OSTimeTickCallback); timerVal.it_interval.tv_sec = (1000000/ OS_TICKS_PER_SEC) / 1000000; timerVal.it_interval.tv_usec = (1000000/ OS_TICKS_PER_SEC) % 1000000; timerVal.it_value=timerVal.it_interval; if (rc=setitimer(ITIMER_REAL,&timerVal, NULL)) { printf("uCOS-II ERROR: Timer could not be installed 1 %d\n", rc); 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; cpu_set_t cpuSet; 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); __CPU_ZERO(&cpuSet); __CPU_SET(0, &cpuSet); if (sched_setaffinity(hScheduleThread, sizeof(cpuSet), &cpuSet)) printf("ERROR: Could not set processor affinity\n"); sched_setaffinity(hInterruptThread, sizeof(cpuSet), &cpuSet); 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}// OSTCBInitHook// This hook is invoked during TCB initializationvoid OSTCBInitHook(OS_TCB * pTcb){ int i; void *p; cpu_set_t cpuSet; DBGPRINT(0x00000004, "*** OSTaskCreateHook %u\n", pTcb->OSTCBPrio); p=malloc(TASKSTACKSIZE); p=p+TASKSTACKSIZE; hTaskThread[pTcb->OSTCBPrio]=clone((LPTHREAD_START_ROUTINE) *(pTcb->OSTCBStkPtr), p, //Map uCOS-II task to LINUX thread CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_VM | CLONE_STOPPED, (void *) *(pTcb->OSTCBStkPtr + 1)); __CPU_ZERO(&cpuSet); __CPU_SET(0, &cpuSet); sched_setaffinity(hTaskThread[pTcb->OSTCBPrio], sizeof(cpuSet), &cpuSet); taskSuspended[pTcb->OSTCBPrio]=1; //Create thread in LINUX suspended state DBGPRINT(0x00000004, "--- PID new Task = %u\n", hTaskThread[pTcb->OSTCBPrio]);/* 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 + -