cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L071 Multiple channels ADC using DMA

LPetr.1
Senior

Hey. I am trying to read multiple ADC channels using DMA but with no luck. My project settings:

I have selected 4 channels and enabled Continuous conversion mode as well as DMA Continuous requests.

0693W00000LyK4lQAF.png 

I have also created a circular DMA for ADC :

0693W00000LyK50QAF.png 

#include "main.h"
#include  <errno.h>
#include  <sys/unistd.h> // STDOUT_FILENO, STDERR_FILENO
 
 ADC_HandleTypeDef hadc;
DMA_HandleTypeDef hdma_adc;
 
UART_HandleTypeDef huart1;
 
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_ADC_Init(void);
static void MX_DMA_Init(void);
static void MX_USART1_UART_Init(void);
 
int _write(int file, char *data, int len)
{
   if ((file != STDOUT_FILENO) && (file != STDERR_FILENO))
   {
      errno = EBADF;
      return -1;
   }
 
   // arbitrary timeout 1000
   HAL_StatusTypeDef status =
      HAL_UART_Transmit(&huart1, (uint8_t*)data, len, 1000);
 
   // return # of bytes written - as best we can tell
   return (status == HAL_OK ? len : 0);
}
 
 
 
 
 
 
 
uint16_t dma_buf[4];
 
int main(void)
{
 
  HAL_Init();
 
  SystemClock_Config();
  MX_GPIO_Init();
  MX_ADC_Init();
  MX_DMA_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
  HAL_ADC_Start_DMA(&hadc, dma_buf, 4);
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
	  HAL_Delay(1000);
 
  }
 
}
 
 
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
 
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
 
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
  PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}
 
/**
  * @brief ADC Initialization Function
  * @param None
  * @retval None
  */
static void MX_ADC_Init(void)
{
 
  /* USER CODE BEGIN ADC_Init 0 */
 
  /* USER CODE END ADC_Init 0 */
 
  ADC_ChannelConfTypeDef sConfig = {0};
 
  /* USER CODE BEGIN ADC_Init 1 */
 
  /* USER CODE END ADC_Init 1 */
 
  /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  */
  hadc.Instance = ADC1;
  hadc.Init.OversamplingMode = DISABLE;
  hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  hadc.Init.Resolution = ADC_RESOLUTION_12B;
  hadc.Init.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
  hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
  hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc.Init.ContinuousConvMode = ENABLE;
  hadc.Init.DiscontinuousConvMode = DISABLE;
  hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc.Init.DMAContinuousRequests = ENABLE;
  hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  hadc.Init.LowPowerAutoWait = DISABLE;
  hadc.Init.LowPowerFrequencyMode = DISABLE;
  hadc.Init.LowPowerAutoPowerOff = DISABLE;
  if (HAL_ADC_Init(&hadc) != HAL_OK)
  {
    Error_Handler();
  }
 
  /** Configure for the selected ADC regular channel to be converted.
  */
  sConfig.Channel = ADC_CHANNEL_0;
  sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
 
  /** Configure for the selected ADC regular channel to be converted.
  */
  sConfig.Channel = ADC_CHANNEL_1;
  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
 
  /** Configure for the selected ADC regular channel to be converted.
  */
  sConfig.Channel = ADC_CHANNEL_2;
  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
 
  /** Configure for the selected ADC regular channel to be converted.
  */
  sConfig.Channel = ADC_CHANNEL_3;
  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN ADC_Init 2 */
 
  /* USER CODE END ADC_Init 2 */
 
}
 
/**
  * @brief USART1 Initialization Function
  * @param None
  * @retval None
  */
static void MX_USART1_UART_Init(void)
{
 
  /* USER CODE BEGIN USART1_Init 0 */
 
  /* USER CODE END USART1_Init 0 */
 
  /* USER CODE BEGIN USART1_Init 1 */
 
  /* USER CODE END USART1_Init 1 */
  huart1.Instance = USART1;
  huart1.Init.BaudRate = 115200;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART1_Init 2 */
 
  /* USER CODE END USART1_Init 2 */
 
}
 
/**
  * Enable DMA controller clock
  */
static void MX_DMA_Init(void)
{
 
  /* DMA controller clock enable */
  __HAL_RCC_DMA1_CLK_ENABLE();
 
  /* DMA interrupt init */
  /* DMA1_Channel1_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
 
}
 
/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
 
  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOA_CLK_ENABLE();
 
}
 
/* USER CODE BEGIN 4 */
 
/* USER CODE END 4 */
 
/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}
 
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

As you can see I have created an array of size 4 for my DMA buffer:

uint16_t dma_buf[4];

and I start the DMA:

HAL_ADC_Start_DMA(&hadc, dma_buf, 4);

I set a breakpoint in while loop every 1 second and have added dma_buf to the expressions. Only the first element of an array is getting filled and it seems that it is a wrong value. I have about 2.9V on that pin so a reading should be about 3600 or 3500:

0693W00000LyK5yQAF.png 

4 REPLIES 4
LPetr.1
Senior

I have also attempted to try and read multiple channels using polling method instead of DMA.

