⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 os_cpu_c.c

📁 Ucos ii原理及应用,非常实用的资料,发给大家参考!
💻 C
📖 第 1 页 / 共 2 页
字号:
*********************************************************************************************************
*/


#if OS_CPU_HOOKS_EN > 0
void OSTaskDelHook (OS_TCB *ptcb)
{
    if (ptcb->OSTCBOpt & OS_TASK_OPT_SAVE_FP) {            /* See if task had FP support               */
        if (ptcb->OSTCBExtPtr != (void *)0) {              /* Yes, OSTCBExtPtr must not be NULL        */
            OSMemPut(OSFPPartPtr, ptcb->OSTCBExtPtr);      /*      Return memory block to free pool    */
          //释放内存块到原来的内存分区
        }
    }
}
#endif

/*$PAGE*/
/*
*********************************************************************************************************
*                                             IDLE TASK HOOK
*
* Description: This function is called by the idle task.  This hook has been added to allow you to do  
*              such things as STOP the CPU to conserve power.
*
* Arguments  : none
*
* Note(s)    : 1) Interrupts are enabled during this call.
*********************************************************************************************************
*/
/*
*********************************************************************************************************
                                              空闲任务接口函数
 什么也不做。
 备注:在调用此函数时要开中断
*********************************************************************************************************
*/


#if OS_CPU_HOOKS_EN && OS_VERSION >= 251
void  OSTaskIdleHook (void)
{
}
#endif

/*
*********************************************************************************************************
*                                           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
*********************************************************************************************************
*/
/*
*********************************************************************************************************
                                             统计任务接口函数
描述:这个任务由icosII统计任务每一秒钟调用,它允许应用中
加入功能到统计任务
参数:无
*********************************************************************************************************
*/


#if OS_CPU_HOOKS_EN > 0
void OSTaskStatHook (void)
{
}
#endif

/*$PAGE*/
/*
*********************************************************************************************************
*                                        INITIALIZE A TASK'S STACK
*
* Description: This function is called by either OSTaskCreate() or OSTaskCreateExt() to initialize the
*              stack frame of the task being created.  This function is highly processor specific.
*
* Arguments  : task          is a pointer to the task code
*
*              pdata         is a pointer to a user supplied data area that will be passed to the task
*                            when the task first executes.
*
*              ptos          is a pointer to the top of stack.  It is assumed that 'ptos' points to
*                            a 'free' entry on the task stack.  If OS_STK_GROWTH is set to 1 then 
*                            'ptos' will contain the HIGHEST valid address of the stack.  Similarly, if
*                            OS_STK_GROWTH is set to 0, the 'ptos' will contains the LOWEST valid address
*                            of the stack.
*
*              opt           specifies options that can be used to alter the behavior of OSTaskStkInit().
*                            (see uCOS_II.H for OS_TASK_OPT_???).
*
* Returns    : Always returns the location of the new top-of-stack' once the processor registers have
*              been placed on the stack in the proper order.
*
* Note(s)    : Interrupts are enabled when your task starts executing. You can change this by setting the
*              PSW to 0x0002 instead.  In this case, interrupts would be disabled upon task startup.  The
*              application code would be responsible for enabling interrupts at the beginning of the task
*              code.  You will need to modify OSTaskIdle() and OSTaskStat() so that they enable 
*              interrupts.  Failure to do this will make your system crash!
*********************************************************************************************************
*/
/*
*********************************************************************************************************
                                                          任务堆栈初始化函数
描述:这个函数由OSTaskCreate() or OSTaskCreateExt()调用,初始化将要建立任务的堆栈结构。
           这个函数是一些处理器特有的。
参数:task:指向任务代码的指针
           pdata:当任务第一次运行时,指用户将传递给任务的由任务指定的数据区域指针
           ptos:指向栈顶的指针。它假定 ptos 指向任务堆栈的空闲入口。如果OS_STK_GROWTH
                  为1,ptos将包含堆栈的最高有效埴,同理,如果为0,将包含堆栈最低地址。
           opt:用于改变OSTaskStkInit()习惯的专有特征(见uCOS_II.H for OS_TASK_OPT_???)
返回:如果处理器寄存器放置在堆栈适当顺序,将始终返回新栈顶位置。
注意:当任务开始执行的时候中断打开。你可以通过将PSW改变成0x0002来改变它,这种情况下,任务
         启动的时候中断将关闭。你还需要修改OSTaskIdle() and OSTaskStat()这样它们会开中断,
         如果不这样做系统会崩溃。

*********************************************************************************************************
*/


