Skip to content

Commit

Permalink
802.15.4 raw stack driver example usage
Browse files Browse the repository at this point in the history
  • Loading branch information
wprzytula committed Jun 23, 2024
1 parent b9d3963 commit 4c7282a
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions examples/ieee802154.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//! An example showing use of IEEE 802.15.4 networking.
#![no_main]
#![no_std]
use libtock::ieee802154::{
Ieee802154, RxBufferAlternatingOperator, RxOperator as _, RxRingBuffer, RxSingleBufferOperator,
};
use libtock::runtime::{set_main, stack_size};

set_main! {main}
stack_size! {0x400}

fn main() {
// Configure the radio
let pan: u16 = 0xcafe;
let addr_short: u16 = 0xdead;
let addr_long: u64 = 0xdead_dad;
let tx_power: i8 = -0x42;
let channel: u8 = 0xff;

Ieee802154::set_pan(pan);
Ieee802154::set_address_short(addr_short);
Ieee802154::set_address_long(addr_long);
Ieee802154::set_tx_power(tx_power).unwrap();
Ieee802154::set_channel(channel).unwrap();

// Don't forget to commit the config!
Ieee802154::commit_config();

// Turn the radio on
Ieee802154::radio_on().unwrap();
assert!(Ieee802154::is_on());

// Transmit a frame
Ieee802154::transmit_frame(b"foobar").unwrap();

// Showcase receiving to a single buffer - there is a risk of losing some frames.
// See [RxSingleBufferOperator] and [RxBufferAlternatingOperator] docs for more details.
rx_single_buffer();

// Showcase receiving to a pair of alternating buffers - there is no risk of losing frames.
rx_alternating_buffers();
}

fn rx_single_buffer() {
let mut buf = RxRingBuffer::<2>::new();
let mut operator = RxSingleBufferOperator::new(&mut buf);

let frame1 = operator.receive_frame().unwrap();
// Access frame1 data here:
let _body_len = frame1.payload_len;
let _first_body_byte = frame1.body[0];

let _frame2 = operator.receive_frame().unwrap();
// Access frame2 data here
}

fn rx_alternating_buffers() {
let mut buf1 = RxRingBuffer::<2>::new();
let mut buf2 = RxRingBuffer::<2>::new();
let mut operator = RxBufferAlternatingOperator::new(&mut buf1, &mut buf2).unwrap();

let frame1 = operator.receive_frame().unwrap();
// Access frame1 data here:
let _body_len = frame1.payload_len;
let _first_body_byte = frame1.body[0];

let _frame2 = operator.receive_frame().unwrap();
// Access frame2 data here
}

0 comments on commit 4c7282a

Please sign in to comment.