diff options
| author | Dario Nieuwenhuis <[email protected]> | 2021-05-12 01:57:01 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2021-05-17 00:57:37 +0200 |
| commit | 0310e4d458b86df31f1765104eb3aa9a6ee09bfc (patch) | |
| tree | aca167ae6e37442cafc932b3c729e37082d23b04 /embassy-nrf/src/lib.rs | |
| parent | bfc7f52e6dd7b5ad12fa1f09483fa60f2732ae0c (diff) | |
Add `init` fn. Initializes hw and returns Peripherals.
Diffstat (limited to 'embassy-nrf/src/lib.rs')
| -rw-r--r-- | embassy-nrf/src/lib.rs | 86 |
1 files changed, 85 insertions, 1 deletions
diff --git a/embassy-nrf/src/lib.rs b/embassy-nrf/src/lib.rs index 4349bf39f..6ab9cbb27 100644 --- a/embassy-nrf/src/lib.rs +++ b/embassy-nrf/src/lib.rs | |||
| @@ -35,7 +35,6 @@ pub mod rtc; | |||
| 35 | #[cfg(not(feature = "nrf52820"))] | 35 | #[cfg(not(feature = "nrf52820"))] |
| 36 | pub mod saadc; | 36 | pub mod saadc; |
| 37 | pub mod spim; | 37 | pub mod spim; |
| 38 | pub mod system; | ||
| 39 | pub mod timer; | 38 | pub mod timer; |
| 40 | pub mod twim; | 39 | pub mod twim; |
| 41 | pub mod uarte; | 40 | pub mod uarte; |
| @@ -73,3 +72,88 @@ pub mod interrupt { | |||
| 73 | pub use embassy_extras::interrupt::Priority3 as Priority; | 72 | pub use embassy_extras::interrupt::Priority3 as Priority; |
| 74 | } | 73 | } |
| 75 | pub use embassy_macros::interrupt; | 74 | pub use embassy_macros::interrupt; |
| 75 | |||
| 76 | pub mod config { | ||
| 77 | pub enum HfclkSource { | ||
| 78 | Internal, | ||
| 79 | ExternalXtal, | ||
| 80 | } | ||
| 81 | |||
| 82 | pub enum LfclkSource { | ||
| 83 | InternalRC, | ||
| 84 | Synthesized, | ||
| 85 | ExternalXtal, | ||
| 86 | ExternalLowSwing, | ||
| 87 | ExternalFullSwing, | ||
| 88 | } | ||
| 89 | |||
| 90 | #[non_exhaustive] | ||
| 91 | pub struct Config { | ||
| 92 | pub hfclk_source: HfclkSource, | ||
| 93 | pub lfclk_source: LfclkSource, | ||
| 94 | } | ||
| 95 | |||
| 96 | impl Default for Config { | ||
| 97 | fn default() -> Self { | ||
| 98 | Self { | ||
| 99 | // There are hobby nrf52 boards out there without external XTALs... | ||
| 100 | // Default everything to internal so it Just Works. User can enable external | ||
| 101 | // xtals if they know they have them. | ||
| 102 | hfclk_source: HfclkSource::Internal, | ||
| 103 | lfclk_source: LfclkSource::InternalRC, | ||
| 104 | } | ||
| 105 | } | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | pub fn init(config: config::Config) -> Peripherals { | ||
| 110 | // Do this first, so that it panics if user is calling `init` a second time | ||
| 111 | // before doing anything important. | ||
| 112 | let peripherals = Peripherals::take(); | ||
| 113 | |||
| 114 | let r = unsafe { &*pac::CLOCK::ptr() }; | ||
| 115 | |||
| 116 | // Start HFCLK. | ||
| 117 | match config.hfclk_source { | ||
| 118 | config::HfclkSource::Internal => {} | ||
| 119 | config::HfclkSource::ExternalXtal => { | ||
| 120 | // Datasheet says this is likely to take 0.36ms | ||
| 121 | r.events_hfclkstarted.write(|w| unsafe { w.bits(0) }); | ||
| 122 | r.tasks_hfclkstart.write(|w| unsafe { w.bits(1) }); | ||
| 123 | while r.events_hfclkstarted.read().bits() == 0 {} | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | // Configure LFCLK. | ||
| 128 | match config.lfclk_source { | ||
| 129 | config::LfclkSource::InternalRC => r.lfclksrc.write(|w| w.src().rc()), | ||
| 130 | config::LfclkSource::Synthesized => r.lfclksrc.write(|w| w.src().synth()), | ||
| 131 | |||
| 132 | config::LfclkSource::ExternalXtal => r.lfclksrc.write(|w| w.src().xtal()), | ||
| 133 | |||
| 134 | config::LfclkSource::ExternalLowSwing => r.lfclksrc.write(|w| { | ||
| 135 | w.src().xtal(); | ||
| 136 | w.external().enabled(); | ||
| 137 | w.bypass().disabled(); | ||
| 138 | w | ||
| 139 | }), | ||
| 140 | config::LfclkSource::ExternalFullSwing => r.lfclksrc.write(|w| { | ||
| 141 | w.src().xtal(); | ||
| 142 | w.external().enabled(); | ||
| 143 | w.bypass().enabled(); | ||
| 144 | w | ||
| 145 | }), | ||
| 146 | } | ||
| 147 | |||
| 148 | // Start LFCLK. | ||
| 149 | // Datasheet says this could take 100us from synth source | ||
| 150 | // 600us from rc source, 0.25s from an external source. | ||
| 151 | r.events_lfclkstarted.write(|w| unsafe { w.bits(0) }); | ||
| 152 | r.tasks_lfclkstart.write(|w| unsafe { w.bits(1) }); | ||
| 153 | while r.events_lfclkstarted.read().bits() == 0 {} | ||
| 154 | |||
| 155 | // Init GPIOTE | ||
| 156 | crate::gpiote::init(); | ||
| 157 | |||
| 158 | peripherals | ||
| 159 | } | ||
