@@ -93,6 +93,10 @@ impl usb_device::bus::UsbBus for UsbBus {
93
93
Some ( ep) if ep. index ( ) == 0 => {
94
94
self . ep_buf_ctrl ( ep) . set ( 0 ) ;
95
95
96
+ if ep_dir == UsbDirection :: Out {
97
+ self . ep_max_packet_size [ 0 ] = max_packet_size;
98
+ }
99
+
96
100
// EP0 is treated specially by the hardware
97
101
return Ok ( ep) ;
98
102
}
@@ -204,8 +208,9 @@ impl usb_device::bus::UsbBus for UsbBus {
204
208
self . ep_buf_ctrl ( EndpointAddress :: from_parts ( 0 , UsbDirection :: In ) )
205
209
. set ( 0 ) ;
206
210
211
+ // TODO: putting the correct value causes a protocol error. Why?
207
212
self . ep_buf_ctrl ( EndpointAddress :: from_parts ( 0 , UsbDirection :: Out ) )
208
- . set ( EP_BUF_CTRL_PID_DATA1 | EP_BUF_CTRL_AVAIL ) ;
213
+ . set ( 0 ) ;
209
214
210
215
self . ep_in_ready . set ( 0xffff ) ;
211
216
@@ -229,6 +234,15 @@ impl usb_device::bus::UsbBus for UsbBus {
229
234
return Err ( usb_device:: UsbError :: WouldBlock ) ;
230
235
}
231
236
237
+ if ep_i == 0 {
238
+ // When writing to EP0 IN, reset EP0 OUT's state. We could be too
239
+ // late if we tried to do this when a SETUP packet is received.
240
+ self . ep_buf_ctrl ( EndpointAddress :: from_parts ( 0 , UsbDirection :: Out ) )
241
+ . set (
242
+ EP_BUF_CTRL_PID_DATA1 | EP_BUF_CTRL_AVAIL | ( self . ep_max_packet_size [ 0 ] as u32 ) ,
243
+ ) ;
244
+ }
245
+
232
246
let hw_buf = & self . usbctrl_dpram_u8 ( ) [ self . ep_buffer_offset [ ep_i] as _ ..] ;
233
247
for ( x, y) in hw_buf. iter ( ) . zip ( buf. iter ( ) ) {
234
248
x. set ( * y) ;
@@ -286,14 +300,6 @@ impl usb_device::bus::UsbBus for UsbBus {
286
300
. sie_status
287
301
. write ( |b| b. setup_rec ( ) . set_bit ( ) ) ;
288
302
289
- // the first OUT data packet must be DATA1
290
- let buf_ctrl = self . ep_buf_ctrl ( EndpointAddress :: from_parts ( 0 , UsbDirection :: Out ) ) ;
291
- buf_ctrl. set ( buf_ctrl. get ( ) | EP_BUF_CTRL_PID_DATA1 ) ;
292
-
293
- // should respond by DATA1
294
- let buf_ctrl = self . ep_buf_ctrl ( EndpointAddress :: from_parts ( 0 , UsbDirection :: In ) ) ;
295
- buf_ctrl. set ( buf_ctrl. get ( ) & !EP_BUF_CTRL_PID_DATA1 ) ;
296
-
297
303
return Ok ( 8 ) ;
298
304
}
299
305
@@ -388,6 +394,16 @@ impl usb_device::bus::UsbBus for UsbBus {
388
394
389
395
if status. setup_req ( ) . bit ( ) {
390
396
ep_setup |= 1 ;
397
+
398
+ // The first DATA packet to receive by EP0 OUT must be DATA1.
399
+ // However, we can't modify `ep_buf_ctrl((0, Out))` at this point
400
+ // because it may already have a valid PID (DATA0/DATA1) and the
401
+ // hardware may already be receiving the first packet. Therefore,
402
+ // we do this when writing to EP0 In.
403
+
404
+ // The first DATA packet to send to EP0 IN must be DATA1
405
+ self . ep_buf_ctrl ( EndpointAddress :: from_parts ( 0 , UsbDirection :: In ) )
406
+ . set ( 0 /* clear `EP_BUF_CTRL_PID_DATA1` */ ) ;
391
407
}
392
408
393
409
if status. buff_status ( ) . bit ( ) {
0 commit comments