imx6ul gpio中断接收(代码)

痛定思痛。 2022-12-30 04:59 291阅读 0赞

gpio这里的调试要注意的是管脚的功能配置,需要更改设备树文件;

更改设备树文件:https://blog.csdn.net/cao849861802/article/details/111604277

然后注意gpio的计算比如gpio2_8

(2-1)*32 + 8 = 40

这个计算公式可以算出操作的gpio管脚是哪个。

  1. #include "fcntl.h"
  2. #include "stdio.h"
  3. #include "poll.h"
  4. #include "string.h"
  5. #include "stdlib.h"
  6. #include "unistd.h"
  7. #define GPIO_EXPORT_BASE 118
  8. #define GPIO_KEY_NUMBER 6
  9. void edgfdinit()
  10. {
  11. char buf[64];
  12. int i = 0;
  13. int edgfd;
  14. int len = 0;
  15. int ret ;
  16. for(i = 0; i < GPIO_KEY_NUMBER; i++)
  17. {
  18. memset(buf,0,sizeof(buf));
  19. sprintf(buf,"echo %d > /sys/class/gpio/export",GPIO_EXPORT_BASE + i);
  20. ret = system(buf);
  21. if(ret < 0)
  22. {
  23. printf("system %s failed\r\n",buf);
  24. continue;
  25. }
  26. memset(buf,0,sizeof(buf));
  27. sprintf(buf,"/sys/class/gpio/gpio%d/edge",GPIO_EXPORT_BASE + i);
  28. edgfd = open(buf,O_RDWR);
  29. if(edgfd < 0)
  30. {
  31. printf("open %s failed\r\n",buf);
  32. continue;
  33. }
  34. len = write(edgfd,"falling",strlen("falling"));
  35. if(len < strlen("falling"))
  36. {
  37. printf("write edgfd falling failed GPIO = %d\r\n",i + GPIO_EXPORT_BASE);
  38. close(edgfd);
  39. continue;
  40. }
  41. }
  42. }
  43. void main()
  44. {
  45. int i = 0;
  46. char buf[64];
  47. int ret ;
  48. struct pollfd fds[GPIO_KEY_NUMBER];
  49. printf("gpio task start\r\n");
  50. edgfdinit();
  51. for(i = 0; i < GPIO_KEY_NUMBER; i++)
  52. {
  53. memset(buf,0,sizeof(buf));
  54. sprintf(buf,"/sys/class/gpio/gpio%d/value",GPIO_EXPORT_BASE + i);
  55. fds[i].fd = open(buf,O_RDONLY);
  56. if(fds[i].fd < 0)
  57. {
  58. printf("open fd %s failed\r\n",buf);
  59. continue;
  60. }
  61. fds[i].events = POLLPRI;
  62. }
  63. while(1)
  64. {
  65. ret = poll(fds,GPIO_KEY_NUMBER,-1);
  66. if(ret < 0)
  67. {
  68. printf("poll fds failed \r\n");
  69. return ;
  70. }
  71. for(i = 0; i < GPIO_KEY_NUMBER; i++)
  72. {
  73. if(fds[i].revents & POLLPRI)
  74. {
  75. printf("get fd is %d\r\n",fds[i].fd);
  76. ret = lseek(fds[i].fd,0,SEEK_SET);
  77. if(ret < 0)
  78. {
  79. printf("lseek seek_set failed i = %d\r\n",i);
  80. return ;
  81. }
  82. memset(buf,0,sizeof(buf));
  83. ret = read(fds[i].fd,buf,1);
  84. if(ret < 0)
  85. {
  86. printf("read fds.fd failed\r\n");
  87. return ;
  88. }
  89. printf("the key %d is %s",i,buf);
  90. }
  91. }
  92. }
  93. }

发表评论

表情:
评论列表 (有 0 条评论,291人围观)

还没有评论,来说两句吧...

相关阅读

    相关 imx6ul can调试(代码

    下面是我的代码,然后代码呢实际的使用 需要注意,如果重新设置了can的引脚对应关系,需要重新配置设备树文件; 更改设备树文件:[https://blog.csdn.net/c