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 | |
| parent | bfc7f52e6dd7b5ad12fa1f09483fa60f2732ae0c (diff) | |
Add `init` fn. Initializes hw and returns Peripherals.
Diffstat (limited to 'embassy-nrf/src')
| -rw-r--r-- | embassy-nrf/src/lib.rs | 86 | ||||
| -rw-r--r-- | embassy-nrf/src/system.rs | 79 |
2 files changed, 85 insertions, 80 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 | } | ||
diff --git a/embassy-nrf/src/system.rs b/embassy-nrf/src/system.rs deleted file mode 100644 index e358d2c38..000000000 --- a/embassy-nrf/src/system.rs +++ /dev/null | |||
| @@ -1,79 +0,0 @@ | |||
| 1 | use crate::pac; | ||
| 2 | |||
| 3 | pub enum HfclkSource { | ||
| 4 | Internal, | ||
| 5 | ExternalXtal, | ||
| 6 | } | ||
| 7 | |||
| 8 | pub enum LfclkSource { | ||
| 9 | InternalRC, | ||
| 10 | Synthesized, | ||
| 11 | ExternalXtal, | ||
| 12 | ExternalLowSwing, | ||
| 13 | ExternalFullSwing, | ||
| 14 | } | ||
| 15 | |||
| 16 | #[non_exhaustive] | ||
| 17 | pub struct Config { | ||
| 18 | pub hfclk_source: HfclkSource, | ||
| 19 | pub lfclk_source: LfclkSource, | ||
| 20 | } | ||
| 21 | |||
| 22 | impl Default for Config { | ||
| 23 | fn default() -> Self { | ||
| 24 | Self { | ||
| 25 | // There are hobby nrf52 boards out there without external XTALs... | ||
| 26 | // Default everything to internal so it Just Works. User can enable external | ||
| 27 | // xtals if they know they have them. | ||
| 28 | hfclk_source: HfclkSource::Internal, | ||
| 29 | lfclk_source: LfclkSource::InternalRC, | ||
| 30 | } | ||
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | /// safety: must only call once. | ||
| 35 | pub unsafe fn configure(config: Config) { | ||
| 36 | let r = &*pac::CLOCK::ptr(); | ||
| 37 | |||
| 38 | // Start HFCLK. | ||
| 39 | match config.hfclk_source { | ||
| 40 | HfclkSource::Internal => {} | ||
| 41 | HfclkSource::ExternalXtal => { | ||
| 42 | // Datasheet says this is likely to take 0.36ms | ||
| 43 | r.events_hfclkstarted.write(|w| unsafe { w.bits(0) }); | ||
| 44 | r.tasks_hfclkstart.write(|w| unsafe { w.bits(1) }); | ||
| 45 | while r.events_hfclkstarted.read().bits() == 0 {} | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | // Configure LFCLK. | ||
| 50 | match config.lfclk_source { | ||
| 51 | LfclkSource::InternalRC => r.lfclksrc.write(|w| w.src().rc()), | ||
| 52 | LfclkSource::Synthesized => r.lfclksrc.write(|w| w.src().synth()), | ||
| 53 | |||
| 54 | LfclkSource::ExternalXtal => r.lfclksrc.write(move |w| w.src().xtal()), | ||
| 55 | |||
| 56 | LfclkSource::ExternalLowSwing => r.lfclksrc.write(move |w| { | ||
| 57 | w.src().xtal(); | ||
| 58 | w.external().enabled(); | ||
| 59 | w.bypass().disabled(); | ||
| 60 | w | ||
| 61 | }), | ||
| 62 | LfclkSource::ExternalFullSwing => r.lfclksrc.write(move |w| { | ||
| 63 | w.src().xtal(); | ||
| 64 | w.external().enabled(); | ||
| 65 | w.bypass().enabled(); | ||
| 66 | w | ||
| 67 | }), | ||
| 68 | } | ||
| 69 | |||
| 70 | // Start LFCLK. | ||
| 71 | // Datasheet says this could take 100us from synth source | ||
| 72 | // 600us from rc source, 0.25s from an external source. | ||
| 73 | r.events_lfclkstarted.write(|w| unsafe { w.bits(0) }); | ||
| 74 | r.tasks_lfclkstart.write(|w| unsafe { w.bits(1) }); | ||
| 75 | while r.events_lfclkstarted.read().bits() == 0 {} | ||
| 76 | |||
| 77 | // Init GPIOTE | ||
| 78 | crate::gpiote::init(); | ||
| 79 | } | ||
