Skip to content

Commit

Permalink
Merge pull request tock#3904 from tyler-potyondy/radio-channel
Browse files Browse the repository at this point in the history
15.4 Radio channel Enum
  • Loading branch information
alevy authored Mar 8, 2024
2 parents d2e7618 + b286a72 commit 67acd8f
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 102 deletions.
6 changes: 3 additions & 3 deletions boards/components/src/rf233.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ use capsules_core::virtualizers::virtual_spi::VirtualSpiMasterDevice;
use capsules_extra::rf233::RF233;
use core::mem::MaybeUninit;
use kernel::component::Component;
use kernel::hil;
use kernel::hil::spi::{SpiMaster, SpiMasterDevice};
use kernel::hil::{self, radio};

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

impl<S: SpiMaster<'static> + 'static> RF233Component<S> {
Expand All @@ -57,7 +57,7 @@ impl<S: SpiMaster<'static> + 'static> RF233Component<S> {
sleep: &'static dyn hil::gpio::Pin,
irq: &'static dyn hil::gpio::InterruptPin<'static>,
ctl: &'static dyn hil::gpio::InterruptPin<'static>,
channel: u8,
channel: radio::RadioChannel,
) -> Self {
Self {
spi,
Expand Down
2 changes: 1 addition & 1 deletion boards/imix/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const NUM_PROCS: usize = 4;
// have those devices talk to each other without having to modify the kernel flashed
// onto each device. This makes MAC address configuration a good target for capabilities -
// only allow one app per board to have control of MAC address configuration?
const RADIO_CHANNEL: u8 = 26;
const RADIO_CHANNEL: radio::RadioChannel = radio::RadioChannel::Channel26;
const DST_MAC_ADDR: MacAddress = MacAddress::Short(49138);
const DEFAULT_CTX_PREFIX_LEN: u8 = 8; //Length of context for 6LoWPAN compression
const DEFAULT_CTX_PREFIX: [u8; 16] = [0x0_u8; 16]; //Context for 6LoWPAN Compression
Expand Down
19 changes: 7 additions & 12 deletions capsules/extra/src/rf233.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ pub struct RF233<'a, S: spi::SpiMasterDevice<'a>> {
addr_long: Cell<[u8; 8]>,
pan: Cell<u16>,
tx_power: Cell<i8>,
channel: Cell<u8>,
channel: Cell<radio::RadioChannel>,
spi_rx: TakeCell<'static, [u8]>,
spi_tx: TakeCell<'static, [u8]>,
spi_buf: TakeCell<'static, [u8]>,
Expand Down Expand Up @@ -488,7 +488,7 @@ impl<'a, S: spi::SpiMasterDevice<'a>> spi::SpiMasterClient for RF233<'a, S> {
);
}
InternalState::START_CTRL1_SET => {
let val = self.channel.get() | PHY_CC_CCA_MODE_CS_OR_ED;
let val = self.channel.get().get_channel_number() | PHY_CC_CCA_MODE_CS_OR_ED;
self.state_transition_write(
RF233Register::PHY_CC_CCA,
val,
Expand Down Expand Up @@ -1004,7 +1004,7 @@ impl<'a, S: spi::SpiMasterDevice<'a>> spi::SpiMasterClient for RF233<'a, S> {
);
}
InternalState::CONFIG_POWER_SET => {
let val = self.channel.get() | PHY_CC_CCA_MODE_CS_OR_ED;
let val = self.channel.get().get_channel_number() | PHY_CC_CCA_MODE_CS_OR_ED;
self.state_transition_write(
RF233Register::PHY_CC_CCA,
val,
Expand Down Expand Up @@ -1034,7 +1034,7 @@ impl<'a, S: spi::SpiMasterDevice<'a>> RF233<'a, S> {
reset: &'a dyn gpio::Pin,
sleep: &'a dyn gpio::Pin,
irq: &'a dyn gpio::InterruptPin<'a>,
channel: u8,
channel: radio::RadioChannel,
) -> RF233<'a, S> {
RF233 {
spi: spi,
Expand Down Expand Up @@ -1283,13 +1283,8 @@ impl<'a, S: spi::SpiMasterDevice<'a>> radio::RadioConfig<'a> for RF233<'a, S> {
}
}

fn set_channel(&self, chan: u8) -> Result<(), ErrorCode> {
if chan >= 11 && chan <= 26 {
self.channel.set(chan);
Ok(())
} else {
Err(ErrorCode::INVAL)
}
fn set_channel(&self, chan: radio::RadioChannel) {
self.channel.set(chan);
}

fn get_address(&self) -> u16 {
Expand All @@ -1310,7 +1305,7 @@ impl<'a, S: spi::SpiMasterDevice<'a>> radio::RadioConfig<'a> for RF233<'a, S> {
}
/// The 802.15.4 channel
fn get_channel(&self) -> u8 {
self.channel.get()
self.channel.get().get_channel_number()
}

fn config_commit(&self) {
Expand Down
89 changes: 7 additions & 82 deletions chips/nrf52840/src/ieee802154_radio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
use crate::timer::TimerAlarm;
use core::cell::Cell;
use kernel::hil::radio::{self, PowerClient, RadioData};
use core::convert::TryFrom;
use kernel;
use kernel::hil::radio::{self, PowerClient, RadioChannel, RadioData};
use kernel::hil::time::{Alarm, AlarmClient, Time};
use kernel::utilities::cells::{OptionalCell, TakeCell};
use kernel::utilities::registers::interfaces::{Readable, Writeable};
Expand Down Expand Up @@ -92,77 +94,6 @@ pub const ACK_BUF_SIZE: usize = 6;
// to return the frame buffer.
const MIMIC_PSDU_OFFSET: u32 = 1;

// IEEEStd 802.15.4-2011 Section 8.1.2.2
// Frequency is 2405 + 5 * (k - 11) MHz, where k = 11, 12, ... , 26.
#[derive(PartialEq, Debug, Copy, Clone)]
pub enum RadioChannel {
DataChannel11 = 5,
DataChannel12 = 10,
DataChannel13 = 15,
DataChannel14 = 20,
DataChannel15 = 25,
DataChannel16 = 30,
DataChannel17 = 35,
DataChannel18 = 40,
DataChannel19 = 45,
DataChannel20 = 50,
DataChannel21 = 55,
DataChannel22 = 60,
DataChannel23 = 65,
DataChannel24 = 70,
DataChannel25 = 75,
DataChannel26 = 80,
}

impl RadioChannel {
pub fn get_channel_index(&self) -> u8 {
match *self {
RadioChannel::DataChannel11 => 11,
RadioChannel::DataChannel12 => 12,
RadioChannel::DataChannel13 => 13,
RadioChannel::DataChannel14 => 14,
RadioChannel::DataChannel15 => 15,
RadioChannel::DataChannel16 => 16,
RadioChannel::DataChannel17 => 17,
RadioChannel::DataChannel18 => 18,
RadioChannel::DataChannel19 => 19,
RadioChannel::DataChannel20 => 20,
RadioChannel::DataChannel21 => 21,
RadioChannel::DataChannel22 => 22,
RadioChannel::DataChannel23 => 23,
RadioChannel::DataChannel24 => 24,
RadioChannel::DataChannel25 => 25,
RadioChannel::DataChannel26 => 26,
}
}
}

impl TryFrom<u8> for RadioChannel {
type Error = ();

fn try_from(val: u8) -> Result<RadioChannel, ()> {
match val {
11 => Ok(RadioChannel::DataChannel11),
12 => Ok(RadioChannel::DataChannel12),
13 => Ok(RadioChannel::DataChannel13),
14 => Ok(RadioChannel::DataChannel14),
15 => Ok(RadioChannel::DataChannel15),
16 => Ok(RadioChannel::DataChannel16),
17 => Ok(RadioChannel::DataChannel17),
18 => Ok(RadioChannel::DataChannel18),
19 => Ok(RadioChannel::DataChannel19),
20 => Ok(RadioChannel::DataChannel20),
21 => Ok(RadioChannel::DataChannel21),
22 => Ok(RadioChannel::DataChannel22),
23 => Ok(RadioChannel::DataChannel23),
24 => Ok(RadioChannel::DataChannel24),
25 => Ok(RadioChannel::DataChannel25),
26 => Ok(RadioChannel::DataChannel26),
_ => Err(()),
}
}
}

#[repr(C)]
struct RadioRegisters {
/// Enable Radio in TX mode
Expand Down Expand Up @@ -768,7 +699,7 @@ impl<'a> Radio<'a> {
cca_count: Cell::new(0),
cca_be: Cell::new(0),
random_nonce: Cell::new(0xDEADBEEF),
channel: Cell::new(RadioChannel::DataChannel26),
channel: Cell::new(RadioChannel::Channel26),
timer0: OptionalCell::empty(),
state: Cell::new(RadioState::OFF),
}
Expand Down Expand Up @@ -1279,7 +1210,7 @@ impl<'a> kernel::hil::radio::RadioConfig<'a> for Radio<'a> {
}
/// The 802.15.4 channel
fn get_channel(&self) -> u8 {
self.channel.get().get_channel_index()
self.channel.get().get_channel_number()
}

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

fn set_channel(&self, chan: u8) -> Result<(), ErrorCode> {
match RadioChannel::try_from(chan) {
Err(()) => Err(ErrorCode::NOSUPPORT),
Ok(res) => {
self.channel.set(res);
Ok(())
}
}
fn set_channel(&self, chan: RadioChannel) {
self.channel.set(chan);
}

fn set_tx_power(&self, tx_power: i8) -> Result<(), ErrorCode> {
Expand Down
6 changes: 3 additions & 3 deletions doc/reference/trd-radio.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ A caller can configure the 16-bit short address, 64-bit full address,
PAN (personal area network) identifier, transmit power, and
channel. The PAN address and node address are both 16-bit values.
Channel is an integer in the range 11-26 (the 802.15.4 channel
numbers). `config_set_channel` MUST return INVAL if passed a channel
not in the range 11-26 and Ok(()) otherwise.
numbers). The channel is encoded in the `radio::RadioChannel`
enum, ensuring the channel value resides in the valid range.

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

`config_set_tx_power` takes an signed integer, whose units are dBm.
If the specified value is greater than the maximum supported transmit
Expand Down
72 changes: 71 additions & 1 deletion kernel/src/hil/radio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub trait RadioConfig<'a> {
fn set_address_long(&self, addr: [u8; 8]);
fn set_pan(&self, id: u16);
fn set_tx_power(&self, power: i8) -> Result<(), ErrorCode>;
fn set_channel(&self, chan: u8) -> Result<(), ErrorCode>;
fn set_channel(&self, chan: RadioChannel);
}

pub trait RadioData<'a> {
Expand All @@ -118,3 +118,73 @@ pub trait RadioData<'a> {
frame_len: usize,
) -> Result<(), (ErrorCode, &'static mut [u8])>;
}

#[derive(PartialEq, Debug, Copy, Clone)]
pub enum RadioChannel {
Channel11 = 5,
Channel12 = 10,
Channel13 = 15,
Channel14 = 20,
Channel15 = 25,
Channel16 = 30,
Channel17 = 35,
Channel18 = 40,
Channel19 = 45,
Channel20 = 50,
Channel21 = 55,
Channel22 = 60,
Channel23 = 65,
Channel24 = 70,
Channel25 = 75,
Channel26 = 80,
}

impl RadioChannel {
/// Returns the u8 value of the channel, which is the IEEE 802.15.4 channel
pub fn get_channel_number(&self) -> u8 {
match *self {
RadioChannel::Channel11 => 11,
RadioChannel::Channel12 => 12,
RadioChannel::Channel13 => 13,
RadioChannel::Channel14 => 14,
RadioChannel::Channel15 => 15,
RadioChannel::Channel16 => 16,
RadioChannel::Channel17 => 17,
RadioChannel::Channel18 => 18,
RadioChannel::Channel19 => 19,
RadioChannel::Channel20 => 20,
RadioChannel::Channel21 => 21,
RadioChannel::Channel22 => 22,
RadioChannel::Channel23 => 23,
RadioChannel::Channel24 => 24,
RadioChannel::Channel25 => 25,
RadioChannel::Channel26 => 26,
}
}
}

impl TryFrom<u8> for RadioChannel {
type Error = ();
/// Returns the RadioChannel for the given u8 value, which is the IEEE 802.15.4 channel
fn try_from(val: u8) -> Result<RadioChannel, ()> {
match val {
11 => Ok(RadioChannel::Channel11),
12 => Ok(RadioChannel::Channel12),
13 => Ok(RadioChannel::Channel13),
14 => Ok(RadioChannel::Channel14),
15 => Ok(RadioChannel::Channel15),
16 => Ok(RadioChannel::Channel16),
17 => Ok(RadioChannel::Channel17),
18 => Ok(RadioChannel::Channel18),
19 => Ok(RadioChannel::Channel19),
20 => Ok(RadioChannel::Channel20),
21 => Ok(RadioChannel::Channel21),
22 => Ok(RadioChannel::Channel22),
23 => Ok(RadioChannel::Channel23),
24 => Ok(RadioChannel::Channel24),
25 => Ok(RadioChannel::Channel25),
26 => Ok(RadioChannel::Channel26),
_ => Err(()),
}
}
}

0 comments on commit 67acd8f

Please sign in to comment.