Skip to content

Commit ddcff85

Browse files
committed
Add debugging. Scanning doesn't crash but returns no results. Need to config station.
1 parent 1a6f4e0 commit ddcff85

8 files changed

Lines changed: 85 additions & 15 deletions

File tree

main.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,11 +436,12 @@ int run_repl(void) {
436436
}
437437

438438
int __attribute__((used)) main(void) {
439-
memory_init();
440-
441439
// initialise the cpu and peripherals
442440
safe_mode_t safe_mode = port_init();
443441

442+
// Init memory after the port in case the port needs to set aside memory.
443+
memory_init();
444+
444445
// Turn on LEDs
445446
init_status_leds();
446447
rgb_led_status_init();

ports/esp32s2/common-hal/wifi/Radio.c

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,41 @@
3131

3232
#include "esp-idf/components/esp_wifi/include/esp_wifi.h"
3333

34+
#include "esp_log.h"
35+
static const char *TAG = "cp radio";
36+
37+
static void start_station(wifi_radio_obj_t *self) {
38+
if (self->sta_mode) {
39+
return;
40+
}
41+
wifi_mode_t next_mode;
42+
if (self->ap_mode) {
43+
next_mode = WIFI_MODE_APSTA;
44+
} else {
45+
next_mode = WIFI_MODE_STA;
46+
}
47+
esp_wifi_set_mode(next_mode);
48+
49+
esp_wifi_set_config(WIFI_MODE_STA, &self->sta_config);
50+
}
51+
3452
bool common_hal_wifi_radio_get_enabled(wifi_radio_obj_t *self) {
3553
return self->started;
3654
}
3755

3856
void common_hal_wifi_radio_set_enabled(wifi_radio_obj_t *self, bool enabled) {
3957
if (self->started && !enabled) {
40-
esp_wifi_stop();
58+
ESP_LOGI(TAG, "stop");
59+
if (self->current_scan != NULL) {
60+
common_hal_wifi_radio_stop_scanning_networks(self);
61+
}
62+
ESP_ERROR_CHECK(esp_wifi_stop());
4163
self->started = false;
4264
return;
4365
}
4466
if (!self->started && enabled) {
45-
esp_wifi_start();
67+
ESP_LOGI(TAG, "start");
68+
ESP_ERROR_CHECK(esp_wifi_start());
4669
self->started = true;
4770
return;
4871
}
@@ -59,7 +82,9 @@ mp_obj_t common_hal_wifi_radio_start_scanning_networks(wifi_radio_obj_t *self) {
5982
mp_raise_RuntimeError(translate("Already scanning for wifi networks"));
6083
}
6184
// check enabled
85+
start_station(self);
6286

87+
ESP_LOGI(TAG, "start scan");
6388
wifi_scannednetworks_obj_t *scan = m_new_obj(wifi_scannednetworks_obj_t);
6489
self->current_scan = scan;
6590
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) {
7297

7398
void common_hal_wifi_radio_stop_scanning_networks(wifi_radio_obj_t *self) {
7499
// Free the memory used to store the found aps.
100+
ESP_EARLY_LOGI(TAG, "stop scan");
75101
wifi_scannednetworks_deinit(self->current_scan);
76102
self->current_scan = NULL;
103+
ESP_EARLY_LOGI(TAG, "stop scan done");
77104
}
78105

79106
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) {

ports/esp32s2/common-hal/wifi/Radio.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ typedef struct {
4343
wifi_scannednetworks_obj_t *current_scan;
4444
StaticEventGroup_t event_group;
4545
EventGroupHandle_t event_group_handle;
46+
wifi_config_t sta_config;
47+
esp_netif_t *netif;
4648
bool started;
49+
bool ap_mode;
50+
bool sta_mode;
4751
} wifi_radio_obj_t;
4852

4953
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_WIFI_RADIO_H

ports/esp32s2/common-hal/wifi/ScannedNetworks.c

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,16 @@
3838

3939
#include "esp-idf/components/esp_wifi/include/esp_wifi.h"
4040

41+
#include "esp_log.h"
42+
static const char *TAG = "cp scannednetworks";
43+
4144
static void wifi_scannednetworks_done(wifi_scannednetworks_obj_t *self) {
4245
self->done = true;
43-
free(self->results);
44-
self->results = NULL;
46+
ESP_EARLY_LOGI(TAG, "free %x", self->results);
47+
if (self->results != NULL) {
48+
m_free(self->results);
49+
self->results = NULL;
50+
}
4551
}
4652

4753
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)
7581
}
7682

