Page 1 of 1

ADC half complete callback bug

Posted: Wed Jun 13, 2012 4:47 am
by GHF
In 2.4.1 (and possibly later), it looks like the ADC conversion callback's buffer parameter is passed the wrong pointer when using a circular double buffer (circular = true, depth > 1) and the second half of the buffer is converted. The callback invocation code adds half the number of rows to the sample buffer pointer, which becomes an issue when there's more than one channel in the conversion group.

Code: Select all

size_t half = (adcp)->depth / 2;
(adcp)->grpp->end_cb(adcp, (adcp)->samples + half, half);


Here's my patch:

Code: Select all

diff --git a/os/hal/include/adc.h b/os/hal/include/adc.h
index 54fc8a9..25e1151 100644
--- a/os/hal/include/adc.h
+++ b/os/hal/include/adc.h
@@ -217,7 +217,8 @@ typedef enum {
       if ((adcp)->depth > 1) {                                              \
         /* Invokes the callback passing the 2nd half of the buffer.*/       \
         size_t half = (adcp)->depth / 2;                                    \
-        (adcp)->grpp->end_cb(adcp, (adcp)->samples + half, half);           \
+        size_t half_index = half * (adcp)->grpp->num_channels;              \
+        (adcp)->grpp->end_cb(adcp, (adcp)->samples + half_index, half);     \
       }                                                                     \
       else {                                                                \
         /* Invokes the callback passing the whole buffer.*/                 \
@@ -233,7 +234,8 @@ typedef enum {
       if ((adcp)->depth > 1) {                                              \
         /* Invokes the callback passing the 2nd half of the buffer.*/       \
         size_t half = (adcp)->depth / 2;                                    \
-        (adcp)->grpp->end_cb(adcp, (adcp)->samples + half, half);           \
+        size_t half_index = half * (adcp)->grpp->num_channels;              \
+        (adcp)->grpp->end_cb(adcp, (adcp)->samples + half_index, half);     \
       }                                                                     \
       else {                                                                \
         /* Invokes the callback passing the whole buffer.*/                 \

Re: ADC half complete callback bug

Posted: Wed Jun 13, 2012 7:12 am
by Giovanni
Hi,

Thanks for finding, probably this is shared among all the various STM32 ADC implementations.

Giovanni

Re: ADC half complete callback bug

Posted: Mon Jun 18, 2012 6:21 am
by GHF
I was working on some triple simultaneous ADC code but was running into DMA issues. Based on this link I separated out the writing to DMA CR from setting the enable bit. Most of the other drivers already do this (except SPI).

Code: Select all

diff --git a/os/hal/platforms/STM32F1xx/adc_lld.c b/os/hal/platforms/STM32F1xx/adc_lld.c
index eaff995..8785688 100644
--- a/os/hal/platforms/STM32F1xx/adc_lld.c
+++ b/os/hal/platforms/STM32F1xx/adc_lld.c
@@ -104,7 +104,7 @@ void adc_lld_init(void) {
   ADCD1.dmamode = STM32_DMA_CR_PL(STM32_ADC_ADC1_DMA_PRIORITY) |
                   STM32_DMA_CR_MSIZE_HWORD | STM32_DMA_CR_PSIZE_HWORD |
                   STM32_DMA_CR_MINC        | STM32_DMA_CR_TCIE        |
-                  STM32_DMA_CR_TEIE        | STM32_DMA_CR_EN;
+                  STM32_DMA_CR_TEIE;
 
   /* Temporary activation.*/
   rccEnableADC1(FALSE);
@@ -206,6 +206,7 @@ void adc_lld_start_conversion(ADCDriver *adcp) {
   dmaStreamSetMemory0(adcp->dmastp, adcp->samples);
   dmaStreamSetTransactionSize(adcp->dmastp, n);
   dmaStreamSetMode(adcp->dmastp, mode);
+  dmaStreamEnable(adcp->dmastp);
 
   /* ADC setup.*/
   adcp->adc->CR1   = grpp->cr1 | ADC_CR1_SCAN;
diff --git a/os/hal/platforms/STM32F2xx/adc_lld.c b/os/hal/platforms/STM32F2xx/adc_lld.c
index 091a9c5..016b12c 100644
--- a/os/hal/platforms/STM32F2xx/adc_lld.c
+++ b/os/hal/platforms/STM32F2xx/adc_lld.c
@@ -193,8 +193,7 @@ void adc_lld_init(void) {
                   STM32_DMA_CR_DIR_P2M |
                   STM32_DMA_CR_MSIZE_HWORD | STM32_DMA_CR_PSIZE_HWORD |
                   STM32_DMA_CR_MINC        | STM32_DMA_CR_TCIE        |
-                  STM32_DMA_CR_DMEIE       | STM32_DMA_CR_TEIE        |
-                  STM32_DMA_CR_EN;
+                  STM32_DMA_CR_DMEIE       | STM32_DMA_CR_TEIE;
 #endif
 
 #if STM32_ADC_USE_ADC2
@@ -207,8 +206,7 @@ void adc_lld_init(void) {
                   STM32_DMA_CR_DIR_P2M |
                   STM32_DMA_CR_MSIZE_HWORD | STM32_DMA_CR_PSIZE_HWORD |
                   STM32_DMA_CR_MINC        | STM32_DMA_CR_TCIE        |
-                  STM32_DMA_CR_DMEIE       | STM32_DMA_CR_TEIE        |
-                  STM32_DMA_CR_EN;
+                  STM32_DMA_CR_DMEIE       | STM32_DMA_CR_TEIE;
 #endif
 
 #if STM32_ADC_USE_ADC3
@@ -221,8 +219,7 @@ void adc_lld_init(void) {
                   STM32_DMA_CR_DIR_P2M |
                   STM32_DMA_CR_MSIZE_HWORD | STM32_DMA_CR_PSIZE_HWORD |
                   STM32_DMA_CR_MINC        | STM32_DMA_CR_TCIE        |
-                  STM32_DMA_CR_DMEIE       | STM32_DMA_CR_TEIE        |
-                  STM32_DMA_CR_EN;
+                  STM32_DMA_CR_DMEIE       | STM32_DMA_CR_TEIE;
 #endif
 
   /* The shared vector is initialized on driver initialization and never
@@ -345,6 +342,7 @@ void adc_lld_start_conversion(ADCDriver *adcp) {
   dmaStreamSetTransactionSize(adcp->dmastp, (uint32_t)grpp->num_channels *
                                             (uint32_t)adcp->depth);
   dmaStreamSetMode(adcp->dmastp, mode);
+  dmaStreamEnable(adcp->dmastp);
 
   /* ADC setup.*/
   adcp->adc->SR    = 0;
diff --git a/os/hal/platforms/STM32F4xx/adc_lld.c b/os/hal/platforms/STM32F4xx/adc_lld.c
index cbe7173..4340f8f 100644
--- a/os/hal/platforms/STM32F4xx/adc_lld.c
+++ b/os/hal/platforms/STM32F4xx/adc_lld.c
@@ -193,8 +193,7 @@ void adc_lld_init(void) {
                   STM32_DMA_CR_DIR_P2M |
                   STM32_DMA_CR_MSIZE_HWORD | STM32_DMA_CR_PSIZE_HWORD |
                   STM32_DMA_CR_MINC        | STM32_DMA_CR_TCIE        |
-                  STM32_DMA_CR_DMEIE       | STM32_DMA_CR_TEIE        |
-                  STM32_DMA_CR_EN;
+                  STM32_DMA_CR_DMEIE       | STM32_DMA_CR_TEIE;
 #endif
 
 #if STM32_ADC_USE_ADC2
@@ -207,8 +206,7 @@ void adc_lld_init(void) {
                   STM32_DMA_CR_DIR_P2M |
                   STM32_DMA_CR_MSIZE_HWORD | STM32_DMA_CR_PSIZE_HWORD |
                   STM32_DMA_CR_MINC        | STM32_DMA_CR_TCIE        |
-                  STM32_DMA_CR_DMEIE       | STM32_DMA_CR_TEIE        |
-                  STM32_DMA_CR_EN;
+                  STM32_DMA_CR_DMEIE       | STM32_DMA_CR_TEIE;
 #endif
 
 #if STM32_ADC_USE_ADC3
@@ -221,8 +219,7 @@ void adc_lld_init(void) {
                   STM32_DMA_CR_DIR_P2M |
                   STM32_DMA_CR_MSIZE_HWORD | STM32_DMA_CR_PSIZE_HWORD |
                   STM32_DMA_CR_MINC        | STM32_DMA_CR_TCIE        |
-                  STM32_DMA_CR_DMEIE       | STM32_DMA_CR_TEIE        |
-                  STM32_DMA_CR_EN;
+                  STM32_DMA_CR_DMEIE       | STM32_DMA_CR_TEIE;
 #endif
 
   /* The shared vector is initialized on driver initialization and never
@@ -345,6 +342,7 @@ void adc_lld_start_conversion(ADCDriver *adcp) {
   dmaStreamSetTransactionSize(adcp->dmastp, (uint32_t)grpp->num_channels *
                                             (uint32_t)adcp->depth);
   dmaStreamSetMode(adcp->dmastp, mode);
+  dmaStreamEnable(adcp->dmastp);
 
   /* ADC setup.*/
   adcp->adc->SR    = 0;
diff --git a/os/hal/platforms/STM32L1xx/adc_lld.c b/os/hal/platforms/STM32L1xx/adc_lld.c
index 3f9b57e..b19aa6b 100644
--- a/os/hal/platforms/STM32L1xx/adc_lld.c
+++ b/os/hal/platforms/STM32L1xx/adc_lld.c
@@ -136,8 +136,7 @@ void adc_lld_init(void) {
                   STM32_DMA_CR_DIR_P2M |
                   STM32_DMA_CR_MSIZE_HWORD | STM32_DMA_CR_PSIZE_HWORD |
                   STM32_DMA_CR_MINC        | STM32_DMA_CR_TCIE        |
-                  STM32_DMA_CR_DMEIE       | STM32_DMA_CR_TEIE        |
-                  STM32_DMA_CR_EN;
+                  STM32_DMA_CR_DMEIE       | STM32_DMA_CR_TEIE;
 #endif
 
   /* The shared vector is initialized on driver initialization and never
@@ -224,6 +223,7 @@ void adc_lld_start_conversion(ADCDriver *adcp) {
   dmaStreamSetTransactionSize(adcp->dmastp, (uint32_t)grpp->num_channels *
                                             (uint32_t)adcp->depth);
   dmaStreamSetMode(adcp->dmastp, mode);
+  dmaStreamEnable(adcp->dmastp);
 
   /* ADC setup.*/
   adcp->adc->SR    = 0;

Re: ADC half complete callback bug

Posted: Mon Jun 18, 2012 9:44 am
by Giovanni
Hi,

Thanks for the patch, I fixed both issues and also modified the SPI driver in the same way. Tickets 3534767 and 3535938.

Giovanni