Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rtc module #93

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ cortex-m-rt = "0.6"
embedded-hal = "0.2"
nb = "0.1"
stm32f3 = "0.11"
rtcc = "0.2.0"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since specifying versions in Cargo.toml defaults to the caret operator (https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html), this is the same as "^0.2.0", which actually allows any patch version (i.e. 0.2.x). Therefore, it is less confusing to just specify rtcc = "0.2" here. We use this pattern for the other dependencies too.


[dependencies.bare-metal]
version = "0.2"
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ pub mod pwm;
#[cfg(feature = "device-selected")]
pub mod rcc;
#[cfg(feature = "device-selected")]
pub mod rtc;
#[cfg(feature = "device-selected")]
pub mod serial;
#[cfg(feature = "device-selected")]
pub mod spi;
Expand Down
46 changes: 46 additions & 0 deletions src/rcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ impl RccExt for RCC {
ahb: AHB { _0: () },
apb1: APB1 { _0: () },
apb2: APB2 { _0: () },
bdcr: BDCR { _0: () },
cfgr: CFGR {
hse: None,
hclk: None,
pclk1: None,
pclk2: None,
sysclk: None,
},
cr: CR { _0: () },
csr: CSR { _0: () },
}
}
}
Expand All @@ -39,8 +42,14 @@ pub struct Rcc {
pub apb1: APB1,
/// Advanced Peripheral Bus 2 (APB2) registers
pub apb2: APB2,
/// RCC Backup Domain
pub bdcr: BDCR,
/// Clock configuration
pub cfgr: CFGR,
/// RCC Clock Control register
pub cr: CR,
/// RCC Control/Status register
pub csr: CSR,
}

/// AMBA High-performance Bus (AHB) registers
Expand Down Expand Up @@ -157,7 +166,20 @@ mod usb_clocking {
}
}

/// Backup Domain Control register (RCC_BDCR)
pub struct BDCR {
_0: (),
}

impl BDCR {
pub(crate) fn bdcr(&mut self) -> &rcc::BDCR {
// NOTE(unsafe) this proxy grants exclusive access to this register
unsafe { &(*RCC::ptr()).bdcr }
}
}

/// Clock configuration
#[derive(Clone, Copy)]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We must not allow cloning of the CFGR struct. This struct is responsible for making sure that the clocks are only configured once. When you call freeze() it consumes the struct and thereby ensures the clocks won't change anymore, so other code can rely on that. If you make CFGR clonable you could suddenly have multiple instances of this struct around, which would allow you to change the clocks and break the assumption that they are stable

pub struct CFGR {
hse: Option<u32>,
hclk: Option<u32>,
Expand Down Expand Up @@ -580,6 +602,30 @@ impl CFGR {
}
}

/// RCC Clock Control register (RCC_CR)
pub struct CR {
_0: (),
}

impl CR {
pub(crate) fn cr(&mut self) -> &rcc::CR {
// NOTE(unsafe) this proxy grants exclusive access to this register
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not true. If you look at CFGR's freeze() method, that one also modifies the CR register, so the access here is not exclusive at all. I guess CFGR already assumes exclusive access to CR though that's not documented unfortunately. Which means this CR proxy shouldn't exist. Or at least it shouldn't exist as long as freeze() was not called

unsafe { &(*RCC::ptr()).cr }
}
}

/// RCC Control/Status register
pub struct CSR {
_0: (),
}

impl CSR {
pub(crate) fn csr(&mut self) -> &rcc::CSR {
// NOTE(unsafe) this proxy grants exclusive access to this register
unsafe { &(*RCC::ptr()).csr }
}
}

/// Frozen clock frequencies
///
/// The existence of this value indicates that the clock configuration can no longer be changed
Expand Down
Loading