os_cpu_c.c

来源:互联网 发布:淘宝闲鱼小二介入草率 编辑:程序博客网 时间:2024/06/05 16:08

这个函数里主要是OSTaskStkInit()函数和一些钩子函数,需要关心的是OSTaskStkInit()和OSTaskSwHook()。

OSTaskStkInit()是任务堆栈初始化函数,它由任务建立函数OSTaskCreateExt()调用,用于初始化任务的堆栈,堆栈的结构初始化成看起来刚刚发生过中断一样,所有的寄存器都被入栈。

OSTaskStkInit()函数有四个参数,task,p_arg,ptos,opt,它们和建立任务所使用的参数是一致的,具体的介绍可以看前面的日志。函数返回堆栈指针所指的地址。

当我们使用OSTaskCreateExt()来建立任务时,就把上述四个参数传递给OSTaskStkInit()函数,初始化好堆栈,再返回给OSTaskCreateExt()堆栈指针,继续走下去。

注意看OSTaskStkInit()函数代码最后,使用R0来传递参数,这是符合ARM汇编的规范,使用R0来传递第一个参数。

OSTaskSwHook()函数用于扩展任务切换代码的功能,他被OSStartHighRdy(),OSCtxSw(),OSIntCtxSw()调用。OSTaskSwHook()可以直接访问全局变量OSTCBCur 和OSTCBHighRdy。OSTCBCur 指向被切换出去的任务OS_TCB,而OSTCBHighRdy 指向新任务OS_TCB。注意在调用OSTaskSwHook()期间中断一直是被禁止的。

 
 
 

/*
*********************************************************************************************************
* 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
*
* p_arg 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_xxx).
*
* 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) : 1) Interrupts are enabled when your task starts executing.
* 2) All tasks run in SVC mode.
*********************************************************************************************************
*/


OS_STK *OSTaskStkInit (void (*task)(void *p_arg), void *p_arg, OS_STK *ptos, INT16U opt)
{
    OS_STK *stk;
    INT32U task_addr;


    opt = opt; /* 'opt' is not used, prevent warning */
    stk = ptos; /* Load stack pointer */
    task_addr = (INT32U)task & ~1; /* Mask off lower bit in case task is thumb mode */
    *(stk) = (INT32U)task_addr; /* Entry Point */
    *(--stk) = (INT32U)0x14141414L; /* R14 (LR) */
    *(--stk) = (INT32U)0x12121212L; /* R12 */
    *(--stk) = (INT32U)0x11111111L; /* R11 */
    *(--stk) = (INT32U)0x10101010L; /* R10 */
    *(--stk) = (INT32U)0x09090909L; /* R9 */
    *(--stk) = (INT32U)0x08080808L; /* R8 */
    *(--stk) = (INT32U)0x07070707L; /* R7 */
    *(--stk) = (INT32U)0x06060606L; /* R6 */
    *(--stk) = (INT32U)0x05050505L; /* R5 */
    *(--stk) = (INT32U)0x04040404L; /* R4 */
    *(--stk) = (INT32U)0x03030303L; /* R3 */
    *(--stk) = (INT32U)0x02020202L; /* R2 */
    *(--stk) = (INT32U)0x01010101L; /* R1 */
    *(--stk) = (INT32U)p_arg; /* R0 : argument */
    if ((INT32U)task & 0x01) { /* See if task runs in Thumb or ARM mode */
        *(--stk) = (INT32U)ARM_SYS_MODE_THUMB; /* CPSR (Enable both IRQ and FIQ interrupts, THUMB-mode) */
    } else {
        *(--stk) = (INT32U)ARM_SYS_MODE_ARM; /* CPSR (Enable both IRQ and FIQ interrupts, ARM-mode) */
    }

    return (stk);

}

 

 

/*
*********************************************************************************************************
* 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).
*********************************************************************************************************
*/

#if (OS_CPU_HOOKS_EN > 0) && (OS_TASK_SW_HOOK_EN > 0)
void OSTaskSwHook (void)
{
#if OS_CPU_FPU_EN > 0
    void *pblk;
#endif

#if OS_CPU_FPU_EN > 0 /* Save VFP context of preempted task */
    if (OSRunning == OS_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 */
                OS_CPU_FP_Save(pblk); /* Save the VFP registers in block */
            }
        }
    }
                                                           /* Restore VFP context of new task */
    if (OSTCBHighRdy->OSTCBOpt & OS_TASK_OPT_SAVE_FP) { /* See if new task uses FP */
        pblk = OSTCBHighRdy->OSTCBExtPtr; /* Yes, Get pointer to FP storage area */
        if (pblk != (void *)0) { /* Make sure we have storage */
            OS_CPU_FP_Restore(pblk); /* Get contents of VFP registers */
        }
    }
#endif

#if OS_VIEW_MODULE > 0
    OSView_TaskSwHook();
#endif
}
#endif

原创粉丝点击