STM32F091RC ADC interrupt enabling

64 views Asked by At

I am trying to configure adc in stm32f091rc in interrupt mode but having problem with callback function called from the interrupt request handler. I have attached my codes below, I tried to follow a tutorial on stm32f4 and i ported the code for my specific MCU but it didn't work.

#define ADCENABLE       (1U<<9)
#define GPIOAENABLE     (1U<<17)
#define ADC_CH1         (1U<<1)
#define CR_ADEN         (1U<<0)
#define CFGR_CONT       (1U<<13)
#define CR_EOCIE        (1U<<2)


void pa1_adc_interrupt_init(void)
{


    /* Configure the ADC GPIO Pin */

    /* Enable Clock access to gpioa */
    RCC->AHBENR |= GPIOAENABLE;
    /* Set the mode of PA1 to analog mode */
    GPIOA->MODER |= (1<<2);
    GPIOA->MODER |= (1<<3);



    /* Configure the ADC module */
    /* Enable clock access to ADC */
    RCC->APB2ENR |= ADCENABLE;


    /* Enable ADC end-of-conversion interrupt */
    ADC1->IER |= CR_EOCIE;


    /* Setting Priority */
    //NVIC_SetPriority(ADC1_COMP_IRQn, 0);


    /* Enable ADC interrupt in NVIC */
    NVIC_EnableIRQ(ADC1_COMP_IRQn);


    /* Configure adc parameters */
    /* Conversion sequence start */


    ADC1->CHSELR = ADC_CH1;

    /* Conversion sequence length */
    /* Enable ADC module */
    ADC1->CR |= CR_ADEN;

}
static void adc_callback(void)
{
    sensor_value = ADC1->DR;
    printf("the sensor value is %d \n\r", (int)sensor_value);
}





void ADC_COM_IRQHandler(void)
{
    /* Check for eoc in SR */

    /*
    if(ADC1->ISR & ADC_EOC)
    {
//       Clear EOC
        ADC1->ISR &= ~ADC_EOC;

        // doing sth here
        adc_callback();


    }

*/

    if(EXTI->PR & (1U<<21))
        {

        adc_callback();

            val = 1;
            EXTI->PR |= (1U<<21);



        }


}

adc_callback is somewhat not called, I tried to call the callback when the pending status is read as 1 and i clear it with 1 to indicate that the pending is cleared.

Can anyone help me with the problem i am facing ?

1

There are 1 answers

2
0___________ On BEST ANSWER
  1. Do not use prinf in the interrupt handlers
  2. You GPIO & ADC clock initialization is invalid (read Reference manual about the timimg. You cant use any GPIO registers directly after clock enable)
  3. Your handler name does not match the standard STM startup file name. It is ADC1_COMP_IRQHandler. Check your vector table definition.
  4. SYSCFG clock not enabled
  5. You did not follow the ADC initialization procedure. ADC has dual clock domain. Enabling digital part clock is not enough.

And probably plenty other issues.

I would suggest to write the code with the Reference Manual in your hand instead of following the internet tutorials.