OS_STK  *OSTaskStkInit (void (*task)(void *pd), void *pdata, OS_STK *ptos, INT16U opt)
{
    INT16U *stk;


    opt    = opt;                           /* 'opt' is not used, prevent warning                      */
	//opt没有用到,防止编译器警告
    stk    = (INT16U *)ptos;                /* Load stack pointer                                      */
	//建立并初始化一个变量,指向以字为单位的内存区,同时
	//指针ptos指向空堆栈入口
    *stk-- = (INT16U)FP_SEG(pdata); /* Simulate call to function with argument *///用堆栈来传递参数pdata。此
    *stk-- = (INT16U)FP_OFF(pdata);//时参数pdata的段地址和偏移量都将按次序保存在堆栈中         
    *stk-- = (INT16U)FP_SEG(task);//堆栈紧接着是任务函数的起始地址,理论上应该为任务的返回
    *stk-- = (INT16U)FP_OFF(task);//地址,但ucos任务为无限循环结构,无返回点,所以此内容是什么不重要。
    *stk-- = (INT16U)0x0202;                /* SW = Interrupts enabled                                 */
	//接着是状态字,为0x2020时任务启动时,中断开着,也可以设置成关着,但函数要么中断全
	//开,要么全关,否则会造成系统崩溃。
    *stk-- = (INT16U)FP_SEG(task);          /* Put pointer to task   on top of stack                   */
    *stk-- = (INT16U)FP_OFF(task);
//堆栈中还须留出各个寄存器空间,寄存器在堆栈中的位置要和运行
//指令PUSHA,PUSH ES和PUSH DS后压入堆栈的次序相同。上述指令在
//每次进入中断服务子程序时都会被调用。下面几个寄存器的
//次序是与指令PUSHA的压栈次序相同的。如果使用没有PUSHA指令的
//处理器,就要使用多个PUSH指令压入上述寄存器,且顺序要与PUSHA
//相同,程序中,每个寄存器值各不相同是为了调试方便。
    *stk-- = (INT16U)0xAAAA;                /* AX = 0xAAAA                                             */
    *stk-- = (INT16U)0xCCCC;                /* CX = 0xCCCC                                             */
    *stk-- = (INT16U)0xDDDD;                /* DX = 0xDDDD                                             */
    *stk-- = (INT16U)0xBBBB;                /* BX = 0xBBBB                                             */
    *stk-- = (INT16U)0x0000;                /* SP = 0x0000                                             */
    *stk-- = (INT16U)0x1111;                /* BP = 0x1111                                             */
    *stk-- = (INT16U)0x2222;                /* SI = 0x2222                                             */
    *stk-- = (INT16U)0x3333;                /* DI = 0x3333                                             */
    *stk-- = (INT16U)0x4444;                /* ES = 0x4444                                             */
    *stk   = _DS;                           /* DS = Current value of DS    */
	//Borland编译器支持虚拟寄存器变量操作,程序清单用_OS关键字
	//取得CPU中DS的值,直接复制到堆栈中
    return ((OS_STK *)stk);
	//返回新的堆栈栈顶指针,OSTaskCreate() or OSTaskCreateExt()将指针保存在
	//任务的控制块OS_TCB中。
}

/*$PAGE*/
/*
*********************************************************************************************************
*                                           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).
*********************************************************************************************************
*/
/*
*********************************************************************************************************
                                               任务切换函数
描述:要执行任务切换的时候调用此函数,当任务切换时,允许做
其它运行动作。
参数:无
备注:1、调用此函数时中断要关闭
                         2、它假定全局变量OSTCBHighRdy指向任务的TCB时将切换进去,
                             而如果OSTCBCur指向任务则被切换出去
                         3、OSTCBCur指向被切换出去任务的TCB,而  OSTCBHighRdy 指向新任务的TCB
*********************************************************************************************************
*/


#if OS_CPU_HOOKS_EN > 0
void OSTaskSwHook (void)
{
    INT8U  err;
    void  *pblk;
                                                           /* Save FPU context of preempted task       */
    if (OSRunning == TRUE) {                               /* Don't save on OSStart()!                 */
        if (OSTCBCur->OSTCBOpt & OS_TASK_OPT_SAVE_FP) {    /* See if task used FP                      */
            pblk = OSTCBCur->OSTCBExtPtr;                  /* Yes, Get pointer to FP storage area      */
            if (pblk != (void *)0) {                       /*      Make sure we have storage           */
                OSFPSave(pblk);                            /*      Save the FPU registers in block     */
            }
        }
    }
                                                           /* Restore FPU context of new task          */
		//保存新任务FPU内容
    if (OSTCBHighRdy->OSTCBOpt & OS_TASK_OPT_SAVE_FP) {    /* See if new task uses FP                  */
		//新任务用浮点吗?
        pblk = OSTCBHighRdy->OSTCBExtPtr;                  /* Yes, Get pointer to FP storage area      */
		//用的话,得到FP存储空间的地址
        if (pblk != (void *)0) {                           /*      Make sure we have storage           */
			//保证得到的空间合法
            OSFPRestore(pblk);                             /*      Get contents of FPU registers       */
			//保存
        }
    }
}
#endif

/*
*********************************************************************************************************
*                                           OSTCBInit() HOOK
*
* Description: This function is called by OS_TCBInit() after setting up most of the TCB.
*
* Arguments  : ptcb    is a pointer to the TCB of the task being created.
*
* Note(s)    : 1) Interrupts may or may not be ENABLED during this call.
*********************************************************************************************************
*/
/*
********************************************************************
                                           OSTCBInit() HOOK
 描述:这个函数在设置大多数TCB后由OS_TCBInit() 调用
 参数:ptcb,是一个被创建任务的TCB的指针
 备注:当调用它的时候中断可以或者不被能使
**********************************************************************
*/
#if OS_CPU_HOOKS_EN > 0 && OS_VERSION > 203
void  OSTCBInitHook (OS_TCB *ptcb)
{
    ptcb = ptcb;     /* Prevent Compiler warning  防止编译器警告      */
}
#endif


/*
*********************************************************************************************************
*                                               TICK HOOK
*
* Description: This function is called every tick.
*
* Arguments  : none
*
* Note(s)    : 1) Interrupts may or may not be ENABLED during this call.
*********************************************************************************************************
*/
//此函数每一个节拍都调用,以便用户能处理应急的事务。
//备注:调用此函数的时候中断可能开,也可能不开。
#if OS_CPU_HOOKS_EN > 0
void OSTimeTickHook (void)
{
}
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -