board_gpio_profile.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #include "board_gpio_profile.h"
  2. #include "esp_log.h"
  3. #include "../../include/defines.h"
  4. static const char *TAG = "GPIO_PROFILE";
  5. #if defined(BOARD_ESP32CAM_AITHINKER)
  6. /*
  7. * AI-Thinker ESP32-CAM:
  8. * - Camera, flash LED, status LED, UART, strapping pins, flash/PSRAM and the
  9. * active SDMMC signals are reserved.
  10. * - The current firmware selects 1-bit SD mode. GPIO13 is the SD DAT3 contact,
  11. * but is not part of the active 1-bit data bus. It remains input-only here:
  12. * no output or PWM capability is granted.
  13. * - If the build is changed to 4-bit SD mode, the user whitelist is empty.
  14. */
  15. #if defined(__SD_USE_ONE_LINE_MODE__)
  16. static const GpioBoardPin kUserPins[] = {
  17. { GPIO_NUM_13,
  18. static_cast<uint8_t>(GPIO_PIN_CAP_INPUT | GPIO_PIN_CAP_PULL_UP |
  19. GPIO_PIN_CAP_INTERRUPT),
  20. "GPIO13 - input only; SD DAT3 is inactive in 1-bit SD mode" }
  21. };
  22. static const size_t kUserPinCount = sizeof(kUserPins) / sizeof(kUserPins[0]);
  23. #else
  24. static const GpioBoardPin * const kUserPins = NULL;
  25. static const size_t kUserPinCount = 0U;
  26. #endif
  27. static const gpio_num_t kReservedPins[] = {
  28. CAM_PIN_PWDN, CAM_PIN_RESET, CAM_PIN_XCLK, CAM_PIN_SIOD, CAM_PIN_SIOC,
  29. CAM_PIN_D7, CAM_PIN_D6, CAM_PIN_D5, CAM_PIN_D4,
  30. CAM_PIN_D3, CAM_PIN_D2, CAM_PIN_D1, CAM_PIN_D0,
  31. CAM_PIN_VSYNC, CAM_PIN_HREF, CAM_PIN_PCLK,
  32. GPIO_SDCARD_CLK, GPIO_SDCARD_CMD, GPIO_SDCARD_D0,
  33. #if !defined(__SD_USE_ONE_LINE_MODE__)
  34. GPIO_SDCARD_D1, GPIO_SDCARD_D2, GPIO_SDCARD_D3,
  35. #endif
  36. /* Boot straps, UART, SPI flash and module memory connections. */
  37. GPIO_NUM_0, GPIO_NUM_1, GPIO_NUM_2, GPIO_NUM_3, GPIO_NUM_5,
  38. GPIO_NUM_6, GPIO_NUM_7, GPIO_NUM_8, GPIO_NUM_9, GPIO_NUM_10, GPIO_NUM_11,
  39. GPIO_NUM_12, GPIO_NUM_15, GPIO_NUM_16, GPIO_NUM_17,
  40. FLASH_GPIO,
  41. BLINK_GPIO
  42. };
  43. static const GpioBoardProfile kBoardProfile = {
  44. "AI-Thinker ESP32-CAM (1-bit SD)",
  45. kUserPins,
  46. kUserPinCount,
  47. kReservedPins,
  48. sizeof(kReservedPins) / sizeof(kReservedPins[0]),
  49. FLASH_GPIO,
  50. GPIO_MAX_USER_CHANNELS
  51. };
  52. #elif defined(BOARD_WROVER_KIT)
  53. static const gpio_num_t kReservedPins[] = {
  54. CAM_PIN_PWDN, CAM_PIN_RESET, CAM_PIN_XCLK, CAM_PIN_SIOD, CAM_PIN_SIOC,
  55. CAM_PIN_D7, CAM_PIN_D6, CAM_PIN_D5, CAM_PIN_D4,
  56. CAM_PIN_D3, CAM_PIN_D2, CAM_PIN_D1, CAM_PIN_D0,
  57. CAM_PIN_VSYNC, CAM_PIN_HREF, CAM_PIN_PCLK,
  58. GPIO_SDCARD_CLK, GPIO_SDCARD_CMD, GPIO_SDCARD_D0,
  59. GPIO_SDCARD_D1, GPIO_SDCARD_D2, GPIO_SDCARD_D3,
  60. FLASH_GPIO, BLINK_GPIO
  61. };
  62. /* No user GPIOs are enabled until this board's complete wiring is audited. */
  63. static const GpioBoardProfile kBoardProfile = {
  64. "ESP32-WROVER-KIT (GPIO profile not yet audited)",
  65. NULL, 0U,
  66. kReservedPins, sizeof(kReservedPins) / sizeof(kReservedPins[0]),
  67. FLASH_GPIO,
  68. GPIO_MAX_USER_CHANNELS
  69. };
  70. #elif defined(BOARD_M5STACK_PSRAM)
  71. static const gpio_num_t kReservedPins[] = {
  72. CAM_PIN_PWDN, CAM_PIN_RESET, CAM_PIN_XCLK, CAM_PIN_SIOD, CAM_PIN_SIOC,
  73. CAM_PIN_D7, CAM_PIN_D6, CAM_PIN_D5, CAM_PIN_D4,
  74. CAM_PIN_D3, CAM_PIN_D2, CAM_PIN_D1, CAM_PIN_D0,
  75. CAM_PIN_VSYNC, CAM_PIN_HREF, CAM_PIN_PCLK,
  76. FLASH_GPIO, BLINK_GPIO
  77. };
  78. /* No user GPIOs are enabled until this board's complete wiring is audited. */
  79. static const GpioBoardProfile kBoardProfile = {
  80. "M5Stack PSRAM (GPIO profile not yet audited)",
  81. NULL, 0U,
  82. kReservedPins, sizeof(kReservedPins) / sizeof(kReservedPins[0]),
  83. FLASH_GPIO,
  84. GPIO_MAX_USER_CHANNELS
  85. };
  86. #else
  87. /*
  88. * Unknown boards fail closed. A new board must add an explicit profile rather
  89. * than inheriting a generic list of SoC GPIO numbers.
  90. */
  91. static const GpioBoardProfile kBoardProfile = {
  92. "Unconfigured board (no user GPIOs enabled)",
  93. NULL, 0U,
  94. NULL, 0U,
  95. GPIO_NUM_NC,
  96. GPIO_MAX_USER_CHANNELS
  97. };
  98. #endif
  99. const GpioBoardProfile *gpioGetBoardProfile(void)
  100. {
  101. return &kBoardProfile;
  102. }
  103. static bool isValidSocGpio(gpio_num_t gpio)
  104. {
  105. return (static_cast<int>(gpio) >= 0) && GPIO_IS_VALID_GPIO(gpio);
  106. }
  107. bool gpioBoardIsReservedPin(gpio_num_t gpio)
  108. {
  109. if (!isValidSocGpio(gpio)) {
  110. return true;
  111. }
  112. if (gpioBoardIsBuiltInFlashLed(gpio)) {
  113. return true;
  114. }
  115. if (kBoardProfile.reservedPins != NULL) {
  116. for (size_t i = 0; i < kBoardProfile.reservedPinCount; ++i) {
  117. if (kBoardProfile.reservedPins[i] == gpio) {
  118. return true;
  119. }
  120. }
  121. }
  122. return false;
  123. }
  124. const GpioBoardPin *gpioBoardFindUserPin(gpio_num_t gpio)
  125. {
  126. if (!isValidSocGpio(gpio) || gpioBoardIsReservedPin(gpio) ||
  127. kBoardProfile.userPins == NULL) {
  128. return NULL;
  129. }
  130. for (size_t i = 0; i < kBoardProfile.userPinCount; ++i) {
  131. if (kBoardProfile.userPins[i].gpio == gpio) {
  132. return &kBoardProfile.userPins[i];
  133. }
  134. }
  135. return NULL;
  136. }
  137. bool gpioBoardPinSupports(gpio_num_t gpio, uint8_t requiredCapabilities)
  138. {
  139. const GpioBoardPin *pin = gpioBoardFindUserPin(gpio);
  140. if (pin == NULL) {
  141. return false;
  142. }
  143. return (pin->capabilities & requiredCapabilities) == requiredCapabilities;
  144. }
  145. bool gpioBoardIsBuiltInFlashLed(gpio_num_t gpio)
  146. {
  147. return isValidSocGpio(gpio) &&
  148. isValidSocGpio(kBoardProfile.builtInFlashLedPin) &&
  149. gpio == kBoardProfile.builtInFlashLedPin;
  150. }
  151. bool gpioBoardProfileIsValid(void)
  152. {
  153. if (kBoardProfile.name == NULL ||
  154. kBoardProfile.maxUserChannels == 0U ||
  155. kBoardProfile.maxUserChannels > GPIO_MAX_USER_CHANNELS) {
  156. ESP_LOGE(TAG, "Invalid board GPIO profile metadata");
  157. return false;
  158. }
  159. if (kBoardProfile.userPinCount > 0U && kBoardProfile.userPins == NULL) {
  160. ESP_LOGE(TAG, "GPIO profile '%s' has a null user pin list", kBoardProfile.name);
  161. return false;
  162. }
  163. for (size_t i = 0; i < kBoardProfile.userPinCount; ++i) {
  164. const GpioBoardPin &pin = kBoardProfile.userPins[i];
  165. if (!isValidSocGpio(pin.gpio) || pin.description == NULL ||
  166. pin.capabilities == 0U || gpioBoardIsReservedPin(pin.gpio)) {
  167. ESP_LOGE(TAG, "Invalid or reserved GPIO%d in board profile '%s'",
  168. static_cast<int>(pin.gpio), kBoardProfile.name);
  169. return false;
  170. }
  171. for (size_t j = i + 1U; j < kBoardProfile.userPinCount; ++j) {
  172. if (pin.gpio == kBoardProfile.userPins[j].gpio) {
  173. ESP_LOGE(TAG, "Duplicate GPIO%d in board profile '%s'",
  174. static_cast<int>(pin.gpio), kBoardProfile.name);
  175. return false;
  176. }
  177. }
  178. }
  179. ESP_LOGI(TAG, "Using board GPIO profile '%s': %u user channels maximum, %u pins whitelisted",
  180. kBoardProfile.name,
  181. static_cast<unsigned int>(kBoardProfile.maxUserChannels),
  182. static_cast<unsigned int>(kBoardProfile.userPinCount));
  183. return true;
  184. }