From b4de9b3ed59818e5e6c236763f96a941ceb5ffc6 Mon Sep 17 00:00:00 2001 From: Mirabellensaft Date: Mon, 13 Mar 2023 18:33:02 +0100 Subject: [PATCH] add solution and template for uarte_enable --- down-the-stack/apps/src/bin/uarte_enable.rs | 31 ++---------- .../apps/src/bin/uarte_enable_solution.rs | 48 +++++++++++++++++++ 2 files changed, 53 insertions(+), 26 deletions(-) create mode 100644 down-the-stack/apps/src/bin/uarte_enable_solution.rs diff --git a/down-the-stack/apps/src/bin/uarte_enable.rs b/down-the-stack/apps/src/bin/uarte_enable.rs index 2f46d79..0f380e4 100644 --- a/down-the-stack/apps/src/bin/uarte_enable.rs +++ b/down-the-stack/apps/src/bin/uarte_enable.rs @@ -3,7 +3,8 @@ use cortex_m::asm; use cortex_m_rt::entry; -use dk_pac::UARTE0; + +// ^^^^ import the PAC here // this imports `down-the-stack/apps/lib.rs` to retrieve our global logger + panicking-behavior use apps as _; @@ -15,23 +16,8 @@ fn main() -> ! { // to enable more verbose logs, go to your `Cargo.toml` and set defmt logging levels // to `defmt-trace` by changing the `default = []` entry in `[features]` - // takes ownership of the nRF52840-DK peripherals - let periph = dk_pac::Peripherals::take().unwrap(); - let uarte = periph.UARTE0; - - uarte_enabled(&uarte); - - // enable the UART0 peripheral the safe way - uarte.enable.write(|w| w.enable().enabled()); - - uarte_enabled(&uarte); - - // disable the UART0 peripheral by writing 0 directly into the register -- the unsafe way - unsafe { - uarte.enable.write(|w| w.bits(0x00 as u32)); - } - - uarte_enabled(&uarte); + // Your code goes here... + // this program does not `exit`; use Ctrl+C to terminate it loop { @@ -39,11 +25,4 @@ fn main() -> ! { } } -// Reads the bits in the UART0 register and prints `enabled` or `disabled` -fn uarte_enabled(uarte: &UARTE0) { - if uarte.enable.read().bits() != 0 { - defmt::println!("Uarte0 is enabled"); - } else { - defmt::println!("Uarte0 is disabled"); - } -} +// The helper function goes here... diff --git a/down-the-stack/apps/src/bin/uarte_enable_solution.rs b/down-the-stack/apps/src/bin/uarte_enable_solution.rs new file mode 100644 index 0000000..ef29275 --- /dev/null +++ b/down-the-stack/apps/src/bin/uarte_enable_solution.rs @@ -0,0 +1,48 @@ +#![no_main] +#![no_std] + +use cortex_m::asm; +use cortex_m_rt::entry; +use dk_pac::UARTE0; + +// this imports `down-the-stack/apps/lib.rs` to retrieve our global logger + panicking-behavior +use apps as _; +use defmt; +use defmt_rtt as _; // global logger + +#[entry] +fn main() -> ! { + // to enable more verbose logs, go to your `Cargo.toml` and set defmt logging levels + // to `defmt-trace` by changing the `default = []` entry in `[features]` + + // takes ownership of the nRF52840-DK peripherals + let periph = dk_pac::Peripherals::take().unwrap(); + let uarte = periph.UARTE0; + + is_uarte_enabled(&uarte); + + // enable the UART0 peripheral the safe way + uarte.enable.write(|w| w.enable().enabled()); + + is_uarte_enabled(&uarte); + + // disable the UART0 peripheral by writing 0 directly into the register -- the unsafe way + unsafe { + uarte.enable.write(|w| w.bits(0x00 as u32)); + } + + is_uarte_enabled(&uarte); + + // this program does not `exit`; use Ctrl+C to terminate it + loop { + asm::nop(); + } +} + +fn is_uarte_enabled(uarte: &UARTE0) { + if uarte.enable.read().enable().is_enabled() { + defmt::println!("Uarte0 is enabled"); + } else { + defmt::println!("Uarte0 is disabled"); + } +} \ No newline at end of file