From 34e463d861c5ebb33ec317749a008d461616cfbc Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Mon, 5 Aug 2024 22:02:13 +0100 Subject: [PATCH] rp2/machine_adc: Add ADC support for RP2350B. Signed-off-by: Damien George --- ports/rp2/machine_adc.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/ports/rp2/machine_adc.c b/ports/rp2/machine_adc.c index 3594f4b12..40c216d59 100644 --- a/ports/rp2/machine_adc.c +++ b/ports/rp2/machine_adc.c @@ -31,9 +31,8 @@ #include "hardware/adc.h" #include "machine_pin.h" -#define ADC_IS_VALID_GPIO(gpio) ((gpio) >= 26 && (gpio) <= 29) -#define ADC_CHANNEL_FROM_GPIO(gpio) ((gpio) - 26) -#define ADC_CHANNEL_TEMPSENSOR (4) +#define ADC_IS_VALID_GPIO(gpio) ((gpio) >= ADC_BASE_PIN && (gpio) < (ADC_BASE_PIN + NUM_ADC_CHANNELS)) +#define ADC_CHANNEL_FROM_GPIO(gpio) ((gpio) - ADC_BASE_PIN) static uint16_t adc_config_and_read_u16(uint32_t channel) { adc_select_input(channel); @@ -47,7 +46,7 @@ static uint16_t adc_config_and_read_u16(uint32_t channel) { // MicroPython bindings for machine.ADC #define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS \ - { MP_ROM_QSTR(MP_QSTR_CORE_TEMP), MP_ROM_INT(ADC_CHANNEL_TEMPSENSOR) }, \ + { MP_ROM_QSTR(MP_QSTR_CORE_TEMP), MP_ROM_INT(ADC_TEMPERATURE_CHANNEL_NUM) }, \ typedef struct _machine_adc_obj_t { mp_obj_base_t base; @@ -75,7 +74,7 @@ static mp_obj_t mp_machine_adc_make_new(const mp_obj_type_t *type, size_t n_args if (mp_obj_is_int(source)) { channel = mp_obj_get_int(source); - if (!(channel >= 0 && channel <= ADC_CHANNEL_TEMPSENSOR)) { + if (!(channel >= 0 && channel < NUM_ADC_CHANNELS)) { // Not a valid ADC channel, fallback to searching for a pin. channel = -1; } @@ -116,7 +115,7 @@ static mp_obj_t mp_machine_adc_make_new(const mp_obj_type_t *type, size_t n_args adc_gpio_init(pin->id); channel = ADC_CHANNEL_FROM_GPIO(pin->id); } - } else if (channel == ADC_CHANNEL_TEMPSENSOR) { + } else if (channel == ADC_TEMPERATURE_CHANNEL_NUM) { // Enable temperature sensor. adc_set_temp_sensor_enabled(1); }