8000 Fix analogbufio.BufferedIn pin validation on RP2350B · adafruit/circuitpython@a8b55b4 · GitHub
[go: up one dir, main page]

Skip to content

Commit a8b55b4

Browse files
committed
Fix analogbufio.BufferedIn pin validation on RP2350B
1 parent f108587 commit a8b55b4

File tree

3 files changed

+15
-18
lines changed

3 files changed

+15
-18
lines changed

ports/raspberrypi/common-hal/analogbufio/BufferedIn.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,18 @@
1515
#include "hardware/dma.h"
1616
#include "pico/stdlib.h"
1717

18-
#define ADC_FIRST_PIN_NUMBER 26
19-
#define ADC_PIN_COUNT 4
20-
2118
#define ADC_CLOCK_INPUT 48000000
2219
#define ADC_MAX_CLOCK_DIV (1 << (ADC_DIV_INT_MSB - ADC_DIV_INT_LSB + 1))
2320

2421
void common_hal_analogbufio_bufferedin_construct(analogbufio_bufferedin_obj_t *self, const mcu_pin_obj_t *pin, uint32_t sample_rate) {
2522
// Make sure pin number is in range for ADC
26-
if (pin->number < ADC_FIRST_PIN_NUMBER || pin->number >= (ADC_FIRST_PIN_NUMBER + ADC_PIN_COUNT)) {
27-
raise_ValueError_invalid_pins();
23+
if ((pin->number < ADC_BASE_PIN) ||
24+
(pin->number > ADC_BASE_PIN + NUM_ADC_CHANNELS - 1) ||
25+
// On Pico W and Pico 2 W, GPIO29 is both a voltage monitor and used for SPI to the CYW43.
26+
// Disallow its use for BufferedIn.
27+
(CIRCUITPY_CYW43 && pin->number == 29)
28+
) {
29+
raise_ValueError_invalid_pin();
2830
}
2931

3032
// Validate sample rate here
@@ -35,7 +37,7 @@ void common_hal_analogbufio_bufferedin_construct(analogbufio_bufferedin_obj_t *s
3537
claim_pin(pin);
3638

3739
// TODO: find a way to accept ADC4 for temperature
38-
self->chan = pin->number - ADC_FIRST_PIN_NUMBER;
40+
self->chan = pin->number - ADC_BASE_PIN;
3941

4042
// Init GPIO for analogue use: hi-Z, no pulls, disable digital input buffer.
4143
// TODO: Make sure we share the ADC well. Right now we just assume it is

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,7 @@
1212

1313
#include "hardware/adc.h"
1414

15-
#define ADC_PIN_COUNT (NUM_ADC_CHANNELS - 1)
16-
17-
#if ADC_PIN_COUNT == 4
18-
#define ADC_FIRST_PIN_NUMBER 26
19-
#else
20-
#define ADC_FIRST_PIN_NUMBER 40
21-
#endif
22-
23-
// Voltage monitor is special on Pico W, because this pin is shared between the
15+
// Voltage monitor is special on Pico W and Pico 2 W, because this pin is shared between the
2416
// voltage monitor function and the wifi function. Special handling is required
2517
// to read the analog voltage.
2618
#if CIRCUITPY_CYW43
@@ -35,7 +27,7 @@ const mcu_pin_obj_t *common_hal_analogio_analogin_validate_pin(mp_obj_t obj) {
3527
#endif
3628

3729
void common_hal_analogio_analogin_construct(analogio_analogin_obj_t *self, const mcu_pin_obj_t *pin) {
38-
if (pin->number < ADC_FIRST_PIN_NUMBER || pin->number > ADC_FIRST_PIN_NUMBER + ADC_PIN_COUNT) {
30+
if (pin->number < ADC_BASE_PIN || pin->number > ADC_BASE_PIN + NUM_ADC_CHANNELS - 1) {
3931
raise_ValueError_invalid_pin();
4032
}
4133

@@ -70,15 +62,15 @@ uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) {
7062
uint32_t old_pad = pads_bank0_hw->io[self->pin->number];
7163
uint32_t old_ctrl = io_bank0_hw->io[self->pin->number].ctrl;
7264
adc_gpio_init(self->pin->number);
73-
adc_select_input(self->pin->number - ADC_FIRST_PIN_NUMBER);
65+
adc_select_input(self->pin->number - ADC_BASE_PIN);
7466
common_hal_mcu_delay_us(100);
7567
value = adc_read();
7668
gpio_init(self->pin->number);
7769
pads_bank0_hw->io[self->pin->number] = old_pad;
7870
io_bank0_hw->io[self->pin->number].ctrl = old_ctrl;
7971
common_hal_mcu_enable_interrupts();
8072
} else {
81-
adc_select_input(self->pin->number - ADC_FIRST_PIN_NUMBER);
73+
adc_select_input(self->pin->number - ADC_BASE_PIN);
8274
value = adc_read();
8375
}
8476
// Stretch 12-bit ADC reading to 16-bit range

shared-bindings/analogbufio/BufferedIn.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
//| def __init__(self, pin: microcontroller.Pin, *, sample_rate: int) -> None:
4040
//| """Create a `BufferedIn` on the given pin and given sample rate.
4141
//|
42+
//| **Limitations**: On Pi Pico W and Pi Pico 2 W, pin ``board.A3`` is not supported
43+
//| because it is also used to control the CYW43 radio module.
44+
//|
4245
//| :param ~microcontroller.Pin pin: the pin to read from
4346
//| :param ~int sample_rate: rate: sampling frequency, in samples per second"""
4447
//| ...

0 commit comments

Comments
 (0)
0