From aebd9701a78d267cb264d400804b02c5b7a00e9a Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 11 Apr 2018 14:08:23 +1000 Subject: [PATCH] stm32/adc: Optimise read_timed_multi() by caching buffer pointers. --- docs/library/pyb.ADC.rst | 7 ++++--- ports/stm32/adc.c | 6 +++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/docs/library/pyb.ADC.rst b/docs/library/pyb.ADC.rst index c2d09ad402..1256c6a3c1 100644 --- a/docs/library/pyb.ADC.rst +++ b/docs/library/pyb.ADC.rst @@ -125,9 +125,10 @@ Methods The maximum rate depends on factors including the data width and the number of ADC's being read. In testing two ADC's were sampled at a timer - rate of 140KHz without overrun. Samples were missed at 180KHz. At high - sample rates disabling interrupts for the duration can reduce the risk - of sporadic data loss. + rate of 210kHz without overrun. Samples were missed at 215kHz. For three + ADC's the limit is around 140kHz, and for four it is around 110kHz. + At high sample rates disabling interrupts for the duration can reduce the + risk of sporadic data loss. The ADCAll Object ----------------- diff --git a/ports/stm32/adc.c b/ports/stm32/adc.c index ba1ca5ce59..f765870d5f 100644 --- a/ports/stm32/adc.c +++ b/ports/stm32/adc.c @@ -475,12 +475,14 @@ STATIC mp_obj_t adc_read_timed_multi(mp_obj_t adc_array_in, mp_obj_t buf_array_i mp_buffer_info_t bufinfo; mp_get_buffer_raise(buf_array[0], &bufinfo, MP_BUFFER_WRITE); size_t typesize = mp_binary_get_size('@', bufinfo.typecode, NULL); + void *bufptrs[nbufs]; for (uint array_index = 0; array_index < nbufs; array_index++) { mp_buffer_info_t bufinfo_curr; mp_get_buffer_raise(buf_array[array_index], &bufinfo_curr, MP_BUFFER_WRITE); if ((bufinfo.len != bufinfo_curr.len) || (bufinfo.typecode != bufinfo_curr.typecode)) { mp_raise_ValueError("size and type of buffers must match"); } + bufptrs[array_index] = bufinfo_curr.buf; } // 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 if (typesize == 1) { value >>= 4; } - mp_buffer_info_t bufinfo_curr; // Get buf for current ADC - mp_get_buffer_raise(buf_array[array_index], &bufinfo_curr, MP_BUFFER_WRITE); - mp_binary_set_val_array_from_int(bufinfo_curr.typecode, bufinfo_curr.buf, elem_index, value); + mp_binary_set_val_array_from_int(bufinfo.typecode, bufptrs[array_index], elem_index, value); } }