1 /* Teensyduino Core Library
2 * http://www.pjrc.com/teensy/
3 * Copyright (c) 2013 PJRC.COM, LLC.
4 * Modifications 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
32 // ----- Includes -----
35 #include <Lib/OutputLib.h>
44 // ----- Defines -----
47 // XXX - Only use when using usbMuxUart Module
48 // Delay causes issues initializing more than 1 hid device (i.e. NKRO keyboard)
49 //#define UART_DEBUG 1
50 // Debug Unknown USB requests, usually what you want to debug USB issues
51 //#define UART_DEBUG_UNKNOWN 1
54 #define TX_STATE_BOTH_FREE_EVEN_FIRST 0
55 #define TX_STATE_BOTH_FREE_ODD_FIRST 1
56 #define TX_STATE_EVEN_FREE 2
57 #define TX_STATE_ODD_FREE 3
58 #define TX_STATE_NONE_FREE_EVEN_FIRST 4
59 #define TX_STATE_NONE_FREE_ODD_FIRST 5
62 #define BDT_DATA1 0x40
63 #define BDT_DATA0 0x00
65 #define BDT_STALL 0x04
76 #define CLEAR_FEATURE 1
79 #define GET_DESCRIPTOR 6
80 #define SET_DESCRIPTOR 7
81 #define GET_CONFIGURATION 8
82 #define SET_CONFIGURATION 9
83 #define GET_INTERFACE 10
84 #define SET_INTERFACE 11
85 #define SYNCH_FRAME 12
87 #define TX_STATE_BOTH_FREE_EVEN_FIRST 0
88 #define TX_STATE_BOTH_FREE_ODD_FIRST 1
89 #define TX_STATE_EVEN_FREE 2
90 #define TX_STATE_ODD_FREE 3
91 #define TX_STATE_NONE_FREE 4
99 #define BDT_PID(n) (((n) >> 2) & 15)
101 #define BDT_DESC(count, data) (BDT_OWN | BDT_DTS \
102 | ((data) ? BDT_DATA1 : BDT_DATA0) \
105 #define index(endpoint, tx, odd) (((endpoint) << 2) | ((tx) << 1) | (odd))
106 #define stat2bufferdescriptor(stat) (table + ((stat) >> 2))
110 // ----- Structs -----
112 // buffer descriptor table
123 uint8_t bmRequestType;
126 uint16_t wRequestAndType;
140 // ----- Variables -----
142 __attribute__ ((section(".usbdescriptortable"), used))
143 static bdt_t table[ (NUM_ENDPOINTS + 1) * 4 ];
145 static usb_packet_t *rx_first [ NUM_ENDPOINTS ];
146 static usb_packet_t *rx_last [ NUM_ENDPOINTS ];
147 static usb_packet_t *tx_first [ NUM_ENDPOINTS ];
148 static usb_packet_t *tx_last [ NUM_ENDPOINTS ];
149 uint16_t usb_rx_byte_count_data[ NUM_ENDPOINTS ];
151 static uint8_t tx_state[NUM_ENDPOINTS];
153 // SETUP always uses a DATA0 PID for the data field of the SETUP transaction.
154 // transactions in the data phase start with DATA1 and toggle (figure 8-12, USB1.1)
155 // Status stage uses a DATA1 PID.
157 static uint8_t ep0_rx0_buf[EP0_SIZE] __attribute__ ((aligned (4)));
158 static uint8_t ep0_rx1_buf[EP0_SIZE] __attribute__ ((aligned (4)));
159 static const uint8_t *ep0_tx_ptr = NULL;
160 static uint16_t ep0_tx_len;
161 static uint8_t ep0_tx_bdt_bank = 0;
162 static uint8_t ep0_tx_data_toggle = 0;
163 uint8_t usb_rx_memory_needed = 0;
165 volatile uint8_t usb_configuration = 0;
166 volatile uint8_t usb_reboot_timer = 0;
168 static uint8_t reply_buffer[8];
172 // ----- Functions -----
174 static void endpoint0_stall()
176 USB0_ENDPT0 = USB_ENDPT_EPSTALL | USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK;
179 static void endpoint0_transmit( const void *data, uint32_t len )
181 table[index(0, TX, ep0_tx_bdt_bank)].addr = (void *)data;
182 table[index(0, TX, ep0_tx_bdt_bank)].desc = BDT_DESC(len, ep0_tx_data_toggle);
183 ep0_tx_data_toggle ^= 1;
184 ep0_tx_bdt_bank ^= 1;
187 static void usb_setup()
189 const uint8_t *data = NULL;
190 uint32_t datalen = 0;
191 const usb_descriptor_list_t *list;
193 volatile uint8_t *reg;
198 switch ( setup.wRequestAndType )
200 case 0x0500: // SET_ADDRESS
202 case 0x0900: // SET_CONFIGURATION
204 print("CONFIGURE - ");
206 usb_configuration = setup.wValue;
208 cfg = usb_endpoint_config_table;
209 // clear all BDT entries, free any allocated memory...
210 for ( i = 4; i < ( NUM_ENDPOINTS + 1) * 4; i++ )
212 if ( table[i].desc & BDT_OWN )
214 usb_free( (usb_packet_t *)((uint8_t *)(table[ i ].addr) - 8) );
217 // free all queued packets
218 for ( i = 0; i < NUM_ENDPOINTS; i++ )
228 rx_first[ i ] = NULL;
237 tx_first[ i ] = NULL;
239 usb_rx_byte_count_data[i] = 0;
241 switch ( tx_state[ i ] )
243 case TX_STATE_EVEN_FREE:
244 case TX_STATE_NONE_FREE_EVEN_FIRST:
245 tx_state[ i ] = TX_STATE_BOTH_FREE_EVEN_FIRST;
247 case TX_STATE_ODD_FREE:
248 case TX_STATE_NONE_FREE_ODD_FIRST:
249 tx_state[ i ] = TX_STATE_BOTH_FREE_ODD_FIRST;
255 usb_rx_memory_needed = 0;
256 for ( i = 1; i <= NUM_ENDPOINTS; i++ )
261 if ( epconf & USB_ENDPT_EPRXEN )
267 table[ index( i, RX, EVEN ) ].addr = p->buf;
268 table[ index( i, RX, EVEN ) ].desc = BDT_DESC( 64, 0 );
272 table[ index( i, RX, EVEN ) ].desc = 0;
273 usb_rx_memory_needed++;
278 table[ index( i, RX, ODD ) ].addr = p->buf;
279 table[ index( i, RX, ODD ) ].desc = BDT_DESC( 64, 1 );
283 table[ index( i, RX, ODD ) ].desc = 0;
284 usb_rx_memory_needed++;
287 table[ index( i, TX, EVEN ) ].desc = 0;
288 table[ index( i, TX, ODD ) ].desc = 0;
291 case 0x0880: // GET_CONFIGURATION
292 reply_buffer[0] = usb_configuration;
296 case 0x0080: // GET_STATUS (device)
302 case 0x0082: // GET_STATUS (endpoint)
303 if (setup.wIndex > NUM_ENDPOINTS)
305 // TODO: do we need to handle IN vs OUT here?
311 if ( *(uint8_t *)(&USB0_ENDPT0 + setup.wIndex * 4) & 0x02 )
316 case 0x0102: // CLEAR_FEATURE (endpoint)
317 i = setup.wIndex & 0x7F;
318 if ( i > NUM_ENDPOINTS || setup.wValue != 0 )
320 // TODO: do we need to handle IN vs OUT here?
324 (*(uint8_t *)(&USB0_ENDPT0 + setup.wIndex * 4)) &= ~0x02;
325 // TODO: do we need to clear the data toggle here?
327 case 0x0302: // SET_FEATURE (endpoint)
328 i = setup.wIndex & 0x7F;
329 if ( i > NUM_ENDPOINTS || setup.wValue != 0 )
331 // TODO: do we need to handle IN vs OUT here?
335 (*(uint8_t *)(&USB0_ENDPT0 + setup.wIndex * 4)) |= 0x02;
336 // TODO: do we need to clear the data toggle here?
338 case 0x0680: // GET_DESCRIPTOR
342 printHex( setup.wValue );
345 for ( list = usb_descriptor_list; 1; list++ )
347 if ( list->addr == NULL )
349 if ( setup.wValue == list->wValue && setup.wIndex == list->wIndex )
352 if ( (setup.wValue >> 8) == 3 )
354 // for string descriptors, use the descriptor's
355 // length field, allowing runtime configured
357 datalen = *(list->addr);
361 datalen = list->length;
364 print("Desc found, ");
365 printHex32( (uint32_t)data );
369 printHex_op( data[0], 2 );
370 printHex_op( data[1], 2 );
371 printHex_op( data[2], 2 );
372 printHex_op( data[3], 2 );
373 printHex_op( data[4], 2 );
374 printHex_op( data[5], 2 );
381 print( "desc: not found" NL );
386 case 0x2221: // CDC_SET_CONTROL_LINE_STATE
387 usb_cdc_line_rtsdtr = setup.wValue;
388 //serial_print("set control line state\n");
392 case 0x21A1: // CDC_GET_LINE_CODING
393 data = (uint8_t*)usb_cdc_line_coding;
394 datalen = sizeof( usb_cdc_line_coding );
397 case 0x2021: // CDC_SET_LINE_CODING
399 //serial_print("set coding, waiting...\n");
401 return; // Cannot stall here (causes issues)
403 case 0x0921: // HID SET_REPORT
405 print("SET_REPORT - ");
406 printHex( setup.wValue );
408 printHex( setup.wValue & 0xFF );
411 USBKeys_LEDs = setup.wValue & 0xFF;
415 case 0x01A1: // HID GET_REPORT
417 print("GET_REPORT - ");
418 printHex( USBKeys_LEDs );
421 data = (uint8_t*)&USBKeys_LEDs;
425 case 0x0A21: // HID SET_IDLE
427 print("SET_IDLE - ");
428 printHex( setup.wValue );
431 USBKeys_Idle_Config = (setup.wValue >> 8);
432 USBKeys_Idle_Count = 0;
436 case 0x0B21: // HID SET_PROTOCOL
438 print("SET_PROTOCOL - ");
439 printHex( setup.wValue );
441 printHex( setup.wValue & 0xFF );
444 USBKeys_Protocol = setup.wValue & 0xFF; // 0 - Boot Mode, 1 - NKRO Mode
450 #ifdef UART_DEBUG_UNKNOWN
459 print("setup send ");
460 printHex32((uint32_t)data);
466 if ( datalen > setup.wLength )
467 datalen = setup.wLength;
470 if ( size > EP0_SIZE )
473 endpoint0_transmit(data, size);
477 // See if transmit has finished
478 if ( datalen == 0 && size < EP0_SIZE )
482 if ( size > EP0_SIZE )
484 endpoint0_transmit(data, size);
488 // See if transmit has finished
489 if ( datalen == 0 && size < EP0_SIZE )
492 // Save rest of transfer for later? XXX
494 ep0_tx_len = datalen;
498 //A bulk endpoint's toggle sequence is initialized to DATA0 when the endpoint
499 //experiences any configuration event (configuration events are explained in
500 //Sections 9.1.1.5 and 9.4.5).
502 //Configuring a device or changing an alternate setting causes all of the status
503 //and configuration values associated with endpoints in the affected interfaces
504 //to be set to their default values. This includes setting the data toggle of
505 //any endpoint using data toggles to the value DATA0.
507 //For endpoints using data toggle, regardless of whether an endpoint has the
508 //Halt feature set, a ClearFeature(ENDPOINT_HALT) request always results in the
509 //data toggle being reinitialized to DATA0.
511 static void usb_control( uint32_t stat )
521 b = stat2bufferdescriptor( stat );
522 pid = BDT_PID( b->desc );
534 case 0x0D: // Setup received from host
535 //serial_print("PID=Setup\n");
536 //if (count != 8) ; // panic?
537 // grab the 8 byte setup info
538 setup.word1 = *(uint32_t *)(buf);
539 setup.word2 = *(uint32_t *)(buf + 4);
541 // give the buffer back
542 b->desc = BDT_DESC( EP0_SIZE, DATA1 );
543 //table[index(0, RX, EVEN)].desc = BDT_DESC(EP0_SIZE, 1);
544 //table[index(0, RX, ODD)].desc = BDT_DESC(EP0_SIZE, 1);
546 // clear any leftover pending IN transactions
548 if ( ep0_tx_data_toggle )
551 //if (table[index(0, TX, EVEN)].desc & 0x80) {
552 //serial_print("leftover tx even\n");
554 //if (table[index(0, TX, ODD)].desc & 0x80) {
555 //serial_print("leftover tx odd\n");
557 table[index(0, TX, EVEN)].desc = 0;
558 table[index(0, TX, ODD)].desc = 0;
559 // first IN after Setup is always DATA1
560 ep0_tx_data_toggle = 1;
562 #ifdef UART_DEBUG_UNKNOWN
563 print("bmRequestType:");
564 printHex(setup.bmRequestType);
565 print(", bRequest:");
566 printHex(setup.bRequest);
568 printHex(setup.wValue);
570 printHex(setup.wIndex);
572 printHex(setup.wLength);
575 // actually "do" the setup request
577 // unfreeze the USB, now that we're ready
578 USB0_CTL = USB_CTL_USBENSOFEN; // clear TXSUSPENDTOKENBUSY bit
580 case 0x01: // OUT transaction received from host
586 if ( setup.wRequestAndType == 0x2021 /*CDC_SET_LINE_CODING*/ )
589 uint8_t *dst = (uint8_t *)usb_cdc_line_coding;
590 //serial_print("set line coding ");
591 for ( i = 0; i < 7; i++ )
596 //serial_phex32(usb_cdc_line_coding[0]);
597 //serial_print("\n");
598 if ( usb_cdc_line_coding[0] == 134 )
599 usb_reboot_timer = 15;
600 endpoint0_transmit( NULL, 0 );
603 // Keyboard Interface
604 if ( setup.word1 == 0x02000921 && setup.word2 == ( (1<<16) | KEYBOARD_INTERFACE ) )
606 USBKeys_LEDs = buf[0];
607 endpoint0_transmit( NULL, 0 );
609 // NKRO Keyboard Interface
610 if ( setup.word1 == 0x02000921 && setup.word2 == ( (1<<16) | NKRO_KEYBOARD_INTERFACE ) )
612 USBKeys_LEDs = buf[0];
613 endpoint0_transmit( NULL, 0 );
616 // give the buffer back
617 b->desc = BDT_DESC( EP0_SIZE, DATA1 );
620 case 0x09: // IN transaction completed to host
627 // send remaining data, if any...
632 if (size > EP0_SIZE) size = EP0_SIZE;
633 endpoint0_transmit(data, size);
636 ep0_tx_ptr = (ep0_tx_len > 0 || size == EP0_SIZE) ? data : NULL;
639 if ( setup.bRequest == 5 && setup.bmRequestType == 0 )
643 print("set address: ");
644 printHex(setup.wValue);
647 USB0_ADDR = setup.wValue;
653 print("PID=unknown:");
659 USB0_CTL = USB_CTL_USBENSOFEN; // clear TXSUSPENDTOKENBUSY bit
662 usb_packet_t *usb_rx( uint32_t endpoint )
667 if ( endpoint >= NUM_ENDPOINTS )
670 ret = rx_first[endpoint];
672 rx_first[ endpoint ] = ret->next;
673 usb_rx_byte_count_data[ endpoint ] -= ret->len;
675 //serial_print("rx, epidx=");
676 //serial_phex(endpoint);
677 //serial_print(", packet=");
678 //serial_phex32(ret);
679 //serial_print("\n");
683 static uint32_t usb_queue_byte_count( const usb_packet_t *p )
688 for ( ; p; p = p->next )
696 uint32_t usb_tx_byte_count( uint32_t endpoint )
699 if ( endpoint >= NUM_ENDPOINTS )
701 return usb_queue_byte_count( tx_first[ endpoint ] );
704 uint32_t usb_tx_packet_count( uint32_t endpoint )
706 const usb_packet_t *p;
710 if ( endpoint >= NUM_ENDPOINTS )
713 for ( p = tx_first[ endpoint ]; p; p = p->next )
720 // Called from usb_free, but only when usb_rx_memory_needed > 0, indicating
721 // receive endpoints are starving for memory. The intention is to give
722 // endpoints needing receive memory priority over the user's code, which is
723 // likely calling usb_malloc to obtain memory for transmitting. When the
724 // user is creating data very quickly, their consumption could starve reception
725 // without this prioritization. The packet buffer (input) is assigned to the
726 // first endpoint needing memory.
728 void usb_rx_memory( usb_packet_t *packet )
730 //print("USB RX MEMORY");
734 cfg = usb_endpoint_config_table;
735 //serial_print("rx_mem:");
737 for ( i = 1; i <= NUM_ENDPOINTS; i++ )
739 if ( *cfg++ & USB_ENDPT_EPRXEN )
741 if ( table[ index( i, RX, EVEN ) ].desc == 0 )
743 table[ index( i, RX, EVEN ) ].addr = packet->buf;
744 table[ index( i, RX, EVEN ) ].desc = BDT_DESC( 64, 0 );
745 usb_rx_memory_needed--;
748 //serial_print(",even\n");
751 if ( table[ index( i, RX, ODD ) ].desc == 0 )
753 table[ index( i, RX, ODD ) ].addr = packet->buf;
754 table[ index( i, RX, ODD ) ].desc = BDT_DESC( 64, 1 );
755 usb_rx_memory_needed--;
758 //serial_print(",odd\n");
764 // we should never reach this point. If we get here, it means
765 // usb_rx_memory_needed was set greater than zero, but no memory
766 // was actually needed.
767 usb_rx_memory_needed = 0;
772 //#define index(endpoint, tx, odd) (((endpoint) << 2) | ((tx) << 1) | (odd))
773 //#define stat2bufferdescriptor(stat) (table + ((stat) >> 2))
775 void usb_tx( uint32_t endpoint, usb_packet_t *packet )
777 bdt_t *b = &table[ index( endpoint, TX, EVEN ) ];
781 if ( endpoint >= NUM_ENDPOINTS )
784 //serial_print("txstate=");
785 //serial_phex(tx_state[ endpoint ]);
786 //serial_print("\n");
787 switch ( tx_state[ endpoint ] )
789 case TX_STATE_BOTH_FREE_EVEN_FIRST:
790 next = TX_STATE_ODD_FREE;
792 case TX_STATE_BOTH_FREE_ODD_FIRST:
794 next = TX_STATE_EVEN_FREE;
796 case TX_STATE_EVEN_FREE:
797 next = TX_STATE_NONE_FREE_ODD_FIRST;
799 case TX_STATE_ODD_FREE:
801 next = TX_STATE_NONE_FREE_EVEN_FIRST;
804 if (tx_first[ endpoint ] == NULL)
806 tx_first[ endpoint ] = packet;
810 tx_last[ endpoint ]->next = packet;
812 tx_last[ endpoint ] = packet;
817 tx_state[ endpoint ] = next;
818 b->addr = packet->buf;
819 b->desc = BDT_DESC( packet->len, ((uint32_t)b & 8) ? DATA1 : DATA0 );
824 void usb_device_reload()
827 #if defined(_mk20dx128vlf5_)
829 // MCHCK Kiibohd Variant
830 // Check to see if PTA3 (has a pull-up) is connected to GND (usually via jumper)
831 // Only allow reload if the jumper is present (security)
832 GPIOA_PDDR &= ~(1<<3); // Input
833 PORTA_PCR3 = PORT_PCR_PFE | PORT_PCR_MUX(1); // Internal pull-up
836 if ( GPIOA_PDIR & (1<<3) )
839 warn_print("Security jumper not present, cancelling firmware reload...");
840 info_msg("Replace jumper on middle 2 pins, or manually press the firmware reload button.");
844 // Copies variable into the VBAT register, must be identical to the variable in the bootloader to jump to the bootloader flash mode
845 for ( int pos = 0; pos < sizeof(sys_reset_to_loader_magic); pos++ )
846 (&VBAT)[ pos ] = sys_reset_to_loader_magic[ pos ];
850 // Teensy 3.0 and 3.1
852 asm volatile("bkpt");
859 uint8_t status, stat, t;
861 //serial_print("isr");
862 //status = USB0_ISTAT;
863 //serial_phex(status);
864 //serial_print("\n");
868 print("USB ISR STATUS: ");
873 if ( (status & USB_INTEN_SOFTOKEN /* 04 */ ) )
875 if ( usb_configuration )
877 t = usb_reboot_timer;
880 usb_reboot_timer = --t;
886 t = usb_cdc_transmit_flush_timer;
889 usb_cdc_transmit_flush_timer = --t;
891 usb_serial_flush_callback();
895 USB0_ISTAT = USB_INTEN_SOFTOKEN;
898 if ( (status & USB_ISTAT_TOKDNE /* 08 */ ) )
902 //serial_print("token: ep=");
903 //serial_phex(stat >> 4);
904 //serial_print(stat & 0x08 ? ",tx" : ",rx");
905 //serial_print(stat & 0x04 ? ",odd\n" : ",even\n");
906 endpoint = stat >> 4;
913 bdt_t *b = stat2bufferdescriptor(stat);
914 usb_packet_t *packet = (usb_packet_t *)((uint8_t *)(b->addr) - 8);
917 serial_phex(endpoint);
918 serial_print(", pid:");
919 serial_phex(BDT_PID(b->desc));
920 serial_print(((uint32_t)b & 8) ? ", odd" : ", even");
921 serial_print(", count:");
922 serial_phex(b->desc >> 16);
925 endpoint--; // endpoint is index to zero-based arrays
930 packet = tx_first[ endpoint ];
933 //serial_print("tx packet\n");
934 tx_first[endpoint] = packet->next;
935 b->addr = packet->buf;
936 switch ( tx_state[ endpoint ] )
938 case TX_STATE_BOTH_FREE_EVEN_FIRST:
939 tx_state[ endpoint ] = TX_STATE_ODD_FREE;
941 case TX_STATE_BOTH_FREE_ODD_FIRST:
942 tx_state[ endpoint ] = TX_STATE_EVEN_FREE;
944 case TX_STATE_EVEN_FREE:
945 tx_state[ endpoint ] = TX_STATE_NONE_FREE_ODD_FIRST;
947 case TX_STATE_ODD_FREE:
948 tx_state[ endpoint ] = TX_STATE_NONE_FREE_EVEN_FIRST;
953 b->desc = BDT_DESC( packet->len, ((uint32_t)b & 8) ? DATA1 : DATA0 );
955 //serial_print("tx no packet\n");
956 switch ( tx_state[ endpoint ] )
958 case TX_STATE_BOTH_FREE_EVEN_FIRST:
959 case TX_STATE_BOTH_FREE_ODD_FIRST:
961 case TX_STATE_EVEN_FREE:
962 tx_state[ endpoint ] = TX_STATE_BOTH_FREE_EVEN_FIRST;
964 case TX_STATE_ODD_FREE:
965 tx_state[ endpoint ] = TX_STATE_BOTH_FREE_ODD_FIRST;
968 tx_state[ endpoint ] = ((uint32_t)b & 8)
970 : TX_STATE_EVEN_FREE;
977 packet->len = b->desc >> 16;
978 if ( packet->len > 0 )
982 if ( rx_first[ endpoint ] == NULL )
984 //serial_print("rx 1st, epidx=");
985 //serial_phex(endpoint);
986 //serial_print(", packet=");
987 //serial_phex32((uint32_t)packet);
988 //serial_print("\n");
989 rx_first[ endpoint ] = packet;
993 //serial_print("rx Nth, epidx=");
994 //serial_phex(endpoint);
995 //serial_print(", packet=");
996 //serial_phex32((uint32_t)packet);
997 //serial_print("\n");
998 rx_last[ endpoint ]->next = packet;
1000 rx_last[ endpoint ] = packet;
1001 usb_rx_byte_count_data[ endpoint ] += packet->len;
1002 // TODO: implement a per-endpoint maximum # of allocated packets
1003 // so a flood of incoming data on 1 endpoint doesn't starve
1004 // the others if the user isn't reading it regularly
1005 packet = usb_malloc();
1008 b->addr = packet->buf;
1009 b->desc = BDT_DESC( 64, ((uint32_t)b & 8) ? DATA1 : DATA0 );
1013 //serial_print("starving ");
1014 //serial_phex(endpoint + 1);
1015 //serial_print(((uint32_t)b & 8) ? ",odd\n" : ",even\n");
1017 usb_rx_memory_needed++;
1022 b->desc = BDT_DESC( 64, ((uint32_t)b & 8) ? DATA1 : DATA0 );
1030 USB0_ISTAT = USB_ISTAT_TOKDNE;
1035 if ( status & USB_ISTAT_USBRST /* 01 */ )
1037 //serial_print("reset\n");
1039 // initialize BDT toggle bits
1040 USB0_CTL = USB_CTL_ODDRST;
1041 ep0_tx_bdt_bank = 0;
1043 // set up buffers to receive Setup and OUT packets
1044 table[index( 0, RX, EVEN ) ].desc = BDT_DESC( EP0_SIZE, 0 );
1045 table[index( 0, RX, EVEN ) ].addr = ep0_rx0_buf;
1046 table[index( 0, RX, ODD ) ].desc = BDT_DESC( EP0_SIZE, 0 );
1047 table[index( 0, RX, ODD ) ].addr = ep0_rx1_buf;
1048 table[index( 0, TX, EVEN ) ].desc = 0;
1049 table[index( 0, TX, ODD ) ].desc = 0;
1051 // activate endpoint 0
1052 USB0_ENDPT0 = USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK;
1054 // clear all ending interrupts
1055 USB0_ERRSTAT = 0xFF;
1058 // set the address to zero during enumeration
1061 // enable other interrupts
1063 USB0_INTEN = USB_INTEN_TOKDNEEN |
1064 USB_INTEN_SOFTOKEN |
1067 USB_INTEN_USBRSTEN |
1070 // is this necessary?
1071 USB0_CTL = USB_CTL_USBENSOFEN;
1076 if ( (status & USB_ISTAT_STALL /* 80 */ ) )
1078 //serial_print("stall:\n");
1079 USB0_ENDPT0 = USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK;
1080 USB0_ISTAT = USB_ISTAT_STALL;
1082 if ( (status & USB_ISTAT_ERROR /* 02 */ ) )
1084 uint8_t err = USB0_ERRSTAT;
1086 //serial_print("err:");
1088 //serial_print("\n");
1089 USB0_ISTAT = USB_ISTAT_ERROR;
1092 if ( (status & USB_ISTAT_SLEEP /* 10 */ ) )
1094 //serial_print("sleep\n");
1095 USB0_ISTAT = USB_ISTAT_SLEEP;
1104 print("USB INIT"NL);
1107 // If no USB cable is attached, do not initialize usb
1109 //if ( USB0_OTGISTAT & USB_OTGSTAT_ID )
1112 // Clear out endpoints table
1113 for ( int i = 0; i <= NUM_ENDPOINTS * 4; i++ )
1119 // this basically follows the flowchart in the Kinetis
1120 // Quick Reference User Guide, Rev. 1, 03/2012, page 141
1122 // assume 48 MHz clock already running
1123 // SIM - enable clock
1124 SIM_SCGC4 |= SIM_SCGC4_USBOTG;
1127 USB0_USBTRC0 = USB_USBTRC_USBRESET;
1128 while ( (USB0_USBTRC0 & USB_USBTRC_USBRESET) != 0 ); // wait for reset to end
1130 // set desc table base addr
1131 USB0_BDTPAGE1 = ((uint32_t)table) >> 8;
1132 USB0_BDTPAGE2 = ((uint32_t)table) >> 16;
1133 USB0_BDTPAGE3 = ((uint32_t)table) >> 24;
1135 // clear all ISR flags
1137 USB0_ERRSTAT = 0xFF;
1138 USB0_OTGISTAT = 0xFF;
1140 USB0_USBTRC0 |= 0x40; // undocumented bit
1143 USB0_CTL = USB_CTL_USBENSOFEN;
1146 // enable reset interrupt
1147 USB0_INTEN = USB_INTEN_USBRSTEN;
1149 // enable interrupt in NVIC...
1150 NVIC_SET_PRIORITY( IRQ_USBOTG, 112 );
1151 NVIC_ENABLE_IRQ( IRQ_USBOTG );
1154 USB0_CONTROL = USB_CONTROL_DPPULLUPNONOTG;
1159 // return 0 if the USB is not configured, or the configuration
1160 // number selected by the HOST
1161 uint8_t usb_configured()
1163 return usb_configuration;