Skip to content

Commit 09f6919

Browse files
committed
Add ability to read VOLTAGE_MONITOR on Pico W
Because this must be treated like an in-use pin for all other purposes, unfortunately a special case must be added in shared-bindings. Multiple AnalogIn objects for VOLTAGE_MONITOR can be created (because in use tracking isn't working) but this causes no harm. Testing performed: Read the monitor, then imported wifi. When the pin state was insufficiently restored, the second step would fail with debug messages about do_ioctl timeout. ``` import analogio, board a = analogio.AnalogIn(board.VOLTAGE_MONITOR) print(a.value) import wifi ``` Closes: #7020
1 parent 1853f49 commit 09f6919

File tree

5 files changed

+53
-8
lines changed

5 files changed

+53
-8
lines changed

ports/raspberrypi/bindings/cyw43/__init__.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,14 @@ const mcu_pin_obj_t *validate_obj_is_pin_including_cyw43(mp_obj_t obj) {
123123
return MP_OBJ_TO_PTR(obj);
124124
}
125125

126+
const mcu_pin_obj_t *validate_obj_is_free_pin_or_gpio29(mp_obj_t obj) {
127+
const mcu_pin_obj_t *pin = validate_obj_is_pin(obj);
128+
if (obj != &pin_GPIO29) {
129+
assert_pin_free(pin);
130+
}
131+
return pin;
132+
}
133+
126134
const mcu_pin_obj_t *validate_obj_is_free_pin_including_cyw43(mp_obj_t obj) {
127135
const mcu_pin_obj_t *pin = validate_obj_is_pin_including_cyw43(obj);
128136
assert_pin_free(pin);

ports/raspberrypi/bindings/cyw43/__init__.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
extern const mp_obj_type_t cyw43_pin_type;
3434
const mcu_pin_obj_t *validate_obj_is_free_pin_including_cyw43(mp_obj_t obj);
35+
const mcu_pin_obj_t *validate_obj_is_free_pin_or_gpio29(mp_obj_t obj);
3536
const mcu_pin_obj_t *validate_obj_is_pin_including_cyw43(mp_obj_t obj);
3637

3738
#define CONSTANT_CYW43_PM_VALUE(pm_mode, pm2_sleep_ret_ms, li_beacon_period, li_dtim_period, li_assoc) \

ports/raspberrypi/boards/raspberry_pi_pico_w/pins.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ STATIC const mp_rom_map_elem_t board_module_globals_table[] = {
4545
{ MP_ROM_QSTR(MP_QSTR_GP28), MP_ROM_PTR(&pin_GPIO28) },
4646
{ MP_ROM_QSTR(MP_QSTR_A2), MP_ROM_PTR(&pin_GPIO28) },
4747

48+
{ MP_ROM_QSTR(MP_QSTR_VOLTAGE_MONITOR), MP_ROM_PTR(&pin_GPIO29) },
49+
{ MP_ROM_QSTR(MP_QSTR_A3), MP_ROM_PTR(&pin_GPIO29) },
50+
51+
4852
{ MP_ROM_QSTR(MP_QSTR_STEMMA_I2C), MP_ROM_PTR(&board_i2c_obj) },
4953
};
5054
MP_DEFINE_CONST_DICT(board_module_globals, board_module_globals_table);

ports/raspberrypi/common-hal/analogio/AnalogIn.c

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,26 @@
3535
#define ADC_FIRST_PIN_NUMBER 26
3636
#define ADC_PIN_COUNT 4
3737

38+
// Voltage monitor is special on Pico W, because this pin is shared between the
39+
// voltage monitor function and the wifi function. Special handling is required
40+
// to read the analog voltage.
41+
#if CIRCUITPY_CYW43
42+
#define SPECIAL_PIN(pin) (pin->number == 29)
43+
#else
44+
#define SPECIAL_PIN(pin) false
45+
#endif
46+
3847
void common_hal_analogio_analogin_construct(analogio_analogin_obj_t *self, const mcu_pin_obj_t *pin) {
3948
if (pin->number < ADC_FIRST_PIN_NUMBER || pin->number > ADC_FIRST_PIN_NUMBER + ADC_PIN_COUNT) {
4049
raise_ValueError_invalid_pin();
4150
}
4251

4352
adc_init();
53+
if (!SPECIAL_PIN(pin)) {
54+
adc_gpio_init(pin->number);
55+
claim_pin(pin);
56+
}
4457

45-
adc_gpio_init(pin->number);
46-
47-
claim_pin(pin);
4858
self->pin = pin;
4959
}
5060

@@ -57,14 +67,29 @@ void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t *self) {
5767
return;
5868
}
5969

60-
reset_pin_number(self->pin->number);
70+
if (!SPECIAL_PIN(self->pin)) {
71+
reset_pin_number(self->pin->number);
72+
}
6173
self->pin = NULL;
6274
}
6375

6476
uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) {
65-
adc_select_input(self->pin->number - ADC_FIRST_PIN_NUMBER);
66-
uint16_t value = adc_read();
67-
77+
uint16_t value;
78+
if (SPECIAL_PIN(self->pin)) {
79+
common_hal_mcu_disable_interrupts();
80+
uint32_t old_pad = padsbank0_hw->io[self->pin->number];
81+
uint32_t old_ctrl = iobank0_hw->io[self->pin->number].ctrl;
82+
adc_gpio_init(self->pin->number);
83+
adc_select_input(self->pin->number - ADC_FIRST_PIN_NUMBER);
84+
value = adc_read();
85+
gpio_init(self->pin->number);
86+
padsbank0_hw->io[self->pin->number] = old_pad;
87+
iobank0_hw->io[self->pin->number].ctrl = old_ctrl;
88+
common_hal_mcu_enable_interrupts();
89+
} else {
90+
adc_select_input(self->pin->number - ADC_FIRST_PIN_NUMBER);
91+
value = adc_read();
92+
}
6893
// Stretch 12-bit ADC reading to 16-bit range
6994
return (value << 4) | (value >> 8);
7095
}

shared-bindings/analogio/AnalogIn.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
#include "shared-bindings/analogio/AnalogIn.h"
3737
#include "shared-bindings/util.h"
3838

39+
#if CIRCUITPY_CYW43
40+
#include "bindings/cyw43/__init__.h"
41+
#endif
42+
3943
//| class AnalogIn:
4044
//| """Read analog voltage levels
4145
//|
@@ -60,8 +64,11 @@ STATIC mp_obj_t analogio_analogin_make_new(const mp_obj_type_t *type,
6064
mp_arg_check_num(n_args, n_kw, 1, 1, false);
6165

6266
// 1st argument is the pin
67+
#if CIRCUITPY_CYW43
68+
const mcu_pin_obj_t *pin = validate_obj_is_free_pin_or_gpio29(args[0]);
69+
#else
6370
const mcu_pin_obj_t *pin = validate_obj_is_free_pin(args[0]);
64-
71+
#endif
6572
analogio_analogin_obj_t *self = m_new_obj(analogio_analogin_obj_t);
6673
self->base.type = &analogio_analogin_type;
6774
common_hal_analogio_analogin_construct(self, pin);

0 commit comments

Comments
 (0)