fomu: commit latest version

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2019-10-29 21:35:03 +08:00
parent 22fd7bf85e
commit 843136d0e4
1 changed files with 181 additions and 117 deletions

View File

@ -48,19 +48,49 @@ uint8_t volatile * rx_buffer[16];
uint16_t volatile rx_buffer_max[16]; uint16_t volatile rx_buffer_max[16];
volatile uint8_t tx_ep; volatile uint8_t tx_ep;
volatile uint16_t tx_len; volatile uint16_t tx_buffer_offset[16];
uint8_t volatile * tx_buffer; uint8_t volatile * tx_buffer[16];
volatile uint16_t tx_offset; volatile uint16_t tx_buffer_max[16];
volatile uint8_t reset_count; volatile uint8_t reset_count;
__attribute__((used)) uint8_t volatile * last_tx_buffer;
__attribute__((used)) volatile uint8_t last_tx_ep;
uint8_t setup_packet_bfr[10];
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// PIPE HELPER // PIPE HELPER
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
static void finish_tx(void) { static bool advance_tx_ep(void) {
// Ignore "ACK" packets where there was no data to send. // Move on to the next transmit buffer in a round-robin manner
if (!tx_buffer) { uint8_t prev_tx_ep = tx_ep;
return; for (tx_ep = (tx_ep + 1) & 0xf; tx_ep != prev_tx_ep; tx_ep = ((tx_ep + 1) & 0xf)) {
if (tx_buffer[tx_ep])
return true;
}
if (!tx_buffer[tx_ep])
return false;
return true;
}
static void tx_more_data(void) {
// Send more data
uint8_t added_bytes;
for (added_bytes = 0; (added_bytes < EP_SIZE) && (added_bytes + tx_buffer_offset[tx_ep] < tx_buffer_max[tx_ep]); added_bytes++) {
usb_in_data_write(tx_buffer[tx_ep][added_bytes + tx_buffer_offset[tx_ep]]);
}
// Updating the epno queues the data
usb_in_ctrl_write(tx_ep & 0xf);
}
static void process_tx(bool in_isr) {
// If the buffer is now empty, search for the next buffer to fill.
if (!tx_buffer[tx_ep]) {
if (advance_tx_ep())
tx_more_data();
return;
} }
// If the system isn't idle, then something is very wrong. // If the system isn't idle, then something is very wrong.
@ -68,62 +98,78 @@ static void finish_tx(void) {
if (!(in_status & (1 << CSR_USB_IN_STATUS_IDLE_OFFSET))) if (!(in_status & (1 << CSR_USB_IN_STATUS_IDLE_OFFSET)))
fomu_error(__LINE__); fomu_error(__LINE__);
tx_offset += EP_SIZE; tx_buffer_offset[tx_ep] += EP_SIZE;
if (tx_offset >= tx_len) {
dcd_event_xfer_complete(0, tu_edpt_addr(tx_ep, TUSB_DIR_IN), tx_len, XFER_RESULT_SUCCESS, true); if (tx_buffer_offset[tx_ep] >= tx_buffer_max[tx_ep]) {
tx_buffer = NULL; last_tx_buffer = tx_buffer[tx_ep];
return; last_tx_ep = tx_ep;
tx_buffer[tx_ep] = NULL;
dcd_event_xfer_complete(0, tu_edpt_addr(tx_ep, TUSB_DIR_IN), tx_buffer_max[tx_ep], XFER_RESULT_SUCCESS, in_isr);
if (!advance_tx_ep())
return;
} }
// Send more data tx_more_data();
uint8_t added_bytes;
for (added_bytes = 0; (added_bytes < EP_SIZE) && (added_bytes + tx_offset < tx_len); added_bytes++) {
usb_in_data_write(tx_buffer[added_bytes + tx_offset]);
}
// Updating the epno queues the data
usb_in_ctrl_write(tx_ep & 0xf);
return; return;
} }
static void process_rx(bool in_isr) { static void process_rx(bool in_isr) {
// If the OUT handler is still waiting to send, don't do anything. // If the OUT handler is still waiting to send, don't do anything.
uint8_t out_status = usb_out_status_read(); uint8_t out_status = usb_out_status_read();
if (!(out_status & (1 << CSR_USB_OUT_STATUS_IDLE_OFFSET))) if (!(out_status & (1 << CSR_USB_OUT_STATUS_HAVE_OFFSET)))
return; fomu_error(__LINE__);
// return;
uint8_t rx_ep = (out_status >> CSR_USB_OUT_STATUS_EPNO_OFFSET) & 0xf; uint8_t rx_ep = (out_status >> CSR_USB_OUT_STATUS_EPNO_OFFSET) & 0xf;
// If the destination buffer doesn't exist, don't drain the hardware // If the destination buffer doesn't exist, don't drain the hardware
// fifo. Note that this can cause deadlocks if the host is waiting // fifo. Note that this can cause deadlocks if the host is waiting
// on some other endpoint's data! // on some other endpoint's data!
if (rx_buffer[rx_ep] == NULL) {
fomu_error(__LINE__);
return;
}
uint32_t total_read = 0;
uint32_t current_offset = rx_buffer_offset[rx_ep];
if (current_offset > rx_buffer_max[rx_ep])
fomu_error(__LINE__);
while (usb_out_status_read() & (1 << CSR_USB_OUT_STATUS_HAVE_OFFSET)) {
uint8_t c = usb_out_data_read();
total_read++;
if ((rx_buffer_offset[rx_ep] + current_offset) < rx_buffer_max[rx_ep])
rx_buffer[rx_ep][current_offset++] = c;
}
if (total_read > 66)
fomu_error(__LINE__);
// Strip off the CRC16
rx_buffer_offset[rx_ep] += (total_read - 2);
if (rx_buffer_offset[rx_ep] > rx_buffer_max[rx_ep])
rx_buffer_offset[rx_ep] = rx_buffer_max[rx_ep];
if (rx_buffer_max[rx_ep] == rx_buffer_offset[rx_ep]) {
if (rx_buffer[rx_ep] == NULL) if (rx_buffer[rx_ep] == NULL)
return;
uint32_t total_read = 0;
uint32_t current_offset = rx_buffer_offset[rx_ep];
if (current_offset > rx_buffer_max[rx_ep])
fomu_error(__LINE__); fomu_error(__LINE__);
while (usb_out_status_read() & (1 << CSR_USB_OUT_STATUS_HAVE_OFFSET)) {
uint8_t c = usb_out_data_read();
total_read++;
if ((rx_buffer_offset[rx_ep] + current_offset) < rx_buffer_max[rx_ep])
rx_buffer[rx_ep][current_offset++] = c;
}
// Strip off the CRC16 // Disable this endpoint (causing it to respond NAK) until we have
rx_buffer_offset[rx_ep] += (total_read - 2); // a buffer to place the data into.
if (rx_buffer_offset[rx_ep] > rx_buffer_max[rx_ep]) rx_buffer[rx_ep] = NULL;
rx_buffer_offset[rx_ep] = rx_buffer_max[rx_ep]; uint16_t len = rx_buffer_offset[rx_ep];
if (rx_buffer_max[rx_ep] == rx_buffer_offset[rx_ep]) { // uint16_t ep_en_mask = usb_out_enable_status_read();
rx_buffer[rx_ep] = NULL; // int i;
uint16_t len = rx_buffer_offset[rx_ep]; // for (i = 0; i < 16; i++) {
dcd_event_xfer_complete(0, tu_edpt_addr(rx_ep, TUSB_DIR_OUT), len, XFER_RESULT_SUCCESS, in_isr); // if ((!!(ep_en_mask & (1 << i))) ^ (!!(rx_buffer[i])))
} // fomu_error(__LINE__);
// }
dcd_event_xfer_complete(0, tu_edpt_addr(rx_ep, TUSB_DIR_OUT), len, XFER_RESULT_SUCCESS, in_isr);
return;
}
// Acknowledge having received the data, and re-enable data reception // If there's more data, re-enable data reception
usb_out_ctrl_write(1 << CSR_USB_OUT_CTRL_ENABLE_OFFSET); usb_out_ctrl_write((1 << CSR_USB_OUT_CTRL_ENABLE_OFFSET) | rx_ep);
} }
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
@ -147,18 +193,16 @@ static void dcd_reset(void)
memset((void *)rx_buffer, 0, sizeof(rx_buffer)); memset((void *)rx_buffer, 0, sizeof(rx_buffer));
memset((void *)rx_buffer_max, 0, sizeof(rx_buffer_max)); memset((void *)rx_buffer_max, 0, sizeof(rx_buffer_max));
memset((void *)rx_buffer_offset, 0, sizeof(rx_buffer_offset)); memset((void *)rx_buffer_offset, 0, sizeof(rx_buffer_offset));
tx_len = 0;
tx_buffer = NULL; memset((void *)tx_buffer, 0, sizeof(tx_buffer));
tx_offset = 0; memset((void *)tx_buffer_max, 0, sizeof(tx_buffer_max));
memset((void *)tx_buffer_offset, 0, sizeof(tx_buffer_offset));
tx_ep = 0; tx_ep = 0;
// Accept incoming data by default.
usb_out_ctrl_write(1 << CSR_USB_OUT_CTRL_ENABLE_OFFSET);
// Enable all event handlers and clear their contents // Enable all event handlers and clear their contents
usb_setup_ev_pending_write(usb_setup_ev_pending_read()); usb_setup_ev_pending_write(-1);
usb_in_ev_pending_write(usb_in_ev_pending_read()); usb_in_ev_pending_write(-1);
usb_out_ev_pending_write(usb_out_ev_pending_read()); usb_out_ev_pending_write(-1);
usb_in_ev_enable_write(1); usb_in_ev_enable_write(1);
usb_out_ev_enable_write(1); usb_out_ev_enable_write(1);
usb_setup_ev_enable_write(3); usb_setup_ev_enable_write(3);
@ -203,12 +247,15 @@ void dcd_int_disable(uint8_t rhport)
// Called when the device is given a new bus address. // Called when the device is given a new bus address.
void dcd_set_address(uint8_t rhport, uint8_t dev_addr) void dcd_set_address(uint8_t rhport, uint8_t dev_addr)
{ {
(void)rhport; // Respond with ACK status first before changing device address
// Set address and then acknowledge the SETUP packet dcd_edpt_xfer(rhport, tu_edpt_addr(0, TUSB_DIR_IN), NULL, 0);
usb_address_write(dev_addr);
// ACK the transfer (sets the address) // Wait for the response packet to get sent
usb_setup_ctrl_write(1 << CSR_USB_SETUP_CTRL_HANDLED_OFFSET); while (tx_buffer[0] != NULL)
;
// Activate the new address
usb_address_write(dev_addr);
} }
// Called when the device received SET_CONFIG request, you can leave this // Called when the device received SET_CONFIG request, you can leave this
@ -243,24 +290,36 @@ bool dcd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc)
rx_buffer[ep_num] = NULL; rx_buffer[ep_num] = NULL;
} }
else if (ep_dir == TUSB_DIR_OUT) {
tx_buffer_offset[ep_num] = 0;
tx_buffer_max[ep_num] = 0;
tx_buffer[ep_num] = NULL;
}
return true; return true;
} }
void dcd_edpt_stall(uint8_t rhport, uint8_t ep_addr) void dcd_edpt_stall(uint8_t rhport, uint8_t ep_addr)
{ {
(void) rhport; (void) rhport;
if (tu_edpt_number(ep_addr) == 2)
fomu_error(__LINE__);
if (tu_edpt_dir(ep_addr) == TUSB_DIR_OUT) if (tu_edpt_dir(ep_addr) == TUSB_DIR_OUT)
usb_out_stall_write((1 << CSR_USB_OUT_STALL_STALL_OFFSET) | tu_edpt_number(ep_addr)); usb_out_ctrl_write((1 << CSR_USB_OUT_CTRL_STALL_OFFSET) | (1 << CSR_USB_OUT_CTRL_ENABLE_OFFSET) | tu_edpt_number(ep_addr));
else { else
usb_in_ctrl_write((1 << CSR_USB_IN_CTRL_STALL_OFFSET) | tu_edpt_number(ep_addr)); usb_in_ctrl_write((1 << CSR_USB_IN_CTRL_STALL_OFFSET) | tu_edpt_number(ep_addr));
}
} }
void dcd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr) void dcd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr)
{ {
(void) rhport; (void) rhport;
if (tu_edpt_dir(ep_addr) == TUSB_DIR_OUT) if (tu_edpt_dir(ep_addr) == TUSB_DIR_OUT) {
usb_out_stall_write((0 << CSR_USB_OUT_STALL_STALL_OFFSET) | tu_edpt_number(ep_addr)); uint8_t enable = 0;
if (rx_buffer[ep_addr])
enable = 1;
usb_out_ctrl_write((0 << CSR_USB_OUT_CTRL_STALL_OFFSET) | (enable << CSR_USB_OUT_CTRL_ENABLE_OFFSET) | tu_edpt_number(ep_addr));
}
// IN endpoints will get unstalled when more data is written. // IN endpoints will get unstalled when more data is written.
} }
@ -269,62 +328,64 @@ bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t* buffer, uint16_t t
(void)rhport; (void)rhport;
uint8_t ep_num = tu_edpt_number(ep_addr); uint8_t ep_num = tu_edpt_number(ep_addr);
uint8_t ep_dir = tu_edpt_dir(ep_addr); uint8_t ep_dir = tu_edpt_dir(ep_addr);
TU_ASSERT(tx_ep < 16); TU_ASSERT(ep_num < 16);
// These sorts of transfers are handled in hardware automatically, so simply inform // Give a nonzero buffer when we transmit 0 bytes, so that the
// the core that the transfer was processed. // system doesn't think the endpoint is idle.
if ((ep_num == 0) && (total_bytes == 0) && (buffer == NULL)) { if ((buffer == NULL) && (total_bytes == 0)) {
dcd_event_xfer_complete(0, ep_addr, total_bytes, XFER_RESULT_SUCCESS, false); buffer = (uint8_t *)0xffffffff;
// An IN packet is sent to acknowledge an OUT token. Re-enable OUT after this.
// if (ep_dir == TUSB_DIR_IN) {
// usb_out_ev_enable_write(0);
// process_rx(false);
// usb_out_ev_enable_write(1);
// }
return true;
} }
TU_ASSERT(((uint32_t)buffer) >= 0x10000000); TU_ASSERT(buffer != NULL);
TU_ASSERT(((uint32_t)buffer) <= 0x10020000);
if (ep_dir == TUSB_DIR_IN) { if (ep_dir == TUSB_DIR_IN) {
uint32_t offset;
// Wait for the tx pipe to free up // Wait for the tx pipe to free up
uint8_t previous_reset_count = reset_count; uint8_t previous_reset_count = reset_count;
while (!((tx_buffer == NULL) // Continue until the buffer is empty, the system is idle, and the fifo is empty.
&& (usb_in_status_read() & (1 << CSR_USB_IN_STATUS_IDLE_OFFSET)) while (tx_buffer[ep_num] != NULL)
&& !(usb_in_status_read() & (1 << CSR_USB_IN_STATUS_HAVE_OFFSET))))
; ;
// If a reset happens while we're waiting, abort the transfer // If a reset happens while we're waiting, abort the transfer
if (previous_reset_count != reset_count) if (previous_reset_count != reset_count)
return true; return true;
tx_ep = ep_num; dcd_int_disable(0);
tx_len = total_bytes; TU_ASSERT(tx_buffer[ep_num] == NULL);
tx_offset = 0; tx_buffer_offset[ep_num] = 0;
tx_buffer = buffer; tx_buffer_max[ep_num] = total_bytes;
tx_buffer[ep_num] = buffer;
for (offset = 0; (offset < EP_SIZE) && (offset < total_bytes); offset++) { // If the current buffer is NULL, then that means the tx logic is idle.
usb_in_data_write(buffer[offset]); // Update the tx_ep to point to our endpoint number and queue the data.
// Otherwise, let it be and it'll get picked up after the next transfer
// finishes.
if ((tx_buffer[tx_ep] == NULL) || (tx_ep == ep_num)) {
tx_ep = ep_num;
tx_more_data();
} }
// Updating the epno queues the data dcd_int_enable(0);
usb_in_ctrl_write(ep_num & 0xf);
} }
else if (ep_dir == TUSB_DIR_OUT) { else if (ep_dir == TUSB_DIR_OUT) {
TU_ASSERT(rx_buffer[ep_num] == NULL); while (rx_buffer[ep_num] != NULL)
;
rx_buffer_offset[ep_num] = 0; rx_buffer_offset[ep_num] = 0;
rx_buffer_max[ep_num] = total_bytes; rx_buffer_max[ep_num] = total_bytes;
rx_buffer[ep_num] = buffer;
// If there's data in the buffer already, we'll try draining it dcd_int_disable(0);
// into the current fifo immediately. rx_buffer[ep_num] = buffer;
usb_out_ev_enable_write(0); usb_out_ctrl_write((1 << CSR_USB_OUT_CTRL_ENABLE_OFFSET) | ep_num);
if (usb_out_status_read() & (1 << CSR_USB_OUT_STATUS_HAVE_OFFSET))
process_rx(false); // uint16_t ep_en_mask = usb_out_enable_status_read();
usb_out_ev_enable_write(1); // int i;
// for (i = 0; i < 16; i++) {
// if ((!!(ep_en_mask & (1 << i))) ^ (!!(rx_buffer[i]))) {
// if (rx_buffer[i] && usb_out_ev_pending_read() && (usb_out_status_read() & 0xf) == i)
// continue;
// fomu_error(__LINE__);
// }
// }
dcd_int_enable(0);
} }
return true; return true;
} }
@ -339,8 +400,6 @@ void hal_dcd_isr(uint8_t rhport)
uint8_t in_pending = usb_in_ev_pending_read(); uint8_t in_pending = usb_in_ev_pending_read();
uint8_t out_pending = usb_out_ev_pending_read(); uint8_t out_pending = usb_out_ev_pending_read();
usb_setup_ev_pending_write(setup_pending); usb_setup_ev_pending_write(setup_pending);
usb_in_ev_pending_write(in_pending);
usb_out_ev_pending_write(out_pending);
// This event means a bus reset occurred. Reset everything, and // This event means a bus reset occurred. Reset everything, and
// abandon any further processing. // abandon any further processing.
@ -352,7 +411,10 @@ void hal_dcd_isr(uint8_t rhport)
// An "OUT" transaction just completed so we have new data. // An "OUT" transaction just completed so we have new data.
// (But only if we can accept the data) // (But only if we can accept the data)
// if (out_pending) { // if (out_pending) {
if (usb_out_ev_enable_read() && out_pending) { if (out_pending) {
if (!usb_out_ev_enable_read())
fomu_error(__LINE__);
usb_out_ev_pending_write(out_pending);
process_rx(true); process_rx(true);
} }
@ -364,35 +426,37 @@ void hal_dcd_isr(uint8_t rhport)
// to begin with, then make a subsequent request. If we don't handle // to begin with, then make a subsequent request. If we don't handle
// the IN packets first, then the second request will be truncated. // the IN packets first, then the second request will be truncated.
if (in_pending) { if (in_pending) {
finish_tx(); if (!usb_in_ev_enable_read())
fomu_error(__LINE__);
usb_in_ev_pending_write(in_pending);
process_tx(true);
} }
// We got a SETUP packet. Copy it to the setup buffer and clear // We got a SETUP packet. Copy it to the setup buffer and clear
// the "pending" bit. // the "pending" bit.
if (setup_pending & 1) { if (setup_pending & 1) {
// Setup packets are always 8 bytes, plus two bytes of crc16. // Setup packets are always 8 bytes, plus two bytes of crc16.
uint8_t setup_packet[10];
uint32_t setup_length = 0; uint32_t setup_length = 0;
if (!(usb_setup_status_read() & 1)) if (!(usb_setup_status_read() & (1 << CSR_USB_SETUP_STATUS_HAVE_OFFSET)))
fomu_error(__LINE__); fomu_error(__LINE__);
while (usb_setup_status_read() & 1) { while (usb_setup_status_read() & (1 << CSR_USB_SETUP_STATUS_HAVE_OFFSET)) {
uint8_t c = usb_setup_data_read(); uint8_t c = usb_setup_data_read();
if (setup_length < sizeof(setup_packet)) if (setup_length < sizeof(setup_packet_bfr))
setup_packet[setup_length] = c; setup_packet_bfr[setup_length] = c;
setup_length++; setup_length++;
} }
// If we have 10 bytes, that's a full SETUP packet plus CRC16. // If we have 10 bytes, that's a full SETUP packet plus CRC16.
// Otherwise, it was an RX error. // Otherwise, it was an RX error.
if (setup_length == 10) { if (setup_length == 10) {
dcd_event_setup_received(rhport, setup_packet, true); dcd_event_setup_received(rhport, setup_packet_bfr, true);
// Acknowledge the packet, so long as it isn't a SET_ADDRESS // Acknowledge the packet, so long as it isn't a SET_ADDRESS
// packet. If it is, leave it unacknowledged and we'll do this // packet. If it is, leave it unacknowledged and we'll do this
// in the `dcd_set_address` function instead. // in the `dcd_set_address` function instead.
if (!((setup_packet[0] == 0x00) && (setup_packet[1] == 0x05))) // if (!((setup_packet_bfr[0] == 0x00) && (setup_packet_bfr[1] == 0x05)))
usb_setup_ctrl_write(1 << CSR_USB_SETUP_CTRL_HANDLED_OFFSET); usb_setup_ctrl_write(1 << CSR_USB_SETUP_CTRL_ACK_OFFSET);
} }
else { else {
fomu_error(__LINE__); fomu_error(__LINE__);