nrf/system: add configure
This commit is contained in:
@ -31,8 +31,8 @@ embedded-hal = { version = "0.2.4" }
|
||||
embedded-dma = { version = "0.1.2" }
|
||||
futures = { version = "0.3.5", default-features = false }
|
||||
|
||||
nrf52810-pac = { version = "0.9.0", optional = true }
|
||||
nrf52811-pac = { version = "0.9.1", optional = true }
|
||||
nrf52832-pac = { version = "0.9.0", optional = true }
|
||||
nrf52833-pac = { version = "0.9.0", optional = true }
|
||||
nrf52840-pac = { version = "0.9.0", optional = true }
|
||||
nrf52810-pac = { version = "0.9.0", optional = true, features = [ "rt" ]}
|
||||
nrf52811-pac = { version = "0.9.1", optional = true, features = [ "rt" ]}
|
||||
nrf52832-pac = { version = "0.9.0", optional = true, features = [ "rt" ]}
|
||||
nrf52833-pac = { version = "0.9.0", optional = true, features = [ "rt" ]}
|
||||
nrf52840-pac = { version = "0.9.0", optional = true, features = [ "rt" ]}
|
||||
|
@ -92,6 +92,7 @@ pub mod qspi;
|
||||
pub mod rtc;
|
||||
pub mod saadc;
|
||||
pub mod spim;
|
||||
pub mod system;
|
||||
pub mod timer;
|
||||
pub mod uarte;
|
||||
|
||||
|
76
embassy-nrf/src/system.rs
Normal file
76
embassy-nrf/src/system.rs
Normal file
@ -0,0 +1,76 @@
|
||||
use crate::pac;
|
||||
|
||||
pub enum HfclkSource {
|
||||
Internal,
|
||||
ExternalXtal,
|
||||
}
|
||||
|
||||
pub enum LfclkSource {
|
||||
InternalRC,
|
||||
Synthesized,
|
||||
ExternalXtal,
|
||||
ExternalLowSwing,
|
||||
ExternalFullSwing,
|
||||
}
|
||||
|
||||
#[non_exhaustive]
|
||||
pub struct Config {
|
||||
pub hfclk_source: HfclkSource,
|
||||
pub lfclk_source: LfclkSource,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
// There are hobby nrf52 boards out there without external XTALs...
|
||||
// Default everything to internal so it Just Works. User can enable external
|
||||
// xtals if they know they have them.
|
||||
hfclk_source: HfclkSource::Internal,
|
||||
lfclk_source: LfclkSource::InternalRC,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// safety: must only call once.
|
||||
pub unsafe fn configure(config: Config) {
|
||||
let r = &*pac::CLOCK::ptr();
|
||||
|
||||
// Start HFCLK.
|
||||
match config.hfclk_source {
|
||||
HfclkSource::Internal => {}
|
||||
HfclkSource::ExternalXtal => {
|
||||
// Datasheet says this is likely to take 0.36ms
|
||||
r.events_hfclkstarted.write(|w| unsafe { w.bits(0) });
|
||||
r.tasks_hfclkstart.write(|w| unsafe { w.bits(1) });
|
||||
while r.events_hfclkstarted.read().bits() == 0 {}
|
||||
}
|
||||
}
|
||||
|
||||
// Configure LFCLK.
|
||||
match config.lfclk_source {
|
||||
LfclkSource::InternalRC => r.lfclksrc.write(|w| w.src().rc()),
|
||||
LfclkSource::Synthesized => r.lfclksrc.write(|w| w.src().synth()),
|
||||
|
||||
LfclkSource::ExternalXtal => r.lfclksrc.write(move |w| w.src().xtal()),
|
||||
|
||||
LfclkSource::ExternalLowSwing => r.lfclksrc.write(move |w| {
|
||||
w.src().xtal();
|
||||
w.external().enabled();
|
||||
w.bypass().disabled();
|
||||
w
|
||||
}),
|
||||
LfclkSource::ExternalFullSwing => r.lfclksrc.write(move |w| {
|
||||
w.src().xtal();
|
||||
w.external().enabled();
|
||||
w.bypass().enabled();
|
||||
w
|
||||
}),
|
||||
}
|
||||
|
||||
// Start LFCLK.
|
||||
// Datasheet says this could take 100us from synth source
|
||||
// 600us from rc source, 0.25s from an external source.
|
||||
r.events_lfclkstarted.write(|w| unsafe { w.bits(0) });
|
||||
r.tasks_lfclkstart.write(|w| unsafe { w.bits(1) });
|
||||
while r.events_lfclkstarted.read().bits() == 0 {}
|
||||
}
|
Reference in New Issue
Block a user