|
【HarmonyOS HiSpark AI Camera試用連載 】第六次情迷-鴻蒙OS的GPIO開發(fā)(點(diǎn)燈), 誰? 我! 是你? 是我! 你終于來了! 我終于來了! 你終究是來了? 我終究是來了! 你來干什么! 我來點(diǎn)亮一個(gè)電燈泡! 本文由韋東山的源代碼指導(dǎo),感謝韋神!
本文根據(jù)官方文檔參考https://device.harmonyos.com/cn/ ... ad-0000001051276785
驅(qū)動(dòng)編寫最開始肯定是控制gpio,查找原理圖,發(fā)現(xiàn)有個(gè)LED0是接在gpio2_3這個(gè)引腳上的,果斷選擇點(diǎn)亮這個(gè)燈。
1.led原理圖.jpg (40.66 KB, 下載次數(shù): 0)
下載附件 保存到相冊(cè)
1 小時(shí)前 上傳
從官方文檔看,我們要確定 gpio 的管腳號(hào)
2.gpio.jpg (22.88 KB, 下載次數(shù): 0)
下載附件 保存到相冊(cè)
1 小時(shí)前 上傳 我們的引腳是gpio2_3所以引腳號(hào)是2*8+3=19,如此我們就可以開干。
在/vendor/hisi/hi35xx/hi3516dv300/config/device_info路徑下,打開device_info.hcs文件,修改其中的內(nèi)容
- platform :: host {
- hostName = “platform_host“;
- priority = 50;
- device_led :: device {
- device0 :: deviceNode {
- policy = 2;
- priority = 50;
- permission = 0644;
- moduleName = “HDF_PLATFORM_LED“;
- serviceName = “l(fā)ed_service“;
- }
- }
-
復(fù)制代碼 在目錄/vendor/hisi/hi35xx/hi3516dv300/下新建driver/led文件夾,在led文件夾中新建led_drv.c和Makefile兩個(gè)文件。
Led_drv.c中內(nèi)容如下:
- #include <stdlib.h>
- #include <asm/io.h>
- #include <fs/fs.h>
- #include <fs_poll_pri.h>
- #include <los_queue.h>
- #include <poll.h>
- #include <user_copy.h>
- #include <securec.h>
- #include “gpio_if.h“
- #include “hdf_device_desc.h“
- #include “hdf_log.h“
- #include “osal_irq.h“
- #include “osal_mem.h“
- #include “osal_time.h“
- static int g_led_val = 0x87654321;
- uint16_t gpio = 19; /*待測(cè)試的GPIO管腳號(hào) */
- static int led_open(FAR struct file *filep)
- {
- HDF_LOGI(“%s: called“, __func__);
- if (filep == NULL) {
- HDF_LOGE(“%s: fliep is null“, __func__);
- return HDF_ERR_INVALID_PARAM;
- }
- int32_t ret = GpioSetDir(gpio, GPIO_DIR_OUT);
- if (ret != HDF_SUCCESS) {
- HDF_LOGE(“%s: set dir fail! ret:%d\n“, __func__, ret);
- return ret;
- }
- return HDF_SUCCESS;
- }
- static int led_close(FAR struct file *filep)
- {
- HDF_LOGI(“%s: called“, __func__);
- if (filep == NULL) {
- HDF_LOGE(“%s: fliep is null“, __func__);
- return HDF_ERR_INVALID_PARAM;
- }
- GpioWrite(gpio, 0);//置高gpio
- return HDF_SUCCESS;
- }
- static ssize_t led_read(FAR struct file *filep, FAR char *buffer, size_t buflen)
- {
- if (buflen == 4) {
- LOS_ArchCopyToUser(buffer, &g_led_val, 4);
- return 4;
- }
- return 0;
- }
- static ssize_t led_write(FAR struct file *filep, FAR const char *buffer, size_t buflen)
- {
- if(buffer[0] == 0)
- GpioWrite(gpio, 0);//置高gpio
- else
- GpioWrite(gpio, 1);//置高gpio
- return 0;
- }
- static const struct file_operations_vfs g_ledDevOps = {
- .open = led_open,
- .close = led_close,
- .read = led_read,
- .write = led_write,
- .seek = NULL,
- .ioctl = NULL,
- .mmap = NULL,
- .unlink = NULL,
- };
- int32_t led_dispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data, struct HdfSBuf *reply)
- {
- (void)client;
- (void)cmdId;
- if (data == NULL || reply == NULL) {
- HDF_LOGE(“%s: param is null“, __func__);
- return HDF_FAILURE;
- }
- if (!HdfSbufWriteInt32(reply, g_led_val))
- {
- HDF_LOGE(“%s: reply int32 fail“, __func__);
- }
-
- return HDF_SUCCESS;
- }
- int32_t led_bind(struct HdfDeviceObject *object)
- {
- if (object == NULL) {
- HDF_LOGE(“%s: param is null“, __func__);
- return HDF_ERR_INVALID_PARAM;
- }
- static struct IDeviceIoService service = {
- .object = {0},
- .Dispatch = led_dispatch,
- };
- object->service = &service;
- return HDF_SUCCESS;
- }
- int led_init(struct HdfDeviceObject *object)
- {
- (void)object;
- HDF_LOGI(“%s: enter“, __func__);
- int ret = register_driver(“/dev/led“, &g_ledDevOps, 0666, NULL);
- if (ret != 0) {
- HDF_LOGE(“%s: register led dev failed, ret %d“, __func__, ret);
- return HDF_FAILURE;
- }
- HDF_LOGI(“%s: exit succ“, __func__);
- return HDF_SUCCESS;
- }
- struct HdfDriverEntry g_ledDevEntry = {
- .moduleVersion = 1,
- .moduleName = “HDF_PLATFORM_LED“,
- .Bind = led_bind,
- .Init = led_init,
- };
- HDF_INIT(g_ledDevEntry);
- Makefile文件內(nèi)容如下:
- include $(LITEOSTOPDIR)/config.mk
- include $(LITEOSTOPDIR)/../../drivers/hdf/lite/lite.mk
- MODULE_NAME := $(notdir $(shell pwd))
- LOCAL_SRCS := led_drv.c
- LOCAL_FLAGS := -I$(LITEOSTOPDIR)/../../drivers/hdf/frameworks/include/osal \
- -I$(LITEOSTOPDIR)/../../drivers/hdf/frameworks/include/core \
- -I$(LITEOSTOPDIR)/../../drivers/hdf/frameworks/ability/sbuf/include \
- -I$(LITEOSTOPDIR)/../../drivers/hdf/frameworks/utils/include \
- -I$(LITEOSTOPDIR)/../../drivers/hdf/lite/include/host
- LOCAL_CFLAGS += -fstack-protector-strong
- include $(HDF_DRIVER)
-
復(fù)制代碼 在目錄/vendor/huawei/hdf/hdf_vendor.mk中,修改.mk文件,如截圖所示
3.截圖.jpg (32.85 KB, 下載次數(shù): 0)
下載附件 保存到相冊(cè)
1 小時(shí)前 上傳 然后編寫測(cè)試程序
在源碼根目錄建立myapp文件夾,新建led_test.c文件,文件內(nèi)容如下
- #include “hdf_log.h“
- #include “osal_mem.h“
- #include “hdf_io_service_if.h“
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <string.h>
- #include <unistd.h>
- int main(int argc, char **argv)
- {
- if (argc != 2)
- {
- printf(“Usage: %s <service | /dev/led>\n“, argv[0]);
- return -1;
- }
- int fd = open(argv[1], O_RDWR);
- int val[5] = {0};
-
- if (fd < 0) {
- printf(“can not open %s\n“, argv[1]);
- return -1;
- }
- while(1)
- {
- val[0] = !val[0];
- printf(“l(fā)ed:%d\n“, val[0]);
- write(fd,val,5);
- sleep(1);
- }
- return 0;
- }
-
復(fù)制代碼 修改配置文件,準(zhǔn)備編譯
修改根目錄下/drivers/hdf/lite/manager/BUILD.gn文件如下
- import(“//build/lite/config/component/lite_component.gni“)
- HDF_FRAMEWORKS = “//drivers/hdf/frameworks“
- shared_library(“hdf_core“) {
- sources = [
- “$HDF_FRAMEWORKS/core/shared/src/hdf_io_service.c“,
- “$HDF_FRAMEWORKS/ability/sbuf/src/hdf_sbuf.c“,
- “../adapter/syscall/src/hdf_syscall_adapter.c“
- ]
- include_dirs = [
- “../adapter/syscall/include“,
- “../adapter/vnode/include“,
- “$HDF_FRAMEWORKS/core/shared/include“,
- “$HDF_FRAMEWORKS/core/host/include“,
- “$HDF_FRAMEWORKS/core/manager/include“,
- “$HDF_FRAMEWORKS/ability/sbuf/include“,
- “$HDF_FRAMEWORKS/include/core“,
- “$HDF_FRAMEWORKS/include/utils“,
- “$HDF_FRAMEWORKS/utils/include“,
- “$HDF_FRAMEWORKS/include/osal“,
- “//third_party/bounds_checking_function/include“,
- ]
- deps = [
- “//drivers/hdf/lite/adapter/osal/posix:hdf_posix_osal“,
- “//third_party/bounds_checking_function:libsec_shared“,
- ]
- defines = [
- “__USER__“,
- ]
- cflags = [
- “-Wall“,
- “-Wextra“,
- “-Werror“,
- “-fsigned-char“,
- “-fno-common“,
- “-fno-strict-aliasing“,
- ]
- }
- executable(“l(fā)ed_test“) {
- sources = [
- “//myapp/led_test.c“
- ]
- include_dirs = [
- “../adapter/syscall/include“,
- “../adapter/vnode/include“,“$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“,
- “//third_party/bounds_checking_function/include“,
- ]
- deps = [
- “//drivers/hdf/lite/manager:hdf_core“,
- “//drivers/hdf/lite/adapter/osal/posix:hdf_posix_osal“,
- ]
- public_deps = [
- “//third_party/bounds_checking_function:libsec_shared“,
- ]
- defines = [
- “__USER__“,
- ]
- cflags = [
- “-Wall“,
- “-Wextra“,
- “-Werror“,
- ]
- }
- lite_component(“hdf_manager“) {
- features = [
- “:hdf_core“,
- “:led_test“,
- ]
- }
-
復(fù)制代碼 然后輸入編譯指令
- python build.py ipcamera_hi3516dv300 -b debug
復(fù)制代碼
下載運(yùn)行:進(jìn)入開發(fā)板的bin目錄,輸入指令./led_test ../dev/led,即可看到燈在閃爍
4..jpg (12.01 KB, 下載次數(shù): 0)
下載附件 保存到相冊(cè)
1 小時(shí)前 上傳 |
|