forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy path__init__.c
More file actions
149 lines (131 loc) · 5.93 KB
/
__init__.c
File metadata and controls
149 lines (131 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// This file is part of the CircuitPython project: https://circuitpython.org
//
// SPDX-FileCopyrightText: Copyright (c) 2022 Jeff Epler for Adafruit Industries
// SPDX-FileCopyrightText: Copyright (c) 2016 Scott Shawcroft
//
// SPDX-License-Identifier: MIT
#include "py/runtime.h"
#include "shared-bindings/board/__init__.h"
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "bindings/cyw43/__init__.h"
#include "src/rp2_common/hardware_gpio/include/hardware/gpio.h"
#include "lib/cyw43-driver/src/cyw43.h"
#include "pico/cyw43_arch.h"
static int power_management_value = PM_DISABLED;
// called from common-hal/alarm/__init__.c
void bindings_cyw43_power_down(void) {
cyw43_arch_deinit();
gpio_set_dir(CYW43_DEFAULT_PIN_WL_REG_ON, GPIO_OUT);
gpio_put(CYW43_DEFAULT_PIN_WL_REG_ON, false);
}
// called from supervisor/port.c and common-hal/alarm/__init__.c
bool bindings_cyw43_power_up(void) {
gpio_set_dir(CYW43_DEFAULT_PIN_WL_REG_ON, GPIO_OUT);
gpio_put(CYW43_DEFAULT_PIN_WL_REG_ON, true);
// Change this as a placeholder as to how to init with country code.
// Default country code is CYW43_COUNTRY_WORLDWIDE)
return cyw43_arch_init_with_country(PICO_CYW43_ARCH_DEFAULT_COUNTRY_CODE);
}
void bindings_cyw43_wifi_enforce_pm(void) {
cyw43_wifi_pm(&cyw43_state, power_management_value);
}
//| class CywPin:
//| """A class that represents a GPIO pin attached to the wifi chip.
//|
//| Cannot be constructed at runtime, but may be the type of a pin object
//| in :py:mod:`board`. A `CywPin` can be used as a DigitalInOut, but not with other
//| peripherals such as `PWMOut`."""
//|
MP_DEFINE_CONST_OBJ_TYPE(
cyw43_pin_type,
MP_QSTR_CywPin,
MP_TYPE_FLAG_NONE,
print, shared_bindings_microcontroller_pin_print
);
//| PM_STANDARD: int
//| """The standard power management mode"""
//| PM_AGGRESSIVE: int
//| """Aggressive power management mode for optimal power usage at the cost of performance"""
//| PM_PERFORMANCE: int
//| """Performance power management mode where more power is used to increase performance"""
//| PM_DISABLED: int
//| """Disable power management and always use highest power mode. CircuitPython sets this value at reset time, because it provides the best connectivity reliability."""
//|
//| def set_power_management(value: int) -> None:
//| """Set the power management register
//|
//| For transmitter power, see ``wifi.Radio.txpower``.
//| This controls software power saving features inside the cyw43 chip.
//| it does not control transmitter power.
//|
//| The value is interpreted as a 24-bit hexadecimal number of the form
//| ``0x00adbrrm``.
//|
//| The low 4 bits, ``m``, are the power management mode:
//| * 0: disabled
//| * 1: aggressive power saving which reduces wifi throughput
//| * 2: Power saving with high throughput
//|
//| The next 8 bits, ``r``, specify "the maximum time to wait before going back to sleep" for power management mode 2. The units of ``r`` are 10ms.
//|
//| The next 4 bits, ``b``, are the "wake period is measured in beacon periods".
//|
//| The next 4 bits, ``d``, specify the "wake interval measured in DTIMs. If this is set to 0, the wake interval is measured in beacon periods".
//|
//| The top 4 bits, ``a``, specifies the "wake interval sent to the access point"
//|
//| Several ``PM_`` constants gathered from various sources are included
//| in this module. According to Raspberry Pi documentation, the value 0xa11140
//| (called `cyw43.PM_DISABLED` here) increases responsiveness at the cost of higher power
//| usage.
//| """
//|
static mp_obj_t cyw43_set_power_management(const mp_obj_t value_in) {
mp_int_t value = mp_obj_get_int(value_in);
power_management_value = value;
bindings_cyw43_wifi_enforce_pm();
return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_1(cyw43_set_power_management_obj, cyw43_set_power_management);
//| def get_power_management() -> int:
//| """Retrieve the power management register"""
//|
static mp_obj_t cyw43_get_power_management() {
return mp_obj_new_int(power_management_value);
}
static MP_DEFINE_CONST_FUN_OBJ_0(cyw43_get_power_management_obj, cyw43_get_power_management);
const mcu_pin_obj_t *validate_obj_is_pin_including_cyw43(mp_obj_t obj, qstr arg_name) {
if (!mp_obj_is_type(obj, &mcu_pin_type) && !mp_obj_is_type(obj, &cyw43_pin_type)) {
mp_raise_TypeError_varg(MP_ERROR_TEXT("%q must be of type %q or %q, not %q"), arg_name, mcu_pin_type.name, cyw43_pin_type.name, mp_obj_get_type(obj)->name);
}
return MP_OBJ_TO_PTR(obj);
}
const mcu_pin_obj_t *validate_obj_is_free_pin_or_gpio29(mp_obj_t obj, qstr arg_name) {
const mcu_pin_obj_t *pin = validate_obj_is_pin(obj, arg_name);
if (obj != &pin_GPIO29) {
assert_pin_free(pin);
}
return pin;
}
const mcu_pin_obj_t *validate_obj_is_free_pin_including_cyw43(mp_obj_t obj, qstr arg_name) {
const mcu_pin_obj_t *pin = validate_obj_is_pin_including_cyw43(obj, arg_name);
assert_pin_free(pin);
return pin;
}
static const mp_rom_map_elem_t cyw43_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_cyw43) },
{ MP_ROM_QSTR(MP_QSTR_CywPin), MP_ROM_PTR(&cyw43_pin_type) },
{ MP_ROM_QSTR(MP_QSTR_set_power_management), &cyw43_set_power_management_obj },
{ MP_ROM_QSTR(MP_QSTR_get_power_management), &cyw43_get_power_management_obj },
{ MP_ROM_QSTR(MP_QSTR_PM_STANDARD), MP_ROM_INT(PM_STANDARD) },
{ MP_ROM_QSTR(MP_QSTR_PM_AGGRESSIVE), MP_ROM_INT(PM_AGGRESSIVE) },
{ MP_ROM_QSTR(MP_QSTR_PM_PERFORMANCE), MP_ROM_INT(PM_PERFORMANCE) },
{ MP_ROM_QSTR(MP_QSTR_PM_DISABLED), MP_ROM_INT(PM_DISABLED) },
};
static MP_DEFINE_CONST_DICT(cyw43_module_globals, cyw43_module_globals_table);
const mp_obj_module_t cyw43_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t *)&cyw43_module_globals,
};
MP_REGISTER_MODULE(MP_QSTR_cyw43, cyw43_module);