Procházet zdrojové kódy

Expose GPIO12 only when 3.3 V flash eFuses are forced

SybexXx před 3 dny
rodič
revize
6d00cf01e0

+ 81 - 36
code/components/jomjol_controlGPIO/board_gpio_profile.cpp

@@ -3,31 +3,51 @@
 #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 current firmware selects 1-bit SD mode and mounts the SD card before
- *   creating the GPIO handler.
- * - GPIO13 (DAT3) is pulled up before SD-card initialization to keep the card
- *   in native SD/MMC mode. In 1-bit mode DAT3 is not part of the active bus;
- *   after a successful mount, the generic GPIO handler may reconfigure it.
- * - GPIO12 (DAT2) remains reserved: it is physically connected to the card
- *   and is the MTDI flash-voltage strapping pin sampled before app_main().
- *   1-bit mode alone does not remove that boot-time electrical connection.
- * - If the build is changed to 4-bit SD mode, GPIO13 is reserved as well.
+ * - 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 kUserPins[] = {
+static const GpioBoardPin kUserPinList[] = {
     { GPIO_NUM_13,
       static_cast<uint8_t>(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)" }
+      "SD DAT3 / GPIO13 (released after SD mount in 1-bit mode)" },
+    { GPIO_NUM_12,
+      static_cast<uint8_t>(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 size_t kUserPinCount = sizeof(kUserPins) / sizeof(kUserPins[0]);
+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;
@@ -47,7 +67,7 @@ static const gpio_num_t kReservedPins[] = {
     /* 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,
+    GPIO_NUM_15, GPIO_NUM_16, GPIO_NUM_17,
 
     FLASH_GPIO,
     BLINK_GPIO
@@ -218,7 +238,22 @@ static const GpioBoardProfile kBoardProfile = {
 
 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)
@@ -232,13 +267,20 @@ bool gpioBoardIsReservedPin(gpio_num_t 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 (kBoardProfile.reservedPins != NULL) {
-        for (size_t i = 0; i < kBoardProfile.reservedPinCount; ++i) {
-            if (kBoardProfile.reservedPins[i] == gpio) {
+    if (profile->reservedPins != NULL) {
+        for (size_t i = 0; i < profile->reservedPinCount; ++i) {
+            if (profile->reservedPins[i] == gpio) {
                 return true;
             }
         }
@@ -249,14 +291,15 @@ bool gpioBoardIsReservedPin(gpio_num_t gpio)
 
 const GpioBoardPin *gpioBoardFindUserPin(gpio_num_t gpio)
 {
+    const GpioBoardProfile *profile = gpioGetBoardProfile();
     if (!isValidSocGpio(gpio) || gpioBoardIsReservedPin(gpio) ||
-        kBoardProfile.userPins == NULL) {
+        profile->userPins == NULL) {
         return NULL;
     }
 
-    for (size_t i = 0; i < kBoardProfile.userPinCount; ++i) {
-        if (kBoardProfile.userPins[i].gpio == gpio) {
-            return &kBoardProfile.userPins[i];
+    for (size_t i = 0; i < profile->userPinCount; ++i) {
+        if (profile->userPins[i].gpio == gpio) {
+            return &profile->userPins[i];
         }
     }
 
@@ -275,47 +318,49 @@ bool gpioBoardPinSupports(gpio_num_t gpio, uint8_t requiredCapabilities)
 
 bool gpioBoardIsBuiltInFlashLed(gpio_num_t gpio)
 {
+    const GpioBoardProfile *profile = gpioGetBoardProfile();
     return isValidSocGpio(gpio) &&
-           isValidSocGpio(kBoardProfile.builtInFlashLedPin) &&
-           gpio == kBoardProfile.builtInFlashLedPin;
+           isValidSocGpio(profile->builtInFlashLedPin) &&
+           gpio == profile->builtInFlashLedPin;
 }
 
 bool gpioBoardProfileIsValid(void)
 {
-    if (kBoardProfile.name == NULL ||
-        kBoardProfile.maxUserChannels == 0U ||
-        kBoardProfile.maxUserChannels > GPIO_MAX_USER_CHANNELS) {
+    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 (kBoardProfile.userPinCount > 0U && kBoardProfile.userPins == NULL) {
-        ESP_LOGE(TAG, "GPIO profile '%s' has a null user pin list", kBoardProfile.name);
+    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 < kBoardProfile.userPinCount; ++i) {
-        const GpioBoardPin &pin = kBoardProfile.userPins[i];
+    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<int>(pin.gpio), kBoardProfile.name);
+                     static_cast<int>(pin.gpio), profile->name);
             return false;
         }
 
-        for (size_t j = i + 1U; j < kBoardProfile.userPinCount; ++j) {
-            if (pin.gpio == kBoardProfile.userPins[j].gpio) {
+        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<int>(pin.gpio), kBoardProfile.name);
+                         static_cast<int>(pin.gpio), profile->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));
+             profile->name,
+             static_cast<unsigned int>(profile->maxUserChannels),
+             static_cast<unsigned int>(profile->userPinCount));
     return true;
 }