aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2020-11-01 17:17:24 +0100
committerDario Nieuwenhuis <[email protected]>2020-11-01 17:17:24 +0100
commit5e8608c7a5b1e4e92f1fb650ea0ef47e5a6df563 (patch)
treef731a0329d92d8fae4168957f8e90716b58ad00b /examples
parent2c13e251849fd3e3cf06e2d5363c311ac880c77d (diff)
Make defmt optional with new `anyfmt` crate
Diffstat (limited to 'examples')
-rw-r--r--examples/Cargo.toml13
-rw-r--r--examples/src/bin/gpiote.rs22
-rw-r--r--examples/src/bin/multiprio.rs16
-rw-r--r--examples/src/bin/qspi.rs7
-rw-r--r--examples/src/bin/rtc_async.rs6
-rw-r--r--examples/src/bin/rtc_raw.rs2
-rw-r--r--examples/src/bin/uart.rs12
-rw-r--r--examples/src/example_common.rs51
8 files changed, 40 insertions, 89 deletions
diff --git a/examples/Cargo.toml b/examples/Cargo.toml
index 8eb7a5ff7..719f73269 100644
--- a/examples/Cargo.toml
+++ b/examples/Cargo.toml
@@ -17,14 +17,17 @@ defmt-error = []
17 17
18 18
19[dependencies] 19[dependencies]
20embassy = { version = "0.1.0", path = "../embassy", features = ["defmt"] }
21embassy-nrf = { version = "0.1.0", path = "../embassy-nrf", features = ["defmt", "defmt-trace", "52840"] }
22anyfmt = { version = "0.1.0", path = "../anyfmt", features = ["defmt"] }
23
24defmt = "0.1.0"
25defmt-rtt = "0.1.0"
26
20cortex-m = { version = "0.6.3" } 27cortex-m = { version = "0.6.3" }
21cortex-m-rt = "0.6.12" 28cortex-m-rt = "0.6.12"
22defmt = "0.1.0"
23embedded-hal = { version = "0.2.4" } 29embedded-hal = { version = "0.2.4" }
24defmt-rtt = "0.1.0"
25panic-probe = "0.1.0" 30panic-probe = "0.1.0"
26nrf52840-hal = { version = "0.11.0" } 31nrf52840-hal = { version = "0.11.0" }
27embassy = { version = "0.1.0", path = "../embassy" }
28embassy-nrf = { version = "0.1.0", path = "../embassy-nrf", features = ["defmt-trace", "52840"] }
29futures = { version = "0.3.7", default-features = false, features = ["async-await"] } 32futures = { version = "0.3.7", default-features = false, features = ["async-await"] }
30cortex-m-rtic = { git = "https://github.com/rtic-rs/cortex-m-rtic", branch = "master"} \ No newline at end of file 33cortex-m-rtic = { git = "https://github.com/rtic-rs/cortex-m-rtic", branch = "master"}
diff --git a/examples/src/bin/gpiote.rs b/examples/src/bin/gpiote.rs
index 16b4f06d7..d8394155d 100644
--- a/examples/src/bin/gpiote.rs
+++ b/examples/src/bin/gpiote.rs
@@ -15,7 +15,7 @@ use embassy_nrf::gpiote;
15 15
16#[task] 16#[task]
17async fn run() { 17async fn run() {
18 let p = embassy_nrf::pac::Peripherals::take().dewrap(); 18 let p = unwrap!(embassy_nrf::pac::Peripherals::take());
19 let port0 = gpio::p0::Parts::new(p.P0); 19 let port0 = gpio::p0::Parts::new(p.P0);
20 20
21 let g = gpiote::Gpiote::new(p.GPIOTE); 21 let g = gpiote::Gpiote::new(p.GPIOTE);
@@ -24,9 +24,7 @@ async fn run() {
24 24
25 let pin1 = port0.p0_11.into_pullup_input().degrade(); 25 let pin1 = port0.p0_11.into_pullup_input().degrade();
26 let button1 = async { 26 let button1 = async {
27 let ch = g 27 let ch = unwrap!(g.new_input_channel(pin1, gpiote::EventPolarity::HiToLo));
28 .new_input_channel(pin1, gpiote::EventPolarity::HiToLo)
29 .dewrap();
30 28
31 loop { 29 loop {
32 ch.wait().await; 30 ch.wait().await;
@@ -36,9 +34,7 @@ async fn run() {
36 34
37 let pin2 = port0.p0_12.into_pullup_input().degrade(); 35 let pin2 = port0.p0_12.into_pullup_input().degrade();
38 let button2 = async { 36 let button2 = async {
39 let ch = g 37 let ch = unwrap!(g.new_input_channel(pin2, gpiote::EventPolarity::LoToHi));
40 .new_input_channel(pin2, gpiote::EventPolarity::LoToHi)
41 .dewrap();
42 38
43 loop { 39 loop {
44 ch.wait().await; 40 ch.wait().await;
@@ -48,9 +44,7 @@ async fn run() {
48 44
49 let pin3 = port0.p0_24.into_pullup_input().degrade(); 45 let pin3 = port0.p0_24.into_pullup_input().degrade();
50 let button3 = async { 46 let button3 = async {
51 let ch = g 47 let ch = unwrap!(g.new_input_channel(pin3, gpiote::EventPolarity::Toggle));
52 .new_input_channel(pin3, gpiote::EventPolarity::Toggle)
53 .dewrap();
54 48
55 loop { 49 loop {
56 ch.wait().await; 50 ch.wait().await;
@@ -60,9 +54,7 @@ async fn run() {
60 54
61 let pin4 = port0.p0_25.into_pullup_input().degrade(); 55 let pin4 = port0.p0_25.into_pullup_input().degrade();
62 let button4 = async { 56 let button4 = async {
63 let ch = g 57 let ch = unwrap!(g.new_input_channel(pin4, gpiote::EventPolarity::Toggle));
64 .new_input_channel(pin4, gpiote::EventPolarity::Toggle)
65 .dewrap();
66 58
67 loop { 59 loop {
68 ch.wait().await; 60 ch.wait().await;
@@ -79,8 +71,8 @@ static EXECUTOR: Forever<Executor> = Forever::new();
79fn main() -> ! { 71fn main() -> ! {
80 info!("Hello World!"); 72 info!("Hello World!");
81 73
82 let executor = EXECUTOR.put(Executor::new(cortex_m::asm::wfi)); 74 let executor = EXECUTOR.put(Executor::new(cortex_m::asm::sev));
83 executor.spawn(run()).dewrap(); 75 unwrap!(executor.spawn(run()));
84 76
85 loop { 77 loop {
86 executor.run(); 78 executor.run();
diff --git a/examples/src/bin/multiprio.rs b/examples/src/bin/multiprio.rs
index dbca491e0..e73747ac6 100644
--- a/examples/src/bin/multiprio.rs
+++ b/examples/src/bin/multiprio.rs
@@ -120,7 +120,7 @@ static EXECUTOR_HIGH: Forever<TimerExecutor<rtc::Alarm<pac::RTC1>>> = Forever::n
120fn main() -> ! { 120fn main() -> ! {
121 info!("Hello World!"); 121 info!("Hello World!");
122 122
123 let p = embassy_nrf::pac::Peripherals::take().dewrap(); 123 let p = unwrap!(embassy_nrf::pac::Peripherals::take());
124 124
125 clocks::Clocks::new(p.CLOCK) 125 clocks::Clocks::new(p.CLOCK)
126 .enable_ext_hfosc() 126 .enable_ext_hfosc()
@@ -132,17 +132,21 @@ fn main() -> ! {
132 unsafe { embassy::time::set_clock(rtc) }; 132 unsafe { embassy::time::set_clock(rtc) };
133 133
134 let executor_low = EXECUTOR_LOW.put(TimerExecutor::new(rtc.alarm0(), cortex_m::asm::sev)); 134 let executor_low = EXECUTOR_LOW.put(TimerExecutor::new(rtc.alarm0(), cortex_m::asm::sev));
135 let executor_med = EXECUTOR_MED.put(TimerExecutor::new(rtc.alarm1(), cortex_m::asm::sev)); 135 let executor_med = EXECUTOR_MED.put(TimerExecutor::new(rtc.alarm1(), || {
136 let executor_high = EXECUTOR_HIGH.put(TimerExecutor::new(rtc.alarm2(), cortex_m::asm::sev)); 136 interrupt::pend(interrupt::SWI0_EGU0)
137 }));
138 let executor_high = EXECUTOR_HIGH.put(TimerExecutor::new(rtc.alarm2(), || {
139 interrupt::pend(interrupt::SWI1_EGU1)
140 }));
137 141
138 interrupt::set_priority(interrupt::SWI0_EGU0, interrupt::Priority::Level7); 142 interrupt::set_priority(interrupt::SWI0_EGU0, interrupt::Priority::Level7);
139 interrupt::set_priority(interrupt::SWI1_EGU1, interrupt::Priority::Level6); 143 interrupt::set_priority(interrupt::SWI1_EGU1, interrupt::Priority::Level6);
140 interrupt::enable(interrupt::SWI0_EGU0); 144 interrupt::enable(interrupt::SWI0_EGU0);
141 interrupt::enable(interrupt::SWI1_EGU1); 145 interrupt::enable(interrupt::SWI1_EGU1);
142 146
143 executor_low.spawn(run_low()).dewrap(); 147 unwrap!(executor_low.spawn(run_low()));
144 executor_med.spawn(run_med()).dewrap(); 148 unwrap!(executor_med.spawn(run_med()));
145 executor_high.spawn(run_high()).dewrap(); 149 unwrap!(executor_high.spawn(run_high()));
146 150
147 loop { 151 loop {
148 executor_low.run(); 152 executor_low.run();
diff --git a/examples/src/bin/qspi.rs b/examples/src/bin/qspi.rs
index caabac8ba..a7f668ca7 100644
--- a/examples/src/bin/qspi.rs
+++ b/examples/src/bin/qspi.rs
@@ -6,6 +6,7 @@
6mod example_common; 6mod example_common;
7use example_common::*; 7use example_common::*;
8 8
9use anyfmt::panic;
9use cortex_m_rt::entry; 10use cortex_m_rt::entry;
10use nrf52840_hal::gpio; 11use nrf52840_hal::gpio;
11 12
@@ -23,7 +24,7 @@ struct AlignedBuf([u8; 4096]);
23 24
24#[task] 25#[task]
25async fn run() { 26async fn run() {
26 let p = embassy_nrf::pac::Peripherals::take().dewrap(); 27 let p = unwrap!(embassy_nrf::pac::Peripherals::take());
27 28
28 let port0 = gpio::p0::Parts::new(p.P0); 29 let port0 = gpio::p0::Parts::new(p.P0);
29 30
@@ -121,8 +122,8 @@ static EXECUTOR: Forever<Executor> = Forever::new();
121fn main() -> ! { 122fn main() -> ! {
122 info!("Hello World!"); 123 info!("Hello World!");
123 124
124 let executor = EXECUTOR.put(Executor::new(cortex_m::asm::wfi)); 125 let executor = EXECUTOR.put(Executor::new(cortex_m::asm::sev));
125 executor.spawn(run()).dewrap(); 126 unwrap!(executor.spawn(run()));
126 127
127 loop { 128 loop {
128 executor.run(); 129 executor.run();
diff --git a/examples/src/bin/rtc_async.rs b/examples/src/bin/rtc_async.rs
index 5126a2cc8..b4ee736b7 100644
--- a/examples/src/bin/rtc_async.rs
+++ b/examples/src/bin/rtc_async.rs
@@ -39,7 +39,7 @@ static EXECUTOR: Forever<TimerExecutor<rtc::Alarm<pac::RTC1>>> = Forever::new();
39fn main() -> ! { 39fn main() -> ! {
40 info!("Hello World!"); 40 info!("Hello World!");
41 41
42 let p = embassy_nrf::pac::Peripherals::take().dewrap(); 42 let p = unwrap!(embassy_nrf::pac::Peripherals::take());
43 43
44 clocks::Clocks::new(p.CLOCK) 44 clocks::Clocks::new(p.CLOCK)
45 .enable_ext_hfosc() 45 .enable_ext_hfosc()
@@ -53,8 +53,8 @@ fn main() -> ! {
53 53
54 let executor = EXECUTOR.put(TimerExecutor::new(rtc.alarm0(), cortex_m::asm::sev)); 54 let executor = EXECUTOR.put(TimerExecutor::new(rtc.alarm0(), cortex_m::asm::sev));
55 55
56 executor.spawn(run1()).dewrap(); 56 unwrap!(executor.spawn(run1()));
57 executor.spawn(run2()).dewrap(); 57 unwrap!(executor.spawn(run2()));
58 58
59 loop { 59 loop {
60 executor.run(); 60 executor.run();
diff --git a/examples/src/bin/rtc_raw.rs b/examples/src/bin/rtc_raw.rs
index 4453ecae1..ad5fab246 100644
--- a/examples/src/bin/rtc_raw.rs
+++ b/examples/src/bin/rtc_raw.rs
@@ -18,7 +18,7 @@ static mut RTC: MaybeUninit<rtc::RTC<embassy_nrf::pac::RTC1>> = MaybeUninit::uni
18fn main() -> ! { 18fn main() -> ! {
19 info!("Hello World!"); 19 info!("Hello World!");
20 20
21 let p = embassy_nrf::pac::Peripherals::take().dewrap(); 21 let p = unwrap!(embassy_nrf::pac::Peripherals::take());
22 22
23 clocks::Clocks::new(p.CLOCK) 23 clocks::Clocks::new(p.CLOCK)
24 .enable_ext_hfosc() 24 .enable_ext_hfosc()
diff --git a/examples/src/bin/uart.rs b/examples/src/bin/uart.rs
index eeaf5fee6..553bbb356 100644
--- a/examples/src/bin/uart.rs
+++ b/examples/src/bin/uart.rs
@@ -17,7 +17,7 @@ use embassy_nrf::uarte;
17 17
18#[task] 18#[task]
19async fn run() { 19async fn run() {
20 let p = embassy_nrf::pac::Peripherals::take().dewrap(); 20 let p = unwrap!(embassy_nrf::pac::Peripherals::take());
21 21
22 let port0 = gpio::p0::Parts::new(p.P0); 22 let port0 = gpio::p0::Parts::new(p.P0);
23 23
@@ -41,14 +41,14 @@ async fn run() {
41 41
42 info!("uarte initialized!"); 42 info!("uarte initialized!");
43 43
44 u.write_all(b"Hello!\r\n").await.dewrap(); 44 unwrap!(u.write_all(b"Hello!\r\n").await);
45 info!("wrote hello in uart!"); 45 info!("wrote hello in uart!");
46 46
47 // Simple demo, reading 8-char chunks and echoing them back reversed. 47 // Simple demo, reading 8-char chunks and echoing them back reversed.
48 loop { 48 loop {
49 info!("reading..."); 49 info!("reading...");
50 let mut buf = [0u8; 8]; 50 let mut buf = [0u8; 8];
51 u.read_exact(&mut buf).await.dewrap(); 51 unwrap!(u.read_exact(&mut buf).await);
52 info!("read done, got {:[u8]}", buf); 52 info!("read done, got {:[u8]}", buf);
53 53
54 // Reverse buf 54 // Reverse buf
@@ -59,7 +59,7 @@ async fn run() {
59 } 59 }
60 60
61 info!("writing..."); 61 info!("writing...");
62 u.write_all(&buf).await.dewrap(); 62 unwrap!(u.write_all(&buf).await);
63 info!("write done"); 63 info!("write done");
64 } 64 }
65} 65}
@@ -70,8 +70,8 @@ static EXECUTOR: Forever<Executor> = Forever::new();
70fn main() -> ! { 70fn main() -> ! {
71 info!("Hello World!"); 71 info!("Hello World!");
72 72
73 let executor = EXECUTOR.put(Executor::new(cortex_m::asm::wfi)); 73 let executor = EXECUTOR.put(Executor::new(cortex_m::asm::sev));
74 executor.spawn(run()).dewrap(); 74 unwrap!(executor.spawn(run()));
75 75
76 loop { 76 loop {
77 executor.run(); 77 executor.run();
diff --git a/examples/src/example_common.rs b/examples/src/example_common.rs
index 65bfe6bb1..1a12fa69a 100644
--- a/examples/src/example_common.rs
+++ b/examples/src/example_common.rs
@@ -4,7 +4,7 @@ use defmt_rtt as _; // global logger
4use nrf52840_hal as _; 4use nrf52840_hal as _;
5use panic_probe as _; 5use panic_probe as _;
6 6
7pub use defmt::{info, intern}; 7pub use anyfmt::*;
8 8
9use core::sync::atomic::{AtomicUsize, Ordering}; 9use core::sync::atomic::{AtomicUsize, Ordering};
10 10
@@ -16,52 +16,3 @@ fn timestamp() -> u64 {
16 COUNT.store(n + 1, Ordering::Relaxed); 16 COUNT.store(n + 1, Ordering::Relaxed);
17 n as u64 17 n as u64
18} 18}
19
20macro_rules! depanic {
21 ($( $i:expr ),*) => {
22 {
23 defmt::error!($( $i ),*);
24 panic!();
25 }
26 }
27}
28
29pub trait Dewrap<T> {
30 /// dewrap = defmt unwrap
31 fn dewrap(self) -> T;
32
33 /// dexpect = defmt expect
34 fn dexpect<M: defmt::Format>(self, msg: M) -> T;
35}
36
37impl<T> Dewrap<T> for Option<T> {
38 fn dewrap(self) -> T {
39 match self {
40 Some(t) => t,
41 None => depanic!("Dewrap failed: enum is none"),
42 }
43 }
44
45 fn dexpect<M: defmt::Format>(self, msg: M) -> T {
46 match self {
47 Some(t) => t,
48 None => depanic!("Unexpected None: {:?}", msg),
49 }
50 }
51}
52
53impl<T, E: defmt::Format> Dewrap<T> for Result<T, E> {
54 fn dewrap(self) -> T {
55 match self {
56 Ok(t) => t,
57 Err(e) => depanic!("Dewrap failed: {:?}", e),
58 }
59 }
60
61 fn dexpect<M: defmt::Format>(self, msg: M) -> T {
62 match self {
63 Ok(t) => t,
64 Err(e) => depanic!("Unexpected error: {:?}: {:?}", msg, e),
65 }
66 }
67}