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

Add methods to select and deselect SPI slaves #258

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@

mod clock;
mod instances;
mod interrupts;
mod peripheral;

pub use self::{
clock::{Clock, ClockSource},
instances::Instance,
instances::{Instance, SlaveSelect},
interrupts::Interrupts,
peripheral::SPI,
};

Expand Down
47 changes: 43 additions & 4 deletions src/spi/instances.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use core::ops::Deref;

use crate::{
pac, swm,
pac::{self, spi0::TXCTL},
swm,
syscon::{self, clock_source::PeripheralClockSelector},
};

Expand All @@ -23,14 +24,28 @@ pub trait Instance:
type Miso;
}

/// Implemented for slave select functions of a given SPI instance
pub trait SlaveSelect<I> {
/// Select slave
///
/// Intended for internal use only.
fn select(_: &TXCTL);

/// Deselect slave
///
/// Intended for internal use only.
fn deselect(_: &TXCTL);
}

macro_rules! instances {
(
$(
$instance:ident,
$clock_num:expr,
$sck:ident,
$mosi:ident,
$miso:ident;
$miso:ident,
[$($ssel:ident, $ssel_reg:ident;)*];
)*
) => {
$(
Expand All @@ -45,13 +60,37 @@ macro_rules! instances {
impl PeripheralClockSelector for pac::$instance {
const REGISTER_NUM: usize = $clock_num;
}

$(
impl SlaveSelect<pac::$instance> for swm::$ssel {
fn select(txctl: &TXCTL) {
txctl.modify(|_, w| w.$ssel_reg().clear_bit());
}

fn deselect(txctl: &TXCTL) {
txctl.modify(|_, w| w.$ssel_reg().set_bit());
}
}
)*
)*
};
}

instances!(
SPI0, 9, SPI0_SCK, SPI0_MOSI, SPI0_MISO;
SPI1, 10, SPI1_SCK, SPI1_MOSI, SPI1_MISO;
SPI0, 9,
SPI0_SCK, SPI0_MOSI, SPI0_MISO,
[
SPI0_SSEL0, txssel0_n;
SPI0_SSEL1, txssel1_n;
SPI0_SSEL2, txssel2_n;
SPI0_SSEL3, txssel3_n;
];
SPI1, 10,
SPI1_SCK, SPI1_MOSI, SPI1_MISO,
[
SPI1_SSEL0, txssel0_n;
SPI1_SSEL1, txssel1_n;
];
);

mod private {
Expand Down
69 changes: 69 additions & 0 deletions src/spi/interrupts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use super::Instance;

macro_rules! interrupts {
(
$(
$doc:expr,
$field:ident,
$reg_field:ident;
)*
) => {
/// Used to enable or disable SPI interrupts
///
/// See [`SPI::enable_interrupts`] or [`SPI::disable_interrupts`].
///
/// [`SPI::enable_interrupts`]: struct.SPI.html#method.enable_interrupts
/// [`SPI::disable_interrupts`]: struct.SPI.html#method.disable_interrupts
pub struct Interrupts {
$(
#[doc = $doc]
pub $field: bool,
)*
}

impl Interrupts {
pub(super) fn enable<I: Instance>(&self, spi: &I) {
spi.intenset.modify(|_, w| {
$(
if self.$field {
w.$reg_field().set_bit();
}
)*

w
})
}

pub(super) fn disable<I: Instance>(&self, spi: &I) {
spi.intenclr.write(|w| {
$(
if self.$field {
w.$reg_field().set_bit();
}
)*

w
})
}
}

impl Default for Interrupts {
fn default() -> Self {
Self {
$(
$field: false,
)*
}
}
}
};
}

interrupts!(
"RX Ready", rx_ready, rxrdyen;
"TX Ready", tx_ready, txrdyen;
"Receiver Overrun", rx_overrun, rxoven;
"Transmitter Underrun", tx_underrun, txuren;
"Slave Select Asserted", slave_select_asserted, ssaen;
"Slave Select Deasserted", slave_select_deasserted, ssden;
);
51 changes: 50 additions & 1 deletion src/spi/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
syscon,
};

use super::{Clock, ClockSource, Instance};
use super::{Clock, ClockSource, Instance, Interrupts, SlaveSelect};

/// Interface to a SPI peripheral
///
Expand Down Expand Up @@ -124,6 +124,55 @@ impl<I> SPI<I, init_state::Enabled>
where
I: Instance,
{
/// Enable interrupts
///
/// Enables all interrupts set to `true` in `interrupts`. Interrupts set to
/// `false` are not affected.
pub fn enable_interrupts(&mut self, interrupts: Interrupts) {
interrupts.enable(&self.spi);
}

/// Disable interrupts
///
/// Disables all interrupts set to `true` in `interrupts`. Interrupts set to
/// `false` are not affected.
pub fn disable_interrupts(&mut self, interrupts: Interrupts) {
interrupts.disable(&self.spi);
}

/// Select slave
///
/// Selects the slave connected to the pin that the provided slave select
/// function is assigned to.
///
/// This API allows you the select multiple slave at once. Please take care
/// to only select slaves in a way that doesn't cause conflicts between
/// them.
///
/// All slave select lines for which the respective function has been
/// assigned to a pin are selected by default.
pub fn select_slave<F, P>(
&mut self,
_: &swm::Function<F, swm::state::Assigned<P>>,
) where
F: SlaveSelect<I>,
{
F::select(&self.spi.txctl);
}

/// Deselect slave
///
/// Deselects the slave connected to the pin that the provided slave select
/// function is assigned to.
pub fn deselect_slave<F, P>(
&mut self,
_: &swm::Function<F, swm::state::Assigned<P>>,
) where
F: SlaveSelect<I>,
{
F::deselect(&self.spi.txctl);
}

/// Disable the SPI peripheral
///
/// This method is only available, if `SPI` is in the [`Enabled`] state.
Expand Down