Skip to content

Commit aebd970

Browse files
committed
stm32/adc: Optimise read_timed_multi() by caching buffer pointers.
1 parent 4f40fa5 commit aebd970

2 files changed

Lines changed: 7 additions & 6 deletions

File tree

docs/library/pyb.ADC.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,10 @@ Methods
125125

126126
The maximum rate depends on factors including the data width and the
127127
number of ADC's being read. In testing two ADC's were sampled at a timer
128-
rate of 140KHz without overrun. Samples were missed at 180KHz. At high
129-
sample rates disabling interrupts for the duration can reduce the risk
130-
of sporadic data loss.
128+
rate of 210kHz without overrun. Samples were missed at 215kHz. For three
129+
ADC's the limit is around 140kHz, and for four it is around 110kHz.
130+
At high sample rates disabling interrupts for the duration can reduce the
131+
risk of sporadic data loss.
131132

132133
The ADCAll Object
133134
-----------------

ports/stm32/adc.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -475,12 +475,14 @@ STATIC mp_obj_t adc_read_timed_multi(mp_obj_t adc_array_in, mp_obj_t buf_array_i
475475
mp_buffer_info_t bufinfo;
476476
mp_get_buffer_raise(buf_array[0], &bufinfo, MP_BUFFER_WRITE);
477477
size_t typesize = mp_binary_get_size('@', bufinfo.typecode, NULL);
478+
void *bufptrs[nbufs];
478479
for (uint array_index = 0; array_index < nbufs; array_index++) {
479480
mp_buffer_info_t bufinfo_curr;
480481
mp_get_buffer_raise(buf_array[array_index], &bufinfo_curr, MP_BUFFER_WRITE);
481482
if ((bufinfo.len != bufinfo_curr.len) || (bufinfo.typecode != bufinfo_curr.typecode)) {
482483
mp_raise_ValueError("size and type of buffers must match");
483484
}
485+
bufptrs[array_index] = bufinfo_curr.buf;
484486
}
485487

486488
// Use the supplied timer object as the sampling time base
@@ -541,9 +543,7 @@ STATIC mp_obj_t adc_read_timed_multi(mp_obj_t adc_array_in, mp_obj_t buf_array_i
541543
if (typesize == 1) {
542544
value >>= 4;
543545
}
544-
mp_buffer_info_t bufinfo_curr; // Get buf for current ADC
545-
mp_get_buffer_raise(buf_array[array_index], &bufinfo_curr, MP_BUFFER_WRITE);
546-
mp_binary_set_val_array_from_int(bufinfo_curr.typecode, bufinfo_curr.buf, elem_index, value);
546+
mp_binary_set_val_array_from_int(bufinfo.typecode, bufptrs[array_index], elem_index, value);
547547
}
548548
}
549549

0 commit comments

Comments
 (0)