Skip to content

Commit 23265ea

Browse files
Add ensure_transaction_end option to request_out
Proper implementation is only provided for embassy-rp. embassy-stm32 only gets a stub.
1 parent a76c6f2 commit 23265ea

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

embassy-rp/src/usb/host.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ impl<'d, T: Instance, E: channel::Type, D: channel::Direction> UsbChannel<E, D>
584584

585585
// Data stage
586586
if setup.length > 0 {
587-
self.request_out(&buf[..setup.length as usize]).await?;
587+
self.request_out(&buf[..setup.length as usize], false).await?;
588588
}
589589

590590
// Status stage
@@ -648,7 +648,7 @@ impl<'d, T: Instance, E: channel::Type, D: channel::Direction> UsbChannel<E, D>
648648
res
649649
}
650650

651-
async fn request_out(&mut self, buf: &[u8]) -> Result<(), ChannelError>
651+
async fn request_out(&mut self, buf: &[u8], ensure_transaction_end: bool) -> Result<(), ChannelError>
652652
where
653653
D: channel::IsOut,
654654
{
@@ -675,6 +675,11 @@ impl<'d, T: Instance, E: channel::Type, D: channel::Direction> UsbChannel<E, D>
675675
count += packet;
676676

677677
if count == buf.len() {
678+
if packet == self.max_packet_size as usize && ensure_transaction_end {
679+
trace!("CHANNEL {} START ZLP WRITE", self.index);
680+
self.set_data_out(&[]);
681+
trace!("ZLP WRITE DONE");
682+
}
678683
break Ok(());
679684
}
680685
};

embassy-stm32/src/usb/usb_host.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,8 @@ impl<'d, I: Instance, D: channel::Direction, T: channel::Type> Channel<'d, I, D,
462462
}
463463
}
464464

465-
async fn write(&mut self, buf: &[u8]) -> Result<(), ChannelError> {
465+
//TODO: Emit a zero length packet when ensure_transaction_end is true and the packet is of max size
466+
async fn write(&mut self, buf: &[u8], ensure_transaction_end: bool) -> Result<(), ChannelError> {
466467
self.write_data(buf);
467468

468469
let index = self.index;
@@ -577,7 +578,7 @@ impl<'d, I: Instance, T: channel::Type, D: channel::Direction> UsbChannel<T, D>
577578
epr_val.set_setup(true);
578579
epr0.write_value(epr_val);
579580

580-
self.write(setup.as_bytes()).await?;
581+
self.write(setup.as_bytes(), false).await?;
581582

582583
// data stage
583584
let count = self.read(buf).await?;
@@ -586,7 +587,7 @@ impl<'d, I: Instance, T: channel::Type, D: channel::Direction> UsbChannel<T, D>
586587

587588
// Send 0 bytes
588589
let zero: [u8; 0] = [0u8; 0];
589-
self.write(&zero).await?;
590+
self.write(&zero, false).await?;
590591

591592
Ok(count)
592593
}
@@ -606,12 +607,12 @@ impl<'d, I: Instance, T: channel::Type, D: channel::Direction> UsbChannel<T, D>
606607
let mut epr_val = invariant(epr0.read());
607608
epr_val.set_setup(true);
608609
epr0.write_value(epr_val);
609-
self.write(setup.as_bytes()).await?;
610+
self.write(setup.as_bytes(), false).await?;
610611

611612
if buf.is_empty() {
612613
// do nothing
613614
} else {
614-
self.write(buf).await?;
615+
self.write(buf, false).await?;
615616
}
616617

617618
// Status stage
@@ -654,11 +655,11 @@ impl<'d, I: Instance, T: channel::Type, D: channel::Direction> UsbChannel<T, D>
654655
self.read(buf).await
655656
}
656657

657-
async fn request_out(&mut self, buf: &[u8]) -> Result<(), ChannelError>
658+
async fn request_out(&mut self, buf: &[u8], ensure_transaction_end: bool) -> Result<(), ChannelError>
658659
where
659660
D: channel::IsOut,
660661
{
661-
self.write(buf).await
662+
self.write(buf, ensure_transaction_end).await
662663
}
663664

664665
async fn set_timeout(&mut self, _: TimeoutConfig) {}

embassy-usb-driver/src/host.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,8 @@ pub trait UsbChannel<T: channel::Type, D: channel::Direction> {
292292
D: channel::IsIn;
293293

294294
/// Send OUT request of type other from control
295-
async fn request_out(&mut self, buf: &[u8]) -> Result<(), ChannelError>
295+
/// ensure_transaction_end: Send a zero length packet at the end of transaction if last packet is of max size.
296+
async fn request_out(&mut self, buf: &[u8], ensure_transaction_end: bool) -> Result<(), ChannelError>
296297
where
297298
D: channel::IsOut;
298299

0 commit comments

Comments
 (0)