|
【HarmonyOS HiSpark AI Camera试用连载 】第六次情迷-鸿蒙OS的GPIO开发(点灯), 谁? 我! 是你? 是我! 你终于来了! 我终于来了! 你终究是来了? 我终究是来了! 你来干什么! 我来点亮一个电灯泡! 本文由韦东山的源代码指导,感谢韦神!
本文根据官方文档参考https://device.harmonyos.com/cn/ ... ad-0000001051276785
驱动编写最开始肯定是控制gpio,查找原理图,发现有个LED0是接在gpio2_3这个引脚上的,果断选择点亮这个灯。
1.led原理图.jpg (40.66 KB, 下载次数: 0)
下载附件 保存到相册
1 小时前 上传
从官方文档看,我们要确定 gpio 的管脚号
2.gpio.jpg (22.88 KB, 下载次数: 0)
下载附件 保存到相册
1 小时前 上传 我们的引脚是gpio2_3所以引脚号是2*8+3=19,如此我们就可以开干。
在/vendor/hisi/hi35xx/hi3516dv300/config/device_info路径下,打开device_info.hcs文件,修改其中的内容
- platform :: host {
- hostName = “platform_host“;
- priority = 50;
- device_led :: device {
- device0 :: deviceNode {
- policy = 2;
- priority = 50;
- permission = 0644;
- moduleName = “HDF_PLATFORM_LED“;
- serviceName = “led_service“;
- }
- }
-
复制代码 在目录/vendor/hisi/hi35xx/hi3516dv300/下新建driver/led文件夹,在led文件夹中新建led_drv.c和Makefile两个文件。
Led_drv.c中内容如下:
- #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; /*待测试的GPIO管脚号 */
- 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文件内容如下:
- 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)
-
复制代码 在目录/vendor/huawei/hdf/hdf_vendor.mk中,修改.mk文件,如截图所示
3.截图.jpg (32.85 KB, 下载次数: 0)
下载附件 保存到相册
1 小时前 上传 然后编写测试程序
在源码根目录建立myapp文件夹,新建led_test.c文件,文件内容如下
- #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(“led:%d\n“, val[0]);
- write(fd,val,5);
- sleep(1);
- }
- return 0;
- }
-
复制代码 修改配置文件,准备编译
修改根目录下/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(“led_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“,
- ]
- }
-
复制代码 然后输入编译指令
- python build.py ipcamera_hi3516dv300 -b debug
复制代码
下载运行:进入开发板的bin目录,输入指令./led_test ../dev/led,即可看到灯在闪烁
4..jpg (12.01 KB, 下载次数: 0)
下载附件 保存到相册
1 小时前 上传 |
|