Skip to content

Commit 51ca96f

Browse files
committed
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
1 parent e2a2bd3 commit 51ca96f

File tree

6 files changed

+11
-5
lines changed

6 files changed

+11
-5
lines changed

embassy-stm32/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3131
- feat: stm32/usart: add `eager_reads` option to control if buffered readers return as soon as possible or after more data is available ([#4668](https://github.com/embassy-rs/embassy/pull/4668))
3232
- feat: stm32/usart: add `de_assertion_time` and `de_deassertion_time` config options
3333
- change: stm32/uart: BufferedUartRx now returns all available bytes from the internal buffer
34+
- fix: Calculate the ADC prescaler in a way that it allows for the max frequency to be reached
3435

3536
## 0.4.0 - 2025-08-26
3637

embassy-stm32/src/adc/adc4.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ enum Prescaler {
128128

129129
impl Prescaler {
130130
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;
132133
match raw_prescaler {
133134
0 => Self::NotDivided,
134135
1 => Self::DividedBy2,

embassy-stm32/src/adc/c0.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ pub enum Prescaler {
6666

6767
impl Prescaler {
6868
fn from_ker_ck(frequency: Hertz) -> Self {
69-
let raw_prescaler = frequency.0 / MAX_ADC_CLK_FREQ.0;
69+
// Calculate prescaler in a way where the clock can hit MAX CLK
70+
let raw_prescaler = frequency.0.saturating_sub(1) / MAX_ADC_CLK_FREQ.0;
7071
match raw_prescaler {
7172
0 => Self::NotDivided,
7273
1 => Self::DividedBy2,

embassy-stm32/src/adc/g4.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ enum Prescaler {
7272

7373
impl Prescaler {
7474
fn from_ker_ck(frequency: Hertz) -> Self {
75-
let raw_prescaler = frequency.0 / MAX_ADC_CLK_FREQ.0;
75+
// Calculate prescaler in a way where the clock can hit MAX CLK
76+
let raw_prescaler = frequency.0.saturating_sub(1) / MAX_ADC_CLK_FREQ.0;
7677
match raw_prescaler {
7778
0 => Self::NotDivided,
7879
1 => Self::DividedBy2,

embassy-stm32/src/adc/v2.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ impl Prescaler {
7171
// Datasheet for both F4 and F7 specifies min frequency 0.6 MHz, typ freq. 30 MHz and max 36 MHz.
7272
#[cfg(not(stm32f2))]
7373
const MAX_FREQUENCY: Hertz = Hertz(36_000_000);
74-
let raw_div = freq.0 / MAX_FREQUENCY.0;
74+
// Calculate prescaler divider including MAX_FREQ
75+
let raw_div = freq.0.saturating_sub(1) / MAX_FREQUENCY.0;
7576
match raw_div {
7677
0..=1 => Self::Div2,
7778
2..=3 => Self::Div4,

embassy-stm32/src/adc/v4.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ enum Prescaler {
9393

9494
impl Prescaler {
9595
fn from_ker_ck(frequency: Hertz) -> Self {
96-
let raw_prescaler = frequency.0 / MAX_ADC_CLK_FREQ.0;
96+
// Calculate prescaler in a way where the clock can hit MAX CLK
97+
let raw_prescaler = frequency.0.saturating_sub(1) / MAX_ADC_CLK_FREQ.0;
9798
match raw_prescaler {
9899
0 => Self::NotDivided,
99100
1 => Self::DividedBy2,

0 commit comments

Comments
 (0)