@@ -9,6 +9,7 @@ use super::{BusClock, BusTimerClock, RccBus};
99#[ cfg_attr( test, allow( unused_imports) ) ]
1010use micromath:: F32Ext ;
1111
12+ use crate :: pac:: rcc:: RegisterBlock as RccRB ;
1213use crate :: pac:: { rcc, FLASH , PWR , RCC } ;
1314use fugit:: { HertzU32 as Hertz , RateExtU32 } ;
1415
@@ -24,12 +25,6 @@ pub trait RccExt {
2425impl RccExt for RCC {
2526 fn constrain ( self ) -> Rcc {
2627 Rcc {
27- ahb1 : AHB1 :: new ( ) ,
28- ahb2 : AHB2 :: new ( ) ,
29- ahb3 : AHB3 :: new ( ) ,
30- apb1 : APB1 :: new ( ) ,
31- apb2 : APB2 :: new ( ) ,
32- bdcr : BDCR :: new ( ) ,
3328 cfgr : CFGR {
3429 hse : None ,
3530 hse_bypass : false ,
@@ -64,48 +59,27 @@ impl RccExt for RCC {
6459
6560/// Constrained RCC peripheral
6661pub struct Rcc {
67- /// Advanced High-Performance Bus 1 (AHB1) registers
68- pub ahb1 : AHB1 ,
69- /// Advanced High-Performance Bus 2 (AHB2) registers
70- pub ahb2 : AHB2 ,
71- /// Advanced High-Performance Bus 3 (AHB3) registers
72- pub ahb3 : AHB3 ,
73-
74- /// Advanced Peripheral Bus 1 (APB1) registers
75- pub apb1 : APB1 ,
76- /// Advanced Peripheral Bus 2 (APB2) registers
77- pub apb2 : APB2 ,
78- /// RCC Backup Domain
79- pub bdcr : BDCR ,
8062 pub cfgr : CFGR ,
8163}
8264
8365macro_rules! bus_struct {
8466 ( $( $busX: ident => ( $EN: ident, $en: ident, $LPEN: ident, $lpen: ident, $RST: ident, $rst: ident, $doc: literal) , ) +) => {
8567 $(
8668 #[ doc = $doc]
87- pub struct $busX {
88- _0: ( ) ,
89- }
69+ #[ non_exhaustive]
70+ pub struct $busX;
9071
9172 impl $busX {
92- pub ( crate ) fn new( ) -> Self {
93- Self { _0: ( ) }
94- }
95-
96- pub ( crate ) fn enr( & self ) -> & rcc:: $EN {
97- // NOTE(unsafe) this proxy grants exclusive access to this register
98- unsafe { ( * RCC :: ptr( ) ) . $en( ) }
73+ pub ( crate ) fn enr( rcc: & RccRB ) -> & rcc:: $EN {
74+ rcc. $en( )
9975 }
10076
101- pub ( crate ) fn lpenr( & self ) -> & rcc:: $LPEN {
102- // NOTE(unsafe) this proxy grants exclusive access to this register
103- unsafe { ( * RCC :: ptr( ) ) . $lpen( ) }
77+ pub ( crate ) fn lpenr( rcc: & RccRB ) -> & rcc:: $LPEN {
78+ rcc. $lpen( )
10479 }
10580
106- pub ( crate ) fn rstr( & self ) -> & rcc:: $RST {
107- // NOTE(unsafe) this proxy grants exclusive access to this register
108- unsafe { ( * RCC :: ptr( ) ) . $rst( ) }
81+ pub ( crate ) fn rstr( rcc: & RccRB ) -> & rcc:: $RST {
82+ rcc. $rst( )
10983 }
11084 }
11185 ) +
@@ -120,17 +94,6 @@ bus_struct! {
12094 AHB3 => ( AHB3ENR , ahb3enr, AHB3LPENR , ahb3lpenr, AHB3RSTR , ahb3rstr, "Advanced High-performance Bus 3 (AHB3) registers" ) ,
12195}
12296
123- /// Backup Domain Control register (RCC_BDCR)
124- pub struct BDCR {
125- _0 : ( ) ,
126- }
127-
128- impl BDCR {
129- pub ( crate ) fn new ( ) -> Self {
130- Self { _0 : ( ) }
131- }
132- }
133-
13497/*impl HSEClock {
13598 /// Provide HSE frequency.
13699 ///
@@ -255,19 +218,14 @@ pub enum MCO2 {
255218 Pll ,
256219}
257220
258- #[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
221+ #[ derive( Clone , Copy , Debug , Default , PartialEq , Eq ) ]
259222enum VOSscale {
260223 PwrScale1 ,
261224 PwrScale2 ,
225+ #[ default]
262226 PwrScale3 ,
263227}
264228
265- impl Default for VOSscale {
266- fn default ( ) -> Self {
267- VOSscale :: PwrScale3
268- }
269- }
270-
271229#[ derive( Clone , Copy , Debug , PartialEq , Eq , Default ) ]
272230struct InternalRCCConfig {
273231 hpre : u8 ,
@@ -605,7 +563,7 @@ impl CFGR {
605563 }
606564 + 1 )
607565 >> 1 ;
608- sysclk = ( ( ( base_clk as u64 * self . plln as u64 * one_over_m as u64 )
566+ sysclk = ( ( ( base_clk * self . plln as u64 * one_over_m as u64 )
609567 >> Self :: FIXED_POINT_RSHIFT )
610568 * one_over_p as u64 )
611569 >> Self :: FIXED_POINT_RSHIFT
@@ -621,7 +579,7 @@ impl CFGR {
621579 ( ( 1 << Self :: FIXED_POINT_LSHIFT ) / ( self . pllm as u32 ) + 1 ) >> 1 ;
622580 let one_over_q =
623581 ( ( 1 << Self :: FIXED_POINT_LSHIFT ) / ( self . pllq as u32 ) + 1 ) >> 1 ;
624- let pll48clk = ( ( ( base_clk as u64 * self . plln as u64 * one_over_m as u64 )
582+ let pll48clk = ( ( ( base_clk * self . plln as u64 * one_over_m as u64 )
625583 >> Self :: FIXED_POINT_RSHIFT )
626584 * one_over_q as u64 )
627585 >> Self :: FIXED_POINT_RSHIFT
@@ -644,12 +602,11 @@ impl CFGR {
644602 }
645603 + 1 )
646604 >> 1 ;
647- let pll48clk =
648- ( ( ( base_clk as u64 * self . pllsain as u64 * one_over_m as u64 )
649- >> Self :: FIXED_POINT_RSHIFT )
650- * one_over_p as u64 )
651- >> Self :: FIXED_POINT_RSHIFT
652- << Self :: BASE_CLK_SHIFT ;
605+ let pll48clk = ( ( ( base_clk * self . pllsain as u64 * one_over_m as u64 )
606+ >> Self :: FIXED_POINT_RSHIFT )
607+ * one_over_p as u64 )
608+ >> Self :: FIXED_POINT_RSHIFT
609+ << Self :: BASE_CLK_SHIFT ;
653610 ( 48_000_000 - 120_000 ..=48_000_000 + 120_000 ) . contains ( & pll48clk)
654611 } else {
655612 false
@@ -845,7 +802,7 @@ impl CFGR {
845802 continue ;
846803 }
847804 // See the comments around Self::FIXED_POINT_LSHIFT to see how this works.
848- let one_over_m = ( ( 1 << Self :: FIXED_POINT_LSHIFT ) / ( m as u32 ) + 1 ) >> 1 ;
805+ let one_over_m = ( ( 1 << Self :: FIXED_POINT_LSHIFT ) / m + 1 ) >> 1 ;
849806 let f_vco_clock = ( ( ( f_pll_clock_input as u64 >> Self :: BASE_CLK_SHIFT )
850807 * n as u64
851808 * one_over_m as u64 )
@@ -1333,66 +1290,83 @@ impl From<MCOPRE> for crate::pac::rcc::cfgr::MCO1PRE {
13331290/// Enable/disable peripheral
13341291pub trait Enable : RccBus {
13351292 /// Enables peripheral
1336- fn enable ( bus : & mut Self :: Bus ) ;
1293+ fn enable ( rcc : & mut RCC ) ;
13371294
13381295 /// Disables peripheral
1339- fn disable ( bus : & mut Self :: Bus ) ;
1296+ fn disable ( rcc : & mut RCC ) ;
13401297
13411298 /// Check if peripheral enabled
13421299 fn is_enabled ( ) -> bool ;
13431300
13441301 /// Check if peripheral disabled
1302+ #[ inline]
13451303 fn is_disabled ( ) -> bool {
13461304 !Self :: is_enabled ( )
13471305 }
13481306
13491307 /// # Safety
13501308 ///
13511309 /// Enables peripheral. Takes access to RCC internally
1352- unsafe fn enable_unchecked ( ) ;
1310+ unsafe fn enable_unchecked ( ) {
1311+ let mut rcc = RCC :: steal ( ) ;
1312+ Self :: enable ( & mut rcc) ;
1313+ }
13531314
13541315 /// # Safety
13551316 ///
13561317 /// Disables peripheral. Takes access to RCC internally
1357- unsafe fn disable_unchecked ( ) ;
1318+ unsafe fn disable_unchecked ( ) {
1319+ let mut rcc = RCC :: steal ( ) ;
1320+ Self :: disable ( & mut rcc) ;
1321+ }
13581322}
13591323
1360- /// Enable /disable peripheral in low power mode
1324+ /// Low power enable /disable peripheral
13611325pub trait LPEnable : RccBus {
13621326 /// Enables peripheral in low power mode
1363- fn low_power_enable ( bus : & mut Self :: Bus ) ;
1327+ fn enable_in_low_power ( rcc : & mut RCC ) ;
13641328
13651329 /// Disables peripheral in low power mode
1366- fn low_power_disable ( bus : & mut Self :: Bus ) ;
1330+ fn disable_in_low_power ( rcc : & mut RCC ) ;
13671331
13681332 /// Check if peripheral enabled in low power mode
1369- fn is_low_power_enabled ( ) -> bool ;
1333+ fn is_enabled_in_low_power ( ) -> bool ;
13701334
13711335 /// Check if peripheral disabled in low power mode
1372- fn is_low_power_disabled ( ) -> bool {
1373- !Self :: is_low_power_enabled ( )
1336+ #[ inline]
1337+ fn is_disabled_in_low_power ( ) -> bool {
1338+ !Self :: is_enabled_in_low_power ( )
13741339 }
13751340
13761341 /// # Safety
13771342 ///
13781343 /// Enables peripheral in low power mode. Takes access to RCC internally
1379- unsafe fn low_power_enable_unchecked ( ) ;
1344+ unsafe fn enable_in_low_power_unchecked ( ) {
1345+ let mut rcc = RCC :: steal ( ) ;
1346+ Self :: enable_in_low_power ( & mut rcc) ;
1347+ }
13801348
13811349 /// # Safety
13821350 ///
13831351 /// Disables peripheral in low power mode. Takes access to RCC internally
1384- unsafe fn low_power_disable_unchecked ( ) ;
1352+ unsafe fn disable_in_low_power_unchecked ( ) {
1353+ let mut rcc = RCC :: steal ( ) ;
1354+ Self :: disable_in_low_power ( & mut rcc) ;
1355+ }
13851356}
13861357
13871358/// Reset peripheral
13881359pub trait Reset : RccBus {
13891360 /// Resets peripheral
1390- fn reset ( bus : & mut Self :: Bus ) ;
1361+ fn reset ( rcc : & mut RCC ) ;
13911362
13921363 /// # Safety
13931364 ///
13941365 /// Resets peripheral. Takes access to RCC internally
1395- unsafe fn reset_unchecked ( ) ;
1366+ unsafe fn reset_unchecked ( ) {
1367+ let mut rcc = RCC :: steal ( ) ;
1368+ Self :: reset ( & mut rcc) ;
1369+ }
13961370}
13971371
13981372#[ cfg( test) ]
0 commit comments