Harmony OS 设备开发学习记录(一)-- 第一个程序hello world

本文记录了在HarmonyOS 2.0环境下,使用hisparkwifi套件从创建demo目录、编写hello.c代码到编译固件的全过程。通过hb工具进行编译后,固件被烧录到开发板,最终在串口监视器中验证了helloworld的输出,成功运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Harmony OS 设备开发学习记录(一)-- 第一个程序hello world

基于hispark wifi套件采用harmony os 2.0全量代码

一、在源码中建立demo目录

项目目录结构

二、编写代码

在firstapp/hello.c中写入

#include <stdio.h>

#include "ohos_init.h"

void hello(void){
    printf("hello world!\n");
}

SYS_RUN(hello);

在firstapp/BUILD.gn中写入

static_library("firstapp") {
    sources = [
        "hello.c"
    ]

    include_dirs = [
        "//utils/native/lite/include",
        # "//kernel/liteos_m/components/cmsis/2.0",
        # "//base/iot_hardware/peripheral/interfaces/kits",
    ]
}

在上级目录的app/BUILD.gn中写入

import("//build/lite/config/component/lite_component.gni")

lite_component("app") {
    features = [
        "firstapp",
    ]
}

三、在Linux下使用hb工具进行编译

root@DESKTOP-QAO2AOK:~/harmonyos/code-2.0-canary# hb set
[OHOS INFO] Input code path: .
OHOS Which product do you need?  wifiiot_hispark_pegasus
root@DESKTOP-QAO2AOK:~/harmonyos/code-2.0-canary# hb build
[OHOS INFO] [1/1] Regenerating ninja files
[OHOS INFO] [1/46] ACTION //test/xts/acts/communication_lite/softbus_hal:ActsSoftBusTest(//build/lite/toolchain:riscv32-unknown-elf)
[OHOS INFO] [2/45] ACTION //test/xts/acts/distributed_schedule_lite/system_ability_manager_hal:ActsSamgrTest(//build/lite/toolchain:riscv32-unknown-elf)
[OHOS INFO] [3/44] ACTION //test/xts/acts/communication_lite/wifiservice_hal:ActsWifiServiceTest(//build/lite/toolchain:riscv32-unknown-elf)
[OHOS INFO] [4/43] ACTION //test/xts/acts/hiviewdfx_lite/hilog_hal:ActsDfxFuncTest(//build/lite/toolchain:riscv32-unknown-elf)
[OHOS INFO] [5/42] ACTION //test/xts/acts/iot_hardware_lite/iot_controller_hal:ActsWifiIotTest(//build/lite/toolchain:riscv32-unknown-elf)
[OHOS INFO] [6/41] ACTION //test/xts/acts/distributeddatamgr_lite/dbm_kv_store_hal:ActsDbmKvStoreTest(//build/lite/toolchain:riscv32-unknown-elf)
[OHOS INFO] [7/40] ACTION //test/xts/acts/communication_lite/lwip_hal:ActsLwipTest(//build/lite/toolchain:riscv32-unknown-elf)
[OHOS INFO] [8/39] ACTION //test/xts/acts/startup_lite/syspara_hal:ActsParameterTest(//build/lite/toolchain:riscv32-unknown-elf)
[OHOS INFO] [9/38] ACTION //test/xts/acts/update_lite/updater_hal:ActsUpdaterFuncTest(//build/lite/toolchain:riscv32-unknown-elf)
[OHOS INFO] [10/37] ACTION //test/xts/acts/security_lite/datahuks_hal:ActsSecurityDataTest(//build/lite/toolchain:riscv32-unknown-elf)
[OHOS INFO] [11/36] ACTION //test/xts/acts/utils_lite/file_hal:ActsUtilsFileTest(//build/lite/toolchain:riscv32-unknown-elf)
[OHOS INFO] [12/35] ACTION //test/xts/acts/kernel_lite/kernelcmsis_hal:ActsCMSISTest(//build/lite/toolchain:riscv32-unknown-elf)
[OHOS INFO] [13/34] ACTION //test/xts/acts/startup_lite/bootstrap_hal:ActsBootstrapTest(//build/lite/toolchain:riscv32-unknown-elf)
[OHOS INFO] [14/33] gcc cross compiler obj/applications/sample/wifi-iot/app/firstapp/libfirstapp.hello.o
[OHOS INFO] [15/33] gcc cross compiler obj/base/startup/syspara_lite/frameworks/parameter/src/param_impl_hal/libsysparam.param_impl_hal.o
[OHOS INFO] [16/33] AR libs/libfirstapp.a
[OHOS INFO] [17/33] STAMP obj/applications/sample/wifi-iot/app/app.stamp
[OHOS INFO] [18/33] ACTION //test/xts/acts/utils_lite/kv_store_hal:ActsKvStoreTest(//build/lite/toolchain:riscv32-unknown-elf)
[OHOS INFO] [19/27] gcc cross compiler obj/vendor/hisilicon/hispark_pegasus/hals/utils/sys_param/libhal_sysparam.hal_sys_param.o
[OHOS INFO] [20/27] AR libs/libhal_sysparam.a
[OHOS INFO] [21/27] gcc cross compiler obj/base/startup/syspara_lite/frameworks/parameter/src/libsysparam.parameter_common.o
[OHOS INFO] [22/27] AR libs/libsysparam.a
[OHOS INFO] [23/27] STAMP obj/base/startup/syspara_lite/frameworks/parameter/parameter_notes.stamp
[OHOS INFO] [24/27] STAMP obj/base/startup/syspara_lite/frameworks/parameter/parameter.stamp
[OHOS INFO] [25/27] STAMP obj/build/lite/ohos.stamp
[OHOS INFO] [26/27] ACTION //device/hisilicon/hispark_pegasus/sdk_liteos:run_wifiiot_scons(//build/lite/toolchain:riscv32-unknown-elf)
[OHOS INFO] /root/harmonyos/code-2.0-canary/vendor/hisilicon/hispark_pegasus/fs.yml not found, stop packing fs. If the product does not need to be packaged, ignore it.
[OHOS INFO] wifiiot_hispark_pegasus build success

四、将编译好的固件烧录到开发板

将linux中的源码文件夹拷贝到Windows下
打开vscode使用DevEco Device Tool打开源码文件夹
选择对应的开发板型号
这里选择的是hi3861
然后在项目设置中按照实际端口情况进行如下设置
在这里插入图片描述
保存项目并打开
在这里插入图片描述
点击upload进行烧录,烧录时需要根据提示按下开发板的rst键,稍等片刻,看到success代表烧录成功。
在这里插入图片描述
点击左侧串口工具验证
在这里插入图片描述
按下开发板的rst键在串口监视器看到如下输出代表成功
在这里插入图片描述

注:开发板运行代码后会持续打印运行信息,须将串口监视器拉到开始部位才能看到打印的hello world。

这里是引用

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EMb-Y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值