board_gpio_profile.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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_ESP32_S3_ALEKSEI)
  46. /*
  47. * Reference mapping taken from allexoK/AI-on-the-edge-device,
  48. * branch esp32s3-test, BOARD_ESP32_S3_ALEKSEI.
  49. *
  50. * The build enables Octal PSRAM. Camera, SDMMC, W5500, UART0, battery ADC,
  51. * power-control rails and the board LEDs account for the remaining GPIOs.
  52. * GPIO12 is the on-board four-LED WS2812 chain; camera D4 is GPIO47.
  53. *
  54. * No general-purpose user pin is exposed. This is a conservative software
  55. * profile for the dedicated board, not proof that a different ESP32-S3
  56. * carrier or module revision has the same wiring.
  57. */
  58. static const gpio_num_t kReservedPins[] = {
  59. /* Strapping pins, battery ADC, UART / I2C and camera signals. */
  60. GPIO_NUM_0, GPIO_NUM_1, GPIO_NUM_2, GPIO_NUM_3, GPIO_NUM_4,
  61. GPIO_NUM_5, GPIO_NUM_6, GPIO_NUM_7, GPIO_NUM_8, GPIO_NUM_9,
  62. GPIO_NUM_10, GPIO_NUM_11, GPIO_NUM_12, GPIO_NUM_13, GPIO_NUM_14,
  63. GPIO_NUM_15, GPIO_NUM_16, GPIO_NUM_17, GPIO_NUM_18,
  64. /* USB Serial/JTAG and Octal Flash/PSRAM connections. */
  65. GPIO_NUM_19, GPIO_NUM_20, GPIO_NUM_21,
  66. GPIO_NUM_26, GPIO_NUM_27, GPIO_NUM_28, GPIO_NUM_29,
  67. GPIO_NUM_30, GPIO_NUM_31, GPIO_NUM_32, GPIO_NUM_33,
  68. GPIO_NUM_34, GPIO_NUM_35, GPIO_NUM_36, GPIO_NUM_37,
  69. /* W5500, SDMMC, UART0, power rails, strapping and camera/flash LED. */
  70. GPIO_NUM_38, GPIO_NUM_39, GPIO_NUM_40, GPIO_NUM_41, GPIO_NUM_42,
  71. GPIO_NUM_43, GPIO_NUM_44, GPIO_NUM_45, GPIO_NUM_46,
  72. GPIO_NUM_47, GPIO_NUM_48
  73. };
  74. static const GpioBoardProfile kBoardProfile = {
  75. "allexoK ESP32-S3-POE-A-CAM+ (Octal PSRAM, no user GPIOs)",
  76. NULL, 0U,
  77. kReservedPins, sizeof(kReservedPins) / sizeof(kReservedPins[0]),
  78. GPIO_NUM_48,
  79. GPIO_MAX_USER_CHANNELS
  80. };
  81. #elif defined(BOARD_FREENOVE_ESP32S3)
  82. /*
  83. * Freenove ESP32-S3-CAM mapping supplied for audit:
  84. * camera, SDMMC, UART and on-board LEDs are reserved. GPIO21 is assigned
  85. * to the legacy generic GPIO handler, but no carrier schematic or complete
  86. * peripheral audit was supplied to prove it is user-safe. Fail closed.
  87. */
  88. static const gpio_num_t kReservedPins[] = {
  89. GPIO_NUM_0, GPIO_NUM_3, GPIO_NUM_4, GPIO_NUM_5, GPIO_NUM_6, GPIO_NUM_7,
  90. GPIO_NUM_8, GPIO_NUM_9, GPIO_NUM_10, GPIO_NUM_11, GPIO_NUM_12,
  91. GPIO_NUM_13, GPIO_NUM_14, GPIO_NUM_15, GPIO_NUM_16, GPIO_NUM_17,
  92. GPIO_NUM_18, GPIO_NUM_19, GPIO_NUM_20, GPIO_NUM_26, GPIO_NUM_27,
  93. GPIO_NUM_28, GPIO_NUM_29, GPIO_NUM_30, GPIO_NUM_31, GPIO_NUM_32,
  94. GPIO_NUM_33, GPIO_NUM_34, GPIO_NUM_35, GPIO_NUM_36, GPIO_NUM_37,
  95. GPIO_NUM_38, GPIO_NUM_39, GPIO_NUM_40, GPIO_NUM_41, GPIO_NUM_43,
  96. GPIO_NUM_44, GPIO_NUM_45, GPIO_NUM_46, GPIO_NUM_47, GPIO_NUM_48,
  97. GPIO_NUM_2, GPIO_NUM_21
  98. };
  99. static const GpioBoardProfile kBoardProfile = {
  100. "Freenove ESP32-S3-CAM (GPIO profile not yet audited)",
  101. NULL, 0U,
  102. kReservedPins, sizeof(kReservedPins) / sizeof(kReservedPins[0]),
  103. GPIO_NUM_48,
  104. GPIO_MAX_USER_CHANNELS
  105. };
  106. #elif defined(BOARD_ESP32_S3_ETH_V1) || defined(BOARD_ESP32_S3_ETH_V2)
  107. /*
  108. * ESP32-S3-ETH V1/V2 mapping supplied for audit:
  109. * camera, SDMMC, UART, W5500 and board LEDs are reserved. Legacy GPIO
  110. * handler pins 33-36 are not whitelisted: module flash/PSRAM wiring must
  111. * be confirmed for the exact module variant before any pin is released.
  112. */
  113. static const gpio_num_t kReservedPins[] = {
  114. GPIO_NUM_0, GPIO_NUM_3, GPIO_NUM_4, GPIO_NUM_5, GPIO_NUM_6, GPIO_NUM_7,
  115. GPIO_NUM_8, GPIO_NUM_9, GPIO_NUM_10, GPIO_NUM_11, GPIO_NUM_12,
  116. GPIO_NUM_13, GPIO_NUM_14, GPIO_NUM_15, GPIO_NUM_16, GPIO_NUM_17,
  117. GPIO_NUM_18, GPIO_NUM_19, GPIO_NUM_20, GPIO_NUM_21, GPIO_NUM_26,
  118. GPIO_NUM_27, GPIO_NUM_28, GPIO_NUM_29, GPIO_NUM_30, GPIO_NUM_31,
  119. GPIO_NUM_32, GPIO_NUM_33, GPIO_NUM_34, GPIO_NUM_35, GPIO_NUM_36,
  120. GPIO_NUM_37, GPIO_NUM_38, GPIO_NUM_39, GPIO_NUM_40, GPIO_NUM_41,
  121. GPIO_NUM_42, GPIO_NUM_43, GPIO_NUM_44, GPIO_NUM_45, GPIO_NUM_46,
  122. GPIO_NUM_47, GPIO_NUM_48, GPIO_NUM_1
  123. };
  124. static const GpioBoardProfile kBoardProfile = {
  125. "ESP32-S3-ETH V1/V2 (GPIO profile not yet audited)",
  126. NULL, 0U,
  127. kReservedPins, sizeof(kReservedPins) / sizeof(kReservedPins[0]),
  128. GPIO_NUM_NC,
  129. GPIO_MAX_USER_CHANNELS
  130. };
  131. #elif defined(BOARD_WROVER_KIT)
  132. static const gpio_num_t kReservedPins[] = {
  133. CAM_PIN_PWDN, CAM_PIN_RESET, CAM_PIN_XCLK, CAM_PIN_SIOD, CAM_PIN_SIOC,
  134. CAM_PIN_D7, CAM_PIN_D6, CAM_PIN_D5, CAM_PIN_D4,
  135. CAM_PIN_D3, CAM_PIN_D2, CAM_PIN_D1, CAM_PIN_D0,
  136. CAM_PIN_VSYNC, CAM_PIN_HREF, CAM_PIN_PCLK,
  137. GPIO_SDCARD_CLK, GPIO_SDCARD_CMD, GPIO_SDCARD_D0,
  138. GPIO_SDCARD_D1, GPIO_SDCARD_D2, GPIO_SDCARD_D3,
  139. FLASH_GPIO, BLINK_GPIO
  140. };
  141. /* No user GPIOs are enabled until this board's complete wiring is audited. */
  142. static const GpioBoardProfile kBoardProfile = {
  143. "ESP32-WROVER-KIT (GPIO profile not yet audited)",
  144. NULL, 0U,
  145. kReservedPins, sizeof(kReservedPins) / sizeof(kReservedPins[0]),
  146. FLASH_GPIO,
  147. GPIO_MAX_USER_CHANNELS
  148. };
  149. #elif defined(BOARD_M5STACK_PSRAM)
  150. static const gpio_num_t kReservedPins[] = {
  151. CAM_PIN_PWDN, CAM_PIN_RESET, CAM_PIN_XCLK, CAM_PIN_SIOD, CAM_PIN_SIOC,
  152. CAM_PIN_D7, CAM_PIN_D6, CAM_PIN_D5, CAM_PIN_D4,
  153. CAM_PIN_D3, CAM_PIN_D2, CAM_PIN_D1, CAM_PIN_D0,
  154. CAM_PIN_VSYNC, CAM_PIN_HREF, CAM_PIN_PCLK,
  155. FLASH_GPIO, BLINK_GPIO
  156. };
  157. /* No user GPIOs are enabled until this board's complete wiring is audited. */
  158. static const GpioBoardProfile kBoardProfile = {
  159. "M5Stack PSRAM (GPIO profile not yet audited)",
  160. NULL, 0U,
  161. kReservedPins, sizeof(kReservedPins) / sizeof(kReservedPins[0]),
  162. FLASH_GPIO,
  163. GPIO_MAX_USER_CHANNELS
  164. };
  165. #else
  166. /*
  167. * Unknown boards fail closed. A new board must add an explicit profile rather
  168. * than inheriting a generic list of SoC GPIO numbers.
  169. */
  170. static const GpioBoardProfile kBoardProfile = {
  171. "Unconfigured board (no user GPIOs enabled)",
  172. NULL, 0U,
  173. NULL, 0U,
  174. GPIO_NUM_NC,
  175. GPIO_MAX_USER_CHANNELS
  176. };
  177. #endif
  178. const GpioBoardProfile *gpioGetBoardProfile(void)
  179. {
  180. return &kBoardProfile;
  181. }
  182. static bool isValidSocGpio(gpio_num_t gpio)
  183. {
  184. return (static_cast<int>(gpio) >= 0) && GPIO_IS_VALID_GPIO(gpio);
  185. }
  186. bool gpioBoardIsReservedPin(gpio_num_t gpio)
  187. {
  188. if (!isValidSocGpio(gpio)) {
  189. return true;
  190. }
  191. if (gpioBoardIsBuiltInFlashLed(gpio)) {
  192. return true;
  193. }
  194. if (kBoardProfile.reservedPins != NULL) {
  195. for (size_t i = 0; i < kBoardProfile.reservedPinCount; ++i) {
  196. if (kBoardProfile.reservedPins[i] == gpio) {
  197. return true;
  198. }
  199. }
  200. }
  201. return false;
  202. }
  203. const GpioBoardPin *gpioBoardFindUserPin(gpio_num_t gpio)
  204. {
  205. if (!isValidSocGpio(gpio) || gpioBoardIsReservedPin(gpio) ||
  206. kBoardProfile.userPins == NULL) {
  207. return NULL;
  208. }
  209. for (size_t i = 0; i < kBoardProfile.userPinCount; ++i) {
  210. if (kBoardProfile.userPins[i].gpio == gpio) {
  211. return &kBoardProfile.userPins[i];
  212. }
  213. }
  214. return NULL;
  215. }
  216. bool gpioBoardPinSupports(gpio_num_t gpio, uint8_t requiredCapabilities)
  217. {
  218. const GpioBoardPin *pin = gpioBoardFindUserPin(gpio);
  219. if (pin == NULL) {
  220. return false;
  221. }
  222. return (pin->capabilities & requiredCapabilities) == requiredCapabilities;
  223. }
  224. bool gpioBoardIsBuiltInFlashLed(gpio_num_t gpio)
  225. {
  226. return isValidSocGpio(gpio) &&
  227. isValidSocGpio(kBoardProfile.builtInFlashLedPin) &&
  228. gpio == kBoardProfile.builtInFlashLedPin;
  229. }
  230. bool gpioBoardProfileIsValid(void)
  231. {
  232. if (kBoardProfile.name == NULL ||
  233. kBoardProfile.maxUserChannels == 0U ||
  234. kBoardProfile.maxUserChannels > GPIO_MAX_USER_CHANNELS) {
  235. ESP_LOGE(TAG, "Invalid board GPIO profile metadata");
  236. return false;
  237. }
  238. if (kBoardProfile.userPinCount > 0U && kBoardProfile.userPins == NULL) {
  239. ESP_LOGE(TAG, "GPIO profile '%s' has a null user pin list", kBoardProfile.name);
  240. return false;
  241. }
  242. for (size_t i = 0; i < kBoardProfile.userPinCount; ++i) {
  243. const GpioBoardPin &pin = kBoardProfile.userPins[i];
  244. if (!isValidSocGpio(pin.gpio) || pin.description == NULL ||
  245. pin.capabilities == 0U || gpioBoardIsReservedPin(pin.gpio)) {
  246. ESP_LOGE(TAG, "Invalid or reserved GPIO%d in board profile '%s'",
  247. static_cast<int>(pin.gpio), kBoardProfile.name);
  248. return false;
  249. }
  250. for (size_t j = i + 1U; j < kBoardProfile.userPinCount; ++j) {
  251. if (pin.gpio == kBoardProfile.userPins[j].gpio) {
  252. ESP_LOGE(TAG, "Duplicate GPIO%d in board profile '%s'",
  253. static_cast<int>(pin.gpio), kBoardProfile.name);
  254. return false;
  255. }
  256. }
  257. }
  258. ESP_LOGI(TAG, "Using board GPIO profile '%s': %u user channels maximum, %u pins whitelisted",
  259. kBoardProfile.name,
  260. static_cast<unsigned int>(kBoardProfile.maxUserChannels),
  261. static_cast<unsigned int>(kBoardProfile.userPinCount));
  262. return true;
  263. }