this requires an adjustment to the SoC, but now we're seeing half the setup

a small bug was found in the device core that is now fixed. the
device core was not computing the target for the SETUP transaction
correctly if the HEAD of the OUT descriptor was set in advance of
the SETUP transaction. we now see much more consistent SETUP
transactions, and even the first IN response.

the next challenge is figuring out why the second IN response doesn't
seem to be issued.
This commit is contained in:
bunnie 2022-05-15 14:11:14 +08:00
parent 8c39b64126
commit 26739472af

View file

@ -56,7 +56,11 @@ impl fmt::Debug for UdcInterrupts {
f.write_str("NIL") f.write_str("NIL")
} else { } else {
if self.endpoint() != 0 { if self.endpoint() != 0 {
f.write_fmt(format_args!("ep{:x} ", &self.endpoint()))?; for i in 0..NUM_ENDPOINTS {
if self.endpoint() & (1 << i) != 0 {
f.write_fmt(format_args!("ep{:x} ", i))?;
}
}
} }
if self.reset() { if self.reset() {
f.write_str("reset ")?; f.write_str("reset ")?;
@ -669,11 +673,12 @@ impl UsbBus for SpinalUsbDevice {
index == 0, // only trigger for ep0 (per spinal linux driver) index == 0, // only trigger for ep0 (per spinal linux driver)
); );
// now commit the ep config
if index == 0 { if index == 0 {
// stash a copy of the ep0 IN head location, because the SETUP packet requires this to be 0 // stash a copy of the ep0 IN head location, because the SETUP packet resets this to 0
self.ep0in_head = ep_status.head_offset(); self.ep0in_head = ep_status.head_offset();
} }
// now commit the ep config
self.status_write_volatile(index, ep_status); self.status_write_volatile(index, ep_status);
self.ep_allocs[index] = true; self.ep_allocs[index] = true;
@ -716,17 +721,12 @@ impl UsbBus for SpinalUsbDevice {
if ep { if ep {
if index == 0 { if index == 0 {
let mut ep0_status = self.status_read_volatile(0); let mut ep0_status = self.status_read_volatile(0);
// reset the descriptor offset to 0
ep0_status.set_head_offset(self.ep0in_head);
let descriptor = self.descriptor_from_status(&ep0_status); let descriptor = self.descriptor_from_status(&ep0_status);
ep0_status.set_head_offset(0); // reset the descriptor offset to 0, so the IN packet doesn't fire until prepared
self.status_write_volatile(0, ep0_status);
if descriptor.offset() != 0 { if descriptor.offset() != 0 {
descriptor.set_offset(0); // reset the pointer to 0 descriptor.set_offset(0); // reset the pointer to 0
} }
// now prepare the SETUP packet edge case -- it assumes *no* descriptor has been assigned
ep0_status.set_head_offset(0);
self.status_write_volatile(
0,
ep0_status);
} else { } else {
let ep_status = self.status_read_volatile(index); let ep_status = self.status_read_volatile(index);
let descriptor = self.descriptor_from_status(&ep_status); let descriptor = self.descriptor_from_status(&ep_status);
@ -739,15 +739,7 @@ impl UsbBus for SpinalUsbDevice {
// now confirm the config // now confirm the config
for (index, &ep) in self.ep_allocs.iter().enumerate() { for (index, &ep) in self.ep_allocs.iter().enumerate() {
if ep { if ep {
let ep_status = if index == 0 { let ep_status = self.status_read_volatile(index);
let mut ep0_status = self.status_read_volatile(0);
log::info!("ep0 for setup: {:?}", ep0_status);
// then report the IN config below
ep0_status.set_head_offset(self.ep0in_head);
ep0_status
} else {
self.status_read_volatile(index)
};
log::info!("ep{}_status: {:?}", index, ep_status); log::info!("ep{}_status: {:?}", index, ep_status);
let descriptor = self.descriptor_from_status(&ep_status); let descriptor = self.descriptor_from_status(&ep_status);
log::info!("desc{}: {:?}", index, descriptor); log::info!("desc{}: {:?}", index, descriptor);
@ -784,17 +776,15 @@ impl UsbBus for SpinalUsbDevice {
/// ///
/// Implementations may also return other errors if applicable. /// Implementations may also return other errors if applicable.
fn write(&self, ep_addr: EndpointAddress, buf: &[u8]) -> Result<usize> { fn write(&self, ep_addr: EndpointAddress, buf: &[u8]) -> Result<usize> {
if self.ep_allocs[ep_addr.index()] {
let mut ep_status = self.status_read_volatile(ep_addr.index()); let mut ep_status = self.status_read_volatile(ep_addr.index());
if ep_addr.index() == 0 {
// restore the ep0 config because the SETUP transaction resets the settings here
if ep_status.head_offset() == 0 {
ep_status.set_head_offset(self.ep0in_head);
// ep_status.set_force_nack(false); // ------- experiment
}
}
if buf.len() > ep_status.max_packet_size() as usize { if buf.len() > ep_status.max_packet_size() as usize {
Err(UsbError::BufferOverflow) Err(UsbError::BufferOverflow)
} else { } else {
if ep_addr.index() == 0 {
// this is reset to 0 after every transaction by the hardware for ep0, so we must reset it
ep_status.set_head_offset(self.ep0in_head);
}
let descriptor = self.descriptor_from_status(&ep_status); let descriptor = self.descriptor_from_status(&ep_status);
descriptor.set_offset(0); // reset the write pointer to 0 descriptor.set_offset(0); // reset the write pointer to 0
if buf.len() % 4 != 0 { if buf.len() % 4 != 0 {
@ -806,13 +796,19 @@ impl UsbBus for SpinalUsbDevice {
descriptor.write_data(index, w); descriptor.write_data(index, w);
} }
if ep_addr.index() == 0 { if ep_addr.index() == 0 {
self.status_write_volatile(0, UdcEpStatus(ep_status.0)); // this is required to commit the ep_status record once all the setup is done
log::info!("restored ep0: {:?}", self.status_read_volatile(0)); self.status_write_volatile(0, ep_status);
} }
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst); core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
log::info!("ep{} write: {:x?}", ep_addr.index(), &buf); log::info!("ep{} write: {:x?}", ep_addr.index(), &buf);
if ep_addr.index() == 0 {
log::info!("ep0 sanity check: {:?}", self.status_read_volatile(0));
}
Ok(buf.len()) Ok(buf.len())
} }
} else {
Err(UsbError::InvalidEndpoint)
}
} }
/// Reads a single packet of data from the specified endpoint and returns the actual length of /// Reads a single packet of data from the specified endpoint and returns the actual length of
@ -834,6 +830,7 @@ impl UsbBus for SpinalUsbDevice {
/// ///
/// Implementations may also return other errors if applicable. /// Implementations may also return other errors if applicable.
fn read(&self, ep_addr: EndpointAddress, buf: &mut [u8]) -> Result<usize> { fn read(&self, ep_addr: EndpointAddress, buf: &mut [u8]) -> Result<usize> {
if self.ep_allocs[ep_addr.index()] {
log::trace!("read ep{} into buf of len {}", ep_addr.index(), buf.len()); log::trace!("read ep{} into buf of len {}", ep_addr.index(), buf.len());
if ep_addr.index() == 0 { if ep_addr.index() == 0 {
// hard coded to 8 bytes in hardware // hard coded to 8 bytes in hardware
@ -844,27 +841,12 @@ impl UsbBus for SpinalUsbDevice {
// setup data is in a special, fixed location // setup data is in a special, fixed location
buf[..8].copy_from_slice(&self.get_setup()); buf[..8].copy_from_slice(&self.get_setup());
log::info!("ep0 read: {:x?}", &buf[..8]); log::info!("ep0 read: {:x?}", &buf[..8]);
/*
// now that the SETUP read is done, prep for an IN packet
let mut ep0_status = self.status_read_volatile(0);
ep0_status.set_head_offset(self.ep0in_head);
let desc0 = self.descriptor_from_status(&ep0_status);
desc0.set_offset(0);
// ep0_status.set_force_nack(true); // ------- experiment
self.status_write_volatile(0, ep0_status);
*/
Ok(8) Ok(8)
} else { } else {
let ep_status = self.status_read_volatile(ep_addr.index()); let ep_status = self.status_read_volatile(ep_addr.index());
let descriptor = self.descriptor_from_status(&ep_status); let descriptor = self.descriptor_from_status(&ep_status);
let mut iters = 0; if descriptor.in_progress() {
while descriptor.in_progress() { return Err(UsbError::WouldBlock)
xous::yield_slice();
iters += 1;
if iters > 10_000 {
log::info!("ep{} in_progress timeout", ep_addr.index());
break;
}
} }
let len = descriptor.offset(); let len = descriptor.offset();
if len % 4 != 0 { if len % 4 != 0 {
@ -885,6 +867,9 @@ impl UsbBus for SpinalUsbDevice {
log::info!("ep{} read: {:x?} (len {} into buf of {})", ep_addr.index(), &buf[..len], len, buf.len()); log::info!("ep{} read: {:x?} (len {} into buf of {})", ep_addr.index(), &buf[..len], len, buf.len());
Ok(len) Ok(len)
} }
} else {
Err(UsbError::InvalidEndpoint)
}
} }
/// Sets or clears the STALL condition for an endpoint. If the endpoint is an OUT endpoint, it /// Sets or clears the STALL condition for an endpoint. If the endpoint is an OUT endpoint, it
@ -942,16 +927,19 @@ impl UsbBus for SpinalUsbDevice {
let mut ints_to_clear = UdcInterrupts(0); let mut ints_to_clear = UdcInterrupts(0);
log::info!(">>>> frame {}: {:x?}", self.regs.frame_id(), interrupts); log::info!(">>>> frame {}: {:x?}", self.regs.frame_id(), interrupts);
let poll_result = let poll_result =
if interrupts.endpoint() != 0 || interrupts.ep0_setup() { if interrupts.reset() {
ints_to_clear.set_reset(true);
log::trace!("aft reset: {:x?}", interrupts.0);
PollResult::Reset
} else if interrupts.endpoint() != 0 || interrupts.ep0_setup() {
let ep_setup = if interrupts.ep0_setup() { let ep_setup = if interrupts.ep0_setup() {
ints_to_clear.set_ep0_setup(true); ints_to_clear.set_ep0_setup(true);
1 1
} else {0}; } else {0};
let mut ep_in_complete = 0; let mut ep_in_complete = 0;
let mut ep_out = ep_setup; let mut ep_out = 0;
// all of them will be handled here, so, clear the interrupts as needed // all of them will be handled here, so, clear the interrupts as needed
if interrupts.endpoint() != 0 { if interrupts.endpoint() != 0 {
let mut bit = 0; let mut bit = 0;
loop { loop {
@ -959,7 +947,7 @@ impl UsbBus for SpinalUsbDevice {
log::info!("ep{} int", bit); log::info!("ep{} int", bit);
// form a descriptor from the memory range assigned to the EP // form a descriptor from the memory range assigned to the EP
let mut ep_status = self.status_read_volatile(bit); let mut ep_status = self.status_read_volatile(bit);
if bit == 0 { // WHY IS THIS BEING RESET HERE?? if bit == 0 { // EP0 SETUP overrides the descriptor offset, restore it (but don't write it back, it will get written back on the next `write`)
ep_status.set_head_offset(self.ep0in_head); ep_status.set_head_offset(self.ep0in_head);
} }
let descriptor = self.descriptor_from_status(&ep_status); let descriptor = self.descriptor_from_status(&ep_status);
@ -977,10 +965,6 @@ impl UsbBus for SpinalUsbDevice {
} }
} }
PollResult::Data { ep_out, ep_in_complete, ep_setup } PollResult::Data { ep_out, ep_in_complete, ep_setup }
} else if interrupts.reset() {
ints_to_clear.set_reset(true);
log::trace!("aft reset: {:x?}", interrupts.0);
PollResult::Reset
} else if interrupts.resume() { } else if interrupts.resume() {
ints_to_clear.set_reset(true); ints_to_clear.set_reset(true);
log::trace!("aft resume: {:x?}", interrupts.0); log::trace!("aft resume: {:x?}", interrupts.0);