Skip to content

Commit 48150f3

Browse files
ForsakenHarmonykorken89
authored andcommitted
move around character match
Signed-off-by: Leah <[email protected]>
1 parent 239e790 commit 48150f3

File tree

3 files changed

+78
-53
lines changed

3 files changed

+78
-53
lines changed

examples/rtic_frame_serial_dma.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ const APP: () = {
9191
let fr = if let Some(dma_buf) = SerialDMAPool::alloc() {
9292
// Set up the first reader frame
9393
let dma_buf = dma_buf.init(DMAFrame::new());
94-
serial_rx.with_dma(dma_ch6).frame_read(dma_buf)
94+
serial_rx.with_dma(dma_ch6).frame_reader(dma_buf)
9595
} else {
9696
unreachable!()
9797
};
@@ -100,7 +100,6 @@ const APP: () = {
100100
let fs: FrameSender<Box<SerialDMAPool>, _, 8> = serial_tx.with_dma(dma_ch7).frame_sender();
101101

102102
init::LateResources {
103-
rx: serial_rx,
104103
frame_reader: fr,
105104
frame_sender: fs,
106105
}
@@ -112,7 +111,7 @@ const APP: () = {
112111
#[task(binds = USART2, resources = [rx, frame_reader, frame_sender], priority = 3)]
113112
fn serial_isr(cx: serial_isr::Context) {
114113
// Check for character match
115-
if cx.resources.rx.is_character_match(true) {
114+
if cx.resources.frame_reader.check_character_match(true) {
116115
if let Some(dma_buf) = SerialDMAPool::alloc() {
117116
let dma_buf = dma_buf.init(DMAFrame::new());
118117
let buf = cx.resources.frame_reader.character_match_interrupt(dma_buf);

src/dma.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ pub enum Half {
3232
Second,
3333
}
3434

35+
pub trait CharacterMatch {
36+
/// Checks to see if the peripheral has detected a character match and
37+
/// clears the flag
38+
fn check_character_match(&mut self, clear: bool) -> bool;
39+
}
40+
3541
/// Frame reader "worker", access and handling of frame reads is made through this structure.
3642
pub struct FrameReader<BUFFER, PAYLOAD, const N: usize>
3743
where
@@ -59,6 +65,17 @@ where
5965
}
6066
}
6167

68+
impl<BUFFER, PAYLOAD, CHANNEL, const N: usize> FrameReader<BUFFER, RxDma<PAYLOAD, CHANNEL>, N>
69+
where
70+
PAYLOAD: CharacterMatch,
71+
{
72+
/// Checks to see if the peripheral has detected a character match and
73+
/// clears the flag
74+
pub fn check_character_match(&mut self, clear: bool) -> bool {
75+
self.payload.payload.check_character_match(clear)
76+
}
77+
}
78+
6279
/// Frame sender "worker", access and handling of frame transmissions is made through this
6380
/// structure.
6481
pub struct FrameSender<BUFFER, PAYLOAD, const N: usize>
@@ -377,7 +394,7 @@ macro_rules! dma {
377394
use core::ptr;
378395
use stable_deref_trait::StableDeref;
379396

380-
use crate::dma::{CircBuffer, FrameReader, FrameSender, DMAFrame, DmaExt, Error, Event, Half, Transfer, W, R, RxDma, TxDma, TransferPayload};
397+
use crate::dma::{CircBuffer, FrameReader, CharacterMatch, FrameSender, DMAFrame, DmaExt, Error, Event, Half, Transfer, W, R, RxDma, TxDma, TransferPayload};
381398
use crate::rcc::AHB1;
382399

383400
#[allow(clippy::manual_non_exhaustive)]

src/serial.rs

Lines changed: 58 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,63 @@ macro_rules! hal {
575575

576576
Ok(())
577577
}
578+
579+
/// Checks to see if the USART peripheral has detected an idle line and clears
580+
/// the flag
581+
pub fn is_idle(&mut self, clear: bool) -> bool {
582+
let isr = unsafe { &(*pac::$USARTX::ptr()).isr.read() };
583+
let icr = unsafe { &(*pac::$USARTX::ptr()).icr };
584+
585+
if isr.idle().bit_is_set() {
586+
if clear {
587+
icr.write(|w| w.idlecf().set_bit() );
588+
}
589+
true
590+
} else {
591+
false
592+
}
593+
}
594+
595+
596+
/// Checks to see if the USART peripheral has detected an receiver timeout and
597+
/// clears the flag
598+
pub fn is_receiver_timeout(&mut self, clear: bool) -> bool {
599+
let isr = unsafe { &(*pac::$USARTX::ptr()).isr.read() };
600+
let icr = unsafe { &(*pac::$USARTX::ptr()).icr };
601+
602+
if isr.rtof().bit_is_set() {
603+
if clear {
604+
icr.write(|w| w.rtocf().set_bit() );
605+
}
606+
true
607+
} else {
608+
false
609+
}
610+
}
611+
612+
/// Checks to see if the USART peripheral has detected an character match and
613+
/// clears the flag
614+
pub fn check_character_match(&mut self, clear: bool) -> bool {
615+
let isr = unsafe { &(*pac::$USARTX::ptr()).isr.read() };
616+
let icr = unsafe { &(*pac::$USARTX::ptr()).icr };
617+
618+
if isr.cmf().bit_is_set() {
619+
if clear {
620+
icr.write(|w| w.cmcf().set_bit() );
621+
}
622+
true
623+
} else {
624+
false
625+
}
626+
}
627+
}
628+
629+
impl crate::dma::CharacterMatch for Rx<pac::$USARTX> {
630+
/// Checks to see if the USART peripheral has detected an character match and
631+
/// clears the flag
632+
fn check_character_match(&mut self, clear: bool) -> bool {
633+
self.check_character_match(clear)
634+
}
578635
}
579636

580637
impl Tx<pac::$USARTX> {
@@ -665,7 +722,7 @@ macro_rules! hal {
665722
impl $rxdma {
666723
/// Create a frame reader that can either react on the Character match interrupt or
667724
/// Transfer Complete from the DMA.
668-
pub fn frame_read<BUFFER, const N: usize>(
725+
pub fn frame_reader<BUFFER, const N: usize>(
669726
mut self,
670727
buffer: BUFFER,
671728
) -> FrameReader<BUFFER, Self, N>
@@ -712,54 +769,6 @@ macro_rules! hal {
712769

713770
FrameReader::new(buffer, self, usart.cr2.read().add().bits())
714771
}
715-
716-
/// Checks to see if the USART peripheral has detected an idle line and clears
717-
/// the flag
718-
pub fn is_idle(&mut self, clear: bool) -> bool {
719-
let isr = unsafe { &(*pac::$USARTX::ptr()).isr.read() };
720-
let icr = unsafe { &(*pac::$USARTX::ptr()).icr };
721-
722-
if isr.idle().bit_is_set() {
723-
if clear {
724-
icr.write(|w| w.idlecf().set_bit() );
725-
}
726-
true
727-
} else {
728-
false
729-
}
730-
}
731-
732-
/// Checks to see if the USART peripheral has detected an character match and
733-
/// clears the flag
734-
pub fn is_character_match(&mut self, clear: bool) -> bool {
735-
let isr = unsafe { &(*pac::$USARTX::ptr()).isr.read() };
736-
let icr = unsafe { &(*pac::$USARTX::ptr()).icr };
737-
738-
if isr.cmf().bit_is_set() {
739-
if clear {
740-
icr.write(|w| w.cmcf().set_bit() );
741-
}
742-
true
743-
} else {
744-
false
745-
}
746-
}
747-
748-
/// Checks to see if the USART peripheral has detected an receiver timeout and
749-
/// clears the flag
750-
pub fn is_receiver_timeout(&mut self, clear: bool) -> bool {
751-
let isr = unsafe { &(*pac::$USARTX::ptr()).isr.read() };
752-
let icr = unsafe { &(*pac::$USARTX::ptr()).icr };
753-
754-
if isr.rtof().bit_is_set() {
755-
if clear {
756-
icr.write(|w| w.rtocf().set_bit() );
757-
}
758-
true
759-
} else {
760-
false
761-
}
762-
}
763772
}
764773

765774
impl $txdma {

0 commit comments

Comments
 (0)