diff options
Diffstat (limited to 'stmhal/adc.c')
-rw-r--r-- | stmhal/adc.c | 25 |
1 files changed, 20 insertions, 5 deletions
diff --git a/stmhal/adc.c b/stmhal/adc.c index 39f0364078..bcf8956254 100644 --- a/stmhal/adc.c +++ b/stmhal/adc.c @@ -54,7 +54,16 @@ #define ADCx (ADC1) #define ADCx_CLK_ENABLE __ADC1_CLK_ENABLE #define ADC_NUM_CHANNELS (19) -#define ADC_NUM_GPIO_CHANNELS (16) + +#if defined(MCU_SERIES_F4) || defined(MCU_SERIES_F7) +#define ADC_FIRST_GPIO_CHANNEL (0) +#define ADC_LAST_GPIO_CHANNEL (15) +#elif defined(MCU_SERIES_L4) +#define ADC_FIRST_GPIO_CHANNEL (1) +#define ADC_LAST_GPIO_CHANNEL (16) +#else +#error Unsupported processor +#endif #if defined(STM32F405xx) || defined(STM32F415xx) || \ defined(STM32F407xx) || defined(STM32F417xx) || \ @@ -124,7 +133,7 @@ STATIC void adc_init_single(pyb_obj_adc_t *adc_obj) { return; } - if (adc_obj->channel < ADC_NUM_GPIO_CHANNELS) { + if (ADC_FIRST_GPIO_CHANNEL <= adc_obj->channel && adc_obj->channel <= ADC_LAST_GPIO_CHANNEL) { // Channels 0-16 correspond to real pins. Configure the GPIO pin in // ADC mode. const pin_obj_t *pin = pin_adc1[adc_obj->channel]; @@ -253,8 +262,14 @@ STATIC mp_obj_t adc_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uin if (!is_adcx_channel(channel)) { nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "not a valid ADC Channel: %d", channel)); } - if (pin_adc1[channel] == NULL) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "channel %d not available on this board", channel)); + + + if (ADC_FIRST_GPIO_CHANNEL <= channel && channel <= ADC_LAST_GPIO_CHANNEL) { + // these channels correspond to physical GPIO ports so make sure they exist + if (pin_adc1[channel] == NULL) { + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, + "channel %d not available on this board", channel)); + } } pyb_obj_adc_t *o = m_new_obj(pyb_obj_adc_t); @@ -423,7 +438,7 @@ void adc_init_all(pyb_adc_all_obj_t *adc_all, uint32_t resolution) { "resolution %d not supported", resolution)); } - for (uint32_t channel = 0; channel < ADC_NUM_GPIO_CHANNELS; channel++) { + for (uint32_t channel = ADC_FIRST_GPIO_CHANNEL; channel <= ADC_LAST_GPIO_CHANNEL; ++channel) { // Channels 0-16 correspond to real pins. Configure the GPIO pin in // ADC mode. const pin_obj_t *pin = pin_adc1[channel]; |