OpenHarmony-4.基于dayu800 GPIO 实践(2)

打印 上一主题 下一主题

主题 574|帖子 574|积分 1722


  • 基于dayu800 GPIO 举行开发
1.DAYU800开发板硬件接口
  LicheePi 4A 板载 2x10pin 插针,其中有 16 个原生 IO,包罗 6 个平凡 IO,3 对串口,一个 SPI。TH1520 SOC 具有4个GPIO bank,每个bank最大有32个IO:

  以俯视底板正面为视角,TOP为左侧,BOTTOM为右侧,GPIO对应关系如下:

  GPIO 号的对应关系如下图所示:

  从以上可以看出板载20pin插针中,4个平凡GPIO对应的数字分别如下表:

1.2.搭建点灯情况
  给GPIO接灯,利用HiSpark_WiFi_IoT_SSL_VER.A红绿灯板,将红绿灯板和DAYU800开发版按以下方式接线:

  GPIO举行相应的操纵:
  1. #黄灯
  2. echo 428 > /sys/class/gpio/export  
  3. echo out > /sys/class/gpio/gpio428/direction
  4. echo 1 > /sys/class/gpio/gpio428/value  
  5. echo 0 > /sys/class/gpio/gpio428/value
  6. #红灯
  7. echo 429 > /sys/class/gpio/export  
  8. echo out > /sys/class/gpio/gpio429/direction
  9. echo 1 > /sys/class/gpio/gpio429/value  
  10. echo 0 > /sys/class/gpio/gpio429/value
  11. #绿灯
  12. echo 430 > /sys/class/gpio/export  
  13. echo out > /sys/class/gpio/gpio430/direction
  14. echo 1 > /sys/class/gpio/gpio430/value  
复制代码
2.GPIO 代码框架


  • vendor/hihope/dayu800/hdf_config/khdf/device_info/device_info.hcs
  1. 32         platform :: host {
  2. 33             hostName = "platform_host";
  3. 34             priority = 50;
  4. 35             device_gpio :: device {
  5. 36                 device0 :: deviceNode {
  6. 37                     policy = 0;
  7. 38                     priority = 10;
  8. 39                     permission = 0644;
  9. 40                     moduleName = "linux_gpio_adapter";
  10. 41                     deviceMatchAttr = "linux_gpio_adapter";
  11. 42                 }
  12. 43             }
复制代码


  • driver/hdf_core/adapter/khdf/linux/platform/gpio/gpio_adapter.c:
  1. 269 static int32_t LinuxGpioInit(struct HdfDeviceObject *device)
  2. 270 {
  3. 271     if (device == NULL) {
  4. 272         HDF_LOGE("%s: Fail, device is NULL.", __func__);
  5. 273         return HDF_ERR_INVALID_OBJECT;
  6. 274     }
  7. 275
  8. 276     (void)gpiochip_find(device, LinuxGpioMatchProbe);
  9. 277     HDF_LOGI("%s: dev service:%s init done!", __func__, HdfDeviceGetServiceName(device));
  10. 278     return HDF_SUCCESS;
  11. 279 }
  12. 319 struct HdfDriverEntry g_gpioLinuxDriverEntry = {
  13. 320     .moduleVersion = 1,
  14. 321     .Bind = LinuxGpioBind,
  15. 322     .Init = LinuxGpioInit,
  16. 323     .Release = LinuxGpioRelease,
  17. 324     .moduleName = "linux_gpio_adapter",
  18. 325 };
  19. 326 HDF_INIT(g_gpioLinuxDriverEntry);
复制代码
  根据moduleName = "linux_gpio_adapter"举行配置之后,加载gpio hdf 驱动,调用LinuxGpioInit举行初始化,其中调用linux 内核gpiochip_find函数 遍历GPIO设备(gpio_devices)获取gpio控制器(gpio_chip).
  1. 232 static int LinuxGpioMatchProbe(struct gpio_chip *chip, void *data)
  2. 233 {
  3. 234     int32_t ret;
  4. 235     struct GpioCntlr *cntlr = NULL;
  5. 236
  6. 241     HDF_LOGI("%s: find gpio chip(start:%d, count:%u)", __func__, chip->base, chip->ngpio);
  7. 246
  8. 247     cntlr = (struct GpioCntlr *)OsalMemCalloc(sizeof(*cntlr));
  9. 252
  10. 253     cntlr->ops = &g_method;  //gpio 操作集
  11. 254     cntlr->start = (uint16_t)chip->base;
  12. 255     cntlr->count = (uint16_t)chip->ngpio;
  13. 256     ret = GpioCntlrAdd(cntlr);
  14. 263
  15. 264     HDF_LOGI("%s: add gpio controller(start:%d, count:%u) succeed",
  16. 265         __func__, cntlr->start, cntlr->count);
  17. 266     return 0; // return 0 to continue
  18. 267 }
  19. 215 static struct GpioMethod g_method = {
  20. 216     .write = LinuxGpioWrite,
  21. 217     .read = LinuxGpioRead,
  22. 218     .setDir = LinuxGpioSetDir,
  23. 219     .getDir = LinuxGpioGetDir,
  24. 220     .setIrq = LinuxGpioSetIrq,
  25. 221     .unsetIrq = LinuxGpioUnsetIrq,
  26. 222     .enableIrq = LinuxGpioEnableIrq,
  27. 223     .disableIrq = LinuxGpioDisableIrq,
  28. 224 };
复制代码
3.调试GPIO代码
3.1.代码目次结构
  vendor/hihope/dayu800目次下新建sample目次,目次结构如下:
  1. dayu800/dayu800-sig/vendor/hihope/dayu800/sample$ tree
  2. .
  3. ├── BUILD.gn
  4. └── hardware
  5.     ├── BUILD.gn
  6.     └── gpio
  7.         ├── BUILD.gn
  8.         ├── gpio_dayu800.c
  9.         ├── gpio_dayu800.h
  10.         └── main.c
  11. 2 directories, 6 files
复制代码
3.2.增加编译sample
  创建模块目次//vendor/hihope/dayu800/sample,在vendor/hihope/dayu800/ohos.build module_list中添加 模块名称dayu800_sample,如下所示:
  1. diff --git a/dayu800/ohos.build b/dayu800/ohos.build
  2. index cea86ba..d268f2a 100644
  3. --- a/dayu800/ohos.build
  4. +++ b/dayu800/ohos.build
  5. @@ -7,7 +7,8 @@
  6.          "//vendor/hihope/dayu800/preinstall-config:preinstall-config",
  7.          "//vendor/hihope/dayu800/resourceschedule:resourceschedule",
  8.          "//vendor/hihope/dayu800/etc:product_etc_conf",
  9. -        "//vendor/hihope/dayu800/audio:audio_policy_config"
  10. +        "//vendor/hihope/dayu800/audio:audio_policy_config",
  11. +        "//vendor/hihope/dayu800/sample:dayu800_sample"
  12.        ]
  13.      }
  14.    },
