From 1463b0b521a6df505d7d1799f33670b871c2caab Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Tue, 28 Mar 2017 15:01:19 +0100 Subject: usb: Cleanup and more debug --- usb/bdt.h | 2 +- usb/descriptors.h | 5 +++++ usb/endpt0.c | 39 +++++++++++++++++++++------------------ usb/usb.c | 3 ++- 4 files changed, 29 insertions(+), 20 deletions(-) diff --git a/usb/bdt.h b/usb/bdt.h index 5f0a123..2945e2b 100644 --- a/usb/bdt.h +++ b/usb/bdt.h @@ -3,7 +3,7 @@ #include -extern struct usb0_bd usb_bdt[4 * 16]; +extern volatile struct usb0_bd usb_bdt[4 * 16]; #define BDT_ENDPT(n, tx, odd) (usb_bdt[(n) << 2 | (tx & 1) << 1 | (odd & 1)]) #define BDT_RX 0 diff --git a/usb/descriptors.h b/usb/descriptors.h index 8ff539e..f60ed86 100644 --- a/usb/descriptors.h +++ b/usb/descriptors.h @@ -1,6 +1,11 @@ #ifndef USB_DESCRIPTORS_H #define USB_DESCRIPTORS_H +/* + * TODO: Move into a source file and use extern so there's only ever one copy + * LTO should take care of the rest. + */ + /* Device descriptor */ unsigned char ds_dev[] = { 18, // Length diff --git a/usb/endpt0.c b/usb/endpt0.c index 22d78df..485e464 100644 --- a/usb/endpt0.c +++ b/usb/endpt0.c @@ -16,8 +16,8 @@ #define MAX_PACKET 64 static unsigned char buf[2][MAX_PACKET]; -static bool odd; -static bool data01; +static bool tx_odd; +static bool tx_data01; static volatile unsigned int nextaddr; @@ -47,15 +47,16 @@ static void read_setup(struct tok_setup *setup, const void *_data) /* TODO: Make this a shared thing across all USB endpoints */ /* puttx: place data in the current buffer descriptor */ -static bool puttx(const void *data, size_t size) +static bool puttx(void *data, size_t size) { - if (GET_BIT(BDT_ENDPT(0, BDT_TX, odd).desc, BD_OWN)) + if (GET_BIT(BDT_ENDPT(0, BDT_TX, tx_odd).desc, BD_OWN)) return false; - BDT_ENDPT(0, BDT_TX, odd).addr = (void *)data; - BDT_ENDPT(0, BDT_TX, odd).desc = USB0_BD_INIT(size, data01); - odd = !odd; - data01 = !data01; + /* TODO: Just stop bothering with const */ + BDT_ENDPT(0, BDT_TX, tx_odd).addr = data; + BDT_ENDPT(0, BDT_TX, tx_odd).desc = USB0_BD_INIT(size, tx_data01); + tx_odd = !tx_odd; + tx_data01 = !tx_data01; return true; } @@ -74,7 +75,7 @@ static bool pushtx(void) if (!puttx(tx_data, size)) return false; - tx_data = (const char *)tx_data + size; + tx_data = (char *)tx_data + size; tx_size -= size; if (tx_size == 0 && size < MAX_PACKET) @@ -84,7 +85,7 @@ static bool pushtx(void) } /* quetx: enqueue a transmission */ -static void quetx(const void *data, size_t size) +static void quetx(void *data, size_t size) { tx_data = data; tx_size = size; @@ -116,8 +117,8 @@ void usb_endpt0_enable(void) USB0_ENDPT(0) = BV(ENDPT_EPRXEN) | BV(ENDPT_EPTXEN) | BV(ENDPT_EPHSHK); nextaddr = 0; tx_data = NULL; - odd = 0; - data01 = 0; + tx_odd = 0; + tx_data01 = 0; } /* trunc: truncate size_t to a limit TODO: MOVE THIS */ @@ -163,7 +164,9 @@ static void tok_setup(struct tok_setup *setup) } /* fall through */ default: - /* since this EP was called successfuly, we unstall it */ + /* We received an unexpected SETUP so we STALL the endpoint */ + printf("Unexpected SETUP: RT: 0x%x R: 0x%x V: 0x%x\n", + setup->reqtyp, setup->req, setup->value); SET_BIT(USB0_ENDPT(0), ENDPT_EPSTALL); break; } @@ -176,7 +179,6 @@ void usb_endpt0_token(uint8_t state) bd = &BDT_ENDPT(0, GET_BIT(state, STAT_TX), GET_BIT(state, STAT_ODD)); - switch (GET_BITS(bd->desc, BD_TOK_PID)) { case BD_TOK_PID_OUT: uart_puts(" PID_OUT"); @@ -199,13 +201,14 @@ void usb_endpt0_token(uint8_t state) BDT_ENDPT(0, BDT_TX, BDT_ODD).desc = 0; tx_data = NULL; - data01 = 1; + tx_data01 = 1; tok_setup(&setup); + /* SETUP sets CTL_TXSUSPENDTOKENBUSY to suspend TX */ + uart_printf("Clearing USB0_CTL, was: 0x%x", (unsigned)USB0_CTL); + USB0_CTL = 0; + break; } - - /*SET_BIT(USB0_CTL, CTL_ODDRST);*/ - USB0_CTL = BV(CTL_USBENSOFEN); } diff --git a/usb/usb.c b/usb/usb.c index a8cbbf8..c37fc5f 100644 --- a/usb/usb.c +++ b/usb/usb.c @@ -15,8 +15,9 @@ /* TODO: Only use the number of endpoints that are needed */ /* TODO: Try using static */ +/* TODO: Try [16][2][2] since everything is contiguous. */ __attribute__ ((aligned(512))) -struct usb0_bd usb_bdt[16 * 4]; +volatile struct usb0_bd usb_bdt[16 * 4]; /* usb_setup: Setup function for USB subsystem */ void usb_setup(void) -- cgit v1.2.3-54-g00ecf