小熊派:用OpenHarmory3.0點亮LED
摘要:作為一個代表性的完整的開發,本案例可以分成3大部分:程式碼檔案的規劃,LED燈的驅動開發,點亮LED的業務開發。
本文分享自華為雲社群《在小熊派Micro上用OpenHarmory3.0點亮LED(1)LED驅動開發》,作者:神龍居市。
專案總覽
作為一個代表性的完整的開發,本案例可以分成3大部分:程式碼檔案的規劃,LED燈的驅動開發,點亮LED的業務開發。
一、LED驅動開發
1.1.確定目錄結構
1.1.1.根據HDF框架以元件化的驅動模型作為核心設計思路,HDF框架將一類裝置驅動放在同一個host裡面,開發者也可以將驅動功能分層獨立開發和部署,支援一個驅動多個node,HDF驅動模型如下圖
1.1.2.本案例目錄
在./device/st/drivers路徑下新建一個led的目錄,用來存放驅動原始碼檔案。
在./device/st/bearpi_hm_micro/liteos_a/hdf_config路徑下新建led資料夾,並建立驅動配置檔案led_config.hcs
1.2.LED驅動實現
1.2.1.驅動實現包含驅動業務程式碼和驅動入口註冊,在led.c檔案中新增以下程式碼
#include "hdf_device_desc.h" #include "hdf_log.h" #include "device_resource_if.h" #include "osal_io.h" #include "osal.h" #include "osal_mem.h" #include "gpio_if.h" #define HDF_LOG_TAG led_driver // 列印日誌所包含的標籤,如果不定義則用預設定義的HDF_TAG標籤 #define LED_WRITE_READ 1 // 讀寫操作碼1 enum LedOps { LED_OFF, LED_ON, LED_TOGGLE, }; struct Stm32Mp1ILed { uint32_t gpioNum; }; static struct Stm32Mp1ILed g_Stm32Mp1ILed; uint8_t status= 0; // Dispatch是用來處理使用者態發下來的訊息 int32_t LedDriverDispatch(struct HdfDeviceIoClient *client, int cmdCode, struct HdfSBuf *data, struct HdfSBuf *reply) { uint8_t contrl; HDF_LOGE("Led driver dispatch"); if (client == NULL || client->device == NULL) { HDF_LOGE("Led driver device is NULL"); return HDF_ERR_INVALID_OBJECT; } switch (cmdCode) { /* 接收到使用者態發來的LED_WRITE_READ命令 */ case LED_WRITE_READ: /* 讀取data裡的資料,賦值給contrl */ HdfSbufReadUint8(data,&contrl); switch (contrl) { /* 開燈 */ case LED_ON: GpioWrite(g_Stm32Mp1ILed.gpioNum, GPIO_VAL_LOW); status = 1; break; /* 關燈 */ case LED_OFF: GpioWrite(g_Stm32Mp1ILed.gpioNum, GPIO_VAL_HIGH); status = 0; break; /* 狀態翻轉 */ case LED_TOGGLE: if(status == 0) { GpioWrite(g_Stm32Mp1ILed.gpioNum, GPIO_VAL_LOW); status = 1; } else { GpioWrite(g_Stm32Mp1ILed.gpioNum, GPIO_VAL_HIGH); status = 0; } break; default: break; } /* 把LED的狀態值寫入reply, 可被帶至使用者程式 */ if (!HdfSbufWriteInt32(reply, status)) { HDF_LOGE("replay is fail"); return HDF_FAILURE; } break; default: break; } return HDF_SUCCESS; } // 讀取驅動私有配置 static int32_t Stm32LedReadDrs(struct Stm32Mp1ILed *led, const struct DeviceResourceNode *node) { int32_t ret; struct DeviceResourceIface *drsOps = NULL; drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE); if (drsOps == NULL || drsOps->GetUint32 == NULL) { HDF_LOGE("%s: invalid drs ops!", __func__); return HDF_FAILURE; } /* 讀取led.hcs裡面led_gpio_num的值 */ ret = drsOps->GetUint32(node, "led_gpio_num", &led->gpioNum, 0); if (ret != HDF_SUCCESS) { HDF_LOGE("%s: read led gpio num fail!", __func__); return ret; } return HDF_SUCCESS; } //驅動對外提供的服務能力,將相關的服務介面繫結到HDF框架 int32_t HdfLedDriverBind(struct HdfDeviceObject *deviceObject) { if (deviceObject == NULL) { HDF_LOGE("Led driver bind failed!"); return HDF_ERR_INVALID_OBJECT; } static struct IDeviceIoService ledDriver = { .Dispatch = LedDriverDispatch, }; deviceObject->service = (struct IDeviceIoService *)(&ledDriver); HDF_LOGD("Led driver bind success"); return HDF_SUCCESS; } // 驅動自身業務初始的介面 int32_t HdfLedDriverInit(struct HdfDeviceObject *device) { struct Stm32Mp1ILed *led = &g_Stm32Mp1ILed; int32_t ret; if (device == NULL || device->property == NULL) { HDF_LOGE("%s: device or property NULL!", __func__); return HDF_ERR_INVALID_OBJECT; } /* 讀取hcs私有屬性值 */ ret = Stm32LedReadDrs(led, device->property); if (ret != HDF_SUCCESS) { HDF_LOGE("%s: get led device resource fail:%d", __func__, ret); return ret; } /* 將GPIO管腳配置為輸出 */ ret = GpioSetDir(led->gpioNum, GPIO_DIR_OUT); if (ret != 0) { HDF_LOGE("GpioSerDir: failed, ret %d\n", ret); return ret; } HDF_LOGD("Led driver Init success"); return HDF_SUCCESS; } // 驅動資源釋放的介面 void HdfLedDriverRelease(struct HdfDeviceObject *deviceObject) { if (deviceObject == NULL) { HDF_LOGE("Led driver release failed!"); return; } HDF_LOGD("Led driver release success"); return; } // 定義驅動入口的物件,必須為HdfDriverEntry(在hdf_device_desc.h中定義)型別的全域性變數 struct HdfDriverEntry g_ledDriverEntry = { .moduleVersion = 1, .moduleName = "HDF_LED", .Bind = HdfLedDriverBind, .Init = HdfLedDriverInit, .Release = HdfLedDriverRelease, }; // 呼叫HDF_INIT將驅動入口註冊到HDF框架中 HDF_INIT(g_ledDriverEntry);
1.2.2.編譯指令碼檔案
在led/BUILD.gn檔案中新增以下程式碼,將led.c編譯成hdf_led
import("//drivers/adapter/khdf/liteos/hdf.gni") hdf_driver("hdf_led") { sources = [ "led.c", ] }
在/device/st/drivers/BUILD.gn檔案中新增以下程式碼,將hdf_led編譯進核心,"led"是新增內容
1.3.驅動配置
HDF使用HCS作為配置描述原始碼,驅動配置包含兩部分,HDF框架定義的驅動裝置描述和驅動的私有配置資訊。
HDF框架載入驅動所需要的資訊來源於HDF框架定義的驅動裝置描述,因此基於HDF框架開發的驅動必須要在HDF框架定義的device_info.hcs配置檔案中新增對應的裝置描述,所以我們需要在device\st\bearpi_hm_micro\liteos_a\hdf_config\device_info\device_info.hcs中新增LED裝置描述
device_led :: device { // led裝置節點 device0 :: deviceNode { // led驅動的DeviceNode節點 policy = 2; // policy欄位是驅動服務釋出的策略,在驅動服務管理章節有詳細介紹 priority = 10; // 驅動啟動優先順序(0-200),值越大優先順序越低,建議預設配100,優先順序相同則不保證device的載入順序 preload = 1; // 驅動按需載入欄位 permission = 0777; // 驅動建立裝置節點許可權 moduleName = "HDF_LED"; // 驅動名稱,該欄位的值必須和驅動入口結構的moduleName值一致 serviceName = "hdf_led"; // 驅動對外發布服務的名稱,必須唯一 deviceMatchAttr = "st_stm32mp157_led"; // 驅動私有資料匹配的關鍵字,必須和驅動私有資料配置表中的match_attr值相等 } }
1.4.私有驅動資訊配置
如果驅動有私有配置,則可以新增一個驅動的配置檔案,用來填寫一些驅動的預設配置資訊,HDF框架在載入驅動的時候,會將對應的配置資訊獲取並儲存在HdfDeviceObject 中的property裡面,通過Bind和Init傳遞給驅動,所以我們需要在device\st\bearpi_hm_micro\liteos_a\hdf_config\led\led_config.hcs中新增LED私有配置描述。
root { LedDriverConfig { led_gpio_num = 13; match_attr = "st_stm32mp157_led"; //該欄位的值必須和device_info.hcs中的deviceMatchAttr值一致 } }
1.5.板級配置入口檔案
配置資訊定義之後,需要將該配置檔案新增到板級配置入口檔案device\st\bearpi_hm_micro\liteos_a\hdf_config\hdf.hcs
#include "device_info/device_info.hcs" #include "led/led_config.hcs"
二、點亮LED業務程式碼
2.1.建立目錄
編寫業務時,務必先在./applications/BearPi/BearPi-HM_Micro/samples路徑下新建一個目錄(或一套目錄結構),用於存放業務原始碼檔案.
在samples資料夾下增加my_led_app資料夾,並新建BUILD.gn和my_led_app.c兩個檔案
2.2.編寫業務程式碼
在my_led_app.c中新增以下業務程式碼
#include <fcntl.h> #include <sys/stat.h> #include <sys/ioctl.h> #include <unistd.h> #include <stdio.h> #include "hdf_sbuf.h" #include "hdf_io_service_if.h" #define LED_WRITE_READ 1 #define LED_SERVICE "hdf_led" static int SendEvent(struct HdfIoService *serv, uint8_t eventData) { int ret = 0; struct HdfSBuf *data = HdfSBufObtainDefaultSize(); if (data == NULL) { printf("fail to obtain sbuf data!\r\n"); return 1; } struct HdfSBuf *reply = HdfSBufObtainDefaultSize(); if (reply == NULL) { printf("fail to obtain sbuf reply!\r\n"); ret = HDF_DEV_ERR_NO_MEMORY; goto out; } /* 寫入資料 */ if (!HdfSbufWriteUint8(data, eventData)) { printf("fail to write sbuf!\r\n"); ret = HDF_FAILURE; goto out; } /* 通過Dispatch傳送到驅動 */ ret = serv->dispatcher->Dispatch(&serv->object, LED_WRITE_READ, data, reply); if (ret != HDF_SUCCESS) { printf("fail to send service call!\r\n"); goto out; } int replyData = 0; /* 讀取驅動的回覆資料 */ if (!HdfSbufReadInt32(reply, &replyData)) { printf("fail to get service call reply!\r\n"); ret = HDF_ERR_INVALID_OBJECT; goto out; } printf("\r\nGet reply is: %d\r\n", replyData); out: HdfSBufRecycle(data); HdfSBufRecycle(reply); return ret; } int main(int argc, char **argv) { int i; /* 獲取服務 */ struct HdfIoService *serv = HdfIoServiceBind(LED_SERVICE); if (serv == NULL) { printf("fail to get service %s!\r\n", LED_SERVICE); return HDF_FAILURE; } for (i=0; i < argc; i++) { printf("\r\nArgument %d is %s.\r\n", i, argv[i]); } SendEvent(serv, atoi(argv[1])); HdfIoServiceRecycle(serv); printf("exit"); return HDF_SUCCESS; }
2.3.編寫將構建業務程式碼的BUILD.gn檔案
BUILD.gn檔案由三部分內容(目標、原始檔、標頭檔案路徑)構成,需由開發者完成填寫。以my_led_app為例,需要建立./applications/BearPi/BearPi-HM_Micro/samples/my_led_app/BUILD.gn,並完如下配置
import("//build/lite/config/component/lite_component.gni") HDF_FRAMEWORKS = "//drivers/framework" executable("led_lib") { output_name = "my_led" sources = [ "my_led_app.c", ] include_dirs = [ "$HDF_FRAMEWORKS/ability/sbuf/include", "$HDF_FRAMEWORKS/core/shared/include", "$HDF_FRAMEWORKS/core/host/include", "$HDF_FRAMEWORKS/core/master/include", "$HDF_FRAMEWORKS/include/core", "$HDF_FRAMEWORKS/include/utils", "$HDF_FRAMEWORKS/utils/include", "$HDF_FRAMEWORKS/include/osal", "//drivers/adapter/uhdf/posix/include", "//third_party/bounds_checking_function/include", "//base/hiviewdfx/hilog_lite/interfaces/native/innerkits", ] deps = [ "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared", "//drivers/adapter/uhdf/manager:hdf_core", "//drivers/adapter/uhdf/posix:hdf_posix_osal", ] } lite_component("my_led_app") { features = [ ":led_lib", ] }
- 首先匯入 gni 元件,將原始碼my_led_app.c編譯成led_lib庫檔案
- 輸出的可執行檔名稱由 output_name 定義為my_led
- include_dirs 裡面加入my_led_app.c裡面需要用到的.h的標頭檔案路徑
- deps 裡面加入所依賴的庫。
- 然後將led_lib打包成 lite_component,命名為my_led_app元件。
2.4.新增新元件
修改檔案build/lite/components/applications.json,新增元件my_sample的配置
{ "component": "my_sample", "description": "my samples", "optional": "true", "dirs": [ "applications/BearPi/BearPi-HM_Micro/samples/my_first_app", "applications/BearPi/BearPi-HM_Micro/samples/my_led_app" ], "targets": [ "//applications/BearPi/BearPi-HM_Micro/samples/my_first_app:my_app", "//applications/BearPi/BearPi-HM_Micro/samples/my_led_app:my_led_app" ], "rom": "", "ram": "", "output": [], "adapted_kernel": [ "liteos_a" ], "features": [], "deps": { "components": [], "third_party": [ ] } },
2.5.修改單板配置檔案
修改檔案vendor/bearpi/bearpi_hm_micro/config.json,新增my_sample元件的條目
{ "subsystem": "applications", "components": [ { "component": "my_sample", "features":[] }, { "component": "bearpi_sample_app", "features":[] }, { "component": "bearpi_screensaver_app", "features":[] } ] },
三、編譯
在專案根目錄下執行hb set 設定開發板,只有一個,回車即可
執行編譯命令
hb build -t notest --tee -f
等待編譯完成後,螢幕出現:build success字樣,說明編譯成功。
當編譯完後,可以直接檢視到最終編譯的韌體,具體路徑在: \project\bearpi-hm_micro_small\out\bearpi-hm_micro\bearpi-hm_micro
資料夾結構說明
- OHOS_Image.stm32:系統映象檔案
- rootfs_vfat.img:根檔案系統
- userfs_vfat.img:使用者檔案系統
執行以下三條指令將以上三個檔案複製到applications/BearPi/BearPi-HM_Micro/tools/download_img/kernel/下,以便後續燒錄系統使用
cp out/bearpi_hm_micro/bearpi_hm_micro/OHOS_Image.stm32 applications/BearPi/BearPi-HM_Micro/tools/download_img/kernel/ cp out/bearpi_hm_micro/bearpi_hm_micro/rootfs_vfat.img applications/BearPi/BearPi-HM_Micro/tools/download_img/kernel/ cp out/bearpi_hm_micro/bearpi_hm_micro/userfs_vfat.img applications/BearPi/BearPi-HM_Micro/tools/download_img/kernel/
四、總結
至此,完整的專案工程就開發好了,下一步就是燒錄和運行了。
更多學習內容,請關注IoT物聯網社群,新增華為雲IoT小助手微訊號(hwc-iot),回覆“閱讀”獲取更多資訊。