From 57c33b5892932f385ae9e617f830332876a92bab Mon Sep 17 00:00:00 2001 From: Brad Campbell Date: Tue, 8 Oct 2024 15:06:38 -0400 Subject: [PATCH] components: use ComponentTypes in component macros --- boards/components/src/alarm.rs | 10 ++++-- boards/components/src/led.rs | 6 ++-- boards/components/src/lib.rs | 20 +++++++++++ .../nrf52840dk-test-appid-sha256/src/main.rs | 33 +++++++------------ 4 files changed, 43 insertions(+), 26 deletions(-) diff --git a/boards/components/src/alarm.rs b/boards/components/src/alarm.rs index e224cd82f5..08c1e7d418 100644 --- a/boards/components/src/alarm.rs +++ b/boards/components/src/alarm.rs @@ -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, + >, > ); diff --git a/boards/components/src/led.rs b/boards/components/src/led.rs index 7b971e5e95..f0cdc88f4c 100644 --- a/boards/components/src/led.rs +++ b/boards/components/src/led.rs @@ -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) };}; } diff --git a/boards/components/src/lib.rs b/boards/components/src/lib.rs index 40640c5a66..8deece6a5d 100644 --- a/boards/components/src/lib.rs +++ b/boards/components/src/lib.rs @@ -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; @@ -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 = (); +} diff --git a/boards/configurations/nrf52840dk/nrf52840dk-test-appid-sha256/src/main.rs b/boards/configurations/nrf52840dk/nrf52840dk-test-appid-sha256/src/main.rs index 99c9d02d48..660da85722 100644 --- a/boards/configurations/nrf52840dk/nrf52840dk-test-appid-sha256/src/main.rs +++ b/boards/configurations/nrf52840dk/nrf52840dk-test-appid-sha256/src/main.rs @@ -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; @@ -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 ::AlarmDriver, + led: &'static >::LedDriver, + alarm: &'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>; + + type AlarmType = nrf52840::rtc::Rtc<'static>; + type AlarmDriver = components::alarm::AlarmDriverComponentType; + + type LedType = kernel::hil::led::LedLow<'static, nrf52840::gpio::GPIOPin<'static>>; + type LedDriver = components::led::LedsComponentType; } impl SyscallDriverLookup for Platform { @@ -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]), @@ -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