Skip to content

Commit 148b250

Browse files
committed
src: Disable PM mode during wifi join.
Had a few reports that joining wifi can be very slow. Disabling power management seems to fix the problem. The PM mode is cleared when a join is started and restored when complete on success or failure. Fixes #9
1 parent b06bfb3 commit 148b250

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

src/cyw43.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ typedef struct _cyw43_t {
124124

125125
// mac from otp (or from cyw43_hal_generate_laa_mac if not set)
126126
uint8_t mac[6];
127+
128+
// Power management setting, restored after connect
129+
uint32_t saved_pm;
127130
} cyw43_t;
128131

129132
extern cyw43_t cyw43_state;

src/cyw43_ctrl.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ STATIC void cyw43_dump_async_event(const cyw43_async_event_t *ev) {
309309

310310
void cyw43_cb_process_async_event(void *cb_data, const cyw43_async_event_t *ev) {
311311
cyw43_t *self = cb_data;
312+
bool connect_done = false;
312313

313314
if (self->trace_flags & CYW43_TRACE_ASYNC_EV) {
314315
cyw43_dump_async_event(ev);
@@ -352,10 +353,12 @@ void cyw43_cb_process_async_event(void *cb_data, const cyw43_async_event_t *ev)
352353
// Success setting SSID
353354
} else if (ev->status == 3 && ev->reason == 0) {
354355
self->wifi_join_state = WIFI_JOIN_STATE_NONET;
356+
connect_done = true;
355357
// No matching SSID found (could be out of range, or down)
356358
} else {
357359
// Other failure setting SSID
358360
self->wifi_join_state = WIFI_JOIN_STATE_FAIL;
361+
connect_done = true;
359362
}
360363
} else if (ev->event_type == CYW43_EV_AUTH) {
361364
if (ev->status == 0) {
@@ -369,6 +372,7 @@ void cyw43_cb_process_async_event(void *cb_data, const cyw43_async_event_t *ev)
369372
} else {
370373
// Cannot authenticate
371374
self->wifi_join_state = WIFI_JOIN_STATE_BADAUTH;
375+
connect_done = true;
372376
}
373377
} else if (ev->event_type == CYW43_EV_DEAUTH_IND) {
374378
if (ev->status == 0 && ev->reason == 2) {
@@ -401,13 +405,22 @@ void cyw43_cb_process_async_event(void *cb_data, const cyw43_async_event_t *ev)
401405
} else {
402406
// PSK_SUP failure
403407
self->wifi_join_state = WIFI_JOIN_STATE_BADAUTH;
408+
connect_done = true;
404409
}
405410
}
406411

407412
if (self->wifi_join_state == WIFI_JOIN_STATE_ALL) {
413+
408414
// STA connected
409415
self->wifi_join_state = WIFI_JOIN_STATE_ACTIVE;
410416
cyw43_cb_tcpip_set_link_up(self, CYW43_ITF_STA);
417+
connect_done = true;
418+
}
419+
420+
// Restore power management value
421+
if (connect_done && self->saved_pm) {
422+
cyw43_wifi_pm(self, self->saved_pm);
423+
self->saved_pm = 0;
411424
}
412425
}
413426

@@ -604,6 +617,11 @@ int cyw43_wifi_join(cyw43_t *self, size_t ssid_len, const uint8_t *ssid, size_t
604617
return ret;
605618
}
606619

620+
// Disable power management while connecting
621+
if (cyw43_wifi_get_pm(self, &self->saved_pm) == 0) {
622+
cyw43_wifi_pm(self, self->saved_pm & ~0xf);
623+
}
624+
607625
ret = cyw43_ll_wifi_join(&self->cyw43_ll, ssid_len, ssid, key_len, key, auth_type, bssid, channel);
608626

609627
if (ret == 0) {

0 commit comments

Comments
 (0)