Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 67acd8f

Browse files
authored
Merge pull request #3904 from tyler-potyondy/radio-channel
15.4 Radio channel Enum
2 parents d2e7618 + b286a72 commit 67acd8f

File tree

6 files changed

+92
-102
lines changed

6 files changed

+92
-102
lines changed

boards/components/src/rf233.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ use capsules_core::virtualizers::virtual_spi::VirtualSpiMasterDevice;
2525
use capsules_extra::rf233::RF233;
2626
use core::mem::MaybeUninit;
2727
use kernel::component::Component;
28-
use kernel::hil;
2928
use kernel::hil::spi::{SpiMaster, SpiMasterDevice};
29+
use kernel::hil::{self, radio};
3030

3131
// Setup static space for the objects.
3232
#[macro_export]
@@ -47,7 +47,7 @@ pub struct RF233Component<S: SpiMaster<'static> + 'static> {
4747
sleep: &'static dyn hil::gpio::Pin,
4848
irq: &'static dyn hil::gpio::InterruptPin<'static>,
4949
ctl: &'static dyn hil::gpio::InterruptPin<'static>,
50-
channel: u8,
50+
channel: radio::RadioChannel,
5151
}
5252

5353
impl<S: SpiMaster<'static> + 'static> RF233Component<S> {
@@ -57,7 +57,7 @@ impl<S: SpiMaster<'static> + 'static> RF233Component<S> {
5757
sleep: &'static dyn hil::gpio::Pin,
5858
irq: &'static dyn hil::gpio::InterruptPin<'static>,
5959
ctl: &'static dyn hil::gpio::InterruptPin<'static>,
60-
channel: u8,
60+
channel: radio::RadioChannel,
6161
) -> Self {
6262
Self {
6363
spi,

boards/imix/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ const NUM_PROCS: usize = 4;
8888
// have those devices talk to each other without having to modify the kernel flashed
8989
// onto each device. This makes MAC address configuration a good target for capabilities -
9090
// only allow one app per board to have control of MAC address configuration?
91-
const RADIO_CHANNEL: u8 = 26;
91+
const RADIO_CHANNEL: radio::RadioChannel = radio::RadioChannel::Channel26;
9292
const DST_MAC_ADDR: MacAddress = MacAddress::Short(49138);
9393
const DEFAULT_CTX_PREFIX_LEN: u8 = 8; //Length of context for 6LoWPAN compression
9494
const DEFAULT_CTX_PREFIX: [u8; 16] = [0x0_u8; 16]; //Context for 6LoWPAN Compression

capsules/extra/src/rf233.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ pub struct RF233<'a, S: spi::SpiMasterDevice<'a>> {
217217
addr_long: Cell<[u8; 8]>,
218218
pan: Cell<u16>,
219219
tx_power: Cell<i8>,
220-
channel: Cell<u8>,
220+
channel: Cell<radio::RadioChannel>,
221221
spi_rx: TakeCell<'static, [u8]>,
222222
spi_tx: TakeCell<'static, [u8]>,
223223
spi_buf: TakeCell<'static, [u8]>,
@@ -488,7 +488,7 @@ impl<'a, S: spi::SpiMasterDevice<'a>> spi::SpiMasterClient for RF233<'a, S> {
488488
);
489489
}
490490
InternalState::START_CTRL1_SET => {
491-
let val = self.channel.get() | PHY_CC_CCA_MODE_CS_OR_ED;
491+
let val = self.channel.get().get_channel_number() | PHY_CC_CCA_MODE_CS_OR_ED;
492492
self.state_transition_write(
493493
RF233Register::PHY_CC_CCA,
494494
val,
@@ -1004,7 +1004,7 @@ impl<'a, S: spi::SpiMasterDevice<'a>> spi::SpiMasterClient for RF233<'a, S> {
10041004
);
10051005
}
10061006
InternalState::CONFIG_POWER_SET => {
1007-
let val = self.channel.get() | PHY_CC_CCA_MODE_CS_OR_ED;
1007+
let val = self.channel.get().get_channel_number() | PHY_CC_CCA_MODE_CS_OR_ED;
10081008
self.state_transition_write(
10091009
RF233Register::PHY_CC_CCA,
10101010
val,
@@ -1034,7 +1034,7 @@ impl<'a, S: spi::SpiMasterDevice<'a>> RF233<'a, S> {
10341034
reset: &'a dyn gpio::Pin,
10351035
sleep: &'a dyn gpio::Pin,
10361036
irq: &'a dyn gpio::InterruptPin<'a>,
1037-
channel: u8,
1037+
channel: radio::RadioChannel,
10381038
) -> RF233<'a, S> {
10391039
RF233 {
10401040
spi: spi,
@@ -1283,13 +1283,8 @@ impl<'a, S: spi::SpiMasterDevice<'a>> radio::RadioConfig<'a> for RF233<'a, S> {
12831283
}
12841284
}
12851285

1286-
fn set_channel(&self, chan: u8) -> Result<(), ErrorCode> {
1287-
if chan >= 11 && chan <= 26 {
1288-
self.channel.set(chan);
1289-
Ok(())
1290-
} else {
1291-
Err(ErrorCode::INVAL)
1292-
}
1286+
fn set_channel(&self, chan: radio::RadioChannel) {
1287+
self.channel.set(chan);
12931288
}
12941289

12951290
fn get_address(&self) -> u16 {
@@ -1310,7 +1305,7 @@ impl<'a, S: spi::SpiMasterDevice<'a>> radio::RadioConfig<'a> for RF233<'a, S> {
13101305
}
13111306
/// The 802.15.4 channel
13121307
fn get_channel(&self) -> u8 {
1313-
self.channel.get()
1308+
self.channel.get().get_channel_number()
13141309
}
13151310

13161311
fn config_commit(&self) {

chips/nrf52840/src/ieee802154_radio.rs

Lines changed: 7 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
77
use crate::timer::TimerAlarm;
88
use core::cell::Cell;
9-
use kernel::hil::radio::{self, PowerClient, RadioData};
9+
use core::convert::TryFrom;
10+
use kernel;
11+
use kernel::hil::radio::{self, PowerClient, RadioChannel, RadioData};
1012
use kernel::hil::time::{Alarm, AlarmClient, Time};
1113
use kernel::utilities::cells::{OptionalCell, TakeCell};
1214
use kernel::utilities::registers::interfaces::{Readable, Writeable};
@@ -92,77 +94,6 @@ pub const ACK_BUF_SIZE: usize = 6;
9294
// to return the frame buffer.
9395
const MIMIC_PSDU_OFFSET: u32 = 1;
9496

95-
// IEEEStd 802.15.4-2011 Section 8.1.2.2
96-
// Frequency is 2405 + 5 * (k - 11) MHz, where k = 11, 12, ... , 26.
97-
#[derive(PartialEq, Debug, Copy, Clone)]
98-
pub enum RadioChannel {
99-
DataChannel11 = 5,
100-
DataChannel12 = 10,
101-
DataChannel13 = 15,
102-
DataChannel14 = 20,
103-
DataChannel15 = 25,
104-
DataChannel16 = 30,
105-
DataChannel17 = 35,
106-
DataChannel18 = 40,
107-
DataChannel19 = 45,
108-
DataChannel20 = 50,
109-
DataChannel21 = 55,
110-
DataChannel22 = 60,
111-
DataChannel23 = 65,
112-
DataChannel24 = 70,
113-
DataChannel25 = 75,
114-
DataChannel26 = 80,
115-
}
116-
117-
impl RadioChannel {
118-
pub fn get_channel_index(&self) -> u8 {
119-
match *self {
120-
RadioChannel::DataChannel11 => 11,
121-
RadioChannel::DataChannel12 => 12,
122-
RadioChannel::DataChannel13 => 13,
123-
RadioChannel::DataChannel14 => 14,
124-
RadioChannel::DataChannel15 => 15,
125-
RadioChannel::DataChannel16 => 16,
126-
RadioChannel::DataChannel17 => 17,
127-
RadioChannel::DataChannel18 => 18,
128-
RadioChannel::DataChannel19 => 19,
129-
RadioChannel::DataChannel20 => 20,
130-
RadioChannel::DataChannel21 => 21,
131-
RadioChannel::DataChannel22 => 22,
132-
RadioChannel::DataChannel23 => 23,
133-
RadioChannel::DataChannel24 => 24,
134-
RadioChannel::DataChannel25 => 25,
135-
RadioChannel::DataChannel26 => 26,
136-
}
137-
}
138-
}
139-
140-
impl TryFrom<u8> for RadioChannel {
141-
type Error = ();
142-
143-
fn try_from(val: u8) -> Result<RadioChannel, ()> {
144-
match val {
145-
11 => Ok(RadioChannel::DataChannel11),
146-
12 => Ok(RadioChannel::DataChannel12),
147-
13 => Ok(RadioChannel::DataChannel13),
148-
14 => Ok(RadioChannel::DataChannel14),
149-
15 => Ok(RadioChannel::DataChannel15),
150-
16 => Ok(RadioChannel::DataChannel16),
151-
17 => Ok(RadioChannel::DataChannel17),
152-
18 => Ok(RadioChannel::DataChannel18),
153-
19 => Ok(RadioChannel::DataChannel19),
154-
20 => Ok(RadioChannel::DataChannel20),
155-
21 => Ok(RadioChannel::DataChannel21),
156-
22 => Ok(RadioChannel::DataChannel22),
157-
23 => Ok(RadioChannel::DataChannel23),
158-
24 => Ok(RadioChannel::DataChannel24),
159-
25 => Ok(RadioChannel::DataChannel25),
160-
26 => Ok(RadioChannel::DataChannel26),
161-
_ => Err(()),
162-
}
163-
}
164-
}
165-
16697
#[repr(C)]
16798
struct RadioRegisters {
16899
/// Enable Radio in TX mode
@@ -768,7 +699,7 @@ impl<'a> Radio<'a> {
768699
cca_count: Cell::new(0),
769700
cca_be: Cell::new(0),
770701
random_nonce: Cell::new(0xDEADBEEF),
771-
channel: Cell::new(RadioChannel::DataChannel26),
702+
channel: Cell::new(RadioChannel::Channel26),
772703
timer0: OptionalCell::empty(),
773704
state: Cell::new(RadioState::OFF),
774705
}
@@ -1279,7 +1210,7 @@ impl<'a> kernel::hil::radio::RadioConfig<'a> for Radio<'a> {
12791210
}
12801211
/// The 802.15.4 channel
12811212
fn get_channel(&self) -> u8 {
1282-
self.channel.get().get_channel_index()
1213+
self.channel.get().get_channel_number()
12831214
}
12841215

12851216
//#################################################
@@ -1298,14 +1229,8 @@ impl<'a> kernel::hil::radio::RadioConfig<'a> for Radio<'a> {
12981229
self.pan.set(id);
12991230
}
13001231

1301-
fn set_channel(&self, chan: u8) -> Result<(), ErrorCode> {
1302-
match RadioChannel::try_from(chan) {
1303-
Err(()) => Err(ErrorCode::NOSUPPORT),
1304-
Ok(res) => {
1305-
self.channel.set(res);
1306-
Ok(())
1307-
}
1308-
}
1232+
fn set_channel(&self, chan: RadioChannel) {
1233+
self.channel.set(chan);
13091234
}
13101235

13111236
fn set_tx_power(&self, tx_power: i8) -> Result<(), ErrorCode> {

doc/reference/trd-radio.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ A caller can configure the 16-bit short address, 64-bit full address,
160160
PAN (personal area network) identifier, transmit power, and
161161
channel. The PAN address and node address are both 16-bit values.
162162
Channel is an integer in the range 11-26 (the 802.15.4 channel
163-
numbers). `config_set_channel` MUST return INVAL if passed a channel
164-
not in the range 11-26 and Ok(()) otherwise.
163+
numbers). The channel is encoded in the `radio::RadioChannel`
164+
enum, ensuring the channel value resides in the valid range.
165165

166166
fn config_address(&self) -> u16;
167167
fn config_address_long(&self) -> [u8;8];
@@ -172,7 +172,7 @@ not in the range 11-26 and Ok(()) otherwise.
172172
fn config_set_address_long(&self, addr: [u8;8]);
173173
fn config_set_pan(&self, addr: u16);
174174
fn config_set_tx_power(&self, power: i8) -> Result<(), ErrorCode>;
175-
fn config_set_channel(&self, chan: u8) -> Result<(), ErrorCode>;
175+
fn config_set_channel(&self, chan: radio::RadioChannel);
176176

177177
`config_set_tx_power` takes an signed integer, whose units are dBm.
178178
If the specified value is greater than the maximum supported transmit

kernel/src/hil/radio.rs

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ pub trait RadioConfig<'a> {
104104
fn set_address_long(&self, addr: [u8; 8]);
105105
fn set_pan(&self, id: u16);
106106
fn set_tx_power(&self, power: i8) -> Result<(), ErrorCode>;
107-
fn set_channel(&self, chan: u8) -> Result<(), ErrorCode>;
107+
fn set_channel(&self, chan: RadioChannel);
108108
}
109109

110110
pub trait RadioData<'a> {
@@ -118,3 +118,73 @@ pub trait RadioData<'a> {
118118
frame_len: usize,
119119
) -> Result<(), (ErrorCode, &'static mut [u8])>;
120120
}
121+
122+
#[derive(PartialEq, Debug, Copy, Clone)]
123+
pub enum RadioChannel {
124+
Channel11 = 5,
125+
Channel12 = 10,
126+
Channel13 = 15,
127+
Channel14 = 20,
128+
Channel15 = 25,
129+
Channel16 = 30,
130+
Channel17 = 35,
131+
Channel18 = 40,
132+
Channel19 = 45,
133+
Channel20 = 50,
134+
Channel21 = 55,
135+
Channel22 = 60,
136+
Channel23 = 65,
137+
Channel24 = 70,
138+
Channel25 = 75,
139+
Channel26 = 80,
140+
}
141+
142+
impl RadioChannel {
143+
/// Returns the u8 value of the channel, which is the IEEE 802.15.4 channel
144+
pub fn get_channel_number(&self) -> u8 {
145+
match *self {
146+
RadioChannel::Channel11 => 11,
147+
RadioChannel::Channel12 => 12,
148+
RadioChannel::Channel13 => 13,
149+
RadioChannel::Channel14 => 14,
150+
RadioChannel::Channel15 => 15,
151+
RadioChannel::Channel16 => 16,
152+
RadioChannel::Channel17 => 17,
153+
RadioChannel::Channel18 => 18,
154+
RadioChannel::Channel19 => 19,
155+
RadioChannel::Channel20 => 20,
156+
RadioChannel::Channel21 => 21,
157+
RadioChannel::Channel22 => 22,
158+
RadioChannel::Channel23 => 23,
159+
RadioChannel::Channel24 => 24,
160+
RadioChannel::Channel25 => 25,
161+
RadioChannel::Channel26 => 26,
162+
}
163+
}
164+
}
165+
166+
impl TryFrom<u8> for RadioChannel {
167+
type Error = ();
168+
/// Returns the RadioChannel for the given u8 value, which is the IEEE 802.15.4 channel
169+
fn try_from(val: u8) -> Result<RadioChannel, ()> {
170+
match val {
171+
11 => Ok(RadioChannel::Channel11),
172+
12 => Ok(RadioChannel::Channel12),
173+
13 => Ok(RadioChannel::Channel13),
174+
14 => Ok(RadioChannel::Channel14),
175+
15 => Ok(RadioChannel::Channel15),
176+
16 => Ok(RadioChannel::Channel16),
177+
17 => Ok(RadioChannel::Channel17),
178+
18 => Ok(RadioChannel::Channel18),
179+
19 => Ok(RadioChannel::Channel19),
180+
20 => Ok(RadioChannel::Channel20),
181+
21 => Ok(RadioChannel::Channel21),
182+
22 => Ok(RadioChannel::Channel22),
183+
23 => Ok(RadioChannel::Channel23),
184+
24 => Ok(RadioChannel::Channel24),
185+
25 => Ok(RadioChannel::Channel25),
186+
26 => Ok(RadioChannel::Channel26),
187+
_ => Err(()),
188+
}
189+
}
190+
}

0 commit comments

Comments
 (0)