aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2022-12-13 14:42:23 -0500
committerKevin O'Connor <kevin@koconnor.net>2023-01-17 18:22:21 -0500
commitcefbad9ef4cea82c6bc006b1f78f174f69bc594e (patch)
tree04b262d1489d9f67b32d9c66b124092da6fa416d
parentd372f913efb84b2303aff9906caeed44d9b37069 (diff)
downloadkutter-cefbad9ef4cea82c6bc006b1f78f174f69bc594e.tar.gz
kutter-cefbad9ef4cea82c6bc006b1f78f174f69bc594e.tar.xz
kutter-cefbad9ef4cea82c6bc006b1f78f174f69bc594e.zip
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>
-rw-r--r--src/stm32/stm32h7_adc.c33
1 files changed, 12 insertions, 21 deletions
diff --git a/src/stm32/stm32h7_adc.c b/src/stm32/stm32h7_adc.c
index b9e14916..75a56508 100644
--- a/src/stm32/stm32h7_adc.c
+++ b/src/stm32/stm32h7_adc.c
@@ -209,31 +209,22 @@ gpio_adc_setup(uint32_t pin)
// Enable the ADC
if (!(adc->CR & ADC_CR_ADEN)) {
- // Pwr
- // Exit deep power down
- MODIFY_REG(adc->CR, ADC_CR_DEEPPWD_Msk, 0);
- // Switch on voltage regulator
- adc->CR |= ADC_CR_ADVREGEN;
- // Wait for voltage regulator to stabilize
+ // Switch on voltage regulator and wait for it to stabilize
+ adc->CR = ADC_CR_ADVREGEN;
uint32_t end = timer_read_time() + timer_from_us(20);
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)
-#ifdef ADC_CR_BOOST
- MODIFY_REG(adc->CR, ADC_CR_BOOST_Msk, 0b10 << ADC_CR_BOOST_Pos);
-#endif
-
- // Calibration
- // 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);
+ cr |= 0b10 << ADC_CR_BOOST_Pos;
+ // Use linear calibration on stm32h7
+ cr |= ADC_CR_ADCALLIN;
#endif
- // Start the calibration
- MODIFY_REG(adc->CR, ADC_CR_ADCAL_Msk, ADC_CR_ADCAL);
- while(adc->CR & ADC_CR_ADCAL)
+ adc->CR = cr;
+ while (adc->CR & ADC_CR_ADCAL)
;
// Enable ADC
@@ -265,8 +256,8 @@ gpio_adc_setup(uint32_t pin)
gpio_peripheral(pin, GPIO_ANALOG, 0);
}
- // Preselect (connect) channel
-#ifdef ADC_PCSEL_PCSEL
+ // Setup preselect (connect) channel on stm32h7
+#if CONFIG_MACH_STM32H7
adc->PCSEL |= (1 << chan);
#endif
return (struct gpio_adc){ .adc = adc, .chan = chan };