The Raspberry PI Pico RP2040 USB CDC ACM serial port does not work with many machines
The Raspberry PI Pico RP2040 USB CDC ACM serial port does not work with many machines
Description
The Raspberry PI Pico RP2040 Tock USB CDC ACM serial port implementation does not work with many machines. All Windows 11 Dell and HP Envy laptops I tried reported a Unknown USB Device (Set Address Failed). Their firmware is up to date. It does work with a Windows 11 GMKtec mini PC. It is randomly recognized by a Raspberry Pi 4 and once recognized works fine as a serial port. It does not work with the Raspberry Pi 5. I applied the eeprom updates for the Raspberry Pi.
On all machines the RP 2040's firmware programming mode with the USB drive always works.
When I capture the physical USB line traffic between the RP 2040 and most computers (Windows/Raspberry Pi) I see a violation of the USB 2.0 control-transfer data-toggle rules, specifically Sections 8.5.3 and 8.6.1 (I'm looking at Revision 2.0 April 27, 2000). For a control read/write as shown in Figure 8-37 after a valid SETUP transaction for the IN transaction DATA must be toggled by the device to DATA1. But as shown below the RP2040 is incorrectly using DATA0. Additionally the RP2040 sends the wrong amount of data for the GET_DESCRIPTOR request. The RP2040 should only send 18 bytes as specified by the spec (Table 9-8. Standard Device Descriptor), but instead it is sending 64 bytes on the first IN transaction. Then it sends a second IN with the correct 18 bytes. This happens on the Windows 11 machines and on the Raspberry Pis when they fail.
Expanded 2nd IN with correct 18 bytes:
This matches when the GET_DESCRIPTION does work and both DATA1 is correct and only one IN is correctly sent (below)
Does anyone have any thoughts on this. I'm not familiar with developing or debugging device side USB interfaces. Currently I'm debugging the issue based on OpenAI's analysis below but I don't have the skill to judge if it is reasonable or not. thanks!
OpenAI analysis
The Pico sends a 64-byte DATA0 packet on endpoint zero immediately after the SETUP transaction. Two details make this a smoking gun:
- The first data or status packet after a control SETUP must use DATA1, not DATA0.
- The packet length of exactly 64 bytes matches a hard-coded value in Tock’s RP2040 bus-reset handler.
The host ACKs the DATA0 packet but apparently discards it as having the wrong data toggle, then retries the IN transaction. That behavior is consistent with duplicate/wrong-toggle handling: the receiver ACKs so the sender does not retransmit forever, but does not accept the packet as the expected DATA1 transaction.
Tock’s current RP2040 USB driver does this during bus reset:
self.dpsram.ep_buf_ctrl[0].ep_in_buf_ctrl.modify(
EP_BUFFER_CONTROL::AVAILABLE0::SET
+ EP_BUFFER_CONTROL::TRANSFER_LENGTH0.val(64)
+ EP_BUFFER_CONTROL::DATA_PID0::CLEAR,
);That gives the RP2040 USB controller ownership of EP0 IN with:
AVAILABLE = 1
length = 64
PID = DATA0That is exactly what appears in packet 140:
DATA0-BAD Data(64 bytes)
The setup-request path subsequently tries to configure the first control-read packet as DATA1. It sets next_pid_in to 1 and calls transmit_in_ep0(). The transmission function then attempts to replace the length and PID and finally sets AVAILABLE.
But AVAILABLE was already set by handle_bus_reset(). The USB controller already owns that buffer and can read it concurrently while the CPU modifies the length, PID, and contents.
The RP2040 datasheet explicitly warns against this. It says USB DPRAM is asynchronous and non-atomic, and software must:
- write the buffer information;
- wait enough system-clock cycles for a USB-clock cycle;
- set AVAILABLE separately and last.
This ensures that the controller cannot observe AVAILABLE=1 with stale length or PID information.
The capture appears to show that exact race: the controller observes the old reset values—64 bytes and DATA0—instead of the newly prepared descriptor response—probably 18 bytes and DATA1.
Source: tock/tock