Skip to content

Commit

Permalink
Merge pull request #21011 from crasbe/pr/stm32l0_adc_fix
Browse files Browse the repository at this point in the history
cpu/stm32l0,l1: Fix ADC initialization order
  • Loading branch information
maribu authored Jan 15, 2025
2 parents 9a5ef12 + 17ee40d commit f4fcac2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
6 changes: 3 additions & 3 deletions cpu/stm32/periph/adc_l0.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,6 @@ int32_t adc_sample(adc_t line, adc_res_t res)
/* lock and power on the ADC device */
prep();

/* Enable ADC */
_enable_adc();

/* Reactivate VREFINT and temperature sensor if necessary */
if (adc_config[line].chan == 17) {
ADC->CCR |= ADC_CCR_VREFEN;
Expand All @@ -145,6 +142,9 @@ int32_t adc_sample(adc_t line, adc_res_t res)
ADC1->CFGR1 |= res & ADC_CFGR1_RES;
ADC1->CHSELR = (1 << adc_config[line].chan);

/* Enable ADC */
_enable_adc();

/* clear flag */
ADC1->ISR |= ADC_ISR_EOC;

Expand Down
17 changes: 12 additions & 5 deletions cpu/stm32/periph/adc_l1.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ int adc_init(adc_t line)
ADC->CCR |= ADC_CCR_TSVREFE;
}

/* enable the ADC module */
ADC1->CR2 = ADC_CR2_ADON;
/* turn off during idle phase*/
ADC1->CR1 = ADC_CR1_PDI;

Expand All @@ -157,12 +155,17 @@ int32_t adc_sample(adc_t line, adc_res_t res)
/* lock and power on the ADC device */
prep();

/* set resolution, conversion channel and single read */
ADC1->CR1 |= res & ADC_CR1_RES;
/* mask and set resolution, conversion channel and single read */
ADC1->CR1 = (ADC1->CR1 & ~ADC_CR1_RES) | (res & ADC_CR1_RES);
ADC1->SQR1 &= ~ADC_SQR1_L;
ADC1->SQR5 = adc_config[line].chan;

/* wait for regulat channel to be ready*/
/* only set ADON when ADONS bit is cleared (ADC not ready) */
if (!(ADC1->SR & ADC_SR_ADONS)) {
ADC1->CR2 |= ADC_CR2_ADON;
}

/* wait for regular channel to be ready*/
while (!(ADC1->SR & ADC_SR_RCNR)) {}
/* start conversion and wait for results */
ADC1->CR2 |= ADC_CR2_SWSTART;
Expand All @@ -171,6 +174,10 @@ int32_t adc_sample(adc_t line, adc_res_t res)
sample = (int)ADC1->DR;
ADC1 -> SR &= ~ADC_SR_STRT;

/* wait for ADC to become ready before disabling it */
while (!(ADC1->SR & ADC_SR_ADONS)) {}
ADC1->CR2 &= ~ADC_CR2_ADON;

/* power off and unlock device again */
done();

Expand Down

0 comments on commit f4fcac2

Please sign in to comment.