-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
802.15.4 raw stack driver example usage
- Loading branch information
Showing
1 changed file
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |