embedded-trainings-2020/beginner/apps/src/bin/radio-recv.rs

40 lines
991 B
Rust
Raw Normal View History

2020-06-09 09:52:27 +00:00
#![deny(unused_must_use)]
#![no_main]
#![no_std]
use core::str;
use cortex_m_rt::entry;
use dk::ieee802154::{Error, Packet};
2020-06-09 09:52:27 +00:00
use panic_log as _; // the panicking behavior
#[entry]
fn main() -> ! {
// initializes the peripherals
let board = dk::init().unwrap();
let mut radio = board.radio;
let mut timer = board.timer;
2020-06-09 09:52:27 +00:00
let mut packet = Packet::new();
let msg = b"olleh";
packet.copy_from_slice(msg);
radio.send(&packet);
log::info!("sent: {:?}", msg);
let timeout = 1_000;
let res = radio.recv_timeout(&mut packet, &mut timer, timeout);
2020-06-09 09:52:27 +00:00
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),
2020-06-09 09:52:27 +00:00
}
dk::exit()
2020-06-09 09:52:27 +00:00
}