Skip to content

Commit 5c6ee20

Browse files
committed
on esp32 need to initialize nvs before starting wifi
Also, change error handling so that the esp-idf error number is shown in the traceback in the case of an error. This allows scanning & connecting to work. I didn't try requests yet.
1 parent f620c29 commit 5c6ee20

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

ports/espressif/common-hal/wifi/__init__.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include "bindings/espidf/__init__.h"
2728
#include "common-hal/wifi/__init__.h"
2829
#include "shared-bindings/wifi/__init__.h"
2930

@@ -47,6 +48,10 @@ wifi_radio_obj_t common_hal_wifi_radio_obj;
4748

4849
#include "esp_ipc.h"
4950

51+
#ifdef CONFIG_IDF_TARGET_ESP32
52+
#include "nvs_flash.h"
53+
#endif
54+
5055
static const char *TAG = "CP wifi";
5156

5257
STATIC void schedule_background_on_cp_core(void *arg) {
@@ -174,11 +179,21 @@ void common_hal_wifi_init(bool user_initiated) {
174179
&self->handler_instance_got_ip));
175180

176181
wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT();
182+
#ifdef CONFIG_IDF_TARGET_ESP32
183+
esp_err_t err = nvs_flash_init();
184+
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
185+
// NVS partition was truncated and needs to be erased
186+
// Retry nvs_flash_init
187+
ESP_ERROR_CHECK(nvs_flash_erase());
188+
err = nvs_flash_init();
189+
}
190+
ESP_ERROR_CHECK(err);
191+
#endif
177192
esp_err_t result = esp_wifi_init(&config);
178193
if (result == ESP_ERR_NO_MEM) {
179194
mp_raise_msg(&mp_type_MemoryError, translate("Failed to allocate Wifi memory"));
180195
} else if (result != ESP_OK) {
181-
mp_raise_RuntimeError(translate("Failed to init wifi"));
196+
raise_esp_error(result);
182197
}
183198
// set station mode to avoid the default SoftAP
184199
common_hal_wifi_radio_start_station(self);

0 commit comments

Comments
 (0)