复制代码
3.3.新增sample目次BUILD.gn
  添加vendor/hihope/dayu800/sample/BUILD.gn文件,内容如下:
  1.   1 import("//build/ohos.gni")
  2.   2
  3.   3 group("dayu800_sample") {
  4.   4  deps = [
  5.   5   "hardware:hardware"
  6.   6  ]
  7.   7 }
复制代码
  创建名为"dayu800_sample"的group。group的作用是将多个target(可以是源文件、库文件或可实行文件等)组织在一起,方便举行编译和管理。在这个group中,依靠名为"hardware:hardware"的target。这个依靠关系意味着在编译"dayu800_sample"时,需要先编译并链接"hardware:hardware"这个target。通过利用group,可以更方便地管理项目的编译和构建过程。
3.4.新增sample/hardware目次下的BUILD.gn
  新建hardware目次,并添加vendor/hihope/dayu800/sample/hardware/BUILD.gn文件:
  1.   1 import("//build/ohos.gni")
  2.   2
  3.   3 group("hardware") {
  4.   4  deps = [
  5.   5   "gpio:gpio_dayu800"
  6.   6  ]
  7.   7 }
复制代码
  创建名为"hardware"的组(group)。该组依靠于名为"gpio:gpio_dayu800"的依靠项。这个函数的作用是将"hardware"组与"gpio:gpio_dayu800"依靠项相关联。
