開(kāi)發(fā)環(huán)境:
開(kāi)發(fā)系統(tǒng):Ubuntu 20.04
開(kāi)發(fā)板:Pegasus物聯(lián)網(wǎng)開(kāi)發(fā)板
MCU:Hi3861
OpenHarmony版本:3.0.1-LTS
4.1新建工程及配置
1.新建工程及源碼
- 新建目錄
$ mkdir hello
在applications/sample/myapp中新建src目錄以及myapp.c文件,代碼如下所示。
#include <stdio.h>
#include "ohos_init.h"
#include "ohos_types.h"
void app_task(void)
{
printf("\n");
printf("Hello hi3861!\n");
printf("\n");
}
SYS_RUN(app_task);
- 新建編譯組織文件
新建applications/sample/myapp/BUILD.gn文件,內(nèi)容如下所示:
static_library("myapp") {
sources = [
"src/myapp.c"
]
include_dirs = [
"http://utils/native/lite/include"
]
}
static_library中指定業(yè)務(wù)模塊的編譯結(jié)果,為靜態(tài)庫(kù)文件libmyapp.a,開(kāi)發(fā)者根據(jù)實(shí)際情況完成填寫(xiě)。
sources中指定靜態(tài)庫(kù).a所依賴(lài)的.c文件及其路徑,若路徑中包含"http://“則表示絕對(duì)路徑(此處為代碼根路徑),若不包含”//"則表示相對(duì)路徑。
include_dirs中指定source所需要依賴(lài)的.h文件路徑
新建的工程目錄如下:
$ tree
2.添加新組件
修改文件build/lite/components/applications.json,添加組件hello_world_app的配置。
{
"component": "my_app",
"description": "appsamples.",
"optional": "true",
"dirs": [
"applications/sample/myapp"
],
"targets": [
"http://applications/sample/myapp:myapp"
],
"rom": "",
"ram": "",
"output": [],
"adapted_kernel": [ "liteos_m" ],
"features": [],
"deps": {
"components": [],
"third_party": []
}
},
3.修改單板配置文件
修改文件vendor/hisilicon/hispark_pegasus/config.json,新增my_app組件的條目。
{
"subsystem": "applications",
"components": [
{ "component": "wifi_iot_sample_app ", "features":[] }
{ "component": "my_app", "features":[] }
]
},
4.關(guān)閉xts測(cè)試子系統(tǒng)。
系統(tǒng)每次開(kāi)機(jī)后都要跑xts認(rèn)證程序,這里先刪除該部分內(nèi)容。
4.2編譯下載驗(yàn)證
接下來(lái)就可以編譯了。
$ hb set
全編譯。
$ hb build -f
成功編譯后,固件在out/hispark_pegasus/wifiiot_hispark_pegasus目錄下。
Hi3861_wifiiot_app_allinone.bin就是需要燒寫(xiě)的固件。
然后把固件下載到板子中。
接下來(lái)就可以根據(jù)該實(shí)例開(kāi)發(fā)自己的應(yīng)用了。
4.3系統(tǒng)啟動(dòng)流程分析
下面簡(jiǎn)單分析下系統(tǒng)的啟動(dòng)流程,系統(tǒng)的入口函數(shù)是app_main(),在device/hisilicon/hispark_pegasus/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();
/* 此處設(shè)為T(mén)RUE后中斷中看門(mén)狗復(fù)位會(huì)顯示復(fù)位時(shí)PC值,但有復(fù)位不完全風(fēng)險(xiǎn),量產(chǎn)版本請(qǐng)務(wù)必設(shè)為FALSE */
hi_syserr_watchdog_debug(HI_FALSE);
/* 默認(rèn)記錄宕機(jī)信息到FLASH,根據(jù)應(yīng)用場(chǎng)景,可不記錄,避免頻繁異常宕機(jī)情況損耗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驅(qū)動(dòng)運(yùn)行日志等,無(wú)需初始化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(); /* 釋放系統(tǒng)棧內(nèi)存所使用任務(wù) */
#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();
}
該函數(shù)首先打印SDK的版本信息,然后掛載文件系統(tǒng),初始化WiFi信息等等一系列初始化,接這就到OHOS_Main(),該函數(shù)就是OpenHarmony系統(tǒng)的初始化。OHOS_Main()函數(shù)在文件device/hisilicon/hispark_pegasus/sdk_liteos/app/wifiiot_app/src/ohos_main.c中。
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()函數(shù)是一個(gè)弱函數(shù),其定義如下:
void __attribute__((weak)) OHOS_SystemInit(void)
{
return;
}
因此該函數(shù)主要是系統(tǒng)為應(yīng)用開(kāi)發(fā)者提供的。OHOS_SystemInit()函數(shù)在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();
}
到這里基本就完成了所得初始化,其中我們編寫(xiě)的應(yīng)用就是MODULE_INIT(run)中完成的。
在base/startup/bootstrap_lite/services/source/core_main.h文件中,有如下定義:
MODULE_INIT定義如下:
#define MODULE_INIT(name) \
do { \
MODULE_CALL(name, 0); \
} while (0)
MODULE_CALL定義如下:
#define MODULE_CALL(name, step) \
do { \
InitCall *initcall = (InitCall *)(MODULE_BEGIN(name, step)); \
InitCall *initend = (InitCall *)(MODULE_END(name, step)); \
for (; initcall < initend; initcall++) { \
(*initcall)(); \
} \
} while (0)
模塊的名字定義如下:
#define MODULE_NAME(name, step) “.zinitcall.” #name #step “.init”
而SYS_RUN在utils/native/lite/include/ohos_init.h中定義。
/**
* @brief Identifies the entry for initializing and starting a system running phase by the
* priority 2.
*
* This macro is used to identify the entry called at the priority 2 in the system startup
* phase of the startup process. \n
*
* @param func Indicates the entry function for initializing and starting a system running phase.
* The type is void (*)(void).
*/
#define SYS_RUN(func) LAYER_INITCALL_DEF(func, run, "run")
而LAYER_INITCALL_DEF定義如下:
#define LAYER_INITCALL(func, layer, clayer, priority) \
static __attribute__((constructor(CTOR_VALUE_##layer + LAYER_INIT_LEVEL_##priority))) \
void BOOT_##layer##priority##func() {func();}
#else
#define LAYER_INITCALL(func, layer, clayer, priority) \
static const InitCall USED_ATTR __zinitcall_##layer##_##func \
__attribute__((section(".zinitcall." clayer #priority ".init"))) = func
#endif
// Default priority is 2, priority range is [0, 4]
#define LAYER_INITCALL_DEF(func, layer, clayer) \
LAYER_INITCALL(func, layer, clayer, 2)
可以看到最終SYS_RUN宏定義都是定義在.zinitcall中,因此SYS_RUN()宏設(shè)置的函數(shù)都會(huì)在MODULE_INIT(run)完成調(diào)用。
好了,最后看看應(yīng)用啟動(dòng)的調(diào)用流程:
官方文檔
歡迎訪(fǎng)問(wèn)我的網(wǎng)站
BruceOu的嗶哩嗶哩
BruceOu的主頁(yè)
BruceOu的博客
BruceOu的CSDN博客
BruceOu的簡(jiǎn)書(shū)
BruceOu的知乎文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-485693.html
歡迎訂閱我的微信公眾號(hào)
關(guān)注公眾號(hào)[嵌入式實(shí)驗(yàn)樓]]獲取更多資訊文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-485693.html
到了這里,關(guān)于《HarmonyOS開(kāi)發(fā) – OpenHarmony開(kāi)發(fā)筆記(基于小型系統(tǒng))》第4章 OpenHarmony應(yīng)用開(kāi)發(fā)實(shí)例的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!