Skip to content

Commit 2b8de74

Browse files
robert-hhdpgeorge
authored andcommitted
rp2/machine_adc: Initialise ADC GPIO when a Pin is referenced by int.
The change closes the gap in the API when an integer is used as Pin reference. With the change, e.g. ADC(26), ADC(Pin(26)) and ADC("GP26") behave identically and the GPIO is initialised in ACD/high-Z mode. Only when using ADC channel numbers 0-3 are the corresponding GPIO left uninitialised, and then the user is responsible for configuring the GPIO. Signed-off-by: robert-hh <[email protected]>
1 parent f61fac0 commit 2b8de74

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

ports/rp2/machine_adc.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ static mp_obj_t mp_machine_adc_make_new(const mp_obj_type_t *type, size_t n_args
7474
const machine_pin_obj_t *pin = NULL;
7575

7676
if (mp_obj_is_int(source)) {
77-
// Get and validate channel number.
7877
channel = mp_obj_get_int(source);
79-
if (ADC_IS_VALID_GPIO(channel)) {
80-
channel = ADC_CHANNEL_FROM_GPIO(channel);
81-
} else if (!(channel >= 0 && channel <= ADC_CHANNEL_TEMPSENSOR)) {
82-
mp_raise_ValueError(MP_ERROR_TEXT("invalid channel"));
78+
if (!(channel >= 0 && channel <= ADC_CHANNEL_TEMPSENSOR)) {
79+
// Not a valid ADC channel, fallback to searching for a pin.
80+
channel = -1;
8381
}
84-
} else {
82+
}
83+
84+
if (channel == -1) {
8585
// Get GPIO and check it has ADC capabilities.
8686
pin = machine_pin_find(source);
8787
bool valid_adc_pin = false;

0 commit comments

Comments
 (0)