[文章]【HarmonyOS HiSpark AI Camera试用连载 】第六次情迷-鸿蒙OS的GPIO开发(点灯)

阅读量0
0
0
谁?
我!
是你?
是我!
你终于来了!
我终于来了!
你终究是来了?
我终究是来了!
你来干什么!
我来点亮一个电灯泡!
本文由韦东山的源代码指导,感谢韦神!


驱动编写最开始肯定是控制gpio,查找原理图,发现有个LED0是接在gpio2_3这个引脚上的,果断选择点亮这个灯。
1.led原理图.jpg

从官方文档看,我们要确定gpio的管脚号
2.gpio.jpg
我们的引脚是gpio2_3所以引脚号是2*8+3=19,如此我们就可以开干。

在/vendor/hisi/hi35xx/hi3516dv300/config/device_info路径下,打开device_info.hcs文件,修改其中的内容
  1. platform :: host {
  2.             hostName = "platform_host";
  3.             priority = 50;

  4.             device_led :: device {
  5.                 device0 :: deviceNode {
  6.                     policy = 2;
  7.                     priority = 50;
  8.                     permission = 0644;
  9.                     moduleName = "HDF_PLATFORM_LED";
  10.                     serviceName = "led_service";
  11.                 }
  12.             }
复制代码
在目录/vendor/hisi/hi35xx/hi3516dv300/下新建driver/led文件夹,在led文件夹中新建led_drv.c和Makefile两个文件。

Led_drv.c中内容如下:
  1. #include <stdlib.h>
  2. #include <asm/io.h>
  3. #include <fs/fs.h>
  4. #include <fs_poll_pri.h>
  5. #include <los_queue.h>
  6. #include <poll.h>
  7. #include <user_copy.h>
  8. #include <securec.h>
  9. #include "gpio_if.h"
  10. #include "hdf_device_desc.h"
  11. #include "hdf_log.h"
  12. #include "osal_irq.h"
  13. #include "osal_mem.h"
  14. #include "osal_time.h"


  15. static int g_led_val = 0x87654321;
  16. uint16_t gpio = 19; /*待测试的GPIO管脚号 */

  17. static int led_open(FAR struct file *filep)
  18. {
  19.     HDF_LOGI("%s: called", __func__);
  20.     if (filep == NULL) {
  21.         HDF_LOGE("%s: fliep is null", __func__);
  22.         return HDF_ERR_INVALID_PARAM;
  23.     }
  24.     int32_t ret = GpioSetDir(gpio, GPIO_DIR_OUT);
  25.     if (ret != HDF_SUCCESS) {
  26.         HDF_LOGE("%s: set dir fail! ret:%dn", __func__, ret);
  27.         return ret;
  28.     }
  29.     return HDF_SUCCESS;
  30. }

  31. static int led_close(FAR struct file *filep)
  32. {
  33.     HDF_LOGI("%s: called", __func__);

  34.     if (filep == NULL) {
  35.         HDF_LOGE("%s: fliep is null", __func__);
  36.         return HDF_ERR_INVALID_PARAM;
  37.     }
  38.     GpioWrite(gpio, 0);//置高gpio
  39.     return HDF_SUCCESS;
  40. }

  41. static ssize_t led_read(FAR struct file *filep, FAR char *buffer, size_t buflen)
  42. {
  43.         if (buflen == 4) {
  44.                 LOS_ArchCopyToUser(buffer, &g_led_val, 4);
  45.                 return 4;
  46.         }
  47.         return 0;
  48. }

  49. static ssize_t led_write(FAR struct file *filep, FAR const char *buffer, size_t buflen)
  50. {
  51.     if(buffer[0] == 0)
  52.         GpioWrite(gpio, 0);//置高gpio
  53.     else
  54.         GpioWrite(gpio, 1);//置高gpio
  55.         return 0;
  56. }

  57. static const struct file_operations_vfs g_ledDevOps = {
  58.     .open   = led_open,
  59.     .close  = led_close,
  60.     .read   = led_read,
  61.     .write  = led_write,
  62.     .seek   = NULL,
  63.     .ioctl  = NULL,
  64.     .mmap   = NULL,
  65.     .unlink = NULL,
  66. };


  67. int32_t led_dispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data, struct HdfSBuf *reply)
  68. {
  69.     (void)client;
  70.     (void)cmdId;
  71.     if (data == NULL || reply == NULL) {
  72.         HDF_LOGE("%s: param is null", __func__);
  73.         return HDF_FAILURE;
  74.     }

  75.         if (!HdfSbufWriteInt32(reply, g_led_val))
  76.         {
  77.         HDF_LOGE("%s: reply int32 fail", __func__);
  78.         }
  79.         
  80.     return HDF_SUCCESS;
  81. }

  82. int32_t led_bind(struct HdfDeviceObject *object)
  83. {
  84.     if (object == NULL) {
  85.         HDF_LOGE("%s: param is null", __func__);
  86.         return HDF_ERR_INVALID_PARAM;
  87.     }
  88.     static struct IDeviceIoService service = {
  89.         .object = {0},
  90.         .Dispatch = led_dispatch,
  91.     };
  92.     object->service = &service;
  93.     return HDF_SUCCESS;
  94. }

  95. int led_init(struct HdfDeviceObject *object)
  96. {
  97.     (void)object;
  98.     HDF_LOGI("%s: enter", __func__);
  99.     int ret = register_driver("/dev/led", &g_ledDevOps, 0666, NULL);
  100.     if (ret != 0) {
  101.         HDF_LOGE("%s: register led dev failed, ret %d", __func__, ret);
  102.         return HDF_FAILURE;
  103.     }
  104.     HDF_LOGI("%s: exit succ", __func__);
  105.     return HDF_SUCCESS;

  106. }

  107. struct HdfDriverEntry g_ledDevEntry = {
  108.     .moduleVersion = 1,
  109.     .moduleName = "HDF_PLATFORM_LED",
  110.     .Bind = led_bind,
  111.     .Init = led_init,
  112. };

  113. HDF_INIT(g_ledDevEntry);
  114. Makefile文件内容如下:
  115. include $(LITEOSTOPDIR)/config.mk
  116. include $(LITEOSTOPDIR)/../../drivers/hdf/lite/lite.mk

  117. MODULE_NAME := $(notdir $(shell pwd))

  118. LOCAL_SRCS :=  led_drv.c

  119. LOCAL_FLAGS :=  -I$(LITEOSTOPDIR)/../../drivers/hdf/frameworks/include/osal
  120.                 -I$(LITEOSTOPDIR)/../../drivers/hdf/frameworks/include/core
  121.                 -I$(LITEOSTOPDIR)/../../drivers/hdf/frameworks/ability/***uf/include
  122.                 -I$(LITEOSTOPDIR)/../../drivers/hdf/frameworks/utils/include
  123.                 -I$(LITEOSTOPDIR)/../../drivers/hdf/lite/include/host

  124. LOCAL_CFLAGS += -fstack-protector-strong

  125. include $(HDF_DRIVER)
复制代码
在目录/vendor/huawei/hdf/hdf_vendor.mk中,修改.mk文件,如截图所示
3.截图.jpg
然后编写测试程序

在源码根目录建立myapp文件夹,新建led_test.c文件,文件内容如下
  1. #include "hdf_log.h"
  2. #include "osal_mem.h"
  3. #include "hdf_io_service_if.h"
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <string.h>
  8. #include <unistd.h>


  9. int main(int argc, char **argv)
  10. {

  11.         if (argc != 2)
  12.         {
  13.                 printf("Usage: %s <service | /dev/led>n", argv[0]);
  14.                 return -1;
  15.         }
  16.         int fd = open(argv[1], O_RDWR);
  17.         int val[5] = {0};
  18.         
  19.         if (fd < 0) {
  20.                 printf("can not open %sn", argv[1]);
  21.                 return -1;
  22.         }
  23.         while(1)
  24.         {
  25.                 val[0] = !val[0];
  26.                 printf("led:%dn", val[0]);
  27.                 write(fd,val,5);
  28.                 sleep(1);
  29.         }
  30.         return 0;
  31. }
复制代码
修改配置文件,准备编译

修改根目录下/drivers/hdf/lite/manager/BUILD.gn文件如下
  1. import("//build/lite/config/component/lite_component.gni")

  2. HDF_FRAMEWORKS = "//drivers/hdf/frameworks"

  3. shared_library("hdf_core") {
  4.     sources = [
  5.         "$HDF_FRAMEWORKS/core/shared/src/hdf_io_service.c",
  6.         "$HDF_FRAMEWORKS/ability/***uf/src/hdf_***uf.c",
  7.         "../adapter/syscall/src/hdf_syscall_adapter.c"
  8.     ]

  9.     include_dirs = [
  10.         "../adapter/syscall/include",
  11.         "../adapter/vnode/include",
  12.         "$HDF_FRAMEWORKS/core/shared/include",
  13.         "$HDF_FRAMEWORKS/core/host/include",
  14.         "$HDF_FRAMEWORKS/core/manager/include",
  15.         "$HDF_FRAMEWORKS/ability/***uf/include",
  16.         "$HDF_FRAMEWORKS/include/core",
  17.         "$HDF_FRAMEWORKS/include/utils",
  18.         "$HDF_FRAMEWORKS/utils/include",
  19.         "$HDF_FRAMEWORKS/include/osal",
  20.         "//third_party/bounds_checking_function/include",
  21.     ]

  22.     deps = [
  23.         "//drivers/hdf/lite/adapter/osal/posix:hdf_posix_osal",
  24.         "//third_party/bounds_checking_function:libsec_shared",
  25.     ]

  26.     defines = [
  27.         "__USER__",
  28.     ]

  29.     cflags = [
  30.         "-Wall",
  31.         "-Wextra",
  32.         "-Werror",
  33.         "-fsigned-char",
  34.         "-fno-common",
  35.         "-fno-strict-aliasing",
  36.     ]
  37. }

  38. executable("led_test") {
  39.     sources = [
  40.         "//myapp/led_test.c"
  41.     ]

  42.     include_dirs = [
  43.         "../adapter/syscall/include",
  44.         "../adapter/vnode/include","$HDF_FRAMEWORKS/ability/***uf/include",
  45.         "$HDF_FRAMEWORKS/core/shared/include",
  46.         "$HDF_FRAMEWORKS/core/host/include",
  47.         "$HDF_FRAMEWORKS/core/master/include",
  48.         "$HDF_FRAMEWORKS/include/core",
  49.         "$HDF_FRAMEWORKS/include/utils",
  50.         "$HDF_FRAMEWORKS/utils/include",
  51.         "$HDF_FRAMEWORKS/include/osal",
  52.         "//third_party/bounds_checking_function/include",
  53.     ]

  54.     deps = [
  55.         "//drivers/hdf/lite/manager:hdf_core",
  56.         "//drivers/hdf/lite/adapter/osal/posix:hdf_posix_osal",
  57.     ]

  58.     public_deps = [
  59.         "//third_party/bounds_checking_function:libsec_shared",
  60.     ]
  61.     defines = [
  62.         "__USER__",
  63.     ]

  64.     cflags = [
  65.         "-Wall",
  66.         "-Wextra",
  67.         "-Werror",
  68.     ]
  69. }

  70. lite_component("hdf_manager") {
  71.     features = [
  72.         ":hdf_core",
  73.         ":led_test",
  74.     ]
  75. }
复制代码
然后输入编译指令
  1. python build.py ipcamera_hi3516dv300 -b debug
复制代码

下载运行:进入开发板的bin目录,输入指令./led_test ../dev/led,即可看到灯在闪烁
4..jpg

code-1.0-led.zip
(33.68 KB, 下载次数: 4)

led源码

回帖

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容图片侵权或者其他问题,请联系本站作侵删。 侵权投诉
链接复制成功,分享给好友