0693W00000LyKKpQAN.png 

  while (1)
  {
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
 
 
	  HAL_ADC_Start(&hadc);
 
	  HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY);
	  uint16_t adc_value1 = HAL_ADC_GetValue(&hadc);
 
	  //HAL_ADC_Start(&hadc);
	  HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY);
	  uint16_t adc_value2 = HAL_ADC_GetValue(&hadc);
 
	  //HAL_ADC_Start(&hadc);
	  HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY);
	  uint16_t adc_value3 = HAL_ADC_GetValue(&hadc);
 
	  //HAL_ADC_Start(&hadc);
	  HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY);
	  uint16_t adc_value4 = HAL_ADC_GetValue(&hadc);
 
	  HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY);
	  uint16_t adc_value5 = HAL_ADC_GetValue(&hadc);
 
	  //HAL_ADC_Start(&hadc);
	  HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY);
	  uint16_t adc_value6 = HAL_ADC_GetValue(&hadc);
 
	  //HAL_ADC_Start(&hadc);
	  HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY);
	  uint16_t adc_value7 = HAL_ADC_GetValue(&hadc);
 
	  //HAL_ADC_Start(&hadc);
	  HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY);
	  uint16_t adc_value8 = HAL_ADC_GetValue(&hadc);
 
	  //HAL_ADC_Start(&hadc);
	  HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY);
	  uint16_t adc_value9 = HAL_ADC_GetValue(&hadc);
 
	  //HAL_ADC_Start(&hadc);
	  HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY);
	  uint16_t adc_value10 = HAL_ADC_GetValue(&hadc);
 
 
	  printf("adc_value 1 = %u \n",adc_value1);
	  printf("adc_value 2 = %u \n",adc_value2);
	  printf("adc_value 3 = %u \n",adc_value3);
	  printf("adc_value 4 = %u \n",adc_value4);
	  printf("adc_value 5 = %u \n",adc_value5);
	  printf("adc_value 6 = %u \n",adc_value6);
	  printf("adc_value 7 = %u \n",adc_value7);
	  printf("adc_value 8 = %u \n",adc_value8);
	  printf("adc_value 9 = %u \n",adc_value9);
	  printf("adc_value 10 = %u \n",adc_value10);
	  printf("\n");
	  HAL_ADC_Stop(&hadc);
 
	  HAL_Delay(1000);
 
 
 
  }

I am getting the strangest results, when ADC0 is shorted to ground(adc_value 5 changed)

adc_value 1 = 3554 

adc_value 2 = 3486 

adc_value 3 = 3602 

adc_value 4 = 3605 

adc_value 5 = 66 

adc_value 6 = 3520 

adc_value 7 = 3560 

adc_value 8 = 3486 

adc_value 9 = 3521 

adc_value 10 = 3606

when ADC1 is shorted to ground, none of the values change

adc_value 1 = 3546 

adc_value 2 = 3462 

adc_value 3 = 3394 

adc_value 4 = 3462 

adc_value 5 = 3594 

adc_value 6 = 3523 

adc_value 7 = 3560 

adc_value 8 = 3462 

adc_value 9 = 3504 

adc_value 10 = 66 

when ADC2 is shorted to ground (adc value4 and ac value10 are changed):

adc_value 1 = 3550 

adc_value 2 = 3469 

adc_value 3 = 3397 

adc_value 4 = 66 

adc_value 5 = 3600 

adc_value 6 = 3524 

adc_value 7 = 3561 

adc_value 8 = 3469 

adc_value 9 = 3510 

adc_value 10 = 66 

When ADC3 is shorted to ground adc value 3 change

adc_value 1 = 3543 

adc_value 2 = 3462 

adc_value 3 = 66 

adc_value 4 = 3610 

adc_value 5 = 3604 

adc_value 6 = 3528 

adc_value 7 = 3564 

adc_value 8 = 3461 

adc_value 9 = 3291 

adc_value 10 = 3612 

when ADC4 is shorted to ground, adc_value 9 change:

adc_value 1 = 3533 

adc_value 2 = 3200 

adc_value 3 = 3602 

adc_value 4 = 3609 

adc_value 5 = 3605 

adc_value 6 = 3524 

adc_value 7 = 3557 

adc_value 8 = 3200 

adc_value 9 = 66 

adc_value 10 = 3610

when ADC5 is shorted to ground adc value 2 and and value 8 change:

adc_value 1 = 3316 

adc_value 2 = 68 

adc_value 3 = 3608 

adc_value 4 = 3616 

adc_value 5 = 3611 

adc_value 6 = 3523 

adc_value 7 = 3554 

adc_value 8 = 66 

adc_value 9 = 3534 

adc_value 10 = 3616 

when ADC6 is shorted to ground, adc_value 1 is changed:

adc_value 1 = 66 

adc_value 2 = 3484 

adc_value 3 = 3607 

adc_value 4 = 3610 

adc_value 5 = 3606 

adc_value 6 = 3508 

adc_value 7 = 3308 

adc_value 8 = 3484 

adc_value 9 = 3530 

adc_value 10 = 3612 

and etc.... As you can see, it is messed up. Could someone help me understand why would that happen?

well spotted. That was indeed an issue with the CubeMX generating code in wrong order. What a silly mistake form CubeMX side. Thanks

Piranha
Chief II

With 1,5 sampling cycles the total conversion time is 14 cycles. Those are ADC clock cycles, but it's still pretty fast. The code of the HAL broken bloatware just cannot keep up to such speeds, especially when compiled in debug mode with no optimizations. And, even with a decent optimal code, which disables interrupts, capturing multiple ADC channels without DMA is not a good design.