@@ -231,7 +231,7 @@ impl Rcc {
231231 }
232232 }
233233
234- pub fn unlock_rtc ( & mut self ) {
234+ pub fn unlock_rtc_old ( & mut self ) {
235235 self . rb . apb1enr1 . modify ( |_, w| w. pwren ( ) . set_bit ( ) ) ;
236236 let pwr = unsafe { & ( * PWR :: ptr ( ) ) } ;
237237 pwr. cr1 . modify ( |_, w| w. dbp ( ) . set_bit ( ) ) ;
@@ -457,6 +457,46 @@ impl Rcc {
457457 pub fn clear_reset_reason ( & mut self ) {
458458 self . rb . csr . modify ( |_, w| w. rmvf ( ) . set_bit ( ) ) ;
459459 }
460+
461+ pub ( crate ) fn unlock_rtc ( & self ) {
462+ self . rb . apb1enr1 . modify ( |_, w| w. pwren ( ) . set_bit ( ) ) ;
463+ let pwr = unsafe { & ( * PWR :: ptr ( ) ) } ;
464+ pwr. cr1 . modify ( |_, w| w. dbp ( ) . set_bit ( ) ) ;
465+ while pwr. cr1 . read ( ) . dbp ( ) . bit_is_clear ( ) { }
466+ }
467+
468+ pub ( crate ) fn enable_rtc ( & self , src : RTCSrc ) {
469+ self . unlock_rtc ( ) ;
470+ self . rb
471+ . apb1enr1
472+ . modify ( |_, w| w. rtcapben ( ) . set_bit ( ) . pwren ( ) . set_bit ( ) ) ;
473+ self . rb . apb1smenr1 . modify ( |_, w| w. rtcapbsmen ( ) . set_bit ( ) ) ;
474+ self . rb . bdcr . modify ( |_, w| w. bdrst ( ) . set_bit ( ) ) ;
475+
476+ let rtc_sel = match src {
477+ RTCSrc :: LSE | RTCSrc :: LSE_BYPASS => 0b01 ,
478+ RTCSrc :: LSI => 0b10 ,
479+ RTCSrc :: HSE | RTCSrc :: HSE_BYPASS => 0b11 ,
480+ } ;
481+
482+ self . rb . bdcr . modify ( |_, w| {
483+ w. rtcsel ( )
484+ . bits ( rtc_sel)
485+ . rtcen ( )
486+ . set_bit ( )
487+ . bdrst ( )
488+ . clear_bit ( )
489+ } ) ;
490+
491+ self . unlock_rtc ( ) ;
492+ match src {
493+ RTCSrc :: LSE => self . enable_lse ( false ) ,
494+ RTCSrc :: LSE_BYPASS => self . enable_lse ( true ) ,
495+ RTCSrc :: LSI => self . enable_lsi ( ) ,
496+ RTCSrc :: HSE => self . enable_hse ( false ) ,
497+ RTCSrc :: HSE_BYPASS => self . enable_hse ( true ) ,
498+ } ;
499+ }
460500}
461501
462502pub struct ResetReason {
0 commit comments