board_gpio_profile.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. #include "board_gpio_profile.h"
  2. #include "esp_log.h"
  3. #include "../../include/defines.h"
  4. #if defined(BOARD_ESP32CAM_AITHINKER) && defined(__SD_USE_ONE_LINE_MODE__)
  5. #include "esp_efuse.h"
  6. #include "esp_efuse_table.h"
  7. /* GPIO12 is safe only when the boot-time MTDI strap is overridden by eFuse
  8. * to force VDD_SDIO to 3.3 V. This is read-only; firmware never burns eFuses. */
  9. static bool gpio12FlashVoltageForcedTo3V3()
  10. {
  11. return esp_efuse_read_field_bit(ESP_EFUSE_XPD_SDIO_FORCE) &&
  12. esp_efuse_read_field_bit(ESP_EFUSE_XPD_SDIO_REG) &&
  13. esp_efuse_read_field_bit(ESP_EFUSE_XPD_SDIO_TIEH);
  14. }
  15. #endif
  16. static const char *TAG = "GPIO_PROFILE";
  17. #if defined(BOARD_ESP32CAM_AITHINKER)
  18. /*
  19. * AI-Thinker ESP32-CAM:
  20. * - The SD card is mounted in 1-bit mode before the generic GPIO handler is
  21. * created. DAT2 and DAT3 are not used by the active host data bus then.
  22. * - GPIO13 is pulled up before card initialization; after a successful mount
  23. * the generic handler may reconfigure DAT3 as an ordinary GPIO.
  24. * - GPIO12 is exposed only when eFuses force VDD_SDIO to 3.3 V. This makes
  25. * MTDI irrelevant to boot-time flash-voltage selection, even if the SD card
  26. * pulls DAT2 high. Without all three force/reg/tieh eFuse bits set, GPIO12
  27. * remains reserved. The firmware only reads eFuses; it never burns them.
  28. * - If the build changes to 4-bit SD mode, neither DAT2 nor DAT3 is exposed.
  29. */
  30. #if defined(__SD_USE_ONE_LINE_MODE__)
  31. static const GpioBoardPin kUserPinList[] = {
  32. { GPIO_NUM_13,
  33. static_cast<uint8_t>(GPIO_PIN_CAP_INPUT | GPIO_PIN_CAP_OUTPUT |
  34. GPIO_PIN_CAP_PULL_UP | GPIO_PIN_CAP_PULL_DOWN |
  35. GPIO_PIN_CAP_INTERRUPT),
  36. "SD DAT3 / GPIO13 (released after SD mount in 1-bit mode)" },
  37. { GPIO_NUM_12,
  38. static_cast<uint8_t>(GPIO_PIN_CAP_INPUT | GPIO_PIN_CAP_OUTPUT |
  39. GPIO_PIN_CAP_PULL_UP | GPIO_PIN_CAP_PULL_DOWN |
  40. GPIO_PIN_CAP_INTERRUPT),
  41. "SD DAT2 / GPIO12 (only with fixed 3.3 V VDD_SDIO eFuses)" }
  42. };
  43. static const GpioBoardPin * const kUserPins = kUserPinList;
  44. static const size_t kUserPinCount = 1U; // GPIO12 is conditionally added at runtime.
  45. #else
  46. static const GpioBoardPin * const kUserPins = NULL;
  47. static const size_t kUserPinCount = 0U;
  48. #endif
  49. static const gpio_num_t kReservedPins[] = {
  50. CAM_PIN_PWDN, CAM_PIN_RESET, CAM_PIN_XCLK, CAM_PIN_SIOD, CAM_PIN_SIOC,
  51. CAM_PIN_D7, CAM_PIN_D6, CAM_PIN_D5, CAM_PIN_D4,
  52. CAM_PIN_D3, CAM_PIN_D2, CAM_PIN_D1, CAM_PIN_D0,
  53. CAM_PIN_VSYNC, CAM_PIN_HREF, CAM_PIN_PCLK,
  54. GPIO_SDCARD_CLK, GPIO_SDCARD_CMD, GPIO_SDCARD_D0,
  55. #if !defined(__SD_USE_ONE_LINE_MODE__)
  56. GPIO_SDCARD_D1, GPIO_SDCARD_D2, GPIO_SDCARD_D3,
  57. #endif
  58. /* Boot straps, UART, SPI flash and module memory connections. */
  59. GPIO_NUM_0, GPIO_NUM_1, GPIO_NUM_2, GPIO_NUM_3, GPIO_NUM_5,
  60. GPIO_NUM_6, GPIO_NUM_7, GPIO_NUM_8, GPIO_NUM_9, GPIO_NUM_10, GPIO_NUM_11,
  61. GPIO_NUM_15, GPIO_NUM_16, GPIO_NUM_17,
  62. FLASH_GPIO,
  63. BLINK_GPIO
  64. };
  65. static const GpioBoardProfile kBoardProfile = {
  66. "AI-Thinker ESP32-CAM (1-bit SD)",
  67. kUserPins,
  68. kUserPinCount,
  69. kReservedPins,
  70. sizeof(kReservedPins) / sizeof(kReservedPins[0]),
  71. FLASH_GPIO,
  72. GPIO_MAX_USER_CHANNELS
  73. };
  74. #elif defined(BOARD_ESP32_S3_ALEKSEI)
  75. /*
  76. * Reference mapping taken from allexoK/AI-on-the-edge-device,
  77. * branch esp32s3-test, BOARD_ESP32_S3_ALEKSEI.
  78. *
  79. * The build enables Octal PSRAM. Camera, SDMMC, W5500, UART0, battery ADC,
  80. * power-control rails and the board LEDs account for the remaining GPIOs.
  81. * GPIO12 is the on-board four-LED WS2812 chain; camera D4 is GPIO47.
  82. *
  83. * No general-purpose user pin is exposed. This is a conservative software
  84. * profile for the dedicated board, not proof that a different ESP32-S3
  85. * carrier or module revision has the same wiring.
  86. */
  87. static const gpio_num_t kReservedPins[] = {
  88. /* Strapping pins, battery ADC, UART / I2C and camera signals. */
  89. GPIO_NUM_0, GPIO_NUM_1, GPIO_NUM_2, GPIO_NUM_3, GPIO_NUM_4,
  90. GPIO_NUM_5, GPIO_NUM_6, GPIO_NUM_7, GPIO_NUM_8, GPIO_NUM_9,
  91. GPIO_NUM_10, GPIO_NUM_11, GPIO_NUM_12, GPIO_NUM_13, GPIO_NUM_14,
  92. GPIO_NUM_15, GPIO_NUM_16, GPIO_NUM_17, GPIO_NUM_18,
  93. /* USB Serial/JTAG and Octal Flash/PSRAM connections. */
  94. GPIO_NUM_19, GPIO_NUM_20, GPIO_NUM_21,
  95. GPIO_NUM_26, GPIO_NUM_27, GPIO_NUM_28, GPIO_NUM_29,
  96. GPIO_NUM_30, GPIO_NUM_31, GPIO_NUM_32, GPIO_NUM_33,
  97. GPIO_NUM_34, GPIO_NUM_35, GPIO_NUM_36, GPIO_NUM_37,
  98. /* W5500, SDMMC, UART0, power rails, strapping and camera/flash LED. */
  99. GPIO_NUM_38, GPIO_NUM_39, GPIO_NUM_40, GPIO_NUM_41, GPIO_NUM_42,
  100. GPIO_NUM_43, GPIO_NUM_44, GPIO_NUM_45, GPIO_NUM_46,
  101. GPIO_NUM_47, GPIO_NUM_48
  102. };
  103. static const GpioBoardProfile kBoardProfile = {
  104. "allexoK ESP32-S3-POE-A-CAM+ (Octal PSRAM, no user GPIOs)",
  105. NULL, 0U,
  106. kReservedPins, sizeof(kReservedPins) / sizeof(kReservedPins[0]),
  107. GPIO_NUM_48,
  108. GPIO_MAX_USER_CHANNELS
  109. };
  110. #elif defined(BOARD_FREENOVE_ESP32S3)
  111. /*
  112. * Freenove ESP32-S3-CAM mapping supplied for audit:
  113. * camera, SDMMC, UART and on-board LEDs are reserved. GPIO21 is assigned
  114. * to the legacy generic GPIO handler, but no carrier schematic or complete
  115. * peripheral audit was supplied to prove it is user-safe. Fail closed.
  116. */
  117. static const gpio_num_t kReservedPins[] = {
  118. GPIO_NUM_0, GPIO_NUM_3, GPIO_NUM_4, GPIO_NUM_5, GPIO_NUM_6, GPIO_NUM_7,
  119. GPIO_NUM_8, GPIO_NUM_9, GPIO_NUM_10, GPIO_NUM_11, GPIO_NUM_12,
  120. GPIO_NUM_13, GPIO_NUM_14, GPIO_NUM_15, GPIO_NUM_16, GPIO_NUM_17,
  121. GPIO_NUM_18, GPIO_NUM_19, GPIO_NUM_20, GPIO_NUM_26, GPIO_NUM_27,
  122. GPIO_NUM_28, GPIO_NUM_29, GPIO_NUM_30, GPIO_NUM_31, GPIO_NUM_32,
  123. GPIO_NUM_33, GPIO_NUM_34, GPIO_NUM_35, GPIO_NUM_36, GPIO_NUM_37,
  124. GPIO_NUM_38, GPIO_NUM_39, GPIO_NUM_40, GPIO_NUM_41, GPIO_NUM_43,
  125. GPIO_NUM_44, GPIO_NUM_45, GPIO_NUM_46, GPIO_NUM_47, GPIO_NUM_48,
  126. GPIO_NUM_2, GPIO_NUM_21
  127. };
  128. static const GpioBoardProfile kBoardProfile = {
  129. "Freenove ESP32-S3-CAM (GPIO profile not yet audited)",
  130. NULL, 0U,
  131. kReservedPins, sizeof(kReservedPins) / sizeof(kReservedPins[0]),
  132. GPIO_NUM_48,
  133. GPIO_MAX_USER_CHANNELS
  134. };
  135. #elif defined(BOARD_ESP32_S3_ETH_V1) || defined(BOARD_ESP32_S3_ETH_V2)
  136. /*
  137. * ESP32-S3-ETH V1/V2 mapping supplied for audit:
  138. * camera, SDMMC, UART, W5500 and board LEDs are reserved. Legacy GPIO
  139. * handler pins 33-36 are not whitelisted: module flash/PSRAM wiring must
  140. * be confirmed for the exact module variant before any pin is released.
  141. */
  142. static const gpio_num_t kReservedPins[] = {
  143. GPIO_NUM_0, GPIO_NUM_3, GPIO_NUM_4, GPIO_NUM_5, GPIO_NUM_6, GPIO_NUM_7,
  144. GPIO_NUM_8, GPIO_NUM_9, GPIO_NUM_10, GPIO_NUM_11, GPIO_NUM_12,
  145. GPIO_NUM_13, GPIO_NUM_14, GPIO_NUM_15, GPIO_NUM_16, GPIO_NUM_17,
  146. GPIO_NUM_18, GPIO_NUM_19, GPIO_NUM_20, GPIO_NUM_21, GPIO_NUM_26,
  147. GPIO_NUM_27, GPIO_NUM_28, GPIO_NUM_29, GPIO_NUM_30, GPIO_NUM_31,
  148. GPIO_NUM_32, GPIO_NUM_33, GPIO_NUM_34, GPIO_NUM_35, GPIO_NUM_36,
  149. GPIO_NUM_37, GPIO_NUM_38, GPIO_NUM_39, GPIO_NUM_40, GPIO_NUM_41,
  150. GPIO_NUM_42, GPIO_NUM_43, GPIO_NUM_44, GPIO_NUM_45, GPIO_NUM_46,
  151. GPIO_NUM_47, GPIO_NUM_48, GPIO_NUM_1
  152. };
  153. static const GpioBoardProfile kBoardProfile = {
  154. "ESP32-S3-ETH V1/V2 (GPIO profile not yet audited)",
  155. NULL, 0U,
  156. kReservedPins, sizeof(kReservedPins) / sizeof(kReservedPins[0]),
  157. GPIO_NUM_NC,
  158. GPIO_MAX_USER_CHANNELS
  159. };
  160. #elif defined(BOARD_WROVER_KIT)
  161. static const gpio_num_t kReservedPins[] = {
  162. CAM_PIN_PWDN, CAM_PIN_RESET, CAM_PIN_XCLK, CAM_PIN_SIOD, CAM_PIN_SIOC,
  163. CAM_PIN_D7, CAM_PIN_D6, CAM_PIN_D5, CAM_PIN_D4,
  164. CAM_PIN_D3, CAM_PIN_D2, CAM_PIN_D1, CAM_PIN_D0,
  165. CAM_PIN_VSYNC, CAM_PIN_HREF, CAM_PIN_PCLK,
  166. GPIO_SDCARD_CLK, GPIO_SDCARD_CMD, GPIO_SDCARD_D0,
  167. GPIO_SDCARD_D1, GPIO_SDCARD_D2, GPIO_SDCARD_D3,
  168. FLASH_GPIO, BLINK_GPIO
  169. };
  170. /* No user GPIOs are enabled until this board's complete wiring is audited. */
  171. static const GpioBoardProfile kBoardProfile = {
  172. "ESP32-WROVER-KIT (GPIO profile not yet audited)",
  173. NULL, 0U,
  174. kReservedPins, sizeof(kReservedPins) / sizeof(kReservedPins[0]),
  175. FLASH_GPIO,
  176. GPIO_MAX_USER_CHANNELS
  177. };
  178. #elif defined(BOARD_M5STACK_PSRAM)
  179. static const gpio_num_t kReservedPins[] = {
  180. CAM_PIN_PWDN, CAM_PIN_RESET, CAM_PIN_XCLK, CAM_PIN_SIOD, CAM_PIN_SIOC,
  181. CAM_PIN_D7, CAM_PIN_D6, CAM_PIN_D5, CAM_PIN_D4,
  182. CAM_PIN_D3, CAM_PIN_D2, CAM_PIN_D1, CAM_PIN_D0,
  183. CAM_PIN_VSYNC, CAM_PIN_HREF, CAM_PIN_PCLK,
  184. FLASH_GPIO, BLINK_GPIO
  185. };
  186. /* No user GPIOs are enabled until this board's complete wiring is audited. */
  187. static const GpioBoardProfile kBoardProfile = {
  188. "M5Stack PSRAM (GPIO profile not yet audited)",
  189. NULL, 0U,
  190. kReservedPins, sizeof(kReservedPins) / sizeof(kReservedPins[0]),
  191. FLASH_GPIO,
  192. GPIO_MAX_USER_CHANNELS
  193. };
  194. #else
  195. /*
  196. * Unknown boards fail closed. A new board must add an explicit profile rather
  197. * than inheriting a generic list of SoC GPIO numbers.
  198. */
  199. static const GpioBoardProfile kBoardProfile = {
  200. "Unconfigured board (no user GPIOs enabled)",
  201. NULL, 0U,
  202. NULL, 0U,
  203. GPIO_NUM_NC,
  204. GPIO_MAX_USER_CHANNELS
  205. };
  206. #endif
  207. const GpioBoardProfile *gpioGetBoardProfile(void)
  208. {
  209. #if defined(BOARD_ESP32CAM_AITHINKER) && defined(__SD_USE_ONE_LINE_MODE__)
  210. static const GpioBoardProfile profile = []() {
  211. GpioBoardProfile selected = kBoardProfile;
  212. if (gpio12FlashVoltageForcedTo3V3()) {
  213. selected.userPinCount = 2U;
  214. selected.name = "AI-Thinker ESP32-CAM (1-bit SD, GPIO12/13 available; fixed 3.3 V eFuse)";
  215. } else {
  216. selected.userPinCount = 1U;
  217. selected.name = "AI-Thinker ESP32-CAM (1-bit SD, GPIO13 available; GPIO12 reserved: VDD_SDIO not forced)";
  218. }
  219. return selected;
  220. }();
  221. return &profile;
  222. #else
  223. return &kBoardProfile;
  224. #endif
  225. }
  226. static bool isValidSocGpio(gpio_num_t gpio)
  227. {
  228. return (static_cast<int>(gpio) >= 0) && GPIO_IS_VALID_GPIO(gpio);
  229. }
  230. bool gpioBoardIsReservedPin(gpio_num_t gpio)
  231. {
  232. if (!isValidSocGpio(gpio)) {
  233. return true;
  234. }
  235. const GpioBoardProfile *profile = gpioGetBoardProfile();
  236. #if defined(BOARD_ESP32CAM_AITHINKER) && defined(__SD_USE_ONE_LINE_MODE__)
  237. if (gpio == GPIO_NUM_12 && profile->userPinCount < 2U) {
  238. return true;
  239. }
  240. #endif
  241. if (gpioBoardIsBuiltInFlashLed(gpio)) {
  242. return true;
  243. }
  244. if (profile->reservedPins != NULL) {
  245. for (size_t i = 0; i < profile->reservedPinCount; ++i) {
  246. if (profile->reservedPins[i] == gpio) {
  247. return true;
  248. }
  249. }
  250. }
  251. return false;
  252. }
  253. const GpioBoardPin *gpioBoardFindUserPin(gpio_num_t gpio)
  254. {
  255. const GpioBoardProfile *profile = gpioGetBoardProfile();
  256. if (!isValidSocGpio(gpio) || gpioBoardIsReservedPin(gpio) ||
  257. profile->userPins == NULL) {
  258. return NULL;
  259. }
  260. for (size_t i = 0; i < profile->userPinCount; ++i) {
  261. if (profile->userPins[i].gpio == gpio) {
  262. return &profile->userPins[i];
  263. }
  264. }
  265. return NULL;
  266. }
  267. bool gpioBoardPinSupports(gpio_num_t gpio, uint8_t requiredCapabilities)
  268. {
  269. const GpioBoardPin *pin = gpioBoardFindUserPin(gpio);
  270. if (pin == NULL) {
  271. return false;
  272. }
  273. return (pin->capabilities & requiredCapabilities) == requiredCapabilities;
  274. }
  275. bool gpioBoardIsBuiltInFlashLed(gpio_num_t gpio)
  276. {
  277. const GpioBoardProfile *profile = gpioGetBoardProfile();
  278. return isValidSocGpio(gpio) &&
  279. isValidSocGpio(profile->builtInFlashLedPin) &&
  280. gpio == profile->builtInFlashLedPin;
  281. }
  282. bool gpioBoardProfileIsValid(void)
  283. {
  284. const GpioBoardProfile *profile = gpioGetBoardProfile();
  285. if (profile->name == NULL ||
  286. profile->maxUserChannels == 0U ||
  287. profile->maxUserChannels > GPIO_MAX_USER_CHANNELS) {
  288. ESP_LOGE(TAG, "Invalid board GPIO profile metadata");
  289. return false;
  290. }
  291. if (profile->userPinCount > 0U && profile->userPins == NULL) {
  292. ESP_LOGE(TAG, "GPIO profile '%s' has a null user pin list", profile->name);
  293. return false;
  294. }
  295. for (size_t i = 0; i < profile->userPinCount; ++i) {
  296. const GpioBoardPin &pin = profile->userPins[i];
  297. if (!isValidSocGpio(pin.gpio) || pin.description == NULL ||
  298. pin.capabilities == 0U || gpioBoardIsReservedPin(pin.gpio)) {
  299. ESP_LOGE(TAG, "Invalid or reserved GPIO%d in board profile '%s'",
  300. static_cast<int>(pin.gpio), profile->name);
  301. return false;
  302. }
  303. for (size_t j = i + 1U; j < profile->userPinCount; ++j) {
  304. if (pin.gpio == profile->userPins[j].gpio) {
  305. ESP_LOGE(TAG, "Duplicate GPIO%d in board profile '%s'",
  306. static_cast<int>(pin.gpio), profile->name);
  307. return false;
  308. }
  309. }
  310. }
  311. ESP_LOGI(TAG, "Using board GPIO profile '%s': %u user channels maximum, %u pins whitelisted",
  312. profile->name,
  313. static_cast<unsigned int>(profile->maxUserChannels),
  314. static_cast<unsigned int>(profile->userPinCount));
  315. return true;
  316. }