Skip to content

Commit c392745

Browse files
committed
serial: add ReadExact, ReadUntilIdle
1 parent 2158bdc commit c392745

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

embedded-hal-async/src/serial.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,48 @@
22
33
pub use embedded_hal::serial::{Error, ErrorKind, ErrorType};
44

5+
/// Read an exact amount of words from a serial interface
6+
///
7+
/// Some serial interfaces support different data sizes (8 bits, 9 bits, etc.);
8+
/// This can be encoded in this trait via the `Word` type parameter.
9+
pub trait ReadExact<Word: 'static + Copy = u8>: ErrorType {
10+
/// Read an exact amount of words.
11+
///
12+
/// This does not return until exactly `read.len()` words have been read.
13+
async fn read_exact(&mut self, read: &mut [Word]) -> Result<(), Self::Error>;
14+
}
15+
16+
impl<T: ReadExact<Word>, Word: 'static + Copy> ReadExact<Word> for &mut T {
17+
async fn read_exact(&mut self, read: &mut [Word]) -> Result<(), Self::Error> {
18+
T::read_exact(self, read).await
19+
}
20+
}
21+
22+
/// Read words from a serial interface, until the line becomes idle.
23+
///
24+
/// Some serial interfaces support different data sizes (8 bits, 9 bits, etc.);
25+
/// This can be encoded in this trait via the `Word` type parameter.
26+
pub trait ReadUntilIdle<Word: 'static + Copy = u8>: ErrorType {
27+
/// Read words until the line becomes idle.
28+
///
29+
/// Returns the amount of words received.
30+
///
31+
/// This returns at the earliest of either:
32+
/// - at least 1 word has been received, and then the line becomes idle
33+
/// - exactly `read.len()` words have been read (the buffer is full)
34+
///
35+
/// The serial line is considered idle after a timeout of it being constantly
36+
/// at high level. The exact timeout is implementation-defined, but it should be
37+
/// short, around 1 or 2 words' worth of time.
38+
async fn read_until_idle(&mut self, read: &mut [Word]) -> Result<usize, Self::Error>;
39+
}
40+
41+
impl<T: ReadUntilIdle<Word>, Word: 'static + Copy> ReadUntilIdle<Word> for &mut T {
42+
async fn read_until_idle(&mut self, read: &mut [Word]) -> Result<usize, Self::Error> {
43+
T::read_until_idle(self, read).await
44+
}
45+
}
46+
547
/// Write half of a serial interface
648
pub trait Write<Word: 'static + Copy = u8>: ErrorType {
749
/// Writes a slice, blocking until everything has been written.

embedded-hal/src/serial.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,49 @@ impl<T: ErrorType> ErrorType for &mut T {
7373
type Error = T::Error;
7474
}
7575

76-
/// Write half of a serial interface (blocking variant)
76+
/// Read an exact amount of words from a serial interface
77+
///
78+
/// Some serial interfaces support different data sizes (8 bits, 9 bits, etc.);
79+
/// This can be encoded in this trait via the `Word` type parameter.
80+
pub trait ReadExact<Word: 'static + Copy = u8>: ErrorType {
81+
/// Read an exact amount of words.
82+
///
83+
/// This does not return until exactly `read.len()` words have been read.
84+
fn read_exact(&mut self, read: &mut [Word]) -> Result<(), Self::Error>;
85+
}
86+
87+
impl<T: ReadExact<Word>, Word: 'static + Copy> ReadExact<Word> for &mut T {
88+
fn read_exact(&mut self, read: &mut [Word]) -> Result<(), Self::Error> {
89+
T::read_exact(self, read)
90+
}
91+
}
92+
93+
/// Read words from a serial interface, until the line becomes idle.
94+
///
95+
/// Some serial interfaces support different data sizes (8 bits, 9 bits, etc.);
96+
/// This can be encoded in this trait via the `Word` type parameter.
97+
pub trait ReadUntilIdle<Word: 'static + Copy = u8>: ErrorType {
98+
/// Read words until the line becomes idle.
99+
///
100+
/// Returns the amount of words received.
101+
///
102+
/// This returns at the earliest of either:
103+
/// - at least 1 word has been received, and then the line becomes idle
104+
/// - exactly `read.len()` words have been read (the buffer is full)
105+
///
106+
/// The serial line is considered idle after a timeout of it being constantly
107+
/// at high level. The exact timeout is implementation-defined, but it should be
108+
/// short, around 1 or 2 words' worth of time.
109+
fn read_until_idle(&mut self, read: &mut [Word]) -> Result<usize, Self::Error>;
110+
}
111+
112+
impl<T: ReadUntilIdle<Word>, Word: 'static + Copy> ReadUntilIdle<Word> for &mut T {
113+
fn read_until_idle(&mut self, read: &mut [Word]) -> Result<usize, Self::Error> {
114+
T::read_until_idle(self, read)
115+
}
116+
}
117+
118+
/// Write half of a serial interface.
77119
pub trait Write<Word: Copy = u8>: ErrorType {
78120
/// Writes a slice, blocking until everything has been written
79121
///

0 commit comments

Comments
 (0)