use capsules_core::virtualizers::virtual_spi::{MuxSpiMaster, VirtualSpiMasterDevice};
use capsules_extra::l3gd20::L3gd20Spi;
use core::mem::MaybeUninit;
use kernel::capabilities;
use kernel::component::Component;
use kernel::create_capability;
use kernel::hil::spi;
use kernel::hil::spi::SpiMasterDevice;
#[macro_export]
macro_rules! l3gd20_component_static {
($S:ty $(,)?) => {{
let txbuffer = kernel::static_buf!([u8; capsules_extra::l3gd20::TX_BUF_LEN]);
let rxbuffer = kernel::static_buf!([u8; capsules_extra::l3gd20::RX_BUF_LEN]);
let spi = kernel::static_buf!(
capsules_core::virtualizers::virtual_spi::VirtualSpiMasterDevice<'static, $S>
);
let l3gd20spi = kernel::static_buf!(
capsules_extra::l3gd20::L3gd20Spi<
'static,
capsules_core::virtualizers::virtual_spi::VirtualSpiMasterDevice<'static, $S>,
>
);
(spi, l3gd20spi, txbuffer, rxbuffer)
};};
}
pub type L3gd20ComponentType<S> = capsules_extra::l3gd20::L3gd20Spi<'static, S>;
pub struct L3gd20Component<
S: 'static + spi::SpiMaster<'static>,
CS: spi::cs::IntoChipSelect<S::ChipSelect, spi::cs::ActiveLow>,
> {
spi_mux: &'static MuxSpiMaster<'static, S>,
chip_select: CS,
board_kernel: &'static kernel::Kernel,
driver_num: usize,
}
impl<
S: 'static + spi::SpiMaster<'static>,
CS: spi::cs::IntoChipSelect<S::ChipSelect, spi::cs::ActiveLow>,
> L3gd20Component<S, CS>
{
pub fn new(
spi_mux: &'static MuxSpiMaster<'static, S>,
chip_select: CS,
board_kernel: &'static kernel::Kernel,
driver_num: usize,
) -> Self {
Self {
spi_mux,
chip_select,
board_kernel,
driver_num,
}
}
}
impl<
S: 'static + spi::SpiMaster<'static>,
CS: spi::cs::IntoChipSelect<S::ChipSelect, spi::cs::ActiveLow>,
> Component for L3gd20Component<S, CS>
{
type StaticInput = (
&'static mut MaybeUninit<VirtualSpiMasterDevice<'static, S>>,
&'static mut MaybeUninit<L3gd20Spi<'static, VirtualSpiMasterDevice<'static, S>>>,
&'static mut MaybeUninit<[u8; capsules_extra::l3gd20::TX_BUF_LEN]>,
&'static mut MaybeUninit<[u8; capsules_extra::l3gd20::RX_BUF_LEN]>,
);
type Output = &'static L3gd20Spi<'static, VirtualSpiMasterDevice<'static, S>>;
fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
let grant_cap = create_capability!(capabilities::MemoryAllocationCapability);
let grant = self.board_kernel.create_grant(self.driver_num, &grant_cap);
let spi_device = static_buffer.0.write(VirtualSpiMasterDevice::new(
self.spi_mux,
self.chip_select.into_cs(),
));
spi_device.setup();
let txbuffer = static_buffer
.2
.write([0; capsules_extra::l3gd20::TX_BUF_LEN]);
let rxbuffer = static_buffer
.3
.write([0; capsules_extra::l3gd20::RX_BUF_LEN]);
let l3gd20 = static_buffer
.1
.write(L3gd20Spi::new(spi_device, txbuffer, rxbuffer, grant));
spi_device.set_client(l3gd20);
let _ = l3gd20.configure();
l3gd20
}
}