Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LPUART: embedded-io 0.6 compatibility #154

Merged
merged 4 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ version = "0.2"
package = "embedded-hal"
version = "1.0.0"

[dependencies.eio06]
package = "embedded-io"
version = "0.6.1"

[dependencies.rand_core]
version = "0.5"
default-features = false
Expand Down
111 changes: 111 additions & 0 deletions src/common/lpuart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,35 @@ impl<P, const N: u8> Lpuart<P, N> {
ral::modify_reg!(ral::lpuart, self.lpuart, BAUD, RDMAE: 0);
}
}

/// Attempts to write a single byte to the bus.
///
/// Returns `false` if the fifo was already full.
pub fn try_write(&mut self, byte: u8) -> bool {
ral::modify_reg!(ral::lpuart, self.lpuart, FIFO, TXOF: TXOF_1);
self.write_byte(byte);
ral::read_reg!(ral::lpuart, self.lpuart, FIFO, TXOF == TXOF_0)
}

/// Attempts to read a single byte from the bus.
///
/// Returns:
/// - `Ok(Some(u8))` if data was read
/// - `Ok(None)` if the fifo was empty
/// - `Err(..)` if a read error happened
pub fn try_read(&mut self) -> Result<Option<u8>, ReadFlags> {
let data = self.read_data();
if data.flags().contains(ReadFlags::RXEMPT) {
Ok(None)
} else if data
.flags()
.intersects(ReadFlags::PARITY_ERROR | ReadFlags::FRAME_ERROR | ReadFlags::NOISY)
{
Err(data.flags())
} else {
Ok(Some(data.into()))
}
}
}

fn flush_fifo<const N: u8>(lpuart: &Instance<N>, direction: Direction) {
Expand Down Expand Up @@ -930,6 +959,88 @@ impl<P, const N: u8> eh02::blocking::serial::Write<u8> for Lpuart<P, N> {
}
}

impl eio06::Error for ReadFlags {
fn kind(&self) -> eio06::ErrorKind {
eio06::ErrorKind::Other
}
}

impl<P, const N: u8> eio06::ErrorType for Lpuart<P, N> {
type Error = ReadFlags;
}

impl<P, const N: u8> eio06::WriteReady for Lpuart<P, N> {
fn write_ready(&mut self) -> Result<bool, Self::Error> {
Ok(self.status().contains(Status::TRANSMIT_EMPTY))
}
}

impl<P, const N: u8> eio06::ReadReady for Lpuart<P, N> {
fn read_ready(&mut self) -> Result<bool, Self::Error> {
Ok(self.status().contains(Status::RECEIVE_FULL))
}
}

impl<P, const N: u8> eio06::Write for Lpuart<P, N> {
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
let mut num_written = 0;
for word in buf {
if num_written == 0 {
// For the first word, continue trying until we send.
// This function is supposed to block until at least one word is
// sent.
while !self.try_write(*word) {}
} else {
// If we already sent at least one word, return once
// the buffer is full
if !self.try_write(*word) {
break;
}
}
num_written += 1;
}

Ok(num_written)
}

fn flush(&mut self) -> Result<(), Self::Error> {
while !self.status().contains(Status::TRANSMIT_COMPLETE) {}

Ok(())
}
}

impl<P, const N: u8> eio06::Read for Lpuart<P, N> {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
let mut num_read = 0;
for word in buf {
let data = if num_read == 0 {
// For the first word, continue querying until we receive something.
// This function is supposed to block until at least one word is
// received.
loop {
if let Some(data) = self.try_read()? {
break data;
}
}
} else {
// If we already read at least one word, return once
// the buffer is empty
if let Some(data) = self.try_read()? {
data
} else {
break;
}
};

*word = data;
num_read += 1;
}

Ok(num_read)
}
}

#[cfg(test)]
mod tests {
use super::{Baud, ReadData, ReadFlags, Status};
Expand Down