AnsweredAssumed Answered

STM32F3 two ADC with DMA transferred data mixup [SOLVED]

Question asked by Ole56 on Nov 22, 2016
Latest reply on Jan 5, 2017 by Labrado.Jeremy
Hallo,

I am trying to solve this problem that I am facing with ADC and DMA use.
I use a STM32F303 Microcontroller, with CubeMX vers4.17 and Eclipse.
I was able to test and use all the peripherals I needed but I encountered problems with the ADC and DMA.
I use the internal Opamps and I want to sample the two analog signals coming from Opamps with ADC1 and ADC3.
I followed the tutorial 
http://tunizem.blogspot.de/2014/09/using-adc-with-dma-on-stm32.html
And with this I was able to succesfully enable ADC1 and sample using DMA1, with the TIMER3 configured as trimmer.
Then I tryed only with the other channel, I was able to succesfully enable ADC3 and sample using DMA2 (again with TMR3).
The problems come out when I use the two configurations together: the DMA1 buffer is filled with both data coming from ADC1 and data coming from ADC3!! This doesn't happen with the buffer of DMA2 in which the sampled data are still correct.
I checked the data both through sending them undersampled through UART to the computer and with DEBUG checking the whole buffer contents.

I checked the frequency of the IRQ of TMR3, DMA1 and DMA2 and they are all correct and the same.

I also tried with another board to sample ADC1 and ADC4 and the problem happens again but this time I get in the DMA2 buffer (which is connected to ADC4) data coming from ADC1.

I think that it could be some wrong settings but I was not able to figure out which.

If somebody have some ideas or suggestions it would be really helpful!

thank you very much in advance!
regards
-------------------------
Here's the code:


#define DMA1_ARRAY_LEN 30
#define DMA2_ARRAY_LEN 30


int main(void)
{


/* USER CODE BEGIN 1 */


uint32_t DMA1_error, DMA2_error;
char str[32];


/* USER CODE END 1 */


/* MCU Configuration----------------------------------------------------------*/


/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();


/* Configure the system clock */
SystemClock_Config();


/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_ADC1_Init();
MX_ADC3_Init();
MX_OPAMP1_Init();
MX_OPAMP2_Init();
MX_OPAMP3_Init();
MX_OPAMP4_Init();
MX_SPI2_Init();
MX_TIM3_Init();
MX_USART1_UART_Init();


/* USER CODE BEGIN 2 */


LED_OFF(LED_GREEN_Pin);
LED_OFF(LED_RED_Pin);


HAL_OPAMP_Start(&hopamp1);
HAL_OPAMP_Start(&hopamp2);
HAL_OPAMP_Start(&hopamp3);
HAL_OPAMP_Start(&hopamp4);


SensorPower_ON();


// -- Enables ADC DMA request
if (HAL_ADC_Start_DMA(&hadc1, (uint32_t*)ADC1ConvertedValues, 2*DMA1_ARRAY_LEN) != HAL_OK)
return 0;


if (HAL_ADC_Start_DMA(&hadc3, (uint32_t*)ADC3ConvertedValues, 2*DMA2_ARRAY_LEN) != HAL_OK)
return 0;


// Enable Timer3
HAL_TIM_Base_Start(&htim3);


/* USER CODE END 2 */


/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{


HAL_Delay(1);


DMA1_error = HAL_DMA_GetError(&hdma_adc1);
DMA2_error = HAL_DMA_GetError(&hdma_adc3);


sprintf(str, "$%d %d;", ADC1ConvertedValues[1], ADC3ConvertedValues[1]); // serial port plotter viewer
UART_return = HAL_UART_Transmit(&huart1, str, strlen(str), 1000);


// Toggle green LED


SysTick_Counter++;
if (SysTick_Counter == 700)
LED_ON(LED_GREEN_Pin);
if (SysTick_Counter == 800)
LED_OFF(LED_GREEN_Pin);
if (SysTick_Counter == 900)
LED_ON(LED_GREEN_Pin);
if (SysTick_Counter == 1000)
{
LED_OFF(LED_GREEN_Pin);
SysTick_Counter = 0;
}


/* USER CODE END WHILE */


/* USER CODE BEGIN 3 */


}
/* USER CODE END 3 */


}

Outcomes