Skip to content

Commit d3e85d1

Browse files
committed
Set cyw43 power management as needed, default to disabled
.. the value actually needs to be enforced each time the STA or AP is enabled, because internally there's a call to cyw43_wifi_pm with the library's defaut power management value, not ours. Add a getter, though it only returns our idea of what the power management register is set to, it doesn't read out from the actual hardware, sadly.
1 parent 943b992 commit d3e85d1

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

ports/raspberrypi/bindings/cyw43/__init__.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@
3131
#include "shared-bindings/microcontroller/__init__.h"
3232
#include "shared-bindings/microcontroller/Pin.h"
3333
#include "bindings/cyw43/__init__.h"
34+
35+
36+
static int power_management_value = PM_DISABLED;
37+
38+
void bindings_cyw43_wifi_enforce_pm() {
39+
cyw43_wifi_pm(&cyw43_state, power_management_value);
40+
}
41+
3442
//| class CywPin:
3543
//| """A class that represents a GPIO pin attached to the wifi chip.
3644
//|
@@ -55,7 +63,7 @@ const mp_obj_type_t cyw43_pin_type = {
5563
//| PM_PERFORMANCE: int
5664
//| """Performance power management mode where more power is used to increase performance"""
5765
//| PM_DISABLED: int
58-
//| """Disable power management and always use highest power mode"""
66+
//| """Disable power management and always use highest power mode. CircuitPython sets this value at reset time, because it provides the best reliability."""
5967
//|
6068
//| def set_power_management(value: int) -> None:
6169
//| """Set the power management register
@@ -88,11 +96,20 @@ const mp_obj_type_t cyw43_pin_type = {
8896
//|
8997
STATIC mp_obj_t cyw43_set_power_management(const mp_obj_t value_in) {
9098
mp_int_t value = mp_obj_get_int(value_in);
91-
cyw43_wifi_pm(&cyw43_state, value);
99+
power_management_value = value;
100+
bindings_cyw43_wifi_enforce_pm();
92101
return mp_const_none;
93102
}
94103
STATIC MP_DEFINE_CONST_FUN_OBJ_1(cyw43_set_power_management_obj, cyw43_set_power_management);
95104

105+
//| def get_power_management() -> int:
106+
//| """Retrieve the power management register"""
107+
//|
108+
STATIC mp_obj_t cyw43_get_power_management() {
109+
return mp_obj_new_int(power_management_value);
110+
}
111+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(cyw43_get_power_management_obj, cyw43_get_power_management);
112+
96113
const mcu_pin_obj_t *validate_obj_is_pin_including_cyw43(mp_obj_t obj) {
97114
if (!mp_obj_is_type(obj, &mcu_pin_type) && !mp_obj_is_type(obj, &cyw43_pin_type)) {
98115
mp_raise_TypeError_varg(translate("Expected a %q or %q"), mcu_pin_type.name, cyw43_pin_type.name);
@@ -110,6 +127,7 @@ STATIC const mp_rom_map_elem_t cyw43_module_globals_table[] = {
110127
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_cyw43) },
111128
{ MP_ROM_QSTR(MP_QSTR_CywPin), MP_ROM_QSTR(MP_QSTR_CywPin) },
112129
{ MP_ROM_QSTR(MP_QSTR_set_power_management), &cyw43_set_power_management_obj },
130+
{ MP_ROM_QSTR(MP_QSTR_get_power_management), &cyw43_get_power_management_obj },
113131
{ MP_ROM_QSTR(MP_QSTR_PM_STANDARD), MP_ROM_INT(PM_STANDARD) },
114132
{ MP_ROM_QSTR(MP_QSTR_PM_AGGRESSIVE), MP_ROM_INT(PM_AGGRESSIVE) },
115133
{ MP_ROM_QSTR(MP_QSTR_PM_PERFORMANCE), MP_ROM_INT(PM_PERFORMANCE) },

ports/raspberrypi/bindings/cyw43/__init__.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#pragma once
2929

3030
#include "py/obj.h"
31+
#include "common-hal/microcontroller/Pin.h"
3132

3233
extern const mp_obj_type_t cyw43_pin_type;
3334
const mcu_pin_obj_t *validate_obj_is_free_pin_including_cyw43(mp_obj_t obj);
@@ -48,3 +49,5 @@ const mcu_pin_obj_t *validate_obj_is_pin_including_cyw43(mp_obj_t obj);
4849
#define PM_PERFORMANCE CONSTANT_CYW43_PM_VALUE(CYW43_PM2_POWERSAVE_MODE, 20, 1, 1, 1)
4950
// The 0xa11140 magic value
5051
#define PM_DISABLED CONSTANT_CYW43_PM_VALUE(CYW43_NO_POWERSAVE_MODE, 200, 1, 1, 10)
52+
53+
extern void bindings_cyw43_wifi_enforce_pm(void);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "shared/runtime/interrupt_char.h"
3636
#include "py/gc.h"
3737
#include "py/runtime.h"
38+
#include "bindings/cyw43/__init__.h"
3839
#include "shared-bindings/ipaddress/IPv4Address.h"
3940
#include "shared-bindings/wifi/ScannedNetworks.h"
4041
#include "shared-bindings/wifi/AuthMode.h"
@@ -152,13 +153,15 @@ void common_hal_wifi_radio_stop_scanning_networks(wifi_radio_obj_t *self) {
152153

153154
void common_hal_wifi_radio_start_station(wifi_radio_obj_t *self) {
154155
cyw43_arch_enable_sta_mode();
156+
bindings_cyw43_wifi_enforce_pm();
155157
}
156158

157159
void common_hal_wifi_radio_stop_station(wifi_radio_obj_t *self) {
158160
}
159161

160162
void common_hal_wifi_radio_start_ap(wifi_radio_obj_t *self, uint8_t *ssid, size_t ssid_len, uint8_t *password, size_t password_len, uint8_t channel, uint8_t authmode, uint8_t max_connections) {
161163
mp_raise_NotImplementedError(NULL);
164+
bindings_cyw43_wifi_enforce_pm();
162165
}
163166

164167
void common_hal_wifi_radio_stop_ap(wifi_radio_obj_t *self) {
@@ -173,6 +176,7 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t
173176
// TODO use connect_async so we can service bg tasks & check for ctrl-c during
174177
// connect
175178
int result = cyw43_arch_wifi_connect_timeout_ms((const char *)ssid, (const char *)password, CYW43_AUTH_WPA2_AES_PSK, timeout_ms);
179+
bindings_cyw43_wifi_enforce_pm();
176180
switch (result) {
177181
case 0:
178182
return WIFI_RADIO_ERROR_NONE;

0 commit comments

Comments
 (0)