📄 os_core.c
字号:
void OS_EventTOAbort(OS_EVENT *pevent)
{
INT8U y;
y = OSTCBCur->OSTCBY;
pevent->OSEventTbl[y] &= ~OSTCBCur->OSTCBBitX; /* Remove task from wait list */
if (pevent->OSEventTbl[y] == 0x00)
{
pevent->OSEventGrp &= ~OSTCBCur->OSTCBBitY;
}
OSTCBCur->OSTCBStatPend = OS_STAT_PEND_OK; /* Clear pend status */
OSTCBCur->OSTCBStat = OS_STAT_RDY; /* Set status to ready */
OSTCBCur->OSTCBEventPtr = (OS_EVENT *) 0; /* No longer waiting for event */
}
#endif
/*$PAGE*/
/*
*********************************************************************************************************
* INITIALIZE EVENT CONTROL BLOCK'S WAIT LIST
*
* Description: This function is called by other uC/OS-II services to initialize the event wait list.
*
* Arguments : pevent is a pointer to the event control block allocated to the event.
*
* Returns : none
*
* Note : This function is INTERNAL to uC/OS-II and your application should not call it.
*********************************************************************************************************
*/
#if OS_EVENT_EN
void OS_EventWaitListInit(OS_EVENT *pevent)
{
#if OS_LOWEST_PRIO <= 63
INT8U *ptbl;
#else
INT16U *ptbl;
#endif
INT8U i;
pevent->OSEventGrp = 0; /* No task waiting on event */
ptbl = &pevent->OSEventTbl[0];
for (i = 0; i < OS_EVENT_TBL_SIZE; i++)
{
*ptbl++ = 0;
}
}
#endif
/*$PAGE*/
/*
*********************************************************************************************************
* INITIALIZATION
* INITIALIZE THE FREE LIST OF EVENT CONTROL BLOCKS
*
* Description: This function is called by OSInit() to initialize the free list of event control blocks.
*
* Arguments : none
*
* Returns : none
*********************************************************************************************************
*/
static void OS_InitEventList(void)
{
#if OS_EVENT_EN && (OS_MAX_EVENTS > 0)
#if (OS_MAX_EVENTS > 1)
INT16U i;
OS_EVENT *pevent1;
OS_EVENT *pevent2;
OS_MemClr((INT8U *) &OSEventTbl[0], sizeof(OSEventTbl)); /* Clear the event table */
pevent1 = &OSEventTbl[0];
pevent2 = &OSEventTbl[1];
for (i = 0; i < (OS_MAX_EVENTS - 1); i++)
{
/* Init. list of free EVENT control blocks */
pevent1->OSEventType = OS_EVENT_TYPE_UNUSED;
pevent1->OSEventPtr = pevent2;
#if OS_EVENT_NAME_SIZE > 1
pevent1->OSEventName[0] = '?'; /* Unknown name */
pevent1->OSEventName[1] = OS_ASCII_NUL;
#endif
pevent1++;
pevent2++;
}
pevent1->OSEventType = OS_EVENT_TYPE_UNUSED;
pevent1->OSEventPtr = (OS_EVENT *) 0;
#if OS_EVENT_NAME_SIZE > 1
pevent1->OSEventName[0] = '?';
pevent1->OSEventName[1] = OS_ASCII_NUL;
#endif
OSEventFreeList = &OSEventTbl[0];
#else
OSEventFreeList = &OSEventTbl[0]; /* Only have ONE event control block */
OSEventFreeList->OSEventType = OS_EVENT_TYPE_UNUSED;
OSEventFreeList->OSEventPtr = (OS_EVENT *) 0;
#if OS_EVENT_NAME_SIZE > 1
OSEventFreeList->OSEventName[0] = '?'; /* Unknown name */
OSEventFreeList->OSEventName[1] = OS_ASCII_NUL;
#endif
#endif
#endif
}
/*$PAGE*/
/*
*********************************************************************************************************
* INITIALIZATION
* INITIALIZE MISCELLANEOUS VARIABLES
*
* Description: This function is called by OSInit() to initialize miscellaneous variables.
*
* Arguments : none
*
* Returns : none
*********************************************************************************************************
*/
static void OS_InitMisc(void)
{
#if OS_TIME_GET_SET_EN > 0
OSTime = 0L; /* Clear the 32-bit system clock */
#endif
OSIntNesting = 0; /* Clear the interrupt nesting counter */
OSLockNesting = 0; /* Clear the scheduling lock counter */
OSTaskCtr = 0; /* Clear the number of tasks */
OSRunning = OS_FALSE; /* Indicate that multitasking not started */
OSCtxSwCtr = 0; /* Clear the context switch counter */
OSIdleCtr = 0L; /* Clear the 32-bit idle counter */
#if OS_TASK_STAT_EN > 0
OSIdleCtrRun = 0L;
OSIdleCtrMax = 0L;
OSStatRdy = OS_FALSE; /* Statistic task is not ready */
#endif
}
/*$PAGE*/
/*
*********************************************************************************************************
* INITIALIZATION
* INITIALIZE THE READY LIST
*
* Description: This function is called by OSInit() to initialize the Ready List.
*
* Arguments : none
*
* Returns : none
*********************************************************************************************************
*/
static void OS_InitRdyList(void)
{
INT8U i;
#if OS_LOWEST_PRIO <= 63
INT8U *prdytbl;
#else
INT16U *prdytbl;
#endif
OSRdyGrp = 0; /* Clear the ready list */
prdytbl = &OSRdyTbl[0];
for (i = 0; i < OS_RDY_TBL_SIZE; i++)
{
*prdytbl++ = 0;
}
OSPrioCur = 0;
OSPrioHighRdy = 0;
OSTCBHighRdy = (OS_TCB *) 0;
OSTCBCur = (OS_TCB *) 0;
}
/*$PAGE*/
/*
*********************************************************************************************************
* INITIALIZATION
* CREATING THE IDLE TASK
*
* Description: This function creates the Idle Task.
*
* Arguments : none
*
* Returns : none
*********************************************************************************************************
*/
static void OS_InitTaskIdle(void)
{
#if OS_TASK_NAME_SIZE > 7
INT8U err;
#endif
#if OS_TASK_CREATE_EXT_EN > 0
#if OS_STK_GROWTH == 1
(void) OSTaskCreateExt(OS_TaskIdle, (void *) 0, /* No arguments passed to OS_TaskIdle() */ & OSTaskIdleStk[OS_TASK_IDLE_STK_SIZE - 1], /* Set Top-Of-Stack */
OS_TASK_IDLE_PRIO, /* Lowest priority level */
OS_TASK_IDLE_ID, &OSTaskIdleStk[0], /* Set Bottom-Of-Stack */
OS_TASK_IDLE_STK_SIZE, (void *) 0, /* No TCB extension */
OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);/* Enable stack checking + clear stack */
#else
(void) OSTaskCreateExt(OS_TaskIdle, (void *) 0, /* No arguments passed to OS_TaskIdle() */ & OSTaskIdleStk[0], /* Set Top-Of-Stack */
OS_TASK_IDLE_PRIO, /* Lowest priority level */
OS_TASK_IDLE_ID, &OSTaskIdleStk[OS_TASK_IDLE_STK_SIZE - 1], /* Set Bottom-Of-Stack */
OS_TASK_IDLE_STK_SIZE, (void *) 0, /* No TCB extension */
OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);/* Enable stack checking + clear stack */
#endif
#else
#if OS_STK_GROWTH == 1
(void) OSTaskCreate(OS_TaskIdle, (void *) 0, &OSTaskIdleStk[OS_TASK_IDLE_STK_SIZE - 1], OS_TASK_IDLE_PRIO);
#else
(void) OSTaskCreate(OS_TaskIdle, (void *) 0, &OSTaskIdleStk[0], OS_TASK_IDLE_PRIO);
#endif
#endif
#if OS_TASK_NAME_SIZE > 14
OSTaskNameSet(OS_TASK_IDLE_PRIO, (INT8U *) "uC/OS-II Idle", &err);
#else
#if OS_TASK_NAME_SIZE > 7
OSTaskNameSet(OS_TASK_IDLE_PRIO, (INT8U *) "OS-Idle", &err);
#endif
#endif
}
/*$PAGE*/
/*
*********************************************************************************************************
* INITIALIZATION
* CREATING THE STATISTIC TASK
*
* Description: This function creates the Statistic Task.
*
* Arguments : none
*
* Returns : none
*********************************************************************************************************
*/
#if OS_TASK_STAT_EN > 0
static void OS_InitTaskStat(void)
{
#if OS_TASK_NAME_SIZE > 7
INT8U err;
#endif
#if OS_TASK_CREATE_EXT_EN > 0
#if OS_STK_GROWTH == 1
(void) OSTaskCreateExt(OS_TaskStat, (void *) 0, /* No args passed to OS_TaskStat()*/ & OSTaskStatStk[OS_TASK_STAT_STK_SIZE - 1], /* Set Top-Of-Stack */
OS_TASK_STAT_PRIO, /* One higher than the idle task */
OS_TASK_STAT_ID, &OSTaskStatStk[0], /* Set Bottom-Of-Stack */
OS_TASK_STAT_STK_SIZE, (void *) 0, /* No TCB extension */
OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR); /* Enable stack checking + clear */
#else
(void) OSTaskCreateExt(OS_TaskStat, (void *) 0, /* No args passed to OS_TaskStat()*/ & OSTaskStatStk[0], /* Set Top-Of-Stack */
OS_TASK_STAT_PRIO, /* One higher than the idle task */
OS_TASK_STAT_ID, &OSTaskStatStk[OS_TASK_STAT_STK_SIZE - 1], /* Set Bottom-Of-Stack */
OS_TASK_STAT_STK_SIZE, (void *) 0, /* No TCB extension */
OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR); /* Enable stack checking + clear */
#endif
#else
#if OS_STK_GROWTH == 1
(void) OSTaskCreate(OS_TaskStat, (void *) 0, /* No args passed to OS_TaskStat()*/ & OSTaskStatStk[OS_TASK_STAT_STK_SIZE - 1], /* Set Top-Of-Stack */
OS_TASK_STAT_PRIO); /* One higher than the idle task */
#else
(void) OSTaskCreate(OS_TaskStat, (void *) 0, /* No args passed to OS_TaskStat()*/ & OSTaskStatStk[0], /* Set Top-Of-Stack */
OS_TASK_STAT_PRIO); /* One higher than the idle task */
#endif
#endif
#if OS_TASK_NAME_SIZE > 14
OSTaskNameSet(OS_TASK_STAT_PRIO, (INT8U *) "uC/OS-II Stat", &err);
#else
#if OS_TASK_NAME_SIZE > 7
OSTaskNameSet(OS_TASK_STAT_PRIO, (INT8U *) "OS-Stat", &err);
#endif
#endif
}
#endif
/*$PAGE*/
/*
*********************************************************************************************************
* INITIALIZATION
* INITIALIZE THE FREE LIST OF TASK CONTROL BLOCKS
*
* Description: This function is called by OSInit() to initialize the free list of OS_TCBs.
*
* Arguments : none
*
* Returns : none
*********************************************************************************************************
*/
static void OS_InitTCBList(void)
{
INT8U i;
OS_TCB *ptcb1;
OS_TCB *ptcb2;
OS_MemClr((INT8U *) &OSTCBTbl[0], sizeof(OSTCBTbl)); /* Clear all the TCBs */
OS_MemClr((INT8U *) &OSTCBPrioTbl[0], sizeof(OSTCBPrioTbl)); /* Clear the priority table */
ptcb1 = &OSTCBTbl[0];
ptcb2 = &OSTCBTbl[1];
for (i = 0; i < (OS_MAX_TASKS + OS_N_SYS_TASKS - 1); i++)
{
/* Init. list of free TCBs */
ptcb1->OSTCBNext = ptcb2;
#if OS_TASK_NAME_SIZE > 1
ptcb1->OSTCBTaskName[0] = '?'; /* Unknown name */
ptcb1->OSTCBTaskName[1] = OS_ASCII_NUL;
#endif
ptcb1++;
ptcb2++;
}
ptcb1->OSTCBNext = (OS_TCB *) 0; /* Last OS_TCB */
#if OS_TASK_NAME_SIZE > 1
ptcb1->OSTCBTaskName[0] = '?'; /* Unknown name */
ptcb1->OSTCBTaskName[1] = OS_ASCII_NUL;
#endif
OSTCBList = (OS_TCB *) 0; /* TCB lists initializations */
OSTCBFreeList = &OSTCBTbl[0];
}
/*$PAGE*/
/*
*********************************************************************************************************
* CLEAR A SECTION OF MEMORY
*
* Description: This function is called by other uC/OS-II services to clear a contiguous block of RAM.
*
* Arguments : pdest is the start of the RAM to clear (i.e. write 0x00 to)
*
* size is the number of bytes to clear.
*
* Returns : none
*
* Notes : 1) This function is INTERNAL to uC/OS-II and your application should not call it.
* 2) Note that we can only clear up to 64K bytes of RAM. This is not an issue because none
* of the uses of this function gets close to this limit.
* 3) The clear is done one byte at a time since this will work on any processor irrespective
* of the alignment of the destination.
*********************************************************************************************************
*/
void OS_MemClr(INT8U *pdest, INT16U size)
{
while (size > 0)
{
*pdest++ = (INT8U) 0;
size--;
}
}
/*$PAGE*/
/*
*********************************************************************************************************
* COPY A BLOCK OF MEMORY
*
* Description: This function is called by other uC/OS-II services to copy a block of memory from one
* location to another.
*
* Arguments : pdest is a pointer to the 'destination' memory block
*
* psrc is a pointer to the 'source' memory block
*
* size is the number of bytes to copy.
*
* Returns : none
*
* Notes : 1) This function is INTERNAL to uC/OS-II and your application should not call it. There is
* no provision to handle overlapping memory copy. However, that's not a problem since this
* is not a situation that will happen.
* 2) Note that we can only copy up to 64K bytes of RAM
* 3) The copy is done one byte at a time since this will work on any processor irrespective
* of the alignment of the source and destination.
*********************************************************************************************************
*/
void OS_MemCopy(INT8U *pdest, INT8U *psrc, INT16U size)
{
while (size > 0)
{
*pdest++ = *psrc++;
size--;
}
}
/*$PAGE*/
/*
*********************************************************************************************************
* SCHEDULER
*
* Description: This function is called by other uC/OS-II services to determine whether a new, high
* priority task has been made ready to run. This function is invoked by TASK level code
* and is not used to reschedule tasks from ISRs (see OSIntExit() for ISR rescheduling).
*
* Arguments : none
*
* Returns : none
*
* Notes : 1) This function is INTERNAL to uC/OS-II and your application should not call it.
* 2) Rescheduling is prevented when the scheduler is locked (see OS_SchedLock())
*********************************************************************************************************
*/
void OS_Sched(void)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0;
#endif
OS_ENTER_CRITICAL();
if (OSIntNesting == 0)
{
/* Schedule only if all ISRs done and ... */
if (OSLockNesting == 0)
{
/* ... scheduler is not locked */
OS_SchedNew();
if (OSPrioHighRdy != OSPrioCur)
{
/* No Ctx Sw if current task is highest rdy */
OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy];
#if OS_TASK_PROFILE_EN > 0
OSTCBHighRdy->OSTCBCtxSwCtr++; /* Inc. # of context switches to this task */
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -