Skip to content

Commit

Permalink
Merge pull request #810 from cpluss/refactor_ble_adv
Browse files Browse the repository at this point in the history
Refactor ble advertising to capsule & kernel hil
  • Loading branch information
alevy committed Mar 18, 2018
2 parents e5b9251 + df5d5ba commit 66f676d
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 43 deletions.
14 changes: 7 additions & 7 deletions boards/nrf51dk/src/main.rs
Expand Up @@ -95,7 +95,7 @@ static mut PROCESSES: [Option<kernel::Process<'static>>; NUM_PROCS] = [None];

/// Supported drivers by the platform
pub struct Platform {
ble_radio: &'static nrf5x::ble_advertising_driver::BLE<
ble_radio: &'static capsules::ble_advertising_driver::BLE<
'static,
nrf51::radio::Radio,
VirtualMuxAlarm<'static, Rtc>,
Expand All @@ -121,7 +121,7 @@ impl kernel::Platform for Platform {
capsules::led::DRIVER_NUM => f(Some(self.led)),
capsules::button::DRIVER_NUM => f(Some(self.button)),
capsules::rng::DRIVER_NUM => f(Some(self.rng)),
nrf5x::ble_advertising_driver::DRIVER_NUM => f(Some(self.ble_radio)),
capsules::ble_advertising_driver::DRIVER_NUM => f(Some(self.ble_radio)),
capsules::temperature::DRIVER_NUM => f(Some(self.temp)),
_ => f(None),
}
Expand Down Expand Up @@ -291,24 +291,24 @@ pub unsafe fn reset_handler() {
nrf5x::trng::TRNG.set_client(rng);

let ble_radio = static_init!(
nrf5x::ble_advertising_driver::BLE<
capsules::ble_advertising_driver::BLE<
'static,
nrf51::radio::Radio,
VirtualMuxAlarm<'static, Rtc>,
>,
nrf5x::ble_advertising_driver::BLE::new(
capsules::ble_advertising_driver::BLE::new(
&mut nrf51::radio::RADIO,
kernel::Grant::create(),
&mut nrf5x::ble_advertising_driver::BUF,
&mut capsules::ble_advertising_driver::BUF,
ble_radio_virtual_alarm
),
256 / 8
);
nrf5x::ble_advertising_hil::BleAdvertisementDriver::set_receive_client(
kernel::hil::ble_advertising::BleAdvertisementDriver::set_receive_client(
&nrf51::radio::RADIO,
ble_radio,
);
nrf5x::ble_advertising_hil::BleAdvertisementDriver::set_transmit_client(
kernel::hil::ble_advertising::BleAdvertisementDriver::set_transmit_client(
&nrf51::radio::RADIO,
ble_radio,
);
Expand Down
14 changes: 7 additions & 7 deletions boards/nrf52dk/src/main.rs
Expand Up @@ -110,7 +110,7 @@ static mut PROCESSES: [Option<kernel::Process<'static>>; NUM_PROCS] = [None, Non

/// Supported drivers by the platform
pub struct Platform {
ble_radio: &'static nrf5x::ble_advertising_driver::BLE<
ble_radio: &'static capsules::ble_advertising_driver::BLE<
'static,
nrf52::radio::Radio,
VirtualMuxAlarm<'static, Rtc>,
Expand Down Expand Up @@ -140,7 +140,7 @@ impl kernel::Platform for Platform {
capsules::led::DRIVER_NUM => f(Some(self.led)),
capsules::button::DRIVER_NUM => f(Some(self.button)),
capsules::rng::DRIVER_NUM => f(Some(self.rng)),
nrf5x::ble_advertising_driver::DRIVER_NUM => f(Some(self.ble_radio)),
capsules::ble_advertising_driver::DRIVER_NUM => f(Some(self.ble_radio)),
capsules::temperature::DRIVER_NUM => f(Some(self.temp)),
kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)),
_ => f(None),
Expand Down Expand Up @@ -300,23 +300,23 @@ pub unsafe fn reset_handler() {
kernel::debug::assign_console_driver(Some(console), kc);

let ble_radio = static_init!(
nrf5x::ble_advertising_driver::BLE<
capsules::ble_advertising_driver::BLE<
'static,
nrf52::radio::Radio,
VirtualMuxAlarm<'static, Rtc>,
>,
nrf5x::ble_advertising_driver::BLE::new(
capsules::ble_advertising_driver::BLE::new(
&mut nrf52::radio::RADIO,
kernel::Grant::create(),
&mut nrf5x::ble_advertising_driver::BUF,
&mut capsules::ble_advertising_driver::BUF,
ble_radio_virtual_alarm
)
);
nrf5x::ble_advertising_hil::BleAdvertisementDriver::set_receive_client(
kernel::hil::ble_advertising::BleAdvertisementDriver::set_receive_client(
&nrf52::radio::RADIO,
ble_radio,
);
nrf5x::ble_advertising_hil::BleAdvertisementDriver::set_transmit_client(
kernel::hil::ble_advertising::BleAdvertisementDriver::set_transmit_client(
&nrf52::radio::RADIO,
ble_radio,
);
Expand Down
3 changes: 2 additions & 1 deletion capsules/README.md
Expand Up @@ -52,7 +52,8 @@ Support for wireless radios.
- **[nRF51822 Serialization](src/nrf51822_serialization.rs)**: Kernel support
for using the nRF51 serialization library.
- **[RF233](src/rf233.rs)**: Driver for RF233 radio.

- **[BLE Advertising](src/ble_advertising_driver.rs)**: Driver for sending BLE
advertisements.

### Libraries

Expand Down
Expand Up @@ -195,11 +195,11 @@
//! * Fredrik Nilsson <frednils@student.chalmers.se>
//! * Date: June 22, 2017

use ble_advertising_hil;
use ble_advertising_hil::RadioChannel;
use core::cell::Cell;
use core::cmp;
use kernel;
use kernel::hil::ble_advertising;
use kernel::hil::ble_advertising::RadioChannel;
use kernel::hil::time::Frequency;
use kernel::returncode::ReturnCode;

Expand Down Expand Up @@ -536,7 +536,7 @@ impl App {

fn send_advertisement<'a, B, A>(&self, ble: &BLE<'a, B, A>, channel: RadioChannel) -> ReturnCode
where
B: ble_advertising_hil::BleAdvertisementDriver + ble_advertising_hil::BleConfig + 'a,
B: ble_advertising::BleAdvertisementDriver + ble_advertising::BleConfig + 'a,
A: kernel::hil::time::Alarm + 'a,
{
self.advertisement_buf
Expand Down Expand Up @@ -587,7 +587,7 @@ impl App {

pub struct BLE<'a, B, A>
where
B: ble_advertising_hil::BleAdvertisementDriver + ble_advertising_hil::BleConfig + 'a,
B: ble_advertising::BleAdvertisementDriver + ble_advertising::BleConfig + 'a,
A: kernel::hil::time::Alarm + 'a,
{
radio: &'a B,
Expand All @@ -601,7 +601,7 @@ where

impl<'a, B, A> BLE<'a, B, A>
where
B: ble_advertising_hil::BleAdvertisementDriver + ble_advertising_hil::BleConfig + 'a,
B: ble_advertising::BleAdvertisementDriver + ble_advertising::BleConfig + 'a,
A: kernel::hil::time::Alarm + 'a,
{
pub fn new(
Expand Down Expand Up @@ -653,7 +653,7 @@ where
// Timer alarm
impl<'a, B, A> kernel::hil::time::Client for BLE<'a, B, A>
where
B: ble_advertising_hil::BleAdvertisementDriver + ble_advertising_hil::BleConfig + 'a,
B: ble_advertising::BleAdvertisementDriver + ble_advertising::BleConfig + 'a,
A: kernel::hil::time::Alarm + 'a,
{
// When an alarm is fired, we find which apps have expired timers. Expired
Expand Down Expand Up @@ -718,9 +718,9 @@ where
}

// Callback from the radio once a RX event occur
impl<'a, B, A> ble_advertising_hil::RxClient for BLE<'a, B, A>
impl<'a, B, A> ble_advertising::RxClient for BLE<'a, B, A>
where
B: ble_advertising_hil::BleAdvertisementDriver + ble_advertising_hil::BleConfig + 'a,
B: ble_advertising::BleAdvertisementDriver + ble_advertising::BleConfig + 'a,
A: kernel::hil::time::Alarm + 'a,
{
fn receive_event(&self, buf: &'static mut [u8], len: u8, result: ReturnCode) {
Expand Down Expand Up @@ -787,9 +787,9 @@ where
}

// Callback from the radio once a TX event occur
impl<'a, B, A> ble_advertising_hil::TxClient for BLE<'a, B, A>
impl<'a, B, A> ble_advertising::TxClient for BLE<'a, B, A>
where
B: ble_advertising_hil::BleAdvertisementDriver + ble_advertising_hil::BleConfig + 'a,
B: ble_advertising::BleAdvertisementDriver + ble_advertising::BleConfig + 'a,
A: kernel::hil::time::Alarm + 'a,
{
// The ReturnCode indicates valid CRC or not, not used yet but could be used for
Expand Down Expand Up @@ -831,7 +831,7 @@ where
// System Call implementation
impl<'a, B, A> kernel::Driver for BLE<'a, B, A>
where
B: ble_advertising_hil::BleAdvertisementDriver + ble_advertising_hil::BleConfig + 'a,
B: ble_advertising::BleAdvertisementDriver + ble_advertising::BleConfig + 'a,
A: kernel::hil::time::Alarm + 'a,
{
fn command(
Expand Down
1 change: 1 addition & 0 deletions capsules/src/lib.rs
Expand Up @@ -10,6 +10,7 @@ pub mod test;

pub mod alarm;
pub mod ambient_light;
pub mod ble_advertising_driver;
pub mod button;
pub mod console;
pub mod fm25cl;
Expand Down
15 changes: 8 additions & 7 deletions chips/nrf51/src/radio.rs
Expand Up @@ -15,8 +15,9 @@ use core::convert::TryFrom;
use kernel;
use kernel::ReturnCode;
use kernel::common::VolatileCell;
use kernel::hil::ble_advertising;
use kernel::hil::ble_advertising::RadioChannel;
use nrf5x;
use nrf5x::ble_advertising_hil::RadioChannel;
use nrf5x::constants::TxPower;

const RADIO_BASE: usize = 0x40001000;
Expand Down Expand Up @@ -114,8 +115,8 @@ pub struct RadioRegisters {
pub struct Radio {
regs: *const RadioRegisters,
tx_power: Cell<TxPower>,
rx_client: Cell<Option<&'static nrf5x::ble_advertising_hil::RxClient>>,
tx_client: Cell<Option<&'static nrf5x::ble_advertising_hil::TxClient>>,
rx_client: Cell<Option<&'static ble_advertising::RxClient>>,
tx_client: Cell<Option<&'static ble_advertising::TxClient>>,
}

impl Radio {
Expand Down Expand Up @@ -341,7 +342,7 @@ impl Radio {
}
}

impl nrf5x::ble_advertising_hil::BleAdvertisementDriver for Radio {
impl ble_advertising::BleAdvertisementDriver for Radio {
fn transmit_advertisement(
&self,
buf: &'static mut [u8],
Expand All @@ -361,16 +362,16 @@ impl nrf5x::ble_advertising_hil::BleAdvertisementDriver for Radio {
self.enable_interrupts();
}

fn set_receive_client(&self, client: &'static nrf5x::ble_advertising_hil::RxClient) {
fn set_receive_client(&self, client: &'static ble_advertising::RxClient) {
self.rx_client.set(Some(client));
}

fn set_transmit_client(&self, client: &'static nrf5x::ble_advertising_hil::TxClient) {
fn set_transmit_client(&self, client: &'static ble_advertising::TxClient) {
self.tx_client.set(Some(client));
}
}

impl nrf5x::ble_advertising_hil::BleConfig for Radio {
impl ble_advertising::BleConfig for Radio {
// The BLE Advertising Driver validates that the `tx_power` is between -20 to 10 dBm but then
// underlying chip must validate if the current `tx_power` is supported as well
fn set_tx_power(&self, tx_power: u8) -> kernel::ReturnCode {
Expand Down
15 changes: 8 additions & 7 deletions chips/nrf52/src/radio.rs
Expand Up @@ -37,8 +37,9 @@ use core::cell::Cell;
use core::convert::TryFrom;
use kernel;
use kernel::ReturnCode;
use kernel::hil::ble_advertising;
use kernel::hil::ble_advertising::RadioChannel;
use nrf5x;
use nrf5x::ble_advertising_hil::RadioChannel;
use nrf5x::constants::TxPower;
use peripheral_registers;

Expand All @@ -54,8 +55,8 @@ static mut PAYLOAD: [u8; nrf5x::constants::RADIO_PAYLOAD_LENGTH] =
pub struct Radio {
regs: *const peripheral_registers::RADIO,
tx_power: Cell<TxPower>,
rx_client: Cell<Option<&'static nrf5x::ble_advertising_hil::RxClient>>,
tx_client: Cell<Option<&'static nrf5x::ble_advertising_hil::TxClient>>,
rx_client: Cell<Option<&'static ble_advertising::RxClient>>,
tx_client: Cell<Option<&'static ble_advertising::TxClient>>,
}

pub static mut RADIO: Radio = Radio::new();
Expand Down Expand Up @@ -319,7 +320,7 @@ impl Radio {
}
}

impl nrf5x::ble_advertising_hil::BleAdvertisementDriver for Radio {
impl ble_advertising::BleAdvertisementDriver for Radio {
fn transmit_advertisement(
&self,
buf: &'static mut [u8],
Expand All @@ -339,16 +340,16 @@ impl nrf5x::ble_advertising_hil::BleAdvertisementDriver for Radio {
self.enable_interrupts();
}

fn set_receive_client(&self, client: &'static nrf5x::ble_advertising_hil::RxClient) {
fn set_receive_client(&self, client: &'static ble_advertising::RxClient) {
self.rx_client.set(Some(client));
}

fn set_transmit_client(&self, client: &'static nrf5x::ble_advertising_hil::TxClient) {
fn set_transmit_client(&self, client: &'static ble_advertising::TxClient) {
self.tx_client.set(Some(client));
}
}

impl nrf5x::ble_advertising_hil::BleConfig for Radio {
impl ble_advertising::BleConfig for Radio {
// The BLE Advertising Driver validates that the `tx_power` is between -20 to 10 dBm but then
// underlying chip must validate if the current `tx_power` is supported as well
fn set_tx_power(&self, tx_power: u8) -> kernel::ReturnCode {
Expand Down
2 changes: 0 additions & 2 deletions chips/nrf5x/src/lib.rs
Expand Up @@ -8,8 +8,6 @@ extern crate kernel;
mod peripheral_registers;

pub mod aes;
pub mod ble_advertising_driver;
pub mod ble_advertising_hil;
pub mod gpio;
pub mod peripheral_interrupts;
pub mod pinmux;
Expand Down
Expand Up @@ -47,7 +47,7 @@
//!
//! ```

use kernel::ReturnCode;
use returncode::ReturnCode;

pub trait BleAdvertisementDriver {
fn transmit_advertisement(
Expand Down
1 change: 1 addition & 0 deletions kernel/src/hil/mod.rs
Expand Up @@ -18,6 +18,7 @@ pub mod gpio_async;
pub mod dac;
pub mod nonvolatile_storage;
pub mod usb;
pub mod ble_advertising;

/// Shared interface for configuring components.
pub trait Controller {
Expand Down

0 comments on commit 66f676d

Please sign in to comment.