Skip to content

Commit 10e4b23

Browse files
committed
F7
1 parent 8272723 commit 10e4b23

File tree

3 files changed

+71
-112
lines changed

3 files changed

+71
-112
lines changed

.zed/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"rust-analyzer": {
44
"initialization_options": {
55
"cargo": {
6-
"features": ["defmt", "rtic1", "stm32f411"]
6+
"features": ["defmt", "rtic1", "stm32f777"]
77
},
88
"check": {
99
"allTargets": false,

src/rcc/f7/enable.rs

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,19 @@ macro_rules! bus_enable {
44
($PER:ident => $en:ident) => {
55
impl Enable for crate::pac::$PER {
66
#[inline(always)]
7-
fn enable(bus: &mut Self::Bus) {
8-
bus.enr().modify(|_, w| w.$en().set_bit());
7+
fn enable(rcc: &mut RCC) {
8+
Self::Bus::enr(rcc).modify(|_, w| w.$en().set_bit());
99
// Stall the pipeline to work around erratum 2.1.13 (DM00037591)
1010
cortex_m::asm::dsb();
1111
}
1212
#[inline(always)]
13-
fn disable(bus: &mut Self::Bus) {
14-
bus.enr().modify(|_, w| w.$en().clear_bit());
13+
fn disable(rcc: &mut RCC) {
14+
Self::Bus::enr(rcc).modify(|_, w| w.$en().clear_bit());
1515
}
1616
#[inline(always)]
1717
fn is_enabled() -> bool {
18-
Self::Bus::new().enr().read().$en().bit_is_set()
19-
}
20-
#[inline(always)]
21-
unsafe fn enable_unchecked() {
22-
Self::enable(&mut Self::Bus::new());
23-
}
24-
#[inline(always)]
25-
unsafe fn disable_unchecked() {
26-
Self::disable(&mut Self::Bus::new());
18+
let rcc = RCC::ptr();
19+
Self::Bus::enr(unsafe { &*rcc }).read().$en().bit_is_set()
2720
}
2821
}
2922
};
@@ -33,26 +26,22 @@ macro_rules! bus_lpenable {
3326
($PER:ident => $lpen:ident) => {
3427
impl LPEnable for crate::pac::$PER {
3528
#[inline(always)]
36-
fn low_power_enable(bus: &mut Self::Bus) {
37-
bus.lpenr().modify(|_, w| w.$lpen().set_bit());
29+
fn enable_in_low_power(rcc: &mut RCC) {
30+
Self::Bus::lpenr(rcc).modify(|_, w| w.$lpen().set_bit());
3831
// Stall the pipeline to work around erratum 2.1.13 (DM00037591)
3932
cortex_m::asm::dsb();
4033
}
4134
#[inline(always)]
42-
fn low_power_disable(bus: &mut Self::Bus) {
43-
bus.lpenr().modify(|_, w| w.$lpen().clear_bit());
44-
}
45-
#[inline(always)]
46-
fn is_low_power_enabled() -> bool {
47-
Self::Bus::new().lpenr().read().$lpen().bit_is_set()
48-
}
49-
#[inline(always)]
50-
unsafe fn low_power_enable_unchecked() {
51-
Self::enable(&mut Self::Bus::new());
35+
fn disable_in_low_power(rcc: &mut RCC) {
36+
Self::Bus::lpenr(rcc).modify(|_, w| w.$lpen().clear_bit());
5237
}
5338
#[inline(always)]
54-
unsafe fn low_power_disable_unchecked() {
55-
Self::disable(&mut Self::Bus::new());
39+
fn is_enabled_in_low_power() -> bool {
40+
let rcc = RCC::ptr();
41+
Self::Bus::lpenr(unsafe { &*rcc })
42+
.read()
43+
.$lpen()
44+
.bit_is_set()
5645
}
5746
}
5847
};
@@ -61,14 +50,10 @@ macro_rules! bus_reset {
6150
($PER:ident => $rst:ident) => {
6251
impl Reset for crate::pac::$PER {
6352
#[inline(always)]
64-
fn reset(bus: &mut Self::Bus) {
65-
let bits = bus.rstr().modify(|_, w| w.$rst().set_bit());
66-
bus.rstr()
67-
.write(|w| unsafe { w.bits(bits).$rst().clear_bit() });
68-
}
69-
#[inline(always)]
70-
unsafe fn reset_unchecked() {
71-
Self::reset(&mut Self::Bus::new());
53+
fn reset(rcc: &mut RCC) {
54+
let rstr = Self::Bus::rstr(rcc);
55+
let bits = rstr.modify(|_, w| w.$rst().set_bit());
56+
rstr.write(|w| unsafe { w.bits(bits).$rst().clear_bit() });
7257
}
7358
}
7459
};

src/rcc/f7/mod.rs

Lines changed: 50 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use super::{BusClock, BusTimerClock, RccBus};
99
#[cfg_attr(test, allow(unused_imports))]
1010
use micromath::F32Ext;
1111

12+
use crate::pac::rcc::RegisterBlock as RccRB;
1213
use crate::pac::{rcc, FLASH, PWR, RCC};
1314
use fugit::{HertzU32 as Hertz, RateExtU32};
1415

@@ -24,12 +25,6 @@ pub trait RccExt {
2425
impl 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
6661
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,
8062
pub cfgr: CFGR,
8163
}
8264

8365
macro_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)]
259222
enum 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)]
272230
struct 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
13341291
pub 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
13611325
pub 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
13881359
pub 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

Comments
 (0)