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().
pull/9221/head
yn386 2022-08-28 15:40:00 +09:00 zatwierdzone przez Damien George
rodzic 4e4c28bf27
commit 8770cd2f4d
1 zmienionych plików z 3 dodań i 0 usunięć

Wyświetl plik

@ -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);
}