Skip to content

Commit 87f418e

Browse files
committed
Listen default implementations
1 parent b6bc8e3 commit 87f418e

File tree

13 files changed

+141
-149
lines changed

13 files changed

+141
-149
lines changed

CHANGELOG.md

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

88
## [Unreleased]
99

10+
- use `Listen` for `Rx/Tx`
11+
1012
## [v0.23.0] - 2025-09-22
1113

1214
- Implement `embedded_hal::i2c::I2c` for `I2cMasterDma` [#838]

examples/rtic-serial-dma-rx-idle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ mod app {
7676
.unwrap();
7777

7878
// Listen UART IDLE event, which will be call USART1 interrupt
79-
rx.listen_idle();
79+
rx.listen(serial::RxEvent::Idle);
8080

8181
let dma2 = StreamsTuple::new(dp.DMA2, &mut rcc);
8282

examples/uart-dma.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ fn main() -> ! {
159159

160160
let (tx, mut rx) = uart3.split();
161161

162-
rx.listen_idle();
162+
rx.listen(serial::RxEvent::Idle);
163163

164164
cortex_m::interrupt::free(|cs| *G_UART3_TX.borrow(cs).borrow_mut() = Some(tx));
165165

src/dma/mod.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -456,18 +456,12 @@ where
456456
type Event = DmaEvent;
457457

458458
#[inline(always)]
459-
fn listen(&mut self, interrupts: impl Into<BitFlags<DmaEvent>>) {
460-
self.listen_event(None, Some(interrupts.into()));
461-
}
462-
463-
#[inline(always)]
464-
fn listen_only(&mut self, interrupts: impl Into<BitFlags<DmaEvent>>) {
465-
self.listen_event(Some(BitFlags::ALL), Some(interrupts.into()));
466-
}
467-
468-
#[inline(always)]
469-
fn unlisten(&mut self, interrupts: impl Into<BitFlags<DmaEvent>>) {
470-
self.listen_event(Some(interrupts.into()), None);
459+
fn listen_event(
460+
&mut self,
461+
disable: Option<BitFlags<Self::Event>>,
462+
enable: Option<BitFlags<Self::Event>>,
463+
) {
464+
self.listen_event(disable, enable)
471465
}
472466
}
473467

src/lib.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,20 +177,35 @@ pub trait Listen {
177177
/// Enum of bit flags associated with events
178178
type Event: BitFlag;
179179

180+
#[doc(hidden)]
181+
fn listen_event(
182+
&mut self,
183+
disable: Option<BitFlags<Self::Event>>,
184+
enable: Option<BitFlags<Self::Event>>,
185+
);
186+
180187
/// Start listening for `Event`s
181188
///
182189
/// Note, you will also have to enable the appropriate interrupt in the NVIC to start
183190
/// receiving events.
184-
fn listen(&mut self, event: impl Into<BitFlags<Self::Event>>);
191+
#[inline(always)]
192+
fn listen(&mut self, event: impl Into<BitFlags<Self::Event>>) {
193+
self.listen_event(None, Some(event.into()));
194+
}
185195

186196
/// Start listening for `Event`s, stop all other
187197
///
188198
/// Note, you will also have to enable the appropriate interrupt in the NVIC to start
189199
/// receiving events.
190-
fn listen_only(&mut self, event: impl Into<BitFlags<Self::Event>>);
200+
#[inline(always)]
201+
fn listen_only(&mut self, event: impl Into<BitFlags<Self::Event>>) {
202+
self.listen_event(Some(BitFlags::ALL), Some(event.into()));
203+
}
191204

192205
/// Stop listening for `Event`s
193-
fn unlisten(&mut self, event: impl Into<BitFlags<Self::Event>>);
206+
fn unlisten(&mut self, event: impl Into<BitFlags<Self::Event>>) {
207+
self.listen_event(Some(event.into()), None);
208+
}
194209

195210
/// Start listening all `Event`s
196211
#[inline(always)]

src/prelude.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,8 @@ pub use crate::serial::dma::SerialHandleIT as _stm32f4xx_hal_serial_dma_SerialHa
7676
pub use crate::serial::dma::SerialReadDMA as _stm32f4xx_hal_serial_dma_SerialReadDMA;
7777
pub use crate::serial::dma::SerialWriteDMA as _stm32f4xx_hal_serial_dma_SerialWriteDMA;
7878
pub use crate::serial::RxISR as _stm32f4xx_hal_serial_RxISR;
79-
pub use crate::serial::RxListen as _stm32f4xx_hal_serial_RxListen;
8079
pub use crate::serial::SerialExt as _stm32f4xx_hal_serial_SerialExt;
8180
pub use crate::serial::TxISR as _stm32f4xx_hal_serial_TxISR;
82-
pub use crate::serial::TxListen as _stm32f4xx_hal_serial_TxListen;
8381
pub use crate::spi::SpiExt as _stm32f4xx_hal_spi_SpiExt;
8482
pub use crate::syscfg::SysCfgExt as _stm32f4xx_hal_syscfg_SysCfgExt;
8583
pub use crate::time::U32Ext as _stm32f4xx_hal_time_U32Ext;

src/serial.rs

Lines changed: 54 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ mod hal_02;
2424
mod hal_1;
2525

2626
mod uart_impls;
27-
use uart_impls::RegisterBlockImpl;
27+
use uart_impls::RBExt;
2828

2929
use crate::gpio::{self, PushPull};
3030

@@ -77,6 +77,32 @@ pub enum Event {
7777
ParityError = 1 << 8,
7878
}
7979

80+
/// UART interrupt events
81+
#[enumflags2::bitflags]
82+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
83+
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
84+
#[repr(u16)]
85+
pub enum RxEvent {
86+
/// IDLE interrupt enable
87+
Idle = 1 << 4,
88+
/// RXNE interrupt enable
89+
RxNotEmpty = 1 << 5,
90+
/// PE interrupt enable
91+
ParityError = 1 << 8,
92+
}
93+
94+
/// UART interrupt events
95+
#[enumflags2::bitflags]
96+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
97+
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
98+
#[repr(u16)]
99+
pub enum TxEvent {
100+
/// Transmission complete interrupt enable
101+
TransmissionComplete = 1 << 6,
102+
/// TXE interrupt enable
103+
TxEmpty = 1 << 7,
104+
}
105+
80106
/// UART/USART status flags
81107
#[enumflags2::bitflags]
82108
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
@@ -128,7 +154,7 @@ pub use gpio::alt::SerialAsync as CommonPins;
128154
// Implemented by all USART/UART instances
129155
pub trait Instance:
130156
crate::Sealed
131-
+ crate::Ptr<RB: RegisterBlockImpl>
157+
+ crate::Ptr<RB: RBExt>
132158
+ crate::Steal
133159
+ core::ops::Deref<Target = Self::RB>
134160
+ rcc::Enable
@@ -161,37 +187,30 @@ pub trait TxISR {
161187
fn is_tx_empty(&self) -> bool;
162188
}
163189

164-
/// Trait for listening [`Rx`] interrupt events.
165-
pub trait RxListen {
166-
/// Start listening for an rx not empty interrupt event
167-
///
168-
/// Note, you will also have to enable the corresponding interrupt
169-
/// in the NVIC to start receiving events.
170-
fn listen(&mut self);
171-
172-
/// Stop listening for the rx not empty interrupt event
173-
fn unlisten(&mut self);
174-
175-
/// Start listening for a line idle interrupt event
176-
///
177-
/// Note, you will also have to enable the corresponding interrupt
178-
/// in the NVIC to start receiving events.
179-
fn listen_idle(&mut self);
190+
impl<UART: Instance> crate::Listen for Rx<UART> {
191+
type Event = RxEvent;
180192

181-
/// Stop listening for the line idle interrupt event
182-
fn unlisten_idle(&mut self);
193+
#[inline(always)]
194+
fn listen_event(
195+
&mut self,
196+
disable: Option<BitFlags<Self::Event>>,
197+
enable: Option<BitFlags<Self::Event>>,
198+
) {
199+
self.usart.listen_rx(disable, enable)
200+
}
183201
}
184202

185-
/// Trait for listening [`Tx`] interrupt event.
186-
pub trait TxListen {
187-
/// Start listening for a tx empty interrupt event
188-
///
189-
/// Note, you will also have to enable the corresponding interrupt
190-
/// in the NVIC to start receiving events.
191-
fn listen(&mut self);
203+
impl<UART: Instance> crate::Listen for Tx<UART> {
204+
type Event = TxEvent;
192205

193-
/// Stop listening for the tx empty interrupt event
194-
fn unlisten(&mut self);
206+
#[inline(always)]
207+
fn listen_event(
208+
&mut self,
209+
disable: Option<BitFlags<Self::Event>>,
210+
enable: Option<BitFlags<Self::Event>>,
211+
) {
212+
self.usart.listen_tx(disable, enable)
213+
}
195214
}
196215

197216
/// Serial abstraction
@@ -578,34 +597,6 @@ impl<UART: Instance, WORD> TxISR for Tx<UART, WORD> {
578597
}
579598
}
580599

581-
impl<UART: Instance, WORD> RxListen for Rx<UART, WORD> {
582-
fn listen(&mut self) {
583-
self.usart.listen_rxne()
584-
}
585-
586-
fn unlisten(&mut self) {
587-
self.usart.unlisten_rxne()
588-
}
589-
590-
fn listen_idle(&mut self) {
591-
self.usart.listen_idle()
592-
}
593-
594-
fn unlisten_idle(&mut self) {
595-
self.usart.unlisten_idle()
596-
}
597-
}
598-
599-
impl<UART: Instance, WORD> TxListen for Tx<UART, WORD> {
600-
fn listen(&mut self) {
601-
self.usart.listen_txe()
602-
}
603-
604-
fn unlisten(&mut self) {
605-
self.usart.unlisten_txe()
606-
}
607-
}
608-
609600
impl<UART: Instance, WORD> crate::ClearFlags for Serial<UART, WORD> {
610601
type Flag = CFlag;
611602

@@ -628,20 +619,12 @@ impl<UART: Instance, WORD> crate::Listen for Serial<UART, WORD> {
628619
type Event = Event;
629620

630621
#[inline(always)]
631-
fn listen(&mut self, event: impl Into<BitFlags<Event>>) {
632-
self.tx.usart.listen_event(None, Some(event.into()));
633-
}
634-
635-
#[inline(always)]
636-
fn listen_only(&mut self, event: impl Into<BitFlags<Self::Event>>) {
637-
self.tx
638-
.usart
639-
.listen_event(Some(BitFlags::ALL), Some(event.into()));
640-
}
641-
642-
#[inline(always)]
643-
fn unlisten(&mut self, event: impl Into<BitFlags<Event>>) {
644-
self.tx.usart.listen_event(Some(event.into()), None);
622+
fn listen_event(
623+
&mut self,
624+
disable: Option<BitFlags<Self::Event>>,
625+
enable: Option<BitFlags<Self::Event>>,
626+
) {
627+
self.tx.usart.listen_event(disable, enable)
645628
}
646629
}
647630

src/serial/dma.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use core::{marker::PhantomData, mem::transmute};
22

3-
use super::{Instance, RegisterBlockImpl, Serial};
3+
use super::{Instance, RBExt, Serial};
44
use crate::dma::{
55
config::DmaConfig,
66
traits::{Channel, DMASet, DmaFlagExt, PeriAddress, Stream, StreamISR},

src/serial/hal_02.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
mod nb {
22
#[allow(unused)]
3-
use super::super::RegisterBlockImpl;
3+
use super::super::RBExt;
44
use super::super::{Error, Instance, Rx, Serial, Tx};
55
use embedded_hal_02::serial::{Read, Write};
66

@@ -84,7 +84,7 @@ mod blocking {
8484
use core::ops::Deref;
8585

8686
#[allow(unused)]
87-
use super::super::RegisterBlockImpl;
87+
use super::super::RBExt;
8888
use super::super::{Error, Instance, Serial, Tx};
8989
use embedded_hal_02::blocking::serial::Write;
9090

src/serial/hal_1.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
mod nb {
22
#[allow(unused)]
3-
use super::super::RegisterBlockImpl;
3+
use super::super::RBExt;
44
use super::super::{Error, Instance, Rx, Serial, Tx};
55
use embedded_hal_nb::serial::{ErrorKind, Read, Write};
66

@@ -92,7 +92,7 @@ mod nb {
9292

9393
mod io {
9494
#[allow(unused)]
95-
use super::super::RegisterBlockImpl;
95+
use super::super::RBExt;
9696
use super::super::{Error, Instance, Rx, Serial, Tx};
9797
use embedded_io::Write;
9898

0 commit comments

Comments
 (0)