cancel
Showing results for 
Search instead for 
Did you mean: 

Encountering Issues with Obtaining ADC Values via DMA

Ken_102633
Associate II

Hi All,

I have an question when i used STM32MP157.

I encountered an issue when using DMA to store the values of ADC conversions.

When I store the data from the ADC_DMA_Buffer to the ADC_DATA_Buffer in the HAL_ADC_ConvCpltCallback() function.

Then check the buffer values using STM32CubeIDE.

I notice that the ADC_DMA_Buffer[1] and ADC_DATA_Buffer[1], ADC_DATA_Buffer[3] ... do not match.

What could be the reason for this?

 

Here is the code and ADC1 configuration: (TIM2 frequency is 1000 Hz (1ms))

 

uint32_t ADC_DMA_Buffer[2] ={0};
uint32_t ADC_DATA_Buffer[500] ={0};
uint16_t ADC_Conversion_Done_Count = 0;

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
{
  if(hadc->Instance == ADC1)
  {
    ADC_DATA_Buffer[ADC_Conversion_Done_Count] = ADC_DMA_Buffer[0];
    ADC_Conversion_Done_Count++;
    ADC_DATA_Buffer[ADC_Conversion_Done_Count] = ADC_DMA_Buffer[1];
    ADC_Conversion_Done_Count++;
    ADC_Conversion_Done_Count = ADC_Conversion_Done_Count % 500; 
  }
}

int main(void)
{
  HAL_Init();

  if(IS_ENGINEERING_BOOT_MODE())
  {
    SystemClock_Config();
  }
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_ADC1_Init();
  MX_ADC2_Init();
  MX_TIM2_Init();

  /* Run the ADC linear calibration in single-ended mode */
  if (HAL_ADCEx_Calibration_Start(&hadc1,ADC_CALIB_OFFSET_LINEARITY, ADC_SINGLE_ENDED) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_ADCEx_Calibration_Start(&hadc2,ADC_CALIB_OFFSET_LINEARITY, ADC_SINGLE_ENDED) != HAL_OK)
  {
    Error_Handler();
  }

  /* Start ADC conversion */
  if (HAL_ADC_Start(&hadc2) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_ADCEx_MultiModeStart_DMA(&hadc1, ADC_DMA_Buffer, 2) != HAL_OK)
  {
    Error_Handler();
  }

  /* Start Timer */
  HAL_TIM_Base_Start(&htim2);

  while (1)
  {
  }
}

 

 

 

static void MX_ADC1_Init(void)
{
  ADC_MultiModeTypeDef multimode = {0};
  ADC_ChannelConfTypeDef sConfig = {0};

  hadc1.Instance = ADC1;
  hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
  hadc1.Init.EOCSelection = ADC_EOC_SEQ_CONV;
  hadc1.Init.LowPowerAutoWait = DISABLE;
  hadc1.Init.ContinuousConvMode = DISABLE;
  hadc1.Init.NbrOfConversion = 2;
  hadc1.Init.DiscontinuousConvMode = DISABLE;
  hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T2_TRGO;
  hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
  hadc1.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DMA_CIRCULAR;
  hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  hadc1.Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE;
  hadc1.Init.OversamplingMode = DISABLE;
  if (HAL_ADC_Init(&hadc1) != HAL_OK)
  {
    Error_Handler();
  }

  /** Configure the ADC multi-mode
  */
  multimode.Mode = ADC_DUALMODE_INTERL;
  multimode.DualModeData = ADC_DUALMODEDATAFORMAT_DISABLED;
  multimode.TwoSamplingDelay = ADC_TWOSAMPLINGDELAY_5CYCLES;
  if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK)
  {
    Error_Handler();
  }

  /** Configure Regular Channel
  */
  sConfig.Channel = ADC_CHANNEL_1;
  sConfig.Rank = ADC_REGULAR_RANK_1;
  sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
  sConfig.SingleDiff = ADC_SINGLE_ENDED;
  sConfig.OffsetNumber = ADC_OFFSET_NONE;
  sConfig.Offset = 0;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure Regular Channel  */
  sConfig.Channel = ADC_CHANNEL_2;
  sConfig.Rank = ADC_REGULAR_RANK_2;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
}

 

 

 Best Regards,

Ken

0 REPLIES 0