3.5.新增sample/hardware/gpio目次下的BUILD.gn
  新建gpio目次,并添加vendor/hihope/dayu800/sample/hardware/gpio/BUILD.gn文件,输入以下内容:
  1.   1 import("//build/ohos.gni")
  2.   2 import("//build/ohos/ndk/ndk.gni")
  3.   3
  4.   4 ohos_executable("gpio_dayu800") {
  5.   5  sources = [
  6.   6   "main.c",
  7.   7   "gpio_dayu800.c"
  8.   8  ]
  9.   9
  10. 10  include_dirs = [ "//commonlibrary/c_utils/base/include" ]
  11. 11
  12. 12  external_deps = [
  13. 13   "c_utils:utils",
  14. 14   "hilog_native:libhilog",
  15. 15  ]
  16. 16
  17. 17  install_images = [ "system" ]
  18. 18  part_name = "product_dayu800"
  19. 19 }
复制代码
  定义名为gpio_dayu800的可实行文件目标。该目标包含了两个源文件main.c和gpio_dayu800.c,并指定了包含目次//commonlibrary/c_utils/base/include。该目标依靠于外部库c_utils:utils和hilog_native:libhilog。末了,它指定了将生成的可实行文件安装到system镜像,并将该目标归属于product_dayu800部分。
