AnsweredAssumed Answered

STM32F302 change ADC clocksource

Question asked by Clemens Ehm on Jan 12, 2018
Latest reply on Feb 5, 2018 by Clemens Ehm

Hey,

 

in my current ADC code I use the PLL as clock source (72MHz). That causes a problem in the powersave mode, where the PLL is disabled and the clock source of the mcu core is just HSE (12 MHz).

Now I want to use the AHB clock instead of the PLL clock but it's simply not working.

 

Here an overview of my current pll adc code:

[code]

    //GPIO Init
    RCC->AHBENR |= RCC_AHBENR_GPIOAEN ;
    GPIOA->MODER |= GPIO_MODER_MODER0;

 

    //CLK Init
    RCC->CFGR2 |= RCC_CFGR2_ADCPRE12_DIV8; //PLL Clk / 4
    RCC->AHBENR |= RCC_AHBENR_ADC12EN;

 

    //Calibration
    ADC1->CR &= ~ADC_CR_ADVREGEN;
    ADC1->CR |= ADC_CR_ADVREGEN_0;
    ADC1->CR &= ~ADC_CR_ADCALDIF;
    ADC1->CR |= ADC_CR_ADCAL;
    while (ADC1->CR & ADC_CR_ADCAL);

 

    set_saved_vref_cal_value( *((volatile t_u16*)(0x1FFFF7BA)));

 

    //Enable ADC
    do
    {
        ADC1->CR |= ADC_CR_ADEN;
    } while ((ADC1->ISR & ADC_ISR_ADRD) == 0);

 

    //Config ADC
    ADC1->CFGR &= ~ADC_CFGR_RES; //12bit resolution
    ADC1->CFGR |= ADC_CFGR_CONT; //ADC_ContinuousConvMode_Enable
    ADC1->CFGR &= ~ADC_CFGR_ALIGN; // Right data alignment

 


    ADC1->SQR1 |= ADC_SQR1_SQ1_0; //ADC Channel In1 (PA0)
    ADC1->SQR1 |= ADC_SQR1_SQ2_4; //ADC Channel 16 Temperature Sensor
    ADC1->SQR1 |= ADC_SQR1_SQ3_4 | ADC_SQR1_SQ3_1; //ADC Channel 18 VRef Voltage
    ADC1->SQR1 &= ~ADC_SQR1_L; // reset active channels
    ADC1->SQR1 |= ADC_SQR1_L_1 | ADC_SQR1_L_0; //Set active channel count to 3

 

    ADC1_2->CCR = ADC12_CCR_VREFEN | ADC12_CCR_TSEN;

 

    ADC1->SMPR1 |= ADC_SMPR1_SMP0_2 | ADC_SMPR1_SMP0_1 | ADC_SMPR1_SMP0_0; //Sampling
    ADC1->SMPR2 |= ADC_SMPR2_SMP16_2 | ADC_SMPR2_SMP16_1 | ADC_SMPR2_SMP16_0;
    ADC1->SMPR2 |= ADC_SMPR2_SMP18_2 | ADC_SMPR2_SMP18_1 | ADC_SMPR2_SMP18_0;

 

    ADC1->IER  =  ADC_IER_EOC;

 

    NVIC_SetPriority(ADC1_IRQn, 0);
    NVIC_EnableIRQ(ADC1_IRQn);

[/code]

 

I tried a lot with the clk init part without success. As I saw in the manuel the RCC->CFGR2 must be set to zero (bits 8:4) to use the AHB clock. I also change the ADC1_2->CCR Register and use CKMode - but it's not working, the adc always stuck at the init part.

Could someone please show me how to use the AHB clock?

Outcomes