Skip to content

Commit 347fc6f

Browse files
committed
Introduce fallback if no clock is set for adc
1 parent 1731687 commit 347fc6f

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

examples/adc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ fn main() -> ! {
2525
// The following is only needed to make sure the clock signal for the ADC is set up
2626
// correctly.
2727
clocks,
28+
&mut dp.ADC1_2,
2829
&mut rcc.ahb,
2930
);
3031

src/adc.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ where
286286
// and thus is maye a candidate for a more general RccClocksManangement thingy.
287287
// frequency: impl Into<Generic<u32>>,
288288
clocks: Clocks,
289-
// adc_shared : &mut <ADC as Instance>::SharedInstance,
289+
common_adc: &mut <ADC as Instance>::SharedInstance,
290290
ahb: &mut AHB,
291291
) -> Self {
292292
let mut adc = Self {
@@ -299,8 +299,8 @@ where
299299
// crate::panic!("Clock settings not well defined");
300300
// }
301301

302-
303-
ADC::enable_clock(ahb);
302+
ADC::enable_clock(&clocks, ahb, common_adc);
303+
ADC::clock(&clocks);
304304
// if !(ADC::enable_clock(ahb)){
305305
// crate::panic!("Clock already enabled with a different setting");
306306
// }
@@ -417,10 +417,8 @@ where
417417
}
418418

419419
fn wait_adc_clk_cycles(&self, cycles: u32, clocks: Clocks) {
420-
let frequency = ADC::clock(&clocks);
420+
let frequency = ADC::clock(&clocks).unwrap_or(clocks.sysclk());
421421
let cpu_cycles = cycles * clocks.sysclk().0 / frequency.0;
422-
defmt::info!("{}", cpu_cycles);
423-
424422
asm::delay(cpu_cycles);
425423
}
426424

@@ -521,9 +519,9 @@ pub trait Instance: Deref<Target = adc1::RegisterBlock> + crate::private::Sealed
521519
/// Shared Instance / Registerblock between multiple ADCs
522520
type SharedInstance;
523521
#[doc(hidden)]
524-
fn enable_clock(ahb: &mut AHB);
522+
fn enable_clock(clocks: &Clocks, ahb: &mut AHB, common_adc: &mut Self::SharedInstance);
525523
#[doc(hidden)]
526-
fn clock(clocks: &Clocks) -> Hertz;
524+
fn clock(clocks: &Clocks) -> Option<Hertz>;
527525
}
528526

529527
// Macro to implement ADC functionallity for ADC1 and ADC2
@@ -541,17 +539,24 @@ macro_rules! adc {
541539
impl crate::private::Sealed for $ADC {}
542540
impl Instance for $ADC {
543541
type SharedInstance = $ADCX_Y;
544-
fn enable_clock(ahb: &mut AHB) {
542+
fn enable_clock(clocks: &Clocks, ahb: &mut AHB, common_adc: &mut Self::SharedInstance) {
545543
ahb.enr().modify(|_, w| w.$adcXYen().enabled());
544+
545+
// No clock can be set, so we have to fallback to a default.
546+
if Self::clock(clocks).is_none() {
547+
common_adc.ccr.modify(|_, w| w
548+
.ckmode().variant(CKMODE_A::SYNCDIV1)
549+
);
550+
};
546551
}
547552

548-
fn clock(clocks: &Clocks) -> Hertz {
553+
fn clock(clocks: &Clocks) -> Option<Hertz> {
549554
use crate::pac::rcc::cfgr2::$ADCXYPRES_A;
550555
// SAFETY: atomic read with no side effects
551556
let cfgr2 = unsafe { &(*RCC::ptr()).cfgr2 };
552557
let common_adc = unsafe { &(*Self::SharedInstance::ptr()) };
553558

554-
match clocks.pllclk() {
559+
Some(match clocks.pllclk() {
555560
Some(pllclk) if !cfgr2.read().$adcXYpres().is_no_clock() => {
556561
pllclk
557562
/ match cfgr2.read().$adcXYpres().variant() {
@@ -576,10 +581,13 @@ macro_rules! adc {
576581
CKMODE_A::SYNCDIV1 => 1,
577582
CKMODE_A::SYNCDIV2 => 2,
578583
CKMODE_A::SYNCDIV4 => 4,
579-
CKMODE_A::ASYNCHRONOUS => crate::panic!("No ADC clock"),
584+
// Asynchronous should be enabled if PLL is on.
585+
// If this line of code is reached PLL is off.
586+
// Indicate that, so fallbacks can be set.
587+
CKMODE_A::ASYNCHRONOUS => return None,
580588
}
581589
}
582-
}
590+
})
583591
}
584592
}
585593
)+

testsuite/tests/adc.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ mod tests {
5050
adc: Some(adc::Adc::new(
5151
dp.ADC1,
5252
clocks,
53+
&mut dp.ADC1_2,
5354
&mut rcc.ahb,
5455
)),
5556
analog: pair.0,
@@ -90,6 +91,7 @@ mod tests {
9091
let new_adc = adc::Adc::new(
9192
adc1,
9293
state.clocks,
94+
&mut state.adc1_2,
9395
&mut state.ahb,
9496
);
9597

0 commit comments

Comments
 (0)