#include "board_gpio_profile.h" #include "esp_log.h" #include "../../include/defines.h" #if defined(BOARD_ESP32CAM_AITHINKER) && defined(__SD_USE_ONE_LINE_MODE__) #include "esp_efuse.h" #include "esp_efuse_table.h" /* GPIO12 is safe only when the boot-time MTDI strap is overridden by eFuse * to force VDD_SDIO to 3.3 V. This is read-only; firmware never burns eFuses. */ static bool gpio12FlashVoltageForcedTo3V3() { return esp_efuse_read_field_bit(ESP_EFUSE_XPD_SDIO_FORCE) && esp_efuse_read_field_bit(ESP_EFUSE_XPD_SDIO_REG) && esp_efuse_read_field_bit(ESP_EFUSE_XPD_SDIO_TIEH); } #endif static const char *TAG = "GPIO_PROFILE"; #if defined(BOARD_ESP32CAM_AITHINKER) /* * AI-Thinker ESP32-CAM: * - The SD card is mounted in 1-bit mode before the generic GPIO handler is * created. DAT2 and DAT3 are not used by the active host data bus then. * - GPIO13 is pulled up before card initialization; after a successful mount * the generic handler may reconfigure DAT3 as an ordinary GPIO. * - GPIO12 is exposed only when eFuses force VDD_SDIO to 3.3 V. This makes * MTDI irrelevant to boot-time flash-voltage selection, even if the SD card * pulls DAT2 high. Without all three force/reg/tieh eFuse bits set, GPIO12 * remains reserved. The firmware only reads eFuses; it never burns them. * - If the build changes to 4-bit SD mode, neither DAT2 nor DAT3 is exposed. */ #if defined(__SD_USE_ONE_LINE_MODE__) static const GpioBoardPin kUserPinList[] = { { GPIO_NUM_13, static_cast(GPIO_PIN_CAP_INPUT | GPIO_PIN_CAP_OUTPUT | GPIO_PIN_CAP_PULL_UP | GPIO_PIN_CAP_PULL_DOWN | GPIO_PIN_CAP_INTERRUPT), "SD DAT3 / GPIO13 (released after SD mount in 1-bit mode)" }, { GPIO_NUM_12, static_cast(GPIO_PIN_CAP_INPUT | GPIO_PIN_CAP_OUTPUT | GPIO_PIN_CAP_PULL_UP | GPIO_PIN_CAP_PULL_DOWN | GPIO_PIN_CAP_INTERRUPT), "SD DAT2 / GPIO12 (only with fixed 3.3 V VDD_SDIO eFuses)" } }; static const GpioBoardPin * const kUserPins = kUserPinList; static const size_t kUserPinCount = 1U; // GPIO12 is conditionally added at runtime. #else static const GpioBoardPin * const kUserPins = NULL; static const size_t kUserPinCount = 0U; #endif static const gpio_num_t kReservedPins[] = { CAM_PIN_PWDN, CAM_PIN_RESET, CAM_PIN_XCLK, CAM_PIN_SIOD, CAM_PIN_SIOC, CAM_PIN_D7, CAM_PIN_D6, CAM_PIN_D5, CAM_PIN_D4, CAM_PIN_D3, CAM_PIN_D2, CAM_PIN_D1, CAM_PIN_D0, CAM_PIN_VSYNC, CAM_PIN_HREF, CAM_PIN_PCLK, GPIO_SDCARD_CLK, GPIO_SDCARD_CMD, GPIO_SDCARD_D0, #if !defined(__SD_USE_ONE_LINE_MODE__) GPIO_SDCARD_D1, GPIO_SDCARD_D2, GPIO_SDCARD_D3, #endif /* Boot straps, UART, SPI flash and module memory connections. */ GPIO_NUM_0, GPIO_NUM_1, GPIO_NUM_2, GPIO_NUM_3, GPIO_NUM_5, GPIO_NUM_6, GPIO_NUM_7, GPIO_NUM_8, GPIO_NUM_9, GPIO_NUM_10, GPIO_NUM_11, GPIO_NUM_15, GPIO_NUM_16, GPIO_NUM_17, FLASH_GPIO, BLINK_GPIO }; static const GpioBoardProfile kBoardProfile = { "AI-Thinker ESP32-CAM (1-bit SD)", kUserPins, kUserPinCount, kReservedPins, sizeof(kReservedPins) / sizeof(kReservedPins[0]), FLASH_GPIO, GPIO_MAX_USER_CHANNELS }; #elif defined(BOARD_ESP32_S3_ALEKSEI) /* * Reference mapping taken from allexoK/AI-on-the-edge-device, * branch esp32s3-test, BOARD_ESP32_S3_ALEKSEI. * * The build enables Octal PSRAM. Camera, SDMMC, W5500, UART0, battery ADC, * power-control rails and the board LEDs account for the remaining GPIOs. * GPIO12 is the on-board four-LED WS2812 chain; camera D4 is GPIO47. * * No general-purpose user pin is exposed. This is a conservative software * profile for the dedicated board, not proof that a different ESP32-S3 * carrier or module revision has the same wiring. */ static const gpio_num_t kReservedPins[] = { /* Strapping pins, battery ADC, UART / I2C and camera signals. */ GPIO_NUM_0, GPIO_NUM_1, GPIO_NUM_2, GPIO_NUM_3, GPIO_NUM_4, GPIO_NUM_5, GPIO_NUM_6, GPIO_NUM_7, GPIO_NUM_8, GPIO_NUM_9, GPIO_NUM_10, GPIO_NUM_11, GPIO_NUM_12, GPIO_NUM_13, GPIO_NUM_14, GPIO_NUM_15, GPIO_NUM_16, GPIO_NUM_17, GPIO_NUM_18, /* USB Serial/JTAG and Octal Flash/PSRAM connections. */ GPIO_NUM_19, GPIO_NUM_20, GPIO_NUM_21, GPIO_NUM_26, GPIO_NUM_27, GPIO_NUM_28, GPIO_NUM_29, GPIO_NUM_30, GPIO_NUM_31, GPIO_NUM_32, GPIO_NUM_33, GPIO_NUM_34, GPIO_NUM_35, GPIO_NUM_36, GPIO_NUM_37, /* W5500, SDMMC, UART0, power rails, strapping and camera/flash LED. */ GPIO_NUM_38, GPIO_NUM_39, GPIO_NUM_40, GPIO_NUM_41, GPIO_NUM_42, GPIO_NUM_43, GPIO_NUM_44, GPIO_NUM_45, GPIO_NUM_46, GPIO_NUM_47, GPIO_NUM_48 }; static const GpioBoardProfile kBoardProfile = { "allexoK ESP32-S3-POE-A-CAM+ (Octal PSRAM, no user GPIOs)", NULL, 0U, kReservedPins, sizeof(kReservedPins) / sizeof(kReservedPins[0]), GPIO_NUM_48, GPIO_MAX_USER_CHANNELS }; #elif defined(BOARD_FREENOVE_ESP32S3) /* * Freenove ESP32-S3-CAM mapping supplied for audit: * camera, SDMMC, UART and on-board LEDs are reserved. GPIO21 is assigned * to the legacy generic GPIO handler, but no carrier schematic or complete * peripheral audit was supplied to prove it is user-safe. Fail closed. */ static const gpio_num_t kReservedPins[] = { GPIO_NUM_0, GPIO_NUM_3, GPIO_NUM_4, GPIO_NUM_5, GPIO_NUM_6, GPIO_NUM_7, GPIO_NUM_8, GPIO_NUM_9, GPIO_NUM_10, GPIO_NUM_11, GPIO_NUM_12, GPIO_NUM_13, GPIO_NUM_14, GPIO_NUM_15, GPIO_NUM_16, GPIO_NUM_17, GPIO_NUM_18, GPIO_NUM_19, GPIO_NUM_20, GPIO_NUM_26, GPIO_NUM_27, GPIO_NUM_28, GPIO_NUM_29, GPIO_NUM_30, GPIO_NUM_31, GPIO_NUM_32, GPIO_NUM_33, GPIO_NUM_34, GPIO_NUM_35, GPIO_NUM_36, GPIO_NUM_37, GPIO_NUM_38, GPIO_NUM_39, GPIO_NUM_40, GPIO_NUM_41, GPIO_NUM_43, GPIO_NUM_44, GPIO_NUM_45, GPIO_NUM_46, GPIO_NUM_47, GPIO_NUM_48, GPIO_NUM_2, GPIO_NUM_21 }; static const GpioBoardProfile kBoardProfile = { "Freenove ESP32-S3-CAM (GPIO profile not yet audited)", NULL, 0U, kReservedPins, sizeof(kReservedPins) / sizeof(kReservedPins[0]), GPIO_NUM_48, GPIO_MAX_USER_CHANNELS }; #elif defined(BOARD_ESP32_S3_ETH_V1) || defined(BOARD_ESP32_S3_ETH_V2) /* * ESP32-S3-ETH V1/V2 mapping supplied for audit: * camera, SDMMC, UART, W5500 and board LEDs are reserved. Legacy GPIO * handler pins 33-36 are not whitelisted: module flash/PSRAM wiring must * be confirmed for the exact module variant before any pin is released. */ static const gpio_num_t kReservedPins[] = { GPIO_NUM_0, GPIO_NUM_3, GPIO_NUM_4, GPIO_NUM_5, GPIO_NUM_6, GPIO_NUM_7, GPIO_NUM_8, GPIO_NUM_9, GPIO_NUM_10, GPIO_NUM_11, GPIO_NUM_12, GPIO_NUM_13, GPIO_NUM_14, GPIO_NUM_15, GPIO_NUM_16, GPIO_NUM_17, GPIO_NUM_18, GPIO_NUM_19, GPIO_NUM_20, GPIO_NUM_21, GPIO_NUM_26, GPIO_NUM_27, GPIO_NUM_28, GPIO_NUM_29, GPIO_NUM_30, GPIO_NUM_31, GPIO_NUM_32, GPIO_NUM_33, GPIO_NUM_34, GPIO_NUM_35, GPIO_NUM_36, GPIO_NUM_37, GPIO_NUM_38, GPIO_NUM_39, GPIO_NUM_40, GPIO_NUM_41, GPIO_NUM_42, GPIO_NUM_43, GPIO_NUM_44, GPIO_NUM_45, GPIO_NUM_46, GPIO_NUM_47, GPIO_NUM_48, GPIO_NUM_1 }; static const GpioBoardProfile kBoardProfile = { "ESP32-S3-ETH V1/V2 (GPIO profile not yet audited)", NULL, 0U, kReservedPins, sizeof(kReservedPins) / sizeof(kReservedPins[0]), GPIO_NUM_NC, GPIO_MAX_USER_CHANNELS }; #elif defined(BOARD_WROVER_KIT) static const gpio_num_t kReservedPins[] = { CAM_PIN_PWDN, CAM_PIN_RESET, CAM_PIN_XCLK, CAM_PIN_SIOD, CAM_PIN_SIOC, CAM_PIN_D7, CAM_PIN_D6, CAM_PIN_D5, CAM_PIN_D4, CAM_PIN_D3, CAM_PIN_D2, CAM_PIN_D1, CAM_PIN_D0, CAM_PIN_VSYNC, CAM_PIN_HREF, CAM_PIN_PCLK, GPIO_SDCARD_CLK, GPIO_SDCARD_CMD, GPIO_SDCARD_D0, GPIO_SDCARD_D1, GPIO_SDCARD_D2, GPIO_SDCARD_D3, FLASH_GPIO, BLINK_GPIO }; /* No user GPIOs are enabled until this board's complete wiring is audited. */ static const GpioBoardProfile kBoardProfile = { "ESP32-WROVER-KIT (GPIO profile not yet audited)", NULL, 0U, kReservedPins, sizeof(kReservedPins) / sizeof(kReservedPins[0]), FLASH_GPIO, GPIO_MAX_USER_CHANNELS }; #elif defined(BOARD_M5STACK_PSRAM) static const gpio_num_t kReservedPins[] = { CAM_PIN_PWDN, CAM_PIN_RESET, CAM_PIN_XCLK, CAM_PIN_SIOD, CAM_PIN_SIOC, CAM_PIN_D7, CAM_PIN_D6, CAM_PIN_D5, CAM_PIN_D4, CAM_PIN_D3, CAM_PIN_D2, CAM_PIN_D1, CAM_PIN_D0, CAM_PIN_VSYNC, CAM_PIN_HREF, CAM_PIN_PCLK, FLASH_GPIO, BLINK_GPIO }; /* No user GPIOs are enabled until this board's complete wiring is audited. */ static const GpioBoardProfile kBoardProfile = { "M5Stack PSRAM (GPIO profile not yet audited)", NULL, 0U, kReservedPins, sizeof(kReservedPins) / sizeof(kReservedPins[0]), FLASH_GPIO, GPIO_MAX_USER_CHANNELS }; #else /* * Unknown boards fail closed. A new board must add an explicit profile rather * than inheriting a generic list of SoC GPIO numbers. */ static const GpioBoardProfile kBoardProfile = { "Unconfigured board (no user GPIOs enabled)", NULL, 0U, NULL, 0U, GPIO_NUM_NC, GPIO_MAX_USER_CHANNELS }; #endif const GpioBoardProfile *gpioGetBoardProfile(void) { #if defined(BOARD_ESP32CAM_AITHINKER) && defined(__SD_USE_ONE_LINE_MODE__) static const GpioBoardProfile profile = []() { GpioBoardProfile selected = kBoardProfile; if (gpio12FlashVoltageForcedTo3V3()) { selected.userPinCount = 2U; selected.name = "AI-Thinker ESP32-CAM (1-bit SD, GPIO12/13 available; fixed 3.3 V eFuse)"; } else { selected.userPinCount = 1U; selected.name = "AI-Thinker ESP32-CAM (1-bit SD, GPIO13 available; GPIO12 reserved: VDD_SDIO not forced)"; } return selected; }(); return &profile; #else return &kBoardProfile; #endif } static bool isValidSocGpio(gpio_num_t gpio) { return (static_cast(gpio) >= 0) && GPIO_IS_VALID_GPIO(gpio); } bool gpioBoardIsReservedPin(gpio_num_t gpio) { if (!isValidSocGpio(gpio)) { return true; } const GpioBoardProfile *profile = gpioGetBoardProfile(); #if defined(BOARD_ESP32CAM_AITHINKER) && defined(__SD_USE_ONE_LINE_MODE__) if (gpio == GPIO_NUM_12 && profile->userPinCount < 2U) { return true; } #endif if (gpioBoardIsBuiltInFlashLed(gpio)) { return true; } if (profile->reservedPins != NULL) { for (size_t i = 0; i < profile->reservedPinCount; ++i) { if (profile->reservedPins[i] == gpio) { return true; } } } return false; } const GpioBoardPin *gpioBoardFindUserPin(gpio_num_t gpio) { const GpioBoardProfile *profile = gpioGetBoardProfile(); if (!isValidSocGpio(gpio) || gpioBoardIsReservedPin(gpio) || profile->userPins == NULL) { return NULL; } for (size_t i = 0; i < profile->userPinCount; ++i) { if (profile->userPins[i].gpio == gpio) { return &profile->userPins[i]; } } return NULL; } bool gpioBoardPinSupports(gpio_num_t gpio, uint8_t requiredCapabilities) { const GpioBoardPin *pin = gpioBoardFindUserPin(gpio); if (pin == NULL) { return false; } return (pin->capabilities & requiredCapabilities) == requiredCapabilities; } bool gpioBoardIsBuiltInFlashLed(gpio_num_t gpio) { const GpioBoardProfile *profile = gpioGetBoardProfile(); return isValidSocGpio(gpio) && isValidSocGpio(profile->builtInFlashLedPin) && gpio == profile->builtInFlashLedPin; } bool gpioBoardProfileIsValid(void) { const GpioBoardProfile *profile = gpioGetBoardProfile(); if (profile->name == NULL || profile->maxUserChannels == 0U || profile->maxUserChannels > GPIO_MAX_USER_CHANNELS) { ESP_LOGE(TAG, "Invalid board GPIO profile metadata"); return false; } if (profile->userPinCount > 0U && profile->userPins == NULL) { ESP_LOGE(TAG, "GPIO profile '%s' has a null user pin list", profile->name); return false; } for (size_t i = 0; i < profile->userPinCount; ++i) { const GpioBoardPin &pin = profile->userPins[i]; if (!isValidSocGpio(pin.gpio) || pin.description == NULL || pin.capabilities == 0U || gpioBoardIsReservedPin(pin.gpio)) { ESP_LOGE(TAG, "Invalid or reserved GPIO%d in board profile '%s'", static_cast(pin.gpio), profile->name); return false; } for (size_t j = i + 1U; j < profile->userPinCount; ++j) { if (pin.gpio == profile->userPins[j].gpio) { ESP_LOGE(TAG, "Duplicate GPIO%d in board profile '%s'", static_cast(pin.gpio), profile->name); return false; } } } ESP_LOGI(TAG, "Using board GPIO profile '%s': %u user channels maximum, %u pins whitelisted", profile->name, static_cast(profile->maxUserChannels), static_cast(profile->userPinCount)); return true; }