/* ********************************************************************************************************* * uC/OS-II * The Real-Time Kernel * * (c) Copyright 1992-1998, Jean J. Labrosse, Plantation, FL * All Rights Reserved * * * MC9S12DP256/DG128 Specific code * SMALL MEMORY MODEL * * File : OS_CPU_C.C * By : Jean J. Labrosse * Update By : Shao Senlong ********************************************************************************************************* */ #define OS_CPU_GLOBALS #include "includes.h" //#define CRGFLG (*((volatile unsigned char*)(0x0037))) //-------------------------------------------------------------------------* //定时器中断函数,用来产生时钟节拍 * //参数:无 * //返回值:无 * //-------------------------------------------------------------------------* void OSTickISR(void) { DisableInterrupts; //关中断 asm{ ldaa $30 //把存储页面寄存器入栈 psha } OSIntEnter(); OS_SAVE_SP(); CRGFLG|=0x80; // 清定时中断 OSTimeTick(); OSIntExit(); // 退出中断,切换任务 asm{ pula staa $30 //恢复页面寄存器 nop rti } EnableInterrupts; //开中断 } void *OSTaskStkInit (void (*task)(void *pd), void *pdata, void *ptos, INT16U opt) { INT16U *stk; stk = (INT16U *)ptos; // Load stack pointer *--stk = opt; // opt There is one byte blank *--stk = (INT16U)(task); // PC for use of opt in task *--stk = (INT16U)(task); // PC *--stk = (INT16U)(0x1122); // Y *--stk = (INT16U)(0x3344); // X ((INT8U *)stk)--; // Only one byte needed for A *(INT8U *)stk = (INT8U)(((INT16U)pdata)>>8); // A ((INT8U *)stk)--; // Only one byte needed for B *(INT8U *)stk = (INT8U)(pdata); // B //S12X系列单片机用到CCR为16位 ((INT8U *)stk)--; // one byte needed for CCR *(INT8U *)stk = (INT8U)(0x00); // CCR ((INT8U *)stk)--; // one byte needed for CCR *(INT8U *)stk = (INT8U)(0x00); // CCR ((INT8U *)stk)--; // Only one byte needed for PPAGE *(INT8U *)stk = *(INT8U *)pdata; // PPAGE return ((void *)stk); } /*$PAGE*/ /*************************接口函数***********************/ #if OS_CPU_HOOKS_EN /* ********************************************************************************************************* * TASK CREATION HOOK * * Description: This function is called when a task is created. * * Arguments : ptcb is a pointer to the task control block of the task being created. * * Note(s) : 1) Interrupts are disabled during this call. ********************************************************************************************************* */ void OSTaskCreateHook (OS_TCB *ptcb) { ptcb = ptcb; /* Prevent compiler warning */ } /* ********************************************************************************************************* * TASK DELETION HOOK * * Description: This function is called when a task is deleted. * * Arguments : ptcb is a pointer to the task control block of the task being deleted. * * Note(s) : 1) Interrupts are disabled during this call. ********************************************************************************************************* */ void OSTaskDelHook (OS_TCB *ptcb) { ptcb = ptcb; /* Prevent compiler warning */ } /* ********************************************************************************************************* * TASK SWITCH HOOK * * Description: This function is called when a task switch is performed. This allows you to perform other * operations during a context switch. * * Arguments : none * * Note(s) : 1) Interrupts are disabled during this call. * 2) It is assumed that the global pointer 'OSTCBHighRdy' points to the TCB of the task that * will be 'switched in' (i.e. the highest priority task) and, 'OSTCBCur' points to the * task being switched out (i.e. the preempted task). ********************************************************************************************************* */ void OSTaskSwHook (void) { } /* ********************************************************************************************************* * STATISTIC TASK HOOK * * Description: This function is called every second by uC/OS-II's statistics task. This allows your * application to add functionality to the statistics task. * * Arguments : none ********************************************************************************************************* */ void OSTaskStatHook (void) { } /* ********************************************************************************************************* * TICK HOOK * * Description: This function is called every tick. * * Arguments : none * * Note(s) : 1) Interrupts may or may not be ENABLED during this call. ********************************************************************************************************* */ void OSTimeTickHook (void) { } void OSTaskIdleHook(void) { } void OSInitHookBegin(void) { } void OSInitHookEnd(void) { } void OSTCBInitHook(OS_TCB *ptcb) { ptcb = ptcb; /* Prevent compiler warning */ } #endif /****************************************** * START HIGHEST PRIORITY TASK READY-TO-RUN *******************************************/ //OSStartHighRdy:让优先级最高的任务运行函数--------------------------------* //功 能:让优先级最高的就绪态任务开始运行 * //参 数:无 * //返 回:无 * //-------------------------------------------------------------------------* void OSStartHighRdy(void) { OSTaskSwHook(); // Call Hook function asm{ ldx OSTCBCur // Load the value in OSTCBCur or the TCB's address to x lds 0,x // Load the value pointed by OSTCBCur to sp inc OSRunning // OSRunning = 1 pula staa $30 //restore ppage from stack nop rti } } /********************************************** * INTERRUPT LEVEL CONTEXT SWITCH **********************************************/ //OSIntCtxSw:中断级任务切换函数--------------------------------------------* //功 能:如果在中断服务结束时,中断中激活了更高优先级的任务,则调用该函数 * //参 数:无 * //返 回:无 * //-------------------------------------------------------------------------* void OSIntCtxSw(void) { /* asm{ leas 4,sp // Adjust the sp ldx OSTCBCur // Get the TCB's address sts 0,x // Save the sp to TCB's first word } */ OSTaskSwHook(); // Call Hook function OSTCBCur = OSTCBHighRdy; // Change OSTCBCur and OSPrioCur OSPrioCur = OSPrioHighRdy; asm{ ldx OSTCBCur // Get the new task's TCB's address lds 0,x // Load the new task's sp to sp register from its TCB pula staa $30 //restore ppage from stack nop rti } } /******************************************* * TASK LEVEL CONTEXT SWITCH ******************************************/ //OSCtxSw:任务级任务切换函数-----------------------------------------------* //功 能:当前运行的任务切换为比它优先级高的任务 * //参 数:无 * //返 回:无 * //------------------------------------------------------------------------ void OSCtxSw(void) { asm{ ldaa $30 //save ppage to stack psha ldx OSTCBCur // Get the TCB's address sts 0,x // Save the sp to TCB's first word } OSTaskSwHook(); // Call Hook function OSTCBCur = OSTCBHighRdy; // Change OSTCBCur and OSPrioCur OSPrioCur = OSPrioHighRdy; asm{ ldx OSTCBCur // Get the new task's TCB's address lds 0,x // Load the new task's sp to sp register from its TCB pula staa $30 //restore ppage from stack nop rti } }