mirror of
https://github.com/ferrous-systems/embedded-trainings-2020.git
synced 2025-02-13 16:05:13 +00:00
unsafe and safe enable/disable
This commit is contained in:
parent
660647219a
commit
8518686e9a
1 changed files with 25 additions and 14 deletions
|
@ -3,36 +3,47 @@
|
||||||
|
|
||||||
use cortex_m::asm;
|
use cortex_m::asm;
|
||||||
use cortex_m_rt::entry;
|
use cortex_m_rt::entry;
|
||||||
use dk_pac;
|
use dk_pac::UARTE0;
|
||||||
|
|
||||||
// this imports `down-the-stack/apps/lib.rs` to retrieve our global logger + panicking-behavior
|
// this imports `down-the-stack/apps/lib.rs` to retrieve our global logger + panicking-behavior
|
||||||
use apps as _;
|
use apps as _;
|
||||||
use defmt;
|
use defmt;
|
||||||
use defmt_rtt as _; // global logger
|
use defmt_rtt as _; // global logger
|
||||||
|
|
||||||
|
|
||||||
#[entry]
|
#[entry]
|
||||||
fn main() -> ! {
|
fn main() -> ! {
|
||||||
// to enable more verbose logs, go to your `Cargo.toml` and set defmt logging levels
|
// 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]`
|
// to `defmt-trace` by changing the `default = []` entry in `[features]`
|
||||||
unsafe {
|
|
||||||
let periph = dk_pac::Peripherals::steal();
|
// takes ownership of the nRF52840-DK peripherals
|
||||||
|
let periph = dk_pac::Peripherals::take().unwrap();
|
||||||
let uarte = periph.UARTE0;
|
let uarte = periph.UARTE0;
|
||||||
|
|
||||||
|
uarte_enabled(&uarte);
|
||||||
|
|
||||||
|
// enable the UART0 peripheral the safe way
|
||||||
uarte.enable.write(|w| w.enable().enabled());
|
uarte.enable.write(|w| w.enable().enabled());
|
||||||
|
|
||||||
if uarte.enable.read().bits() != 0 {
|
uarte_enabled(&uarte);
|
||||||
defmt::println!("Uarte0 is enabled");
|
|
||||||
|
|
||||||
} else {
|
|
||||||
defmt::println!("Uarte0 is disabled");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
// this program does not `exit`; use Ctrl+C to terminate it
|
// this program does not `exit`; use Ctrl+C to terminate it
|
||||||
loop {
|
loop {
|
||||||
asm::nop();
|
asm::nop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue