📄 rm_task.bak
字号:
/*
*********************************************************************************************************
* uC/OS-II
* The Real-Time Kernel
* RM_TASK MANAGEMENT
*
* (c) Copyright 2006, CHEN JUN, FU XIU XIA
* All Rights Reserved
*
* File : RM_TASK.C
* By : CHEN JUN
*********************************************************************************************************
*/
#ifndef OS_MASTER_FILE
#include "includes.h"
#endif
/*
*********************************************************************************************************
* CREATE A RMTASK
*
* Description: This function is used to have uC/OS-II manage the execution of a rmtask. Tasks can either
* be created prior to the start of multitasking or by a running task. A task cannot be
* created by an ISR.
*
* Arguments : task is a pointer to the task's code
*
* pdata is a pointer to an optional data area which can be used to pass parameters to
* the task when the task first executes. Where the task is concerned it thinks
* it was invoked and passed the argument 'pdata' as follows:
*
* void Task (void *pdata)
* {
* for (;;) {
PeriodSet();
* Task code;
* }
* }
*
* ptos is a pointer to the task's top of stack. If the configuration constant
* OS_STK_GROWTH is set to 1, the stack is assumed to grow downward (i.e. from high
* memory to low memory). 'pstk' will thus point to the highest (valid) memory
* location of the stack. If OS_STK_GROWTH is set to 0, 'pstk' will point to the
* lowest memory location of the stack and the stack will grow with increasing
* memory locations.
*
*
* Returns : RM_NO_ERR if the function was successful.
* RMTASK_FAILED if the task priority already exist
* (each task MUST have a unique priority).
*
*********************************************************************************************************
*/
//#define rm_os_disable 0;
#if RM_TASK_EN > 0
INT8U RMTaskCreate (void (*task)(void *pd),
void *pdata,
OS_STK *ptos,
INT16U RMTASKStartTime,
INT16U RMTASKInitTime,
INT16U RMTASKPeriods,
INT16U RMTASKInitPeriodsNum
)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
OS_STK *psp;
INT8U err;
INT8U rmerr;
INT8U RM_OS_FLAG = 1;
INT8U prio = 0;
RM_TCB *prmtcb = (RM_TCB *)0;
// OSTCBPrioTbl[25] = (OS_TCB *)1 ;
rmerr = RM_TCBInit(RMTASKStartTime, RMTASKInitTime, RMTASKPeriods, RMTASKInitPeriodsNum, prmtcb);
if (rmerr == RM_NO_ERR){
psp = (OS_STK *)OSTaskStkInit(task, pdata, ptos, 0); /* Initialize the task's stack */
err = RMOS_TCBInit( psp, (OS_STK *)0, 0, 0, (void *)0, 0, RMTASKPeriods, prmtcb);
if (err == OS_NO_ERR) {
OS_ENTER_CRITICAL();
OSTaskCtr++; /* Increment the #tasks counter */
OS_EXIT_CRITICAL();
if (OSRunning == TRUE) { /* Find highest priority task if multitasking has started */
OS_Sched();
}
}
return (err);
}
return (rmerr);
}
#endif
/*
*********************************************************************************************************
* INITIALIZE TCB
*
* Description: This function is internal to uC/OS-II and is used to initialize a Task Control Block when
* a task is created (see OSTaskCreate() and OSTaskCreateExt()).
*
* Arguments : prio is the priority of the task being created
*
* ptos is a pointer to the task's top-of-stack assuming that the CPU registers
* have been placed on the stack. Note that the top-of-stack corresponds to a
* 'high' memory location is OS_STK_GROWTH is set to 1 and a 'low' memory
* location if OS_STK_GROWTH is set to 0. Note that stack growth is CPU
* specific.
*
* pbos is a pointer to the bottom of stack. A NULL pointer is passed if called by
* 'OSTaskCreate()'.
*
* id is the task's ID (0..65535)
*
* stk_size is the size of the stack (in 'stack units'). If the stack units are INT8Us
* then, 'stk_size' contains the number of bytes for the stack. If the stack
* units are INT32Us then, the stack contains '4 * stk_size' bytes. The stack
* units are established by the #define constant OS_STK which is CPU
* specific. 'stk_size' is 0 if called by 'OSTaskCreate()'.
*
* pext is a pointer to a user supplied memory area that is used to extend the task
* control block. This allows you to store the contents of floating-point
* registers, MMU registers or anything else you could find useful during a
* context switch. You can even assign a name to each task and store this name
* in this TCB extension. A NULL pointer is passed if called by OSTaskCreate().
*
* opt options as passed to 'OSTaskCreateExt()' or,
* 0 if called from 'OSTaskCreate()'.
*
* Returns : OS_NO_ERR if the call was successful
* OS_NO_MORE_TCB if there are no more free TCBs to be allocated and thus, the task cannot
* be created.
*
* Note : This function is INTERNAL to uC/OS-II and your application should not call it.
*********************************************************************************************************
*/
#if RM_TASK_EN > 0
INT8U RMOS_TCBInit (OS_STK *ptos, OS_STK *pbos, INT16U id, INT32U stk_size, void *pext, INT16U opt , INT16U RMTASKPeriods, RM_TCB *prmtcb)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
OS_TCB *ptcb;
OS_ENTER_CRITICAL();
ptcb = OSTCBFreeList; /* Get a free TCB from the free TCB list */
if (ptcb != (OS_TCB *)0) {
OSTCBFreeList = ptcb->OSTCBNext; /* Update pointer to free TCB list */
OS_EXIT_CRITICAL();
ptcb->OSTCBStkPtr = ptos; /* Load Stack pointer in TCB */
ptcb->OSTCBPrio = 0; /* Load task priority into TCB */
ptcb->OSTCBStat = OS_STAT_RDY; /* Task is ready to run */
ptcb->OSTCBDly = RMTASKPeriods; /* Task is not delayed */
#if OS_TASK_CREATE_EXT_EN > 0
ptcb->OSTCBExtPtr = pext; /* Store pointer to TCB extension */
ptcb->OSTCBStkSize = stk_size; /* Store stack size */
ptcb->OSTCBStkBottom = pbos; /* Store pointer to bottom of stack */
ptcb->OSTCBOpt = opt; /* Store task options */
ptcb->OSTCBId = id; /* Store task ID */
#else
pext = pext; /* Prevent compiler warning if not used */
stk_size = stk_size;
pbos = pbos;
opt = opt;
id = id;
#endif
#if OS_TASK_DEL_EN > 0
ptcb->OSTCBDelReq = OS_NO_ERR;
#endif
ptcb->OSTCBY = 0; /* Pre-compute X, Y, BitX and BitY */
ptcb->OSTCBBitY = 0;
ptcb->OSTCBX = 0;
ptcb->OSTCBBitX = 0;
#if OS_EVENT_EN > 0
ptcb->OSTCBEventPtr = (OS_EVENT *)0; /* Task is not pending on an event */
#endif
#if (OS_VERSION >= 251) && (OS_FLAG_EN > 0) && (OS_MAX_FLAGS > 0) && (OS_TASK_DEL_EN > 0)
ptcb->OSTCBFlagNode = (OS_FLAG_NODE *)0; /* Task is not pending on an event flag */
#endif
#if (OS_MBOX_EN > 0) || ((OS_Q_EN > 0) && (OS_MAX_QS > 0))
ptcb->OSTCBMsg = (void *)0; /* No message received */
#endif
#if OS_VERSION >= 204
OSTCBInitHook(ptcb);
#endif
OSTaskCreateHook(ptcb); /* Call user defined hook */
//按周期顺序插入队列中
OS_ENTER_CRITICAL();
ptcb->OSTCBNext = RMOSTCBList; /* Link into TCB chain */
ptcb->OSTCBPrev = (OS_TCB *)0;
if (RMOSTCBList != (OS_TCB *)0) {
RMOSTCBList->OSTCBPrev = ptcb;
}
RMOSTCBList = ptcb;
ptcb->OS_TO_RMTCBPtr = prmtcb;
OS_EXIT_CRITICAL();
return (OS_NO_ERR);
}
OS_EXIT_CRITICAL();
return (OS_NO_MORE_TCB);
}
#endif
/*
*********************************************************************************************************
* INITIALIZE RMTCB
*
* Description: This function is used to have uC/OS-II manage the execution of a rmtask. Tasks can either
* be created prior to the start of multitasking or by a running task. A task cannot be
* created by an ISR.
*
*
*********************************************************************************************************
*/
#if RM_TASK_EN > 0
INT8U RM_TCBInit ( INT16U RMTASKStartTime,
INT16U RMTASKInitTime,
INT16U RMTASKPeriods,
INT16U RMTASKInitPeriodsNum,
RM_TCB *prmtcb
)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
RM_TCB *ptcb;
OS_ENTER_CRITICAL();
ptcb = RMTCBFreeList; /* Get a free TCB from the free TCB list */
if (ptcb != (RM_TCB *)0) {
RMTCBFreeList = ptcb->RMTCBNext; /* Update pointer to free TCB list */
OS_EXIT_CRITICAL();
ptcb->RMTCBStartTime = RMTASKStartTime; //第一次开始执行时间
ptcb->RMTCBInitTime = RMTASKInitTime; //初始执行时间
ptcb->RMTCBRemainTime = RMTASKInitTime;
ptcb->RMTCBPeriods = RMTASKPeriods; /*任务周期*/
ptcb->RMTCBInitPeriodsNum = RMTASKInitPeriodsNum; //初始化周期数
ptcb->RMTCBRemainPeriodsNum = RMTASKInitPeriodsNum;
prmtcb = ptcb;
OS_EXIT_CRITICAL();
return (RM_NO_ERR);
}
OS_EXIT_CRITICAL();
return (RM_NO_MORE_TCB);
}
#endif
void prio()
{
OS_TCB *ptcb;
INT8U prio;
ptcb = RMOSTCBList;
OSTCBPrioTbl[25] = ptcb;
prio = 25;
OS_ENTER_CRITICAL();
ptcb->OSTCBY = prio >> 3;
ptcb->OSTCBBitY = OSMapTbl[ptcb->OSTCBY];
ptcb->OSTCBX = prio & 0x07;
ptcb->OSTCBBitX = OSMapTbl[ptcb->OSTCBX];
ptcb->OSTCBPrio = prio;
OSRdyGrp |= ptcb->OSTCBBitY; /* Make task ready to run */
OSRdyTbl[ptcb->OSTCBY] |= ptcb->OSTCBBitX;
OS_EXIT_CRITICAL();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -