2019-01-23 03:52:47 +00:00
|
|
|
#include <usb.h>
|
2019-02-28 02:48:25 +00:00
|
|
|
#include <irq.h>
|
2019-01-22 01:02:15 +00:00
|
|
|
#include <generated/csr.h>
|
2019-01-23 03:52:47 +00:00
|
|
|
#include <string.h>
|
2019-01-25 01:28:03 +00:00
|
|
|
#include <printf.h>
|
2019-02-28 02:48:25 +00:00
|
|
|
#include <uart.h>
|
2019-03-28 03:13:25 +00:00
|
|
|
#include <usb.h>
|
2019-02-28 02:48:25 +00:00
|
|
|
|
|
|
|
#ifdef CSR_USB_EP_0_OUT_EV_PENDING_ADDR
|
2019-01-22 01:02:15 +00:00
|
|
|
|
2019-03-05 03:54:48 +00:00
|
|
|
/* The state machine states of a control pipe */
|
|
|
|
enum CONTROL_STATE
|
|
|
|
{
|
|
|
|
WAIT_SETUP,
|
2019-03-11 03:51:19 +00:00
|
|
|
IN_SETUP,
|
2019-03-05 03:54:48 +00:00
|
|
|
IN_DATA,
|
|
|
|
OUT_DATA,
|
|
|
|
LAST_IN_DATA,
|
|
|
|
WAIT_STATUS_IN,
|
|
|
|
WAIT_STATUS_OUT,
|
|
|
|
STALLED,
|
|
|
|
} control_state;
|
|
|
|
|
2019-03-26 01:39:55 +00:00
|
|
|
// Note that our PIDs are only bits 2 and 3 of the token,
|
|
|
|
// since all other bits are effectively redundant at this point.
|
2019-03-11 03:51:19 +00:00
|
|
|
enum USB_PID {
|
2019-03-26 01:39:55 +00:00
|
|
|
USB_PID_OUT = 0,
|
|
|
|
USB_PID_SOF = 1,
|
|
|
|
USB_PID_IN = 2,
|
2019-03-11 03:51:19 +00:00
|
|
|
USB_PID_SETUP = 3,
|
|
|
|
};
|
|
|
|
|
2019-02-28 02:48:25 +00:00
|
|
|
enum epfifo_response {
|
|
|
|
EPF_ACK = 0,
|
|
|
|
EPF_NAK = 1,
|
|
|
|
EPF_NONE = 2,
|
|
|
|
EPF_STALL = 3,
|
|
|
|
};
|
|
|
|
|
|
|
|
#define USB_EV_ERROR 1
|
|
|
|
#define USB_EV_PACKET 2
|
|
|
|
|
2019-03-28 03:13:25 +00:00
|
|
|
void usb_disconnect(void) {
|
2019-04-02 08:39:13 +00:00
|
|
|
usb_ep_0_out_ev_enable_write(0);
|
|
|
|
usb_ep_0_in_ev_enable_write(0);
|
|
|
|
irq_setmask(irq_getmask() & ~(1 << USB_INTERRUPT));
|
2019-03-28 03:13:25 +00:00
|
|
|
usb_pullup_out_write(0);
|
|
|
|
}
|
|
|
|
|
2019-02-28 02:48:25 +00:00
|
|
|
void usb_connect(void) {
|
|
|
|
|
2019-03-04 07:46:59 +00:00
|
|
|
usb_ep_0_out_ev_pending_write(usb_ep_0_out_ev_enable_read());
|
|
|
|
usb_ep_0_in_ev_pending_write(usb_ep_0_in_ev_pending_read());
|
|
|
|
usb_ep_0_out_ev_enable_write(USB_EV_PACKET | USB_EV_ERROR);
|
2019-02-28 02:48:25 +00:00
|
|
|
usb_ep_0_in_ev_enable_write(USB_EV_PACKET | USB_EV_ERROR);
|
|
|
|
|
2019-03-10 07:25:33 +00:00
|
|
|
// By default, it wants to respond with NAK.
|
|
|
|
usb_ep_0_out_respond_write(EPF_ACK);
|
|
|
|
usb_ep_0_in_respond_write(EPF_ACK);
|
|
|
|
|
|
|
|
usb_pullup_out_write(1);
|
|
|
|
|
2019-02-28 02:48:25 +00:00
|
|
|
irq_setmask(irq_getmask() | (1 << USB_INTERRUPT));
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_init(void) {
|
2019-03-28 03:13:25 +00:00
|
|
|
usb_pullup_out_write(0);
|
2019-02-28 02:48:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-26 01:39:55 +00:00
|
|
|
static volatile int irq_count = 0;
|
2019-02-28 02:48:25 +00:00
|
|
|
|
2019-03-26 01:39:55 +00:00
|
|
|
#define EP0OUT_BUFFERS 8
|
2019-03-05 03:54:48 +00:00
|
|
|
__attribute__((aligned(4)))
|
2019-03-26 01:39:55 +00:00
|
|
|
static uint8_t usb_ep0out_buffer[EP0OUT_BUFFERS][256];
|
2019-03-05 03:54:48 +00:00
|
|
|
static uint8_t usb_ep0out_buffer_len[EP0OUT_BUFFERS];
|
2019-03-10 14:41:22 +00:00
|
|
|
static uint8_t usb_ep0out_last_tok[EP0OUT_BUFFERS];
|
2019-03-26 01:39:55 +00:00
|
|
|
static volatile uint8_t usb_ep0out_wr_ptr;
|
|
|
|
static volatile uint8_t usb_ep0out_rd_ptr;
|
2019-03-20 05:14:20 +00:00
|
|
|
static const int max_byte_length = 64;
|
2019-02-28 02:48:25 +00:00
|
|
|
|
|
|
|
static const uint8_t *current_data;
|
|
|
|
static int current_length;
|
|
|
|
static int current_offset;
|
|
|
|
static int current_to_send;
|
|
|
|
|
2019-03-05 03:54:48 +00:00
|
|
|
static int queue_more_data(int epnum) {
|
2019-02-28 02:48:25 +00:00
|
|
|
(void)epnum;
|
2019-03-10 14:42:14 +00:00
|
|
|
|
|
|
|
// Don't allow requeueing -- only queue more data if we're
|
|
|
|
// currently set up to respond NAK.
|
2019-02-28 02:48:25 +00:00
|
|
|
if (usb_ep_0_in_respond_read() != EPF_NAK)
|
|
|
|
return -1;
|
|
|
|
|
2019-03-10 14:42:14 +00:00
|
|
|
// Prevent us from double-filling the buffer.
|
|
|
|
if (!usb_ep_0_in_ibuf_empty_read()) {
|
|
|
|
usb_ep_0_in_respond_write(EPF_ACK);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-02-28 02:48:25 +00:00
|
|
|
int this_offset;
|
|
|
|
current_to_send = current_length - current_offset;
|
|
|
|
if (current_to_send > max_byte_length)
|
|
|
|
current_to_send = max_byte_length;
|
|
|
|
|
2019-03-05 03:54:48 +00:00
|
|
|
for (this_offset = current_offset; this_offset < (current_offset + current_to_send); this_offset++) {
|
2019-02-28 02:48:25 +00:00
|
|
|
usb_ep_0_in_ibuf_head_write(current_data[this_offset]);
|
|
|
|
}
|
|
|
|
usb_ep_0_in_respond_write(EPF_ACK);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-05 03:54:48 +00:00
|
|
|
int usb_send(struct usb_device *dev, int epnum, const void *data, int total_count) {
|
|
|
|
(void)dev;
|
2019-03-10 14:42:14 +00:00
|
|
|
|
2019-03-26 01:39:55 +00:00
|
|
|
while (current_data || current_length)
|
|
|
|
;
|
2019-02-28 02:48:25 +00:00
|
|
|
current_data = (uint8_t *)data;
|
|
|
|
current_length = total_count;
|
|
|
|
current_offset = 0;
|
2019-03-05 03:54:48 +00:00
|
|
|
control_state = IN_DATA;
|
|
|
|
queue_more_data(epnum);
|
2019-03-10 14:42:14 +00:00
|
|
|
|
2019-02-28 02:48:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-28 03:13:25 +00:00
|
|
|
int usb_wait_for_send_done(struct usb_device *dev) {
|
|
|
|
while (current_data && current_length)
|
|
|
|
usb_poll(dev);
|
2019-04-05 09:33:02 +00:00
|
|
|
while ((usb_ep_0_in_dtb_read() & 1) == 1)
|
|
|
|
usb_poll(dev);
|
2019-03-28 03:13:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-02-28 02:48:25 +00:00
|
|
|
void usb_isr(void) {
|
|
|
|
irq_count++;
|
|
|
|
uint8_t ep0o_pending = usb_ep_0_out_ev_pending_read();
|
|
|
|
uint8_t ep0i_pending = usb_ep_0_in_ev_pending_read();
|
|
|
|
|
2019-03-05 03:54:48 +00:00
|
|
|
// We got an OUT or a SETUP packet. Copy it to usb_ep0out_buffer
|
|
|
|
// and clear the "pending" bit.
|
2019-02-28 02:48:25 +00:00
|
|
|
if (ep0o_pending) {
|
2019-03-10 14:42:14 +00:00
|
|
|
uint8_t last_tok = usb_ep_0_out_last_tok_read();
|
2019-03-05 03:54:48 +00:00
|
|
|
|
2019-02-28 02:48:25 +00:00
|
|
|
int byte_count = 0;
|
2019-03-10 14:42:14 +00:00
|
|
|
usb_ep0out_last_tok[usb_ep0out_wr_ptr] = last_tok;
|
2019-02-28 02:48:25 +00:00
|
|
|
uint8_t *obuf = usb_ep0out_buffer[usb_ep0out_wr_ptr];
|
2019-03-05 03:54:48 +00:00
|
|
|
while (!usb_ep_0_out_obuf_empty_read()) {
|
|
|
|
obuf[byte_count++] = usb_ep_0_out_obuf_head_read();
|
2019-02-28 02:48:25 +00:00
|
|
|
usb_ep_0_out_obuf_head_write(0);
|
|
|
|
}
|
|
|
|
usb_ep_0_out_ev_pending_write(ep0o_pending);
|
2019-03-28 03:13:25 +00:00
|
|
|
usb_ep0out_buffer_len[usb_ep0out_wr_ptr] = byte_count - 2 /* Strip off CRC16 */;
|
2019-03-10 14:42:14 +00:00
|
|
|
usb_ep0out_wr_ptr = (usb_ep0out_wr_ptr + 1) & (EP0OUT_BUFFERS-1);
|
2019-02-28 02:48:25 +00:00
|
|
|
|
2019-03-11 03:51:19 +00:00
|
|
|
if (last_tok == USB_PID_SETUP) {
|
2019-03-10 14:42:14 +00:00
|
|
|
current_offset = 0;
|
|
|
|
current_length = 0;
|
|
|
|
current_data = NULL;
|
2019-03-11 03:51:19 +00:00
|
|
|
control_state = IN_SETUP;
|
2019-02-28 02:48:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 03:54:48 +00:00
|
|
|
// We just got an "IN" token. Send data if we have it.
|
2019-02-28 02:48:25 +00:00
|
|
|
if (ep0i_pending) {
|
|
|
|
usb_ep_0_in_respond_write(EPF_NAK);
|
|
|
|
current_offset += current_to_send;
|
2019-03-05 03:54:48 +00:00
|
|
|
queue_more_data(0);
|
2019-02-28 02:48:25 +00:00
|
|
|
usb_ep_0_in_ev_pending_write(ep0i_pending);
|
2019-03-11 03:51:19 +00:00
|
|
|
usb_ep_0_out_respond_write(EPF_ACK);
|
2019-02-28 02:48:25 +00:00
|
|
|
}
|
2019-03-28 03:13:25 +00:00
|
|
|
|
2019-02-28 02:48:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_wait(void) {
|
|
|
|
while (!irq_count)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_irq_happened(void) {
|
2019-03-05 03:54:48 +00:00
|
|
|
return irq_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_ack(struct usb_device *dev, int epnum) {
|
2019-03-11 03:51:19 +00:00
|
|
|
(void)dev;
|
|
|
|
(void)epnum;
|
2019-04-05 09:33:02 +00:00
|
|
|
usb_ep_0_in_dtb_write(1);
|
|
|
|
usb_ep_0_out_dtb_write(1);
|
2019-03-05 03:54:48 +00:00
|
|
|
usb_ep_0_out_respond_write(EPF_ACK);
|
|
|
|
usb_ep_0_in_respond_write(EPF_ACK);
|
2019-03-26 01:39:55 +00:00
|
|
|
return 0;
|
2019-03-05 03:54:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int usb_err(struct usb_device *dev, int epnum) {
|
2019-03-11 03:51:19 +00:00
|
|
|
(void)dev;
|
|
|
|
(void)epnum;
|
2019-03-05 03:54:48 +00:00
|
|
|
usb_ep_0_out_respond_write(EPF_STALL);
|
|
|
|
usb_ep_0_in_respond_write(EPF_STALL);
|
2019-03-26 01:39:55 +00:00
|
|
|
return 0;
|
2019-03-05 03:54:48 +00:00
|
|
|
}
|
|
|
|
|
2019-03-26 01:39:55 +00:00
|
|
|
// int puts_noendl(const char *s);
|
|
|
|
// static void print_eptype(void) {
|
|
|
|
// switch (usb_ep0out_last_tok[usb_ep0out_rd_ptr]) {
|
|
|
|
// case 0: puts("O"); break;
|
|
|
|
// // case 1: puts("SOF"); break;
|
|
|
|
// // case 2: puts("IN"); break;
|
|
|
|
// case 3: puts("S"); break;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2019-03-05 03:54:48 +00:00
|
|
|
int usb_recv(struct usb_device *dev, void *buffer, unsigned int buffer_len) {
|
2019-03-26 01:39:55 +00:00
|
|
|
(void)dev;
|
2019-02-28 02:48:25 +00:00
|
|
|
|
2019-03-26 01:39:55 +00:00
|
|
|
// Set the OUT response to ACK, since we are in a position to receive data now.
|
|
|
|
usb_ep_0_out_respond_write(EPF_ACK);
|
|
|
|
while (1) {
|
|
|
|
if (usb_ep0out_rd_ptr != usb_ep0out_wr_ptr) {
|
|
|
|
if (usb_ep0out_last_tok[usb_ep0out_rd_ptr] == USB_PID_OUT) {
|
|
|
|
unsigned int ep0_buffer_len = usb_ep0out_buffer_len[usb_ep0out_rd_ptr];
|
|
|
|
if (ep0_buffer_len < buffer_len)
|
|
|
|
buffer_len = ep0_buffer_len;
|
|
|
|
usb_ep0out_buffer_len[usb_ep0out_rd_ptr] = 0;
|
|
|
|
memcpy(buffer, &usb_ep0out_buffer[usb_ep0out_rd_ptr], buffer_len);
|
|
|
|
usb_ep0out_rd_ptr = (usb_ep0out_rd_ptr + 1) & (EP0OUT_BUFFERS-1);
|
|
|
|
return buffer_len;
|
|
|
|
}
|
|
|
|
usb_ep0out_rd_ptr = (usb_ep0out_rd_ptr + 1) & (EP0OUT_BUFFERS-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2019-03-05 03:54:48 +00:00
|
|
|
|
2019-03-28 03:13:25 +00:00
|
|
|
void usb_poll(struct usb_device *dev) {
|
|
|
|
(void)dev;
|
2019-03-05 03:54:48 +00:00
|
|
|
// If some data was received, then process it.
|
|
|
|
if (usb_ep0out_rd_ptr != usb_ep0out_wr_ptr) {
|
|
|
|
const struct usb_setup_request *request = (const struct usb_setup_request *)(usb_ep0out_buffer[usb_ep0out_rd_ptr]);
|
2019-03-26 01:39:55 +00:00
|
|
|
// unsigned int len = usb_ep0out_buffer_len[usb_ep0out_rd_ptr];
|
|
|
|
uint8_t last_tok = usb_ep0out_last_tok[usb_ep0out_rd_ptr];
|
2019-03-10 14:42:14 +00:00
|
|
|
|
2019-03-05 03:54:48 +00:00
|
|
|
usb_ep0out_buffer_len[usb_ep0out_rd_ptr] = 0;
|
|
|
|
usb_ep0out_rd_ptr = (usb_ep0out_rd_ptr + 1) & (EP0OUT_BUFFERS-1);
|
2019-03-26 01:39:55 +00:00
|
|
|
|
|
|
|
if (last_tok == USB_PID_SETUP) {
|
|
|
|
usb_setup(NULL, request);
|
|
|
|
}
|
2019-03-05 03:54:48 +00:00
|
|
|
}
|
2019-03-10 07:25:33 +00:00
|
|
|
|
2019-03-10 14:42:14 +00:00
|
|
|
if ((usb_ep_0_in_respond_read() == EPF_NAK) && (current_data)) {
|
|
|
|
current_offset += current_to_send;
|
2019-03-10 07:25:33 +00:00
|
|
|
queue_more_data(0);
|
2019-03-10 14:42:14 +00:00
|
|
|
}
|
2019-02-28 02:48:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CSR_USB_EP_0_OUT_EV_PENDING_ADDR */
|