From 8140c7bba9e6e2b17e929247bd11c580f6210d30 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Wed, 17 Jun 2020 18:51:52 +0200 Subject: [PATCH] WIP: show recv_timeout API TODO update text --- beginner/apps/Cargo.lock | 4 ++-- beginner/apps/src/bin/radio-recv.rs | 23 ++++++++++++++++------- boards/dk/src/lib.rs | 17 +++++++++++++++-- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/beginner/apps/Cargo.lock b/beginner/apps/Cargo.lock index 70b5d4a..92c0383 100644 --- a/beginner/apps/Cargo.lock +++ b/beginner/apps/Cargo.lock @@ -122,7 +122,7 @@ checksum = "b1411551beb3c11dedfb0a90a0fa256b47d28b9ec2cdff34c25a2fa59e45dbdc" [[package]] name = "nrf-hal-common" version = "0.10.0" -source = "git+https://github.com/japaric/nrf-hal?branch=radio#d624e80e5724e4709081ed65abaf63271fe1eca7" +source = "git+https://github.com/japaric/nrf-hal?branch=radio#7076639891fd2493e1c61a7aca71944b48ae0458" dependencies = [ "cast", "cortex-m", @@ -137,7 +137,7 @@ dependencies = [ [[package]] name = "nrf52840-hal" version = "0.10.0" -source = "git+https://github.com/japaric/nrf-hal?branch=radio#d624e80e5724e4709081ed65abaf63271fe1eca7" +source = "git+https://github.com/japaric/nrf-hal?branch=radio#7076639891fd2493e1c61a7aca71944b48ae0458" dependencies = [ "cast", "cortex-m", diff --git a/beginner/apps/src/bin/radio-recv.rs b/beginner/apps/src/bin/radio-recv.rs index ada950c..e9eb4c9 100644 --- a/beginner/apps/src/bin/radio-recv.rs +++ b/beginner/apps/src/bin/radio-recv.rs @@ -4,9 +4,8 @@ use core::str; -use cortex_m::asm; use cortex_m_rt::entry; -use dk::ieee802154::Packet; +use dk::ieee802154::{Error, Packet}; use panic_log as _; // the panicking behavior #[entry] @@ -14,17 +13,27 @@ fn main() -> ! { // initializes the peripherals let board = dk::init().unwrap(); let mut radio = board.radio; + let mut timer = board.timer; let mut packet = Packet::new(); let msg = b"olleh"; packet.copy_from_slice(msg); radio.send(&packet); log::info!("sent: {:?}", msg); - let crc = radio.recv(&mut packet); - let s = str::from_utf8(&*packet).expect("response is not valid UTF-8"); - log::info!("received: {} (CRC={:?})", s, crc); + let timeout = 1_000; + let res = radio.recv_timeout(&mut packet, &mut timer, timeout); - loop { - asm::bkpt(); + match res { + Ok(crc) => { + log::info!( + "received: {} (CRC={})", + str::from_utf8(&*packet).expect("response is not valid UTF-8"), + crc + ); + } + Err(Error::Crc(crc)) => log::error!("invalid CRC: {:06x}", crc), + Err(Error::Timeout) => log::error!("no response within {} ms", timeout), } + + dk::exit() } diff --git a/boards/dk/src/lib.rs b/boards/dk/src/lib.rs index a58f511..901633a 100644 --- a/boards/dk/src/lib.rs +++ b/boards/dk/src/lib.rs @@ -5,6 +5,7 @@ #![no_std] use core::{ + ops, sync::atomic::{AtomicU32, Ordering}, time::Duration, }; @@ -118,7 +119,7 @@ impl Led { } } -/// A timer for blocking delay +/// A timer for creating blocking delays pub struct Timer { inner: hal::Timer, } @@ -157,7 +158,19 @@ impl Timer { } } -// add Instant API +impl ops::Deref for Timer { + type Target = hal::Timer; + + fn deref(&self) -> &Self::Target { + &self.inner + } +} + +impl ops::DerefMut for Timer { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.inner + } +} /// Initializes the board ///