cancel
Showing results for 
Search instead for 
Did you mean: 

Multi channel ADC conversion with DMA and Interrupt.

vshah
Associate II

hello everyone,

i am new to STM32 controller series,

i am trying to read mutli channel ADC with help of DMA and Interrupt.but facing issue.

i am not getting ok status from"HAL_ADC_Start_DMA" this function.

can any-body help me out.

please find below configuration code.

kindly guide me, where i am wrong.

static void ADCx_Init(void)
{
  /* Set ADC instance */
  hnucleo_Adc.Instance                   = NUCLEO_ADCx;
 
  if (HAL_ADC_GetState(&hnucleo_Adc) == HAL_ADC_STATE_RESET)
  {
    /* ADC Config */
    
    hnucleo_Adc.Instance                        = ADC1;
    hnucleo_Adc.Init.ClockPrescaler             = ADC_CLOCK_ASYNC_DIV256;
    hnucleo_Adc.Init.Resolution                 = ADC_RESOLUTION_12B;
    hnucleo_Adc.Init.DataAlign                  = ADC_DATAALIGN_RIGHT;
    hnucleo_Adc.Init.ScanConvMode               = ADC_SCAN_ENABLE;
    hnucleo_Adc.Init.EOCSelection               = ADC_EOC_SEQ_CONV;
    hnucleo_Adc.Init.LowPowerAutoWait           = DISABLE;
    hnucleo_Adc.Init.ContinuousConvMode         = ENABLE;
    hnucleo_Adc.Init.NbrOfConversion            = 2;
    hnucleo_Adc.Init.DiscontinuousConvMode      = DISABLE;
    hnucleo_Adc.Init.ExternalTrigConv           = ADC_SOFTWARE_START;
    hnucleo_Adc.Init.ExternalTrigConvEdge       = ADC_EXTERNALTRIGCONVEDGE_NONE;
    hnucleo_Adc.Init.DMAContinuousRequests      = ENABLE;
    hnucleo_Adc.Init.Overrun                    = ADC_OVR_DATA_PRESERVED;
    hnucleo_Adc.Init.OversamplingMode           = DISABLE;    
 
    /* Initialize ADC */
    HAL_ADC_Init(&hnucleo_Adc);
    
   /**Configure Regular Channel */
    sConfig.Channel = ADC_CHANNEL_15;
    sConfig.Rank = ADC_REGULAR_RANK_1;
    sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5;
    sConfig.SingleDiff = ADC_SINGLE_ENDED;
    sConfig.OffsetNumber = ADC_OFFSET_NONE;
    sConfig.Offset = 0;
    if (HAL_ADC_ConfigChannel(&hnucleo_Adc, &sConfig) != HAL_OK)
    {
      _Error_Handler(__FILE__, __LINE__);
    }   
    
    /**Configure Regular Channel */
    sConfig.Channel = ADC_CHANNEL_16;
    sConfig.Rank = ADC_REGULAR_RANK_2;
    if (HAL_ADC_ConfigChannel(&hnucleo_Adc, &sConfig) != HAL_OK)
    {
      while(1);
    }
    
    if (HAL_ADC_Start_DMA(&hnucleo_Adc,
                          (uint32_t *)aADCxConvertedData,
                          ADC_CONVERTED_DATA_BUFFER_SIZE
                         ) != HAL_OK)
    {
        while(1);
    }
  }
}
void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
{
 
  GPIO_InitTypeDef GPIO_InitStruct;
  if(hadc->Instance==ADC1)
  {
  /* USER CODE BEGIN ADC1_MspInit 0 */
 
  /* USER CODE END ADC1_MspInit 0 */
    /* Peripheral clock enable */
    __HAL_RCC_ADC_CLK_ENABLE();
  
     
    /**ADC1 GPIO Configuration    
    PB0     ------> ADC1_IN15
    PB1     ------> ADC1_IN16 
    */
    GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG_ADC_CONTROL;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);     
 
    /* ADC1 DMA Init */
    /* ADC1 Init */
    hdma_adc1.Instance                  = DMA1_Channel2;
    hdma_adc1.Init.Request              = DMA_REQUEST_ADC1;
    hdma_adc1.Init.Direction            = DMA_PERIPH_TO_MEMORY;
    hdma_adc1.Init.PeriphInc            = DMA_PINC_DISABLE;
    hdma_adc1.Init.MemInc               = DMA_MINC_ENABLE;
    hdma_adc1.Init.PeriphDataAlignment  = DMA_PDATAALIGN_HALFWORD;
    hdma_adc1.Init.MemDataAlignment     = DMA_MDATAALIGN_HALFWORD;
    hdma_adc1.Init.Mode                 = DMA_CIRCULAR;
    hdma_adc1.Init.Priority             = DMA_PRIORITY_LOW;
    if (HAL_DMA_Init(&hdma_adc1) != HAL_OK)
    {
      _Error_Handler(__FILE__, __LINE__);
    }
 
    __HAL_LINKDMA(hadc,DMA_Handle,hdma_adc1);
 
    /* ADC1 interrupt Init */
    HAL_NVIC_SetPriority(ADC1_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(ADC1_IRQn);
  /* USER CODE BEGIN ADC1_MspInit 1 */
 
  /* USER CODE END ADC1_MspInit 1 */
  }
 
}
void MX_DMA_Init(void) 
{
  /* DMA controller clock enable */
  __HAL_RCC_DMAMUX1_CLK_ENABLE();
  __HAL_RCC_DMA1_CLK_ENABLE();
 
  /* DMA interrupt init */
  /* DMA1_Channel2_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(DMA1_Channel2_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(DMA1_Channel2_IRQn);
 
}

Thanks!!!

0 REPLIES 0