Skip to content

Commit

Permalink
Remove &dyn references for some GPIO-related capsules and apply it to…
Browse files Browse the repository at this point in the history
… imix, hail, nucleo_f429zi.
  • Loading branch information
gendx committed May 5, 2020
1 parent 903b90c commit 129f992
Show file tree
Hide file tree
Showing 13 changed files with 370 additions and 263 deletions.
67 changes: 47 additions & 20 deletions boards/components/src/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,28 @@
//! `FloatingState::None` will be used when the board provides external pull-up/pull-down
//! resistors.

use capsules::button::Button;
use core::mem::MaybeUninit;
use kernel::capabilities;
use kernel::component::Component;
use kernel::create_capability;
use kernel::static_init;
use kernel::hil::gpio;
use kernel::hil::gpio::InterruptWithValue;
use kernel::static_init_half;

#[macro_export]
macro_rules! button_component_helper {
($(($P:expr, $M:expr, $F:expr)),+ ) => {{
($Pin:ty, $(($P:expr, $M:expr, $F:expr)),+ ) => {{
use kernel::static_init;
use kernel::count_expressions;
use kernel::hil::gpio::InterruptValueWrapper;
const NUM_BUTTONS: usize = count_expressions!($($P),+);

static_init!(
[(&'static dyn kernel::hil::gpio::InterruptValuePin, kernel::hil::gpio::ActivationMode, kernel::hil::gpio::FloatingState); NUM_BUTTONS],
[(&'static InterruptValueWrapper<'static, $Pin>, kernel::hil::gpio::ActivationMode, kernel::hil::gpio::FloatingState); NUM_BUTTONS],
[
$(
(static_init!(InterruptValueWrapper, InterruptValueWrapper::new($P))
(static_init!(InterruptValueWrapper<$Pin>, InterruptValueWrapper::new($P))
.finalize(),
$M,
$F
Expand All @@ -45,33 +49,56 @@ macro_rules! button_component_helper {
};};
}

pub struct ButtonComponent {
#[macro_export]
macro_rules! button_component_buf {
($Pin:ty) => {{
use capsules::button::Button;
use core::mem::MaybeUninit;
static mut BUF: MaybeUninit<Button<'static, $Pin>> = MaybeUninit::uninit();
&mut BUF
};};
}

pub struct ButtonComponent<IP: 'static + gpio::InterruptPin> {
board_kernel: &'static kernel::Kernel,
button_pins: &'static [(
&'static gpio::InterruptValueWrapper<'static, IP>,
gpio::ActivationMode,
gpio::FloatingState,
)],
}

impl ButtonComponent {
pub fn new(board_kernel: &'static kernel::Kernel) -> ButtonComponent {
ButtonComponent {
impl<IP: 'static + gpio::InterruptPin> ButtonComponent<IP> {
pub fn new(
board_kernel: &'static kernel::Kernel,
button_pins: &'static [(
&'static gpio::InterruptValueWrapper<'static, IP>,
gpio::ActivationMode,
gpio::FloatingState,
)],
) -> Self {
Self {
board_kernel: board_kernel,
button_pins,
}
}
}

impl Component for ButtonComponent {
type StaticInput = &'static [(
&'static dyn kernel::hil::gpio::InterruptValuePin,
kernel::hil::gpio::ActivationMode,
kernel::hil::gpio::FloatingState,
)];
type Output = &'static capsules::button::Button<'static>;
impl<IP: 'static + gpio::InterruptPin> Component for ButtonComponent<IP> {
type StaticInput = &'static mut MaybeUninit<Button<'static, IP>>;
type Output = &'static Button<'static, IP>;

unsafe fn finalize(self, button_pins: Self::StaticInput) -> Self::Output {
unsafe fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
let grant_cap = create_capability!(capabilities::MemoryAllocationCapability);
let button = static_init!(
capsules::button::Button<'static>,
capsules::button::Button::new(button_pins, self.board_kernel.create_grant(&grant_cap))
let button = static_init_half!(
static_buffer,
capsules::button::Button<'static, IP>,
capsules::button::Button::new(
self.button_pins,
self.board_kernel.create_grant(&grant_cap)
)
);
for (pin, _, _) in button_pins.iter() {
for (pin, _, _) in self.button_pins.iter() {
pin.set_client(button);
}

Expand Down
52 changes: 36 additions & 16 deletions boards/components/src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,54 +17,74 @@
//! ));
//! ```

use capsules::gpio::GPIO;
use core::mem::MaybeUninit;
use kernel::capabilities;
use kernel::component::Component;
use kernel::create_capability;
use kernel::static_init;
use kernel::hil::gpio;
use kernel::hil::gpio::InterruptWithValue;
use kernel::static_init_half;

#[macro_export]
macro_rules! gpio_component_helper {
($($P:expr),+ ) => {{
($Pin:ty, $($P:expr),+ ) => {{
use kernel::static_init;
use kernel::count_expressions;
use kernel::hil::gpio::InterruptValueWrapper;
const NUM_PINS: usize = count_expressions!($($P),+);

static_init!(
[&'static dyn kernel::hil::gpio::InterruptValuePin; NUM_PINS],
[&'static InterruptValueWrapper<'static, $Pin>; NUM_PINS],
[
$(
static_init!(InterruptValueWrapper, InterruptValueWrapper::new($P))
static_init!(InterruptValueWrapper<$Pin>, InterruptValueWrapper::new($P))
.finalize(),
)*
]
)
};};
}

pub struct GpioComponent {
#[macro_export]
macro_rules! gpio_component_buf {
($Pin:ty) => {{
use capsules::gpio::GPIO;
use core::mem::MaybeUninit;
static mut BUF: MaybeUninit<GPIO<'static, $Pin>> = MaybeUninit::uninit();
&mut BUF
};};
}

pub struct GpioComponent<IP: 'static + gpio::InterruptPin> {
board_kernel: &'static kernel::Kernel,
gpio_pins: &'static [&'static gpio::InterruptValueWrapper<'static, IP>],
}

impl GpioComponent {
pub fn new(board_kernel: &'static kernel::Kernel) -> GpioComponent {
GpioComponent {
impl<IP: 'static + gpio::InterruptPin> GpioComponent<IP> {
pub fn new(
board_kernel: &'static kernel::Kernel,
gpio_pins: &'static [&'static gpio::InterruptValueWrapper<'static, IP>],
) -> Self {
Self {
board_kernel: board_kernel,
gpio_pins,
}
}
}

impl Component for GpioComponent {
type StaticInput = &'static [&'static dyn kernel::hil::gpio::InterruptValuePin];
type Output = &'static capsules::gpio::GPIO<'static>;
impl<IP: 'static + gpio::InterruptPin> Component for GpioComponent<IP> {
type StaticInput = &'static mut MaybeUninit<GPIO<'static, IP>>;
type Output = &'static GPIO<'static, IP>;

unsafe fn finalize(self, gpio_pins: Self::StaticInput) -> Self::Output {
unsafe fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
let grant_cap = create_capability!(capabilities::MemoryAllocationCapability);
let gpio = static_init!(
capsules::gpio::GPIO<'static>,
capsules::gpio::GPIO::new(gpio_pins, self.board_kernel.create_grant(&grant_cap))
let gpio = static_init_half!(
static_buffer,
GPIO<'static, IP>,
GPIO::new(self.gpio_pins, self.board_kernel.create_grant(&grant_cap))
);
for pin in gpio_pins.iter() {
for pin in self.gpio_pins.iter() {
pin.set_client(gpio);
}

Expand Down
42 changes: 26 additions & 16 deletions boards/components/src/led.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,21 @@
//! ));
//! ```

use capsules;
use capsules::led::LED;
use core::mem::MaybeUninit;
use kernel::component::Component;
use kernel::static_init;
use kernel::static_init_half;

#[macro_export]
macro_rules! led_component_helper {
($($P:expr),+ ) => {{
($Pin:ty, $($P:expr),+ ) => {{
use kernel::count_expressions;
use kernel::static_init;
const NUM_LEDS: usize = count_expressions!($($P),+);

static_init!(
[(
&'static dyn kernel::hil::gpio::Pin,
&'static $Pin,
kernel::hil::gpio::ActivationMode
); NUM_LEDS],
[
Expand All @@ -33,22 +34,31 @@ macro_rules! led_component_helper {
};};
}

pub struct LedsComponent {}
#[macro_export]
macro_rules! led_component_buf {
($Pin:ty) => {{
use capsules::led::LED;
use core::mem::MaybeUninit;
static mut BUF: MaybeUninit<LED<'static, $Pin>> = MaybeUninit::uninit();
&mut BUF
};};
}

pub struct LedsComponent<P: 'static + kernel::hil::gpio::Pin> {
pins: &'static [(&'static P, kernel::hil::gpio::ActivationMode)],
}

impl LedsComponent {
pub fn new() -> LedsComponent {
LedsComponent {}
impl<P: 'static + kernel::hil::gpio::Pin> LedsComponent<P> {
pub fn new(pins: &'static [(&'static P, kernel::hil::gpio::ActivationMode)]) -> Self {
Self { pins }
}
}

impl Component for LedsComponent {
type StaticInput = &'static [(
&'static dyn kernel::hil::gpio::Pin,
kernel::hil::gpio::ActivationMode,
)];
type Output = &'static capsules::led::LED<'static>;
impl<P: 'static + kernel::hil::gpio::Pin> Component for LedsComponent<P> {
type StaticInput = &'static mut MaybeUninit<LED<'static, P>>;
type Output = &'static LED<'static, P>;

unsafe fn finalize(self, pins: Self::StaticInput) -> Self::Output {
static_init!(capsules::led::LED<'static>, capsules::led::LED::new(pins))
unsafe fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
static_init_half!(static_buffer, LED<'static, P>, LED::new(self.pins))
}
}
34 changes: 23 additions & 11 deletions boards/components/src/panic_button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,49 @@
//! ```

use capsules::panic_button::PanicButton;
use core::mem::MaybeUninit;
use kernel::component::Component;
use kernel::hil::gpio;
use kernel::static_init;
use kernel::static_init_half;

pub struct PanicButtonComponent<'a> {
pin: &'a dyn gpio::InterruptPin,
#[macro_export]
macro_rules! panic_button_component_buf {
($Pin:ty) => {{
use capsules::button::PanicButton;
use core::mem::MaybeUninit;
static mut BUF: MaybeUninit<PanicButton<'static, $Pin>> = MaybeUninit::uninit();
&mut BUF
};};
}

pub struct PanicButtonComponent<'a, IP: gpio::InterruptPin> {
pin: &'a IP,
mode: gpio::ActivationMode,
floating_state: gpio::FloatingState,
}

impl<'a> PanicButtonComponent<'a> {
impl<'a, IP: gpio::InterruptPin> PanicButtonComponent<'a, IP> {
pub fn new(
pin: &'a dyn gpio::InterruptPin,
pin: &'a IP,
mode: gpio::ActivationMode,
floating_state: gpio::FloatingState,
) -> Self {
PanicButtonComponent {
Self {
pin,
mode,
floating_state,
}
}
}

impl Component for PanicButtonComponent<'static> {
type StaticInput = ();
impl<IP: 'static + gpio::InterruptPin> Component for PanicButtonComponent<'static, IP> {
type StaticInput = &'static mut MaybeUninit<PanicButton<'static, IP>>;
type Output = ();

unsafe fn finalize(self, _: Self::StaticInput) -> Self::Output {
let panic_button = static_init!(
PanicButton,
unsafe fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
let panic_button = static_init_half!(
static_buffer,
PanicButton<'static, IP>,
PanicButton::new(self.pin, self.mode, self.floating_state)
);
self.pin.set_client(panic_button);
Expand Down

0 comments on commit 129f992

Please sign in to comment.