1 /* Teensyduino Core Library
2 * http://www.pjrc.com/teensy/
3 * Copyright (c) 2013 PJRC.COM, LLC.
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:
13 * 1. The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
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.
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
31 #include <Lib/USBLib.h>
35 // buffer descriptor table
42 __attribute__ ((section(".usbdescriptortable"), used))
43 static bdt_t table[(NUM_ENDPOINTS+1)*4];
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];
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
60 #define BDT_DATA1 0x40
61 #define BDT_DATA0 0x00
63 #define BDT_STALL 0x04
64 #define BDT_PID(n) (((n) >> 2) & 15)
66 #define BDT_DESC(count, data) (BDT_OWN | BDT_DTS \
67 | ((data) ? BDT_DATA1 : BDT_DATA0) \
76 #define index(endpoint, tx, odd) (((endpoint) << 2) | ((tx) << 1) | (odd))
77 #define stat2bufferdescriptor(stat) (table + ((stat) >> 2))
84 uint8_t bmRequestType;
87 uint16_t wRequestAndType;
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
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.
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;
124 volatile uint8_t usb_configuration = 0;
125 volatile uint8_t usb_reboot_timer = 0;
128 static void endpoint0_stall(void)
130 USB0_ENDPT0 = USB_ENDPT_EPSTALL | USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK;
134 static void endpoint0_transmit(const void *data, uint32_t len)
137 serial_print("tx0:");
138 serial_phex32((uint32_t)data);
141 serial_print(ep0_tx_bdt_bank ? ", odd" : ", even");
142 serial_print(ep0_tx_data_toggle ? ", d1\n" : ", d0\n");
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;
150 static uint8_t reply_buffer[8];
152 static void usb_setup(void)
154 const uint8_t *data = NULL;
155 uint32_t datalen = 0;
156 const usb_descriptor_list_t *list;
158 volatile uint8_t *reg;
163 switch (setup.wRequestAndType) {
164 case 0x0500: // SET_ADDRESS
166 case 0x0900: // SET_CONFIGURATION
167 //serial_print("configure\n");
168 usb_configuration = setup.wValue;
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));
177 // free all queued packets
178 for (i=0; i < NUM_ENDPOINTS; i++) {
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;
202 case TX_STATE_ODD_FREE:
203 case TX_STATE_NONE_FREE_ODD_FIRST:
204 tx_state[i] = TX_STATE_BOTH_FREE_ODD_FIRST;
210 usb_rx_memory_needed = 0;
211 for (i=1; i <= NUM_ENDPOINTS; i++) {
215 if (epconf & USB_ENDPT_EPRXEN) {
219 table[index(i, RX, EVEN)].addr = p->buf;
220 table[index(i, RX, EVEN)].desc = BDT_DESC(64, 0);
222 table[index(i, RX, EVEN)].desc = 0;
223 usb_rx_memory_needed++;
227 table[index(i, RX, ODD)].addr = p->buf;
228 table[index(i, RX, ODD)].desc = BDT_DESC(64, 1);
230 table[index(i, RX, ODD)].desc = 0;
231 usb_rx_memory_needed++;
234 table[index(i, TX, EVEN)].desc = 0;
235 table[index(i, TX, ODD)].desc = 0;
238 case 0x0880: // GET_CONFIGURATION
239 reply_buffer[0] = usb_configuration;
243 case 0x0080: // GET_STATUS (device)
249 case 0x0082: // GET_STATUS (endpoint)
250 if (setup.wIndex > NUM_ENDPOINTS) {
251 // TODO: do we need to handle IN vs OUT here?
257 if (*(uint8_t *)(&USB0_ENDPT0 + setup.wIndex * 4) & 0x02) reply_buffer[0] = 1;
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?
268 (*(uint8_t *)(&USB0_ENDPT0 + setup.wIndex * 4)) &= ~0x02;
269 // TODO: do we need to clear the data toggle here?
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?
278 (*(uint8_t *)(&USB0_ENDPT0 + setup.wIndex * 4)) |= 0x02;
279 // TODO: do we need to clear the data toggle here?
281 case 0x0680: // GET_DESCRIPTOR
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) {
292 if ((setup.wValue >> 8) == 3) {
293 // for string descriptors, use the descriptor's
294 // length field, allowing runtime configured
296 datalen = *(list->addr);
298 datalen = list->length;
301 serial_print("Desc found, ");
302 serial_phex32((uint32_t)data);
304 serial_phex16(datalen);
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]);
317 //serial_print("desc: not found\n");
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");
325 case 0x2021: // CDC_SET_LINE_CODING
326 //serial_print("set coding, waiting...\n");
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");
335 case 0x0A21: // HID SET_IDLE
344 //serial_print("setup send ");
345 //serial_phex32(data);
347 //serial_phex16(datalen);
348 //serial_print("\n");
350 if (datalen > setup.wLength) datalen = setup.wLength;
352 if (size > EP0_SIZE) size = EP0_SIZE;
353 endpoint0_transmit(data, size);
356 if (datalen == 0 && size < EP0_SIZE) return;
359 if (size > EP0_SIZE) size = EP0_SIZE;
360 endpoint0_transmit(data, size);
363 if (datalen == 0 && size < EP0_SIZE) return;
366 ep0_tx_len = datalen;
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).
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.
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.
386 // #define stat2bufferdescriptor(stat) (table + ((stat) >> 2))
388 static void usb_control(uint32_t stat)
395 b = stat2bufferdescriptor(stat);
396 pid = BDT_PID(b->desc);
397 //count = b->desc >> 16;
399 //serial_print("pid:");
401 //serial_print(", count:");
402 //serial_phex(count);
403 //serial_print("\n");
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);
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);
418 // clear any leftover pending IN transactions
420 if (ep0_tx_data_toggle) {
422 //if (table[index(0, TX, EVEN)].desc & 0x80) {
423 //serial_print("leftover tx even\n");
425 //if (table[index(0, TX, ODD)].desc & 0x80) {
426 //serial_print("leftover tx odd\n");
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;
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);
446 // actually "do" the setup request
448 // unfreeze the USB, now that we're ready
449 USB0_CTL = USB_CTL_USBENSOFEN; // clear TXSUSPENDTOKENBUSY bit
451 case 0x01: // OUT transaction received from host
453 //serial_print("PID=OUT\n");
454 #ifdef CDC_STATUS_INTERFACE
455 if (setup.wRequestAndType == 0x2021 /*CDC_SET_LINE_CODING*/) {
457 uint8_t *dst = (uint8_t *)usb_cdc_line_coding;
458 //serial_print("set line coding ");
459 for (i=0; i<7; i++) {
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);
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);
475 // give the buffer back
476 b->desc = BDT_DESC(EP0_SIZE, DATA1);
479 case 0x09: // IN transaction completed to host
480 //serial_print("PID=IN:");
482 //serial_print("\n");
484 // send remaining data, if any...
488 if (size > EP0_SIZE) size = EP0_SIZE;
489 endpoint0_transmit(data, size);
492 ep0_tx_ptr = (ep0_tx_len > 0 || size == EP0_SIZE) ? data : NULL;
495 if (setup.bRequest == 5 && setup.bmRequestType == 0) {
497 //serial_print("set address: ");
498 //serial_phex16(setup.wValue);
499 //serial_print("\n");
500 USB0_ADDR = setup.wValue;
505 //serial_print("PID=unknown:");
507 //serial_print("\n");
509 USB0_CTL = USB_CTL_USBENSOFEN; // clear TXSUSPENDTOKENBUSY bit
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];
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
528 usb_packet_t *usb_rx(uint32_t endpoint)
532 if (endpoint >= NUM_ENDPOINTS) return NULL;
534 ret = rx_first[endpoint];
535 if (ret) rx_first[endpoint] = ret->next;
536 usb_rx_byte_count_data[endpoint] -= ret->len;
538 //serial_print("rx, epidx=");
539 //serial_phex(endpoint);
540 //serial_print(", packet=");
541 //serial_phex32(ret);
542 //serial_print("\n");
546 static uint32_t usb_queue_byte_count(const usb_packet_t *p)
551 for ( ; p; p = p->next) {
558 uint32_t usb_tx_byte_count(uint32_t endpoint)
561 if (endpoint >= NUM_ENDPOINTS) return 0;
562 return usb_queue_byte_count(tx_first[endpoint]);
565 uint32_t usb_tx_packet_count(uint32_t endpoint)
567 const usb_packet_t *p;
571 if (endpoint >= NUM_ENDPOINTS) return 0;
573 for (p = tx_first[endpoint]; p; p = p->next) count++;
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.
587 void usb_rx_memory(usb_packet_t *packet)
592 cfg = usb_endpoint_config_table;
593 //serial_print("rx_mem:");
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--;
603 //serial_print(",even\n");
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--;
612 //serial_print(",odd\n");
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;
626 //#define index(endpoint, tx, odd) (((endpoint) << 2) | ((tx) << 1) | (odd))
627 //#define stat2bufferdescriptor(stat) (table + ((stat) >> 2))
629 void usb_tx(uint32_t endpoint, usb_packet_t *packet)
631 bdt_t *b = &table[index(endpoint, TX, EVEN)];
635 if (endpoint >= NUM_ENDPOINTS) return;
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;
644 case TX_STATE_BOTH_FREE_ODD_FIRST:
646 next = TX_STATE_EVEN_FREE;
648 case TX_STATE_EVEN_FREE:
649 next = TX_STATE_NONE_FREE_ODD_FIRST;
651 case TX_STATE_ODD_FREE:
653 next = TX_STATE_NONE_FREE_EVEN_FIRST;
656 if (tx_first[endpoint] == NULL) {
657 tx_first[endpoint] = packet;
659 tx_last[endpoint]->next = packet;
661 tx_last[endpoint] = packet;
665 tx_state[endpoint] = next;
666 b->addr = packet->buf;
667 b->desc = BDT_DESC(packet->len, ((uint32_t)b & 8) ? DATA1 : DATA0);
673 void usb_device_reload()
675 asm volatile("bkpt");
679 void _reboot_Teensyduino_(void)
681 // TODO: initialize R0 with a code....
682 asm volatile("bkpt");
689 uint8_t status, stat, t;
691 //serial_print("isr");
692 //status = USB0_ISTAT;
693 //serial_phex(status);
694 //serial_print("\n");
698 if ((status & USB_INTEN_SOFTOKEN /* 04 */ )) {
699 if (usb_configuration) {
700 t = usb_reboot_timer;
702 usb_reboot_timer = --t;
703 if (!t) _reboot_Teensyduino_();
705 #ifdef CDC_DATA_INTERFACE
706 t = usb_cdc_transmit_flush_timer;
708 usb_cdc_transmit_flush_timer = --t;
709 if (t == 0) usb_serial_flush_callback();
713 USB0_ISTAT = USB_INTEN_SOFTOKEN;
716 if ((status & USB_ISTAT_TOKDNE /* 08 */ )) {
719 //serial_print("token: ep=");
720 //serial_phex(stat >> 4);
721 //serial_print(stat & 0x08 ? ",tx" : ",rx");
722 //serial_print(stat & 0x04 ? ",odd\n" : ",even\n");
723 endpoint = stat >> 4;
727 bdt_t *b = stat2bufferdescriptor(stat);
728 usb_packet_t *packet = (usb_packet_t *)((uint8_t *)(b->addr) - 8);
731 serial_phex(endpoint);
732 serial_print(", pid:");
733 serial_phex(BDT_PID(b->desc));
734 serial_print(((uint32_t)b & 8) ? ", odd" : ", even");
735 serial_print(", count:");
736 serial_phex(b->desc >> 16);
739 endpoint--; // endpoint is index to zero-based arrays
741 if (stat & 0x08) { // transmit
743 packet = tx_first[endpoint];
745 //serial_print("tx packet\n");
746 tx_first[endpoint] = packet->next;
747 b->addr = packet->buf;
748 switch (tx_state[endpoint]) {
749 case TX_STATE_BOTH_FREE_EVEN_FIRST:
750 tx_state[endpoint] = TX_STATE_ODD_FREE;
752 case TX_STATE_BOTH_FREE_ODD_FIRST:
753 tx_state[endpoint] = TX_STATE_EVEN_FREE;
755 case TX_STATE_EVEN_FREE:
756 tx_state[endpoint] = TX_STATE_NONE_FREE_ODD_FIRST;
758 case TX_STATE_ODD_FREE:
759 tx_state[endpoint] = TX_STATE_NONE_FREE_EVEN_FIRST;
764 b->desc = BDT_DESC(packet->len, ((uint32_t)b & 8) ? DATA1 : DATA0);
766 //serial_print("tx no packet\n");
767 switch (tx_state[endpoint]) {
768 case TX_STATE_BOTH_FREE_EVEN_FIRST:
769 case TX_STATE_BOTH_FREE_ODD_FIRST:
771 case TX_STATE_EVEN_FREE:
772 tx_state[endpoint] = TX_STATE_BOTH_FREE_EVEN_FIRST;
774 case TX_STATE_ODD_FREE:
775 tx_state[endpoint] = TX_STATE_BOTH_FREE_ODD_FIRST;
778 tx_state[endpoint] = ((uint32_t)b & 8) ?
779 TX_STATE_ODD_FREE : TX_STATE_EVEN_FREE;
784 packet->len = b->desc >> 16;
785 if (packet->len > 0) {
788 if (rx_first[endpoint] == NULL) {
789 //serial_print("rx 1st, epidx=");
790 //serial_phex(endpoint);
791 //serial_print(", packet=");
792 //serial_phex32((uint32_t)packet);
793 //serial_print("\n");
794 rx_first[endpoint] = packet;
796 //serial_print("rx Nth, epidx=");
797 //serial_phex(endpoint);
798 //serial_print(", packet=");
799 //serial_phex32((uint32_t)packet);
800 //serial_print("\n");
801 rx_last[endpoint]->next = packet;
803 rx_last[endpoint] = packet;
804 usb_rx_byte_count_data[endpoint] += packet->len;
805 // TODO: implement a per-endpoint maximum # of allocated packets
806 // so a flood of incoming data on 1 endpoint doesn't starve
807 // the others if the user isn't reading it regularly
808 packet = usb_malloc();
810 b->addr = packet->buf;
811 b->desc = BDT_DESC(64, ((uint32_t)b & 8) ? DATA1 : DATA0);
813 //serial_print("starving ");
814 //serial_phex(endpoint + 1);
815 //serial_print(((uint32_t)b & 8) ? ",odd\n" : ",even\n");
817 usb_rx_memory_needed++;
820 b->desc = BDT_DESC(64, ((uint32_t)b & 8) ? DATA1 : DATA0);
828 USB0_ISTAT = USB_ISTAT_TOKDNE;
834 if (status & USB_ISTAT_USBRST /* 01 */ ) {
835 //serial_print("reset\n");
837 // initialize BDT toggle bits
838 USB0_CTL = USB_CTL_ODDRST;
841 // set up buffers to receive Setup and OUT packets
842 table[index(0, RX, EVEN)].desc = BDT_DESC(EP0_SIZE, 0);
843 table[index(0, RX, EVEN)].addr = ep0_rx0_buf;
844 table[index(0, RX, ODD)].desc = BDT_DESC(EP0_SIZE, 0);
845 table[index(0, RX, ODD)].addr = ep0_rx1_buf;
846 table[index(0, TX, EVEN)].desc = 0;
847 table[index(0, TX, ODD)].desc = 0;
849 // activate endpoint 0
850 USB0_ENDPT0 = USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK;
852 // clear all ending interrupts
856 // set the address to zero during enumeration
859 // enable other interrupts
861 USB0_INTEN = USB_INTEN_TOKDNEEN |
868 // is this necessary?
869 USB0_CTL = USB_CTL_USBENSOFEN;
874 if ((status & USB_ISTAT_STALL /* 80 */ )) {
875 //serial_print("stall:\n");
876 USB0_ENDPT0 = USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK;
877 USB0_ISTAT = USB_ISTAT_STALL;
879 if ((status & USB_ISTAT_ERROR /* 02 */ )) {
880 uint8_t err = USB0_ERRSTAT;
882 //serial_print("err:");
884 //serial_print("\n");
885 USB0_ISTAT = USB_ISTAT_ERROR;
888 if ((status & USB_ISTAT_SLEEP /* 10 */ )) {
889 //serial_print("sleep\n");
890 USB0_ISTAT = USB_ISTAT_SLEEP;
901 //serial_begin(BAUD2DIV(115200));
902 //serial_print("usb_init\n");
904 //usb_init_serialnumber();
906 for (i=0; i <= NUM_ENDPOINTS*4; i++) {
911 // this basically follows the flowchart in the Kinetis
912 // Quick Reference User Guide, Rev. 1, 03/2012, page 141
914 // assume 48 MHz clock already running
915 // SIM - enable clock
916 SIM_SCGC4 |= SIM_SCGC4_USBOTG;
919 USB0_USBTRC0 = USB_USBTRC_USBRESET;
920 while ((USB0_USBTRC0 & USB_USBTRC_USBRESET) != 0) ; // wait for reset to end
922 // set desc table base addr
923 USB0_BDTPAGE1 = ((uint32_t)table) >> 8;
924 USB0_BDTPAGE2 = ((uint32_t)table) >> 16;
925 USB0_BDTPAGE3 = ((uint32_t)table) >> 24;
927 // clear all ISR flags
930 USB0_OTGISTAT = 0xFF;
932 USB0_USBTRC0 |= 0x40; // undocumented bit
935 USB0_CTL = USB_CTL_USBENSOFEN;
938 // enable reset interrupt
939 USB0_INTEN = USB_INTEN_USBRSTEN;
941 // enable interrupt in NVIC...
942 NVIC_SET_PRIORITY(IRQ_USBOTG, 112);
943 NVIC_ENABLE_IRQ(IRQ_USBOTG);
946 USB0_CONTROL = USB_CONTROL_DPPULLUPNONOTG;
949 // return 0 if the USB is not configured, or the configuration
950 // number selected by the HOST
951 uint8_t usb_configured(void)
953 return usb_configuration;