33
33
34
34
#include "common/cs_dbg.h"
35
35
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
+
36
43
gpio_isr_handle_t s_int_handle ;
37
44
static uint8_t s_int_ena [GPIO_PIN_COUNT ];
38
45
46
+ #if !MGOS_ESP32_DISABLE_GPIO_SPI_FLASH_CHECK
47
+ static int s_chip_pkg ;
48
+ #endif
49
+
39
50
/* Invoked by SDK, runs in ISR context. */
40
51
IRAM static void esp32_gpio_isr (void * arg ) {
41
52
uint32_t int_st = GPIO .status ;
@@ -74,7 +85,15 @@ IRAM bool mgos_gpio_set_mode(int pin, enum mgos_gpio_mode mode) {
74
85
default :
75
86
return false;
76
87
}
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 ) {
78
97
LOG (LL_ERROR , ("GPIO%d is used by SPI flash, don't use it" , pin ));
79
98
/*
80
99
* 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) {
87
106
* crash most ESP32 modules, but in a different way.
88
107
* So really, just stay away from GPIO6-11 if you can help it.
89
108
* 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.
90
118
*/
91
119
return false;
92
120
}
121
+ #endif
122
+
93
123
if (gpio_set_direction (pin , m ) != ESP_OK ) {
94
124
return false;
95
125
}
@@ -233,6 +263,9 @@ enum mgos_init_result mgos_gpio_hal_init() {
233
263
mgos_bitbang_n100_cal = 6 ;
234
264
#else
235
265
#error Unsupported CPU frequency
266
+ #endif
267
+ #if !MGOS_ESP32_DISABLE_GPIO_SPI_FLASH_CHECK
268
+ s_chip_pkg = esp_efuse_get_pkg_ver ();
236
269
#endif
237
270
return MGOS_INIT_OK ;
238
271
}
0 commit comments