From 8770cd2f4d24b1ad14c934b4161e42108fe98f84 Mon Sep 17 00:00:00 2001 From: yn386 Date: Sun, 28 Aug 2022 15:40:00 +0900 Subject: [PATCH] stm32/adc: Make ADCAll.read_channel reject invalid channels. pyb.ADC(channel) checks whether specified channel is valid or have ADC capability but pyb.ADCAll().read_channel() does not. This change adds checking whether specified channel is valid and throw ValueError if channel is invalid. This is same as pyb.ADC(). --- ports/stm32/adc.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ports/stm32/adc.c b/ports/stm32/adc.c index 9b7788e59e..9d58cf2e7c 100644 --- a/ports/stm32/adc.c +++ b/ports/stm32/adc.c @@ -856,6 +856,9 @@ STATIC mp_obj_t adc_all_make_new(const mp_obj_type_t *type, size_t n_args, size_ STATIC mp_obj_t adc_all_read_channel(mp_obj_t self_in, mp_obj_t channel) { pyb_adc_all_obj_t *self = MP_OBJ_TO_PTR(self_in); uint32_t chan = adc_get_internal_channel(mp_obj_get_int(channel)); + if (!is_adcx_channel(chan)) { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("not a valid ADC Channel: %d"), chan); + } uint32_t data = adc_config_and_read_channel(&self->handle, chan); return mp_obj_new_int(data); }