Workaround for using wifi module on S2 with USB-CDC console.

This commit is contained in:
Jade Mattsson 2024-03-28 15:58:12 +11:00
parent eba58124bc
commit 2053549689
1 changed files with 22 additions and 4 deletions

View File

@ -34,6 +34,7 @@
#include "lauxlib.h" #include "lauxlib.h"
#include "lextra.h" #include "lextra.h"
#include "wifi_common.h" #include "wifi_common.h"
#include "task/task.h"
static int wifi_getmode (lua_State *L) static int wifi_getmode (lua_State *L)
{ {
@ -92,7 +93,21 @@ static int wifi_stop (lua_State *L)
0 : luaL_error (L, "failed to stop wifi, code %d", err); 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_ap_init (void);
extern void wifi_sta_init (void); extern void wifi_sta_init (void);
@ -101,10 +116,13 @@ static int wifi_init (lua_State *L)
wifi_ap_init (); wifi_ap_init ();
wifi_sta_init (); wifi_sta_init ();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); #if defined(CONFIG_ESP_CONSOLE_USB_CDC)
esp_err_t err = esp_wifi_init (&cfg); th = task_get_id(do_esp_wifi_init);
return (err == ESP_OK) ? task_post_low(th, 0);
0 : luaL_error (L, "failed to init wifi, code %d", err); #else
do_esp_wifi_init(0, 0);
#endif
return 0;
} }