diff --git a/main.c b/main.c index fb73b09b1f..3a14df0fa6 100755 --- a/main.c +++ b/main.c @@ -436,11 +436,12 @@ int run_repl(void) { } int __attribute__((used)) main(void) { - memory_init(); - // initialise the cpu and peripherals safe_mode_t safe_mode = port_init(); + // Init memory after the port in case the port needs to set aside memory. + memory_init(); + // Turn on LEDs init_status_leds(); rgb_led_status_init(); diff --git a/ports/esp32s2/common-hal/wifi/Radio.c b/ports/esp32s2/common-hal/wifi/Radio.c index 0bdcfb01a4..a2944dd52c 100644 --- a/ports/esp32s2/common-hal/wifi/Radio.c +++ b/ports/esp32s2/common-hal/wifi/Radio.c @@ -31,18 +31,41 @@ #include "esp-idf/components/esp_wifi/include/esp_wifi.h" +#include "esp_log.h" +static const char *TAG = "cp radio"; + +static void start_station(wifi_radio_obj_t *self) { + if (self->sta_mode) { + return; + } + wifi_mode_t next_mode; + if (self->ap_mode) { + next_mode = WIFI_MODE_APSTA; + } else { + next_mode = WIFI_MODE_STA; + } + esp_wifi_set_mode(next_mode); + + esp_wifi_set_config(WIFI_MODE_STA, &self->sta_config); +} + bool common_hal_wifi_radio_get_enabled(wifi_radio_obj_t *self) { return self->started; } void common_hal_wifi_radio_set_enabled(wifi_radio_obj_t *self, bool enabled) { if (self->started && !enabled) { - esp_wifi_stop(); + ESP_LOGI(TAG, "stop"); + if (self->current_scan != NULL) { + common_hal_wifi_radio_stop_scanning_networks(self); + } + ESP_ERROR_CHECK(esp_wifi_stop()); self->started = false; return; } if (!self->started && enabled) { - esp_wifi_start(); + ESP_LOGI(TAG, "start"); + ESP_ERROR_CHECK(esp_wifi_start()); self->started = true; return; } @@ -59,7 +82,9 @@ mp_obj_t common_hal_wifi_radio_start_scanning_networks(wifi_radio_obj_t *self) { mp_raise_RuntimeError(translate("Already scanning for wifi networks")); } // check enabled + start_station(self); + ESP_LOGI(TAG, "start scan"); wifi_scannednetworks_obj_t *scan = m_new_obj(wifi_scannednetworks_obj_t); self->current_scan = scan; scan->base.type = &wifi_scannednetworks_type; @@ -72,8 +97,10 @@ mp_obj_t common_hal_wifi_radio_start_scanning_networks(wifi_radio_obj_t *self) { void common_hal_wifi_radio_stop_scanning_networks(wifi_radio_obj_t *self) { // Free the memory used to store the found aps. + ESP_EARLY_LOGI(TAG, "stop scan"); wifi_scannednetworks_deinit(self->current_scan); self->current_scan = NULL; + ESP_EARLY_LOGI(TAG, "stop scan done"); } bool common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t* ssid, size_t ssid_len, uint8_t* password, size_t password_len, mp_float_t timeout) { diff --git a/ports/esp32s2/common-hal/wifi/Radio.h b/ports/esp32s2/common-hal/wifi/Radio.h index f681213dd8..11b30799ac 100644 --- a/ports/esp32s2/common-hal/wifi/Radio.h +++ b/ports/esp32s2/common-hal/wifi/Radio.h @@ -43,7 +43,11 @@ typedef struct { wifi_scannednetworks_obj_t *current_scan; StaticEventGroup_t event_group; EventGroupHandle_t event_group_handle; + wifi_config_t sta_config; + esp_netif_t *netif; bool started; + bool ap_mode; + bool sta_mode; } wifi_radio_obj_t; #endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_WIFI_RADIO_H diff --git a/ports/esp32s2/common-hal/wifi/ScannedNetworks.c b/ports/esp32s2/common-hal/wifi/ScannedNetworks.c index a67a5f3bd8..248defa574 100644 --- a/ports/esp32s2/common-hal/wifi/ScannedNetworks.c +++ b/ports/esp32s2/common-hal/wifi/ScannedNetworks.c @@ -38,10 +38,16 @@ #include "esp-idf/components/esp_wifi/include/esp_wifi.h" +#include "esp_log.h" +static const char *TAG = "cp scannednetworks"; + static void wifi_scannednetworks_done(wifi_scannednetworks_obj_t *self) { self->done = true; - free(self->results); - self->results = NULL; + ESP_EARLY_LOGI(TAG, "free %x", self->results); + if (self->results != NULL) { + m_free(self->results); + self->results = NULL; + } } static bool wifi_scannednetworks_wait_for_scan(wifi_scannednetworks_obj_t *self) { @@ -75,6 +81,7 @@ mp_obj_t common_hal_wifi_scannednetworks_next(wifi_scannednetworks_obj_t *self) } esp_wifi_scan_get_ap_num(&self->total_results); + self->scanning = false; if (self->total_results > 0) { break; } @@ -83,6 +90,7 @@ mp_obj_t common_hal_wifi_scannednetworks_next(wifi_scannednetworks_obj_t *self) } // We not have found any more results so we're done. if (self->done) { + ESP_LOGI(TAG, "return done"); return mp_const_none; } // If we need more space than we have, realloc. @@ -92,6 +100,7 @@ mp_obj_t common_hal_wifi_scannednetworks_next(wifi_scannednetworks_obj_t *self) self->max_results, self->total_results, true /* allow move */); + ESP_EARLY_LOGI(TAG, "alloc %x", results); if (results != NULL) { self->results = results; self->max_results = self->total_results; @@ -128,7 +137,7 @@ mp_obj_t common_hal_wifi_scannednetworks_next(wifi_scannednetworks_obj_t *self) static uint8_t scan_pattern[] = {6, 1, 11, 3, 9, 13, 2, 4, 8, 12, 5, 7, 10, 14}; void wifi_scannednetworks_scan_next_channel(wifi_scannednetworks_obj_t *self) { - wifi_scan_config_t config; + uint8_t next_channel = sizeof(scan_pattern); while (self->current_channel_index < sizeof(scan_pattern)) { next_channel = scan_pattern[self->current_channel_index]; @@ -137,11 +146,19 @@ void wifi_scannednetworks_scan_next_channel(wifi_scannednetworks_obj_t *self) { break; } } - if (next_channel == sizeof(scan_pattern) || - esp_wifi_scan_start(&config, false) != ESP_OK) { + wifi_scan_config_t config = { 0 }; + config.channel = next_channel; + if (next_channel == sizeof(scan_pattern)) { + ESP_LOGI(TAG, "scan done"); wifi_scannednetworks_done(self); } else { - self->scanning = true; + esp_err_t result = esp_wifi_scan_start(&config, false); + if (result != ESP_OK) { + ESP_LOGI(TAG, "start failed 0x%x", result); + wifi_scannednetworks_done(self); + } else { + self->scanning = true; + } } } diff --git a/ports/esp32s2/common-hal/wifi/__init__.c b/ports/esp32s2/common-hal/wifi/__init__.c index 9f08603b50..d4a1d9892e 100644 --- a/ports/esp32s2/common-hal/wifi/__init__.c +++ b/ports/esp32s2/common-hal/wifi/__init__.c @@ -33,11 +33,13 @@ static const char *TAG = "cp wifi"; #include "esp-idf/components/esp_wifi/include/esp_wifi.h" +#include "esp-idf/components/heap/include/esp_heap_caps.h" + wifi_radio_obj_t common_hal_wifi_radio_obj; static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { - ESP_LOGI(TAG, "event"); + ESP_LOGI(TAG, "event %x", event_id); wifi_radio_obj_t* radio = arg; if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_SCAN_DONE) { xEventGroupSetBits(radio->event_group_handle, WIFI_SCAN_DONE_BIT); @@ -65,16 +67,20 @@ static void event_handler(void* arg, esp_event_base_t event_base, static bool wifi_inited; void common_hal_wifi_init(void) { - ESP_LOGI(TAG, "init"); + ESP_EARLY_LOGI(TAG, "init"); + heap_caps_print_heap_info(MALLOC_CAP_8BIT); wifi_inited = true; common_hal_wifi_radio_obj.base.type = &wifi_radio_type; ESP_ERROR_CHECK(esp_netif_init()); + ESP_EARLY_LOGI(TAG, "create event loop"); ESP_ERROR_CHECK(esp_event_loop_create_default()); - esp_netif_create_default_wifi_sta(); + ESP_EARLY_LOGI(TAG, "create wifi sta"); wifi_radio_obj_t* self = &common_hal_wifi_radio_obj; + self->netif = esp_netif_create_default_wifi_sta(); + self->event_group_handle = xEventGroupCreateStatic(&self->event_group); ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, @@ -88,6 +94,7 @@ void common_hal_wifi_init(void) { self->handler_instance_got_ip)); + ESP_EARLY_LOGI(TAG, "wifi init"); wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT(); esp_err_t result = esp_wifi_init(&config); if (result == ESP_ERR_NO_MEM) { @@ -95,6 +102,7 @@ void common_hal_wifi_init(void) { } else if (result != ESP_OK) { // handle this } + ESP_EARLY_LOGI(TAG, "enable radio"); common_hal_wifi_radio_set_enabled(self, true); } @@ -111,5 +119,8 @@ void wifi_reset(void) { ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, radio->handler_instance_got_ip)); - esp_wifi_deinit(); + ESP_ERROR_CHECK(esp_wifi_deinit()); + esp_netif_destroy(radio->netif); + radio->netif = NULL; + ESP_ERROR_CHECK(esp_netif_deinit()); } diff --git a/ports/esp32s2/supervisor/port.c b/ports/esp32s2/supervisor/port.c index 24e826c195..9a1ac5e2c8 100644 --- a/ports/esp32s2/supervisor/port.c +++ b/ports/esp32s2/supervisor/port.c @@ -48,7 +48,7 @@ #include "esp_log.h" static const char *TAG = "cp port"; -#define HEAP_SIZE (96 * 1024) +#define HEAP_SIZE (64 * 1024) STATIC esp_timer_handle_t _tick_timer; @@ -92,6 +92,7 @@ void reset_port(void) { #if CIRCUITPY_WIFI wifi_reset(); #endif + heap_caps_print_heap_info(MALLOC_CAP_8BIT); } void reset_to_bootloader(void) { diff --git a/shared-bindings/wifi/ScannedNetworks.c b/shared-bindings/wifi/ScannedNetworks.c index 3e60d5f108..c6b77d70b7 100644 --- a/shared-bindings/wifi/ScannedNetworks.c +++ b/shared-bindings/wifi/ScannedNetworks.c @@ -32,6 +32,9 @@ #include "py/runtime.h" #include "shared-bindings/wifi/ScannedNetworks.h" +#include "esp_log.h" +static const char *TAG = "cp iternext"; + //| class ScannedNetworks: //| """Iterates over wifi `Network`s found while scanning. This object is always created //| by a `wifi.Radio`: it has no user-visible constructor.""" @@ -43,6 +46,8 @@ STATIC mp_obj_t scannednetworks_iternext(mp_obj_t self_in) { if (network != mp_const_none) { return network; } + + ESP_EARLY_LOGI(TAG, "stop iteration"); return MP_OBJ_STOP_ITERATION; } diff --git a/supervisor/shared/memory.c b/supervisor/shared/memory.c index 8ae8a16997..79bdeef9db 100755 --- a/supervisor/shared/memory.c +++ b/supervisor/shared/memory.c @@ -31,6 +31,9 @@ #include "supervisor/shared/display.h" +#include "esp_log.h" +static const char *TAG = "memory"; + #define CIRCUITPY_SUPERVISOR_ALLOC_COUNT (12) static supervisor_allocation allocations[CIRCUITPY_SUPERVISOR_ALLOC_COUNT]; @@ -39,6 +42,7 @@ uint32_t* low_address; uint32_t* high_address; void memory_init(void) { + ESP_LOGE(TAG, "memory init"); low_address = port_heap_get_bottom(); high_address = port_heap_get_top(); }