MTK平台LK中的APP_START(LK app service)介绍

快速链接: .

1、LK启动流程

先回顾一下LK的启动流程: _start(crt0.S) --> reset --> .Lstack_setup --> kmain() …bootstrap2() -->apps_init()

在kmain()中创建了bootstrap2线程:

thread_t *thread_bs2 = thread_create("bootstrap2", &bootstrap2, NULL,
		DEFAULT_PRIORITY, DEFAULT_STACK_SIZE);

bootstrap2()–>apps_init()

2、APP_START宏的使用

在LK代码中,有如下APP_START的定义

APP_START(aboot)
	.init = aboot_init,
APP_END

APP_START(mt_boot)
	.init = mt_boot_init,
APP_END

APP_START(shell)
APP_START(tests)
APP_START(clocktests)
APP_START(stringtests)
APP_START(pcitests)

3、APP_START宏的实现原型

其实就是在section段中定义的数组,编译的时候将这些service加入到了section段中(相当于编译时注册service)

#define APP_START(appname) struct app_descriptor _app_##appname __SECTION(".apps") = {
            
      .name = #appname,
#define APP_END };

4、APP_START定义的这些service的启动

在LK启动的时候,调用到apps_init(),在该函数中宏,循环遍历__apps_start,执行编译时注册的这些service

(lk/app/app.c)
/* one time setup */
void apps_init(void)
{
          
   
	const struct app_descriptor *app;

	/* call all the init routines */
	for (app = __apps_start; app != __apps_end; app++) {
          
   
		if (app->init)
			app->init(app);
	}

	/* start any that want to start on boot */
	for (app = __apps_start; app != __apps_end; app++) {
          
   
		if (app->entry && (app->flags & APP_FLAG_DONT_START_ON_BOOT) == 0) {
          
   
			start_app(app);
		}
	}
}
经验分享 程序员 微信小程序 职场和发展