RPI_gpiod.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*****************************************************************************
  2. * | File : RPI_GPIOD.c
  3. * | Author : Waveshare team
  4. * | Function : Drive GPIO
  5. * | Info : Read and write gpio
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2023-11-15
  9. * | Info : Basic version
  10. *
  11. #
  12. # Permission is hereby granted, free of charge, to any person obtaining a copy
  13. # of this software and associated documnetation files (the "Software"), to deal
  14. # GPIOD_IN the Software without restriction, including without limitation the rights
  15. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. # copies of the Software, and to permit persons to whom the Software is
  17. # furished to do so, subject to the folGPIOD_LOWing conditions:
  18. #
  19. # The above copyright notice and this permission notice shall be included GPIOD_IN
  20. # all copies or substantial portions of the Software.
  21. #
  22. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. GPIOD_IN NO EVENT SHALL THE
  25. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. # LIABILITY WHETHER GPIOD_IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. # GPIOD_OUT OF OR GPIOD_IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS GPIOD_IN
  28. # THE SOFTWARE.
  29. #
  30. ******************************************************************************/
  31. #include "RPI_gpiod.h"
  32. #include <sys/stat.h>
  33. #include <sys/types.h>
  34. #include <fcntl.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <unistd.h>
  39. #include <gpiod.h>
  40. struct gpiod_chip *gpiochip;
  41. struct gpiod_line *gpioline;
  42. int ret;
  43. int GPIOD_Export()
  44. {
  45. char buffer[NUM_MAXBUF];
  46. FILE *fp;
  47. fp = popen("cat /proc/cpuinfo | grep 'Raspberry Pi 5'", "r");
  48. if (fp == NULL) {
  49. GPIOD_Debug("It is not possible to determine the model of the Raspberry PI\n");
  50. return -1;
  51. }
  52. if(fgets(buffer, sizeof(buffer), fp) != NULL)
  53. {
  54. gpiochip = gpiod_chip_open("/dev/gpiochip4");
  55. if (gpiochip == NULL)
  56. {
  57. GPIOD_Debug( "gpiochip4 Export Failed\n");
  58. return -1;
  59. }
  60. }
  61. else
  62. {
  63. gpiochip = gpiod_chip_open("/dev/gpiochip0");
  64. if (gpiochip == NULL)
  65. {
  66. GPIOD_Debug( "gpiochip0 Export Failed\n");
  67. return -1;
  68. }
  69. }
  70. return 0;
  71. }
  72. int GPIOD_Unexport(int Pin)
  73. {
  74. gpioline = gpiod_chip_get_line(gpiochip, Pin);
  75. if (gpioline == NULL)
  76. {
  77. GPIOD_Debug( "Export Failed: Pin%d\n", Pin);
  78. return -1;
  79. }
  80. gpiod_line_release(gpioline);
  81. GPIOD_Debug( "Unexport: Pin%d\r\n", Pin);
  82. return 0;
  83. }
  84. int GPIOD_Unexport_GPIO(void)
  85. {
  86. gpiod_line_release(gpioline);
  87. gpiod_chip_close(gpiochip);
  88. return 0;
  89. }
  90. int GPIOD_Direction(int Pin, int Dir)
  91. {
  92. gpioline = gpiod_chip_get_line(gpiochip, Pin);
  93. if (gpioline == NULL)
  94. {
  95. GPIOD_Debug( "Export Failed: Pin%d\n", Pin);
  96. return -1;
  97. }
  98. if(Dir == GPIOD_IN)
  99. {
  100. ret = gpiod_line_request_input(gpioline, "gpio");
  101. if (ret != 0)
  102. {
  103. GPIOD_Debug( "Export Failed: Pin%d\n", Pin);
  104. return -1;
  105. }
  106. GPIOD_Debug("Pin%d:intput\r\n", Pin);
  107. }
  108. else
  109. {
  110. ret = gpiod_line_request_output(gpioline, "gpio", 0);
  111. if (ret != 0)
  112. {
  113. GPIOD_Debug( "Export Failed: Pin%d\n", Pin);
  114. return -1;
  115. }
  116. GPIOD_Debug("Pin%d:Output\r\n", Pin);
  117. }
  118. return 0;
  119. }
  120. int GPIOD_Read(int Pin)
  121. {
  122. gpioline = gpiod_chip_get_line(gpiochip, Pin);
  123. if (gpioline == NULL)
  124. {
  125. GPIOD_Debug( "Export Failed: Pin%d\n", Pin);
  126. return -1;
  127. }
  128. ret = gpiod_line_get_value(gpioline);
  129. if (ret < 0)
  130. {
  131. GPIOD_Debug( "failed to read value!\n");
  132. return -1;
  133. }
  134. return(ret);
  135. }
  136. int GPIOD_Write(int Pin, int value)
  137. {
  138. gpioline = gpiod_chip_get_line(gpiochip, Pin);
  139. if (gpioline == NULL)
  140. {
  141. GPIOD_Debug( "Export Failed: Pin%d\n", Pin);
  142. return -1;
  143. }
  144. ret = gpiod_line_set_value(gpioline, value);
  145. if (ret != 0)
  146. {
  147. GPIOD_Debug( "failed to write value! : Pin%d\n", Pin);
  148. return -1;
  149. }
  150. return 0;
  151. }