7783
esp_wifi_scan_get_ap_num(&self->total_results);
84+
self->scanning = false;
7885
if (self->total_results > 0) {
7986
break;
8087
}
@@ -83,6 +90,7 @@ mp_obj_t common_hal_wifi_scannednetworks_next(wifi_scannednetworks_obj_t *self)
8390
}
8491
// We not have found any more results so we're done.
8592
if (self->done) {
93+
ESP_LOGI(TAG, "return done");
8694
return mp_const_none;
8795
}
8896
// 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)
92100
self->max_results,
93101
self->total_results,
94102
true /* allow move */);
103+
ESP_EARLY_LOGI(TAG, "alloc %x", results);
95104
if (results != NULL) {
96105
self->results = results;
97106
self->max_results = self->total_results;
@@ -128,7 +137,7 @@ mp_obj_t common_hal_wifi_scannednetworks_next(wifi_scannednetworks_obj_t *self)
128137
static uint8_t scan_pattern[] = {6, 1, 11, 3, 9, 13, 2, 4, 8, 12, 5, 7, 10, 14};
129138

130139
void wifi_scannednetworks_scan_next_channel(wifi_scannednetworks_obj_t *self) {
131-
wifi_scan_config_t config;
140+
132141
uint8_t next_channel = sizeof(scan_pattern);
133142
while (self->current_channel_index < sizeof(scan_pattern)) {
134143
next_channel = scan_pattern[self->current_channel_index];
@@ -137,11 +146,19 @@ void wifi_scannednetworks_scan_next_channel(wifi_scannednetworks_obj_t *self) {
137146
break;
138147
}
139148
}
140-
if (next_channel == sizeof(scan_pattern) ||
141-
esp_wifi_scan_start(&config, false) != ESP_OK) {
149+
wifi_scan_config_t config = { 0 };
150+
config.channel = next_channel;
151+
if (next_channel == sizeof(scan_pattern)) {
152+
ESP_LOGI(TAG, "scan done");
142153
wifi_scannednetworks_done(self);
143154
} else {
144-
self->scanning = true;
155+
esp_err_t result = esp_wifi_scan_start(&config, false);
156+
if (result != ESP_OK) {
157+
ESP_LOGI(TAG, "start failed 0x%x", result);
158+
wifi_scannednetworks_done(self);
159+
} else {
160+
self->scanning = true;
161+
}
145162
}
146163
}
147164

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ static const char *TAG = "cp wifi";
3333

3434
#include "esp-idf/components/esp_wifi/include/esp_wifi.h"
3535

36+
#include "esp-idf/components/heap/include/esp_heap_caps.h"
37+
3638
wifi_radio_obj_t common_hal_wifi_radio_obj;
3739

3840
static void event_handler(void* arg, esp_event_base_t event_base,
3941
int32_t event_id, void* event_data) {
40-
ESP_LOGI(TAG, "event");
42+
ESP_LOGI(TAG, "event %x", event_id);
4143
wifi_radio_obj_t* radio = arg;
4244
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_SCAN_DONE) {
4345
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,
6567
static bool wifi_inited;
6668

6769
void common_hal_wifi_init(void) {
68-
ESP_LOGI(TAG, "init");
70+
ESP_EARLY_LOGI(TAG, "init");
71+
heap_caps_print_heap_info(MALLOC_CAP_8BIT);
6972
wifi_inited = true;
7073
common_hal_wifi_radio_obj.base.type = &wifi_radio_type;
7174

7275
ESP_ERROR_CHECK(esp_netif_init());
7376

77+
ESP_EARLY_LOGI(TAG, "create event loop");
7478
ESP_ERROR_CHECK(esp_event_loop_create_default());
75-
esp_netif_create_default_wifi_sta();
7679

80+
ESP_EARLY_LOGI(TAG, "create wifi sta");
7781
wifi_radio_obj_t* self = &common_hal_wifi_radio_obj;
82+
self->netif = esp_netif_create_default_wifi_sta();
83+
7884
self->event_group_handle = xEventGroupCreateStatic(&self->event_group);
7985
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
8086
ESP_EVENT_ANY_ID,
@@ -88,13 +94,15 @@ void common_hal_wifi_init(void) {
8894
self->handler_instance_got_ip));
8995

9096

97+
ESP_EARLY_LOGI(TAG, "wifi init");
9198
wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT();
9299
esp_err_t result = esp_wifi_init(&config);
93100
if (result == ESP_ERR_NO_MEM) {
94101
mp_raise_msg(&mp_type_MemoryError, translate("Failed to allocate Wifi memory"));
95102
} else if (result != ESP_OK) {
96103
// handle this
97104
}
105+
ESP_EARLY_LOGI(TAG, "enable radio");
98106
common_hal_wifi_radio_set_enabled(self, true);
99107
}
100108

@@ -111,5 +119,8 @@ void wifi_reset(void) {
111119
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT,
112120
IP_EVENT_STA_GOT_IP,
113121
radio->handler_instance_got_ip));
114-
esp_wifi_deinit();
122+
ESP_ERROR_CHECK(esp_wifi_deinit());
123+
esp_netif_destroy(radio->netif);
124+
radio->netif = NULL;
125+
ESP_ERROR_CHECK(esp_netif_deinit());
115126
}

