Skip to content

Commit 0f09d5b

Browse files
arkaptherealprof
authored andcommitted
Spi interrupt config (#82)
* add functions to enable and disable spi interrupts Signed-off-by: Arne Kappen <[email protected]>
1 parent 4fff729 commit 0f09d5b

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Added
1111

12+
- API to enable and disable SPI interrupts
13+
1214
- Hal ADC supporting one-shot and sequence conversion of regular channels.
15+
1316
- Implement IndependentWatchdog for the IWDG peripheral
1417

1518
## [v0.3.0] - 2019-01-14

src/spi.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,16 @@ pins! {
705705
MOSI: [PC1<Alternate<AF5>>]
706706
}
707707

708+
/// Interrupt events
709+
pub enum Event {
710+
/// New data has been received
711+
Rxne,
712+
/// Data can be sent
713+
Txe,
714+
/// An error occurred
715+
Error,
716+
}
717+
708718
#[derive(Debug)]
709719
pub struct Spi<SPI, PINS> {
710720
spi: SPI,
@@ -779,6 +789,48 @@ macro_rules! hal {
779789
Spi { spi, pins }
780790
}
781791

792+
/// Enable interrupts for the given `event`:
793+
/// - Received data ready to be read (RXNE)
794+
/// - Transmit data register empty (TXE)
795+
/// - Transfer error
796+
pub fn listen(&mut self, event: Event) {
797+
match event {
798+
Event::Rxne => self.spi.cr2.modify(|_, w| { w.rxneie().set_bit() }),
799+
Event::Txe => self.spi.cr2.modify(|_, w| { w.txeie().set_bit() }),
800+
Event::Error => self.spi.cr2.modify(|_, w| { w.errie().set_bit() }),
801+
}
802+
}
803+
804+
/// Disable interrupts for the given `event`:
805+
/// - Received data ready to be read (RXNE)
806+
/// - Transmit data register empty (TXE)
807+
/// - Transfer error
808+
pub fn unlisten(&mut self, event: Event) {
809+
match event {
810+
Event::Rxne => self.spi.cr2.modify(|_, w| { w.rxneie().clear_bit() }),
811+
Event::Txe => self.spi.cr2.modify(|_, w| { w.txeie().clear_bit() }),
812+
Event::Error => self.spi.cr2.modify(|_, w| { w.errie().clear_bit() }),
813+
}
814+
}
815+
816+
/// Return `true` if the TXE flag is set, i.e. new data to transmit
817+
/// can be written to the SPI.
818+
pub fn is_txe(&self) -> bool {
819+
self.spi.sr.read().txe().bit_is_set()
820+
}
821+
822+
/// Return `true` if the RXNE flag is set, i.e. new data has been received
823+
/// and can be read from the SPI.
824+
pub fn is_rxne(&self) -> bool {
825+
self.spi.sr.read().rxne().bit_is_set()
826+
}
827+
828+
/// Return `true` if the MODF flag is set, i.e. the SPI has experienced a
829+
/// Master Mode Fault. (see chapter 28.3.10 of the STM32F4 Reference Manual)
830+
pub fn is_modf(&self) -> bool {
831+
self.spi.sr.read().modf().bit_is_set()
832+
}
833+
782834
pub fn free(self) -> ($SPIX, PINS) {
783835
(self.spi, self.pins)
784836
}

0 commit comments

Comments
 (0)