OHOS3.0標(biāo)準(zhǔn)系統(tǒng)編寫C程序控制LED
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)
前言:
OpenHarmony分為輕量系統(tǒng)、小型系統(tǒng)、標(biāo)準(zhǔn)系統(tǒng),目前對(duì)應(yīng)LiteOS-M、LiteOS-A、Linux內(nèi)核。但好像并沒有說一定是按照使用內(nèi)核來劃分。我們這里姑且先這么區(qū)分。本文使用的是比較新的OpenHarmony 3.0 LTS版本,Linux內(nèi)核,編譯標(biāo)準(zhǔn)系統(tǒng)。官方文檔已經(jīng)說明了,如何使用DevEco Studio開發(fā)hap包,并運(yùn)行在開發(fā)板,但是ACE框架能力有限。設(shè)備硬件開發(fā)還是需要C,所以這篇文章,將在標(biāo)準(zhǔn)系統(tǒng)下編譯C控制Hi3516開發(fā)板的LED閃爍。

1.環(huán)境準(zhǔn)備
3.0源碼下載:
- repo init -u https://gitee.com/openharmony/manifest.git -b OpenHarmony-3.0-LTS --no-repo-verify
- repo sync -c
- repo forall -c 'git lfs pull'
區(qū)別于2.0需要安裝ruby,其他基本都一樣。
- sudo apt-get install ruby-full
編譯命令
- build/prebuilts_download.sh
- ./build.sh --product-name Hi3516DV300
2.編寫helloworld.c
在applications\standard目錄下新建一個(gè)app目錄來存放.c的業(yè)務(wù)代碼。比如applications\standard\app\helloworld.c內(nèi)容容下:
- #include <stdio.h>
- int main(){
- printf("Hello world.\n");
- return 0;
- }
然后在當(dāng)前目錄新建編譯腳本BUILD.gn內(nèi)容如下:
- 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增加如下內(nèi)容。
- "//applications/standard/app:helloworld"
最后執(zhí)行編譯命令即可,開發(fā)板使用的是Hi3516,在不指定out目錄時(shí),缺省生成在/system/lib64或/system/lib下。

3.點(diǎn)亮開發(fā)板LED
能打印helloworld說明環(huán)境是沒問題的,接下來嘗試點(diǎn)亮開發(fā)板的LED。查看Hi3516DV300原理圖,請(qǐng)?zhí)砑渔溄用枋?/p>



Hi3516DV300共有4層板,由原理圖可知:最上層板的紅外補(bǔ)光燈接在GPIO5_1,綠色LED指示燈在GPIO2_3,核心板的紅色LED在GPIO3_4。
接下來參考OpenHarmony GPIO驅(qū)動(dòng)說明。
確定GPIO管腳號(hào)
不同SOC芯片由于其GPIO控制器型號(hào)、參數(shù)、以及控制器驅(qū)動(dòng)的不同,GPIO管腳號(hào)的換算方式不一樣。
- Hi3516DV300
控制器管理12組GPIO管腳,每組8個(gè)。
GPIO號(hào) = GPIO組索引 (0~11) * 每組GPIO管腳數(shù)(8) + 組內(nèi)偏移
舉例:GPIO10_3的GPIO號(hào) = 10 * 8 + 3 = 83
- Hi3518EV300
控制器管理10組GPIO管腳,每組10個(gè)。
GPIO號(hào) = GPIO組索引 (0~9) * 每組GPIO管腳數(shù)(10) + 組內(nèi)偏移
舉例:GPIO7_3的GPIO管腳號(hào) = 7 * 10 + 3 = 73
由此可以得出:
- GPIO5_1 = 5 * 8 + 1;
- GPIO2_3 = 2 * 8 + 3;
- GPIO3_4 = 3 * 8 + 4;
然后新建applications\standard\app\ledtest.c內(nèi)容如下
- #include <stdlib.h> // standard library 標(biāo)準(zhǔn)庫函數(shù)頭文件
- #include <stdio.h> // standard input output 標(biāo)準(zhǔn)輸入輸出函數(shù)
- #include <stdint.h> // 定義了擴(kuò)展的整數(shù)類型和宏
- #include <unistd.h> // POSIX 系統(tǒng) API 訪問功能的頭文件
- #include <fcntl.h> // unix標(biāo)準(zhǔn)中通用的頭文件 define O_WRONLY and O_RDONLY
- // #include <string.h>
- #define GPIO_DIR_IN "in"
- #define GPIO_DIR_OUT "out"
- #define GPIO_VAL_LOW 0
- #define GPIO_VAL_HIGHT 1
- int32_t GpioSetDir(uint16_t gpio, char* dir){
- char path[100] = {0};
- sprintf(path,"echo %d > /sys/class/gpio/export",gpio);
- system(path);
- printf("info:%s\n",path);
- char direction[100] = {0};
- sprintf(direction,"echo %s > /sys/class/gpio/gpio%d/direction",dir,gpio);
- system(direction);
- printf("info:%s\n",direction);
- return 0;
- }
- int32_t GpioWrite(uint16_t gpio, uint16_t val)
- {
- char path[100] = {0};
- sprintf(path,"echo %d > /sys/class/gpio/gpio%d/value",val,gpio);
- system(path);
- printf("info:%s\n",path);
- return 0;
- }
- int main(){
- uint16_t GPIO5_1 = 5 * 8 + 1;
- uint16_t GPIO2_3 = 2 * 8 + 3;
- uint16_t GPIO3_4 = 3 * 8 + 4;
- printf("LED test start\n");
- int32_t ret;
- // uint16_t val;
- ret = GpioSetDir(GPIO5_1,GPIO_DIR_OUT);
- if (ret != 0) {
- printf("GpioSerDir: failed, ret %d\n", ret);
- return 0;
- }
- ret = GpioSetDir(GPIO2_3,GPIO_DIR_OUT);
- if (ret != 0) {
- printf("GpioSerDir: failed, ret %d\n", ret);
- return 0;
- }
- ret = GpioSetDir(GPIO3_4,GPIO_DIR_OUT);
- if (ret != 0) {
- printf("GpioSerDir: failed, ret %d\n", ret);
- return 0;
- }
- 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);
- }
- return 0;
- }
將業(yè)務(wù)代碼添加到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"
- }
- ohos_executable("ledtest") {
- sources = [
- "ledtest.c"
- ]
- subsystem_name = "applications"
- part_name = "prebuilt_hap"
- }
applications\standard\hap\ohos.build
- "//applications/standard/app:ledtest"
之后將程序燒錄到開發(fā)板,執(zhí)行./system/bin/ledtest

就可以看到LED閃爍起來了。

本來是打算使用鴻蒙的GPIO接口來實(shí)現(xiàn)這個(gè)功能的,不過調(diào)試了很久也沒調(diào)通,最后無奈還是用的system自己實(shí)現(xiàn)的GPIO函數(shù)。有沒使用OpenHarmony的GPIO成功的小伙伴可以留言一起交流啊。
文章相關(guān)附件可以點(diǎn)擊下面的原文鏈接前往下載
https://harmonyos.51cto.com/resource/1269
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)