深入理解HarmonyOS(小白学习笔记)

深入理解HarmonyOS(小白学习笔记)

OpenHarmony设备开发入门【2022年最新版】(润和)笔记:

  1. Hi3861 的启动流程
    在写物联网开发的时候会发现很多和传统文件路径编译不一样的地方,主要表现为:
建立的正确的文档的路径
编写业务代码
编写用于将业务构建成静态库的 BUILD.gn
编写模块 BUILD.gn 文件指定需要参与构建的特性模块

具体的流程可以参见上一篇文章:【物联网开发】HelloWorld!物联网开发(基于海思Hi3861)直达


在《OpenHarmony设备开发入门【2022年最新版》的pdf中有如下详解:

4.3 Hi3861 启动流程
由于 hi3681 的 liteos-m 被芯片 rom 化了,固化在芯片内部了。所以我们主要看内核启
动后的第一个入口函数:(路径:device\soc\hisilicon\hi3861v100\sdk_liteos\app\wifiiot_app\src\app_main.c)


hi_void app_main(hi_void)
{
#ifdef CONFIG_FACTORY_TEST_MODE
        printf("factory test mode!\r\n");
#endif

    const hi_char* sdk_ver = hi_get_sdk_version();
    printf("sdk ver:%s\r\n", sdk_ver);

    hi_flash_partition_table *ptable = HI_NULL;

    peripheral_init();
    peripheral_init_no_sleep();

#ifndef CONFIG_FACTORY_TEST_MODE
    hi_lpc_register_wakeup_entry(peripheral_init);
#endif

    hi_u32 ret = hi_factory_nv_init(HI_FNV_DEFAULT_ADDR, HI_NV_DEFAULT_TOTAL_SIZE, HI_NV_DEFAULT_BLOCK_SIZE);
    if (ret != HI_ERR_SUCCESS) {
        printf("factory nv init fail\r\n");
    }

    /* partion table should init after factory nv init. */
    ret = hi_flash_partition_init();
    if (ret != HI_ERR_SUCCESS) {
        printf("flash partition table init fail:0x%x \r\n", ret);
    }
    ptable = hi_get_partition_table();

    ret = hi_nv_init(ptable->table[HI_FLASH_PARTITON_NORMAL_NV].addr, ptable->table[HI_FLASH_PARTITON_NORMAL_NV].size,
        HI_NV_DEFAULT_BLOCK_SIZE);
    if (ret != HI_ERR_SUCCESS) {
        printf("nv init fail\r\n");
    }

#ifndef CONFIG_FACTORY_TEST_MODE
    hi_upg_init();
#endif

    /* if not use file system, there is no need init it */
    hi_fs_init();

    (hi_void)hi_event_init(APP_INIT_EVENT_NUM, HI_NULL);
    hi_sal_init();
    /* 此处设为TRUE后中断中看门狗复位会显示复位时PC值,但有复位不完全风险,量产版本请务必设为FALSE */
    hi_syserr_watchdog_debug(HI_FALSE);
    /* 默认记录宕机信息到FLASH,根据应用场景,可不记录,避免频繁异常宕机情况损耗FLASH寿命 */
    hi_syserr_record_crash_info(HI_TRUE);

    hi_lpc_init();
    hi_lpc_register_hw_handler(config_before_sleep, config_after_sleep);

#if defined(CONFIG_AT_COMMAND) || defined(CONFIG_FACTORY_TEST_MODE)
    ret = hi_at_init();
    if (ret == HI_ERR_SUCCESS) {
        hi_at_sys_cmd_register();
    }
#endif

    /* 如果不需要使用Histudio查看WIFI驱动运行日志等,无需初始化diag */
    /* if not use histudio for diagnostic, diag initialization is unnecessary */
    /* Shell and Diag use the same uart port, only one of them can be selected */
#ifndef CONFIG_FACTORY_TEST_MODE

#ifndef ENABLE_SHELL_DEBUG
#ifdef CONFIG_DIAG_SUPPORT
    (hi_void)hi_diag_init();
#endif
#else
    (hi_void)hi_shell_init();
#endif

    tcpip_init(NULL, NULL);
#endif

    ret = hi_wifi_init(APP_INIT_VAP_NUM, APP_INIT_USR_NUM);
    if (ret != HISI_OK) {
        printf("wifi init failed!\n");
    } else {
        printf("wifi init success!\n");
    }
    app_demo_task_release_mem(); /* 释放系统栈内存所使用任务 */

#ifndef CONFIG_FACTORY_TEST_MODE
    app_demo_upg_init();
#ifdef CONFIG_HILINK
    ret = hilink_main();
    if (ret != HISI_OK) {
        printf("hilink init failed!\n");
    } else {
        printf("hilink init success!\n");
    }
#endif
#endif
    OHOS_Main();
}

app_main 一开始打印了 SDK 版本号,中间还会有一些初始化动作,最后一行会调用
OHOS_Main();
该函数原型如下:

void OHOS_Main()
{
#if defined(CONFIG_AT_COMMAND) || defined(CONFIG_FACTORY_TEST_MODE)
hi_u32 ret;
ret = hi_at_init();
if (ret == HI_ERR_SUCCESS) {
hi_u32 ret2 = hi_at_register_cmd(G_OHOS_AT_FUNC_TBL, OHOS_AT_FUNC_NUM);
if (ret2 != HI_ERR_SUCCESS) {
printf("Register ohos failed!\n");
}
}
#endif
OHOS_SystemInit();
}

最后,OHOS_SystemInit 函数进行鸿蒙系统的初始化。我们进去看下初始化做了哪些动
作。
路径:base\startup\bootstrap_lite\services\source\system_init.c

void OHOS_SystemInit(void)
{
MODULE_INIT(bsp);
MODULE_INIT(device);
MODULE_INIT(core);
SYS_INIT(service);
SYS_INIT(feature);
MODULE_INIT(run);
SAMGR_Bootstrap();
}

我们可以看到主要是初始化了 一些相关模块、系统,包括有 bsp、device(设备)。其
中最终的是 MODULE_INIT(run)
它负责调用了,所有 run 段的代码,那么 run 段的代码是哪些呢?
事实上就是我们前面 application 中使用 SYS_RUN() 宏设置的函数名。
还记得我们前面写的 hello world 应用程序吗?

#include "ohos_init.h"
#include "ohos_types.h"
void HelloWorld(void)
{
printf("[DEMO] Hello world.\n");
}
SYS_RUN(HelloWorld)

也就是说所有用 SYS_RUN() 宏设置的函数都会在使用 MODULE_INIT(run); 为此我们可以在 OHOS_SystemInit(void)的函数中加一些打印函数:

深入理解HarmonyOS(小白学习笔记)_第1张图片
深入理解HarmonyOS(小白学习笔记)_第2张图片


到此为止,我们已经看到了大概的启动流程:

app_main
OHOS_Main<>
OHOS_SystemInit<>
SYS INIT<>
MODULE INIT<>
MODULE_INIT--run
它负责调用了所有 run 段的代码
SYS_RUN<>

你可能感兴趣的:(harmonyos,学习,笔记)