3.6.新增sample/hardware/gpio目次下文件gpio_dayu800.c和gpio_dayu800.h
  创建vendor/hihope/dayu800/sample/hardware/gpio/gpio_dayu800.c文件,内容如下:
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <signal.h>
  6. #include "hilog/log.h"
  7. #include "securec.h"
  8. #include "gpio_dayu800.h"
  9. int DAYU800_GPIO_Export(int gpioNum, int bExport)
  10. {
  11.     int ret = -1;
  12.     char buffer[256] = {0};
  13.     if (bExport) {
  14.         (void) snprintf_s(buffer, sizeof(buffer), sizeof(buffer), "echo %d > %s", gpioNum, DAYU800_GPIO_EXPORT);
  15.     } else {
  16.         (void) snprintf_s(buffer, sizeof(buffer), sizeof(buffer), "echo %d > %s", gpioNum, DAYU800_GPIO_UNEXPORT);
  17.     }
  18.     sighandler_t old_handler;
  19.     old_handler = signal(SIGCHLD, SIG_DFL);
  20.     ret = system(buffer);
  21.     if (ret < 0) {
  22.         HILOG_ERROR(LOG_CORE, "set gpio%{public}d %{public}s failed", gpioNum, bExport == 1 ? "export" : "unexport");
  23.         return DAYU800_GPIO_ERR;
  24.     }
  25.     (void) signal(SIGCHLD, old_handler);
  26.     return ret;
  27. }
  28. int DAYU800_GPIO_SetDirection(int gpioNum, int direction)
  29. {
  30.     int ret_sprintf_s = -1;
  31.     // check gpio export or not
  32.     char gpio_file_name[128];
  33.     (void) memset_s(gpio_file_name, sizeof(gpio_file_name), 0, sizeof(gpio_file_name));
  34.     ret_sprintf_s = snprintf_s(gpio_file_name, sizeof(gpio_file_name), sizeof(gpio_file_name), "%s%d/direction",
  35.                                DAYU800_GPIO_PEX, gpioNum);
  36.     if (ret_sprintf_s != 0) {
  37.     }
  38.     if (access(gpio_file_name, F_OK) != 0) {
  39.         HILOG_ERROR(LOG_CORE, "gpio%{public}d not export", gpioNum);
  40.         return DAYU800_GPIO_NOT_EXPROT_ERROR;
  41.     }
  42.     // set gpio direction
  43.     FILE *fp = NULL;
  44.     fp = fopen(gpio_file_name, "r+");
  45.     if (fp == NULL) {
  46.         HILOG_ERROR(LOG_CORE, "open %{public}s%{public}d/direction failed", DAYU800_GPIO_PEX, gpioNum);
  47.         return DAYU800_GPIO_ERR;
  48.     }
  49.     if (direction == DAYU800_GPIO_DIRECTION_IN) {
  50.         fprintf(fp, "%s", "in");
  51.     } else if (direction == DAYU800_GPIO_DIRECTION_OUT) {
  52.         fprintf(fp, "%s", "out");
  53.     }
  54.     (void) fclose(fp);
  55.     fp = NULL;
  56.     return 0;
  57. }
  58. int DAYU800_GPIO_SetValue(int gpioNum, int value)
  59. {
  60.     int ret_sprintf_s = -1;
  61.     // check gpio export or not
  62.     char gpio_file_name[128];
  63.     (void) memset_s(gpio_file_name, sizeof(gpio_file_name), 0, sizeof(gpio_file_name));
  64.     ret_sprintf_s = snprintf_s(gpio_file_name, sizeof(gpio_file_name), sizeof(gpio_file_name), "%s%d/value",
  65.                                DAYU800_GPIO_PEX, gpioNum);
  66.     if (ret_sprintf_s != 0) {
  67.     }
  68.     if (access(gpio_file_name, F_OK) != 0) {
  69.         HILOG_ERROR(LOG_CORE, "gpio%{public}d not export", gpioNum);
  70.         return DAYU800_GPIO_NOT_EXPROT_ERROR;
  71.     }
  72.     // set gpio value
  73.     FILE *fp = NULL;
  74.     fp = fopen(gpio_file_name, "r+");
  75.     if (fp == NULL) {
  76.         HILOG_ERROR(LOG_CORE, "open %{public}s%{public}d/value failed", DAYU800_GPIO_PEX, gpioNum);
  77.         return DAYU800_GPIO_ERR;
  78.     }
  79.     if (value == DAYU800_GPIO_LOW_LEVE) {
  80.         fprintf(fp, "%s", "0");
  81.     } else if (value == DAYU800_GPIO_HIGH_LEVE) {
  82.         fprintf(fp, "%s", "1");
  83.     }
  84.     (void) fclose(fp);
  85.     fp = NULL;
  86.     return 0;
  87. }
  88. int DAYU800_GPIO_IsExport(int gpioNum, int *value)
  89. {
  90.     int ret_sprintf_s = -1;
  91.     if (value == NULL) {
  92.         return DAYU800_GPIO_ERR;
  93.     }
  94.     // check gpio export or not
  95.     char gpio_file_name[128];
  96.     (void) memset_s(gpio_file_name, sizeof(gpio_file_name), 0, sizeof(gpio_file_name));
  97.     ret_sprintf_s = snprintf_s(gpio_file_name, sizeof(gpio_file_name), sizeof(gpio_file_name), "%s%d/value",
  98.                                DAYU800_GPIO_PEX, gpioNum);
  99.     if (ret_sprintf_s != 0) {
  100.     }
  101.     if (access(gpio_file_name, F_OK) != 0) {
  102.         HILOG_INFO(LOG_CORE, "gpio%{public}d not export", gpioNum);
  103.         *value = DAYU800_GPIO_NOT_EXPORT;
  104.     } else {
  105.         *value = DAYU800_GPIO_EXPORTED;
  106.     }
  107.     return 0;
  108. }
  109. int DAYU800_GPIO_GetDirection(int gpioNum, int *value)
  110. {
  111.     int ret = 0;
  112.     int ret_sprintf_s = -1;
  113.     if (value == NULL) {
  114.         return DAYU800_GPIO_ERR;
  115.     }
  116.     // check gpio export or not
  117.     char gpio_file_name[128];
  118.     (void) memset_s(gpio_file_name, sizeof(gpio_file_name), 0, sizeof(gpio_file_name));
  119.     ret_sprintf_s = snprintf_s(gpio_file_name, sizeof(gpio_file_name), sizeof(gpio_file_name), "%s%d/direction",
  120.                                DAYU800_GPIO_PEX, gpioNum);
  121.     if (ret_sprintf_s != 0) {
  122.     }
  123.     if (access(gpio_file_name, F_OK) != 0) {
  124.         HILOG_ERROR(LOG_CORE, "gpio%{public}d not export", gpioNum);
  125.         return DAYU800_GPIO_NOT_EXPROT_ERROR;
  126.     }
  127.     // get gpio direction
  128.     FILE *fp = NULL;
  129.     char buffer[20] = {0};
  130.     fp = fopen(gpio_file_name, "r");
  131.     if (fp == NULL) {
  132.         HILOG_ERROR(LOG_CORE, "read %{public}s%{public}d/direction failed", DAYU800_GPIO_PEX, gpioNum);
  133.         return DAYU800_GPIO_ERR;
  134.     }
  135.     (void) fread(buffer, sizeof(buffer), 1, fp);
  136.     (void) fclose(fp);
  137.     fp = NULL;
  138.     if (strstr(buffer, "out") != NULL) {
  139.         *value = DAYU800_GPIO_DIRECTION_OUT;
  140.     } else if (strstr(buffer, "in") != NULL) {
  141.         *value = DAYU800_GPIO_DIRECTION_IN;
  142.     } else {
  143.         ret = DAYU800_GPIO_ERR;
  144.     }
  145.     return ret;
  146. }
  147. int DAYU800_GPIO_GetValue(int gpioNum, int *value)
  148. {
  149.     int ret = 0;
  150.     int ret_sprintf_s = -1;
  151.     if (value == NULL) {
  152.         return DAYU800_GPIO_ERR;
  153.     }
  154.     // check gpio export or not
  155.     char gpio_file_name[128];
  156.     (void) memset_s(gpio_file_name, sizeof(gpio_file_name), 0, sizeof(gpio_file_name));
  157.     ret_sprintf_s = snprintf_s(gpio_file_name, sizeof(gpio_file_name), sizeof(gpio_file_name), "%s%d/value",
  158.                                DAYU800_GPIO_PEX, gpioNum);
  159.     if (ret_sprintf_s != 0) {
  160.     }
  161.     if (access(gpio_file_name, F_OK) != 0) {
  162.         HILOG_ERROR(LOG_CORE, "gpio%{public}d not export", gpioNum);
  163.         return DAYU800_GPIO_NOT_EXPROT_ERROR;
  164.     }
  165.     // get gpio value
  166.     FILE *fp = NULL;
  167.     char buffer[20] = {0};
  168.     fp = fopen(gpio_file_name, "r");
  169.     if (fp == NULL) {
  170.         HILOG_ERROR(LOG_CORE, "read %{public}s%{public}d/value failed", DAYU800_GPIO_PEX, gpioNum);
  171.         return DAYU800_GPIO_ERR;
  172.     }
  173.     (void) fread(buffer, sizeof(buffer), 1, fp);
  174.     (void) fclose(fp);
  175.     fp = NULL;
  176.     if (strstr(buffer, "0") != NULL) {
  177.         *value = DAYU800_GPIO_LOW_LEVE;
  178.     } else if (strstr(buffer, "1") != NULL) {
  179.         *value = DAYU800_GPIO_HIGH_LEVE;
  180.     } else {
  181.         ret = DAYU800_GPIO_ERR;
  182.     }
  183.     return ret;
  184. }