ports/esp32s2/supervisor/port.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
#include "esp_log.h"
4949
static const char *TAG = "cp port";
5050

51-
#define HEAP_SIZE (96 * 1024)
51+
#define HEAP_SIZE (64 * 1024)
5252

5353
STATIC esp_timer_handle_t _tick_timer;
5454

@@ -92,6 +92,7 @@ void reset_port(void) {
9292
#if CIRCUITPY_WIFI
9393
wifi_reset();
9494
#endif
95+
heap_caps_print_heap_info(MALLOC_CAP_8BIT);
9596
}
9697

9798
void reset_to_bootloader(void) {

shared-bindings/wifi/ScannedNetworks.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
#include "py/runtime.h"
3333
#include "shared-bindings/wifi/ScannedNetworks.h"
3434

35+
#include "esp_log.h"
36+
static const char *TAG = "cp iternext";
37+
3538
//| class ScannedNetworks:
3639
//| """Iterates over wifi `Network`s found while scanning. This object is always created
3740
//| 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) {
4346
if (network != mp_const_none) {
4447
return network;
4548
}
49+
50+
ESP_EARLY_LOGI(TAG, "stop iteration");
4651
return MP_OBJ_STOP_ITERATION;
4752
}
4853

supervisor/shared/memory.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131

3232
#include "supervisor/shared/display.h"
3333

34+
#include "esp_log.h"
35+
static const char *TAG = "memory";
36+
3437
#define CIRCUITPY_SUPERVISOR_ALLOC_COUNT (12)
3538

3639
static supervisor_allocation allocations[CIRCUITPY_SUPERVISOR_ALLOC_COUNT];
@@ -39,6 +42,7 @@ uint32_t* low_address;
3942
uint32_t* high_address;
4043

4144
void memory_init(void) {
45+
ESP_LOGE(TAG, "memory init");
4246
low_address = port_heap_get_bottom();
4347
high_address = port_heap_get_top();
4448
}

0 commit comments

Comments
 (0)