stm32: Reduce read-modify-write of CR in stm32h7_adc.c init

Prefer explicitly setting the CR hardware register to defined values
during initialization.

Also, prefer "#if CONFIG_MACH_STM32H7" over bit definitions to make it
clear that the code applies only on stm32h7.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2022-12-13 14:42:23 -05:00
parent d372f913ef
commit cefbad9ef4
1 changed files with 12 additions and 21 deletions

View File

@ -209,31 +209,22 @@ gpio_adc_setup(uint32_t pin)
// Enable the ADC // Enable the ADC
if (!(adc->CR & ADC_CR_ADEN)) { if (!(adc->CR & ADC_CR_ADEN)) {
// Pwr // Switch on voltage regulator and wait for it to stabilize
// Exit deep power down adc->CR = ADC_CR_ADVREGEN;
MODIFY_REG(adc->CR, ADC_CR_DEEPPWD_Msk, 0);
// Switch on voltage regulator
adc->CR |= ADC_CR_ADVREGEN;
// Wait for voltage regulator to stabilize
uint32_t end = timer_read_time() + timer_from_us(20); uint32_t end = timer_read_time() + timer_from_us(20);
while (timer_is_before(timer_read_time(), end)) while (timer_is_before(timer_read_time(), end))
; ;
// Perform adc calibration
uint32_t cr = ADC_CR_ADVREGEN | ADC_CR_ADCAL;
#if CONFIG_MACH_STM32H7
// Set boost mode on stm32h7 (adc clock is at 25Mhz) // Set boost mode on stm32h7 (adc clock is at 25Mhz)
#ifdef ADC_CR_BOOST cr |= 0b10 << ADC_CR_BOOST_Pos;
MODIFY_REG(adc->CR, ADC_CR_BOOST_Msk, 0b10 << ADC_CR_BOOST_Pos); // Use linear calibration on stm32h7
cr |= ADC_CR_ADCALLIN;
#endif #endif
adc->CR = cr;
// Calibration while (adc->CR & ADC_CR_ADCAL)
// Set calibration mode to Single ended (not differential)
MODIFY_REG(adc->CR, ADC_CR_ADCALDIF_Msk, 0);
// Enable linearity calibration
#ifdef ADC_CR_ADCALLIN
MODIFY_REG(adc->CR, ADC_CR_ADCALLIN_Msk, ADC_CR_ADCALLIN);
#endif
// Start the calibration
MODIFY_REG(adc->CR, ADC_CR_ADCAL_Msk, ADC_CR_ADCAL);
while(adc->CR & ADC_CR_ADCAL)
; ;
// Enable ADC // Enable ADC
@ -265,8 +256,8 @@ gpio_adc_setup(uint32_t pin)
gpio_peripheral(pin, GPIO_ANALOG, 0); gpio_peripheral(pin, GPIO_ANALOG, 0);
} }
// Preselect (connect) channel // Setup preselect (connect) channel on stm32h7
#ifdef ADC_PCSEL_PCSEL #if CONFIG_MACH_STM32H7
adc->PCSEL |= (1 << chan); adc->PCSEL |= (1 << chan);
#endif #endif
return (struct gpio_adc){ .adc = adc, .chan = chan }; return (struct gpio_adc){ .adc = adc, .chan = chan };