Skip to content

Commit 2a3f51b

Browse files
committed
Serial: wrap interrupt functions in new traits
this allows consumers to talk about the ISR functions of a `Serial`, `Rx` or `Tx` in a generic way, namely when using type parameters. this will be used in the DMA implementation in a next step (see follow-up commit).
1 parent a2457bf commit 2a3f51b

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

src/prelude.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ pub use crate::qei::QeiExt as _stm32f4xx_hal_QeiExt;
6868
pub use crate::rcc::RccExt as _stm32f4xx_hal_rcc_RccExt;
6969
#[cfg(all(feature = "device-selected", feature = "rng"))]
7070
pub use crate::rng::RngExt as _stm32f4xx_hal_rng_RngExt;
71+
pub use crate::serial::RxISR as _stm32f4xx_hal_serial_RxISR;
7172
pub use crate::serial::SerialExt as _stm32f4xx_hal_serial_SerialExt;
73+
pub use crate::serial::TxISR as _stm32f4xx_hal_serial_TxISR;
7274
pub use crate::spi::SpiExt as _stm32f4xx_hal_spi_SpiExt;
7375
pub use crate::syscfg::SysCfgExt as _stm32f4xx_hal_syscfg_SysCfgExt;
7476
pub use crate::time::U32Ext as _stm32f4xx_hal_time_U32Ext;

src/serial.rs

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -262,19 +262,33 @@ impl<USART: Instance, WORD> Rx<USART, WORD> {
262262
pub fn unlisten_idle(&mut self) {
263263
unsafe { (*USART::ptr()).cr1.modify(|_, w| w.idleie().clear_bit()) }
264264
}
265+
}
265266

267+
/// Trait for [`Rx`] interrupt handling.
268+
pub trait RxISR {
266269
/// Return true if the line idle status is set
267-
pub fn is_idle(&self) -> bool {
270+
fn is_idle(&self) -> bool;
271+
272+
/// Return true if the rx register is not empty (and can be read)
273+
fn is_rx_not_empty(&self) -> bool;
274+
275+
/// Clear idle line interrupt flag
276+
fn clear_idle_interrupt(&self);
277+
}
278+
279+
impl<USART: Instance, WORD> RxISR for Rx<USART, WORD> {
280+
/// Return true if the line idle status is set
281+
fn is_idle(&self) -> bool {
268282
unsafe { (*USART::ptr()).sr.read().idle().bit_is_set() }
269283
}
270284

271285
/// Return true if the rx register is not empty (and can be read)
272-
pub fn is_rx_not_empty(&self) -> bool {
286+
fn is_rx_not_empty(&self) -> bool {
273287
unsafe { (*USART::ptr()).sr.read().rxne().bit_is_set() }
274288
}
275289

276290
/// Clear idle line interrupt flag
277-
pub fn clear_idle_interrupt(&self) {
291+
fn clear_idle_interrupt(&self) {
278292
unsafe {
279293
let _ = (*USART::ptr()).sr.read();
280294
let _ = (*USART::ptr()).dr.read();
@@ -302,9 +316,17 @@ impl<USART: Instance, WORD> Tx<USART, WORD> {
302316
pub fn unlisten(&mut self) {
303317
unsafe { (*USART::ptr()).cr1.modify(|_, w| w.txeie().clear_bit()) }
304318
}
319+
}
320+
321+
/// Trait for [`Tx`] interrupt handling.
322+
pub trait TxISR {
323+
/// Return true if the tx register is empty (and can accept data)
324+
fn is_tx_empty(&self) -> bool;
325+
}
305326

327+
impl<USART: Instance, WORD> TxISR for Tx<USART, WORD> {
306328
/// Return true if the tx register is empty (and can accept data)
307-
pub fn is_tx_empty(&self) -> bool {
329+
fn is_tx_empty(&self) -> bool {
308330
unsafe { (*USART::ptr()).sr.read().txe().bit_is_set() }
309331
}
310332
}
@@ -713,28 +735,32 @@ impl<USART: Instance, PINS, WORD> Serial<USART, PINS, WORD> {
713735
}
714736
}
715737

716-
/// Return true if the line idle status is set
717-
pub fn is_idle(&self) -> bool {
718-
self.rx.is_idle()
738+
pub fn split(self) -> (Tx<USART, WORD>, Rx<USART, WORD>) {
739+
(self.tx, self.rx)
719740
}
741+
}
720742

721-
/// Return true if the tx register is empty (and can accept data)
722-
pub fn is_tx_empty(&self) -> bool {
723-
self.tx.is_tx_empty()
743+
impl<USART: Instance, PINS, WORD> RxISR for Serial<USART, PINS, WORD> {
744+
/// Return true if the line idle status is set
745+
fn is_idle(&self) -> bool {
746+
self.rx.is_idle()
724747
}
725748

726749
/// Return true if the rx register is not empty (and can be read)
727-
pub fn is_rx_not_empty(&self) -> bool {
750+
fn is_rx_not_empty(&self) -> bool {
728751
self.rx.is_rx_not_empty()
729752
}
730753

731754
/// Clear idle line interrupt flag
732-
pub fn clear_idle_interrupt(&self) {
755+
fn clear_idle_interrupt(&self) {
733756
self.rx.clear_idle_interrupt();
734757
}
758+
}
735759

736-
pub fn split(self) -> (Tx<USART, WORD>, Rx<USART, WORD>) {
737-
(self.tx, self.rx)
760+
impl<USART: Instance, PINS, WORD> TxISR for Serial<USART, PINS, WORD> {
761+
/// Return true if the tx register is empty (and can accept data)
762+
fn is_tx_empty(&self) -> bool {
763+
self.tx.is_tx_empty()
738764
}
739765
}
740766

0 commit comments

Comments
 (0)