📄 os_core.c
字号:
/*
*********************************************************************************************************
* START MULTITASKING
*
* Description: This function is used to start the multitasking process which lets uC/OS-II manages the
* task that you have created. Before you can call OSStart(), you MUST have called OSInit()
* and you MUST have created at least one task.
*
* Arguments : none
*
* Returns : none
*
* Note : OSStartHighRdy() MUST:
* a) Call OSTaskSwHook() then,
* b) Set OSRunning to OS_TRUE.
* c) Load the context of the task pointed to by OSTCBHighRdy.
* d_ Execute the task.
*********************************************************************************************************
*/
void OSStart (void) reentrant
{ INT8U x ,y;
if (OSRunning == FALSE) {
//OS_SchedNew(); /* Find highest priority's task priority number */
y = OSUnMapTbl[OSRdyGrp]; /* Find highest priority's task priority number */
x = OSUnMapTbl[OSRdyTbl[y]];
OSPrioHighRdy = (INT8U)((y << 3) + x);
OSPrioCur = OSPrioHighRdy;
OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy]; /* Point to highest priority task ready to run */
OSTCBCur = OSTCBHighRdy;
OSStartHighRdy(); /* Execute target specific code to start task */
}
}
/*$PAGE*/
/*
*********************************************************************************************************
* STATISTICS INITIALIZATION
*
* Description: This function is called by your application to establish CPU usage by first determining
* how high a 32-bit counter would count to in 1 second if no other tasks were to execute
* during that time. CPU usage is then determined by a low priority task which keeps track
* of this 32-bit counter every second but this time, with other tasks running. CPU usage is
* determined by:
*
* OSIdleCtr
* CPU Usage (%) = 100 * (1 - ------------)
* OSIdleCtrMax
*
* Arguments : none
*
* Returns : none
*********************************************************************************************************
*/
#if OS_TASK_STAT_EN > 0
void OSStatInit (void)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0;
#endif
OSTimeDly(2); /* Synchronize with clock tick */
OS_ENTER_CRITICAL();
OSIdleCtr = 0L; /* Clear idle counter */
OS_EXIT_CRITICAL();
OSTimeDly(OS_TICKS_PER_SEC / 10); /* Determine MAX. idle counter value for 1/10 second */
OS_ENTER_CRITICAL();
OSIdleCtrMax = OSIdleCtr; /* Store maximum idle counter count in 1/10 second */
OSStatRdy = OS_TRUE;
OS_EXIT_CRITICAL();
}
#endif
/*$PAGE*/
/*
*********************************************************************************************************
* PROCESS SYSTEM TICK
*
* Description: This function is used to signal to uC/OS-II the occurrence of a 'system tick' (also known
* as a 'clock tick'). This function should be called by the ticker ISR but, can also be
* called by a high priority task.
*
* Arguments : none
*
* Returns : none
*********************************************************************************************************
*/
void OSTimeTick (void) reentrant
{
OS_TCB *ptcb;
ptcb = OSTCBList; //point to the TCB list
while(ptcb->OSTCBPrio!=OS_IDLE_PRIO) //GO through the tcb list
{
OS_ENTER_CRITICAL( );
if (ptcb->OSTCBDly!=0)
{
if(--ptcb->OSTCBDly==0)
{
if((ptcb->OSTCBStat & OS_STAT_SUSPEND) == OS_STAT_RDY)
{
OSRdyGrp |=ptcb->OSTCBBitY;
OSRdyTbl[ptcb->OSTCBY]|=ptcb->OSTCBBitX;
}
else
{ ptcb->OSTCBDly =1; //如果任务延时已经到了
} //但任务还在被挂起,延时置1
}
}
ptcb = ptcb->OSTCBNext;
OS_ENTER_CRITICAL( );
}//END WHILE
OS_ENTER_CRITICAL( );
OSTime++;
OS_EXIT_CRITICAL( );
}
/*$PAGE*/
/*
*********************************************************************************************************
* DUMMY FUNCTION
*
* Description: This function doesn't do anything. It is called by OSTaskDel().
*
* Arguments : none
*
* Returns : none
*********************************************************************************************************
*/
#if OS_TASK_DEL_EN > 0
void OS_Dummy (void)
{
}
#endif
/*$PAGE*/
/*
*********************************************************************************************************
* INITIALIZATION
* CREATING THE IDLE TASK
*
* Description: This function creates the Idle Task.
*
* Arguments : none
*
* Returns : none
*********************************************************************************************************
*/
void OS_InitTaskIdle(void) reentrant
{
#if OS_STK_GROWTH == 1
(void)0STaskCreate(OS_TaskIdle,
(void*)0,
&OSTaskIdleStk[OS_TASK_IDLE_STK_SIZE-1],
OS_IDLE_PRIO);
#else
(void)OSTaskCreate(OS_TaskIdle,
(void*)0,
&OSTaskIdleStk[0],
OS_IDLE_PRIO);
#endif
}
/*$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) reentrant
{ INT8U y;
OS_ENTER_CRITICAL();
if (OSIntNesting == 0) { /* Schedule only if all ISRs done and ... */
if (OSLockNesting == 0) { /* ... scheduler is not locked */
//OS_SchedNew();
y = OSUnMapTbl[OSRdyGrp]; /* Get pointer to highest priority task ready to run */
OSPrioHighRdy = (INT8U)((y << 3) + OSUnMapTbl[OSRdyTbl[y]]);
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
OSCtxSwCtr++; /* Increment context switch counter */
OS_TASK_SW(); /* Perform a context switch */
}
}
}
OS_EXIT_CRITICAL();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -