|
46 | 46 | /* Low level controller |
47 | 47 | *------------------------------------------------------------------*/ |
48 | 48 |
|
49 | | -#define usb_hw_set hw_set_alias(usb_hw) |
50 | | -#define usb_hw_clear hw_clear_alias(usb_hw) |
51 | | - |
52 | 49 | // Init these in dcd_init |
53 | 50 | static uint8_t *next_buffer_ptr; |
54 | 51 |
|
@@ -247,104 +244,133 @@ static void __tusb_irq_path_func(reset_non_control_endpoints)(void) |
247 | 244 |
|
248 | 245 | static void __tusb_irq_path_func(dcd_rp2040_irq)(void) |
249 | 246 | { |
250 | | - uint32_t const status = usb_hw->ints; |
251 | | - uint32_t handled = 0; |
252 | | - |
253 | | - if (status & USB_INTF_DEV_SOF_BITS) |
254 | | - { |
255 | | - handled |= USB_INTF_DEV_SOF_BITS; |
| 247 | + uint32_t const status = usb_hw->ints; |
| 248 | + uint32_t handled = 0; |
256 | 249 |
|
257 | | - // disable SOF interrupt if it is used for RESUME in remote wakeup |
258 | | - if (!_sof_enable) usb_hw_clear->inte = USB_INTS_DEV_SOF_BITS; |
| 250 | + if ( status & USB_INTF_DEV_SOF_BITS ) |
| 251 | + { |
| 252 | + bool keep_sof_alive = false; |
259 | 253 |
|
260 | | - dcd_event_sof(0, usb_hw->sof_rd & USB_SOF_RD_BITS, true); |
261 | | - } |
| 254 | + handled |= USB_INTF_DEV_SOF_BITS; |
262 | 255 |
|
263 | | - // xfer events are handled before setup req. So if a transfer completes immediately |
264 | | - // before closing the EP, the events will be delivered in same order. |
265 | | - if (status & USB_INTS_BUFF_STATUS_BITS) |
266 | | - { |
267 | | - handled |= USB_INTS_BUFF_STATUS_BITS; |
268 | | - hw_handle_buff_status(); |
269 | | - } |
| 256 | +#if TUD_OPT_RP2040_USB_DEVICE_UFRAME_FIX |
| 257 | + // Errata 15 workaround for Device Bulk-In endpoint |
| 258 | + e15_last_sof = time_us_32(); |
270 | 259 |
|
271 | | - if (status & USB_INTS_SETUP_REQ_BITS) |
| 260 | + for ( uint8_t i = 0; i < USB_MAX_ENDPOINTS; i++ ) |
272 | 261 | { |
273 | | - handled |= USB_INTS_SETUP_REQ_BITS; |
274 | | - uint8_t const *setup = (uint8_t const *)&usb_dpram->setup_packet; |
275 | | - |
276 | | - // reset pid to both 1 (data and ack) |
277 | | - reset_ep0_pid(); |
| 262 | + struct hw_endpoint * ep = hw_endpoint_get_by_num(i, TUSB_DIR_IN); |
278 | 263 |
|
279 | | - // Pass setup packet to tiny usb |
280 | | - dcd_event_setup_received(0, setup, true); |
281 | | - usb_hw_clear->sie_status = USB_SIE_STATUS_SETUP_REC_BITS; |
282 | | - } |
| 264 | + // Active Bulk IN endpoint requires SOF |
| 265 | + if ( (ep->transfer_type == TUSB_XFER_BULK) && ep->active ) |
| 266 | + { |
| 267 | + keep_sof_alive = true; |
283 | 268 |
|
284 | | -#if FORCE_VBUS_DETECT == 0 |
285 | | - // Since we force VBUS detect On, device will always think it is connected and |
286 | | - // couldn't distinguish between disconnect and suspend |
287 | | - if (status & USB_INTS_DEV_CONN_DIS_BITS) |
288 | | - { |
289 | | - handled |= USB_INTS_DEV_CONN_DIS_BITS; |
| 269 | + hw_endpoint_lock_update(ep, 1); |
290 | 270 |
|
291 | | - if ( usb_hw->sie_status & USB_SIE_STATUS_CONNECTED_BITS ) |
292 | | - { |
293 | | - // Connected: nothing to do |
294 | | - }else |
| 271 | + // Deferred enable? |
| 272 | + if ( ep->pending ) |
295 | 273 | { |
296 | | - // Disconnected |
297 | | - dcd_event_bus_signal(0, DCD_EVENT_UNPLUGGED, true); |
| 274 | + ep->pending = 0; |
| 275 | + hw_endpoint_start_next_buffer(ep); |
298 | 276 | } |
299 | 277 |
|
300 | | - usb_hw_clear->sie_status = USB_SIE_STATUS_CONNECTED_BITS; |
| 278 | + hw_endpoint_lock_update(ep, -1); |
| 279 | + } |
301 | 280 | } |
302 | 281 | #endif |
303 | 282 |
|
304 | | - // SE0 for 2.5 us or more (will last at least 10ms) |
305 | | - if (status & USB_INTS_BUS_RESET_BITS) |
| 283 | + // disable SOF interrupt if it is used for RESUME in remote wakeup |
| 284 | + if ( !keep_sof_alive && !_sof_enable ) usb_hw_clear->inte = USB_INTS_DEV_SOF_BITS; |
| 285 | + |
| 286 | + dcd_event_sof(0, usb_hw->sof_rd & USB_SOF_RD_BITS, true); |
| 287 | + } |
| 288 | + |
| 289 | + // xfer events are handled before setup req. So if a transfer completes immediately |
| 290 | + // before closing the EP, the events will be delivered in same order. |
| 291 | + if ( status & USB_INTS_BUFF_STATUS_BITS ) |
| 292 | + { |
| 293 | + handled |= USB_INTS_BUFF_STATUS_BITS; |
| 294 | + hw_handle_buff_status(); |
| 295 | + } |
| 296 | + |
| 297 | + if ( status & USB_INTS_SETUP_REQ_BITS ) |
| 298 | + { |
| 299 | + handled |= USB_INTS_SETUP_REQ_BITS; |
| 300 | + uint8_t const * setup = (uint8_t const*) &usb_dpram->setup_packet; |
| 301 | + |
| 302 | + // reset pid to both 1 (data and ack) |
| 303 | + reset_ep0_pid(); |
| 304 | + |
| 305 | + // Pass setup packet to tiny usb |
| 306 | + dcd_event_setup_received(0, setup, true); |
| 307 | + usb_hw_clear->sie_status = USB_SIE_STATUS_SETUP_REC_BITS; |
| 308 | + } |
| 309 | + |
| 310 | +#if FORCE_VBUS_DETECT == 0 |
| 311 | + // Since we force VBUS detect On, device will always think it is connected and |
| 312 | + // couldn't distinguish between disconnect and suspend |
| 313 | + if (status & USB_INTS_DEV_CONN_DIS_BITS) |
| 314 | + { |
| 315 | + handled |= USB_INTS_DEV_CONN_DIS_BITS; |
| 316 | + |
| 317 | + if ( usb_hw->sie_status & USB_SIE_STATUS_CONNECTED_BITS ) |
| 318 | + { |
| 319 | + // Connected: nothing to do |
| 320 | + }else |
306 | 321 | { |
307 | | - pico_trace("BUS RESET\n"); |
| 322 | + // Disconnected |
| 323 | + dcd_event_bus_signal(0, DCD_EVENT_UNPLUGGED, true); |
| 324 | + } |
| 325 | + |
| 326 | + usb_hw_clear->sie_status = USB_SIE_STATUS_CONNECTED_BITS; |
| 327 | + } |
| 328 | +#endif |
| 329 | + |
| 330 | + // SE0 for 2.5 us or more (will last at least 10ms) |
| 331 | + if ( status & USB_INTS_BUS_RESET_BITS ) |
| 332 | + { |
| 333 | + pico_trace("BUS RESET\n"); |
308 | 334 |
|
309 | | - handled |= USB_INTS_BUS_RESET_BITS; |
| 335 | + handled |= USB_INTS_BUS_RESET_BITS; |
310 | 336 |
|
311 | | - usb_hw->dev_addr_ctrl = 0; |
312 | | - reset_non_control_endpoints(); |
313 | | - dcd_event_bus_reset(0, TUSB_SPEED_FULL, true); |
314 | | - usb_hw_clear->sie_status = USB_SIE_STATUS_BUS_RESET_BITS; |
| 337 | + usb_hw->dev_addr_ctrl = 0; |
| 338 | + reset_non_control_endpoints(); |
| 339 | + dcd_event_bus_reset(0, TUSB_SPEED_FULL, true); |
| 340 | + usb_hw_clear->sie_status = USB_SIE_STATUS_BUS_RESET_BITS; |
315 | 341 |
|
316 | 342 | #if TUD_OPT_RP2040_USB_DEVICE_ENUMERATION_FIX |
317 | | - // Only run enumeration walk-around if pull up is enabled |
318 | | - if ( usb_hw->sie_ctrl & USB_SIE_CTRL_PULLUP_EN_BITS ) rp2040_usb_device_enumeration_fix(); |
| 343 | + // Only run enumeration workaround if pull up is enabled |
| 344 | + if ( usb_hw->sie_ctrl & USB_SIE_CTRL_PULLUP_EN_BITS ) rp2040_usb_device_enumeration_fix(); |
319 | 345 | #endif |
320 | | - } |
| 346 | + } |
321 | 347 |
|
322 | | - /* Note from pico datasheet 4.1.2.6.4 (v1.2) |
323 | | - * If you enable the suspend interrupt, it is likely you will see a suspend interrupt when |
324 | | - * the device is first connected but the bus is idle. The bus can be idle for a few ms before |
325 | | - * the host begins sending start of frame packets. You will also see a suspend interrupt |
326 | | - * when the device is disconnected if you do not have a VBUS detect circuit connected. This is |
327 | | - * because without VBUS detection, it is impossible to tell the difference between |
328 | | - * being disconnected and suspended. |
329 | | - */ |
330 | | - if (status & USB_INTS_DEV_SUSPEND_BITS) |
331 | | - { |
332 | | - handled |= USB_INTS_DEV_SUSPEND_BITS; |
333 | | - dcd_event_bus_signal(0, DCD_EVENT_SUSPEND, true); |
334 | | - usb_hw_clear->sie_status = USB_SIE_STATUS_SUSPENDED_BITS; |
335 | | - } |
| 348 | + /* Note from pico datasheet 4.1.2.6.4 (v1.2) |
| 349 | + * If you enable the suspend interrupt, it is likely you will see a suspend interrupt when |
| 350 | + * the device is first connected but the bus is idle. The bus can be idle for a few ms before |
| 351 | + * the host begins sending start of frame packets. You will also see a suspend interrupt |
| 352 | + * when the device is disconnected if you do not have a VBUS detect circuit connected. This is |
| 353 | + * because without VBUS detection, it is impossible to tell the difference between |
| 354 | + * being disconnected and suspended. |
| 355 | + */ |
| 356 | + if ( status & USB_INTS_DEV_SUSPEND_BITS ) |
| 357 | + { |
| 358 | + handled |= USB_INTS_DEV_SUSPEND_BITS; |
| 359 | + dcd_event_bus_signal(0, DCD_EVENT_SUSPEND, true); |
| 360 | + usb_hw_clear->sie_status = USB_SIE_STATUS_SUSPENDED_BITS; |
| 361 | + } |
336 | 362 |
|
337 | | - if (status & USB_INTS_DEV_RESUME_FROM_HOST_BITS) |
338 | | - { |
339 | | - handled |= USB_INTS_DEV_RESUME_FROM_HOST_BITS; |
340 | | - dcd_event_bus_signal(0, DCD_EVENT_RESUME, true); |
341 | | - usb_hw_clear->sie_status = USB_SIE_STATUS_RESUME_BITS; |
342 | | - } |
| 363 | + if ( status & USB_INTS_DEV_RESUME_FROM_HOST_BITS ) |
| 364 | + { |
| 365 | + handled |= USB_INTS_DEV_RESUME_FROM_HOST_BITS; |
| 366 | + dcd_event_bus_signal(0, DCD_EVENT_RESUME, true); |
| 367 | + usb_hw_clear->sie_status = USB_SIE_STATUS_RESUME_BITS; |
| 368 | + } |
343 | 369 |
|
344 | | - if (status ^ handled) |
345 | | - { |
346 | | - panic("Unhandled IRQ 0x%x\n", (uint) (status ^ handled)); |
347 | | - } |
| 370 | + if ( status ^ handled ) |
| 371 | + { |
| 372 | + panic("Unhandled IRQ 0x%x\n", (uint) (status ^ handled)); |
| 373 | + } |
348 | 374 | } |
349 | 375 |
|
350 | 376 | #define USB_INTS_ERROR_BITS ( \ |
@@ -452,7 +478,11 @@ void dcd_sof_enable(uint8_t rhport, bool en) |
452 | 478 | usb_hw_set->inte = USB_INTS_DEV_SOF_BITS; |
453 | 479 | }else |
454 | 480 | { |
| 481 | + // Don't clear immediately if the SOF workaround is in use. |
| 482 | + // The SOF handler will conditionally disable the interrupt. |
| 483 | +#if !TUD_OPT_RP2040_USB_DEVICE_UFRAME_FIX |
455 | 484 | usb_hw_clear->inte = USB_INTS_DEV_SOF_BITS; |
| 485 | +#endif |
456 | 486 | } |
457 | 487 | } |
458 | 488 |
|
|
0 commit comments