]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Output/pjrcUSB/arm/usb_dev.c
Initial code for ARM UART output module (mainly for CLI)
[kiibohd-controller.git] / Output / pjrcUSB / arm / usb_dev.c
1 /* Teensyduino Core Library
2  * http://www.pjrc.com/teensy/
3  * Copyright (c) 2013 PJRC.COM, LLC.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * 1. The above copyright notice and this permission notice shall be 
14  * included in all copies or substantial portions of the Software.
15  *
16  * 2. If the Software is incorporated into a build system that allows 
17  * selection among a list of target devices, then similar target
18  * devices manufactured by PJRC.COM must be included in the list of
19  * target devices and selectable in the same manner.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
25  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
26  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28  * SOFTWARE.
29  */
30
31 #include <Lib/OutputLib.h>
32 #include "usb_dev.h"
33 #include "usb_mem.h"
34
35 // buffer descriptor table
36
37 typedef struct {
38         uint32_t desc;
39         void * addr;
40 } bdt_t;
41
42 __attribute__ ((section(".usbdescriptortable"), used))
43 static bdt_t table[(NUM_ENDPOINTS+1)*4];
44
45 static usb_packet_t *rx_first[NUM_ENDPOINTS];
46 static usb_packet_t *rx_last[NUM_ENDPOINTS];
47 static usb_packet_t *tx_first[NUM_ENDPOINTS];
48 static usb_packet_t *tx_last[NUM_ENDPOINTS];
49 uint16_t usb_rx_byte_count_data[NUM_ENDPOINTS];
50
51 static uint8_t tx_state[NUM_ENDPOINTS];
52 #define TX_STATE_BOTH_FREE_EVEN_FIRST   0
53 #define TX_STATE_BOTH_FREE_ODD_FIRST    1
54 #define TX_STATE_EVEN_FREE              2
55 #define TX_STATE_ODD_FREE               3
56 #define TX_STATE_NONE_FREE_EVEN_FIRST   4
57 #define TX_STATE_NONE_FREE_ODD_FIRST    5
58
59 #define BDT_OWN         0x80
60 #define BDT_DATA1       0x40
61 #define BDT_DATA0       0x00
62 #define BDT_DTS         0x08
63 #define BDT_STALL       0x04
64 #define BDT_PID(n)      (((n) >> 2) & 15)
65
66 #define BDT_DESC(count, data)   (BDT_OWN | BDT_DTS \
67                                 | ((data) ? BDT_DATA1 : BDT_DATA0) \
68                                 | ((count) << 16))
69
70 #define TX   1
71 #define RX   0
72 #define ODD  1
73 #define EVEN 0
74 #define DATA0 0
75 #define DATA1 1
76 #define index(endpoint, tx, odd) (((endpoint) << 2) | ((tx) << 1) | (odd))
77 #define stat2bufferdescriptor(stat) (table + ((stat) >> 2))
78
79
80 static union {
81  struct {
82   union {
83    struct {
84         uint8_t bmRequestType;
85         uint8_t bRequest;
86    };
87         uint16_t wRequestAndType;
88   };
89         uint16_t wValue;
90         uint16_t wIndex;
91         uint16_t wLength;
92  };
93  struct {
94         uint32_t word1;
95         uint32_t word2;
96  };
97 } setup;
98
99
100 #define GET_STATUS              0
101 #define CLEAR_FEATURE           1
102 #define SET_FEATURE             3
103 #define SET_ADDRESS             5
104 #define GET_DESCRIPTOR          6
105 #define SET_DESCRIPTOR          7
106 #define GET_CONFIGURATION       8
107 #define SET_CONFIGURATION       9
108 #define GET_INTERFACE           10
109 #define SET_INTERFACE           11
110 #define SYNCH_FRAME             12
111
112 // SETUP always uses a DATA0 PID for the data field of the SETUP transaction.
113 // transactions in the data phase start with DATA1 and toggle (figure 8-12, USB1.1)
114 // Status stage uses a DATA1 PID.
115
116 static uint8_t ep0_rx0_buf[EP0_SIZE] __attribute__ ((aligned (4)));
117 static uint8_t ep0_rx1_buf[EP0_SIZE] __attribute__ ((aligned (4)));
118 static const uint8_t *ep0_tx_ptr = NULL;
119 static uint16_t ep0_tx_len;
120 static uint8_t ep0_tx_bdt_bank = 0;
121 static uint8_t ep0_tx_data_toggle = 0;
122 uint8_t usb_rx_memory_needed = 0;
123
124 volatile uint8_t usb_configuration = 0;
125 volatile uint8_t usb_reboot_timer = 0;
126
127
128 static void endpoint0_stall(void)
129 {
130         USB0_ENDPT0 = USB_ENDPT_EPSTALL | USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK;
131 }
132
133
134 static void endpoint0_transmit(const void *data, uint32_t len)
135 {
136 #if 0
137         serial_print("tx0:");
138         serial_phex32((uint32_t)data);
139         serial_print(",");
140         serial_phex16(len);
141         serial_print(ep0_tx_bdt_bank ? ", odd" : ", even");
142         serial_print(ep0_tx_data_toggle ? ", d1\n" : ", d0\n");
143 #endif
144         table[index(0, TX, ep0_tx_bdt_bank)].addr = (void *)data;
145         table[index(0, TX, ep0_tx_bdt_bank)].desc = BDT_DESC(len, ep0_tx_data_toggle);
146         ep0_tx_data_toggle ^= 1;
147         ep0_tx_bdt_bank ^= 1;
148 }
149
150 static uint8_t reply_buffer[8];
151
152 static void usb_setup(void)
153 {
154         const uint8_t *data = NULL;
155         uint32_t datalen = 0;
156         const usb_descriptor_list_t *list;
157         uint32_t size;
158         volatile uint8_t *reg;
159         uint8_t epconf;
160         const uint8_t *cfg;
161         int i;
162
163         switch (setup.wRequestAndType) {
164           case 0x0500: // SET_ADDRESS
165                 break;
166           case 0x0900: // SET_CONFIGURATION
167                 //serial_print("configure\n");
168                 usb_configuration = setup.wValue;
169                 reg = &USB0_ENDPT1;
170                 cfg = usb_endpoint_config_table;
171                 // clear all BDT entries, free any allocated memory...
172                 for (i=4; i < (NUM_ENDPOINTS+1)*4; i++) {
173                         if (table[i].desc & BDT_OWN) {
174                                 usb_free((usb_packet_t *)((uint8_t *)(table[i].addr) - 8));
175                         }
176                 }
177                 // free all queued packets
178                 for (i=0; i < NUM_ENDPOINTS; i++) {
179                         usb_packet_t *p, *n;
180                         p = rx_first[i];
181                         while (p) {
182                                 n = p->next;
183                                 usb_free(p);
184                                 p = n;
185                         }
186                         rx_first[i] = NULL;
187                         rx_last[i] = NULL;
188                         p = tx_first[i];
189                         while (p) {
190                                 n = p->next;
191                                 usb_free(p);
192                                 p = n;
193                         }
194                         tx_first[i] = NULL;
195                         tx_last[i] = NULL;
196                         usb_rx_byte_count_data[i] = 0;
197                         switch (tx_state[i]) {
198                           case TX_STATE_EVEN_FREE:
199                           case TX_STATE_NONE_FREE_EVEN_FIRST:
200                                 tx_state[i] = TX_STATE_BOTH_FREE_EVEN_FIRST;
201                                 break;
202                           case TX_STATE_ODD_FREE:
203                           case TX_STATE_NONE_FREE_ODD_FIRST:
204                                 tx_state[i] = TX_STATE_BOTH_FREE_ODD_FIRST;
205                                 break;
206                           default:
207                                 break;
208                         }
209                 }
210                 usb_rx_memory_needed = 0;
211                 for (i=1; i <= NUM_ENDPOINTS; i++) {
212                         epconf = *cfg++;
213                         *reg = epconf;
214                         reg += 4;
215                         if (epconf & USB_ENDPT_EPRXEN) {
216                                 usb_packet_t *p;
217                                 p = usb_malloc();
218                                 if (p) {
219                                         table[index(i, RX, EVEN)].addr = p->buf;
220                                         table[index(i, RX, EVEN)].desc = BDT_DESC(64, 0);
221                                 } else {
222                                         table[index(i, RX, EVEN)].desc = 0;
223                                         usb_rx_memory_needed++;
224                                 }
225                                 p = usb_malloc();
226                                 if (p) {
227                                         table[index(i, RX, ODD)].addr = p->buf;
228                                         table[index(i, RX, ODD)].desc = BDT_DESC(64, 1);
229                                 } else {
230                                         table[index(i, RX, ODD)].desc = 0;
231                                         usb_rx_memory_needed++;
232                                 }
233                         }
234                         table[index(i, TX, EVEN)].desc = 0;
235                         table[index(i, TX, ODD)].desc = 0;
236                 }
237                 break;
238           case 0x0880: // GET_CONFIGURATION
239                 reply_buffer[0] = usb_configuration;
240                 datalen = 1;
241                 data = reply_buffer;
242                 break;
243           case 0x0080: // GET_STATUS (device)
244                 reply_buffer[0] = 0;
245                 reply_buffer[1] = 0;
246                 datalen = 2;
247                 data = reply_buffer;
248                 break;
249           case 0x0082: // GET_STATUS (endpoint)
250                 if (setup.wIndex > NUM_ENDPOINTS) {
251                         // TODO: do we need to handle IN vs OUT here?
252                         endpoint0_stall();
253                         return;
254                 }
255                 reply_buffer[0] = 0;
256                 reply_buffer[1] = 0;
257                 if (*(uint8_t *)(&USB0_ENDPT0 + setup.wIndex * 4) & 0x02) reply_buffer[0] = 1;
258                 data = reply_buffer;
259                 datalen = 2;
260                 break;
261           case 0x0102: // CLEAR_FEATURE (endpoint)
262                 i = setup.wIndex & 0x7F;
263                 if (i > NUM_ENDPOINTS || setup.wValue != 0) {
264                         // TODO: do we need to handle IN vs OUT here?
265                         endpoint0_stall();
266                         return;
267                 }
268                 (*(uint8_t *)(&USB0_ENDPT0 + setup.wIndex * 4)) &= ~0x02;
269                 // TODO: do we need to clear the data toggle here?
270                 break;
271           case 0x0302: // SET_FEATURE (endpoint)
272                 i = setup.wIndex & 0x7F;
273                 if (i > NUM_ENDPOINTS || setup.wValue != 0) {
274                         // TODO: do we need to handle IN vs OUT here?
275                         endpoint0_stall();
276                         return;
277                 }
278                 (*(uint8_t *)(&USB0_ENDPT0 + setup.wIndex * 4)) |= 0x02;
279                 // TODO: do we need to clear the data toggle here?
280                 break;
281           case 0x0680: // GET_DESCRIPTOR
282           case 0x0681:
283                 //serial_print("desc:");
284                 //serial_phex16(setup.wValue);
285                 //serial_print("\n");
286                 for (list = usb_descriptor_list; 1; list++) {
287                         if (list->addr == NULL) break;
288                         //if (setup.wValue == list->wValue && 
289                         //(setup.wIndex == list->wIndex) || ((setup.wValue >> 8) == 3)) {
290                         if (setup.wValue == list->wValue && setup.wIndex == list->wIndex) {
291                                 data = list->addr;
292                                 if ((setup.wValue >> 8) == 3) {
293                                         // for string descriptors, use the descriptor's
294                                         // length field, allowing runtime configured
295                                         // length.
296                                         datalen = *(list->addr);
297                                 } else {
298                                         datalen = list->length;
299                                 }
300 #if 0
301                                 serial_print("Desc found, ");
302                                 serial_phex32((uint32_t)data);
303                                 serial_print(",");
304                                 serial_phex16(datalen);
305                                 serial_print(",");
306                                 serial_phex(data[0]);
307                                 serial_phex(data[1]);
308                                 serial_phex(data[2]);
309                                 serial_phex(data[3]);
310                                 serial_phex(data[4]);
311                                 serial_phex(data[5]);
312                                 serial_print("\n");
313 #endif
314                                 goto send;
315                         }
316                 }
317                 //serial_print("desc: not found\n");
318                 endpoint0_stall();
319                 return;
320 #if defined(CDC_STATUS_INTERFACE)
321           case 0x2221: // CDC_SET_CONTROL_LINE_STATE
322                 usb_cdc_line_rtsdtr = setup.wValue;
323                 //serial_print("set control line state\n");
324                 break;
325           case 0x2021: // CDC_SET_LINE_CODING
326                 //serial_print("set coding, waiting...\n");
327                 return;
328 #endif
329
330 // TODO: this does not work... why?
331 #if defined(SEREMU_INTERFACE) || defined(KEYBOARD_INTERFACE)
332           case 0x0921: // HID SET_REPORT
333                 //serial_print(":)\n");
334                 return;
335           case 0x0A21: // HID SET_IDLE
336                 break;
337           // case 0xC940:
338 #endif
339           default:
340                 endpoint0_stall();
341                 return;
342         }
343         send:
344         //serial_print("setup send ");
345         //serial_phex32(data);
346         //serial_print(",");
347         //serial_phex16(datalen);
348         //serial_print("\n");
349
350         if (datalen > setup.wLength) datalen = setup.wLength;
351         size = datalen;
352         if (size > EP0_SIZE) size = EP0_SIZE;
353         endpoint0_transmit(data, size);
354         data += size;
355         datalen -= size;
356         if (datalen == 0 && size < EP0_SIZE) return;
357
358         size = datalen;
359         if (size > EP0_SIZE) size = EP0_SIZE;
360         endpoint0_transmit(data, size);
361         data += size;
362         datalen -= size;
363         if (datalen == 0 && size < EP0_SIZE) return;
364
365         ep0_tx_ptr = data;
366         ep0_tx_len = datalen;
367 }
368
369
370
371 //A bulk endpoint's toggle sequence is initialized to DATA0 when the endpoint
372 //experiences any configuration event (configuration events are explained in
373 //Sections 9.1.1.5 and 9.4.5).
374
375 //Configuring a device or changing an alternate setting causes all of the status
376 //and configuration values associated with endpoints in the affected interfaces
377 //to be set to their default values. This includes setting the data toggle of
378 //any endpoint using data toggles to the value DATA0.
379
380 //For endpoints using data toggle, regardless of whether an endpoint has the
381 //Halt feature set, a ClearFeature(ENDPOINT_HALT) request always results in the
382 //data toggle being reinitialized to DATA0.
383
384
385
386 // #define stat2bufferdescriptor(stat) (table + ((stat) >> 2))
387
388 static void usb_control(uint32_t stat)
389 {
390         bdt_t *b;
391         uint32_t pid, size;
392         uint8_t *buf;
393         const uint8_t *data;
394
395         b = stat2bufferdescriptor(stat);
396         pid = BDT_PID(b->desc);
397         //count = b->desc >> 16;
398         buf = b->addr;
399         //serial_print("pid:");
400         //serial_phex(pid);
401         //serial_print(", count:");
402         //serial_phex(count);
403         //serial_print("\n");
404
405         switch (pid) {
406         case 0x0D: // Setup received from host
407                 //serial_print("PID=Setup\n");
408                 //if (count != 8) ; // panic?
409                 // grab the 8 byte setup info
410                 setup.word1 = *(uint32_t *)(buf);
411                 setup.word2 = *(uint32_t *)(buf + 4);
412
413                 // give the buffer back
414                 b->desc = BDT_DESC(EP0_SIZE, DATA1);
415                 //table[index(0, RX, EVEN)].desc = BDT_DESC(EP0_SIZE, 1);
416                 //table[index(0, RX, ODD)].desc = BDT_DESC(EP0_SIZE, 1);
417
418                 // clear any leftover pending IN transactions
419                 ep0_tx_ptr = NULL;
420                 if (ep0_tx_data_toggle) {
421                 }
422                 //if (table[index(0, TX, EVEN)].desc & 0x80) {
423                         //serial_print("leftover tx even\n");
424                 //}
425                 //if (table[index(0, TX, ODD)].desc & 0x80) {
426                         //serial_print("leftover tx odd\n");
427                 //}
428                 table[index(0, TX, EVEN)].desc = 0;
429                 table[index(0, TX, ODD)].desc = 0;
430                 // first IN after Setup is always DATA1
431                 ep0_tx_data_toggle = 1;
432
433 #if 0
434                 serial_print("bmRequestType:");
435                 serial_phex(setup.bmRequestType);
436                 serial_print(", bRequest:");
437                 serial_phex(setup.bRequest);
438                 serial_print(", wValue:");
439                 serial_phex16(setup.wValue);
440                 serial_print(", wIndex:");
441                 serial_phex16(setup.wIndex);
442                 serial_print(", len:");
443                 serial_phex16(setup.wLength);
444                 serial_print("\n");
445 #endif
446                 // actually "do" the setup request
447                 usb_setup();
448                 // unfreeze the USB, now that we're ready
449                 USB0_CTL = USB_CTL_USBENSOFEN; // clear TXSUSPENDTOKENBUSY bit
450                 break;
451         case 0x01:  // OUT transaction received from host
452         case 0x02:
453                 //serial_print("PID=OUT\n");
454 #ifdef CDC_STATUS_INTERFACE
455                 if (setup.wRequestAndType == 0x2021 /*CDC_SET_LINE_CODING*/) {
456                         int i;
457                         uint8_t *dst = (uint8_t *)usb_cdc_line_coding;
458                         //serial_print("set line coding ");
459                         for (i=0; i<7; i++) {
460                                 //serial_phex(*buf);
461                                 *dst++ = *buf++;
462                         }
463                         //serial_phex32(usb_cdc_line_coding[0]);
464                         //serial_print("\n");
465                         if (usb_cdc_line_coding[0] == 134) usb_reboot_timer = 15;
466                         endpoint0_transmit(NULL, 0);
467                 }
468 #endif
469 #ifdef KEYBOARD_INTERFACE
470                 if (setup.word1 == 0x02000921 && setup.word2 == ((1<<16)|KEYBOARD_INTERFACE)) {
471                         USBKeys_LEDs = buf[0];
472                         endpoint0_transmit(NULL, 0);
473                 }
474 #endif
475                 // give the buffer back
476                 b->desc = BDT_DESC(EP0_SIZE, DATA1);
477                 break;
478
479         case 0x09: // IN transaction completed to host
480                 //serial_print("PID=IN:");
481                 //serial_phex(stat);
482                 //serial_print("\n");
483
484                 // send remaining data, if any...
485                 data = ep0_tx_ptr;
486                 if (data) {
487                         size = ep0_tx_len;
488                         if (size > EP0_SIZE) size = EP0_SIZE;
489                         endpoint0_transmit(data, size);
490                         data += size;
491                         ep0_tx_len -= size;
492                         ep0_tx_ptr = (ep0_tx_len > 0 || size == EP0_SIZE) ? data : NULL;
493                 }
494
495                 if (setup.bRequest == 5 && setup.bmRequestType == 0) {
496                         setup.bRequest = 0;
497                         //serial_print("set address: ");
498                         //serial_phex16(setup.wValue);
499                         //serial_print("\n");
500                         USB0_ADDR = setup.wValue;
501                 }
502
503                 break;
504         //default:
505                 //serial_print("PID=unknown:");
506                 //serial_phex(pid);
507                 //serial_print("\n");
508         }
509         USB0_CTL = USB_CTL_USBENSOFEN; // clear TXSUSPENDTOKENBUSY bit
510 }
511
512
513
514 static usb_packet_t *rx_first[NUM_ENDPOINTS];
515 static usb_packet_t *rx_last[NUM_ENDPOINTS];
516 static usb_packet_t *tx_first[NUM_ENDPOINTS];
517 static usb_packet_t *tx_last[NUM_ENDPOINTS];
518
519 static uint8_t tx_state[NUM_ENDPOINTS];
520 #define TX_STATE_BOTH_FREE_EVEN_FIRST   0
521 #define TX_STATE_BOTH_FREE_ODD_FIRST    1
522 #define TX_STATE_EVEN_FREE              2
523 #define TX_STATE_ODD_FREE               3
524 #define TX_STATE_NONE_FREE              4
525
526
527
528 usb_packet_t *usb_rx(uint32_t endpoint)
529 {
530         usb_packet_t *ret;
531         endpoint--;
532         if (endpoint >= NUM_ENDPOINTS) return NULL;
533         __disable_irq();
534         ret = rx_first[endpoint];
535         if (ret) rx_first[endpoint] = ret->next;
536         usb_rx_byte_count_data[endpoint] -= ret->len;
537         __enable_irq();
538         //serial_print("rx, epidx=");
539         //serial_phex(endpoint);
540         //serial_print(", packet=");
541         //serial_phex32(ret);
542         //serial_print("\n");
543         return ret;
544 }
545
546 static uint32_t usb_queue_byte_count(const usb_packet_t *p)
547 {
548         uint32_t count=0;
549
550         __disable_irq();
551         for ( ; p; p = p->next) {
552                 count += p->len;
553         }
554         __enable_irq();
555         return count;
556 }
557
558 uint32_t usb_tx_byte_count(uint32_t endpoint)
559 {
560         endpoint--;
561         if (endpoint >= NUM_ENDPOINTS) return 0;
562         return usb_queue_byte_count(tx_first[endpoint]);
563 }
564
565 uint32_t usb_tx_packet_count(uint32_t endpoint)
566 {
567         const usb_packet_t *p;
568         uint32_t count=0;
569
570         endpoint--;
571         if (endpoint >= NUM_ENDPOINTS) return 0;
572         __disable_irq();
573         for (p = tx_first[endpoint]; p; p = p->next) count++;
574         __enable_irq();
575         return count;
576 }
577
578
579 // Called from usb_free, but only when usb_rx_memory_needed > 0, indicating
580 // receive endpoints are starving for memory.  The intention is to give
581 // endpoints needing receive memory priority over the user's code, which is
582 // likely calling usb_malloc to obtain memory for transmitting.  When the
583 // user is creating data very quickly, their consumption could starve reception
584 // without this prioritization.  The packet buffer (input) is assigned to the
585 // first endpoint needing memory.
586 //
587 void usb_rx_memory(usb_packet_t *packet)
588 {
589         unsigned int i;
590         const uint8_t *cfg;
591
592         cfg = usb_endpoint_config_table;
593         //serial_print("rx_mem:");
594         __disable_irq();
595         for (i=1; i <= NUM_ENDPOINTS; i++) {
596                 if (*cfg++ & USB_ENDPT_EPRXEN) {
597                         if (table[index(i, RX, EVEN)].desc == 0) {
598                                 table[index(i, RX, EVEN)].addr = packet->buf;
599                                 table[index(i, RX, EVEN)].desc = BDT_DESC(64, 0);
600                                 usb_rx_memory_needed--;
601                                 __enable_irq();
602                                 //serial_phex(i);
603                                 //serial_print(",even\n");
604                                 return;
605                         }
606                         if (table[index(i, RX, ODD)].desc == 0) {
607                                 table[index(i, RX, ODD)].addr = packet->buf;
608                                 table[index(i, RX, ODD)].desc = BDT_DESC(64, 1);
609                                 usb_rx_memory_needed--;
610                                 __enable_irq();
611                                 //serial_phex(i);
612                                 //serial_print(",odd\n");
613                                 return;
614                         }
615                 }
616         }
617         __enable_irq();
618         // we should never reach this point.  If we get here, it means
619         // usb_rx_memory_needed was set greater than zero, but no memory
620         // was actually needed.  
621         usb_rx_memory_needed = 0;
622         usb_free(packet);
623         return;
624 }
625
626 //#define index(endpoint, tx, odd) (((endpoint) << 2) | ((tx) << 1) | (odd))
627 //#define stat2bufferdescriptor(stat) (table + ((stat) >> 2))
628
629 void usb_tx(uint32_t endpoint, usb_packet_t *packet)
630 {
631         bdt_t *b = &table[index(endpoint, TX, EVEN)];
632         uint8_t next;
633
634         endpoint--;
635         if (endpoint >= NUM_ENDPOINTS) return;
636         __disable_irq();
637         //serial_print("txstate=");
638         //serial_phex(tx_state[endpoint]);
639         //serial_print("\n");
640         switch (tx_state[endpoint]) {
641           case TX_STATE_BOTH_FREE_EVEN_FIRST:
642                 next = TX_STATE_ODD_FREE;
643                 break;
644           case TX_STATE_BOTH_FREE_ODD_FIRST:
645                 b++;
646                 next = TX_STATE_EVEN_FREE;
647                 break;
648           case TX_STATE_EVEN_FREE:
649                 next = TX_STATE_NONE_FREE_ODD_FIRST;
650                 break;
651           case TX_STATE_ODD_FREE:
652                 b++;
653                 next = TX_STATE_NONE_FREE_EVEN_FIRST;
654                 break;
655           default:
656                 if (tx_first[endpoint] == NULL) {
657                         tx_first[endpoint] = packet;
658                 } else {
659                         tx_last[endpoint]->next = packet;
660                 }
661                 tx_last[endpoint] = packet;
662                 __enable_irq();
663                 return;
664         }
665         tx_state[endpoint] = next;
666         b->addr = packet->buf;
667         b->desc = BDT_DESC(packet->len, ((uint32_t)b & 8) ? DATA1 : DATA0);
668         __enable_irq();
669 }
670
671
672 void usb_device_reload()
673 {
674         asm volatile("bkpt");
675 }
676
677
678 void usb_isr(void)
679 {
680         uint8_t status, stat, t;
681
682         //serial_print("isr");
683         //status = USB0_ISTAT;
684         //serial_phex(status);
685         //serial_print("\n");
686         restart:
687         status = USB0_ISTAT;
688
689         if ((status & USB_INTEN_SOFTOKEN /* 04 */ )) {
690                 if (usb_configuration) {
691                         t = usb_reboot_timer;
692                         if (t) {
693                                 usb_reboot_timer = --t;
694                                 if (!t) usb_device_reload();
695                         }
696 #ifdef CDC_DATA_INTERFACE
697                         t = usb_cdc_transmit_flush_timer;
698                         if (t) {
699                                 usb_cdc_transmit_flush_timer = --t;
700                                 if (t == 0) usb_serial_flush_callback();
701                         }
702 #endif
703                 }
704                 USB0_ISTAT = USB_INTEN_SOFTOKEN;
705         }
706
707         if ((status & USB_ISTAT_TOKDNE /* 08 */ )) {
708                 uint8_t endpoint;
709                 stat = USB0_STAT;
710                 //serial_print("token: ep=");
711                 //serial_phex(stat >> 4);
712                 //serial_print(stat & 0x08 ? ",tx" : ",rx");
713                 //serial_print(stat & 0x04 ? ",odd\n" : ",even\n");
714                 endpoint = stat >> 4;
715                 if (endpoint == 0) {
716                         usb_control(stat);
717                 } else {
718                         bdt_t *b = stat2bufferdescriptor(stat);
719                         usb_packet_t *packet = (usb_packet_t *)((uint8_t *)(b->addr) - 8);
720 #if 0
721                         serial_print("ep:");
722                         serial_phex(endpoint);
723                         serial_print(", pid:");
724                         serial_phex(BDT_PID(b->desc));
725                         serial_print(((uint32_t)b & 8) ? ", odd" : ", even");
726                         serial_print(", count:");
727                         serial_phex(b->desc >> 16);
728                         serial_print("\n");
729 #endif
730                         endpoint--;     // endpoint is index to zero-based arrays
731
732                         if (stat & 0x08) { // transmit
733                                 usb_free(packet);
734                                 packet = tx_first[endpoint];
735                                 if (packet) {
736                                         //serial_print("tx packet\n");
737                                         tx_first[endpoint] = packet->next;
738                                         b->addr = packet->buf;
739                                         switch (tx_state[endpoint]) {
740                                           case TX_STATE_BOTH_FREE_EVEN_FIRST:
741                                                 tx_state[endpoint] = TX_STATE_ODD_FREE;
742                                                 break;
743                                           case TX_STATE_BOTH_FREE_ODD_FIRST:
744                                                 tx_state[endpoint] = TX_STATE_EVEN_FREE;
745                                                 break;
746                                           case TX_STATE_EVEN_FREE:
747                                                 tx_state[endpoint] = TX_STATE_NONE_FREE_ODD_FIRST;
748                                                 break;
749                                           case TX_STATE_ODD_FREE:
750                                                 tx_state[endpoint] = TX_STATE_NONE_FREE_EVEN_FIRST;
751                                                 break;
752                                           default:
753                                                 break;
754                                         }
755                                         b->desc = BDT_DESC(packet->len, ((uint32_t)b & 8) ? DATA1 : DATA0);
756                                 } else {
757                                         //serial_print("tx no packet\n");
758                                         switch (tx_state[endpoint]) {
759                                           case TX_STATE_BOTH_FREE_EVEN_FIRST:
760                                           case TX_STATE_BOTH_FREE_ODD_FIRST:
761                                                 break;
762                                           case TX_STATE_EVEN_FREE:
763                                                 tx_state[endpoint] = TX_STATE_BOTH_FREE_EVEN_FIRST;
764                                                 break;
765                                           case TX_STATE_ODD_FREE:
766                                                 tx_state[endpoint] = TX_STATE_BOTH_FREE_ODD_FIRST;
767                                                 break;
768                                           default:
769                                                 tx_state[endpoint] = ((uint32_t)b & 8) ?
770                                                   TX_STATE_ODD_FREE : TX_STATE_EVEN_FREE;
771                                                 break;
772                                         }
773                                 }
774                         } else { // receive
775                                 packet->len = b->desc >> 16;
776                                 if (packet->len > 0) {
777                                         packet->index = 0;
778                                         packet->next = NULL;
779                                         if (rx_first[endpoint] == NULL) {
780                                                 //serial_print("rx 1st, epidx=");
781                                                 //serial_phex(endpoint);
782                                                 //serial_print(", packet=");
783                                                 //serial_phex32((uint32_t)packet);
784                                                 //serial_print("\n");
785                                                 rx_first[endpoint] = packet;
786                                         } else {
787                                                 //serial_print("rx Nth, epidx=");
788                                                 //serial_phex(endpoint);
789                                                 //serial_print(", packet=");
790                                                 //serial_phex32((uint32_t)packet);
791                                                 //serial_print("\n");
792                                                 rx_last[endpoint]->next = packet;
793                                         }
794                                         rx_last[endpoint] = packet;
795                                         usb_rx_byte_count_data[endpoint] += packet->len;
796                                         // TODO: implement a per-endpoint maximum # of allocated packets
797                                         // so a flood of incoming data on 1 endpoint doesn't starve
798                                         // the others if the user isn't reading it regularly
799                                         packet = usb_malloc();
800                                         if (packet) {
801                                                 b->addr = packet->buf;
802                                                 b->desc = BDT_DESC(64, ((uint32_t)b & 8) ? DATA1 : DATA0);
803                                         } else {
804                                                 //serial_print("starving ");
805                                                 //serial_phex(endpoint + 1);
806                                                 //serial_print(((uint32_t)b & 8) ? ",odd\n" : ",even\n");
807                                                 b->desc = 0;
808                                                 usb_rx_memory_needed++;
809                                         }
810                                 } else {
811                                         b->desc = BDT_DESC(64, ((uint32_t)b & 8) ? DATA1 : DATA0);
812                                 }
813                         }
814
815
816
817
818                 }
819                 USB0_ISTAT = USB_ISTAT_TOKDNE;
820                 goto restart;
821         }
822
823
824
825         if (status & USB_ISTAT_USBRST /* 01 */ ) {
826                 //serial_print("reset\n");
827
828                 // initialize BDT toggle bits
829                 USB0_CTL = USB_CTL_ODDRST;
830                 ep0_tx_bdt_bank = 0;
831
832                 // set up buffers to receive Setup and OUT packets
833                 table[index(0, RX, EVEN)].desc = BDT_DESC(EP0_SIZE, 0);
834                 table[index(0, RX, EVEN)].addr = ep0_rx0_buf;
835                 table[index(0, RX, ODD)].desc = BDT_DESC(EP0_SIZE, 0);
836                 table[index(0, RX, ODD)].addr = ep0_rx1_buf;
837                 table[index(0, TX, EVEN)].desc = 0;
838                 table[index(0, TX, ODD)].desc = 0;
839
840                 // activate endpoint 0
841                 USB0_ENDPT0 = USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK;
842
843                 // clear all ending interrupts
844                 USB0_ERRSTAT = 0xFF;
845                 USB0_ISTAT = 0xFF;
846
847                 // set the address to zero during enumeration
848                 USB0_ADDR = 0;
849
850                 // enable other interrupts
851                 USB0_ERREN = 0xFF;
852                 USB0_INTEN = USB_INTEN_TOKDNEEN |
853                         USB_INTEN_SOFTOKEN |
854                         USB_INTEN_STALLEN |
855                         USB_INTEN_ERROREN |
856                         USB_INTEN_USBRSTEN |
857                         USB_INTEN_SLEEPEN;
858
859                 // is this necessary?
860                 USB0_CTL = USB_CTL_USBENSOFEN;
861                 return;
862         }
863
864
865         if ((status & USB_ISTAT_STALL /* 80 */ )) {
866                 //serial_print("stall:\n");
867                 USB0_ENDPT0 = USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK;
868                 USB0_ISTAT = USB_ISTAT_STALL;
869         }
870         if ((status & USB_ISTAT_ERROR /* 02 */ )) {
871                 uint8_t err = USB0_ERRSTAT;
872                 USB0_ERRSTAT = err;
873                 //serial_print("err:");
874                 //serial_phex(err);
875                 //serial_print("\n");
876                 USB0_ISTAT = USB_ISTAT_ERROR;
877         }
878
879         if ((status & USB_ISTAT_SLEEP /* 10 */ )) {
880                 //serial_print("sleep\n");
881                 USB0_ISTAT = USB_ISTAT_SLEEP;
882         }
883
884 }
885
886
887
888 void usb_init(void)
889 {
890         int i;
891
892         //serial_begin(BAUD2DIV(115200));
893         //serial_print("usb_init\n");
894
895         //usb_init_serialnumber();
896
897         for (i=0; i <= NUM_ENDPOINTS*4; i++) {
898                 table[i].desc = 0;
899                 table[i].addr = 0;
900         }
901
902         // this basically follows the flowchart in the Kinetis
903         // Quick Reference User Guide, Rev. 1, 03/2012, page 141
904
905         // assume 48 MHz clock already running
906         // SIM - enable clock
907         SIM_SCGC4 |= SIM_SCGC4_USBOTG;
908
909         // reset USB module
910         USB0_USBTRC0 = USB_USBTRC_USBRESET;
911         while ((USB0_USBTRC0 & USB_USBTRC_USBRESET) != 0) ; // wait for reset to end
912
913         // set desc table base addr
914         USB0_BDTPAGE1 = ((uint32_t)table) >> 8;
915         USB0_BDTPAGE2 = ((uint32_t)table) >> 16;
916         USB0_BDTPAGE3 = ((uint32_t)table) >> 24;
917
918         // clear all ISR flags
919         USB0_ISTAT = 0xFF;
920         USB0_ERRSTAT = 0xFF;
921         USB0_OTGISTAT = 0xFF;
922
923         USB0_USBTRC0 |= 0x40; // undocumented bit
924
925         // enable USB
926         USB0_CTL = USB_CTL_USBENSOFEN;
927         USB0_USBCTRL = 0;
928
929         // enable reset interrupt
930         USB0_INTEN = USB_INTEN_USBRSTEN;
931
932         // enable interrupt in NVIC...
933         NVIC_SET_PRIORITY(IRQ_USBOTG, 112);
934         NVIC_ENABLE_IRQ(IRQ_USBOTG);
935
936         // enable d+ pullup
937         USB0_CONTROL = USB_CONTROL_DPPULLUPNONOTG;
938 }
939
940 // return 0 if the USB is not configured, or the configuration
941 // number selected by the HOST
942 uint8_t usb_configured(void)
943 {
944         return usb_configuration;
945 }
946