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 support for enabling/disabling SPI interrupts #256

Merged
merged 1 commit into from
Jul 21, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions 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,
interrupts::Interrupts,
peripheral::SPI,
};

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.write(|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;
);
18 changes: 17 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};

/// Interface to a SPI peripheral
///
Expand Down Expand Up @@ -124,6 +124,22 @@ 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);
}

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