一、LiteOS-M 初始化内核
在LiteOS-M应用程序中,系统初始化如下:
/**
* @brief This is the ohos entry, and you could call this in your main funciton after the
* necessary hardware has been initialized.
*/
void OHOS_Boot(void)
{
UINT32 ret;
ret = LOS_KernelInit(); //初始化内核
if (ret == LOS_OK) {
...
LOS_Start(); //启动内核任务调度
}
return; // and should never come here
}
二、LOS_KernelInit代码分析
/*****************************************************************************
Function : LOS_KernelInit
Description : System kernel initialization function, configure all system modules
Input : None
Output : None
Return : LOS_OK on success or error code on failure
*****************************************************************************/
LITE_OS_SEC_TEXT_INIT UINT32 LOS_KernelInit(VOID)
{
UINT32 ret;
PRINTK("entering kernel init...\n");
#if (LOSCFG_BACKTRACE_TYPE != 0) //LOSCFG_XXX 宏定义为make menuconfig生成的宏
OsBackTraceInit(); //函数目前用于初始化内存泄漏检查钩子,栈回溯功能
#endif
#ifdef LOSCFG_KERNEL_LMS
OsLmsInit(); //初始化 LMS全称为Lite Memory Sanitizer,是一种实时检测内存操作合法性的调测工具
#endif
ret = OsMemSystemInit(); //系统堆内存初始化
if (ret != LOS_OK) {
PRINT_ERR("OsMemSystemInit error %d\n", ret);
return ret;
}
ArchInit(); //CPU中断向量表初始化,中断接管
ret = OsTickTimerInit(); //系统Tick时钟初始化
if (ret != LOS_OK) {
PRINT_ERR("OsTickTimerInit error! 0x%x\n", ret);
return ret;
}
ret = OsTaskInit(); //系统任务初始化,申请控制块内存,初始化就绪链表,自动删除链表
if (ret != LOS_OK) {
PRINT_ERR("OsTaskInit error\n");
return ret;
}
#if (LOSCFG_BASE_CORE_TSK_MONITOR == 1)
OsTaskMonInit(); //任务监控初始化
#endif
#if (LOSCFG_BASE_CORE_CPUP == 1)
ret = OsCpupInit(); //初始化CPU占空比监控
if (ret != LOS_OK) {
PRINT_ERR("OsCpupInit error\n");
return ret;
}
#endif
#if (LOSCFG_BASE_IPC_SEM == 1)
ret = OsSemInit(); //信号量初始化
if (ret != LOS_OK) {
return ret;
}
#endif
#if (LOSCFG_BASE_IPC_MUX == 1)
ret = OsMuxInit(); //互斥锁初始化
if (ret != LOS_OK) {
return ret;
}
#endif
#if (LOSCFG_BASE_IPC_QUEUE == 1)
ret = OsQueueInit(); //消息队列初始化
if (ret != LOS_OK) {
PRINT_ERR("OsQueueInit error\n");
return ret;
}
#endif
#if (LOSCFG_BASE_CORE_SWTMR == 1)
ret = OsSwtmrInit(); //软件定时器初始化
if (ret != LOS_OK) {
PRINT_ERR("OsSwtmrInit error\n");
return ret;
}
#endif
#if (LOSCFG_CPUP_INCLUDE_IRQ == 1)
ret = OsCpupDaemonInit(); //CPU占空比守护进程
if (ret != LOS_OK) {
PRINT_ERR("OsCpupDaemonInit error\n");
return ret;
}
#endif
#if (LOSCFG_FS_VFS == 1)
ret = OsVfsInit(); //文件系统初始化
if (ret != LOS_OK) {
PRINT_ERR("OsVfsInit error\n");
return ret;
}
#endif
ret = OsIdleTaskCreate(); //创建idle进程
if (ret != LOS_OK) {
return ret;
}
#if (LOSCFG_KERNEL_TRACE == 1)
ret = OsTraceInit(); //系统跟踪初始化
if (ret != LOS_OK) {
PRINT_ERR("OsTraceInit error\n");
return ret;
}
#endif
#if (LOSCFG_KERNEL_PM == 1)
ret = OsPmInit(); //电源控制初始化
if (ret != LOS_OK) {
PRINT_ERR("Pm init failed!\n");
return ret;
}
#endif
#if (LOSCFG_KERNEL_LMK == 1)
OsLmkInit(); //低内存自动Kill框架
#endif
#if (LOSCFG_PLATFORM_EXC == 1)
OsExcMsgDumpInit(); //信号获取初始化
#endif
#if (LOSCFG_DYNLINK == 1)
ret = LOS_DynlinkInit(); //动态链接
if (ret != LOS_OK) {
return ret;
}
#endif
#if (LOSCFG_POSIX_PIPE_API == 1)
ret = OsPipeInit(); //Pipe初始化
if (ret != LOS_OK) {
PRINT_ERR("Pipe init failed!\n");
return ret;
}
#endif
#if (LOSCFG_KERNEL_SIGNAL == 1)
ret = OsSignalInit(); //信号初始化
if (ret != LOS_OK) {
PRINT_ERR("Signal init failed!\n");
return ret;
}
#endif
#if (LOSCFG_SECURE == 1)
OsSyscallHandleInit(); //系统调用
LOS_BoxStart(); //创建BOX用户任务
#endif
return LOS_OK;
}
LOS_KernelInit函数中初始化的系统模块,多数可以根据内核配置进行增减,在lite-m目录下使用make menuconfig进行配置。
三、LOS_Start代码解析
LITE_OS_SEC_TEXT_INIT UINT32 LOS_Start(VOID)
{
return ArchStartSchedule(); //启动任务调度
}
由于系统任务管理、调度算法,会涉及芯片平台的中断开关、任务上下文切换等,因此,下一篇文章先分析SOC Cortex-m4启动代码适配。