Skip to content

Commit

Permalink
cpu/stm32l1: fix ADC initialization & resolution setting
Browse files Browse the repository at this point in the history
Co-authored-by: benpicco <benpicco@googlemail.com>
  • Loading branch information
crasbe and benpicco committed Nov 27, 2024
1 parent 2f8b23a commit 0485824
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 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,18 @@ 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 */
/* mask and set resolution, conversion channel and single read */
ADC1->CR1 &= ~ADC_CR1_RES_Msk;
ADC1->CR1 |= 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_Msk)) {
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 +175,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_Msk;

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

Expand Down

0 comments on commit 0485824

Please sign in to comment.