board_gpio_profile.cpp 6.5 KB

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