From 178f327d45e7431fbcc18ec5e232f8b04758c2aa Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Tue, 19 Dec 2023 16:25:38 +0100 Subject: [PATCH 1/3] AdvancedADC: Disable DMA IRQ before enabling double buffer mode. Signed-off-by: iabdalkader --- src/AdvancedADC.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/AdvancedADC.cpp b/src/AdvancedADC.cpp index ee4ae1f..87e2891 100644 --- a/src/AdvancedADC.cpp +++ b/src/AdvancedADC.cpp @@ -208,7 +208,9 @@ int AdvancedADC::begin(uint32_t resolution, uint32_t sample_rate, size_t n_sampl } // Re/enable DMA double buffer mode. + HAL_NVIC_DisableIRQ(descr->dma_irqn); hal_dma_enable_dbm(&descr->dma, descr->dmabuf[0]->data(), descr->dmabuf[1]->data()); + HAL_NVIC_EnableIRQ(descr->dma_irqn); // Init, config and start the ADC timer. hal_tim_config(&descr->tim, sample_rate); From cd5fb74a62fdddbed22f7e81fc9711753a7b522f Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Tue, 19 Dec 2023 16:26:37 +0100 Subject: [PATCH 2/3] HALConfig: Raise ADC/DAC DMA priority. During testing I noticed mbed OS will preempt or block DMA irq for too long, causing DMA errors. ADC/DAC DMA should have the highest priority since we don't have control over mbed. Signed-off-by: iabdalkader --- src/HALConfig.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/HALConfig.cpp b/src/HALConfig.cpp index f6a7103..493f3f9 100644 --- a/src/HALConfig.cpp +++ b/src/HALConfig.cpp @@ -91,7 +91,7 @@ int hal_dma_config(DMA_HandleTypeDef *dma, IRQn_Type irqn, uint32_t direction) { } // NVIC configuration for DMA Input data interrupt. - HAL_NVIC_SetPriority(irqn, 1, 0); + HAL_NVIC_SetPriority(irqn, 0, 0); HAL_NVIC_EnableIRQ(irqn); return 0; From 434fd378c53d6d4ce914363c98e4fdedfb5843fa Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Wed, 20 Dec 2023 12:29:54 +0100 Subject: [PATCH 3/3] AdvancedADC: Use the high resolution timer for buffer timestamps. Signed-off-by: iabdalkader --- src/AdvancedADC.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/AdvancedADC.cpp b/src/AdvancedADC.cpp index 87e2891..49f7e23 100644 --- a/src/AdvancedADC.cpp +++ b/src/AdvancedADC.cpp @@ -238,8 +238,8 @@ void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *adc) { // NOTE: CT bit is inverted, to get the DMA buffer that's Not currently in use. size_t ct = ! hal_dma_get_ct(&descr->dma); - // Timestamp the buffer. TODO: Should move to timer IRQ. - descr->dmabuf[ct]->timestamp(HAL_GetTick()); + // Timestamp the buffer. + descr->dmabuf[ct]->timestamp(us_ticker_read()); if (descr->pool->writable()) { // Make sure any cached data is discarded.