Skip to content

Commit 92b9ca7

Browse files
examples: ieee802154: update to correct interface
This adds an `ieee802154_tx_raw` example to use the kernel's Phy 15.4 driver. The standard 15.4 driver returns an error on setting channel, setting on/off, and setting power as these are handled by a higher layer in the kernel. This updates the non "raw" examples to no longer use this commands as they will return a no-support error.
1 parent 56e4695 commit 92b9ca7

File tree

4 files changed

+117
-18
lines changed

4 files changed

+117
-18
lines changed

examples/ieee802154_rx.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,26 @@ fn main() {
1616
let pan: u16 = 0xcafe;
1717
let addr_short: u16 = 0xdead;
1818
let addr_long: u64 = 0xdead_dad;
19-
let tx_power: i8 = 5;
20-
let channel: u8 = 11;
19+
20+
writeln!(Console::writer(), "Configuring IEEE 802.15.4 radio...\n").unwrap();
2121

2222
Ieee802154::set_pan(pan);
23+
writeln!(Console::writer(), "Set PAN to {pan:#06x}\n").unwrap();
24+
2325
Ieee802154::set_address_short(addr_short);
26+
writeln!(
27+
Console::writer(),
28+
"Set short address to {addr_short:#06x}\n"
29+
)
30+
.unwrap();
31+
2432
Ieee802154::set_address_long(addr_long);
25-
Ieee802154::set_tx_power(tx_power).unwrap();
26-
Ieee802154::set_channel(channel).unwrap();
33+
writeln!(Console::writer(), "Set long address to {addr_long:#018x}\n").unwrap();
2734

2835
// Don't forget to commit the config!
2936
Ieee802154::commit_config();
37+
writeln!(Console::writer(), "Committed IEEE 802.15.4 config\n").unwrap();
3038

31-
// Turn the radio on
32-
Ieee802154::radio_on().unwrap();
3339
assert!(Ieee802154::is_on());
3440

3541
let mut buf = RxRingBuffer::<2>::new();

examples/ieee802154_rx_tx.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,26 @@ fn main() {
1818
let pan: u16 = 0xcafe;
1919
let addr_short: u16 = 0xdead;
2020
let addr_long: u64 = 0xdead_dad;
21-
let tx_power: i8 = 5;
22-
let channel: u8 = 11;
21+
22+
writeln!(Console::writer(), "Configuring IEEE 802.15.4 radio...\n").unwrap();
2323

2424
Ieee802154::set_pan(pan);
25+
writeln!(Console::writer(), "Set PAN to {pan:#06x}\n").unwrap();
26+
2527
Ieee802154::set_address_short(addr_short);
28+
writeln!(
29+
Console::writer(),
30+
"Set short address to {addr_short:#06x}\n"
31+
)
32+
.unwrap();
33+
2634
Ieee802154::set_address_long(addr_long);
27-
Ieee802154::set_tx_power(tx_power).unwrap();
28-
Ieee802154::set_channel(channel).unwrap();
35+
writeln!(Console::writer(), "Set long address to {addr_long:#018x}\n").unwrap();
2936

3037
// Don't forget to commit the config!
3138
Ieee802154::commit_config();
39+
writeln!(Console::writer(), "Committed IEEE 802.15.4 config\n").unwrap();
3240

33-
// Turn the radio on
34-
Ieee802154::radio_on().unwrap();
3541
assert!(Ieee802154::is_on());
3642

3743
let mut buf = RxRingBuffer::<2>::new();

examples/ieee802154_tx.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,26 @@ fn main() {
1818
let pan: u16 = 0xcafe;
1919
let addr_short: u16 = 0xdead;
2020
let addr_long: u64 = 0xdeaddad;
21-
let tx_power: i8 = 5;
22-
let channel: u8 = 11;
21+
22+
writeln!(Console::writer(), "Configuring IEEE 802.15.4 radio...\n").unwrap();
2323

2424
Ieee802154::set_pan(pan);
25+
writeln!(Console::writer(), "Set PAN to {pan:#06x}\n").unwrap();
26+
2527
Ieee802154::set_address_short(addr_short);
28+
writeln!(
29+
Console::writer(),
30+
"Set short address to {addr_short:#06x}\n"
31+
)
32+
.unwrap();
33+
2634
Ieee802154::set_address_long(addr_long);
27-
Ieee802154::set_tx_power(tx_power).unwrap();
28-
Ieee802154::set_channel(channel).unwrap();
35+
writeln!(Console::writer(), "Set long address to {addr_long:#018x}\n").unwrap();
2936

3037
// Don't forget to commit the config!
3138
Ieee802154::commit_config();
39+
writeln!(Console::writer(), "Committed IEEE 802.15.4 config\n").unwrap();
3240

33-
// Turn the radio on
34-
Ieee802154::radio_on().unwrap();
3541
assert!(Ieee802154::is_on());
3642

3743
let mut counter = 0_usize;

examples/ieee802154_tx_raw.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//! An example showing use of IEEE 802.15.4 networking.
2+
//! It infinitely sends a frame with a constantly incremented counter.
3+
//!
4+
//! This uses the kernel IEEE 802.15.4 PHY driver to send "raw" frames,
5+
//! (i.e. the kernel does not add any headers). This requires your board
6+
//! file to use the IEEE 802.15.4 Phy capsule. Example board files can
7+
//! be found in `boards/tutorials/nrf52840dk-thread-tutorial/`.
8+
9+
#![no_main]
10+
#![no_std]
11+
use core::fmt::Write as _;
12+
13+
use libtock::alarm::{Alarm, Milliseconds};
14+
use libtock::console::Console;
15+
use libtock::ieee802154::Ieee802154;
16+
use libtock::runtime::{set_main, stack_size};
17+
18+
set_main! {main}
19+
stack_size! {0x600}
20+
21+
fn main() {
22+
// Configure the radio
23+
let pan: u16 = 0xcafe;
24+
let addr_short: u16 = 0xdead;
25+
let addr_long: u64 = 0xdeaddad;
26+
let tx_power: i8 = 4;
27+
let channel: u8 = 11;
28+
29+
writeln!(Console::writer(), "Configuring IEEE 802.15.4 radio...\n").unwrap();
30+
31+
Ieee802154::set_pan(pan);
32+
writeln!(Console::writer(), "Set PAN to {pan:#06x}\n").unwrap();
33+
34+
Ieee802154::set_address_short(addr_short);
35+
writeln!(
36+
Console::writer(),
37+
"Set short address to {addr_short:#06x}\n"
38+
)
39+
.unwrap();
40+
41+
Ieee802154::set_address_long(addr_long);
42+
writeln!(Console::writer(), "Set long address to {addr_long:#018x}\n").unwrap();
43+
44+
Ieee802154::set_tx_power(tx_power).unwrap();
45+
writeln!(Console::writer(), "Set TX power to {tx_power} dBm\n").unwrap();
46+
47+
Ieee802154::set_channel(channel).unwrap();
48+
writeln!(Console::writer(), "Set channel to {channel}\n").unwrap();
49+
50+
// Don't forget to commit the config!
51+
Ieee802154::commit_config();
52+
writeln!(Console::writer(), "Committed IEEE 802.15.4 config\n").unwrap();
53+
54+
// Turn the radio on
55+
Ieee802154::radio_on().unwrap();
56+
assert!(Ieee802154::is_on());
57+
58+
let mut counter = 0_usize;
59+
let mut buf = [
60+
b'f', b'r', b'a', b'm', b'e', b' ', b'n', b'.', b'o', b'.', b' ', b'\0', b'\0', b'\0',
61+
b'\0',
62+
];
63+
fn set_buf_cnt(buf: &mut [u8], counter: &mut usize) {
64+
let buf_len = buf.len();
65+
let buf_cnt = &mut buf[buf_len - core::mem::size_of_val(&counter)..];
66+
buf_cnt.copy_from_slice(&counter.to_be_bytes());
67+
}
68+
69+
loop {
70+
Alarm::sleep_for(Milliseconds(1000)).unwrap();
71+
72+
set_buf_cnt(&mut buf, &mut counter);
73+
74+
// Transmit a frame
75+
Ieee802154::transmit_frame_raw(&buf).unwrap();
76+
77+
writeln!(Console::writer(), "Transmitted frame {counter}!\n").unwrap();
78+
79+
counter += 1;
80+
}
81+
}

0 commit comments

Comments
 (0)