aboutsummaryrefslogtreecommitdiff
path: root/embassy-nrf
diff options
context:
space:
mode:
authorMatthew Tran <[email protected]>2025-02-21 23:40:53 -0600
committerMatthew Tran <[email protected]>2025-02-22 00:02:43 -0600
commit1b4f7884271faf79c9f92f940e9fc7d4c83ca644 (patch)
treeebe5cbeef9b0809dac27209a3a9582a6aa215a7e /embassy-nrf
parent2e7a2b61275d70d7d09cb676490e2cba4d2bde3f (diff)
nrf5340: configure LFCLK
Diffstat (limited to 'embassy-nrf')
-rw-r--r--embassy-nrf/Cargo.toml3
-rw-r--r--embassy-nrf/src/chips/nrf5340_app.rs4
-rw-r--r--embassy-nrf/src/lib.rs61
3 files changed, 65 insertions, 3 deletions
diff --git a/embassy-nrf/Cargo.toml b/embassy-nrf/Cargo.toml
index 1d5485f2a..405115dd7 100644
--- a/embassy-nrf/Cargo.toml
+++ b/embassy-nrf/Cargo.toml
@@ -57,6 +57,9 @@ nfc-pins-as-gpio = []
57## * nRF52820, nRF52833, nRF52840: P0_18 57## * nRF52820, nRF52833, nRF52840: P0_18
58reset-pin-as-gpio = [] 58reset-pin-as-gpio = []
59 59
60## Allow using the LFXO pins as regular GPIO pins (P0_00/P0_01 on nRF53)
61lfxo-pins-as-gpio = []
62
60## Implements the MultiwriteNorFlash trait for QSPI. Should only be enabled if your external 63## Implements the MultiwriteNorFlash trait for QSPI. Should only be enabled if your external
61## flash supports the semantics described [here](https://docs.rs/embedded-storage/0.3.1/embedded_storage/nor_flash/trait.MultiwriteNorFlash.html) 64## flash supports the semantics described [here](https://docs.rs/embedded-storage/0.3.1/embedded_storage/nor_flash/trait.MultiwriteNorFlash.html)
62qspi-multiwrite-flash = [] 65qspi-multiwrite-flash = []
diff --git a/embassy-nrf/src/chips/nrf5340_app.rs b/embassy-nrf/src/chips/nrf5340_app.rs
index bc613e351..b33a19df7 100644
--- a/embassy-nrf/src/chips/nrf5340_app.rs
+++ b/embassy-nrf/src/chips/nrf5340_app.rs
@@ -262,7 +262,9 @@ embassy_hal_internal::peripherals! {
262 PPI_GROUP5, 262 PPI_GROUP5,
263 263
264 // GPIO port 0 264 // GPIO port 0
265 #[cfg(any(not(feature = "_nrf5340"), feature = "lfxo-pins-as-gpio"))]
265 P0_00, 266 P0_00,
267 #[cfg(any(not(feature = "_nrf5340"), feature = "lfxo-pins-as-gpio"))]
266 P0_01, 268 P0_01,
267 #[cfg(feature = "nfc-pins-as-gpio")] 269 #[cfg(feature = "nfc-pins-as-gpio")]
268 P0_02, 270 P0_02,
@@ -368,7 +370,9 @@ impl_pdm!(PDM0, PDM0, PDM0);
368impl_qdec!(QDEC0, QDEC0, QDEC0); 370impl_qdec!(QDEC0, QDEC0, QDEC0);
369impl_qdec!(QDEC1, QDEC1, QDEC1); 371impl_qdec!(QDEC1, QDEC1, QDEC1);
370 372
373#[cfg(any(not(feature = "_nrf5340"), feature = "lfxo-pins-as-gpio"))]
371impl_pin!(P0_00, 0, 0); 374impl_pin!(P0_00, 0, 0);
375#[cfg(any(not(feature = "_nrf5340"), feature = "lfxo-pins-as-gpio"))]
372impl_pin!(P0_01, 0, 1); 376impl_pin!(P0_01, 0, 1);
373#[cfg(feature = "nfc-pins-as-gpio")] 377#[cfg(feature = "nfc-pins-as-gpio")]
374impl_pin!(P0_02, 0, 2); 378impl_pin!(P0_02, 0, 2);
diff --git a/embassy-nrf/src/lib.rs b/embassy-nrf/src/lib.rs
index 9a9485f04..4241f90b1 100644
--- a/embassy-nrf/src/lib.rs
+++ b/embassy-nrf/src/lib.rs
@@ -65,6 +65,9 @@ compile_error!("feature `reset-pin-as-gpio` is only valid for nRF52 series chips
65#[cfg(all(feature = "nfc-pins-as-gpio", not(any(feature = "_nrf52", feature = "_nrf5340-app"))))] 65#[cfg(all(feature = "nfc-pins-as-gpio", not(any(feature = "_nrf52", feature = "_nrf5340-app"))))]
66compile_error!("feature `nfc-pins-as-gpio` is only valid for nRF52, or nRF53's application core."); 66compile_error!("feature `nfc-pins-as-gpio` is only valid for nRF52, or nRF53's application core.");
67 67
68#[cfg(all(feature = "lfxo-pins-as-gpio", not(any(feature = "_nrf5340"))))]
69compile_error!("feature `lfxo-pins-as-gpio` is only valid for nRF53 series chips.");
70
68// This mod MUST go first, so that the others see its macros. 71// This mod MUST go first, so that the others see its macros.
69pub(crate) mod fmt; 72pub(crate) mod fmt;
70pub(crate) mod util; 73pub(crate) mod util;
@@ -282,15 +285,24 @@ pub mod config {
282 /// Internal RC oscillator 285 /// Internal RC oscillator
283 InternalRC, 286 InternalRC,
284 /// Synthesized from the high frequency clock source. 287 /// Synthesized from the high frequency clock source.
285 #[cfg(not(any(feature = "_nrf5340", feature = "_nrf91")))] 288 #[cfg(not(any(feature = "_nrf91")))]
286 Synthesized, 289 Synthesized,
287 /// External source from xtal. 290 /// External source from xtal.
291 #[cfg(not(all(feature = "_nrf5340", feature = "lfxo-pins-as-gpio")))]
288 ExternalXtal, 292 ExternalXtal,
289 /// External source from xtal with low swing applied. 293 /// External source from xtal with low swing applied.
290 #[cfg(not(any(feature = "_nrf5340", feature = "_nrf91", feature = "_nrf54l")))] 294 #[cfg(not(any(
295 all(feature = "_nrf5340", feature = "lfxo-pins-as-gpio"),
296 feature = "_nrf91",
297 feature = "_nrf54l"
298 )))]
291 ExternalLowSwing, 299 ExternalLowSwing,
292 /// External source from xtal with full swing applied. 300 /// External source from xtal with full swing applied.
293 #[cfg(not(any(feature = "_nrf5340", feature = "_nrf91", feature = "_nrf54l")))] 301 #[cfg(not(any(
302 all(feature = "_nrf5340", feature = "lfxo-pins-as-gpio"),
303 feature = "_nrf91",
304 feature = "_nrf54l"
305 )))]
294 ExternalFullSwing, 306 ExternalFullSwing,
295 } 307 }
296 308
@@ -706,6 +718,19 @@ pub fn init(config: config::Config) -> Peripherals {
706 } 718 }
707 } 719 }
708 720
721 // Workaround for anomaly 140
722 #[cfg(feature = "nrf5340-app-s")]
723 if unsafe { (0x50032420 as *mut u32).read_volatile() } & 0x80000000 != 0 {
724 r.events_lfclkstarted().write_value(0);
725 r.lfclksrc()
726 .write(|w| w.set_src(nrf_pac::clock::vals::Lfclksrc::LFSYNT));
727 r.tasks_lfclkstart().write_value(1);
728 while r.events_lfclkstarted().read() == 0 {}
729 r.events_lfclkstarted().write_value(0);
730 r.tasks_lfclkstop().write_value(1);
731 r.lfclksrc().write(|w| w.set_src(nrf_pac::clock::vals::Lfclksrc::LFRC));
732 }
733
709 // Configure LFCLK. 734 // Configure LFCLK.
710 #[cfg(not(any(feature = "_nrf51", feature = "_nrf5340", feature = "_nrf91", feature = "_nrf54l")))] 735 #[cfg(not(any(feature = "_nrf51", feature = "_nrf5340", feature = "_nrf91", feature = "_nrf54l")))]
711 match config.lfclk_source { 736 match config.lfclk_source {
@@ -723,6 +748,36 @@ pub fn init(config: config::Config) -> Peripherals {
723 w.set_bypass(true); 748 w.set_bypass(true);
724 }), 749 }),
725 } 750 }
751 #[cfg(feature = "_nrf5340")]
752 {
753 #[allow(unused_mut)]
754 let mut lfxo = false;
755 match config.lfclk_source {
756 config::LfclkSource::InternalRC => r.lfclksrc().write(|w| w.set_src(pac::clock::vals::Lfclksrc::LFRC)),
757 config::LfclkSource::Synthesized => r.lfclksrc().write(|w| w.set_src(pac::clock::vals::Lfclksrc::LFSYNT)),
758 #[cfg(not(feature = "lfxo-pins-as-gpio"))]
759 config::LfclkSource::ExternalXtal => lfxo = true,
760 #[cfg(not(feature = "lfxo-pins-as-gpio"))]
761 config::LfclkSource::ExternalLowSwing => lfxo = true,
762 #[cfg(not(feature = "lfxo-pins-as-gpio"))]
763 config::LfclkSource::ExternalFullSwing => {
764 #[cfg(all(feature = "_nrf5340-app"))]
765 pac::OSCILLATORS.xosc32ki().bypass().write(|w| w.set_bypass(true));
766 lfxo = true;
767 }
768 }
769 if lfxo {
770 if cfg!(feature = "_s") {
771 // MCUSEL is only accessible from secure code.
772 let p0 = pac::P0;
773 p0.pin_cnf(0)
774 .write(|w| w.set_mcusel(pac::gpio::vals::Mcusel::PERIPHERAL));
775 p0.pin_cnf(1)
776 .write(|w| w.set_mcusel(pac::gpio::vals::Mcusel::PERIPHERAL));
777 }
778 r.lfclksrc().write(|w| w.set_src(pac::clock::vals::Lfclksrc::LFXO));
779 }
780 }
726 #[cfg(feature = "_nrf91")] 781 #[cfg(feature = "_nrf91")]
727 match config.lfclk_source { 782 match config.lfclk_source {
728 config::LfclkSource::InternalRC => r.lfclksrc().write(|w| w.set_src(pac::clock::vals::Lfclksrc::LFRC)), 783 config::LfclkSource::InternalRC => r.lfclksrc().write(|w| w.set_src(pac::clock::vals::Lfclksrc::LFRC)),