Skip to content

Commit 5efc79e

Browse files
committed
ESP32: Fix GPIO check for ESP32-U4WDH
GPIO 9, 10 are available while 16, 17 are not. https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf, section 2.2
1 parent e5a232c commit 5efc79e

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

platforms/esp32/src/esp32_gpio.c

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,20 @@
3333

3434
#include "common/cs_dbg.h"
3535

36+
#include "esp_efuse.h"
37+
#include "soc/efuse_reg.h"
38+
39+
#ifndef MGOS_ESP32_DISABLE_GPIO_SPI_FLASH_CHECK
40+
#define MGOS_ESP32_DISABLE_GPIO_SPI_FLASH_CHECK 0
41+
#endif
42+
3643
gpio_isr_handle_t s_int_handle;
3744
static uint8_t s_int_ena[GPIO_PIN_COUNT];
3845

46+
#if !MGOS_ESP32_DISABLE_GPIO_SPI_FLASH_CHECK
47+
static int s_chip_pkg;
48+
#endif
49+
3950
/* Invoked by SDK, runs in ISR context. */
4051
IRAM static void esp32_gpio_isr(void *arg) {
4152
uint32_t int_st = GPIO.status;
@@ -74,7 +85,15 @@ IRAM bool mgos_gpio_set_mode(int pin, enum mgos_gpio_mode mode) {
7485
default:
7586
return false;
7687
}
77-
if (pin >= 6 && pin <= 11) {
88+
89+
#if !MGOS_ESP32_DISABLE_GPIO_SPI_FLASH_CHECK
90+
bool ok;
91+
if (s_chip_pkg == EFUSE_RD_CHIP_VER_PKG_ESP32U4WDH) {
92+
ok = (pin < 6 || (pin > 8 && pin != 11 && pin != 16 && pin != 17));
93+
} else {
94+
ok = (pin < 6 || pin > 11);
95+
}
96+
if (!ok) {
7897
LOG(LL_ERROR, ("GPIO%d is used by SPI flash, don't use it", pin));
7998
/*
8099
* Alright, so you're here to investigate what's up with this error. So,
@@ -87,9 +106,20 @@ IRAM bool mgos_gpio_set_mode(int pin, enum mgos_gpio_mode mode) {
87106
* crash most ESP32 modules, but in a different way.
88107
* So really, just stay away from GPIO6-11 if you can help it.
89108
* If you are sure you know what you're doing, use gpio_set_direction().
109+
*
110+
* Note:
111+
* For ESP32-U4WDH (defined by chip_pkg == 4) GPIO9 and GPIO10 are
112+
* available, but GPIO16 and GPIO17 are not. Source:
113+
* https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf
114+
* Section 2.2 in the notes.
115+
*
116+
* Add `MGOS_ESP32_DISABLE_GPIO_SPI_FLASH_CHECK: 1` to cdefs to disable
117+
* this check.
90118
*/
91119
return false;
92120
}
121+
#endif
122+
93123
if (gpio_set_direction(pin, m) != ESP_OK) {
94124
return false;
95125
}
@@ -233,6 +263,9 @@ enum mgos_init_result mgos_gpio_hal_init() {
233263
mgos_bitbang_n100_cal = 6;
234264
#else
235265
#error Unsupported CPU frequency
266+
#endif
267+
#if !MGOS_ESP32_DISABLE_GPIO_SPI_FLASH_CHECK
268+
s_chip_pkg = esp_efuse_get_pkg_ver();
236269
#endif
237270
return MGOS_INIT_OK;
238271
}

0 commit comments

Comments
 (0)