Skip to content

Commit acf827a

Browse files
committed
shutdown cyw43 during sleep; various bug-fixes and simplifications
1 parent af68bbd commit acf827a

File tree

7 files changed

+46
-13
lines changed

7 files changed

+46
-13
lines changed

ports/raspberrypi/bindings/cyw43/__init__.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,24 @@
1515
#include "src/rp2_common/hardware_gpio/include/hardware/gpio.h"
1616

1717
#include "lib/cyw43-driver/src/cyw43.h"
18+
#include "pico/cyw43_arch.h"
1819

1920
static int power_management_value = PM_DISABLED;
2021

21-
void cyw43_enter_deep_sleep(void) {
22-
#define WL_REG_ON 23
23-
gpio_set_dir(WL_REG_ON, GPIO_OUT);
24-
gpio_put(WL_REG_ON, false);
22+
// called from common-hal/alarm/__init__.c
23+
void bindings_cyw43_power_down(void) {
24+
cyw43_arch_deinit();
25+
gpio_set_dir(CYW43_DEFAULT_PIN_WL_REG_ON, GPIO_OUT);
26+
gpio_put(CYW43_DEFAULT_PIN_WL_REG_ON, false);
27+
}
28+
29+
// called from supervisor/port.c and common-hal/alarm/__init__.c
30+
bool bindings_cyw43_power_up(void) {
31+
gpio_set_dir(CYW43_DEFAULT_PIN_WL_REG_ON, GPIO_OUT);
32+
gpio_put(CYW43_DEFAULT_PIN_WL_REG_ON, true);
33+
// Change this as a placeholder as to how to init with country code.
34+
// Default country code is CYW43_COUNTRY_WORLDWIDE)
35+
return cyw43_arch_init_with_country(PICO_CYW43_ARCH_DEFAULT_COUNTRY_CODE);
2536
}
2637

2738
void bindings_cyw43_wifi_enforce_pm(void) {

ports/raspberrypi/bindings/cyw43/__init__.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ const mcu_pin_obj_t *validate_obj_is_pin_including_cyw43(mp_obj_t obj, qstr arg_
3333
#define PM_DISABLED CONSTANT_CYW43_PM_VALUE(CYW43_NO_POWERSAVE_MODE, 200, 1, 1, 10)
3434

3535
extern void bindings_cyw43_wifi_enforce_pm(void);
36-
void cyw43_enter_deep_sleep(void);
36+
extern void bindings_cyw43_power_down(void);
37+
extern bool bindings_cyw43_power_up(void);

ports/raspberrypi/common-hal/alarm/pin/PinAlarm.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ static void gpio_callback(uint gpio, uint32_t events) {
3030

3131
if (_not_yet_deep_sleeping) {
3232
// Event went off prematurely, before we went to sleep, so set it again.
33-
gpio_set_irq_enabled(gpio, events, false);
33+
gpio_set_irq_enabled(gpio, events, true);
3434
} else {
3535
// Went off during sleep.
3636
// Disable IRQ automatically.
@@ -109,6 +109,7 @@ void alarm_pin_pinalarm_reset(void) {
109109
for (size_t i = 0; i < NUM_BANK0_GPIOS; i++) {
110110
if (alarm_reserved_pins & (1 << i)) {
111111
gpio_set_irq_enabled(i, GPIO_IRQ_ALL_EVENTS, false);
112+
gpio_set_dormant_irq_enabled(i, GPIO_IRQ_ALL_EVENTS, false);
112113
reset_pin_number(i);
113114
}
114115
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ void common_hal_wifi_init(bool user_initiated) {
4949
common_hal_wifi_radio_set_enabled(self, true);
5050
}
5151

52+
// reset after sleep (called from common-hal/alarm/__init__.c)
53+
void wifi_power_up_reset(void) {
54+
if (!wifi_ever_inited) {
55+
// nothing to do
56+
return;
57+
} else {
58+
// start station, regardless if wifi is currently disabled
59+
// (enabling does not start station, why?)
60+
wifi_radio_obj_t *self = &common_hal_wifi_radio_obj;
61+
common_hal_wifi_radio_start_station(self);
62+
}
63+
}
64+
5265
void wifi_user_reset(void) {
5366
if (wifi_user_initiated) {
5467
wifi_reset();

ports/raspberrypi/common-hal/wifi/__init__.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "lwip/ip_addr.h"
1212

1313
void wifi_reset(void);
14+
void wifi_power_up_reset(void);
1415
NORETURN void raise_cyw_error(int err);
1516
#define CHECK_CYW_RESULT(x) do { int res = (x); if (res != 0) raise_cyw_error(res); } while (0)
1617

ports/raspberrypi/supervisor/port.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#endif
2828

2929
#if CIRCUITPY_WIFI
30+
#include "py/mphal.h"
3031
#include "common-hal/wifi/__init__.h"
3132
#endif
3233

@@ -43,7 +44,7 @@
4344
#include "src/rp2_common/hardware_sync/include/hardware/sync.h"
4445
#include "src/rp2_common/hardware_timer/include/hardware/timer.h"
4546
#if CIRCUITPY_CYW43
46-
#include "py/mphal.h"
47+
#include "bindings/cyw43/__init__.h"
4748
#include "pico/cyw43_arch.h"
4849
#endif
4950
#include "src/common/pico_time/include/pico/time.h"
@@ -352,9 +353,7 @@ safe_mode_t port_init(void) {
352353
// are intended to meet the power on timing requirements, but apparently
353354
// are inadequate. We'll back off this long delay based on future testing.
354355
mp_hal_delay_ms(1000);
355-
// Change this as a placeholder as to how to init with country code.
356-
// Default country code is CYW43_COUNTRY_WORLDWIDE)
357-
if (cyw43_arch_init_with_country(PICO_CYW43_ARCH_DEFAULT_COUNTRY_CODE)) {
356+
if (bindings_cyw43_power_up()) {
358357
serial_write("WiFi init failed\n");
359358
} else {
360359
cyw_ever_init = true;

shared-bindings/alarm/__init__.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,16 @@
3030
//| There are two supported levels of sleep: light sleep and deep sleep.
3131
//|
3232
//| Light sleep keeps sufficient state so the program can resume after sleeping.
33-
//| It does not shut down WiFi, BLE, or other communications, or ongoing activities such
34-
//| as audio playback. It reduces power consumption to the extent possible that leaves
35-
//| these continuing activities running. In some cases there may be no decrease in power consumption.
33+
//| Otherwise, the behavior is implementation-dependent. WiFi, BLE or other
34+
//| communications or ongoing activities such as audio playback may or may not work during or
35+
//| after light sleep.
36+
//| In some cases there may be no decrease in power consumption.
37+
//|
38+
//| .. note::
39+
//| **Implementation note for RP2xxx**: light sleep uses aggressive power-saving. For the W-variants,
40+
//| It shuts down WiFi and restarts it after wakeup. User code has to reconnect after wakeup to an
41+
//| AP if necessary. From a power-saving perspective, light and deep sleep are identical (< 2mA@5V),
42+
//| but light sleep does not reset after wakeup like deep sleep does.
3643
//|
3744
//| Deep sleep shuts down power to nearly all of the microcontroller including the CPU and RAM. This can save
3845
//| a more significant amount of power, but CircuitPython must restart ``code.py`` from the beginning when

0 commit comments

Comments
 (0)