Skip to content

Commit

Permalink
components: use ComponentTypes in component macros
Browse files Browse the repository at this point in the history
  • Loading branch information
bradjc committed Oct 8, 2024
1 parent 98638e3 commit 57c33b5
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 26 deletions.
10 changes: 8 additions & 2 deletions boards/components/src/alarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,18 @@ macro_rules! alarm_mux_component_static {
macro_rules! alarm_component_static {
($A:ty $(,)?) => {{
let mux_alarm = kernel::static_buf!(
capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm<'static, $A>
capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm<
'static,
<$A as crate::ComponentTypes>::AlarmType,
>
);
let alarm_driver = kernel::static_buf!(
capsules_core::alarm::AlarmDriver<
'static,
capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm<'static, $A>,
capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm<
'static,
<$A as crate::ComponentTypes>::AlarmType,
>,
>
);

Expand Down
6 changes: 3 additions & 3 deletions boards/components/src/led.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ macro_rules! led_component_static {
use kernel::static_init;
const NUM_LEDS: usize = count_expressions!($($L),+);
let arr = static_init!(
[&'static $Led; NUM_LEDS],
[&'static <$Led as crate::ComponentTypes>::LedType; NUM_LEDS],
[
$(
static_init!(
$Led,
<$Led as crate::ComponentTypes>::LedType,
$L
)
),+
]
);

let led = kernel::static_buf!( capsules_core::led::LedDriver<'static, $Led, NUM_LEDS>);
let led = kernel::static_buf!( capsules_core::led::LedDriver<'static, <$Led as crate::ComponentTypes>::LedType, NUM_LEDS>);
(led, arr)
};};
}
Expand Down
20 changes: 20 additions & 0 deletions boards/components/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

#![feature(associated_type_defaults)]
#![no_std]

pub mod adc;
Expand Down Expand Up @@ -96,3 +97,22 @@ pub mod touch;
pub mod udp_driver;
pub mod udp_mux;
pub mod usb;

use kernel::hil::led::Led;
use kernel::hil::time::Alarm;
use kernel::platform::chip::Chip;
use kernel::process::Process;
use kernel::syscall::SyscallDriver;

pub trait ComponentTypes<'a> {
type ChipType: Chip;
type ProcessType: Process;

type AlarmType: Alarm<'a>;
type AlarmDriver: SyscallDriver = ();

type ButtonDriver: SyscallDriver = ();

type LedType: Led;
type LedDriver: SyscallDriver = ();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@

use core::ptr::{addr_of, addr_of_mut};

use components::ComponentTypes;
use kernel::component::Component;
use kernel::hil::led::LedLow;
use kernel::hil::time::Counter;
use kernel::platform::chip::Chip;
use kernel::platform::{KernelResources, SyscallDriverLookup};
use kernel::process::Process;
use kernel::scheduler::round_robin::RoundRobinSched;
use kernel::syscall::SyscallDriver;
use kernel::{capabilities, create_capability, static_init};
use nrf52840::gpio::Pin;
use nrf52840::interrupt_service::Nrf52840DefaultPeripherals;
Expand Down Expand Up @@ -72,34 +70,27 @@ pub static mut STACK_MEMORY: [u8; 0x2000] = [0; 0x2000];
// kernel::process::ProcessStandardDebugFull,
// >;

trait ComponentTypes {
type ChipType: Chip;
type ProcessType: Process;

type AlarmDriver: SyscallDriver = ();
type ButtonDriver: SyscallDriver = ();
}

/// Supported drivers by the platform
pub struct Platform {
console: &'static capsules_core::console::Console<'static>,
led: &'static capsules_core::led::LedDriver<
'static,
kernel::hil::led::LedLow<'static, nrf52840::gpio::GPIOPin<'static>>,
4,
>,
alarm: &'static <Platform as ComponentTypes>::AlarmDriver,
led: &'static <Platform as ComponentTypes<'static>>::LedDriver,
alarm: &'static <Platform as ComponentTypes<'static>>::AlarmDriver,
scheduler: &'static RoundRobinSched<'static>,
systick: cortexm4::systick::SysTick,
}

impl ComponentTypes for Platform {
impl ComponentTypes<'static> for Platform {
type ChipType = nrf52840::chip::NRF52<'static, Nrf52840DefaultPeripherals<'static>>;
type ProcessType = kernel::process::ProcessStandard<
'static,
nrf52840::chip::NRF52<'static, Nrf52840DefaultPeripherals<'static>>,
>;
type AlarmDriver = components::alarm::AlarmDriverComponentType<nrf52840::rtc::Rtc<'static>>;

type AlarmType = nrf52840::rtc::Rtc<'static>;
type AlarmDriver = components::alarm::AlarmDriverComponentType<Self::AlarmType>;

type LedType = kernel::hil::led::LedLow<'static, nrf52840::gpio::GPIOPin<'static>>;
type LedDriver = components::led::LedsComponentType<Self::LedType, 4>;
}

impl SyscallDriverLookup for Platform {
Expand Down Expand Up @@ -225,7 +216,7 @@ pub unsafe fn main() {
//--------------------------------------------------------------------------

let led = components::led::LedsComponent::new().finalize(components::led_component_static!(
LedLow<'static, nrf52840::gpio::GPIOPin>,
Platform,
LedLow::new(&nrf52840_peripherals.gpio_port[LED1_PIN]),
LedLow::new(&nrf52840_peripherals.gpio_port[LED2_PIN]),
LedLow::new(&nrf52840_peripherals.gpio_port[LED3_PIN]),
Expand All @@ -245,7 +236,7 @@ pub unsafe fn main() {
capsules_core::alarm::DRIVER_NUM,
mux_alarm,
)
.finalize(components::alarm_component_static!(nrf52840::rtc::Rtc));
.finalize(components::alarm_component_static!(Platform));

//--------------------------------------------------------------------------
// UART & CONSOLE & DEBUG
Expand Down

0 comments on commit 57c33b5

Please sign in to comment.