@@ -9,6 +9,7 @@ use super::{BusClock, BusTimerClock, RccBus};
9
9
#[ cfg_attr( test, allow( unused_imports) ) ]
10
10
use micromath:: F32Ext ;
11
11
12
+ use crate :: pac:: rcc:: RegisterBlock as RccRB ;
12
13
use crate :: pac:: { rcc, FLASH , PWR , RCC } ;
13
14
use fugit:: { HertzU32 as Hertz , RateExtU32 } ;
14
15
@@ -24,12 +25,6 @@ pub trait RccExt {
24
25
impl RccExt for RCC {
25
26
fn constrain ( self ) -> Rcc {
26
27
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 ( ) ,
33
28
cfgr : CFGR {
34
29
hse : None ,
35
30
hse_bypass : false ,
@@ -64,48 +59,27 @@ impl RccExt for RCC {
64
59
65
60
/// Constrained RCC peripheral
66
61
pub 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 ,
80
62
pub cfgr : CFGR ,
81
63
}
82
64
83
65
macro_rules! bus_struct {
84
66
( $( $busX: ident => ( $EN: ident, $en: ident, $LPEN: ident, $lpen: ident, $RST: ident, $rst: ident, $doc: literal) , ) +) => {
85
67
$(
86
68
#[ doc = $doc]
87
- pub struct $busX {
88
- _0: ( ) ,
89
- }
69
+ #[ non_exhaustive]
70
+ pub struct $busX;
90
71
91
72
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( )
99
75
}
100
76
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( )
104
79
}
105
80
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( )
109
83
}
110
84
}
111
85
) +
@@ -120,17 +94,6 @@ bus_struct! {
120
94
AHB3 => ( AHB3ENR , ahb3enr, AHB3LPENR , ahb3lpenr, AHB3RSTR , ahb3rstr, "Advanced High-performance Bus 3 (AHB3) registers" ) ,
121
95
}
122
96
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
-
134
97
/*impl HSEClock {
135
98
/// Provide HSE frequency.
136
99
///
@@ -255,19 +218,14 @@ pub enum MCO2 {
255
218
Pll ,
256
219
}
257
220
258
- #[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
221
+ #[ derive( Clone , Copy , Debug , Default , PartialEq , Eq ) ]
259
222
enum VOSscale {
260
223
PwrScale1 ,
261
224
PwrScale2 ,
225
+ #[ default]
262
226
PwrScale3 ,
263
227
}
264
228
265
- impl Default for VOSscale {
266
- fn default ( ) -> Self {
267
- VOSscale :: PwrScale3
268
- }
269
- }
270
-
271
229
#[ derive( Clone , Copy , Debug , PartialEq , Eq , Default ) ]
272
230
struct InternalRCCConfig {
273
231
hpre : u8 ,
@@ -605,7 +563,7 @@ impl CFGR {
605
563
}
606
564
+ 1 )
607
565
>> 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 )
609
567
>> Self :: FIXED_POINT_RSHIFT )
610
568
* one_over_p as u64 )
611
569
>> Self :: FIXED_POINT_RSHIFT
@@ -621,7 +579,7 @@ impl CFGR {
621
579
( ( 1 << Self :: FIXED_POINT_LSHIFT ) / ( self . pllm as u32 ) + 1 ) >> 1 ;
622
580
let one_over_q =
623
581
( ( 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 )
625
583
>> Self :: FIXED_POINT_RSHIFT )
626
584
* one_over_q as u64 )
627
585
>> Self :: FIXED_POINT_RSHIFT
@@ -644,12 +602,11 @@ impl CFGR {
644
602
}
645
603
+ 1 )
646
604
>> 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 ;
653
610
( 48_000_000 - 120_000 ..=48_000_000 + 120_000 ) . contains ( & pll48clk)
654
611
} else {
655
612
false
@@ -845,7 +802,7 @@ impl CFGR {
845
802
continue ;
846
803
}
847
804
// 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 ;
849
806
let f_vco_clock = ( ( ( f_pll_clock_input as u64 >> Self :: BASE_CLK_SHIFT )
850
807
* n as u64
851
808
* one_over_m as u64 )
@@ -1333,66 +1290,83 @@ impl From<MCOPRE> for crate::pac::rcc::cfgr::MCO1PRE {
1333
1290
/// Enable/disable peripheral
1334
1291
pub trait Enable : RccBus {
1335
1292
/// Enables peripheral
1336
- fn enable ( bus : & mut Self :: Bus ) ;
1293
+ fn enable ( rcc : & mut RCC ) ;
1337
1294
1338
1295
/// Disables peripheral
1339
- fn disable ( bus : & mut Self :: Bus ) ;
1296
+ fn disable ( rcc : & mut RCC ) ;
1340
1297
1341
1298
/// Check if peripheral enabled
1342
1299
fn is_enabled ( ) -> bool ;
1343
1300
1344
1301
/// Check if peripheral disabled
1302
+ #[ inline]
1345
1303
fn is_disabled ( ) -> bool {
1346
1304
!Self :: is_enabled ( )
1347
1305
}
1348
1306
1349
1307
/// # Safety
1350
1308
///
1351
1309
/// 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
+ }
1353
1314
1354
1315
/// # Safety
1355
1316
///
1356
1317
/// 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
+ }
1358
1322
}
1359
1323
1360
- /// Enable /disable peripheral in low power mode
1324
+ /// Low power enable /disable peripheral
1361
1325
pub trait LPEnable : RccBus {
1362
1326
/// Enables peripheral in low power mode
1363
- fn low_power_enable ( bus : & mut Self :: Bus ) ;
1327
+ fn enable_in_low_power ( rcc : & mut RCC ) ;
1364
1328
1365
1329
/// Disables peripheral in low power mode
1366
- fn low_power_disable ( bus : & mut Self :: Bus ) ;
1330
+ fn disable_in_low_power ( rcc : & mut RCC ) ;
1367
1331
1368
1332
/// Check if peripheral enabled in low power mode
1369
- fn is_low_power_enabled ( ) -> bool ;
1333
+ fn is_enabled_in_low_power ( ) -> bool ;
1370
1334
1371
1335
/// 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 ( )
1374
1339
}
1375
1340
1376
1341
/// # Safety
1377
1342
///
1378
1343
/// 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
+ }
1380
1348
1381
1349
/// # Safety
1382
1350
///
1383
1351
/// 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
+ }
1385
1356
}
1386
1357
1387
1358
/// Reset peripheral
1388
1359
pub trait Reset : RccBus {
1389
1360
/// Resets peripheral
1390
- fn reset ( bus : & mut Self :: Bus ) ;
1361
+ fn reset ( rcc : & mut RCC ) ;
1391
1362
1392
1363
/// # Safety
1393
1364
///
1394
1365
/// 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
+ }
1396
1370
}
1397
1371
1398
1372
#[ cfg( test) ]
0 commit comments