embedded-trainings-2020/down-the-stack/apps/src/bin/uarte_enable.rs

50 lines
1.3 KiB
Rust
Raw Normal View History

2023-03-10 12:49:09 +00:00
#![no_main]
#![no_std]
use cortex_m::asm;
use cortex_m_rt::entry;
2023-03-10 14:44:41 +00:00
use dk_pac::UARTE0;
2023-03-10 12:49:09 +00:00
// 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]`
2023-03-10 14:44:41 +00:00
// takes ownership of the nRF52840-DK peripherals
let periph = dk_pac::Peripherals::take().unwrap();
2023-03-10 12:49:09 +00:00
let uarte = periph.UARTE0;
2023-03-10 14:44:41 +00:00
uarte_enabled(&uarte);
// enable the UART0 peripheral the safe way
2023-03-10 12:49:09 +00:00
uarte.enable.write(|w| w.enable().enabled());
2023-03-10 14:44:41 +00:00
uarte_enabled(&uarte);
2023-03-10 12:49:09 +00:00
2023-03-10 14:44:41 +00:00
// disable the UART0 peripheral by writing 0 directly into the register -- the unsafe way
unsafe {
uarte.enable.write(|w| w.bits(0x00 as u32));
2023-03-10 12:49:09 +00:00
}
2023-03-10 14:44:41 +00:00
uarte_enabled(&uarte);
2023-03-10 12:49:09 +00:00
// this program does not `exit`; use Ctrl+C to terminate it
loop {
asm::nop();
}
2023-03-10 14:44:41 +00:00
}
// 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");
}
}