Skip to content

Commit 0766d8c

Browse files
authored
Merge pull request #587 from tyler-potyondy/ieee802154-fixes
IEEE 802.15.4 Examples and API Fixes
2 parents e2c0b7c + 3c52dbd commit 0766d8c

File tree

8 files changed

+209
-71
lines changed

8 files changed

+209
-71
lines changed

apis/net/ieee802154/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ impl<S: Syscalls, C: Config> Ieee802154<S, C> {
169169

170170
// Transmission
171171
impl<S: Syscalls, C: Config> Ieee802154<S, C> {
172-
pub fn transmit_frame(frame: &[u8]) -> Result<(), ErrorCode> {
172+
/// Transmit a frame using the IEEE 802.15.4 Phy Driver.
173+
pub fn transmit_frame_raw(frame: &[u8]) -> Result<(), ErrorCode> {
173174
let called: Cell<Option<(u32,)>> = Cell::new(None);
174175
share::scope::<
175176
(
@@ -187,7 +188,7 @@ impl<S: Syscalls, C: Config> Ieee802154<S, C> {
187188
subscribe, &called,
188189
)?;
189190

190-
S::command(DRIVER_NUM, command::TRANSMIT, 0, 0).to_result::<(), ErrorCode>()?;
191+
S::command(DRIVER_NUM, command::TRANSMIT_RAW, 0, 0).to_result::<(), ErrorCode>()?;
191192

192193
loop {
193194
S::yield_wait();
@@ -253,7 +254,7 @@ mod command {
253254
pub const GET_PAN: u32 = 10;
254255
pub const GET_CHAN: u32 = 11;
255256
pub const GET_TX_PWR: u32 = 12;
256-
pub const TRANSMIT: u32 = 27;
257+
pub const TRANSMIT_RAW: u32 = 27;
257258
pub const SET_LONG_ADDR: u32 = 28;
258259
pub const GET_LONG_ADDR: u32 = 29;
259260
pub const TURN_ON: u32 = 30;

apis/net/ieee802154/src/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ fn transmit_frame() {
111111
let driver = fake::Ieee802154Phy::new();
112112
kernel.add_driver(&driver);
113113

114-
Ieee802154::transmit_frame(b"foo").unwrap();
115-
Ieee802154::transmit_frame(b"bar").unwrap();
114+
Ieee802154::transmit_frame_raw(b"foo").unwrap();
115+
Ieee802154::transmit_frame_raw(b"bar").unwrap();
116116
assert_eq!(
117117
driver.take_transmitted_frames(),
118118
&[&b"foo"[..], &b"bar"[..]],
Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
//! An example showing use of IEEE 802.15.4 networking.
2+
//!
3+
//! The kernel contains a standard and phy 15.4 driver. This example
4+
//! expects the kernel to be configured with the phy 15.4 driver to
5+
//! allow direct access to the radio and the ability to send "raw"
6+
//! frames. An example board file using this driver is provided at
7+
//! `boards/tutorials/nrf52840dk-thread-tutorial`.
8+
//!
9+
//! "No Support" Errors for setting the channel/tx power are a telltale
10+
//! sign that the kernel is not configured with the phy 15.4 driver.
211
312
#![no_main]
413
#![no_std]
14+
use core::fmt::Write;
515
use libtock::console::Console;
616
use libtock::ieee802154::{Ieee802154, RxOperator as _, RxRingBuffer, RxSingleBufferOperator};
717
use libtock::runtime::{set_main, stack_size};
@@ -17,21 +27,43 @@ fn main() {
1727
let tx_power: i8 = -3;
1828
let channel: u8 = 11;
1929

30+
writeln!(Console::writer(), "Configuring IEEE 802.15.4 radio...\n").unwrap();
31+
2032
Ieee802154::set_pan(pan);
33+
writeln!(Console::writer(), "Set PAN to {:#06x}\n", pan).unwrap();
34+
2135
Ieee802154::set_address_short(addr_short);
36+
writeln!(
37+
Console::writer(),
38+
"Set short address to {:#06x}\n",
39+
addr_short
40+
)
41+
.unwrap();
42+
2243
Ieee802154::set_address_long(addr_long);
44+
writeln!(
45+
Console::writer(),
46+
"Set long address to {:#018x}\n",
47+
addr_long
48+
)
49+
.unwrap();
50+
2351
Ieee802154::set_tx_power(tx_power).unwrap();
52+
writeln!(Console::writer(), "Set TX power to {}\n", tx_power).unwrap();
53+
2454
Ieee802154::set_channel(channel).unwrap();
55+
writeln!(Console::writer(), "Set channel to {}\n", channel).unwrap();
2556

2657
// Don't forget to commit the config!
2758
Ieee802154::commit_config();
59+
writeln!(Console::writer(), "Committed radio configuration!\n").unwrap();
2860

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

3365
// Transmit a frame
34-
Ieee802154::transmit_frame(b"foobar").unwrap();
66+
Ieee802154::transmit_frame_raw(b"foobar").unwrap();
3567

3668
Console::write(b"Transmitted frame!\n").unwrap();
3769

examples/ieee802154_rx.rs

Lines changed: 0 additions & 50 deletions
This file was deleted.

examples/ieee802154_rx_raw.rs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//! An example showing use of IEEE 802.15.4 networking.
2+
//! It infinitely received a frame and prints its content to Console.
3+
//!
4+
//! The kernel contains a standard and phy 15.4 driver. This example
5+
//! expects the kernel to be configured with the phy 15.4 driver to
6+
//! allow direct access to the radio and the ability to send "raw"
7+
//! frames. An example board file using this driver is provided at
8+
//! `boards/tutorials/nrf52840dk-thread-tutorial`.
9+
//!
10+
//! "No Support" Errors for setting the channel/tx power are a telltale
11+
//! sign that the kernel is not configured with the phy 15.4 driver.
12+
13+
#![no_main]
14+
#![no_std]
15+
use core::fmt::Write;
16+
use libtock::console::Console;
17+
use libtock::ieee802154::{Ieee802154, RxOperator as _, RxRingBuffer, RxSingleBufferOperator};
18+
use libtock::runtime::{set_main, stack_size};
19+
20+
set_main! {main}
21+
stack_size! {0x600}
22+
23+
fn main() {
24+
// Configure the radio
25+
let pan: u16 = 0xcafe;
26+
let addr_short: u16 = 0xdead;
27+
let addr_long: u64 = 0xdead_dad;
28+
let tx_power: i8 = 4;
29+
let channel: u8 = 11;
30+
31+
writeln!(Console::writer(), "Configuring IEEE 802.15.4 radio...\n").unwrap();
32+
33+
Ieee802154::set_pan(pan);
34+
writeln!(Console::writer(), "Set PAN to {:#06x}\n", pan).unwrap();
35+
36+
Ieee802154::set_address_short(addr_short);
37+
writeln!(
38+
Console::writer(),
39+
"Set short address to {:#06x}\n",
40+
addr_short
41+
)
42+
.unwrap();
43+
44+
Ieee802154::set_address_long(addr_long);
45+
writeln!(
46+
Console::writer(),
47+
"Set long address to {:#018x}\n",
48+
addr_long
49+
)
50+
.unwrap();
51+
52+
Ieee802154::set_tx_power(tx_power).unwrap();
53+
writeln!(Console::writer(), "Set TX power to {}\n", tx_power).unwrap();
54+
55+
Ieee802154::set_channel(channel).unwrap();
56+
writeln!(Console::writer(), "Set channel to {}\n", channel).unwrap();
57+
58+
// Don't forget to commit the config!
59+
Ieee802154::commit_config();
60+
writeln!(Console::writer(), "Committed radio configuration!\n").unwrap();
61+
62+
// Turn the radio on
63+
Ieee802154::radio_on().unwrap();
64+
assert!(Ieee802154::is_on());
65+
writeln!(Console::writer(), "Radio is on!\n").unwrap();
66+
67+
let mut buf = RxRingBuffer::<2>::new();
68+
let mut operator = RxSingleBufferOperator::new(&mut buf);
69+
loop {
70+
let frame = operator.receive_frame().unwrap();
71+
72+
let body_len = frame.payload_len;
73+
74+
// Parse the counter.
75+
let text = &frame.body[..body_len as usize - core::mem::size_of::<usize>()];
76+
let counter_bytes =
77+
&frame.body[body_len as usize - core::mem::size_of::<usize>()..body_len as usize];
78+
let counter = usize::from_be_bytes(counter_bytes.try_into().unwrap());
79+
80+
writeln!(
81+
Console::writer(),
82+
"Received frame with body of len {}: \"{} {}\"\n",
83+
body_len,
84+
core::str::from_utf8(text).unwrap_or("-- error decoding utf8 string --"),
85+
counter,
86+
)
87+
.unwrap();
88+
}
89+
}
Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
//! An example showing use of IEEE 802.15.4 networking.
22
//! It infinitely sends a frame with a constantly incremented counter,
33
//! and after each send receives a frame and prints it to Console.
4+
//!
5+
//! The kernel contains a standard and phy 15.4 driver. This example
6+
//! expects the kernel to be configured with the phy 15.4 driver to
7+
//! allow direct access to the radio and the ability to send "raw"
8+
//! frames. An example board file using this driver is provided at
9+
//!
10+
//! "No Support" Errors for setting the channel/tx power are a telltale
11+
//! sign that the kernel is not configured with the phy 15.4 driver.
12+
//! `boards/tutorials/nrf52840dk-thread-tutorial`.
413
514
#![no_main]
615
#![no_std]
7-
use core::fmt::Write as _;
16+
use core::fmt::Write;
817
use libtock::alarm::{Alarm, Milliseconds};
918
use libtock::console::Console;
1019
use libtock::ieee802154::{Ieee802154, RxOperator as _, RxRingBuffer, RxSingleBufferOperator};
@@ -18,21 +27,44 @@ fn main() {
1827
let pan: u16 = 0xcafe;
1928
let addr_short: u16 = 0xdead;
2029
let addr_long: u64 = 0xdead_dad;
21-
let tx_power: i8 = 5;
30+
let tx_power: i8 = 4;
2231
let channel: u8 = 11;
2332

33+
writeln!(Console::writer(), "Configuring IEEE 802.15.4 radio...\n").unwrap();
34+
2435
Ieee802154::set_pan(pan);
36+
writeln!(Console::writer(), "Set PAN to {:#06x}\n", pan).unwrap();
37+
2538
Ieee802154::set_address_short(addr_short);
39+
writeln!(
40+
Console::writer(),
41+
"Set short address to {:#06x}\n",
42+
addr_short
43+
)
44+
.unwrap();
45+
2646
Ieee802154::set_address_long(addr_long);
47+
writeln!(
48+
Console::writer(),
49+
"Set long address to {:#018x}\n",
50+
addr_long
51+
)
52+
.unwrap();
53+
2754
Ieee802154::set_tx_power(tx_power).unwrap();
55+
writeln!(Console::writer(), "Set TX power to {}\n", tx_power).unwrap();
56+
2857
Ieee802154::set_channel(channel).unwrap();
58+
writeln!(Console::writer(), "Set channel to {}\n", channel).unwrap();
2959

3060
// Don't forget to commit the config!
3161
Ieee802154::commit_config();
62+
writeln!(Console::writer(), "Committed radio configuration!\n").unwrap();
3263

3364
// Turn the radio on
3465
Ieee802154::radio_on().unwrap();
3566
assert!(Ieee802154::is_on());
67+
writeln!(Console::writer(), "Radio is on!\n").unwrap();
3668

3769
let mut buf = RxRingBuffer::<2>::new();
3870
let mut operator = RxSingleBufferOperator::new(&mut buf);
@@ -54,25 +86,26 @@ fn main() {
5486
set_buf_cnt(&mut buf, &mut counter);
5587

5688
// Transmit a frame
57-
Ieee802154::transmit_frame(&buf).unwrap();
89+
Ieee802154::transmit_frame_raw(&buf).unwrap();
5890

5991
writeln!(Console::writer(), "Transmitted frame {counter}!\n").unwrap();
6092

6193
let frame = operator.receive_frame().unwrap();
6294

6395
let body_len = frame.payload_len;
96+
97+
// Parse the counter.
98+
let text = &frame.body[..body_len as usize - core::mem::size_of::<usize>()];
99+
let counter_bytes =
100+
&frame.body[body_len as usize - core::mem::size_of::<usize>()..body_len as usize];
101+
let received_counter = usize::from_be_bytes(counter_bytes.try_into().unwrap());
102+
64103
writeln!(
65104
Console::writer(),
66-
"Received frame with body of len {}: {}-{} {:?}!\n",
105+
"Received frame with body of len {}: \"{} {}\"\n",
67106
body_len,
68-
core::str::from_utf8(&frame.body[..frame.body.len() - core::mem::size_of::<usize>()])
69-
.unwrap_or("<error decoding>"),
70-
usize::from_le_bytes(
71-
frame.body[frame.body.len() - core::mem::size_of::<usize>()..]
72-
.try_into()
73-
.unwrap()
74-
),
75-
frame.body
107+
core::str::from_utf8(text).unwrap_or("-- error decoding utf8 string --"),
108+
received_counter,
76109
)
77110
.unwrap();
78111

0 commit comments

Comments
 (0)