复制代码
  创建vendor/hihope/dayu800/sample/hardware/gpio/gpio_dayu800.h文件,内容如下:
  1. #ifndef __DAYU800_GPIO_H__
  2. #define __DAYU800_GPIO_H__
  3. #define DAYU800_GPIO_EXPORT "/sys/class/gpio/export"
  4. #define DAYU800_GPIO_UNEXPORT "/sys/class/gpio/unexport"
  5. #define DAYU800_GPIO_PEX "/sys/class/gpio/gpio"
  6. // hilog
  7. #undef LOG_DOMAIN
  8. #undef LOG_TAG
  9. #define LOG_DOMAIN 0
  10. #define LOG_TAG "GPIO_DAYU800"
  11. // gpios
  12. #define DAYU800_GPI0_1_3 427 /* IO1_3 */
  13. #define DAYU800_GPI0_1_4 428 /* IO1_4 */
  14. #define DAYU800_GPI0_1_5 429 /* IO1_5 */
  15. #define DAYU800_GPI0_1_6 430 /* IO1_6 */
  16. // direction
  17. #define DAYU800_GPIO_DIRECTION_IN 0
  18. #define DAYU800_GPIO_DIRECTION_OUT 1
  19. // is export
  20. #define DAYU800_GPIO_NOT_EXPORT 0
  21. #define DAYU800_GPIO_EXPORTED 1
  22. // errno
  23. #define DAYU800_GPIO_ERR (-1)
  24. #define DAYU800_GPIO_NOT_EXPROT_ERROR (-2)
  25. // value high - low level
  26. #define DAYU800_GPIO_LOW_LEVE 0
  27. #define DAYU800_GPIO_HIGH_LEVE 1
  28. /**
  29. * set gpio export
  30. * @param gpioNum gpioNum
  31. * @param bExport export,0:not export 1:export
  32. */
  33. int DAYU800_GPIO_Export(int gpioNum, int bExport);
  34. /**
  35. * set gpio direction
  36. * @param gpioNum gpioNum
  37. * @param direction direction,0:in 1:out
  38. */
  39. int DAYU800_GPIO_SetDirection(int gpioNum, int direction);
  40. /**
  41. * set gpio value
  42. * @param gpioNum gpioNum
  43. * @param value value,0:low 1:high
  44. */
  45. int DAYU800_GPIO_SetValue(int gpioNum, int value);
  46. /**
  47. * check gpio export or not
  48. * @param gpioNum gpioNum
  49. * @param *value export,0:not export 1:exported
  50. */
  51. int DAYU800_GPIO_IsExport(int gpioNum, int *value);
  52. /**
  53. * get gpio direction
  54. * @param gpioNum gpioNum
  55. * @param *value direction,0:in 1:out
  56. */
  57. int DAYU800_GPIO_GetDirection(int gpioNum, int *value);
  58. /**
  59. * get gpio value
  60. * @param gpioNum gpioNum
  61. * @param *value value,0:low 1:high
  62. */
  63. int DAYU800_GPIO_GetValue(int gpioNum, int *value);
  64. #endif /* __DAYU800_GPIO_H__ */
复制代码
  以上函数提供了对Dayu800开发板GPIO的控制,包罗导出、设置方向、设置值、查抄导出状态、获取方向和获取值等操纵。用于管理Dayu800GPIO的C函数库。以下是每个函数的功能解释:


  • DAYU800_GPIO_Export 函数用于导出或取消导出GPIO。根据输入的bExport参数,函数将构建一个下令行字符串来实行导出或取消导出操纵。如果操纵乐成,函数返回0,否则返回错误代码。
  • DAYU800_GPIO_SetDirection 函数用于设置GPIO的方向。首先,函数查抄GPIO是否已导出。然后,它打开GPIO的方向文件,并根据输入的direction参数设置为输入或输出。
  • DAYU800_GPIO_SetValue 函数用于设置GPIO的值。首先,函数查抄GPIO是否已导出。然后,它打开GPIO的值文件,并根据输入的value参数设置为低电平或高电平。
  • DAYU800_GPIO_IsExport 函数用于查抄GPIO是否已导出。它构建GPIO的值文件路径并查抄该文件是否存在。根据查抄效果,函数通过value参数返回导出状态。
  • DAYU800_GPIO_GetDirection 函数用于获取GPIO的方向。首先,函数查抄GPIO是否已导出。然后,它打开GPIO的方向文件并读取方向值。根据读取的效果,函数通过value参数返回方向值。
  • DAYU800_GPIO_GetValue 函数用于获取GPIO的值。首先,函数查抄GPIO是否已导出。然后,它打开GPIO的值文件并读取值。根据读取的效果,函数通过value参数返回值。
