📄 os_cpu_c.c
字号:
/*
*********************************************************************************************************
* 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
*******************************************************************************************************/
/********************************************************************
//
// Author: CaiXinBo \\\\\\\ //
// ( o o ) //
//----------------------oOO----(_)----OOo-----------------------//
FileName: OS_CPU_C.C
Created Date: 23/5/2006 9:45
Modify Date:
Purpose:
*********************************************************************/
#define OS_CPU_GLOBALS
#include "includes.h"
#define CRGFLG (*((volatile unsigned char*)(0x0037)))
OS_STK *OSTaskStkInit(void (*task)(void *pd), void *pdata, OS_STK *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
((INT8U *)stk)--; // Only one byte needed for CCR
*(INT8U *)stk = (INT8U)(0x00); // CCR
((INT8U *)stk)--; // Only one byte needed for PPAGE
*(INT8U *)stk = *(INT8U *)pdata; // PPAGE
return ((OS_STK *)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
*******************************************/
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
ldaa OSRunning
inca // OSRunning = 1
staa OSRunning
pula
staa $30 //restore ppage from stack
nop
rti
}
}
/**********************************************
* INTERRUPT LEVEL CONTEXT SWITCH
**********************************************/
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 //得到新的TCB的地址 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
******************************************/
interrupt 3 void OSCtxSw(void)
{
asm{
ldaa $30 //保存页面寄存器到堆栈中 ->A ,save ppage to stack
psha // A->SP
ldx OSTCBCur //得到TCB的地址 Get the TCB's address
sts 0,x //将SP保存到TCB的第一个字 Save the sp to TCB's first word
} //OS_EXT OS_TCB *OSTCBCur; // Pointer to currently running TCB
OSTaskSwHook(); // 调用接口函数 Call Hook function
OSTCBCur = OSTCBHighRdy; // 把最高优先级的任务控制块传给当前任务控制块 Change OSTCBCur and OSPrioCur
OSPrioCur = OSPrioHighRdy; // 把最高优先级的任务优先级数传给当前务优先级
asm{
ldx OSTCBCur //得到新的TCB的地址 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
}
}
interrupt 7 void OSTickISR(void)
{
asm{
ldaa $30 //save ppage to stack
psha
}
OSIntEnter();
OS_SAVE_SP();
CRGFLG &=0xEF; // clear the interrupt flag
OSTimeTick();
OSIntExit(); // exit interrupt and task switch
asm{
pula
staa $30 //restore ppage from stack
nop
rti
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -