Skip to content

Commit 4cfef7c

Browse files
authored
Merge pull request #10202 from dhalbert/analogbufio-pin-validation
2 parents 953b7a1 + aa580ea commit 4cfef7c

File tree

3 files changed

+24
-22
lines changed

3 files changed

+24
-22
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,21 @@
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 many boards with a CYW43 radio co-processor, CYW43_DEFAULT_PIN_WL_CLOCK (usually GPIO29),
26+
// is both a voltage monitor and also SPI SCK to the CYW43.
27+
// Disallow its use for BufferedIn.
28+
#if defined(CIRCUITPY_CYW43) && defined(CYW43_DEFAULT_PIN_WL_CLOCK)
29+
|| (pin->number == CYW43_DEFAULT_PIN_WL_CLOCK)
30+
#endif
31+
) {
32+
raise_ValueError_invalid_pin();
2833
}
2934

3035
// Validate sample rate here
@@ -35,7 +40,7 @@ void common_hal_analogbufio_bufferedin_construct(analogbufio_bufferedin_obj_t *s
3540
claim_pin(pin);
3641

3742
// TODO: find a way to accept ADC4 for temperature
38-
self->chan = pin->number - ADC_FIRST_PIN_NUMBER;
43+
self->chan = pin->number - ADC_BASE_PIN;
3944

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

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

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,12 @@
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
24-
// voltage monitor function and the wifi function. Special handling is required
25-
// to read the analog voltage.
15+
// On many boards with a CYW43 radio co-processor, CYW43_DEFAULT_PIN_WL_CLOCK (usually GPIO29),
16+
// is both a voltage monitor and also SPI SCK to the CYW43.
17+
// Special handling is required to read the analog voltage.
2618
#if CIRCUITPY_CYW43
2719
#include "bindings/cyw43/__init__.h"
28-
#define SPECIAL_PIN(pin) (pin->number == 29)
20+
#define SPECIAL_PIN(pin) (pin->number == CYW43_DEFAULT_PIN_WL_CLOCK)
2921

3022
const mcu_pin_obj_t *common_hal_analogio_analogin_validate_pin(mp_obj_t obj) {
3123
return validate_obj_is_free_pin_or_gpio29(obj, MP_QSTR_pin);
@@ -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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@
4040
//| """Create a `BufferedIn` on the given pin and given sample rate.
4141
//|
4242
//| :param ~microcontroller.Pin pin: the pin to read from
43-
//| :param ~int sample_rate: rate: sampling frequency, in samples per second"""
43+
//| :param ~int sample_rate: rate: sampling frequency, in samples per second
44+
//|
45+
//| **Limitations**: On many boards with a CYW43 radio module, such as Pico W,
46+
//| GPIO29 (often ``board.A3``) is also used to control the CYW43,
47+
//| and is therefore not available to use as the `BufferedIn` pin.
48+
//| """
4449
//| ...
4550
//|
4651
static mp_obj_t analogbufio_bufferedin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy