cancel
Showing results for 
Search instead for 
Did you mean: 

ADC with HAL, EOC flag never set

bjornpaulstrom
Associate III
Posted on October 11, 2016 at 11:54

Hello I am trying to use the ADC like this:

ADC_ChannelConfTypeDef sConfig;

 adc.Instance = ADC1;

 adc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;

 adc.Init.Resolution = ADC_RESOLUTION_12B;

 adc.Init.ScanConvMode = DISABLE;

 adc.Init.ContinuousConvMode = DISABLE;

 adc.Init.DiscontinuousConvMode = DISABLE;

 adc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;

 adc.Init.DataAlign = ADC_DATAALIGN_RIGHT;

 adc.Init.NbrOfConversion = 1;

 adc.Init.DMAContinuousRequests = DISABLE;

 adc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;

 if (HAL_ADC_Init(&adc) != HAL_OK)

 {

   //Error_Handler();

 }

 sConfig.Channel = ADC_CHANNEL_0;

 sConfig.Rank = 1;

 sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;

 if (HAL_ADC_ConfigChannel(&adc, &sConfig) != HAL_OK)

 {

   //Error_Handler();

 }

GPIO_InitTypeDef GPIO_InitStruct;

 if(adcHandle->Instance==ADC1)

 {

   __HAL_RCC_ADC1_CLK_ENABLE();

   GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_4;

   GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;

   GPIO_InitStruct.Pull = GPIO_NOPULL;

   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

   HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);

 }

HAL_ADC_Start

(

&

g_AdcHandle

)

;

    

for

(

;

;

)

    

{

        

if

(

HAL_ADC_PollForConversion

(

&

g_AdcHandle

,

1000000

)

==

HAL_OK

)

        

{

            

g_ADCValue

=

HAL_ADC_GetValue

(

&

g_AdcHandle

)

;

            

g_MeasurementNumber

++

;

        

}

    

}

 But it gets stuck in this code, looking for EOC flag:

while(!(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOC)))

  {

    /* Check if timeout is disabled (set to infinite wait) */

    if(Timeout != HAL_MAX_DELAY)

    {

      if((Timeout == 0U) || ((HAL_GetTick() - tickstart ) > Timeout))

      {

        /* Update ADC state machine to timeout */

        SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);

        

        /* Process unlocked */

        __HAL_UNLOCK(hadc);

        

        return HAL_TIMEOUT;

      }

    }

  }

Any ideas why this happens?

#adc #hal
2 REPLIES 2
bjornpaulstrom
Associate III
Posted on October 11, 2016 at 19:00

I managed to solve this, Part of my code was executing in an interrupt and this was blocking the EOC flag from getting set, after I moved it out, it worked great.

Walid FTITI_O
Senior II
Posted on October 11, 2016 at 20:16

Hi paulstrom.bjorn, 

Thank you for sharing your finding . 

You can manage the interrupt conflict by setting the priorities of each interrupt in the NVIC register to be executed by priority's order and avoid such issue.

-Hannibal-