更多内容请访问:与华为联合建立的鸿蒙技术社区https://harmonyos.51cto.com官方前言:OpenHarmony分为轻量级系统、小型系统和标准系统目前对应LiteOS-M、LiteOS-A和Linux内核。但是好像并没有说一定要根据内核的用途来划分。让我们在这里区分一下。本文使用比较新的OpenHarmony3.0LTS版本,Linux内核,编译标准系统。官方文档已经解释了如何使用DevEcoStudio开发hap包并在开发板上运行,但是ACE框架的能力有限。设备硬件开发还是需要C,所以本文将在标准体系下编译C来控制Hi3516开发板的LED闪烁。1.环境准备3.0源码下载:repoinit-uhttps://gitee.com/openharmony/manifest.git-bOpenHarmony-3.0-LTS--no-repo-verifyreposync-crepoforall-c'gitlfspull'与2.0不同,需要安装ruby,其他基本一样。sudoapt-getinstallruby-full编译命令build/prebuilts_download.sh./build.sh--product-nameHi3516DV3002。编写helloworld.c在applications\standard目录下新建一个app目录,存放.c的业务代码。比如applications\standard\app\helloworld.c的内容是:#includeintmain(){printf("Helloworld.\n");return0;}然后新建一个编译脚本BUILD。gn在当前目录中具有以下内容:import("//build/ohos.gni")import("//drivers/adapter/uhdf2/uhdf.gni")ohos_executable("helloworld"){sources=["helloworld.c"]subsystem_name="applications"part_name="prebuilt_hap"}然后在编译框架applications\standard\hap\ohos.build中添加如下内容。"//applications/standard/app:helloworld"足以执行最后的编译命令。开发板采用Hi3516。如果不指定out目录,默认生成在/system/lib64或/system/lib下。3、打开开发板的LED,打印helloworld,说明环境正常。接下来尝试点亮开发板的LED。查看Hi3516DV300原理图,请加个链接说明Hi3516DV300有4层板子,原理图所示:最上面的板子红外补光灯接GPIO5_1,绿色LED指示灯接GPIO2_3,核心板的红色LED连接到GPIO3_4。接下来参考OpenHarmonyGPIO驱动说明。确定GPIO管脚号不同的SOC芯片有不同的GPIO控制器型号、参数和控制器驱动,GPIO管脚号的转换方法也不同。Hi3516DV300控制器管理12组GPIO管脚,每组8个管脚。GPIO编号=GPIO组索引(0~11)*每组GPIO管脚数量(8)+组内偏移量示例:GPIO10_3的GPIO编号=10*8+3=83Hi3518EV300控制器管理10组GPIO管脚,每组10个。GPIO编号=GPIO组索引(0~9)*每组GPIO管脚数(10)+组内偏移量例:GPIO7_3的GPIO管脚号=7*10+3=73由此可得:GPIO5_1=5*8+1;GPIO2_3=2*8+3;GPIO3_4=3*8+4;然后新建applications\standard\app\ledtest.c内容如下#include//standardlibrary标准库函数头文件#include//standardinputoutput标准输入输出函数#include//定义扩展整数类型和宏#include//POSIX系统API访问函数头文件#include//Unix标准通用头文件defineO_WRONLYandO_RDONLY//#include#defineGPIO_DIR_IN"in"#defineGPIO_DIR_OUT"out"#defineGPIO_VAL_LOW0#defineGPIO_VAL_HIGHT1int32_tGpioSetDir(uint16_tgirpio)0{charp,char*{0};sprintf(路径,"echo%d>/sys/class/gpio/export",gpio);system(path);printf("info:%s\n",path);chardirection[100]={0};sprintf(direction,"echo%s>/sys/class/gpio/gpio%d/direction",dir,gpio);system(direction);printf("info:%s\n",direction);return0;}int32_tGpioWrite(uint16_tgpio,uint16_tval){charpath[100]={0};sprintf(路径,“echo%d>/sys/class/gpio/gpio%d/value”,val,gpio);系统(路径);printf(“信息:%s\n”,路径);return0;}intmain(){uint16_tGPIO5_1=5*8+1;uint16_tGPIO2_3=2*8+3;uint16_tGPIO3_4=3*8+4;printf("LEDteststart\n");int32_tret;//uint16_tval;ret=GpioSetDir(GPIO5_1,GPIO_DIR_OUT);if(ret!=0){printf("GpioSerDir:failed,ret%d\n",ret);return0;}ret=GpioSetDir(GPIO2_3,GPIO_DIR_OUT);if(ret!=0){printf("GpioSerDir:failed,ret%d\n",ret);return0;}ret=GpioSetDir(GPIO3_4,GPIO_DIR_OUT);if(ret!=0){printf("GpioSerDir:failed,ret%d\n",ret);return0;}while(1){GpioWrite(GPIO5_1,GPIO_VAL_HIGHT);usleep(1000000);GpioWrite(GPIO5_1,GPIO_VAL_LOW);usleep(1000000);GpioWrite(GPIO2_3,GPIO_VAL_HIGHT);usleep(1000000);GpioWrite(GPIO2_3,GPIO_VAL_LOW));usleep(1000000);GpioWrite(GPIO3_4,GPIO_VAL_HIGHT);usleep(1000000);GpioWrite(GPIO3_4,GPIO_VAL_LOW);usleep(1000000);}return0;}将业务代码添加到BUILD.gnimport("//build/ohos.gni")导入("//drivers/adapter/uhdf2/uhdf.gni")ohos_executable("helloworld"){sources=["helloworld.c"]subsystem_name="applications"part_name="prebuilt_hap"}ohos_executable("ledtest"){sources=["ledtest.c"]subsystem_name="applications"part_name="prebuilt_hap"}applications\standard\hap\ohos.build"//applications/standard/app:ledtest"然后将程序烧录到开发板并执行./system/bin/ledtest可以看到LED在闪烁,本来打算用鸿蒙的GPIO接口来实现这个功能的,调试了半天也没能调出来,最后,无奈只能使用系统自己实现的GPIO功能,有没有用过OpenHarmony的GPIO成功的朋友可以留言一起交流,点击下方链接下载文章相关附件https://harmonyos.51cto.com/resource/1269更多信息请访问:和华为官方Harmonyos技术社区https://harmonyos.51cto.com