double buffered work with host
This commit is contained in:
parent
43656dc0a7
commit
a1a03c92f6
|
@ -198,10 +198,10 @@ static void hw_endpoint_init(uint8_t ep_addr, uint16_t wMaxPacketSize, uint8_t b
|
||||||
_hw_endpoint_init(ep, ep_addr, wMaxPacketSize, bmAttributes);
|
_hw_endpoint_init(ep, ep_addr, wMaxPacketSize, bmAttributes);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void hw_endpoint_xfer(uint8_t ep_addr, uint8_t *buffer, uint16_t total_bytes, bool start)
|
static void hw_endpoint_xfer(uint8_t ep_addr, uint8_t *buffer, uint16_t total_bytes)
|
||||||
{
|
{
|
||||||
struct hw_endpoint *ep = hw_endpoint_get_by_addr(ep_addr);
|
struct hw_endpoint *ep = hw_endpoint_get_by_addr(ep_addr);
|
||||||
_hw_endpoint_xfer(ep, buffer, total_bytes, start);
|
_hw_endpoint_xfer_start(ep, buffer, total_bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void hw_handle_buff_status(void)
|
static void hw_handle_buff_status(void)
|
||||||
|
@ -251,7 +251,7 @@ static void ep0_0len_status(void)
|
||||||
{
|
{
|
||||||
// Send 0len complete response on EP0 IN
|
// Send 0len complete response on EP0 IN
|
||||||
reset_ep0();
|
reset_ep0();
|
||||||
hw_endpoint_xfer(0x80, NULL, 0, true);
|
hw_endpoint_xfer(0x80, NULL, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _hw_endpoint_stall(struct hw_endpoint *ep)
|
static void _hw_endpoint_stall(struct hw_endpoint *ep)
|
||||||
|
@ -477,7 +477,7 @@ void dcd_edpt0_status_complete(uint8_t rhport, tusb_control_request_t const * re
|
||||||
request->bmRequestType_bit.type == TUSB_REQ_TYPE_STANDARD &&
|
request->bmRequestType_bit.type == TUSB_REQ_TYPE_STANDARD &&
|
||||||
request->bRequest == TUSB_REQ_SET_ADDRESS)
|
request->bRequest == TUSB_REQ_SET_ADDRESS)
|
||||||
{
|
{
|
||||||
pico_trace("Set HW address %d\n", assigned_address);
|
pico_trace("Set HW address %d\n", request->wValue);
|
||||||
usb_hw->dev_addr_ctrl = (uint8_t) request->wValue;
|
usb_hw->dev_addr_ctrl = (uint8_t) request->wValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -496,7 +496,7 @@ bool dcd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t t
|
||||||
{
|
{
|
||||||
assert(rhport == 0);
|
assert(rhport == 0);
|
||||||
// True means start new xfer
|
// True means start new xfer
|
||||||
hw_endpoint_xfer(ep_addr, buffer, total_bytes, true);
|
hw_endpoint_xfer(ep_addr, buffer, total_bytes);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -115,9 +115,9 @@ static void hw_xfer_complete(struct hw_endpoint *ep, xfer_result_t xfer_result)
|
||||||
// Mark transfer as done before we tell the tinyusb stack
|
// Mark transfer as done before we tell the tinyusb stack
|
||||||
uint8_t dev_addr = ep->dev_addr;
|
uint8_t dev_addr = ep->dev_addr;
|
||||||
uint8_t ep_addr = ep->ep_addr;
|
uint8_t ep_addr = ep->ep_addr;
|
||||||
uint total_len = ep->total_len;
|
uint xferred_len = ep->len;
|
||||||
hw_endpoint_reset_transfer(ep);
|
hw_endpoint_reset_transfer(ep);
|
||||||
hcd_event_xfer_complete(dev_addr, ep_addr, total_len, xfer_result, true);
|
hcd_event_xfer_complete(dev_addr, ep_addr, xferred_len, xfer_result, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void _handle_buff_status_bit(uint bit, struct hw_endpoint *ep)
|
static void _handle_buff_status_bit(uint bit, struct hw_endpoint *ep)
|
||||||
|
@ -141,6 +141,20 @@ static void hw_handle_buff_status(void)
|
||||||
{
|
{
|
||||||
remaining_buffers &= ~bit;
|
remaining_buffers &= ~bit;
|
||||||
struct hw_endpoint *ep = &epx;
|
struct hw_endpoint *ep = &epx;
|
||||||
|
|
||||||
|
uint32_t ep_ctrl = *ep->endpoint_control;
|
||||||
|
if (ep_ctrl & EP_CTRL_DOUBLE_BUFFERED_BITS)
|
||||||
|
{
|
||||||
|
TU_LOG(2, "Double Buffered ");
|
||||||
|
}else
|
||||||
|
{
|
||||||
|
TU_LOG(2, "Single Buffered ");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ep_ctrl & EP_CTRL_INTERRUPT_PER_DOUBLE_BUFFER) TU_LOG(2, "Interrupt per double ");
|
||||||
|
if (ep_ctrl & EP_CTRL_INTERRUPT_PER_BUFFER) TU_LOG(2, "Interrupt per single ");
|
||||||
|
TU_LOG_HEX(2, ep_ctrl);
|
||||||
|
|
||||||
_handle_buff_status_bit(bit, ep);
|
_handle_buff_status_bit(bit, ep);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -277,11 +291,11 @@ static struct hw_endpoint *_hw_endpoint_allocate(uint8_t transfer_type)
|
||||||
assert(ep);
|
assert(ep);
|
||||||
ep->buffer_control = &usbh_dpram->int_ep_buffer_ctrl[ep->interrupt_num].ctrl;
|
ep->buffer_control = &usbh_dpram->int_ep_buffer_ctrl[ep->interrupt_num].ctrl;
|
||||||
ep->endpoint_control = &usbh_dpram->int_ep_ctrl[ep->interrupt_num].ctrl;
|
ep->endpoint_control = &usbh_dpram->int_ep_ctrl[ep->interrupt_num].ctrl;
|
||||||
// 0x180 for epx
|
// 0 for epx (double buffered): TODO increase to 1024 for ISO
|
||||||
// 0x1c0 for intep0
|
// 2x64 for intep0
|
||||||
// 0x200 for intep1
|
// 3x64 for intep1
|
||||||
// etc
|
// etc
|
||||||
ep->hw_data_buf = &usbh_dpram->epx_data[64 * (ep->interrupt_num + 1)];
|
ep->hw_data_buf = &usbh_dpram->epx_data[64 * (ep->interrupt_num + 2)];
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -311,7 +325,7 @@ static void _hw_endpoint_init(struct hw_endpoint *ep, uint8_t dev_addr, uint8_t
|
||||||
ep->rx = (dir == TUSB_DIR_IN);
|
ep->rx = (dir == TUSB_DIR_IN);
|
||||||
|
|
||||||
// Response to a setup packet on EP0 starts with pid of 1
|
// Response to a setup packet on EP0 starts with pid of 1
|
||||||
ep->next_pid = num == 0 ? 1u : 0u;
|
ep->next_pid = (num == 0 ? 1u : 0u);
|
||||||
ep->wMaxPacketSize = wMaxPacketSize;
|
ep->wMaxPacketSize = wMaxPacketSize;
|
||||||
ep->transfer_type = transfer_type;
|
ep->transfer_type = transfer_type;
|
||||||
|
|
||||||
|
@ -340,6 +354,7 @@ static void _hw_endpoint_init(struct hw_endpoint *ep, uint8_t dev_addr, uint8_t
|
||||||
// preamble
|
// preamble
|
||||||
uint32_t reg = dev_addr | (num << USB_ADDR_ENDP1_ENDPOINT_LSB);
|
uint32_t reg = dev_addr | (num << USB_ADDR_ENDP1_ENDPOINT_LSB);
|
||||||
// Assert the interrupt endpoint is IN_TO_HOST
|
// Assert the interrupt endpoint is IN_TO_HOST
|
||||||
|
// TODO Interrupt can also be OUT
|
||||||
assert(dir == TUSB_DIR_IN);
|
assert(dir == TUSB_DIR_IN);
|
||||||
|
|
||||||
if (need_pre(dev_addr))
|
if (need_pre(dev_addr))
|
||||||
|
@ -402,7 +417,6 @@ bool hcd_port_connect_status(uint8_t rhport)
|
||||||
|
|
||||||
tusb_speed_t hcd_port_speed_get(uint8_t rhport)
|
tusb_speed_t hcd_port_speed_get(uint8_t rhport)
|
||||||
{
|
{
|
||||||
pico_trace("hcd_port_speed_get\n");
|
|
||||||
assert(rhport == 0);
|
assert(rhport == 0);
|
||||||
// TODO: Should enumval this register
|
// TODO: Should enumval this register
|
||||||
switch (dev_speed())
|
switch (dev_speed())
|
||||||
|
@ -445,6 +459,10 @@ void hcd_int_disable(uint8_t rhport)
|
||||||
irq_set_enabled(USBCTRL_IRQ, false);
|
irq_set_enabled(USBCTRL_IRQ, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------+
|
||||||
|
// Endpoint API
|
||||||
|
//--------------------------------------------------------------------+
|
||||||
|
|
||||||
bool hcd_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const * ep_desc)
|
bool hcd_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const * ep_desc)
|
||||||
{
|
{
|
||||||
(void) rhport;
|
(void) rhport;
|
||||||
|
@ -467,6 +485,65 @@ bool hcd_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// return true if double buffered
|
||||||
|
static bool xfer_start(struct hw_endpoint *ep, uint8_t *buffer, uint16_t total_len)
|
||||||
|
{
|
||||||
|
// Fill in info now that we're kicking off the hw
|
||||||
|
ep->total_len = total_len;
|
||||||
|
ep->len = 0;
|
||||||
|
|
||||||
|
// Limit by packet size
|
||||||
|
ep->user_buf = buffer;
|
||||||
|
|
||||||
|
// Buffer 0
|
||||||
|
ep->transfer_size = tu_min16(total_len, ep->wMaxPacketSize);
|
||||||
|
total_len -= ep->transfer_size;
|
||||||
|
|
||||||
|
// Buffer 1
|
||||||
|
ep->buf_1_len = tu_min16(total_len, ep->wMaxPacketSize);
|
||||||
|
total_len -= ep->buf_1_len;
|
||||||
|
|
||||||
|
ep->active = true;
|
||||||
|
|
||||||
|
// Write buffer control
|
||||||
|
|
||||||
|
// Buffer 0
|
||||||
|
uint32_t bufctrl = ep->transfer_size | USB_BUF_CTRL_AVAIL;
|
||||||
|
|
||||||
|
// Copy data to DPB if tx
|
||||||
|
if (!ep->rx)
|
||||||
|
{
|
||||||
|
// Copy data from user buffer to hw buffer
|
||||||
|
memcpy(ep->hw_data_buf, ep->user_buf, ep->transfer_size + ep->buf_1_len);
|
||||||
|
|
||||||
|
// Mark as full
|
||||||
|
bufctrl |= USB_BUF_CTRL_FULL | (ep->buf_1_len ? (USB_BUF_CTRL_FULL << 16) : 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// PID
|
||||||
|
bufctrl |= ep->next_pid ? USB_BUF_CTRL_DATA1_PID : USB_BUF_CTRL_DATA0_PID;
|
||||||
|
ep->next_pid ^= 1u;
|
||||||
|
|
||||||
|
if (ep->buf_1_len)
|
||||||
|
{
|
||||||
|
bufctrl |= (ep->buf_1_len | USB_BUF_CTRL_AVAIL) << 16;
|
||||||
|
bufctrl |= (ep->next_pid ? USB_BUF_CTRL_DATA1_PID : USB_BUF_CTRL_DATA0_PID) << 16;
|
||||||
|
ep->next_pid ^= 1u;
|
||||||
|
}
|
||||||
|
|
||||||
|
// determine which buffer is last
|
||||||
|
if (total_len == 0)
|
||||||
|
{
|
||||||
|
bufctrl |= USB_BUF_CTRL_LAST << (ep->buf_1_len ? 16 : 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
print_bufctrl32(bufctrl);
|
||||||
|
|
||||||
|
_hw_endpoint_buffer_control_set_value32(ep, bufctrl);
|
||||||
|
|
||||||
|
return ep->buf_1_len > 0;
|
||||||
|
}
|
||||||
|
|
||||||
bool hcd_edpt_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t buflen)
|
bool hcd_edpt_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t buflen)
|
||||||
{
|
{
|
||||||
(void) rhport;
|
(void) rhport;
|
||||||
|
@ -486,13 +563,12 @@ bool hcd_edpt_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr, uint8_t *
|
||||||
_hw_endpoint_init(ep, dev_addr, ep_addr, ep->wMaxPacketSize, ep->transfer_type, 0);
|
_hw_endpoint_init(ep, dev_addr, ep_addr, ep->wMaxPacketSize, ep->transfer_type, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start the transfer
|
|
||||||
_hw_endpoint_xfer_start(ep, buffer, buflen);
|
|
||||||
|
|
||||||
// If a normal transfer (non-interrupt) then initiate using
|
// If a normal transfer (non-interrupt) then initiate using
|
||||||
// sie ctrl registers. Otherwise interrupt ep registers should
|
// sie ctrl registers. Otherwise interrupt ep registers should
|
||||||
// already be configured
|
// already be configured
|
||||||
if (ep == &epx) {
|
if (ep == &epx) {
|
||||||
|
_hw_endpoint_xfer_start(ep, buffer, buflen);
|
||||||
|
|
||||||
// That has set up buffer control, endpoint control etc
|
// That has set up buffer control, endpoint control etc
|
||||||
// for host we have to initiate the transfer
|
// for host we have to initiate the transfer
|
||||||
usb_hw->dev_addr_ctrl = dev_addr | (ep_num << USB_ADDR_ENDP_ENDPOINT_LSB);
|
usb_hw->dev_addr_ctrl = dev_addr | (ep_num << USB_ADDR_ENDP_ENDPOINT_LSB);
|
||||||
|
@ -503,6 +579,9 @@ bool hcd_edpt_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr, uint8_t *
|
||||||
flags |= need_pre(dev_addr) ? USB_SIE_CTRL_PREAMBLE_EN_BITS : 0;
|
flags |= need_pre(dev_addr) ? USB_SIE_CTRL_PREAMBLE_EN_BITS : 0;
|
||||||
|
|
||||||
usb_hw->sie_ctrl = flags;
|
usb_hw->sie_ctrl = flags;
|
||||||
|
}else
|
||||||
|
{
|
||||||
|
_hw_endpoint_xfer_start(ep, buffer, buflen);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -556,8 +635,8 @@ bool hcd_setup_send(uint8_t rhport, uint8_t dev_addr, uint8_t const setup_packet
|
||||||
|
|
||||||
bool hcd_edpt_clear_stall(uint8_t dev_addr, uint8_t ep_addr)
|
bool hcd_edpt_clear_stall(uint8_t dev_addr, uint8_t ep_addr)
|
||||||
{
|
{
|
||||||
(void) rhport;
|
|
||||||
(void) dev_addr;
|
(void) dev_addr;
|
||||||
|
(void) ep_addr;
|
||||||
|
|
||||||
panic("hcd_clear_stall");
|
panic("hcd_clear_stall");
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -61,11 +61,11 @@ void rp2040_usb_init(void)
|
||||||
memset(usb_dpram, 0, sizeof(*usb_dpram));
|
memset(usb_dpram, 0, sizeof(*usb_dpram));
|
||||||
|
|
||||||
// Mux the controller to the onboard usb phy
|
// Mux the controller to the onboard usb phy
|
||||||
usb_hw->muxing = USB_USB_MUXING_TO_PHY_BITS | USB_USB_MUXING_SOFTCON_BITS;
|
usb_hw->muxing = USB_USB_MUXING_TO_PHY_BITS | USB_USB_MUXING_SOFTCON_BITS;
|
||||||
|
|
||||||
// Force VBUS detect so the device thinks it is plugged into a host
|
// Force VBUS detect so the device thinks it is plugged into a host
|
||||||
// TODO support VBUs detect
|
// TODO support VBUs detect
|
||||||
usb_hw->pwr = USB_USB_PWR_VBUS_DETECT_BITS | USB_USB_PWR_VBUS_DETECT_OVERRIDE_EN_BITS;
|
usb_hw->pwr = USB_USB_PWR_VBUS_DETECT_BITS | USB_USB_PWR_VBUS_DETECT_OVERRIDE_EN_BITS;
|
||||||
}
|
}
|
||||||
|
|
||||||
void hw_endpoint_reset_transfer(struct hw_endpoint *ep)
|
void hw_endpoint_reset_transfer(struct hw_endpoint *ep)
|
||||||
|
@ -111,150 +111,157 @@ void _hw_endpoint_buffer_control_update32(struct hw_endpoint *ep, uint32_t and_m
|
||||||
*ep->buffer_control = value;
|
*ep->buffer_control = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Prepare buffer control register value
|
||||||
void _hw_endpoint_start_next_buffer(struct hw_endpoint *ep)
|
void _hw_endpoint_start_next_buffer(struct hw_endpoint *ep)
|
||||||
{
|
{
|
||||||
// Prepare buffer control register value
|
uint16_t remaining = ep->total_len - ep->len;
|
||||||
uint32_t val = ep->transfer_size | USB_BUF_CTRL_AVAIL;
|
uint32_t ep_ctrl = *ep->endpoint_control;
|
||||||
|
uint32_t buf_ctrl;
|
||||||
|
|
||||||
if (!ep->rx)
|
// Buffer 0
|
||||||
{
|
ep->transfer_size = tu_min16(remaining, ep->wMaxPacketSize);
|
||||||
// Copy data from user buffer to hw buffer
|
remaining -= ep->transfer_size;
|
||||||
memcpy(ep->hw_data_buf, &ep->user_buf[ep->len], ep->transfer_size);
|
|
||||||
// Mark as full
|
|
||||||
val |= USB_BUF_CTRL_FULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
// PID
|
buf_ctrl = ep->transfer_size | USB_BUF_CTRL_AVAIL;
|
||||||
val |= ep->next_pid ? USB_BUF_CTRL_DATA1_PID : USB_BUF_CTRL_DATA0_PID;
|
if ( !ep->rx )
|
||||||
|
{
|
||||||
|
// Copy data from user buffer to hw buffer
|
||||||
|
memcpy(ep->hw_data_buf, ep->user_buf+ep->len, ep->transfer_size);
|
||||||
|
|
||||||
#if TUSB_OPT_DEVICE_ENABLED
|
// Mark as full
|
||||||
|
buf_ctrl |= USB_BUF_CTRL_FULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// PID
|
||||||
|
buf_ctrl |= ep->next_pid ? USB_BUF_CTRL_DATA1_PID : USB_BUF_CTRL_DATA0_PID;
|
||||||
|
ep->next_pid ^= 1u;
|
||||||
|
|
||||||
|
// Buffer 1
|
||||||
|
ep->buf_1_len = tu_min16(remaining, ep->wMaxPacketSize);
|
||||||
|
remaining -= ep->buf_1_len;
|
||||||
|
|
||||||
|
if (ep->buf_1_len)
|
||||||
|
{
|
||||||
|
buf_ctrl |= (ep->buf_1_len | USB_BUF_CTRL_AVAIL) << 16;
|
||||||
|
buf_ctrl |= (ep->next_pid ? USB_BUF_CTRL_DATA1_PID : USB_BUF_CTRL_DATA0_PID) << 16;
|
||||||
ep->next_pid ^= 1u;
|
ep->next_pid ^= 1u;
|
||||||
|
|
||||||
#else
|
if ( !ep->rx )
|
||||||
// For Host (also device but since we dictate the endpoint size, following scenario does not occur)
|
|
||||||
// Next PID depends on the number of packet in case wMaxPacketSize < 64 (e.g Interrupt Endpoint 8, or 12)
|
|
||||||
// Special case with control status stage where PID is always DATA1
|
|
||||||
if ( ep->transfer_size == 0 )
|
|
||||||
{
|
{
|
||||||
// ZLP also toggle data
|
// Copy data from user buffer to hw buffer
|
||||||
ep->next_pid ^= 1u;
|
memcpy(ep->hw_data_buf+64, ep->user_buf+ep->len+ep->transfer_size, ep->buf_1_len);
|
||||||
}else
|
|
||||||
{
|
|
||||||
uint32_t packet_count = 1 + ((ep->transfer_size - 1) / ep->wMaxPacketSize);
|
|
||||||
|
|
||||||
if ( packet_count & 0x01 )
|
|
||||||
{
|
|
||||||
ep->next_pid ^= 1u;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
|
// Set endpoint control double buffered bit if needed
|
||||||
|
ep_ctrl &= ~EP_CTRL_INTERRUPT_PER_BUFFER;
|
||||||
|
ep_ctrl |= EP_CTRL_DOUBLE_BUFFERED_BITS | EP_CTRL_INTERRUPT_PER_DOUBLE_BUFFER;
|
||||||
|
}else
|
||||||
|
{
|
||||||
|
ep_ctrl &= ~(EP_CTRL_DOUBLE_BUFFERED_BITS | EP_CTRL_INTERRUPT_PER_DOUBLE_BUFFER);
|
||||||
|
ep_ctrl |= EP_CTRL_INTERRUPT_PER_BUFFER;
|
||||||
|
}
|
||||||
|
|
||||||
|
*ep->endpoint_control = ep_ctrl;
|
||||||
|
|
||||||
#if TUSB_OPT_HOST_ENABLED
|
#if TUSB_OPT_HOST_ENABLED
|
||||||
// Is this the last buffer? Only really matters for host mode. Will trigger
|
// Is this the last buffer? Only really matters for host mode. Will trigger
|
||||||
// the trans complete irq but also stop it polling. We only really care about
|
// the trans complete irq but also stop it polling. We only really care about
|
||||||
// trans complete for setup packets being sent
|
// trans complete for setup packets being sent
|
||||||
if (ep->last_buf)
|
if (remaining == 0)
|
||||||
{
|
{
|
||||||
pico_trace("Last buf (%d bytes left)\n", ep->transfer_size);
|
buf_ctrl |= USB_BUF_CTRL_LAST << (ep->buf_1_len ? 16 : 0);
|
||||||
val |= USB_BUF_CTRL_LAST;
|
}
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Finally, write to buffer_control which will trigger the transfer
|
print_bufctrl32(buf_ctrl);
|
||||||
// the next time the controller polls this dpram address
|
|
||||||
_hw_endpoint_buffer_control_set_value32(ep, val);
|
// Finally, write to buffer_control which will trigger the transfer
|
||||||
pico_trace("buffer control (0x%p) <- 0x%x\n", ep->buffer_control, val);
|
// the next time the controller polls this dpram address
|
||||||
//print_bufctrl16(val);
|
_hw_endpoint_buffer_control_set_value32(ep, buf_ctrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void _hw_endpoint_xfer_start(struct hw_endpoint *ep, uint8_t *buffer, uint16_t total_len)
|
void _hw_endpoint_xfer_start(struct hw_endpoint *ep, uint8_t *buffer, uint16_t total_len)
|
||||||
{
|
{
|
||||||
_hw_endpoint_lock_update(ep, 1);
|
_hw_endpoint_lock_update(ep, 1);
|
||||||
pico_trace("Start transfer of total len %d on ep %d %s\n", total_len, tu_edpt_number(ep->ep_addr), ep_dir_string[tu_edpt_dir(ep->ep_addr)]);
|
pico_trace("Start transfer of total len %d on ep %d %s\n", total_len, tu_edpt_number(ep->ep_addr),
|
||||||
if (ep->active)
|
ep_dir_string[tu_edpt_dir(ep->ep_addr)]);
|
||||||
{
|
if ( ep->active )
|
||||||
// TODO: Is this acceptable for interrupt packets?
|
{
|
||||||
pico_warn("WARN: starting new transfer on already active ep %d %s\n", tu_edpt_number(ep->ep_addr), ep_dir_string[tu_edpt_dir(ep->ep_addr)]);
|
// TODO: Is this acceptable for interrupt packets?
|
||||||
|
pico_warn("WARN: starting new transfer on already active ep %d %s\n", tu_edpt_number(ep->ep_addr),
|
||||||
|
ep_dir_string[tu_edpt_dir(ep->ep_addr)]);
|
||||||
|
|
||||||
hw_endpoint_reset_transfer(ep);
|
hw_endpoint_reset_transfer(ep);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fill in info now that we're kicking off the hw
|
// Fill in info now that we're kicking off the hw
|
||||||
ep->total_len = total_len;
|
ep->total_len = total_len;
|
||||||
ep->len = 0;
|
ep->len = 0;
|
||||||
|
ep->active = true;
|
||||||
|
ep->user_buf = buffer;
|
||||||
|
|
||||||
// Limit by packet size but not less 64 (i.e low speed 8 bytes EP0)
|
_hw_endpoint_start_next_buffer(ep);
|
||||||
ep->transfer_size = tu_min16(total_len, tu_max16(64, ep->wMaxPacketSize));
|
_hw_endpoint_lock_update(ep, -1);
|
||||||
|
|
||||||
ep->active = true;
|
|
||||||
ep->user_buf = buffer;
|
|
||||||
#if TUSB_OPT_HOST_ENABLED
|
|
||||||
// Recalculate if this is the last buffer
|
|
||||||
_hw_endpoint_update_last_buf(ep);
|
|
||||||
ep->buf_sel = 0;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
_hw_endpoint_start_next_buffer(ep);
|
|
||||||
_hw_endpoint_lock_update(ep, -1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void _hw_endpoint_xfer_sync(struct hw_endpoint *ep)
|
void _hw_endpoint_xfer_sync (struct hw_endpoint *ep)
|
||||||
{
|
{
|
||||||
// Update hw endpoint struct with info from hardware
|
// Update hw endpoint struct with info from hardware
|
||||||
// after a buff status interrupt
|
// after a buff status interrupt
|
||||||
|
|
||||||
uint32_t buf_ctrl = _hw_endpoint_buffer_control_get_value32(ep);
|
uint32_t const buf_ctrl = _hw_endpoint_buffer_control_get_value32(ep);
|
||||||
|
print_bufctrl32(buf_ctrl);
|
||||||
|
|
||||||
#if TUSB_OPT_HOST_ENABLED
|
// Transferred bytes for each buffer
|
||||||
// RP2040-E4
|
uint16_t xferred_bytes[2];
|
||||||
// tag::host_buf_sel_fix[]
|
|
||||||
// TODO need changes to support double buffering
|
xferred_bytes[0] = buf_ctrl & USB_BUF_CTRL_LEN_MASK;
|
||||||
if (ep->buf_sel == 1)
|
|
||||||
|
// double buffered: take buffer1 into account as well
|
||||||
|
if ( (*ep->endpoint_control) & EP_CTRL_DOUBLE_BUFFERED_BITS )
|
||||||
|
{
|
||||||
|
xferred_bytes[1] = (buf_ctrl >> 16) & USB_BUF_CTRL_LEN_MASK;
|
||||||
|
}else
|
||||||
|
{
|
||||||
|
xferred_bytes[1] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
TU_LOG_INT(2, xferred_bytes[0]);
|
||||||
|
TU_LOG_INT(2, xferred_bytes[1]);
|
||||||
|
|
||||||
|
// We are continuing a transfer here. If we are TX, we have successfully
|
||||||
|
// sent some data can increase the length we have sent
|
||||||
|
if ( !ep->rx )
|
||||||
|
{
|
||||||
|
assert(!(buf_ctrl & USB_BUF_CTRL_FULL));
|
||||||
|
ep->len += xferred_bytes[0] + xferred_bytes[1];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// If we are OUT we have recieved some data, so can increase the length
|
||||||
|
// we have recieved AFTER we have copied it to the user buffer at the appropriate offset
|
||||||
|
assert(buf_ctrl & USB_BUF_CTRL_FULL);
|
||||||
|
|
||||||
|
memcpy(&ep->user_buf[ep->len], ep->hw_data_buf, xferred_bytes[0]);
|
||||||
|
ep->len += xferred_bytes[0];
|
||||||
|
|
||||||
|
if (xferred_bytes[1])
|
||||||
{
|
{
|
||||||
// Host can erroneously write status to top half of buf_ctrl register
|
memcpy(&ep->user_buf[ep->len], ep->hw_data_buf+64, xferred_bytes[1]);
|
||||||
buf_ctrl = buf_ctrl >> 16;
|
ep->len += xferred_bytes[1];
|
||||||
|
|
||||||
// update buf1 -> buf0 to prevent panic with "already available"
|
|
||||||
*ep->buffer_control = buf_ctrl;
|
|
||||||
}
|
}
|
||||||
// Flip buf sel for host
|
}
|
||||||
ep->buf_sel ^= 1u;
|
|
||||||
// end::host_buf_sel_fix[]
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Get tranferred bytes after adjusted buf sel
|
// Sometimes the host will send less data than we expect...
|
||||||
uint16_t const transferred_bytes = buf_ctrl & USB_BUF_CTRL_LEN_MASK;
|
// If this is a short out transfer update the total length of the transfer
|
||||||
|
// to be the current length
|
||||||
// We are continuing a transfer here. If we are TX, we have successfullly
|
if ( (ep->rx) && ((xferred_bytes[0] < ep->wMaxPacketSize) || (xferred_bytes[1] && (xferred_bytes[1] < ep->wMaxPacketSize))) )
|
||||||
// sent some data can increase the length we have sent
|
{
|
||||||
if (!ep->rx)
|
pico_trace("Short rx transfer\n");
|
||||||
{
|
// Reduce total length as this is last packet
|
||||||
assert(!(buf_ctrl & USB_BUF_CTRL_FULL));
|
ep->total_len = ep->len;
|
||||||
pico_trace("tx %d bytes (buf_ctrl 0x%08x)\n", transferred_bytes, buf_ctrl);
|
}
|
||||||
ep->len += transferred_bytes;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// If we are OUT we have recieved some data, so can increase the length
|
|
||||||
// we have recieved AFTER we have copied it to the user buffer at the appropriate
|
|
||||||
// offset
|
|
||||||
pico_trace("rx %d bytes (buf_ctrl 0x%08x)\n", transferred_bytes, buf_ctrl);
|
|
||||||
assert(buf_ctrl & USB_BUF_CTRL_FULL);
|
|
||||||
memcpy(&ep->user_buf[ep->len], ep->hw_data_buf, transferred_bytes);
|
|
||||||
ep->len += transferred_bytes;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sometimes the host will send less data than we expect...
|
|
||||||
// If this is a short out transfer update the total length of the transfer
|
|
||||||
// to be the current length
|
|
||||||
if ((ep->rx) && (transferred_bytes < ep->wMaxPacketSize))
|
|
||||||
{
|
|
||||||
pico_trace("Short rx transfer\n");
|
|
||||||
// Reduce total length as this is last packet
|
|
||||||
ep->total_len = ep->len;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if transfer is complete
|
// Returns true if transfer is complete
|
||||||
|
@ -271,12 +278,11 @@ bool _hw_endpoint_xfer_continue(struct hw_endpoint *ep)
|
||||||
_hw_endpoint_xfer_sync(ep);
|
_hw_endpoint_xfer_sync(ep);
|
||||||
|
|
||||||
// Now we have synced our state with the hardware. Is there more data to transfer?
|
// Now we have synced our state with the hardware. Is there more data to transfer?
|
||||||
// Limit by packet size but not less 64 (i.e low speed 8 bytes EP0)
|
// Limit by packet size
|
||||||
uint16_t remaining_bytes = ep->total_len - ep->len;
|
uint16_t remaining_bytes = ep->total_len - ep->len;
|
||||||
ep->transfer_size = tu_min16(remaining_bytes, tu_max16(64, ep->wMaxPacketSize));
|
ep->transfer_size = tu_min16(remaining_bytes, ep->wMaxPacketSize);
|
||||||
#if TUSB_OPT_HOST_ENABLED
|
|
||||||
_hw_endpoint_update_last_buf(ep);
|
TU_LOG_INT(2, ep->transfer_size);
|
||||||
#endif
|
|
||||||
|
|
||||||
// Can happen because of programmer error so check for it
|
// Can happen because of programmer error so check for it
|
||||||
if (ep->len > ep->total_len)
|
if (ep->len > ep->total_len)
|
||||||
|
@ -303,23 +309,4 @@ bool _hw_endpoint_xfer_continue(struct hw_endpoint *ep)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void _hw_endpoint_xfer(struct hw_endpoint *ep, uint8_t *buffer, uint16_t total_len, bool start)
|
|
||||||
{
|
|
||||||
// Trace
|
|
||||||
pico_trace("hw_endpoint_xfer ep %d %s", tu_edpt_number(ep->ep_addr), ep_dir_string[tu_edpt_dir(ep->ep_addr)]);
|
|
||||||
pico_trace(" total_len %d, start=%d\n", total_len, start);
|
|
||||||
|
|
||||||
assert(ep->configured);
|
|
||||||
|
|
||||||
|
|
||||||
if (start)
|
|
||||||
{
|
|
||||||
_hw_endpoint_xfer_start(ep, buffer, total_len);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_hw_endpoint_xfer_continue(ep);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#if false && !defined(NDEBUG)
|
#if true || false && !defined(NDEBUG)
|
||||||
#define pico_trace(format,args...) printf(format, ## args)
|
#define pico_trace(format,args...) printf(format, ## args)
|
||||||
#else
|
#else
|
||||||
#define pico_trace(format,...) ((void)0)
|
#define pico_trace(format,...) ((void)0)
|
||||||
|
@ -50,6 +50,7 @@ struct hw_endpoint
|
||||||
|
|
||||||
// Endpoint control register
|
// Endpoint control register
|
||||||
io_rw_32 *endpoint_control;
|
io_rw_32 *endpoint_control;
|
||||||
|
|
||||||
// Buffer control register
|
// Buffer control register
|
||||||
io_rw_32 *buffer_control;
|
io_rw_32 *buffer_control;
|
||||||
|
|
||||||
|
@ -63,8 +64,11 @@ struct hw_endpoint
|
||||||
bool active;
|
bool active;
|
||||||
uint16_t total_len;
|
uint16_t total_len;
|
||||||
uint16_t len;
|
uint16_t len;
|
||||||
|
|
||||||
// Amount of data with the hardware
|
// Amount of data with the hardware
|
||||||
uint16_t transfer_size;
|
uint16_t transfer_size; // buf0_len;
|
||||||
|
uint16_t buf_1_len;
|
||||||
|
|
||||||
// User buffer in main memory
|
// User buffer in main memory
|
||||||
uint8_t *user_buf;
|
uint8_t *user_buf;
|
||||||
|
|
||||||
|
@ -76,6 +80,7 @@ struct hw_endpoint
|
||||||
#if TUSB_OPT_HOST_ENABLED
|
#if TUSB_OPT_HOST_ENABLED
|
||||||
// Only needed for host mode
|
// Only needed for host mode
|
||||||
bool last_buf;
|
bool last_buf;
|
||||||
|
|
||||||
// RP2040-E4: HOST BUG. Host will incorrect write status to top half of buffer
|
// RP2040-E4: HOST BUG. Host will incorrect write status to top half of buffer
|
||||||
// control register when doing transfers > 1 packet
|
// control register when doing transfers > 1 packet
|
||||||
uint8_t buf_sel;
|
uint8_t buf_sel;
|
||||||
|
@ -90,11 +95,11 @@ struct hw_endpoint
|
||||||
void rp2040_usb_init(void);
|
void rp2040_usb_init(void);
|
||||||
|
|
||||||
void hw_endpoint_reset_transfer(struct hw_endpoint *ep);
|
void hw_endpoint_reset_transfer(struct hw_endpoint *ep);
|
||||||
void _hw_endpoint_xfer(struct hw_endpoint *ep, uint8_t *buffer, uint16_t total_len, bool start);
|
|
||||||
void _hw_endpoint_start_next_buffer(struct hw_endpoint *ep);
|
void _hw_endpoint_start_next_buffer(struct hw_endpoint *ep);
|
||||||
void _hw_endpoint_xfer_start(struct hw_endpoint *ep, uint8_t *buffer, uint16_t total_len);
|
void _hw_endpoint_xfer_start(struct hw_endpoint *ep, uint8_t *buffer, uint16_t total_len);
|
||||||
void _hw_endpoint_xfer_sync(struct hw_endpoint *ep);
|
void _hw_endpoint_xfer_sync(struct hw_endpoint *ep);
|
||||||
bool _hw_endpoint_xfer_continue(struct hw_endpoint *ep);
|
bool _hw_endpoint_xfer_continue(struct hw_endpoint *ep);
|
||||||
|
|
||||||
void _hw_endpoint_buffer_control_update32(struct hw_endpoint *ep, uint32_t and_mask, uint32_t or_mask);
|
void _hw_endpoint_buffer_control_update32(struct hw_endpoint *ep, uint32_t and_mask, uint32_t or_mask);
|
||||||
static inline uint32_t _hw_endpoint_buffer_control_get_value32(struct hw_endpoint *ep) {
|
static inline uint32_t _hw_endpoint_buffer_control_get_value32(struct hw_endpoint *ep) {
|
||||||
return *ep->buffer_control;
|
return *ep->buffer_control;
|
||||||
|
@ -149,11 +154,11 @@ static inline void print_bufctrl32(uint32_t u32)
|
||||||
uint16_t u16;
|
uint16_t u16;
|
||||||
|
|
||||||
u16 = u32 >> 16;
|
u16 = u32 >> 16;
|
||||||
TU_LOG(2, "Buffer Control 1 0x%x: ", u16);
|
TU_LOG(2, " Buffer Control 1 0x%x: ", u16);
|
||||||
print_bufctrl16(u16);
|
print_bufctrl16(u16);
|
||||||
|
|
||||||
u16 = u32 & 0x0000ffff;
|
u16 = u32 & 0x0000ffff;
|
||||||
TU_LOG(2, "Buffer Control 0 0x%x: ", u16);
|
TU_LOG(2, " Buffer Control 0 0x%x: ", u16);
|
||||||
print_bufctrl16(u16);
|
print_bufctrl16(u16);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue