1 /* Teensyduino Core Library
2 * http://www.pjrc.com/teensy/
3 * Copyright (c) 2013 PJRC.COM, LLC.
4 * Modified by Jacob Alexander 2013-2014
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * 1. The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * 2. If the Software is incorporated into a build system that allows
18 * selection among a list of target devices, then similar target
19 * devices manufactured by PJRC.COM must be included in the list of
20 * target devices and selectable in the same manner.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
26 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
28 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <Lib/OutputLib.h>
40 // buffer descriptor table
47 __attribute__ ((section(".usbdescriptortable"), used))
48 static bdt_t table[(NUM_ENDPOINTS+1)*4];
50 static usb_packet_t *rx_first[NUM_ENDPOINTS];
51 static usb_packet_t *rx_last[NUM_ENDPOINTS];
52 static usb_packet_t *tx_first[NUM_ENDPOINTS];
53 static usb_packet_t *tx_last[NUM_ENDPOINTS];
54 uint16_t usb_rx_byte_count_data[NUM_ENDPOINTS];
56 static uint8_t tx_state[NUM_ENDPOINTS];
57 #define TX_STATE_BOTH_FREE_EVEN_FIRST 0
58 #define TX_STATE_BOTH_FREE_ODD_FIRST 1
59 #define TX_STATE_EVEN_FREE 2
60 #define TX_STATE_ODD_FREE 3
61 #define TX_STATE_NONE_FREE_EVEN_FIRST 4
62 #define TX_STATE_NONE_FREE_ODD_FIRST 5
65 #define BDT_DATA1 0x40
66 #define BDT_DATA0 0x00
68 #define BDT_STALL 0x04
69 #define BDT_PID(n) (((n) >> 2) & 15)
71 #define BDT_DESC(count, data) (BDT_OWN | BDT_DTS \
72 | ((data) ? BDT_DATA1 : BDT_DATA0) \
81 #define index(endpoint, tx, odd) (((endpoint) << 2) | ((tx) << 1) | (odd))
82 #define stat2bufferdescriptor(stat) (table + ((stat) >> 2))
89 uint8_t bmRequestType;
92 uint16_t wRequestAndType;
106 #define CLEAR_FEATURE 1
107 #define SET_FEATURE 3
108 #define SET_ADDRESS 5
109 #define GET_DESCRIPTOR 6
110 #define SET_DESCRIPTOR 7
111 #define GET_CONFIGURATION 8
112 #define SET_CONFIGURATION 9
113 #define GET_INTERFACE 10
114 #define SET_INTERFACE 11
115 #define SYNCH_FRAME 12
117 // SETUP always uses a DATA0 PID for the data field of the SETUP transaction.
118 // transactions in the data phase start with DATA1 and toggle (figure 8-12, USB1.1)
119 // Status stage uses a DATA1 PID.
121 static uint8_t ep0_rx0_buf[EP0_SIZE] __attribute__ ((aligned (4)));
122 static uint8_t ep0_rx1_buf[EP0_SIZE] __attribute__ ((aligned (4)));
123 static const uint8_t *ep0_tx_ptr = NULL;
124 static uint16_t ep0_tx_len;
125 static uint8_t ep0_tx_bdt_bank = 0;
126 static uint8_t ep0_tx_data_toggle = 0;
127 uint8_t usb_rx_memory_needed = 0;
129 volatile uint8_t usb_configuration = 0;
130 volatile uint8_t usb_reboot_timer = 0;
133 static void endpoint0_stall()
136 USB0_ENDPT0 = USB_ENDPT_EPSTALL | USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK;
140 static void endpoint0_transmit(const void *data, uint32_t len)
144 serial_print("tx0:");
145 serial_phex32((uint32_t)data);
148 serial_print(ep0_tx_bdt_bank ? ", odd" : ", even");
149 serial_print(ep0_tx_data_toggle ? ", d1\n" : ", d0\n");
151 table[index(0, TX, ep0_tx_bdt_bank)].addr = (void *)data;
152 table[index(0, TX, ep0_tx_bdt_bank)].desc = BDT_DESC(len, ep0_tx_data_toggle);
153 ep0_tx_data_toggle ^= 1;
154 ep0_tx_bdt_bank ^= 1;
157 static uint8_t reply_buffer[8];
159 static void usb_setup()
162 const uint8_t *data = NULL;
163 uint32_t datalen = 0;
164 const usb_descriptor_list_t *list;
166 volatile uint8_t *reg;
171 switch (setup.wRequestAndType) {
172 case 0x0500: // SET_ADDRESS
174 case 0x0900: // SET_CONFIGURATION
175 //serial_print("configure\n");
176 usb_configuration = setup.wValue;
178 cfg = usb_endpoint_config_table;
179 // clear all BDT entries, free any allocated memory...
180 for (i=4; i < (NUM_ENDPOINTS+1)*4; i++) {
181 if (table[i].desc & BDT_OWN) {
182 usb_free((usb_packet_t *)((uint8_t *)(table[i].addr) - 8));
185 // free all queued packets
186 for (i=0; i < NUM_ENDPOINTS; i++) {
204 usb_rx_byte_count_data[i] = 0;
205 switch (tx_state[i]) {
206 case TX_STATE_EVEN_FREE:
207 case TX_STATE_NONE_FREE_EVEN_FIRST:
208 tx_state[i] = TX_STATE_BOTH_FREE_EVEN_FIRST;
210 case TX_STATE_ODD_FREE:
211 case TX_STATE_NONE_FREE_ODD_FIRST:
212 tx_state[i] = TX_STATE_BOTH_FREE_ODD_FIRST;
218 usb_rx_memory_needed = 0;
219 for (i=1; i <= NUM_ENDPOINTS; i++) {
223 if (epconf & USB_ENDPT_EPRXEN) {
227 table[index(i, RX, EVEN)].addr = p->buf;
228 table[index(i, RX, EVEN)].desc = BDT_DESC(64, 0);
230 table[index(i, RX, EVEN)].desc = 0;
231 usb_rx_memory_needed++;
235 table[index(i, RX, ODD)].addr = p->buf;
236 table[index(i, RX, ODD)].desc = BDT_DESC(64, 1);
238 table[index(i, RX, ODD)].desc = 0;
239 usb_rx_memory_needed++;
242 table[index(i, TX, EVEN)].desc = 0;
243 table[index(i, TX, ODD)].desc = 0;
246 case 0x0880: // GET_CONFIGURATION
247 reply_buffer[0] = usb_configuration;
251 case 0x0080: // GET_STATUS (device)
257 case 0x0082: // GET_STATUS (endpoint)
258 if (setup.wIndex > NUM_ENDPOINTS) {
259 // TODO: do we need to handle IN vs OUT here?
265 if (*(uint8_t *)(&USB0_ENDPT0 + setup.wIndex * 4) & 0x02) reply_buffer[0] = 1;
269 case 0x0102: // CLEAR_FEATURE (endpoint)
270 i = setup.wIndex & 0x7F;
271 if (i > NUM_ENDPOINTS || setup.wValue != 0) {
272 // TODO: do we need to handle IN vs OUT here?
276 (*(uint8_t *)(&USB0_ENDPT0 + setup.wIndex * 4)) &= ~0x02;
277 // TODO: do we need to clear the data toggle here?
279 case 0x0302: // SET_FEATURE (endpoint)
280 i = setup.wIndex & 0x7F;
281 if (i > NUM_ENDPOINTS || setup.wValue != 0) {
282 // TODO: do we need to handle IN vs OUT here?
286 (*(uint8_t *)(&USB0_ENDPT0 + setup.wIndex * 4)) |= 0x02;
287 // TODO: do we need to clear the data toggle here?
289 case 0x0680: // GET_DESCRIPTOR
291 //serial_print("desc:");
292 //serial_phex16(setup.wValue);
293 //serial_print("\n");
294 for (list = usb_descriptor_list; 1; list++) {
295 if (list->addr == NULL) break;
296 //if (setup.wValue == list->wValue &&
297 //(setup.wIndex == list->wIndex) || ((setup.wValue >> 8) == 3)) {
298 if (setup.wValue == list->wValue && setup.wIndex == list->wIndex) {
300 if ((setup.wValue >> 8) == 3) {
301 // for string descriptors, use the descriptor's
302 // length field, allowing runtime configured
304 datalen = *(list->addr);
306 datalen = list->length;
309 serial_print("Desc found, ");
310 serial_phex32((uint32_t)data);
312 serial_phex16(datalen);
314 serial_phex(data[0]);
315 serial_phex(data[1]);
316 serial_phex(data[2]);
317 serial_phex(data[3]);
318 serial_phex(data[4]);
319 serial_phex(data[5]);
325 //serial_print("desc: not found\n");
328 #if defined(CDC_STATUS_INTERFACE)
329 case 0x2221: // CDC_SET_CONTROL_LINE_STATE
330 usb_cdc_line_rtsdtr = setup.wValue;
331 //serial_print("set control line state\n");
333 case 0x2021: // CDC_SET_LINE_CODING
334 //serial_print("set coding, waiting...\n");
338 // TODO: this does not work... why?
339 #if defined(SEREMU_INTERFACE) || defined(KEYBOARD_INTERFACE)
340 case 0x0921: // HID SET_REPORT
341 //serial_print(":)\n");
343 case 0x0A21: // HID SET_IDLE
352 //serial_print("setup send ");
353 //serial_phex32(data);
355 //serial_phex16(datalen);
356 //serial_print("\n");
358 if (datalen > setup.wLength) datalen = setup.wLength;
360 if (size > EP0_SIZE) size = EP0_SIZE;
361 endpoint0_transmit(data, size);
364 if (datalen == 0 && size < EP0_SIZE) return;
367 if (size > EP0_SIZE) size = EP0_SIZE;
368 endpoint0_transmit(data, size);
371 if (datalen == 0 && size < EP0_SIZE) return;
374 ep0_tx_len = datalen;
379 //A bulk endpoint's toggle sequence is initialized to DATA0 when the endpoint
380 //experiences any configuration event (configuration events are explained in
381 //Sections 9.1.1.5 and 9.4.5).
383 //Configuring a device or changing an alternate setting causes all of the status
384 //and configuration values associated with endpoints in the affected interfaces
385 //to be set to their default values. This includes setting the data toggle of
386 //any endpoint using data toggles to the value DATA0.
388 //For endpoints using data toggle, regardless of whether an endpoint has the
389 //Halt feature set, a ClearFeature(ENDPOINT_HALT) request always results in the
390 //data toggle being reinitialized to DATA0.
394 // #define stat2bufferdescriptor(stat) (table + ((stat) >> 2))
396 static void usb_control(uint32_t stat)
404 b = stat2bufferdescriptor(stat);
405 pid = BDT_PID(b->desc);
406 //count = b->desc >> 16;
408 //serial_print("pid:");
410 //serial_print(", count:");
411 //serial_phex(count);
412 //serial_print("\n");
415 case 0x0D: // Setup received from host
416 //serial_print("PID=Setup\n");
417 //if (count != 8) ; // panic?
418 // grab the 8 byte setup info
419 setup.word1 = *(uint32_t *)(buf);
420 setup.word2 = *(uint32_t *)(buf + 4);
422 // give the buffer back
423 b->desc = BDT_DESC(EP0_SIZE, DATA1);
424 //table[index(0, RX, EVEN)].desc = BDT_DESC(EP0_SIZE, 1);
425 //table[index(0, RX, ODD)].desc = BDT_DESC(EP0_SIZE, 1);
427 // clear any leftover pending IN transactions
429 if (ep0_tx_data_toggle) {
431 //if (table[index(0, TX, EVEN)].desc & 0x80) {
432 //serial_print("leftover tx even\n");
434 //if (table[index(0, TX, ODD)].desc & 0x80) {
435 //serial_print("leftover tx odd\n");
437 table[index(0, TX, EVEN)].desc = 0;
438 table[index(0, TX, ODD)].desc = 0;
439 // first IN after Setup is always DATA1
440 ep0_tx_data_toggle = 1;
443 serial_print("bmRequestType:");
444 serial_phex(setup.bmRequestType);
445 serial_print(", bRequest:");
446 serial_phex(setup.bRequest);
447 serial_print(", wValue:");
448 serial_phex16(setup.wValue);
449 serial_print(", wIndex:");
450 serial_phex16(setup.wIndex);
451 serial_print(", len:");
452 serial_phex16(setup.wLength);
455 // actually "do" the setup request
457 // unfreeze the USB, now that we're ready
458 USB0_CTL = USB_CTL_USBENSOFEN; // clear TXSUSPENDTOKENBUSY bit
460 case 0x01: // OUT transaction received from host
462 //serial_print("PID=OUT\n");
463 #ifdef CDC_STATUS_INTERFACE
464 if (setup.wRequestAndType == 0x2021 /*CDC_SET_LINE_CODING*/) {
466 uint8_t *dst = (uint8_t *)usb_cdc_line_coding;
467 //serial_print("set line coding ");
468 for (i=0; i<7; i++) {
472 //serial_phex32(usb_cdc_line_coding[0]);
473 //serial_print("\n");
474 if (usb_cdc_line_coding[0] == 134) usb_reboot_timer = 15;
475 endpoint0_transmit(NULL, 0);
478 #ifdef KEYBOARD_INTERFACE
479 if (setup.word1 == 0x02000921 && setup.word2 == ((1<<16)|KEYBOARD_INTERFACE)) {
480 USBKeys_LEDs = buf[0];
481 endpoint0_transmit(NULL, 0);
484 // give the buffer back
485 b->desc = BDT_DESC(EP0_SIZE, DATA1);
488 case 0x09: // IN transaction completed to host
489 //serial_print("PID=IN:");
491 //serial_print("\n");
493 // send remaining data, if any...
497 if (size > EP0_SIZE) size = EP0_SIZE;
498 endpoint0_transmit(data, size);
501 ep0_tx_ptr = (ep0_tx_len > 0 || size == EP0_SIZE) ? data : NULL;
504 if (setup.bRequest == 5 && setup.bmRequestType == 0) {
506 //serial_print("set address: ");
507 //serial_phex16(setup.wValue);
508 //serial_print("\n");
509 USB0_ADDR = setup.wValue;
514 //serial_print("PID=unknown:");
516 //serial_print("\n");
518 USB0_CTL = USB_CTL_USBENSOFEN; // clear TXSUSPENDTOKENBUSY bit
523 static usb_packet_t *rx_first[NUM_ENDPOINTS];
524 static usb_packet_t *rx_last[NUM_ENDPOINTS];
525 static usb_packet_t *tx_first[NUM_ENDPOINTS];
526 static usb_packet_t *tx_last[NUM_ENDPOINTS];
528 static uint8_t tx_state[NUM_ENDPOINTS];
529 #define TX_STATE_BOTH_FREE_EVEN_FIRST 0
530 #define TX_STATE_BOTH_FREE_ODD_FIRST 1
531 #define TX_STATE_EVEN_FREE 2
532 #define TX_STATE_ODD_FREE 3
533 #define TX_STATE_NONE_FREE 4
537 usb_packet_t *usb_rx(uint32_t endpoint)
542 if (endpoint >= NUM_ENDPOINTS) return NULL;
544 ret = rx_first[endpoint];
545 if (ret) rx_first[endpoint] = ret->next;
546 usb_rx_byte_count_data[endpoint] -= ret->len;
548 //serial_print("rx, epidx=");
549 //serial_phex(endpoint);
550 //serial_print(", packet=");
551 //serial_phex32(ret);
552 //serial_print("\n");
556 static uint32_t usb_queue_byte_count(const usb_packet_t *p)
561 for ( ; p; p = p->next) {
568 uint32_t usb_tx_byte_count(uint32_t endpoint)
571 if (endpoint >= NUM_ENDPOINTS) return 0;
572 return usb_queue_byte_count(tx_first[endpoint]);
575 uint32_t usb_tx_packet_count(uint32_t endpoint)
577 const usb_packet_t *p;
581 if (endpoint >= NUM_ENDPOINTS) return 0;
583 for (p = tx_first[endpoint]; p; p = p->next) count++;
589 // Called from usb_free, but only when usb_rx_memory_needed > 0, indicating
590 // receive endpoints are starving for memory. The intention is to give
591 // endpoints needing receive memory priority over the user's code, which is
592 // likely calling usb_malloc to obtain memory for transmitting. When the
593 // user is creating data very quickly, their consumption could starve reception
594 // without this prioritization. The packet buffer (input) is assigned to the
595 // first endpoint needing memory.
597 void usb_rx_memory(usb_packet_t *packet)
599 //print("USB RX MEMORY");
603 cfg = usb_endpoint_config_table;
604 //serial_print("rx_mem:");
606 for (i=1; i <= NUM_ENDPOINTS; i++) {
607 if (*cfg++ & USB_ENDPT_EPRXEN) {
608 if (table[index(i, RX, EVEN)].desc == 0) {
609 table[index(i, RX, EVEN)].addr = packet->buf;
610 table[index(i, RX, EVEN)].desc = BDT_DESC(64, 0);
611 usb_rx_memory_needed--;
614 //serial_print(",even\n");
617 if (table[index(i, RX, ODD)].desc == 0) {
618 table[index(i, RX, ODD)].addr = packet->buf;
619 table[index(i, RX, ODD)].desc = BDT_DESC(64, 1);
620 usb_rx_memory_needed--;
623 //serial_print(",odd\n");
629 // we should never reach this point. If we get here, it means
630 // usb_rx_memory_needed was set greater than zero, but no memory
631 // was actually needed.
632 usb_rx_memory_needed = 0;
637 //#define index(endpoint, tx, odd) (((endpoint) << 2) | ((tx) << 1) | (odd))
638 //#define stat2bufferdescriptor(stat) (table + ((stat) >> 2))
640 void usb_tx(uint32_t endpoint, usb_packet_t *packet)
642 bdt_t *b = &table[index(endpoint, TX, EVEN)];
646 if (endpoint >= NUM_ENDPOINTS) return;
648 //serial_print("txstate=");
649 //serial_phex(tx_state[endpoint]);
650 //serial_print("\n");
651 switch (tx_state[endpoint]) {
652 case TX_STATE_BOTH_FREE_EVEN_FIRST:
653 next = TX_STATE_ODD_FREE;
655 case TX_STATE_BOTH_FREE_ODD_FIRST:
657 next = TX_STATE_EVEN_FREE;
659 case TX_STATE_EVEN_FREE:
660 next = TX_STATE_NONE_FREE_ODD_FIRST;
662 case TX_STATE_ODD_FREE:
664 next = TX_STATE_NONE_FREE_EVEN_FIRST;
667 if (tx_first[endpoint] == NULL) {
668 tx_first[endpoint] = packet;
670 tx_last[endpoint]->next = packet;
672 tx_last[endpoint] = packet;
676 tx_state[endpoint] = next;
677 b->addr = packet->buf;
678 b->desc = BDT_DESC(packet->len, ((uint32_t)b & 8) ? DATA1 : DATA0);
683 void usb_device_reload()
686 #if defined(_mk20dx128vlf5_)
688 // MCHCK Kiibohd Variant
689 // Check to see if PTA3 (has a pull-up) is connected to GND (usually via jumper)
690 // Only allow reload if the jumper is present (security)
691 GPIOA_PDDR &= ~(1<<3); // Input
692 PORTA_PCR3 = PORT_PCR_PFE | PORT_PCR_MUX(1); // Internal pull-up
695 if ( GPIOA_PDIR & (1<<3) )
698 warn_print("Security jumper not present, cancelling firmware reload...");
699 info_msg("Replace jumper on middle 2 pins, or manually press the firmware reload button.");
703 // Copies variable into the VBAT register, must be identical to the variable in the bootloader to jump to the bootloader flash mode
704 for ( int pos = 0; pos < sizeof(sys_reset_to_loader_magic); pos++ )(&VBAT)[pos] = sys_reset_to_loader_magic[ pos ];
708 // Teensy 3.0 and 3.1
710 asm volatile("bkpt");
717 uint8_t status, stat, t;
719 //serial_print("isr");
720 //status = USB0_ISTAT;
721 //serial_phex(status);
722 //serial_print("\n");
726 print("USB ISR STATUS: ");
731 if ((status & USB_INTEN_SOFTOKEN /* 04 */ )) {
732 if (usb_configuration) {
733 t = usb_reboot_timer;
735 usb_reboot_timer = --t;
736 if (!t) usb_device_reload();
738 #ifdef CDC_DATA_INTERFACE
739 t = usb_cdc_transmit_flush_timer;
741 usb_cdc_transmit_flush_timer = --t;
742 if (t == 0) usb_serial_flush_callback();
746 USB0_ISTAT = USB_INTEN_SOFTOKEN;
749 if ((status & USB_ISTAT_TOKDNE /* 08 */ )) {
752 //serial_print("token: ep=");
753 //serial_phex(stat >> 4);
754 //serial_print(stat & 0x08 ? ",tx" : ",rx");
755 //serial_print(stat & 0x04 ? ",odd\n" : ",even\n");
756 endpoint = stat >> 4;
760 bdt_t *b = stat2bufferdescriptor(stat);
761 usb_packet_t *packet = (usb_packet_t *)((uint8_t *)(b->addr) - 8);
764 serial_phex(endpoint);
765 serial_print(", pid:");
766 serial_phex(BDT_PID(b->desc));
767 serial_print(((uint32_t)b & 8) ? ", odd" : ", even");
768 serial_print(", count:");
769 serial_phex(b->desc >> 16);
772 endpoint--; // endpoint is index to zero-based arrays
774 if (stat & 0x08) { // transmit
776 packet = tx_first[endpoint];
778 //serial_print("tx packet\n");
779 tx_first[endpoint] = packet->next;
780 b->addr = packet->buf;
781 switch (tx_state[endpoint]) {
782 case TX_STATE_BOTH_FREE_EVEN_FIRST:
783 tx_state[endpoint] = TX_STATE_ODD_FREE;
785 case TX_STATE_BOTH_FREE_ODD_FIRST:
786 tx_state[endpoint] = TX_STATE_EVEN_FREE;
788 case TX_STATE_EVEN_FREE:
789 tx_state[endpoint] = TX_STATE_NONE_FREE_ODD_FIRST;
791 case TX_STATE_ODD_FREE:
792 tx_state[endpoint] = TX_STATE_NONE_FREE_EVEN_FIRST;
797 b->desc = BDT_DESC(packet->len, ((uint32_t)b & 8) ? DATA1 : DATA0);
799 //serial_print("tx no packet\n");
800 switch (tx_state[endpoint]) {
801 case TX_STATE_BOTH_FREE_EVEN_FIRST:
802 case TX_STATE_BOTH_FREE_ODD_FIRST:
804 case TX_STATE_EVEN_FREE:
805 tx_state[endpoint] = TX_STATE_BOTH_FREE_EVEN_FIRST;
807 case TX_STATE_ODD_FREE:
808 tx_state[endpoint] = TX_STATE_BOTH_FREE_ODD_FIRST;
811 tx_state[endpoint] = ((uint32_t)b & 8) ?
812 TX_STATE_ODD_FREE : TX_STATE_EVEN_FREE;
817 packet->len = b->desc >> 16;
818 if (packet->len > 0) {
821 if (rx_first[endpoint] == NULL) {
822 //serial_print("rx 1st, epidx=");
823 //serial_phex(endpoint);
824 //serial_print(", packet=");
825 //serial_phex32((uint32_t)packet);
826 //serial_print("\n");
827 rx_first[endpoint] = packet;
829 //serial_print("rx Nth, epidx=");
830 //serial_phex(endpoint);
831 //serial_print(", packet=");
832 //serial_phex32((uint32_t)packet);
833 //serial_print("\n");
834 rx_last[endpoint]->next = packet;
836 rx_last[endpoint] = packet;
837 usb_rx_byte_count_data[endpoint] += packet->len;
838 // TODO: implement a per-endpoint maximum # of allocated packets
839 // so a flood of incoming data on 1 endpoint doesn't starve
840 // the others if the user isn't reading it regularly
841 packet = usb_malloc();
843 b->addr = packet->buf;
844 b->desc = BDT_DESC(64, ((uint32_t)b & 8) ? DATA1 : DATA0);
846 //serial_print("starving ");
847 //serial_phex(endpoint + 1);
848 //serial_print(((uint32_t)b & 8) ? ",odd\n" : ",even\n");
850 usb_rx_memory_needed++;
853 b->desc = BDT_DESC(64, ((uint32_t)b & 8) ? DATA1 : DATA0);
861 USB0_ISTAT = USB_ISTAT_TOKDNE;
867 if (status & USB_ISTAT_USBRST /* 01 */ ) {
868 //serial_print("reset\n");
870 // initialize BDT toggle bits
871 USB0_CTL = USB_CTL_ODDRST;
874 // set up buffers to receive Setup and OUT packets
875 table[index(0, RX, EVEN)].desc = BDT_DESC(EP0_SIZE, 0);
876 table[index(0, RX, EVEN)].addr = ep0_rx0_buf;
877 table[index(0, RX, ODD)].desc = BDT_DESC(EP0_SIZE, 0);
878 table[index(0, RX, ODD)].addr = ep0_rx1_buf;
879 table[index(0, TX, EVEN)].desc = 0;
880 table[index(0, TX, ODD)].desc = 0;
882 // activate endpoint 0
883 USB0_ENDPT0 = USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK;
885 // clear all ending interrupts
889 // set the address to zero during enumeration
892 // enable other interrupts
894 USB0_INTEN = USB_INTEN_TOKDNEEN |
901 // is this necessary?
902 USB0_CTL = USB_CTL_USBENSOFEN;
907 if ((status & USB_ISTAT_STALL /* 80 */ )) {
908 //serial_print("stall:\n");
909 USB0_ENDPT0 = USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK;
910 USB0_ISTAT = USB_ISTAT_STALL;
912 if ((status & USB_ISTAT_ERROR /* 02 */ )) {
913 uint8_t err = USB0_ERRSTAT;
915 //serial_print("err:");
917 //serial_print("\n");
918 USB0_ISTAT = USB_ISTAT_ERROR;
921 if ((status & USB_ISTAT_SLEEP /* 10 */ )) {
922 //serial_print("sleep\n");
923 USB0_ISTAT = USB_ISTAT_SLEEP;
934 // Clear out endpoints table
935 for ( int i = 0; i <= NUM_ENDPOINTS * 4; i++ )
941 // this basically follows the flowchart in the Kinetis
942 // Quick Reference User Guide, Rev. 1, 03/2012, page 141
944 // assume 48 MHz clock already running
945 // SIM - enable clock
946 SIM_SCGC4 |= SIM_SCGC4_USBOTG;
949 USB0_USBTRC0 = USB_USBTRC_USBRESET;
950 while ( (USB0_USBTRC0 & USB_USBTRC_USBRESET) != 0 ); // wait for reset to end
952 // set desc table base addr
953 USB0_BDTPAGE1 = ((uint32_t)table) >> 8;
954 USB0_BDTPAGE2 = ((uint32_t)table) >> 16;
955 USB0_BDTPAGE3 = ((uint32_t)table) >> 24;
957 // clear all ISR flags
960 USB0_OTGISTAT = 0xFF;
962 USB0_USBTRC0 |= 0x40; // undocumented bit
965 USB0_CTL = USB_CTL_USBENSOFEN;
968 // enable reset interrupt
969 USB0_INTEN = USB_INTEN_USBRSTEN;
971 // enable interrupt in NVIC...
972 NVIC_SET_PRIORITY( IRQ_USBOTG, 112 );
973 NVIC_ENABLE_IRQ( IRQ_USBOTG );
976 USB0_CONTROL = USB_CONTROL_DPPULLUPNONOTG;
979 // return 0 if the USB is not configured, or the configuration
980 // number selected by the HOST
981 uint8_t usb_configured()
983 return usb_configuration;