Skip to content

Commit

Permalink
Implement embedded_io_async::Write for SerialPort
Browse files Browse the repository at this point in the history
  • Loading branch information
Tnze committed Jan 24, 2025
1 parent d850634 commit 3e31f1d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 9 deletions.
69 changes: 60 additions & 9 deletions src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,61 @@ impl<Bus: UsbBus, RS: BorrowMut<[u8]>, WS: BorrowMut<[u8]>> embedded_io::WriteRe
}
}

impl<B, RS, WS> embedded_io_async::Write for SerialPort<'_, B, RS, WS>
where
B: UsbBus,
RS: BorrowMut<[u8]>,
WS: BorrowMut<[u8]>,
{
async fn write(&mut self, buffer: &[u8]) -> core::result::Result<usize, Self::Error> {
if buffer.is_empty() {
return Ok(0);
}
AsyncWrite {
serial_port: self,
buffer,
}
.await
}

// async fn flush(&mut self) -> core::result::Result<(), Self::Error> {
// todo!()
// }
}
struct AsyncWrite<'a, 'b, 'c, B, RS, WS>
where
B: UsbBus,
RS: BorrowMut<[u8]>,
WS: BorrowMut<[u8]>,
{
serial_port: &'a mut SerialPort<'b, B, RS, WS>,
buffer: &'c [u8],
}

impl<'a, 'b, 'c, B, RS, WS> Future for AsyncWrite<'a, 'b, 'c, B, RS, WS>
where
B: UsbBus,
RS: BorrowMut<[u8]>,
WS: BorrowMut<[u8]>,
{
type Output = Result<usize, Error>;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let s = self.get_mut();
match s.serial_port.write(&s.buffer) {
Ok(n) => Poll::Ready(Ok(n)),
Err(usb_device::UsbError::WouldBlock) => {
// No need to worry about overriding.
// The ownership is borrowed though the mutable reference,
// so it's impossable to run twice at the same time.
s.serial_port.write_waker = Some(cx.waker().clone());
Poll::Pending
}
Err(err) => Poll::Ready(Err(Error(err))),
}
}
}

impl<B, RS, WS> embedded_io_async::Read for SerialPort<'_, B, RS, WS>
where
B: UsbBus,
Expand Down Expand Up @@ -127,19 +182,15 @@ where
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let s = self.get_mut();
match s.serial_port.read(&mut s.buffer) {
Ok(len) => Poll::Ready(Ok(len)),
Ok(n) => Poll::Ready(Ok(n)),
Err(usb_device::UsbError::WouldBlock) => {
if s.buffer.len() == 0 {
Poll::Ready(Ok(0))
} else {
let prev = s.serial_port.read_waker.replace(cx.waker().clone());
assert!(
prev.is_none(),
concat!(
"The ownership is borrowed though the mutable reference.",
"Impossable to run twice at the same time"
)
);
// No need to worry about overriding.
// The ownership is borrowed though the mutable reference,
// so it's impossable to run twice at the same time.
s.serial_port.read_waker = Some(cx.waker().clone());
Poll::Pending
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/serial_port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ where
write_state: WriteState,

pub(crate) read_waker: Option<Waker>,
pub(crate) write_waker: Option<Waker>,
}

/// If this many full size packets have been sent in a row, a short packet will be sent so that the
Expand Down Expand Up @@ -99,6 +100,7 @@ where
write_buf: Buffer::new(write_store),
write_state: WriteState::Idle,
read_waker: None,
write_waker: None,
}
}

Expand Down Expand Up @@ -265,6 +267,9 @@ where
fn endpoint_in_complete(&mut self, addr: EndpointAddress) {
if addr == self.inner.write_ep().address() {
self.flush().ok();
if let Some(write_waker) = self.write_waker.take() {
write_waker.wake();
}
}
}

Expand Down

0 comments on commit 3e31f1d

Please sign in to comment.