3131#include "hardware/adc.h"
3232#include "machine_pin.h"
3333
34- #define ADC_IS_VALID_GPIO (gpio ) ((gpio) >= 26 && (gpio) <= 29)
35- #define ADC_CHANNEL_FROM_GPIO (gpio ) ((gpio) - 26)
36- #define ADC_CHANNEL_TEMPSENSOR (4)
34+ #define ADC_IS_VALID_GPIO (gpio ) ((gpio) >= ADC_BASE_PIN && (gpio) < (ADC_BASE_PIN + NUM_ADC_CHANNELS))
35+ #define ADC_CHANNEL_FROM_GPIO (gpio ) ((gpio) - ADC_BASE_PIN)
3736
3837static uint16_t adc_config_and_read_u16 (uint32_t channel ) {
3938 adc_select_input (channel );
@@ -47,7 +46,7 @@ static uint16_t adc_config_and_read_u16(uint32_t channel) {
4746// MicroPython bindings for machine.ADC
4847
4948#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS \
50- { MP_ROM_QSTR(MP_QSTR_CORE_TEMP), MP_ROM_INT(ADC_CHANNEL_TEMPSENSOR ) }, \
49+ { MP_ROM_QSTR(MP_QSTR_CORE_TEMP), MP_ROM_INT(ADC_TEMPERATURE_CHANNEL_NUM ) }, \
5150
5251typedef struct _machine_adc_obj_t {
5352 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
7574
7675 if (mp_obj_is_int (source )) {
7776 channel = mp_obj_get_int (source );
78- if (!(channel >= 0 && channel <= ADC_CHANNEL_TEMPSENSOR )) {
77+ if (!(channel >= 0 && channel < NUM_ADC_CHANNELS )) {
7978 // Not a valid ADC channel, fallback to searching for a pin.
8079 channel = -1 ;
8180 }
@@ -116,7 +115,7 @@ static mp_obj_t mp_machine_adc_make_new(const mp_obj_type_t *type, size_t n_args
116115 adc_gpio_init (pin -> id );
117116 channel = ADC_CHANNEL_FROM_GPIO (pin -> id );
118117 }
119- } else if (channel == ADC_CHANNEL_TEMPSENSOR ) {
118+ } else if (channel == ADC_TEMPERATURE_CHANNEL_NUM ) {
120119 // Enable temperature sensor.
121120 adc_set_temp_sensor_enabled (1 );
122121 }
0 commit comments