?? emos_task.c
字號:
/****************************************************************************
*
* (c) Copyright 2001,2008, EMB system, All Rights Reserved.
* THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF EMB SYSTEM, INC.
* The copyright notice above does not evidence any actual or intended
* publication of such source code.
*
* Subsystem: EMOS
* File: emos_task.c
* Author: zenf zhao
* Description: EMOS task implementation
*
****************************************************************************/
/*#include "emos_inc.h"
#include "emos_cfg.h"*/
#include "emos_core.h"
static void emosDummy (void);
static void emosDummy (void)
{
;
}
/**********************************************************************************************************
* CHANGE PRIORITY OF A TASK
*
* Description: This function allows you to change the priority of a task dynamically. Note that the new
* priority MUST be available.
* Arguments : oldp is the old priority
* newp is the new priority
* Returns : EMOS_NO_ERR is the call was successful
* EMOS_PRIO_INVALID if the priority you specify is higher that the maximum allowed
* (i.e. >= EMOS_LOWEST_PRIO)
* EMOS_PRIO_EXIST if the new priority already exist.
* EMOS_PRIO_ERR there is no task with the specified OLD priority (i.e. the OLD task does
* not exist.
**********************************************************************************************************/
#if EMOS_TASK_CHANGE_PRIO_EN
uint8 emosTaskChangePrio (uint8 oldprio, uint8 newprio)
{
EMOS_TCB_T* ptcb;
EMOS_EVENT_T* pevent;
uint8 x;
uint8 y;
uint8 bitx;
uint8 bity;
if ((oldprio >= EMOS_LOWEST_PRIO && oldprio != EMOS_PRIO_SELF)||newprio >= EMOS_LOWEST_PRIO)
{
return (EMOS_PRIO_INVALID);
}
EMOS_ENTER_CRITICAL();
if (gEmosTCBPrioTbl[newprio] != (EMOS_TCB_T *)0)
{
/* New priority must not already exist */
EMOS_EXIT_CRITICAL();
return (EMOS_PRIO_EXIST);
}
else
{
gEmosTCBPrioTbl[newprio] = (EMOS_TCB_T *)1; /* Reserve the entry to prevent others */
EMOS_EXIT_CRITICAL();
y = newprio >> 3; /* Precompute to reduce INT. latency */
bity = gEmosMapTbl[y];
x = newprio & 0x07;
bitx = gEmosMapTbl[x];
EMOS_ENTER_CRITICAL();
if (oldprio == EMOS_PRIO_SELF)
{
/* See if changing self */
oldprio = gEmosTCBCur->osTCBPrio; /* Yes, get priority */
}
if ((ptcb = gEmosTCBPrioTbl[oldprio]) != (EMOS_TCB_T *)0)
{
/* Task to change must exist */
gEmosTCBPrioTbl[oldprio] = (EMOS_TCB_T *)0; /* Remove TCB from old priority */
if (gEmosRdyTbl[ptcb->osTCBY] & ptcb->osTCBBitX)
{
/* If task is ready make it not ready, &= operator and the MaskBit and set the value*/
if ((gEmosRdyTbl[ptcb->osTCBY] &= ~ptcb->osTCBBitX) == 0)
{
gEmosRdyGrp &= ~ptcb->osTCBBitY;
}
gEmosRdyGrp |= bity; /* Make new priority ready to run */
gEmosRdyTbl[y] |= bitx;
}
else
{
if ((pevent = ptcb->osTCBEventPtr) != (EMOS_EVENT_T*)0)
{
/* Remove from event wait list */
if ((pevent->osEventTbl[ptcb->osTCBY] &= ~ptcb->osTCBBitX) == 0)
{
pevent->osEventGrp &= ~ptcb->osTCBBitY;
}
pevent->osEventGrp |= bity; /* Add new priority to wait list */
pevent->osEventTbl[y] |= bitx;
}
}
gEmosTCBPrioTbl[newprio] = ptcb; /* Place pointer to TCB @ new priority */
ptcb->osTCBPrio = newprio; /* Set new task priority */
ptcb->osTCBY = y;
ptcb->osTCBX = x;
ptcb->osTCBBitY = bity;
ptcb->osTCBBitX = bitx;
EMOS_EXIT_CRITICAL();
emosSched(); /* Run highest priority task ready */
return (EMOS_NO_ERR);
}
/*old ptcb is zero, task old does not exist*/
else
{
gEmosTCBPrioTbl[newprio] = (EMOS_TCB_T *)0; /* Release the reserved prio. */
EMOS_EXIT_CRITICAL();
return (EMOS_PRIO_ERR); /* Task to change didn't exist */
}
}/*end of the NULL=gEmosTCBPrioTbl[newprio]*/
return (EMOS_NO_ERR);
}
#endif
/**********************************************************************************************************
* CREATE A TASK
*
* Description: This function is used to have uC/OS-II manage the execution of a task. 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 (;;) {
* Task code;
* }
* }
* ptos is a pointer to the task's top of stack. If the configuration constant
* EMOS_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 EMOS_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.
* prio is the task's priority. A unique priority MUST be assigned to each task and the
* lower the number, the higher the priority.
* Returns : EMOS_NO_ERR if the function was successful.
* EMOS_PRIO_EXIT if the task priority already exist
* (each task MUST have a unique priority).
* EMOS_PRIO_INVALID if the priority you specify is higher that the maximum allowed
* (i.e. >= EMOS_LOWEST_PRIO)
**********************************************************************************************************/
#if EMOS_TASK_CREATE_EN
uint8 emosTaskCreate (void (*task)(void *pd), void *pdata, EMOS_STK *ptos, uint8 prio)
{
void* psp = NULL;
uint8 err;
if (prio > EMOS_LOWEST_PRIO)
{
/* Make sure priority is within allowable range */
return (EMOS_PRIO_INVALID);
}
EMOS_ENTER_CRITICAL();
if (gEmosTCBPrioTbl[prio] == (EMOS_TCB_T *)0)
{
/* Make sure task doesn't already exist at this priority */
gEmosTCBPrioTbl[prio] = (EMOS_TCB_T *)1; /* Reserve the priority to prevent others from doing ... */
/* ... the same thing until task is created. */
EMOS_EXIT_CRITICAL();
psp = (void *)emosTaskStkInit(task, pdata, ptos, 0); /* Initialize the task's stack */
err = emosTCBInit(prio, (EMOS_STK *)psp, (EMOS_STK *)0, 0, 0, (void *)0, 0);
if (err == EMOS_NO_ERR)
{
EMOS_ENTER_CRITICAL();
gEmosTaskCtr++; /* Increment the #tasks counter */
emosTaskCreateHook(gEmosTCBPrioTbl[prio]); /* Call user defined hook */
EMOS_EXIT_CRITICAL();
if (gEmosRunning)
{
/* Find highest priority task if multitasking has started */
emosSched();
}
}
else
{
EMOS_ENTER_CRITICAL();
gEmosTCBPrioTbl[prio] = (EMOS_TCB_T *)0;/* Make this priority available to others */
EMOS_EXIT_CRITICAL();
}
return (err);
}
else
{
EMOS_EXIT_CRITICAL();
return (EMOS_PRIO_EXIST);
}
}
#endif
/**********************************************************************************************************
* CREATE A TASK (Extended Version)
* Description: This function is used to have uC/OS-II manage the execution of a task. Tasks can either
* be created prior to the start of multitasking or by a running task. A task cannot be
* created by an ISR. This function is similar to OSTaskCreate() except that it allows
* additional information about a task to be specified.
* 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 (;;) {
* Task code;
* }
* }
* ptos is a pointer to the task's top of stack. If the configuration constant
* EMOS_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 EMOS_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. 'pstk' MUST point to a valid 'free' data item.
* prio is the task's priority. A unique priority MUST be assigned to each task and the
* lower the number, the higher the priority.
* id is the task's ID (0..65535)
* pbos is a pointer to the task's bottom of stack. If the configuration constant
* EMOS_STK_GROWTH is set to 1, the stack is assumed to grow downward (i.e. from high
* memory to low memory). 'pbos' will thus point to the LOWEST (valid) memory
* location of the stack. If EMOS_STK_GROWTH is set to 0, 'pbos' will point to the
* HIGHEST memory location of the stack and the stack will grow with increasing
* memory locations. 'pbos' MUST point to a valid 'free' data item.
*
* stk_size is the size of the stack in number of elements. If EMOS_STK is set to uint8,
* 'stk_size' corresponds to the number of bytes available. If EMOS_STK is set to
* INT16U, 'stk_size' contains the number of 16-bit entries available. Finally, if
* EMOS_STK is set to INT32U, 'stk_size' contains the number of 32-bit entries
* available on the stack.
* pext is a pointer to a user supplied memory location which is used as a TCB extension.
* For example, this user memory can hold the contents of floating-point registers
* during a context switch, the time each task takes to execute, the number of times
* the task has been switched-in, etc.
* opt contains additional information (or options) about the behavior of the task. The
* LOWER 8-bits are reserved by uC/OS-II while the upper 8 bits can be application
* specific. See EMOS_TASK_OPT_??? in uCOS-II.H.
* Returns : EMOS_NO_ERR if the function was successful.
* EMOS_PRIO_EXIT if the task priority already exist
* (each task MUST have a unique priority).
* EMOS_PRIO_INVALID if the priority you specify is higher that the maximum allowed
* (i.e. > EMOS_LOWEST_PRIO)
*********************************************************************************************************/
#if EMOS_TASK_CREATE_EXT_EN
uint8 emosTaskCreateExt (void (*task)(void *pd),
void *pdata,
EMOS_STK *ptos,
uint8 prio,
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -