Skip to content

Commit 029f215

Browse files
committed
stmhal/adc: Add "mask" selection parameter to pyb.ADCAll constructor.
The "mask" parameter is used to select which pins the ADCAll constructor will initialise to analog mode. It defaults to all pins (0xffffffff), which is backwards compatible with previous behaviour.
1 parent 0d75b0d commit 029f215

1 file changed

Lines changed: 22 additions & 12 deletions

File tree

stmhal/adc.c

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ typedef struct _pyb_adc_all_obj_t {
426426
ADC_HandleTypeDef handle;
427427
} pyb_adc_all_obj_t;
428428

429-
void adc_init_all(pyb_adc_all_obj_t *adc_all, uint32_t resolution) {
429+
void adc_init_all(pyb_adc_all_obj_t *adc_all, uint32_t resolution, uint32_t en_mask) {
430430

431431
switch (resolution) {
432432
case 6: resolution = ADC_RESOLUTION6b; break;
@@ -439,15 +439,20 @@ void adc_init_all(pyb_adc_all_obj_t *adc_all, uint32_t resolution) {
439439
}
440440

441441
for (uint32_t channel = ADC_FIRST_GPIO_CHANNEL; channel <= ADC_LAST_GPIO_CHANNEL; ++channel) {
442-
// Channels 0-16 correspond to real pins. Configure the GPIO pin in
443-
// ADC mode.
444-
const pin_obj_t *pin = pin_adc1[channel];
445-
mp_hal_gpio_clock_enable(pin->gpio);
446-
GPIO_InitTypeDef GPIO_InitStructure;
447-
GPIO_InitStructure.Pin = pin->pin_mask;
448-
GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
449-
GPIO_InitStructure.Pull = GPIO_NOPULL;
450-
HAL_GPIO_Init(pin->gpio, &GPIO_InitStructure);
442+
// only initialise those channels that are selected with the en_mask
443+
if (en_mask & (1 << channel)) {
444+
// Channels 0-16 correspond to real pins. Configure the GPIO pin in
445+
// ADC mode.
446+
const pin_obj_t *pin = pin_adc1[channel];
447+
if (pin) {
448+
mp_hal_gpio_clock_enable(pin->gpio);
449+
GPIO_InitTypeDef GPIO_InitStructure;
450+
GPIO_InitStructure.Pin = pin->pin_mask;
451+
GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
452+
GPIO_InitStructure.Pull = GPIO_NOPULL;
453+
HAL_GPIO_Init(pin->gpio, &GPIO_InitStructure);
454+
}
455+
}
451456
}
452457

453458
adcx_clock_enable();
@@ -536,12 +541,17 @@ float adc_read_core_vref(ADC_HandleTypeDef *adcHandle) {
536541

537542
STATIC mp_obj_t adc_all_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
538543
// check number of arguments
539-
mp_arg_check_num(n_args, n_kw, 1, 1, false);
544+
mp_arg_check_num(n_args, n_kw, 1, 2, false);
540545

541546
// make ADCAll object
542547
pyb_adc_all_obj_t *o = m_new_obj(pyb_adc_all_obj_t);
543548
o->base.type = &pyb_adc_all_type;
544-
adc_init_all(o, mp_obj_get_int(args[0])); // args[0] is the resolution
549+
mp_int_t res = mp_obj_get_int(args[0]);
550+
uint32_t en_mask = 0xffffffff;
551+
if (n_args > 1) {
552+
en_mask = mp_obj_get_int(args[1]);
553+
}
554+
adc_init_all(o, res, en_mask);
545555

546556
return o;
547557
}

0 commit comments

Comments
 (0)