#include "board_gpio_profile.h" #include "esp_log.h" #include "../../include/defines.h" static const char *TAG = "GPIO_PROFILE"; #if defined(BOARD_ESP32CAM_AITHINKER) /* * AI-Thinker ESP32-CAM: * - Camera, flash LED, status LED, UART, strapping pins, flash/PSRAM and the * active SDMMC signals are reserved. * - The current firmware selects 1-bit SD mode. GPIO13 is the SD DAT3 contact, * but is not part of the active 1-bit data bus. It remains input-only here: * no output or PWM capability is granted. * - If the build is changed to 4-bit SD mode, the user whitelist is empty. */ #if defined(__SD_USE_ONE_LINE_MODE__) static const GpioBoardPin kUserPins[] = { { GPIO_NUM_13, static_cast(GPIO_PIN_CAP_INPUT | GPIO_PIN_CAP_PULL_UP | GPIO_PIN_CAP_INTERRUPT), "GPIO13 - input only; SD DAT3 is inactive in 1-bit SD mode" } }; static const size_t kUserPinCount = sizeof(kUserPins) / sizeof(kUserPins[0]); #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_12, 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_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) { return &kBoardProfile; } 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; } if (gpioBoardIsBuiltInFlashLed(gpio)) { return true; } if (kBoardProfile.reservedPins != NULL) { for (size_t i = 0; i < kBoardProfile.reservedPinCount; ++i) { if (kBoardProfile.reservedPins[i] == gpio) { return true; } } } return false; } const GpioBoardPin *gpioBoardFindUserPin(gpio_num_t gpio) { if (!isValidSocGpio(gpio) || gpioBoardIsReservedPin(gpio) || kBoardProfile.userPins == NULL) { return NULL; } for (size_t i = 0; i < kBoardProfile.userPinCount; ++i) { if (kBoardProfile.userPins[i].gpio == gpio) { return &kBoardProfile.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) { return isValidSocGpio(gpio) && isValidSocGpio(kBoardProfile.builtInFlashLedPin) && gpio == kBoardProfile.builtInFlashLedPin; } bool gpioBoardProfileIsValid(void) { if (kBoardProfile.name == NULL || kBoardProfile.maxUserChannels == 0U || kBoardProfile.maxUserChannels > GPIO_MAX_USER_CHANNELS) { ESP_LOGE(TAG, "Invalid board GPIO profile metadata"); return false; } if (kBoardProfile.userPinCount > 0U && kBoardProfile.userPins == NULL) { ESP_LOGE(TAG, "GPIO profile '%s' has a null user pin list", kBoardProfile.name); return false; } for (size_t i = 0; i < kBoardProfile.userPinCount; ++i) { const GpioBoardPin &pin = kBoardProfile.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), kBoardProfile.name); return false; } for (size_t j = i + 1U; j < kBoardProfile.userPinCount; ++j) { if (pin.gpio == kBoardProfile.userPins[j].gpio) { ESP_LOGE(TAG, "Duplicate GPIO%d in board profile '%s'", static_cast(pin.gpio), kBoardProfile.name); return false; } } } ESP_LOGI(TAG, "Using board GPIO profile '%s': %u user channels maximum, %u pins whitelisted", kBoardProfile.name, static_cast(kBoardProfile.maxUserChannels), static_cast(kBoardProfile.userPinCount)); return true; }