From 0a8c0daa9f48093682a3197ad24d37c16dc32c99 Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Sat, 10 Aug 2019 14:19:38 -0400 Subject: Use endpoint's max packet size for pipe. --- src/pipe.rs | 28 ++++++++++++++++++++++++---- src/pipe/pck_size.rs | 10 ++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/pipe.rs b/src/pipe.rs index 607e1c9..e90b88b 100644 --- a/src/pipe.rs +++ b/src/pipe.rs @@ -107,6 +107,26 @@ impl PipeTable { w.pdaddr().set_addr(endpoint.address()); w.pepnum().set_epnum(endpoint.endpoint_num()) }); + pdesc.bank0.pcksize.write(|w| { + let mps = endpoint.max_packet_size(); + if mps >= 1023 { + w.size().bytes1024() + } else if mps >= 512 { + w.size().bytes512() + } else if mps >= 256 { + w.size().bytes256() + } else if mps >= 128 { + w.size().bytes128() + } else if mps >= 64 { + w.size().bytes64() + } else if mps >= 32 { + w.size().bytes32() + } else if mps >= 16 { + w.size().bytes16() + } else { + w.size().bytes8() + } + }); Pipe { num: i, regs: pregs, @@ -175,7 +195,7 @@ impl Pipe<'_, '_> { */ // TODO: status stage has up to 50ms to complete. cf ยง9.2.6.4 // of USB 2.0. - self.desc.bank0.pcksize.write(|w| { + self.desc.bank0.pcksize.modify(|_, w| { unsafe { w.byte_count().bits(0) }; unsafe { w.multi_packet_size().bits(0) } }); @@ -206,7 +226,7 @@ impl Pipe<'_, '_> { .addr .write(|w| unsafe { w.addr().bits(buf.ptr as u32) }); // configure packet size PCKSIZE.SIZE - self.desc.bank0.pcksize.write(|w| { + self.desc.bank0.pcksize.modify(|_, w| { unsafe { w.byte_count().bits(buf.len as u16) }; unsafe { w.multi_packet_size().bits(0) } }); @@ -225,7 +245,7 @@ impl Pipe<'_, '_> { let packet_size = 8; trace!("p{}: Should IN for {}b.", self.num, buf.len()); - self.desc.bank0.pcksize.write(|w| { + self.desc.bank0.pcksize.modify(|_, w| { unsafe { w.byte_count().bits(buf.len() as u16) }; unsafe { w.multi_packet_size().bits(0) } }); @@ -283,7 +303,7 @@ impl Pipe<'_, '_> { millis: &dyn Fn() -> usize, ) -> Result { trace!("p{}: Should OUT for {}b.", self.num, buf.len()); - self.desc.bank0.pcksize.write(|w| { + self.desc.bank0.pcksize.modify(|_, w| { unsafe { w.byte_count().bits(buf.len() as u16) }; unsafe { w.multi_packet_size().bits(0) } }); diff --git a/src/pipe/pck_size.rs b/src/pipe/pck_size.rs index acc499f..c95140d 100644 --- a/src/pipe/pck_size.rs +++ b/src/pipe/pck_size.rs @@ -29,6 +29,16 @@ impl PckSize { f(&mut w); self.0 = w.bits; } + + pub fn modify(&mut self, f: F) + where + for<'w> F: FnOnce(&R, &'w mut W) -> &'w mut W, + { + let r = R { bits: self.0 }; + let mut w = W { bits: self.0 }; + f(&r, &mut w); + self.0 = w.bits; + } } impl From for PckSize { -- cgit v1.2.3