📄 core.c
字号:
:
:
Return : the task state.
Description : This function is called by task fsm, to nodity the task's state
: for FSM scheduler.
******************************************************************************/
U16 getTaskState(HTASK task)
{
if (task >= MAX_SYS_TASK)
{
OS_error ("getTaskState(): task [%d] is invalid!!!\n", task);
return OS_TASK_FAULT;
}
return g_sysTcb[task].state;
}
/******************************************************************************
Function : U32 getTaskCount(void)
Params : N/A
:
:
:
Return : the total task in system.
Description : get the total task count in the system.
:
******************************************************************************/
U32 getTaskCount()
{
U32 i;
U32 count;
count = 0;
OS_ENTER_CRITICAL();
for (i = 0; i < MAX_SYS_TASK; i++)
{
if (g_sysTcb[i].state != OS_TASK_FREE)
{
count++;
}
}
OS_LEAVE_CRITICAL();
return count;
}
/******************************************************************************
Function : HANDLE getTaskPendObj (HTASK task)
Params : task - the task
:
:
:
Return : the system object handle which the task is pending on.
Description : get the system object which the task is pending on. If the task
: is not in pending state, return NULL_HANDLE.
******************************************************************************/
HANDLE getTaskPendObj (HTASK task)
{
HANDLE hpend;
if (task >= MAX_SYS_TASK)
{
OS_error ("getTaskState(): task [%d] is invalid!!!\n", task);
return OS_TASK_FAULT;
}
switch(g_sysTcb[task].state)
{
case OS_TASK_PEND_ON_SEMA:
case OS_TASK_PEND_ON_MSGQ_S:
hpend = g_sysTcb[task].hPend;
break;
case OS_TASK_PEND_ON_MSGQ_R:
hpend = g_sysTcb[task].hMsgQ;
break;
case OS_TASK_SUSPEND:
if ((g_sysTcb[task].suspendState == OS_TASK_PEND_ON_SEMA)
||(g_sysTcb[task].suspendState == OS_TASK_PEND_ON_MSGQ_S))
{
hpend = g_sysTcb[task].hPend;
}
else if (g_sysTcb[task].suspendState == OS_TASK_PEND_ON_MSGQ_R)
{
hpend = g_sysTcb[task].hMsgQ;
}
else
{
hpend = NULL_HANDLE;
}
break;
default:
hpend = NULL_HANDLE;
break;
}
return hpend;
}
/******************************************************************************
Function : U16 getCoreEvent(HTASK task)
Params : task - the task
:
:
:
Return : the last task fsm's trig event.
Description : Get the last trig event of the special task, it is called by
: msgQSend(), msgQReceive or semTake() etc.
******************************************************************************/
U16 getCoreEvent(HTASK task)
{
U16 trigger;
if (task >= MAX_SYS_TASK)
{
OS_error ("getCoreEvent(): task [%d] is invalid!!!\n", task);
return (U16)(-1);
}
if (g_sysTcb->bWaitTimeout == OS_TRUE)
{
trigger = OS_EVENT_DELAY_TMO;
}
else
{
trigger = OS_EVENT_NORMAL;
}
return trigger;
}
/******************************************************************************
Function : void OntaskDeprived(HANDLE handle, void * p)
Params : handle - the task (which is deprived.)
: p - not used.
:
:
Return : N/A
Description : the task is deprived by another higher priority task.
:
******************************************************************************/
void OntaskDeprived(HANDLE handle, void * p)
{
HTASK task;
(void)p;
task = (HTASK)handle;
if (task >= MAX_SYS_TASK)
{
OS_error ("OntaskDeprived(): task [%d] is invalid!!!\n", task);
return;
}
switch (g_sysTcb[task].state)
{
case OS_TASK_RUNNING:
/* this function is called in OSScheduler() only, and has been in critial area,
* so don't call OS_ENTER_CRITIAL() here. */
g_sysTcb[task].state = OS_TASK_READY;
q_add (g_readyTaskQueue, (HANDLE)task, g_sysTcb[task].runningPriority);
break;
default:
break;
}
}
/******************************************************************************
Function : void OnSemGive(HANDLE handle, void * p)
Params : handle - the calling task self.
: p[0] - the samaphore.
:
:
Return : N/A
Description : the task release one of its own semaphores.
:
******************************************************************************/
void OnSemGive(HANDLE handle, void * p)
{
HTASK task;
HSEMA hsema;
HSEMA firstSema;
task = (HTASK)handle;
hsema = (HSEMA)((U32 *)p)[0];
if (task >= MAX_SYS_TASK)
{
OS_error ("OnSemUp(): task [%d] is invalid!!!\n", task);
return;
}
switch (g_sysTcb[task].state)
{
case OS_TASK_RUNNING:
if (getSemType(hsema) == OS_SEMA_MUTEX)
{
OS_ENTER_CRITICAL();
if (g_cpuIntDepth != 0)
{
fatalError("Fatal: mutex semaphore can't used in interrupt!!!\n");
}
/* only mutex samphore should execute these codes. */
if (q_head(g_sysTcb[task].hmutexSemaQ, (HANDLE *)&firstSema) == OS_SUCCESS)
{
if (firstSema != hsema)
{
OS_error("OnSemGive(): might error, you should call semTake() & semGive() as LIFO seq!!!\n");
}
q_del(g_sysTcb[task].hmutexSemaQ, hsema);
OS_LEAVE_CRITICAL();
}
else
{
OS_LEAVE_CRITICAL();
OS_error("OnSemGive(): the semaphore is not owned by the task.\n");
}
}
break;
default:
break;
}
}
/******************************************************************************
Function : void OnSemTakePending(HANDLE handle, void * p)
Params : handle - the task
: p[0] - the semaphore which the task will pend on.
: p[1] - waiting ticks for pending
:
Return : N/A
Description : the task will pending on a semaphore.
:
******************************************************************************/
void OnSemTakePending(HANDLE handle, void * p)
{
HTASK task;
U32 delayTicks;
HSEMA hsema;
task = (HTASK)handle;
hsema = ((U32*)p)[0];
delayTicks = ((U32 *)p)[1];
if (task >= MAX_SYS_TASK)
{
OS_error ("OnSemTakePending(): task [%d] is invalid!!!\n", task);
return;
}
switch (g_sysTcb[task].state)
{
case OS_TASK_RUNNING:
/* change task's state to pend on semaphore. */
OS_ENTER_CRITICAL();
g_sysTcb[task].state = OS_TASK_PEND_ON_SEMA;
g_sysTcb[task].hPend = hsema;
g_sysTcb[task].delayTicks = delayTicks;
OS_LEAVE_CRITICAL();
/* start the scheduler. */
OSScheduler();
break;
default:
OS_error("OnSemTakePending(): task [%d]'s state is invalid [%d]!!!\n", task, g_sysTcb[task].state);
break;
}
}
/******************************************************************************
Function : void OnSemTakeDone(HANDLE handle, void * p)
Params : handle - the task
: p - not used.
:
:
Return : N/A
Description : semTake() completes successfully.
:
******************************************************************************/
void OnSemTakeDone(HANDLE handle, void * p)
{
HTASK task;
HSEMA hsema;
task = (HTASK)handle;
hsema = (HSEMA)p;
switch (g_sysTcb[task].state)
{
case OS_TASK_READY:
case OS_TASK_RUNNING:
OS_ENTER_CRITICAL();
if (getSemType(hsema) == OS_SEMA_MUTEX)
{
if (q_add(g_sysTcb[task].hmutexSemaQ, hsema, 0) == OS_FAIL)
{
fatalError("OnSemTakeDone(): task [%d]'s semaphore queue overflow!!!\n", task);
}
g_sysTcb[task].bWaitTimeout = OS_FALSE;
}
OS_LEAVE_CRITICAL();
break;
case OS_TASK_PEND_ON_SEMA:
/* change task's state to ready. */
OS_ENTER_CRITICAL();
if (getSemType(hsema) == OS_SEMA_MUTEX)
{
if (q_add(g_sysTcb[task].hmutexSemaQ, hsema, 0) == OS_FAIL)
{
fatalError("OnSemTakeDone(): task [%d]'s semaphore queue overflow!!!\n", task);
}
}
g_sysTcb[task].bWaitTimeout = OS_FALSE;
g_sysTcb[task].state = OS_TASK_READY;
g_sysTcb[task].hPend = NULL_HANDLE;
g_sysTcb[task].delayTicks = 0;
q_add(g_readyTaskQueue, (HANDLE)task, g_sysTcb[task].runningPriority);
OS_LEAVE_CRITICAL();
/* start the scheduler. */
OSScheduler();
break;
case OS_TASK_SUSPEND:
/* change task's suspendState to ready. */
OS_ENTER_CRITICAL();
if (getSemType(hsema) == OS_SEMA_MUTEX)
{
if (q_add(g_sysTcb[task].hmutexSemaQ, hsema, 0) == OS_FAIL)
{
fatalError("OnSemTakeDone(): task [%d]'s semaphore queue overflow!!!\n", task);
}
}
g_sysTcb[task].bWaitTimeout = OS_FALSE;
g_sysTcb[task].suspendState = OS_TASK_READY;
g_sysTcb[task].hPend = NULL_HANDLE;
g_sysTcb[task].delayTicks = 0;
OS_LEAVE_CRITICAL();
break;
default:
OS_error("OnSemTakeDone(): task [%d]'s state is invalid [%d]!!!\n", task, g_sysTcb[task].state);
break;
}
}
/******************************************************************************
Function : void OnSemTakeTimeout(HANDLE handle, void * p)
Params : handle - the task
: p - not used.
:
:
Return : N/A
Description : semTake() timeout.
:
******************************************************************************/
void OnSemTakeTimeout(HANDLE handle, void * p)
{
HTASK task;
HSEMA hsema;
(void)p;
task = (HTASK)handle;
switch (g_sysTcb[task].state)
{
case OS_TASK_PEND_ON_SEMA:
OS_ENTER_CRITICAL();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -