Skip to content

Commit d02833d

Browse files
author
高庆丰
committed
add serial is_txe is_idle is_rxne
1 parent 5776ccd commit d02833d

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/serial.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,21 @@ where
342342
}
343343
}
344344

345+
/// Return true if the line idle status is set
346+
pub fn is_idle(&self) -> bool {
347+
self.usart.sr.read().idle().bit_is_set();
348+
}
349+
350+
/// Return true if the tx register is empty (and can accept data)
351+
pub fn is_txe(&self) -> bool {
352+
self.usart.sr.read().txe().bit_is_set();
353+
}
354+
355+
/// Return true if the rx register is not empty (and can be read)
356+
pub fn is_rxne(&self) -> bool {
357+
self.usart.sr.read().rxne().bit_is_set();
358+
}
359+
345360
/// Returns ownership of the borrowed register handles
346361
pub fn release(self) -> (USART, PINS) {
347362
(self.usart, self.pins)
@@ -418,6 +433,10 @@ macro_rules! hal {
418433
pub fn unlisten(&mut self) {
419434
unsafe { (*$USARTX::ptr()).cr1.modify(|_, w| w.txeie().clear_bit()) };
420435
}
436+
437+
pub fn is_txe(&self) -> bool {
438+
unsafe { (*$USARTX::ptr()).sr.read().txe().bit_is_set() }
439+
}
421440
}
422441

423442
impl Rx<$USARTX> {
@@ -428,6 +447,22 @@ macro_rules! hal {
428447
pub fn unlisten(&mut self) {
429448
unsafe { (*$USARTX::ptr()).cr1.modify(|_, w| w.rxneie().clear_bit()) };
430449
}
450+
451+
pub fn listen_idle(&mut self) {
452+
unsafe { (*$USARTX::ptr()).cr1.modify(|_, w| w.idleie().set_bit()) };
453+
}
454+
455+
pub fn unlisten_idle(&mut self) {
456+
unsafe { (*$USARTX::ptr()).cr1.modify(|_, w| w.idleie().clear_bit()) };
457+
}
458+
459+
pub fn is_idle(&self) -> bool {
460+
unsafe { (*$USARTX::ptr()).sr.read().idle().bit_is_set() }
461+
}
462+
463+
pub fn is_rxne(&self) -> bool {
464+
unsafe { (*$USARTX::ptr()).sr.read().rxne().bit_is_set() }
465+
}
431466
}
432467

433468
impl crate::hal::serial::Read<u8> for Rx<$USARTX> {

0 commit comments

Comments
 (0)