| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- #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.
- */
- /* GPIO13 remains connected to SD DAT3 and is explicitly pulled up by main.cpp.
- * Do not expose it as a user pin until SD-card interaction and board wiring
- * have been validated together on hardware. */
- static const GpioBoardPin * const kUserPins = NULL;
- static const size_t kUserPinCount = 0U;
- 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_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)
- {
- return &kBoardProfile;
- }
- static bool isValidSocGpio(gpio_num_t gpio)
- {
- return (static_cast<int>(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<int>(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<int>(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<unsigned int>(kBoardProfile.maxUserChannels),
- static_cast<unsigned int>(kBoardProfile.userPinCount));
- return true;
- }
|