Skip to content

Commit

Permalink
serial
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Mar 11, 2022
1 parent d66dd86 commit b2ef855
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 13 deletions.
16 changes: 3 additions & 13 deletions src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ use crate::rcc::{BusClock, Clocks, Enable, Reset};
use crate::time::{Bps, U32Ext};

mod hal_02;
mod hal_1;

// USART REMAPPING, see: https://www.st.com/content/ccc/resource/technical/document/reference_manual/59/b9/ba/7f/11/af/43/d5/CD00171190.pdf/files/CD00171190.pdf/jcr:content/translations/en.CD00171190.pdf
// Section 9.3.8
Expand Down Expand Up @@ -134,18 +135,7 @@ inst! {
}

/// Serial error
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
/// Framing error
Framing,
/// Noise error
Noise,
/// RX buffer overrun
Overrun,
/// Parity check error
Parity,
}
pub use embedded_hal_one::serial::ErrorKind as Error;

pub enum WordLength {
/// When parity is enabled, a word has 7 data bits + 1 parity bit,
Expand Down Expand Up @@ -514,7 +504,7 @@ impl<USART: Instance> Rx<USART> {
let err = if sr.pe().bit_is_set() {
Some(Error::Parity)
} else if sr.fe().bit_is_set() {
Some(Error::Framing)
Some(Error::FrameFormat)
} else if sr.ne().bit_is_set() {
Some(Error::Noise)
} else if sr.ore().bit_is_set() {
Expand Down
128 changes: 128 additions & 0 deletions src/serial/hal_1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
use super::*;
use embedded_hal_one::{serial::blocking, serial::nb as serial, serial::ErrorType};

impl<USART: Instance> ErrorType for Tx<USART> {
type Error = Infallible;
}

impl<USART: Instance> ErrorType for Rx<USART> {
type Error = Error;
}

impl<USART: Instance, PINS> ErrorType for Serial<USART, PINS> {
type Error = Error;
}

impl<USART: Instance> serial::Write<u8> for Tx<USART> {
fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> {
self.write(word)
}

fn flush(&mut self) -> nb::Result<(), Self::Error> {
self.flush()
}
}

impl<USART: Instance> serial::Write<u16> for Tx<USART> {
fn write(&mut self, word: u16) -> nb::Result<(), Self::Error> {
self.write_u16(word)
}

fn flush(&mut self) -> nb::Result<(), Self::Error> {
self.flush()
}
}

impl<USART: Instance> serial::Read<u8> for Rx<USART> {
fn read(&mut self) -> nb::Result<u8, Self::Error> {
self.read()
}
}

impl<USART: Instance> serial::Read<u16> for Rx<USART> {
fn read(&mut self) -> nb::Result<u16, Self::Error> {
self.read_u16()
}
}

impl<USART: Instance, PINS> serial::Write<u8> for Serial<USART, PINS> {
fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> {
self.tx.write(word).unwrap();
Ok(())
}

fn flush(&mut self) -> nb::Result<(), Self::Error> {
self.tx.flush().unwrap();
Ok(())
}
}

impl<USART: Instance, PINS> serial::Write<u16> for Serial<USART, PINS> {
fn write(&mut self, word: u16) -> nb::Result<(), Self::Error> {
self.tx.write_u16(word).unwrap();
Ok(())
}

fn flush(&mut self) -> nb::Result<(), Self::Error> {
self.tx.flush().unwrap();
Ok(())
}
}

impl<USART: Instance, PINS> serial::Read<u8> for Serial<USART, PINS> {
fn read(&mut self) -> nb::Result<u8, Error> {
self.rx.read()
}
}

impl<USART: Instance, PINS> serial::Read<u16> for Serial<USART, PINS> {
fn read(&mut self) -> nb::Result<u16, Error> {
self.rx.read_u16()
}
}

// Blocking

impl<USART: Instance> blocking::Write<u8> for Tx<USART> {
fn write(&mut self, buffer: &[u8]) -> Result<(), Self::Error> {
self.bwrite_all(buffer)
}

fn flush(&mut self) -> Result<(), Self::Error> {
self.bflush()
}
}

impl<USART: Instance> blocking::Write<u16> for Tx<USART> {
fn write(&mut self, buffer: &[u16]) -> Result<(), Self::Error> {
self.bwrite_all_u16(buffer)
}

fn flush(&mut self) -> Result<(), Self::Error> {
self.bflush()
}
}

impl<USART: Instance, PINS> blocking::Write<u8> for Serial<USART, PINS> {
fn write(&mut self, buffer: &[u8]) -> Result<(), Self::Error> {
self.tx.bwrite_all(buffer).unwrap();
Ok(())
}

fn flush(&mut self) -> Result<(), Self::Error> {
self.tx.bflush().unwrap();
Ok(())
}
}

impl<USART: Instance, PINS> blocking::Write<u16> for Serial<USART, PINS> {
fn write(&mut self, buffer: &[u16]) -> Result<(), Self::Error> {
self.tx.bwrite_all_u16(buffer).unwrap();
Ok(())
}

fn flush(&mut self) -> Result<(), Self::Error> {
self.tx.bflush().unwrap();
Ok(())
}
}

0 comments on commit b2ef855

Please sign in to comment.