📄 os_cpu_c.c
字号:
} 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){ 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 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(8192); p=p+8192; q=malloc(8192); q=q+8192; DBGPRINT(0x00000010, "*** OSInitHookBegin\n"); hMainThread=getpid(); hScheduleEvent = SIGCONT; signal(hScheduleEvent, OSScheduleEventHandler); sigemptyset(&interruptSignalSet); sigaddset(&interruptSignalSet, hScheduleEvent); for (i=0; i< NINTERRUPTS; i++) { hInterruptEvent[i] = SIGRTMIN+10+i; interruptTable[i] = OSDummyISR; 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); 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; } 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; raise(SIGSTOP); //Suspend the task localTaskStart.routine(localTaskStart.param); //Continue the task}// OSTaskCreateHook **********************************************************// This hook is invoked during task creation. MUST NOT BE DEFINED BY THE APPLICATION.void OSTaskCreateHook(OS_TCB *pTcb){ int i; void *p; p=malloc(8192); p=p+8192; DBGPRINT(0x00000004, "*** OSTaskCreateHook %u\n", pTcb->OSTCBPrio); taskStart.flag=FALSE; taskStart.routine=(LPTHREAD_START_ROUTINE) *(pTcb->OSTCBStkPtr); taskStart.param =(void *) *(pTcb->OSTCBStkPtr + 1); 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); 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}// 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 sched_yield(); //Give Linux a chance to run other applications to when uCOS-II idles}/******************************************************************************** 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(){}/*void OSTaskCreateHook(OS_TCB * pTcb) MUST NOT BE DEFINED BY THE APPLICATION, see above.*/// OSTaskDelHook *************************************************************// This hook is invoked during task deletion.void OSTaskDelHook(OS_TCB * pTcb){}// OSTCBInitHook// This hook is invoked during TCB initializationvoid OSTCBInitHook(OS_TCB * ptcb){}/*void OSTaskIdleHook() MUST NOT BE DEFINED BY THE APPLICATION, see above.*/// 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); 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();// DBGPRINT(0x00000002, "*** OSStartHighRdy Idle loop\n"); } printf("ERROR: Primary thread killed - exiting\n");}// OSCtxSw ********************************************************************// Task context switchvoid OSCtxSw(void){ OSTaskSwHook(); //Call the task switch hook function DBGPRINT(0x00000002, "*** OSCtxSw from %2u to %2u\n", OSPrioCur, OSPrioHighRdy); kill(hScheduleThread,hScheduleEvent); //Trigger scheduling thread sched_yield(); //... and give Linux a chance to invoke it}// OSIntCtxSw *****************************************************************// Interrupt context switchvoid OSIntCtxSw(void){ OSTaskSwHook(); //Call the task switch hook function DBGPRINT(0x00000002, "*** OSCIntCtxSw from %2u to %2u\n", OSPrioCur, OSPrioHighRdy); kill(hScheduleThread,hScheduleEvent); //Trigger scheduling thread sched_yield(); //... and give Linux a chance to invoke it}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -