diff --git a/components/base_nodemcu/user_main.c b/components/base_nodemcu/user_main.c index 6528d98c..33a1b780 100644 --- a/components/base_nodemcu/user_main.c +++ b/components/base_nodemcu/user_main.c @@ -21,6 +21,7 @@ #include "esp_netif.h" #include "esp_vfs_dev.h" #include "esp_vfs_cdcacm.h" +#include "esp_vfs_console.h" #include "esp_vfs_usb_serial_jtag.h" #include "driver/uart_vfs.h" #include "driver/usb_serial_jtag.h" @@ -218,6 +219,8 @@ static void console_task(void *) static void console_init(void) { + esp_vfs_console_register(); + fflush(stdout); fsync(fileno(stdout)); diff --git a/components/modules/Kconfig b/components/modules/Kconfig index ce13f848..0ca93e76 100644 --- a/components/modules/Kconfig +++ b/components/modules/Kconfig @@ -323,6 +323,17 @@ menu "NodeMCU modules" help Includes the WiFi module (recommended). + config NODEMCU_CMODULE_WIFI_STARTUP_DELAY + depends on ESP_CONSOLE_USB_CDC + int "WiFi start-up delay (ms)" + default 500 + help + For some unknown reason there is an issue with allowing the + WiFi stack to initialise immediately when using a USB CDC + console (at least on the ESP32-S2). The workaround is to + delay the initialisation sequence by enough that whatever + else is needing to run gets to run first. + config NODEMCU_CMODULE_WS2812 bool "WS2812 module" default "n" diff --git a/components/modules/wifi.c b/components/modules/wifi.c index f16b9833..81b86a0e 100644 --- a/components/modules/wifi.c +++ b/components/modules/wifi.c @@ -93,22 +93,6 @@ static int wifi_stop (lua_State *L) 0 : luaL_error (L, "failed to stop wifi, code %d", err); } -#if defined(CONFIG_ESP_CONSOLE_USB_CDC) -// For some unknown reason, on an S2 with USB CDC console enabled, if we allow -// the esp_wifi_init() to run during initial startup, something Bad(tm) -// happens and the S2 fails to enumerate on the USB bus. However, if we defer -// the wifi initialisation, it starts up fine. This is an ugly workaround, but -// I'm out of ideas at this point. If I use a UART console, I see no errors -// even with the immediate init. -static task_handle_t th; -#endif - -static void do_esp_wifi_init(task_param_t p, task_prio_t) -{ - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init (&cfg)); -} - extern void wifi_ap_init (void); extern void wifi_sta_init (void); static int wifi_init (lua_State *L) @@ -117,11 +101,16 @@ static int wifi_init (lua_State *L) wifi_sta_init (); #if defined(CONFIG_ESP_CONSOLE_USB_CDC) - th = task_get_id(do_esp_wifi_init); - task_post_low(th, 0); -#else - do_esp_wifi_init(0, 0); +// For some unknown reason, on an S2 with USB CDC console enabled, if we allow +// the esp_wifi_init() to run during initial startup, something Bad(tm) +// happens and the S2 fails to enumerate on the USB bus. However, if we defer +// it by half a second or so, everything works. This is an ugly workaround, +// but I'm out of ideas at this point. + vTaskDelay(CONFIG_NODEMCU_CMODULE_WIFI_STARTUP_DELAY / portTICK_PERIOD_MS); #endif + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init (&cfg)); + return 0; }