3.7.新增sample/hardware/gpio目次下的main.c
  创建vendor/hihope/dayu800/sample/hardware/gpio/main.c文件,输入以下内容:
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include "gpio_dayu800.h"
  6. int main(int argc, char **argv)
  7. {
  8.     int gpioNum = DAYU800_GPI0_1_3;
  9.     int bExport = DAYU800_GPIO_EXPORTED;
  10.     int direction = DAYU800_GPIO_DIRECTION_OUT;
  11.     int value = DAYU800_GPIO_HIGH_LEVE;
  12.     int getValue = -1;
  13.     // 检查参数数量以确保至少提供了预期的参数数量
  14.     if (argc < 2) {
  15.         printf("Usage 1: %s <gpioNum> \n", argv[0]);
  16.         printf("Usage 2: %s <gpioNum> <value>\n", argv[0]);
  17.         printf("Usage 3: %s <gpioNum> <value> <direction>\n", argv[0]);
  18.         return DAYU800_GPIO_ERR;
  19.     }
  20.     // 判断是否有输入参数,如有,则赋值指定gpio口
  21.     if (argv[1] != NULL) {
  22.         getValue = atoi(argv[1]);
  23.         if (getValue >= DAYU800_GPI0_1_3 && getValue <= DAYU800_GPI0_1_6) {
  24.             gpioNum = getValue;
  25.         } else {
  26.             printf("please input the gpioNum between 427 and 430.\n");
  27.             return DAYU800_GPIO_ERR;
  28.         }
  29.     }
  30.     // 判断gpio口是否已经导出,如未导出则执行对应函数
  31.     DAYU800_GPIO_IsExport(gpioNum, &getValue);
  32.     if (getValue == DAYU800_GPIO_NOT_EXPORT) {
  33.         DAYU800_GPIO_Export(gpioNum, bExport);
  34.     }
  35.     if (argc == 2) {
  36.         // 设置gpio口值取反
  37.         DAYU800_GPIO_GetValue(gpioNum, &getValue);
  38.         if(getValue == DAYU800_GPIO_LOW_LEVE){
  39.             value = DAYU800_GPIO_HIGH_LEVE;
  40.         }else{
  41.             value = DAYU800_GPIO_LOW_LEVE;
  42.         }
  43.         printf("gpioNum:[%d], curvalue:[%d] setvalue:[%d]\n", gpioNum, getValue,value);
  44.     }
  45.     if (argc >=3 && argc <= 4) {
  46.         if (argv[2] != NULL) {
  47.             //读取GPIO口设定值
  48.             getValue = atoi(argv[2]);
  49.             if (getValue >= DAYU800_GPIO_LOW_LEVE && getValue <= DAYU800_GPIO_HIGH_LEVE) {
  50.                 value = getValue;
  51.             } else {
  52.                 printf("please input the gpio value 0 (low)or 1 (high).\n");
  53.                 return DAYU800_GPIO_ERR;
  54.             }
  55.         }
  56.     }
  57.     if (argc == 4) {
  58.         if (argv[3] != NULL) {
  59.             //读取GPIO口输入或输出设定
  60.             getValue = atoi(argv[3]);
  61.             if (getValue >= DAYU800_GPIO_DIRECTION_IN && getValue <= DAYU800_GPIO_DIRECTION_OUT) {
  62.                 direction = getValue;
  63.             } else {
  64.                 printf("please input the gpio direction 0 (in)or 1 (out).\n");
  65.                 return DAYU800_GPIO_ERR;
  66.             }
  67.         }
  68.     }
  69.     // 设置gpio口为输入或输出模式
  70.     DAYU800_GPIO_SetDirection(gpioNum, direction);
  71.     // 设置gpio口电平高低
  72.     DAYU800_GPIO_SetValue(gpioNum, value);
  73.     // 获取对应gpio口的模式并打印
  74.     DAYU800_GPIO_GetDirection(gpioNum, &getValue);
  75.     printf("gpioNum:[%d], direction:[%d]\n", gpioNum, getValue);
  76.     // 获取对应gpio口的电平值并打印
  77.     DAYU800_GPIO_GetValue(gpioNum, &getValue);
  78.     printf("gpioNum:[%d], Value:[%d]\n", gpioNum, getValue);
  79.     return 0;
  80. }
复制代码
  以上函数实现了是用指令对Dayu800开发板的GPIO引脚的操纵,根据输入参数的数目和值,函数实行不同的操纵,并在终端打印出相应的信息。支持的指令格式如下:
  1. Usage 1: gpio_dayu800 <gpioNum>
  2. Usage 2: gpio_dayu800 <gpioNum> <value>
  3. Usage 3: gpio_dayu800 <gpioNum> <value> <direction>
复制代码
3.8.编译代码
  1. #全量编译
  2. ./build.sh --product-name dayu800 --gn-args full_mini_debug=false --ccache
  3. 编译完成后可以直接烧录out/dayu800/packages/phone/images下生成的档案。
  4. #单模块编译
  5. #前提是之前已全量编译过才可以使用单模块编译指令
  6. ./build.sh --product-name dayu800 --ccache --build-target product_dayu800
  7. 单模块编译后生成bin文件在out/dayu800/product_dayu800/product_dayu800/目录下的gpio_dayu800
复制代码
3.9.hdc 调试
  将 gpio_dayu800推送到开发板,进入hdc工具所在目次,将编译生成的gpio_dayu800拷贝到hdc所在目次,开发板通过Type-C数据线连接到电脑,运行windows自带的“下令提示符”(cmd)窗口
  1. #重新挂载DAYU800开发板的文件系统(以读写权限挂载)
  2. hdc shell mount -o remount,rw /   
  3. #推送到DAYU800开发板/system/bin/目录
  4. hdc file send gpio_dayu800  /system/bin/
复制代码
  在DAYU800开发板上运行测试程序,利用hdc shell指令进入到开发板终端
  1. hdc shell
  2. #接着运行测试指令:
  3. gpio_dayu800 428
  4. gpio_dayu800 429
  5. gpio_dayu800 430
  6. gpio_dayu800 428 0 1  //关灯
  7. gpio_dayu800 428 1 1  //开灯
复制代码
refer to


  • https://blog.csdn.net/lxs_vip/article/details/139391687
  • https://wiki.sipeed.com/hardware/zh/lichee/th1520/lpi4a/6_peripheral.html

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

梦应逍遥

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表