From 0cce42fcc672cb3e0d2fe16c20b46aab9d8f94d5 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 31 Jan 2023 11:38:15 +0700 Subject: [PATCH] minor clean up --- src/portable/raspberrypi/rp2040/dcd_rp2040.c | 19 ++++++++------ src/portable/raspberrypi/rp2040/rp2040_usb.c | 26 +++++++++++++------- src/portable/raspberrypi/rp2040/rp2040_usb.h | 2 ++ 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/portable/raspberrypi/rp2040/dcd_rp2040.c b/src/portable/raspberrypi/rp2040/dcd_rp2040.c index 35faba628..d5a105a29 100644 --- a/src/portable/raspberrypi/rp2040/dcd_rp2040.c +++ b/src/portable/raspberrypi/rp2040/dcd_rp2040.c @@ -246,32 +246,37 @@ static void __tusb_irq_path_func(dcd_rp2040_irq)(void) { uint32_t const status = usb_hw->ints; uint32_t handled = 0; - bool keep_sof_alive = false; if (status & USB_INTF_DEV_SOF_BITS) { + bool keep_sof_alive = false; + handled |= USB_INTF_DEV_SOF_BITS; #if TUD_OPT_RP2040_USB_DEVICE_UFRAME_FIX last_sof = time_us_32(); - for (uint8_t i = 0; i < USB_MAX_ENDPOINTS; i++) { + for (uint8_t i = 0; i < USB_MAX_ENDPOINTS; i++) + { struct hw_endpoint *ep = hw_endpoint_get_by_num(i, TUSB_DIR_IN); hw_endpoint_lock_update(ep, 1); + // Bulk IN endpoint in a transfer? - if (rp2040_ep_needs_sof(ep) && ep->active) - keep_sof_alive = true; + if (rp2040_ep_needs_sof(ep) && ep->active) keep_sof_alive = true; + // Deferred enable? - if (ep->pending) { + if (ep->pending) + { hw_endpoint_start_next_buffer(ep); ep->pending = 0; } + hw_endpoint_lock_update(ep, -1); } #endif + // disable SOF interrupt if it is used for RESUME in remote wakeup - if (!keep_sof_alive && !_sof_enable) - usb_hw_clear->inte = USB_INTS_DEV_SOF_BITS; + if (!keep_sof_alive && !_sof_enable) usb_hw_clear->inte = USB_INTS_DEV_SOF_BITS; dcd_event_sof(0, usb_hw->sof_rd & USB_SOF_RD_BITS, true); } diff --git a/src/portable/raspberrypi/rp2040/rp2040_usb.c b/src/portable/raspberrypi/rp2040/rp2040_usb.c index 6a9f162a5..b7790e0c1 100644 --- a/src/portable/raspberrypi/rp2040/rp2040_usb.c +++ b/src/portable/raspberrypi/rp2040/rp2040_usb.c @@ -57,13 +57,14 @@ bool rp2040_critical_frame_period(struct hw_endpoint *ep) { uint32_t delta; - if (usb_hw->main_ctrl & USB_MAIN_CTRL_HOST_NDEVICE_BITS) - return false; + if (usb_hw->main_ctrl & USB_MAIN_CTRL_HOST_NDEVICE_BITS) return false; if (tu_edpt_dir(ep->ep_addr) == TUSB_DIR_OUT || ep->transfer_type == TUSB_XFER_INTERRUPT || ep->transfer_type == TUSB_XFER_ISOCHRONOUS) + { return false; + } /* Avoid the last 200us (uframe 6.5-7) of a frame, up to the EOF2 point. * The device state machine cannot recover from receiving an incorrect PID @@ -78,10 +79,8 @@ bool rp2040_critical_frame_period(struct hw_endpoint *ep) } bool rp2040_ep_needs_sof(struct hw_endpoint *ep) { - if (tu_edpt_dir(ep->ep_addr) == TUSB_DIR_IN && - ep->transfer_type == TUSB_XFER_BULK) - return true; - return false; + return (tu_edpt_dir(ep->ep_addr) == TUSB_DIR_IN && + ep->transfer_type == TUSB_XFER_BULK); } #endif @@ -115,12 +114,15 @@ void __tusb_irq_path_func(hw_endpoint_reset_transfer)(struct hw_endpoint *ep) ep->user_buf = 0; } -void __tusb_irq_path_func(_hw_endpoint_buffer_control_update32)(struct hw_endpoint *ep, uint32_t and_mask, uint32_t or_mask) { +void __tusb_irq_path_func(_hw_endpoint_buffer_control_update32)(struct hw_endpoint *ep, uint32_t and_mask, uint32_t or_mask) +{ uint32_t value = 0; + if ( and_mask ) { value = *ep->buffer_control & and_mask; } + if ( or_mask ) { value |= or_mask; @@ -146,6 +148,7 @@ void __tusb_irq_path_func(_hw_endpoint_buffer_control_update32)(struct hw_endpoi #endif } } + *ep->buffer_control = value; } @@ -247,13 +250,18 @@ void hw_endpoint_xfer_start(struct hw_endpoint *ep, uint8_t *buffer, uint16_t to ep->user_buf = buffer; if (rp2040_ep_needs_sof(ep)) + { usb_hw_set->inte = USB_INTS_DEV_SOF_BITS; + } - if(!rp2040_critical_frame_period(ep)) { + if(!rp2040_critical_frame_period(ep)) + { hw_endpoint_start_next_buffer(ep); - } else { + } else + { ep->pending = 1; } + hw_endpoint_lock_update(ep, -1); } diff --git a/src/portable/raspberrypi/rp2040/rp2040_usb.h b/src/portable/raspberrypi/rp2040/rp2040_usb.h index e7fee3e3a..d41abfee0 100644 --- a/src/portable/raspberrypi/rp2040/rp2040_usb.h +++ b/src/portable/raspberrypi/rp2040/rp2040_usb.h @@ -82,6 +82,7 @@ typedef struct hw_endpoint // Transfer scheduled but not active uint8_t pending; + #if CFG_TUH_ENABLED // Only needed for host uint8_t dev_addr; @@ -89,6 +90,7 @@ typedef struct hw_endpoint // If interrupt endpoint uint8_t interrupt_num; #endif + } hw_endpoint_t; #if !TUD_OPT_RP2040_USB_DEVICE_UFRAME_FIX