cancel
Showing results for 
Search instead for 
Did you mean: 

Trouble with multiple ADCs and DMA

Hrishikesh
Senior

I'm trying to get ADC values from multiple channels of ADC1 of STM32F103C8T6 using DMA where conversion is triggered by TIM3 every 1ms. However, I'm not seeing the right values. I tried the polling mode and that worked fine. Here is the main function code so far and the settings in CubeMX. The full code is here. What am I missing?

int main(void)
{
	/* USER CODE BEGIN 1 */
 
	/* USER CODE END 1 */
 
	/* MCU Configuration--------------------------------------------------------*/
 
	/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
	HAL_Init();
 
	/* USER CODE BEGIN Init */
 
	/* USER CODE END Init */
 
	/* Configure the system clock */
	SystemClock_Config();
 
	/* USER CODE BEGIN SysInit */
 
	/* USER CODE END SysInit */
 
	/* Initialize all configured peripherals */
	MX_GPIO_Init();
	MX_ADC1_Init();
	MX_SPI1_Init();
	MX_TIM1_Init();
	MX_I2C1_Init();
	MX_USART1_UART_Init();
	MX_DMA_Init();
	MX_TIM2_Init();
	MX_TIM3_Init();
	/* USER CODE BEGIN 2 */
 
	HAL_ADC_Start_DMA(&hadc1, (uint32_t*) adc1_values, 2);
	HAL_TIM_Base_Start(&htim3);
 
	/* USER CODE END 2 */
 
	/* Infinite loop */
	/* USER CODE BEGIN WHILE */
	while (1)
	{
		/* USER CODE END WHILE */
 
		/* USER CODE BEGIN 3 */
	}
	/* USER CODE END 3 */
}

0693W00000DmDkyQAF.png 

0693W00000DmDlDQAV.png 

0693W00000DmDlIQAV.png 

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

> MX_ADC1_Init();

> MX_DMA_Init();

You need to initialize DMA before the peripherals that use it. Sometimes CubeMX gets this correct, sometimes not.

No doubt if you check your newer working project, the order of initialization has changed.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

4 REPLIES 4
Hrishikesh
Senior

I started a new project (through CubeIDE instead of the external CubeMX application) starting with just the ADC. That worked. I then kept enabling more and more peripherals and kept testing if they all work. Now it all works fine. Very strange!!

TDK
Guru

> MX_ADC1_Init();

> MX_DMA_Init();

You need to initialize DMA before the peripherals that use it. Sometimes CubeMX gets this correct, sometimes not.

No doubt if you check your newer working project, the order of initialization has changed.

If you feel a post has answered your question, please click "Accept as Solution".

Thank you. I didn't know this. Yes the new code has the correct init sequence. Is this a bug then?

I’d call it a bug. It’s been reported in other threads.
If you feel a post has answered your question, please click "Accept as Solution".