如何编写第一个hello world程序

本文简单介绍如何编写第一个hello world程序,以及程序是如何被执行的
适合群体:适用于hi3861开发板,启动流程分析
1、编写第一个程序
编写一个hello world程序比较简单,可以参考官网。
本文在这里做下总结:
(1)确定目录结构
开发者编写业务时,务必先在./applications/sample/wifi-iot/app路径下新建一个目录(或一套目录结构),用于存放业务源码文件。
例如:在app下新增业务my_first_app,其中hello_world.c为业务代码,build.gn为编译脚本,具体规划目录结构如下:
└── applications
    └── sample
        └── wifi-iot
            └── app
                │── my_first_app
                │  │── hello_world.c
                │  └── build.gn
                └── build.gn
(2)编写业务代码
在hello_world.c中新建业务入口函数helloworld,并实现业务逻辑。并在代码最下方,使用 harmonyos启动恢复模块接口sys_run()启动业务。(sys_run定义在ohos_init.h文件中)
                    #include #include ohos_init.h#include ohos_types.hvoid helloworld(void){    printf([demo] hello world.);}sys_run(helloworld); (3)编写用于将业务构建成静态库的build.gn文件
如步骤1所述,build.gn文件由三部分内容(目标、源文件、头文件路径)构成,需由开发者完成填写。以my_first_app为例,需要创建./applications/sample/wifi-iot/app/my_first_app/build.gn,并完如下配置。
                          static_library(myapp) {    sources = [        hello_world.c    ]    include_dirs = [        //utils/native/liteos/include    ] static_library中指定业务模块的编译结果,为静态库文件libmyapp.a,开发者根据实际情况完成填写。
sources中指定静态库.a所依赖的.c文件及其路径,若路径中包含//则表示绝对路径(此处为代码根路径),若不包含//则表示相对路径。
include_dirs中指定source所需要依赖的.h文件路径。
(4)编写模块build.gn文件,指定需参与构建的特性模块
配置./applications/sample/wifi-iot/app/build.gn文件,在features字段中增加索引,使目标模块参与编译。features字段指定业务模块的路径和目标,以my_first_app举例,features字段配置如下。
my_first_app是相对路径,指向./applications/sample/wifi-iot/app/my_first_app/build.gn。
myapp是目标,指向./applications/sample/wifi-iot/app/my_first_app/build.gn中的static_library(myapp)。
2、hi3861相关代码结构
目前hi3861用的是liteos-m内核,但是目前hi3681的liteos-m被芯片rom化了,固化在芯片内部了。所以在harmonyos代码是找不到hi3861的内核部分。
但是这样不妨碍我们去理清hi3861的其他代码结构。
hi3861平台配置文件位于:vendorhisiliconhispark_pegasusconfig.json
可以看到该配置文件有很多内容,第一段这里指定了产品名称、版本、使用的内核类型
下面这里都是子系统:
其中我们重点关注这几个模块:
(1)applications:应用子系统
路径:applications/sample/wifi-iot/app
作用:这个路径下存放了hi3681编写的应用程序代码,例如我们刚刚写得hello world 代码就放在这个路径下。
(2)iot_hardware:硬件驱动子系统
头文件路径:baseiot_hardwareperipheralinterfaceskits
具体代码路径,由deviceoardhisiliconhispark_pegasusliteos_mconfig.gni文件中指定:
config.gni文件内容较多,后续会一一解读,作用:存放了 hi3681 芯片相关的驱动、例如spi、gpio、uart等。
(3)xts:xts测试子系统
这里我们先不要xts子系统,不然每次开机后,系统都要跑xts认证程序,影响我们后面测试,我们先注删除,如下:
3、hi3861启动流程
由于hi3681的liteos-m被芯片rom化了,固化在芯片内部了。所以我们主要看内核启动后的第一个入口函数。
代码路径:
                                                                                                                                                                                                      devicesochisiliconhi3861v100sdk_liteosappwifiiot_appsrcapp_main.chi_void app_main(hi_void){#ifdef config_factory_test_mode        printf(factory test mode!);#endif    const hi_char* sdk_ver = hi_get_sdk_version();    printf(sdk ver:%s, 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);    }    /* 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 , 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);    }#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!);    } else {        printf(wifi init success!);    }    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!);    } else {        printf(hilink init success!);    }#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!);        }    }#endif    ohos_systeminit();} 向右滑动查看全部代码
最后,ohos_systeminit函数进行鸿蒙系统的初始化。我们进去看下初始化做了哪些动作。
路径:basestartupootstrap_liteservicessourcesystem_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.hvoid helloworld(void){    printf([demo] hello world.);}sys_run(helloworld); 也就是说所有用sys_run() 宏设置的函数都会在使用module_init(run); 的时候被调用。
为了验证这一点,我们可以加一些打印信息,如下:
我们重新编译后烧录。打开串口查看打印信息,如下:
可以看到在27行之后,就打印 hello world的信息,符合预期。
原文标题:openharmony轻量系统开发【4】编写第一个程序、启动流程分析
文章出处:【微信公众号:harmonyos官方合作社区】欢迎添加关注!文章转载请注明出处。


vivo将全球首发天玑1100处理器
Riskified与电通达成全球战略合作,助推全球零售商数字化转型
北京大学2023年湾区青年企业家研修班一行赴软通动力参观交流
杜比全景声音效,华为新款MateBook X Pro给您五星级私人影院体验
国产半导体自动物料搬运系统领军企业成川科技完成A+轮融资
如何编写第一个hello world程序
区块链如何解决供应链管理的断裂环节
常见的功率器件有哪些?
AC-DC降压芯片至关重要的“四大天王”
机房应用动环监控系统
汽车仪表盘中图形显示技术应用
2021世界机器人大赛-佛山城市赛在佛山市成功举办
金融机构使用案例分析机器学习算法——聚类clustering
安森美推出先进RF系统单芯片支持需要物联网互联的应用
源创通信网络摄像机专用POE受电模块介绍
摄相机镍镉电池放电电路图
希腊10GW太阳能光伏项目披露更多细节
软件测试行业中机器人和人工智能扮演者何种角色?
Note的十代旗舰之作全新Redmi Note10 Pro来了
MgO的湿法清洁