refactor rp2040 usb
- make _hw_endpoint_xfer_sync and _hw_endpoint_start_next_buffer private - drop prefix _ from _hw_endpoint_xfer_continue and _hw_endpoint_reset_transfer
This commit is contained in:
		@@ -201,7 +201,7 @@ static void hw_endpoint_init(uint8_t ep_addr, uint16_t wMaxPacketSize, uint8_t b
 | 
			
		||||
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);
 | 
			
		||||
    _hw_endpoint_xfer_start(ep, buffer, total_bytes);
 | 
			
		||||
    hw_endpoint_xfer_start(ep, buffer, total_bytes);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void hw_handle_buff_status(void)
 | 
			
		||||
@@ -221,7 +221,7 @@ static void hw_handle_buff_status(void)
 | 
			
		||||
            // IN transfer for even i, OUT transfer for odd i
 | 
			
		||||
            struct hw_endpoint *ep = hw_endpoint_get_by_num(i >> 1u, !(i & 1u));
 | 
			
		||||
            // Continue xfer
 | 
			
		||||
            bool done = _hw_endpoint_xfer_continue(ep);
 | 
			
		||||
            bool done = hw_endpoint_xfer_continue(ep);
 | 
			
		||||
            if (done)
 | 
			
		||||
            {
 | 
			
		||||
                // Notify
 | 
			
		||||
 
 | 
			
		||||
@@ -123,7 +123,7 @@ static void hw_xfer_complete(struct hw_endpoint *ep, xfer_result_t xfer_result)
 | 
			
		||||
static void _handle_buff_status_bit(uint bit, struct hw_endpoint *ep)
 | 
			
		||||
{
 | 
			
		||||
    usb_hw_clear->buf_status = bit;
 | 
			
		||||
    bool done = _hw_endpoint_xfer_continue(ep);
 | 
			
		||||
    bool done = hw_endpoint_xfer_continue(ep);
 | 
			
		||||
    if (done)
 | 
			
		||||
    {
 | 
			
		||||
        hw_xfer_complete(ep, XFER_RESULT_SUCCESS);
 | 
			
		||||
@@ -485,65 +485,6 @@ bool hcd_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const
 | 
			
		||||
    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)
 | 
			
		||||
{
 | 
			
		||||
    (void) rhport;
 | 
			
		||||
@@ -567,7 +508,7 @@ bool hcd_edpt_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr, uint8_t *
 | 
			
		||||
    // sie ctrl registers. Otherwise interrupt ep registers should
 | 
			
		||||
    // already be configured
 | 
			
		||||
    if (ep == &epx) {
 | 
			
		||||
        _hw_endpoint_xfer_start(ep, buffer, buflen);
 | 
			
		||||
        hw_endpoint_xfer_start(ep, buffer, buflen);
 | 
			
		||||
 | 
			
		||||
        // That has set up buffer control, endpoint control etc
 | 
			
		||||
        // for host we have to initiate the transfer
 | 
			
		||||
@@ -581,7 +522,7 @@ bool hcd_edpt_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr, uint8_t *
 | 
			
		||||
        usb_hw->sie_ctrl = flags;
 | 
			
		||||
    }else
 | 
			
		||||
    {
 | 
			
		||||
      _hw_endpoint_xfer_start(ep, buffer, buflen);
 | 
			
		||||
      hw_endpoint_xfer_start(ep, buffer, buflen);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return true;
 | 
			
		||||
 
 | 
			
		||||
@@ -50,6 +50,13 @@ static inline void _hw_endpoint_update_last_buf(struct hw_endpoint *ep)
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
//--------------------------------------------------------------------+
 | 
			
		||||
//
 | 
			
		||||
//--------------------------------------------------------------------+
 | 
			
		||||
 | 
			
		||||
void _hw_endpoint_xfer_sync(struct hw_endpoint *ep);
 | 
			
		||||
void _hw_endpoint_start_next_buffer(struct hw_endpoint *ep);
 | 
			
		||||
 | 
			
		||||
void rp2040_usb_init(void)
 | 
			
		||||
{
 | 
			
		||||
    // Reset usb controller
 | 
			
		||||
@@ -180,8 +187,7 @@ void _hw_endpoint_start_next_buffer(struct hw_endpoint *ep)
 | 
			
		||||
  _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);
 | 
			
		||||
  pico_trace("Start transfer of total len %d on ep %d %s\n", total_len, tu_edpt_number(ep->ep_addr),
 | 
			
		||||
@@ -205,58 +211,34 @@ void _hw_endpoint_xfer_start(struct hw_endpoint *ep, uint8_t *buffer, uint16_t t
 | 
			
		||||
  _hw_endpoint_lock_update(ep, -1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void _hw_endpoint_xfer_sync (struct hw_endpoint *ep)
 | 
			
		||||
static void ep_sync_buf(struct hw_endpoint *ep, uint8_t buf_id)
 | 
			
		||||
{
 | 
			
		||||
  // Update hw endpoint struct with info from hardware
 | 
			
		||||
  // after a buff status interrupt
 | 
			
		||||
  uint32_t buf_ctrl = _hw_endpoint_buffer_control_get_value32(ep);
 | 
			
		||||
  if (buf_id)  buf_ctrl = buf_ctrl >> 16;
 | 
			
		||||
 | 
			
		||||
  uint32_t const buf_ctrl = _hw_endpoint_buffer_control_get_value32(ep);
 | 
			
		||||
  print_bufctrl32(buf_ctrl);
 | 
			
		||||
  uint16_t xferred_bytes = buf_ctrl & USB_BUF_CTRL_LEN_MASK;
 | 
			
		||||
 | 
			
		||||
  // Transferred bytes for each buffer
 | 
			
		||||
  uint16_t xferred_bytes[2];
 | 
			
		||||
 | 
			
		||||
  xferred_bytes[0] = buf_ctrl & USB_BUF_CTRL_LEN_MASK;
 | 
			
		||||
 | 
			
		||||
  // 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 )
 | 
			
		||||
  {
 | 
			
		||||
    // We are continuing a transfer here. If we are TX, we have successfully
 | 
			
		||||
    // sent some data can increase the length we have sent
 | 
			
		||||
    assert(!(buf_ctrl & USB_BUF_CTRL_FULL));
 | 
			
		||||
    ep->len += xferred_bytes[0] + xferred_bytes[1];
 | 
			
		||||
  }
 | 
			
		||||
  else
 | 
			
		||||
 | 
			
		||||
    ep->len += xferred_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
 | 
			
		||||
    // If we have received some data, so can increase the length
 | 
			
		||||
    // we have received 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])
 | 
			
		||||
    {
 | 
			
		||||
      memcpy(&ep->user_buf[ep->len], ep->hw_data_buf+64, xferred_bytes[1]);
 | 
			
		||||
      ep->len += xferred_bytes[1];
 | 
			
		||||
    }
 | 
			
		||||
    memcpy(&ep->user_buf[ep->len], ep->hw_data_buf + buf_id*64, xferred_bytes);
 | 
			
		||||
    ep->len += xferred_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) && ((xferred_bytes[0] < ep->wMaxPacketSize) || (xferred_bytes[1] && (xferred_bytes[1] < ep->wMaxPacketSize))) )
 | 
			
		||||
  // Short packet
 | 
			
		||||
  // TODO what if we prepare double buffered but receive short packet on buffer 0 !!!
 | 
			
		||||
  // would the buffer status  or trans complete interrupt is triggered
 | 
			
		||||
  if (xferred_bytes < ep->wMaxPacketSize)
 | 
			
		||||
  {
 | 
			
		||||
    pico_trace("Short rx transfer\n");
 | 
			
		||||
    // Reduce total length as this is last packet
 | 
			
		||||
@@ -264,8 +246,23 @@ 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
 | 
			
		||||
  // after a buff status interrupt
 | 
			
		||||
 | 
			
		||||
  // always sync buffer 0
 | 
			
		||||
  ep_sync_buf(ep, 0);
 | 
			
		||||
 | 
			
		||||
  // sync buffer 1 if double buffered
 | 
			
		||||
  if ( (*ep->endpoint_control) & EP_CTRL_DOUBLE_BUFFERED_BITS )
 | 
			
		||||
  {
 | 
			
		||||
    ep_sync_buf(ep, 1);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Returns true if transfer is complete
 | 
			
		||||
bool _hw_endpoint_xfer_continue(struct hw_endpoint *ep)
 | 
			
		||||
bool hw_endpoint_xfer_continue(struct hw_endpoint *ep)
 | 
			
		||||
{
 | 
			
		||||
    _hw_endpoint_lock_update(ep, 1);
 | 
			
		||||
    // Part way through a transfer
 | 
			
		||||
 
 | 
			
		||||
@@ -94,11 +94,9 @@ struct hw_endpoint
 | 
			
		||||
 | 
			
		||||
void rp2040_usb_init(void);
 | 
			
		||||
 | 
			
		||||
void hw_endpoint_xfer_start(struct hw_endpoint *ep, uint8_t *buffer, uint16_t total_len);
 | 
			
		||||
bool hw_endpoint_xfer_continue(struct hw_endpoint *ep);
 | 
			
		||||
void hw_endpoint_reset_transfer(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_sync(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);
 | 
			
		||||
static inline uint32_t _hw_endpoint_buffer_control_get_value32(struct hw_endpoint *ep) {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user