📄 root.c
字号:
/******************************************************************************
Copyright (c) 2006 by RockOS.
All rights reserved.
This software is supported by Rock Software Workroom.
Any bugs please contact the author with e-mail or QQ:
E-mail : baobaoba520@yahoo.com.cn
QQ : 59681888
*******************************************************************************
File name : root.c
Description : core init functions and system task entries for RockOS.
:
Auther : sunxinqiu
History :
2006-3-15 first release.
******************************************************************************/
#include "os.h"
/******************************************************************************
Global var : U32 g_cpuIntDepth;
Description : to indicate the depth of CPU interrupt.
:
******************************************************************************/
volatile U32 g_cpuIntDepth;
/******************************************************************************
Global var : U32 g_OSRunning;
Description : This global variable is used to indicate the OS's state, such as
: initializing, or scheduling.
******************************************************************************/
U32 g_OSRunning;
/******************************************************************************
Global var : U32 g_CPUPerformanceBase;
Description : The CPU's performance base value tested during the system
: starting up.
******************************************************************************/
U32 g_CPUPerformanceBase;
/******************************************************************************
Global var : volatile U32 g_IdleCount;
Description : to caculate the CPU's idle pecent.
:
******************************************************************************/
volatile U32 g_IdleCount;
/******************************************************************************
Global var : volatile U32 g_OSTickCount;
Description : used to count the ticks during a second.
:
******************************************************************************/
volatile U32 g_OSTickCount;
/******************************************************************************
Global var : HTASK g_tickTaskId;
: HTASK g_idleTaskId;
: HTASK g_shellTaskId;
Description : the system tasks' id.
:
******************************************************************************/
HTASK g_tickTaskId;
HTASK g_idleTaskId;
#if MAX_SHELL_CMD != 0
HTASK g_shellTaskId;
#endif
STATUS app_entry(void);
/******************************************************************************
Function : void OSStart(void)
Params : N/A
:
:
:
Return : never return.
Description : starting RockOS, it will bring the system to muti-task mode.
:
******************************************************************************/
void root()
{
TASK_INFO taskInfo;
/* no interrupt is entered during system starting up. */
g_cpuIntDepth = 0;
/* the system is starting up. */
g_OSRunning = OS_PHASE_INIT;
g_IdleCount = 0;
g_OSTickCount = 0;
/* init all of system resources. */
if (mem_init() != OS_SUCCESS)
{
fatalError("root(): mem init fail!!!\n");
}
if (queue_init() != OS_SUCCESS)
{
fatalError("root(): queue init fail!!!\n");
}
if (msg_init() != OS_SUCCESS)
{
fatalError("root(): msg init fail!!!\n");
}
if (msgQ_init() != OS_SUCCESS)
{
fatalError("root(): msg queue init fail!!!\n");
}
if (sem_init() != OS_SUCCESS)
{
fatalError("root(): semaphore init fail!!!\n");
}
if (fsm_init() != OS_SUCCESS)
{
fatalError("root(): fsm init fail!!!\n");
}
if (core_init() != OS_SUCCESS)
{
fatalError("root(): core init fail!!!\n");
}
#if MAX_SHELL_CMD != 0
if (shell_init() != OS_SUCCESS)
{
fatalError("root(): shell init fail!!!\n");
}
#endif
/* create system semaphores. */
g_stdoutSem = semCreate(OS_SEMA_MUTEX, 1, "stdout sem");
g_stderrSem = semCreate(OS_SEMA_MUTEX, 1, "stderr sem");
/* create tick task. */
taskInfo.priority = 0;
strcpy (&taskInfo.name[0], "tTick");
taskInfo.taskEntry = tTickEntry;
taskInfo.pData = NULL;
taskInfo.msgQSize = TICKS_PER_SECOND * 3;
taskInfo.semaQSize = DEFAULT_MUTEX_Q_SIZE;
taskInfo.stackSize = TICK_TASK_STACK_SIZE;
taskInfo.option = OPT_TASK_DEFAULT;
g_tickTaskId = taskCreate(&taskInfo);
OS_printf("tTick task created, id is [%d]!!\n", g_tickTaskId);
/* create Idle task. */
taskInfo.priority = LOWEST_TASK_PRIO;
strcpy (&taskInfo.name[0], "tIdle");
taskInfo.taskEntry = tIdleEntry;
taskInfo.pData = NULL;
taskInfo.msgQSize = 0;
taskInfo.semaQSize = 0;
taskInfo.stackSize = IDLE_TASK_STACK_SIZE;
taskInfo.option = OPT_TASK_DEFAULT;
g_idleTaskId = taskCreate(&taskInfo);
OS_printf("tIdle task created, id is [%d]!!\n", g_idleTaskId);
#if MAX_SHELL_CMD != 0
taskInfo.priority = LOWEST_TASK_PRIO - 1;
strcpy (&taskInfo.name[0], "tShell");
taskInfo.taskEntry = tShellEntry;
taskInfo.pData = NULL;
taskInfo.msgQSize = MAX_CMD_LINE_LEN + 1;
taskInfo.semaQSize = DEFAULT_MUTEX_Q_SIZE;
taskInfo.stackSize = SHELL_TASK_STACK_SIZE;
taskInfo.option = OPT_TASK_LOCK;
g_shellTaskId = taskCreate(&taskInfo);
OS_printf("tShell task created, id is [%d]!!\n", g_shellTaskId);
#endif
app_entry();
/* now start the scheduler. */
OSStartScheduler();
fatalError("root(): start scheduler error(fatal error), the system will restart!!!\n");
}
/******************************************************************************
Function : void OSOnTick()
Params : N/A
:
:
:
Return : N/A
Description : This is the tick isr hook for RockOS.
:
******************************************************************************/
void OSOnTick()
{
HMSG hmsg;
APP_MSG * pmsg;
g_OSTickCount++;
/* send a message to tick task. */
hmsg = msgAlloc(MINI_MSG_PACK);
pmsg = (APP_MSG *)msgMap(hmsg);
if (pmsg != NULL)
{
pmsg->src = g_tickTaskId;
pmsg->dest = g_tickTaskId;
pmsg->msg = MSG_TICK_INT;
pmsg->length = sizeof(U32);
*((U32 *)&pmsg->data[0])=g_OSTickCount;
if (msgQSend(hmsg, g_tickTaskId, OS_NO_WAIT) == OS_SUCCESS)
{
g_OSTickCount = 0;
}
else
{
msgFree(hmsg);
}
}
}
/******************************************************************************
Function : void tTickEntry(void * p)
Params : p - not used.
:
:
:
Return : N/A
Description : the tick task's entry point.
:
******************************************************************************/
void tTickEntry(void * p)
{
HMSG hmsg;
APP_MSG * pmsg;
U32 tickCount;
U32 tickPeriod;
(void)p;
tickCount = 0;
tickPeriod = 0;
while (1)
{
if (msgQReceive(&hmsg, OS_WAIT_FOR_EVER) != OS_SUCCESS)
{
continue;
}
clear_watchdog();
pmsg = (APP_MSG *)msgMap(hmsg);
tickCount = *((U32 *)&pmsg->data[0]);
OSTickCore(tickCount);
tickPeriod++;
if (tickPeriod == TICKS_PER_SECOND)
{
tickPeriod = 0;
g_IdleCount = 0;
OSRunInd();
msgCheck();
semCheck();
#if MAX_SHELL_CMD > 0
if (getTaskState(g_shellTaskId) == OS_TASK_FAULT)
{
OS_printf("Shell task restart.\n");
taskRestart(g_shellTaskId);
}
#endif
}
msgFree(hmsg);
}
}
/******************************************************************************
Function : void tIdleEntry(void * p)
Params : p - not used.
:
:
:
Return : N/A
Description : the Idle task's entry point.
:
******************************************************************************/
void tIdleEntry(void * p)
{
(void)p;
while (g_OSTickCount < TICKS_PER_SECOND * 3)
{
g_IdleCount++;
}
if (g_OSRunning != OS_PHASE_INIT)
{
fatalError("tIdleEntry(): idle task terminate, system fault now!!!\n");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -