diff options
| author | Piotr Esden-Tempski <[email protected]> | 2025-10-05 15:49:05 -0700 |
|---|---|---|
| committer | Piotr Esden-Tempski <[email protected]> | 2025-10-05 16:16:05 -0700 |
| commit | a9727a17b593f7328f721e8905b7fc8dab9ae7ff (patch) | |
| tree | cf3bf4069e41ce9ca6108be5368a05f212acf6d6 /embassy-stm32/src/adc/adc4.rs | |
| parent | e2a2bd3c573928208a4c85e7fcd6ad630f23f47d (diff) | |
stm32/ADC: Fix prescaler calculation to include max frequency.
Due to the integer rounding rules one has to subtract 1 from the numerator.
For example:
Let max clock be 55 and supplied clock be 110
110/55 = 2 which results in the divider being set to 4 and the clock after division ends up being 27 instead of 55
Subtracting 1 to the numerator get around the rounding issue
109/55 = 1 which results in the divider being set to 2 and the clock after division ends up being 55 which is exactly max clock
Diffstat (limited to 'embassy-stm32/src/adc/adc4.rs')
| -rw-r--r-- | embassy-stm32/src/adc/adc4.rs | 3 |
1 files changed, 2 insertions, 1 deletions
diff --git a/embassy-stm32/src/adc/adc4.rs b/embassy-stm32/src/adc/adc4.rs index 255dc7956..1302dffb8 100644 --- a/embassy-stm32/src/adc/adc4.rs +++ b/embassy-stm32/src/adc/adc4.rs | |||
| @@ -128,7 +128,8 @@ enum Prescaler { | |||
| 128 | 128 | ||
| 129 | impl Prescaler { | 129 | impl Prescaler { |
| 130 | fn from_ker_ck(frequency: Hertz) -> Self { | 130 | fn from_ker_ck(frequency: Hertz) -> Self { |
| 131 | let raw_prescaler = frequency.0 / MAX_ADC_CLK_FREQ.0; | 131 | // Calculate prescaler in a way where the clock can hit MAX CLK |
| 132 | let raw_prescaler = frequency.0.saturating_sub(1) / MAX_ADC_CLK_FREQ.0; | ||
| 132 | match raw_prescaler { | 133 | match raw_prescaler { |
| 133 | 0 => Self::NotDivided, | 134 | 0 => Self::NotDivided, |
| 134 | 1 => Self::DividedBy2, | 135 | 1 => Self::DividedBy2, |
