From a5bd695a3d3b716993f268b21dc6a20d4d6a37da Mon Sep 17 00:00:00 2001 From: Luiz Villa Date: Fri, 16 Oct 2026 19:56:35 +0200 Subject: [PATCH] [bug] data_dispatch: size ADC DMA buffers for several dispatch periods In task dispatch mode, the number of new conversions is computed from the DMA position, and the circular DMA buffer only held one dispatch period (plus one sample per channel). When the critical task was delayed by about two PWM periods (long interrupt, synchronous logging), the DMA wrapped around more than once between two dispatches and conversions were lost without any error. The buffer now holds DATA_DISPATCH_MARGIN_TICKS (4) dispatch periods. The rounding to a multiple of the enabled channel count is applied to the new size instead of to the repetition count. Co-Authored-By: Claude Opus 5.5 --- .../zephyr/src/data/data_dispatch.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/zephyr/modules/owntech_spin_api/zephyr/src/data/data_dispatch.cpp b/zephyr/modules/owntech_spin_api/zephyr/src/data/data_dispatch.cpp index 2a30e32d..6c03e9be 100644 --- a/zephyr/modules/owntech_spin_api/zephyr/src/data/data_dispatch.cpp +++ b/zephyr/modules/owntech_spin_api/zephyr/src/data/data_dispatch.cpp @@ -41,6 +41,9 @@ /* Current file header */ #include "data_dispatch.h" +/* Dispatch periods of conversions the DMA buffers can hold */ +#define DATA_DISPATCH_MARGIN_TICKS 4 + /** * Local variables */ @@ -176,16 +179,24 @@ void data_dispatch_init(dispatch_t dispatch_method, uint32_t repetitions) } else { - dma_buffer_size = repetitions; + /** + * Room for DATA_DISPATCH_MARGIN_TICKS dispatch periods: the + * conversion count is read from the DMA position, so more + * conversions than the buffer between two dispatches are + * silently lost. With one period of room, a critical task + * delayed by ~2 PWM periods (long interrupt, synchronous logging) + * lost data. + */ + dma_buffer_size = repetitions * DATA_DISPATCH_MARGIN_TICKS; /** * Make sure buffer size is a multiple of enabled channels count * so that each channel data will always be at the same position */ - if (repetitions % enabled_channels_count[adc_index] != 0) + if (dma_buffer_size % enabled_channels_count[adc_index] != 0) { dma_buffer_size += (enabled_channels_count[adc_index]) - - (repetitions % + (dma_buffer_size % enabled_channels_count[adc_index]); } else