Skip to content

Commit

Permalink
move around character match
Browse files Browse the repository at this point in the history
Signed-off-by: Leah <github.leah@hrmny.sh>
  • Loading branch information
ForsakenHarmony committed Apr 27, 2021
1 parent 8a02a7e commit d84549f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 53 deletions.
5 changes: 2 additions & 3 deletions examples/rtic_frame_serial_dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const APP: () = {
let fr = if let Some(dma_buf) = SerialDMAPool::alloc() {
// Set up the first reader frame
let dma_buf = dma_buf.init(DMAFrame::new());
serial_rx.with_dma(dma_ch6).frame_read(dma_buf)
serial_rx.with_dma(dma_ch6).frame_reader(dma_buf)
} else {
unreachable!()
};
Expand All @@ -100,7 +100,6 @@ const APP: () = {
let fs: FrameSender<Box<SerialDMAPool>, _, 8> = serial_tx.with_dma(dma_ch7).frame_sender();

init::LateResources {
rx: serial_rx,
frame_reader: fr,
frame_sender: fs,
}
Expand All @@ -112,7 +111,7 @@ const APP: () = {
#[task(binds = USART2, resources = [rx, frame_reader, frame_sender], priority = 3)]
fn serial_isr(cx: serial_isr::Context) {
// Check for character match
if cx.resources.rx.is_character_match(true) {
if cx.resources.frame_reader.check_character_match(true) {
if let Some(dma_buf) = SerialDMAPool::alloc() {
let dma_buf = dma_buf.init(DMAFrame::new());
let buf = cx.resources.frame_reader.character_match_interrupt(dma_buf);
Expand Down
19 changes: 18 additions & 1 deletion src/dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ pub enum Half {
Second,
}

pub trait CharacterMatch {
/// Checks to see if the peripheral has detected a character match and
/// clears the flag
fn check_character_match(&mut self, clear: bool) -> bool;
}

/// Frame reader "worker", access and handling of frame reads is made through this structure.
pub struct FrameReader<BUFFER, PAYLOAD, const N: usize>
where
Expand Down Expand Up @@ -59,6 +65,17 @@ where
}
}

impl<BUFFER, PAYLOAD, CHANNEL, const N: usize> FrameReader<BUFFER, RxDma<PAYLOAD, CHANNEL>, N>
where
PAYLOAD: CharacterMatch,
{
/// Checks to see if the peripheral has detected a character match and
/// clears the flag
pub fn check_character_match(&mut self, clear: bool) -> bool {
self.payload.payload.check_character_match(clear)
}
}

/// Frame sender "worker", access and handling of frame transmissions is made through this
/// structure.
pub struct FrameSender<BUFFER, PAYLOAD, const N: usize>
Expand Down Expand Up @@ -377,7 +394,7 @@ macro_rules! dma {
use core::ptr;
use stable_deref_trait::StableDeref;

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

#[allow(clippy::manual_non_exhaustive)]
Expand Down
107 changes: 58 additions & 49 deletions src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,63 @@ macro_rules! hal {

Ok(())
}

/// Checks to see if the USART peripheral has detected an idle line and clears
/// the flag
pub fn is_idle(&mut self, clear: bool) -> bool {
let isr = unsafe { &(*pac::$USARTX::ptr()).isr.read() };
let icr = unsafe { &(*pac::$USARTX::ptr()).icr };

if isr.idle().bit_is_set() {
if clear {
icr.write(|w| w.idlecf().set_bit() );
}
true
} else {
false
}
}


/// Checks to see if the USART peripheral has detected an receiver timeout and
/// clears the flag
pub fn is_receiver_timeout(&mut self, clear: bool) -> bool {
let isr = unsafe { &(*pac::$USARTX::ptr()).isr.read() };
let icr = unsafe { &(*pac::$USARTX::ptr()).icr };

if isr.rtof().bit_is_set() {
if clear {
icr.write(|w| w.rtocf().set_bit() );
}
true
} else {
false
}
}

/// Checks to see if the USART peripheral has detected an character match and
/// clears the flag
pub fn check_character_match(&mut self, clear: bool) -> bool {
let isr = unsafe { &(*pac::$USARTX::ptr()).isr.read() };
let icr = unsafe { &(*pac::$USARTX::ptr()).icr };

if isr.cmf().bit_is_set() {
if clear {
icr.write(|w| w.cmcf().set_bit() );
}
true
} else {
false
}
}
}

impl crate::dma::CharacterMatch for Rx<pac::$USARTX> {
/// Checks to see if the USART peripheral has detected an character match and
/// clears the flag
fn check_character_match(&mut self, clear: bool) -> bool {
self.check_character_match(clear)
}
}

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

FrameReader::new(buffer, self, usart.cr2.read().add().bits())
}

/// Checks to see if the USART peripheral has detected an idle line and clears
/// the flag
pub fn is_idle(&mut self, clear: bool) -> bool {
let isr = unsafe { &(*pac::$USARTX::ptr()).isr.read() };
let icr = unsafe { &(*pac::$USARTX::ptr()).icr };

if isr.idle().bit_is_set() {
if clear {
icr.write(|w| w.idlecf().set_bit() );
}
true
} else {
false
}
}

/// Checks to see if the USART peripheral has detected an character match and
/// clears the flag
pub fn is_character_match(&mut self, clear: bool) -> bool {
let isr = unsafe { &(*pac::$USARTX::ptr()).isr.read() };
let icr = unsafe { &(*pac::$USARTX::ptr()).icr };

if isr.cmf().bit_is_set() {
if clear {
icr.write(|w| w.cmcf().set_bit() );
}
true
} else {
false
}
}

/// Checks to see if the USART peripheral has detected an receiver timeout and
/// clears the flag
pub fn is_receiver_timeout(&mut self, clear: bool) -> bool {
let isr = unsafe { &(*pac::$USARTX::ptr()).isr.read() };
let icr = unsafe { &(*pac::$USARTX::ptr()).icr };

if isr.rtof().bit_is_set() {
if clear {
icr.write(|w| w.rtocf().set_bit() );
}
true
} else {
false
}
}
}

impl $txdma {
Expand Down

0 comments on commit d84549f

Please sign in to comment.