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

54 lines
1.4 KiB
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::{Channel, Error, Packet};
2021-04-12 09:51:44 +00:00
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
use apps as _;
2020-06-09 09:52:27 +00:00
const TEN_MS: u32 = 10_000;
2020-06-09 09:52:27 +00:00
#[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
// NOTE if you ran `change-channel` then you may need to update the channel here
radio.set_channel(Channel::_20); // <- must match the Dongle's listening channel
2020-06-09 09:52:27 +00:00
let mut packet = Packet::new();
let msg = b"olleh";
packet.copy_from_slice(msg);
2022-01-07 16:24:21 +00:00
defmt::println!(
"sending: {}",
str::from_utf8(msg).expect("message is not valid UTF-8")
);
2021-03-16 12:26:51 +00:00
radio.send(&mut packet);
// TODO try uncommenting this line
// timer.delay(1_000);
let res = radio.recv_timeout(&mut packet, &mut timer, TEN_MS);
2020-06-09 09:52:27 +00:00
match res {
Ok(crc) => {
2022-01-07 16:24:21 +00:00
defmt::println!(
2021-04-12 09:51:44 +00:00
"received: {} (CRC = {:X})",
// ^^ print as uppercase hexadecimal
str::from_utf8(&*packet).expect("response is not valid UTF-8"),
crc
);
}
2021-06-04 14:57:49 +00:00
Err(Error::Crc(crc)) => defmt::error!("invalid CRC: {:X}", crc),
2021-04-12 09:51:44 +00:00
Err(Error::Timeout) => defmt::error!("no response within {} ms", TEN_MS / 1_000),
2020-06-09 09:52:27 +00:00
}
dk::exit()
2020-06-09 09:52:27 +00:00
}