Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow creating a Spi Peripheral #2085

Merged
merged 4 commits into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 40 additions & 2 deletions boards/components/src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@

use core::mem::MaybeUninit;

use capsules::spi::{Spi, DEFAULT_READ_BUF_LENGTH, DEFAULT_WRITE_BUF_LENGTH};
use capsules::spi_controller::{Spi, DEFAULT_READ_BUF_LENGTH, DEFAULT_WRITE_BUF_LENGTH};
use capsules::spi_peripheral::SpiPeripheral;
use capsules::virtual_spi::{MuxSpiMaster, VirtualSpiMasterDevice};
use kernel::component::Component;
use kernel::hil::spi;
Expand All @@ -47,7 +48,7 @@ macro_rules! spi_mux_component_helper {
#[macro_export]
macro_rules! spi_syscall_component_helper {
($S:ty) => {{
use capsules::spi::Spi;
use capsules::spi_controller::Spi;
use capsules::virtual_spi::VirtualSpiMasterDevice;
use core::mem::MaybeUninit;
static mut BUF1: MaybeUninit<VirtualSpiMasterDevice<'static, $S>> = MaybeUninit::uninit();
Expand All @@ -67,6 +68,16 @@ macro_rules! spi_component_helper {
};};
}

#[macro_export]
macro_rules! spi_peripheral_component_helper {
($S:ty) => {{
use capsules::spi_peripheral::SpiPeripheral;
use core::mem::MaybeUninit;
static mut BUF: MaybeUninit<SpiPeripheral<'static, $S>> = MaybeUninit::uninit();
&mut BUF
};};
}

pub struct SpiMuxComponent<S: 'static + spi::SpiMaster> {
spi: &'static S,
}
Expand Down Expand Up @@ -172,3 +183,30 @@ impl<S: 'static + spi::SpiMaster> Component for SpiComponent<S> {
spi_device
}
}

pub struct SpiPeripheralComponent<S: 'static + spi::SpiSlave> {
device: &'static S,
}

impl<S: 'static + spi::SpiSlave> SpiPeripheralComponent<S> {
pub fn new(device: &'static S) -> Self {
SpiPeripheralComponent { device }
}
}

impl<S: 'static + spi::SpiSlave + kernel::hil::spi::SpiSlaveDevice> Component
for SpiPeripheralComponent<S>
{
type StaticInput = &'static mut MaybeUninit<SpiPeripheral<'static, S>>;
type Output = &'static SpiPeripheral<'static, S>;

unsafe fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
let spi_device = static_init_half!(
static_buffer,
SpiPeripheral<'static, S>,
SpiPeripheral::new(self.device)
);

spi_device
}
}
7 changes: 5 additions & 2 deletions boards/hail/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ struct Hail {
temp: &'static capsules::temperature::TemperatureSensor<'static>,
ninedof: &'static capsules::ninedof::NineDof<'static>,
humidity: &'static capsules::humidity::HumiditySensor<'static>,
spi: &'static capsules::spi::Spi<'static, VirtualSpiMasterDevice<'static, sam4l::spi::SpiHw>>,
spi: &'static capsules::spi_controller::Spi<
'static,
VirtualSpiMasterDevice<'static, sam4l::spi::SpiHw>,
>,
nrf51822: &'static capsules::nrf51822_serialization::Nrf51822Serialization<'static>,
adc: &'static capsules::adc::Adc<'static, sam4l::adc::Adc>,
led: &'static capsules::led::LED<'static, sam4l::gpio::GPIOPin<'static>>,
Expand All @@ -80,7 +83,7 @@ impl Platform for Hail {
capsules::gpio::DRIVER_NUM => f(Some(self.gpio)),

capsules::alarm::DRIVER_NUM => f(Some(self.alarm)),
capsules::spi::DRIVER_NUM => f(Some(self.spi)),
capsules::spi_controller::DRIVER_NUM => f(Some(self.spi)),
capsules::nrf51822_serialization::DRIVER_NUM => f(Some(self.nrf51822)),
capsules::ambient_light::DRIVER_NUM => f(Some(self.ambient_light)),
capsules::adc::DRIVER_NUM => f(Some(self.adc)),
Expand Down
7 changes: 5 additions & 2 deletions boards/imix/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ struct Imix {
'static,
sam4l::acifc::Acifc<'static>,
>,
spi: &'static capsules::spi::Spi<'static, VirtualSpiMasterDevice<'static, sam4l::spi::SpiHw>>,
spi: &'static capsules::spi_controller::Spi<
'static,
VirtualSpiMasterDevice<'static, sam4l::spi::SpiHw>,
>,
ipc: kernel::ipc::IPC,
ninedof: &'static capsules::ninedof::NineDof<'static>,
radio_driver: &'static capsules::ieee802154::RadioDriver<'static>,
Expand Down Expand Up @@ -145,7 +148,7 @@ impl kernel::Platform for Imix {
capsules::console::DRIVER_NUM => f(Some(self.console)),
capsules::gpio::DRIVER_NUM => f(Some(self.gpio)),
capsules::alarm::DRIVER_NUM => f(Some(self.alarm)),
capsules::spi::DRIVER_NUM => f(Some(self.spi)),
capsules::spi_controller::DRIVER_NUM => f(Some(self.spi)),
capsules::adc::DRIVER_NUM => f(Some(self.adc)),
capsules::led::DRIVER_NUM => f(Some(self.led)),
capsules::button::DRIVER_NUM => f(Some(self.button)),
Expand Down
3 changes: 2 additions & 1 deletion capsules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ These capsules provide a `Driver` interface for common MCU peripherals.
- **[I2C_MASTER](src/i2c_master.rs)**: I2C master access only.
- **[I2C_MASTER_SLAVE](src/i2c_master_slave_driver.rs)**: I2C master and slave access.
- **[RNG](src/rng.rs)**: Random number generation.
- **[SPI](src/spi.rs)**: SPI master and slave.
- **[SPI Controller](src/spi_controller.rs)**: SPI controller device (SPI master)
- **[SPI Peripheral](src/spi_peripheral.rs)**: SPI peripheral device (SPI slave)


### Helpful Userspace Capsules
Expand Down
1 change: 1 addition & 0 deletions capsules/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub enum NUM {

// HW Buses
Spi = 0x20001,
SpiPeripheral = 0x20002,
I2cMaster = 0x20003,
UsbUser = 0x20005,
I2cMasterSlave = 0x20006,
Expand Down
3 changes: 2 additions & 1 deletion capsules/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ pub mod screen;
pub mod sdcard;
pub mod segger_rtt;
pub mod si7021;
pub mod spi;
pub mod spi_controller;
pub mod spi_peripheral;
pub mod st7735;
pub mod temperature;
pub mod touch;
Expand Down