board_gpio_profile.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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_POE_A_CAM_V1)
  46. /*
  47. * allexoK Esp32-Stick-PoE-A-Cam V1 / schematic Rev. 5.0:
  48. * - Camera: GPIO4-18 (including camera SCCB on GPIO4/5).
  49. * - W5500: GPIO1 (MOSI), GPIO2 (CS), GPIO14 (MISO), GPIO21 (SCLK),
  50. * GPIO38 (INT), GPIO39 (RESET).
  51. * - GPIO2 is also the on-board status LED.
  52. * - GPIO0 is the BOOT strap/button; GPIO3, GPIO45 and GPIO46 are S3 straps.
  53. * - GPIO19/20 are reserved for USB Serial/JTAG; GPIO39-42 for JTAG.
  54. * - GPIO26-37 are withheld for flash/PSRAM, including octal-memory module pins.
  55. * - GPIO43/44 are UART0 TX/RX.
  56. *
  57. * The V1 Rev. 5.0 schematic routes GPIO47 and GPIO48 to H4 and does not
  58. * assign them to another on-board peripheral. They are the only pins
  59. * currently whitelisted. The later CAM+ design is a separate profile because
  60. * it adds SD, WS2812B LEDs and battery/power-control circuitry.
  61. */
  62. static const GpioBoardPin kUserPins[] = {
  63. {
  64. GPIO_NUM_47,
  65. GPIO_PIN_CAP_INPUT | GPIO_PIN_CAP_OUTPUT |
  66. GPIO_PIN_CAP_PULL_UP | GPIO_PIN_CAP_PULL_DOWN |
  67. GPIO_PIN_CAP_INTERRUPT,
  68. "H4 pin 12 (GPIO47)"
  69. },
  70. {
  71. GPIO_NUM_48,
  72. GPIO_PIN_CAP_INPUT | GPIO_PIN_CAP_OUTPUT |
  73. GPIO_PIN_CAP_PULL_UP | GPIO_PIN_CAP_PULL_DOWN |
  74. GPIO_PIN_CAP_INTERRUPT,
  75. "H4 pin 11 (GPIO48)"
  76. }
  77. };
  78. static const gpio_num_t kReservedPins[] = {
  79. GPIO_NUM_0, GPIO_NUM_1, GPIO_NUM_2, GPIO_NUM_3,
  80. GPIO_NUM_4, GPIO_NUM_5, GPIO_NUM_6, GPIO_NUM_7, GPIO_NUM_8,
  81. GPIO_NUM_9, GPIO_NUM_10, GPIO_NUM_11, GPIO_NUM_12, GPIO_NUM_13,
  82. GPIO_NUM_14, GPIO_NUM_15, GPIO_NUM_16, GPIO_NUM_17, GPIO_NUM_18,
  83. GPIO_NUM_19, GPIO_NUM_20, GPIO_NUM_21,
  84. GPIO_NUM_26, GPIO_NUM_27, GPIO_NUM_28, GPIO_NUM_29, GPIO_NUM_30,
  85. GPIO_NUM_31, GPIO_NUM_32, GPIO_NUM_33, GPIO_NUM_34, GPIO_NUM_35,
  86. GPIO_NUM_36, GPIO_NUM_37, GPIO_NUM_38, GPIO_NUM_39, GPIO_NUM_40,
  87. GPIO_NUM_41, GPIO_NUM_42, GPIO_NUM_43, GPIO_NUM_44, GPIO_NUM_45,
  88. GPIO_NUM_46
  89. };
  90. static const GpioBoardProfile kBoardProfile = {
  91. "allexoK ESP32-Stick-PoE-A-Cam V1 (Rev 5.0, N16R8)",
  92. kUserPins,
  93. sizeof(kUserPins) / sizeof(kUserPins[0]),
  94. kReservedPins,
  95. sizeof(kReservedPins) / sizeof(kReservedPins[0]),
  96. GPIO_NUM_2,
  97. GPIO_MAX_USER_CHANNELS
  98. };
  99. #elif defined(BOARD_FREENOVE_ESP32S3)
  100. /*
  101. * Freenove ESP32-S3-CAM mapping supplied for audit:
  102. * camera, SDMMC, UART and on-board LEDs are reserved. GPIO21 is assigned
  103. * to the legacy generic GPIO handler, but no carrier schematic or complete
  104. * peripheral audit was supplied to prove it is user-safe. Fail closed.
  105. */
  106. static const gpio_num_t kReservedPins[] = {
  107. GPIO_NUM_0, GPIO_NUM_3, GPIO_NUM_4, GPIO_NUM_5, GPIO_NUM_6, GPIO_NUM_7,
  108. GPIO_NUM_8, GPIO_NUM_9, GPIO_NUM_10, GPIO_NUM_11, GPIO_NUM_12,
  109. GPIO_NUM_13, GPIO_NUM_14, GPIO_NUM_15, GPIO_NUM_16, GPIO_NUM_17,
  110. GPIO_NUM_18, GPIO_NUM_19, GPIO_NUM_20, GPIO_NUM_26, GPIO_NUM_27,
  111. GPIO_NUM_28, GPIO_NUM_29, GPIO_NUM_30, GPIO_NUM_31, GPIO_NUM_32,
  112. GPIO_NUM_33, GPIO_NUM_34, GPIO_NUM_35, GPIO_NUM_36, GPIO_NUM_37,
  113. GPIO_NUM_38, GPIO_NUM_39, GPIO_NUM_40, GPIO_NUM_41, GPIO_NUM_43,
  114. GPIO_NUM_44, GPIO_NUM_45, GPIO_NUM_46, GPIO_NUM_47, GPIO_NUM_48,
  115. GPIO_NUM_2, GPIO_NUM_21
  116. };
  117. static const GpioBoardProfile kBoardProfile = {
  118. "Freenove ESP32-S3-CAM (GPIO profile not yet audited)",
  119. NULL, 0U,
  120. kReservedPins, sizeof(kReservedPins) / sizeof(kReservedPins[0]),
  121. GPIO_NUM_48,
  122. GPIO_MAX_USER_CHANNELS
  123. };
  124. #elif defined(BOARD_ESP32_S3_ETH_V1) || defined(BOARD_ESP32_S3_ETH_V2)
  125. /*
  126. * ESP32-S3-ETH V1/V2 mapping supplied for audit:
  127. * camera, SDMMC, UART, W5500 and board LEDs are reserved. Legacy GPIO
  128. * handler pins 33-36 are not whitelisted: module flash/PSRAM wiring must
  129. * be confirmed for the exact module variant before any pin is released.
  130. */
  131. static const gpio_num_t kReservedPins[] = {
  132. GPIO_NUM_0, GPIO_NUM_3, GPIO_NUM_4, GPIO_NUM_5, GPIO_NUM_6, GPIO_NUM_7,
  133. GPIO_NUM_8, GPIO_NUM_9, GPIO_NUM_10, GPIO_NUM_11, GPIO_NUM_12,
  134. GPIO_NUM_13, GPIO_NUM_14, GPIO_NUM_15, GPIO_NUM_16, GPIO_NUM_17,
  135. GPIO_NUM_18, GPIO_NUM_19, GPIO_NUM_20, GPIO_NUM_21, GPIO_NUM_26,
  136. GPIO_NUM_27, GPIO_NUM_28, GPIO_NUM_29, GPIO_NUM_30, GPIO_NUM_31,
  137. GPIO_NUM_32, GPIO_NUM_33, GPIO_NUM_34, GPIO_NUM_35, GPIO_NUM_36,
  138. GPIO_NUM_37, GPIO_NUM_38, GPIO_NUM_39, GPIO_NUM_40, GPIO_NUM_41,
  139. GPIO_NUM_42, GPIO_NUM_43, GPIO_NUM_44, GPIO_NUM_45, GPIO_NUM_46,
  140. GPIO_NUM_47, GPIO_NUM_48, GPIO_NUM_1
  141. };
  142. static const GpioBoardProfile kBoardProfile = {
  143. "ESP32-S3-ETH V1/V2 (GPIO profile not yet audited)",
  144. NULL, 0U,
  145. kReservedPins, sizeof(kReservedPins) / sizeof(kReservedPins[0]),
  146. GPIO_NUM_NC,
  147. GPIO_MAX_USER_CHANNELS
  148. };
  149. #elif defined(BOARD_WROVER_KIT)
  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. GPIO_SDCARD_CLK, GPIO_SDCARD_CMD, GPIO_SDCARD_D0,
  156. GPIO_SDCARD_D1, GPIO_SDCARD_D2, GPIO_SDCARD_D3,
  157. FLASH_GPIO, BLINK_GPIO
  158. };
  159. /* No user GPIOs are enabled until this board's complete wiring is audited. */
  160. static const GpioBoardProfile kBoardProfile = {
  161. "ESP32-WROVER-KIT (GPIO profile not yet audited)",
  162. NULL, 0U,
  163. kReservedPins, sizeof(kReservedPins) / sizeof(kReservedPins[0]),
  164. FLASH_GPIO,
  165. GPIO_MAX_USER_CHANNELS
  166. };
  167. #elif defined(BOARD_M5STACK_PSRAM)
  168. static const gpio_num_t kReservedPins[] = {
  169. CAM_PIN_PWDN, CAM_PIN_RESET, CAM_PIN_XCLK, CAM_PIN_SIOD, CAM_PIN_SIOC,
  170. CAM_PIN_D7, CAM_PIN_D6, CAM_PIN_D5, CAM_PIN_D4,
  171. CAM_PIN_D3, CAM_PIN_D2, CAM_PIN_D1, CAM_PIN_D0,
  172. CAM_PIN_VSYNC, CAM_PIN_HREF, CAM_PIN_PCLK,
  173. FLASH_GPIO, BLINK_GPIO
  174. };
  175. /* No user GPIOs are enabled until this board's complete wiring is audited. */
  176. static const GpioBoardProfile kBoardProfile = {
  177. "M5Stack PSRAM (GPIO profile not yet audited)",
  178. NULL, 0U,
  179. kReservedPins, sizeof(kReservedPins) / sizeof(kReservedPins[0]),
  180. FLASH_GPIO,
  181. GPIO_MAX_USER_CHANNELS
  182. };
  183. #else
  184. /*
  185. * Unknown boards fail closed. A new board must add an explicit profile rather
  186. * than inheriting a generic list of SoC GPIO numbers.
  187. */
  188. static const GpioBoardProfile kBoardProfile = {
  189. "Unconfigured board (no user GPIOs enabled)",
  190. NULL, 0U,
  191. NULL, 0U,
  192. GPIO_NUM_NC,
  193. GPIO_MAX_USER_CHANNELS
  194. };
  195. #endif
  196. const GpioBoardProfile *gpioGetBoardProfile(void)
  197. {
  198. return &kBoardProfile;
  199. }
  200. static bool isValidSocGpio(gpio_num_t gpio)
  201. {
  202. return (static_cast<int>(gpio) >= 0) && GPIO_IS_VALID_GPIO(gpio);
  203. }
  204. bool gpioBoardIsReservedPin(gpio_num_t gpio)
  205. {
  206. if (!isValidSocGpio(gpio)) {
  207. return true;
  208. }
  209. if (gpioBoardIsBuiltInFlashLed(gpio)) {
  210. return true;
  211. }
  212. if (kBoardProfile.reservedPins != NULL) {
  213. for (size_t i = 0; i < kBoardProfile.reservedPinCount; ++i) {
  214. if (kBoardProfile.reservedPins[i] == gpio) {
  215. return true;
  216. }
  217. }
  218. }
  219. return false;
  220. }
  221. const GpioBoardPin *gpioBoardFindUserPin(gpio_num_t gpio)
  222. {
  223. if (!isValidSocGpio(gpio) || gpioBoardIsReservedPin(gpio) ||
  224. kBoardProfile.userPins == NULL) {
  225. return NULL;
  226. }
  227. for (size_t i = 0; i < kBoardProfile.userPinCount; ++i) {
  228. if (kBoardProfile.userPins[i].gpio == gpio) {
  229. return &kBoardProfile.userPins[i];
  230. }
  231. }
  232. return NULL;
  233. }
  234. bool gpioBoardPinSupports(gpio_num_t gpio, uint8_t requiredCapabilities)
  235. {
  236. const GpioBoardPin *pin = gpioBoardFindUserPin(gpio);
  237. if (pin == NULL) {
  238. return false;
  239. }
  240. return (pin->capabilities & requiredCapabilities) == requiredCapabilities;
  241. }
  242. bool gpioBoardIsBuiltInFlashLed(gpio_num_t gpio)
  243. {
  244. return isValidSocGpio(gpio) &&
  245. isValidSocGpio(kBoardProfile.builtInFlashLedPin) &&
  246. gpio == kBoardProfile.builtInFlashLedPin;
  247. }
  248. bool gpioBoardProfileIsValid(void)
  249. {
  250. if (kBoardProfile.name == NULL ||
  251. kBoardProfile.maxUserChannels == 0U ||
  252. kBoardProfile.maxUserChannels > GPIO_MAX_USER_CHANNELS) {
  253. ESP_LOGE(TAG, "Invalid board GPIO profile metadata");
  254. return false;
  255. }
  256. if (kBoardProfile.userPinCount > 0U && kBoardProfile.userPins == NULL) {
  257. ESP_LOGE(TAG, "GPIO profile '%s' has a null user pin list", kBoardProfile.name);
  258. return false;
  259. }
  260. for (size_t i = 0; i < kBoardProfile.userPinCount; ++i) {
  261. const GpioBoardPin &pin = kBoardProfile.userPins[i];
  262. if (!isValidSocGpio(pin.gpio) || pin.description == NULL ||
  263. pin.capabilities == 0U || gpioBoardIsReservedPin(pin.gpio)) {
  264. ESP_LOGE(TAG, "Invalid or reserved GPIO%d in board profile '%s'",
  265. static_cast<int>(pin.gpio), kBoardProfile.name);
  266. return false;
  267. }
  268. for (size_t j = i + 1U; j < kBoardProfile.userPinCount; ++j) {
  269. if (pin.gpio == kBoardProfile.userPins[j].gpio) {
  270. ESP_LOGE(TAG, "Duplicate GPIO%d in board profile '%s'",
  271. static_cast<int>(pin.gpio), kBoardProfile.name);
  272. return false;
  273. }
  274. }
  275. }
  276. ESP_LOGI(TAG, "Using board GPIO profile '%s': %u user channels maximum, %u pins whitelisted",
  277. kBoardProfile.name,
  278. static_cast<unsigned int>(kBoardProfile.maxUserChannels),
  279. static_cast<unsigned int>(kBoardProfile.userPinCount));
  280. return true;
  281. }