@@ -286,7 +286,7 @@ where
286
286
// and thus is maye a candidate for a more general RccClocksManangement thingy.
287
287
// frequency: impl Into<Generic<u32>>,
288
288
clocks : Clocks ,
289
- // adc_shared : &mut <ADC as Instance>::SharedInstance,
289
+ common_adc : & mut <ADC as Instance >:: SharedInstance ,
290
290
ahb : & mut AHB ,
291
291
) -> Self {
292
292
let mut adc = Self {
@@ -299,8 +299,8 @@ where
299
299
// crate::panic!("Clock settings not well defined");
300
300
// }
301
301
302
-
303
- ADC :: enable_clock ( ahb ) ;
302
+ ADC :: enable_clock ( & clocks , ahb , common_adc ) ;
303
+ ADC :: clock ( & clocks ) ;
304
304
// if !(ADC::enable_clock(ahb)){
305
305
// crate::panic!("Clock already enabled with a different setting");
306
306
// }
@@ -417,10 +417,8 @@ where
417
417
}
418
418
419
419
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 ( ) ) ;
421
421
let cpu_cycles = cycles * clocks. sysclk ( ) . 0 / frequency. 0 ;
422
- defmt:: info!( "{}" , cpu_cycles) ;
423
-
424
422
asm:: delay ( cpu_cycles) ;
425
423
}
426
424
@@ -521,9 +519,9 @@ pub trait Instance: Deref<Target = adc1::RegisterBlock> + crate::private::Sealed
521
519
/// Shared Instance / Registerblock between multiple ADCs
522
520
type SharedInstance ;
523
521
#[ doc( hidden) ]
524
- fn enable_clock ( ahb : & mut AHB ) ;
522
+ fn enable_clock ( clocks : & Clocks , ahb : & mut AHB , common_adc : & mut Self :: SharedInstance ) ;
525
523
#[ doc( hidden) ]
526
- fn clock ( clocks : & Clocks ) -> Hertz ;
524
+ fn clock ( clocks : & Clocks ) -> Option < Hertz > ;
527
525
}
528
526
529
527
// Macro to implement ADC functionallity for ADC1 and ADC2
@@ -541,17 +539,24 @@ macro_rules! adc {
541
539
impl crate :: private:: Sealed for $ADC { }
542
540
impl Instance for $ADC {
543
541
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 ) {
545
543
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
+ } ;
546
551
}
547
552
548
- fn clock( clocks: & Clocks ) -> Hertz {
553
+ fn clock( clocks: & Clocks ) -> Option < Hertz > {
549
554
use crate :: pac:: rcc:: cfgr2:: $ADCXYPRES_A;
550
555
// SAFETY: atomic read with no side effects
551
556
let cfgr2 = unsafe { & ( * RCC :: ptr( ) ) . cfgr2 } ;
552
557
let common_adc = unsafe { & ( * Self :: SharedInstance :: ptr( ) ) } ;
553
558
554
- match clocks. pllclk( ) {
559
+ Some ( match clocks. pllclk( ) {
555
560
Some ( pllclk) if !cfgr2. read( ) . $adcXYpres( ) . is_no_clock( ) => {
556
561
pllclk
557
562
/ match cfgr2. read( ) . $adcXYpres( ) . variant( ) {
@@ -576,10 +581,13 @@ macro_rules! adc {
576
581
CKMODE_A :: SYNCDIV1 => 1 ,
577
582
CKMODE_A :: SYNCDIV2 => 2 ,
578
583
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 ,
580
588
}
581
589
}
582
- }
590
+ } )
583
591
}
584
592
}
585
593
) +
0 commit comments