comparison Output/pjrcUSB/arm/usb_dev.c @ 447:56237ba5da6f

Adding auto-restart support whenever USB gets into an odd state - Somewhat aggresive, may cause restarts if the keyboard/OS hasn't fully intialized the keyboard - Added GET_IDLE handling and correct usage of SET_IDLE - Initial implementation of idle send, commented out as it causes issues on Mac OSX for sleeping (keyboard has been working without it) - MacOSX seems to have some sort of data corruption on the USB link, not sure why (other OSs have no issues) - Cleaned up some code - Added a longer sleep after the resume sequence to prevent possible issues sending keys too soon (may need to be increased more) Ipad support now seems flaky, though Mac, Windows seems solid. Init sequence on Linux seems slow, even though there are no errors.
author Jacob Alexander <haata@kiibohd.com>
date Fri, 27 May 2016 01:21:57 -0700
parents 68e19d7c953e
children 45feb80a2ad1
comparison
equal deleted inserted replaced
446:f570ab7c861a 447:56237ba5da6f
191 table[index(0, TX, ep0_tx_bdt_bank)].desc = BDT_DESC(len, ep0_tx_data_toggle); 191 table[index(0, TX, ep0_tx_bdt_bank)].desc = BDT_DESC(len, ep0_tx_data_toggle);
192 ep0_tx_data_toggle ^= 1; 192 ep0_tx_data_toggle ^= 1;
193 ep0_tx_bdt_bank ^= 1; 193 ep0_tx_bdt_bank ^= 1;
194 } 194 }
195 195
196 void usb_reinit()
197 {
198 power_neg_delay = 0;
199 usb_configuration = 0; // Clear USB configuration if we have one
200 USB0_CONTROL = 0; // Disable D+ Pullup to simulate disconnect
201 delay(10); // Delay is necessary to simulate disconnect
202 usb_init();
203 }
204
196 // Used to check any USB state changes that may not have a proper interrupt 205 // Used to check any USB state changes that may not have a proper interrupt
197 // Called once per scan loop, should take minimal processing time or it may affect other modules 206 // Called once per scan loop, should take minimal processing time or it may affect other modules
198 void usb_device_check() 207 void usb_device_check()
199 { 208 {
200 // Check to see if we're still waiting for the next USB request after Get Configuration Descriptor 209 // Check to see if we're still waiting for the next USB request after Get Configuration Descriptor
210 // XXX Currently only transitions to 100 mA 219 // XXX Currently only transitions to 100 mA
211 // It may be possible to transition down again to 20 mA 220 // It may be possible to transition down again to 20 mA
212 *usb_bMaxPower = 50; 221 *usb_bMaxPower = 50;
213 222
214 // Re-initialize USB 223 // Re-initialize USB
215 power_neg_delay = 0; 224 usb_reinit();
216 usb_configuration = 0; // Clear USB configuration if we have one
217 USB0_CONTROL = 0; // Disable D+ Pullup to simulate disconnect
218 delay(10); // Delay is necessary to simulate disconnect
219 usb_init();
220 } 225 }
221 } 226 }
222 } 227 }
223 228
224 static void usb_setup() 229 static void usb_setup()
543 print( NL ); 548 print( NL );
544 endpoint0_stall(); 549 endpoint0_stall();
545 break; 550 break;
546 } 551 }
547 552
548 return; 553 goto send;
549 554
550 case 0x01A1: // HID GET_REPORT 555 case 0x01A1: // HID GET_REPORT
551 #ifdef UART_DEBUG 556 #ifdef UART_DEBUG
552 print("GET_REPORT - "); 557 print("GET_REPORT - ");
553 printHex( setup.wIndex ); 558 printHex( setup.wIndex );
572 577
573 case 0x0A21: // HID SET_IDLE 578 case 0x0A21: // HID SET_IDLE
574 #ifdef UART_DEBUG 579 #ifdef UART_DEBUG
575 print("SET_IDLE - "); 580 print("SET_IDLE - ");
576 printHex( setup.wValue ); 581 printHex( setup.wValue );
582 print(" - ");
583 printHex( setup.wValue >> 8 );
577 print(NL); 584 print(NL);
578 #endif 585 #endif
579 USBKeys_Idle_Config = (setup.wValue >> 8); 586 USBKeys_Idle_Config = (setup.wValue >> 8);
580 USBKeys_Idle_Count = 0; 587 USBKeys_Idle_Expiry = 0;
581 goto send; 588 goto send;
589
590 case 0x02A1: // HID GET_IDLE
591 #ifdef UART_DEBUG
592 print("SET_IDLE - ");
593 printHex( setup.wValue );
594 print(" - ");
595 printHex( USBKeys_Idle_Config );
596 print(NL);
597 #endif
598 reply_buffer[0] = USBKeys_Idle_Config;
599 datalen = 1;
600 goto send;
601
582 602
583 case 0x0B21: // HID SET_PROTOCOL 603 case 0x0B21: // HID SET_PROTOCOL
584 #ifdef UART_DEBUG 604 #ifdef UART_DEBUG
585 print("SET_PROTOCOL - "); 605 print("SET_PROTOCOL - ");
586 printHex( setup.wValue ); 606 printHex( setup.wValue );
977 //#define index(endpoint, tx, odd) (((endpoint) << 2) | ((tx) << 1) | (odd)) 997 //#define index(endpoint, tx, odd) (((endpoint) << 2) | ((tx) << 1) | (odd))
978 //#define stat2bufferdescriptor(stat) (table + ((stat) >> 2)) 998 //#define stat2bufferdescriptor(stat) (table + ((stat) >> 2))
979 999
980 void usb_tx( uint32_t endpoint, usb_packet_t *packet ) 1000 void usb_tx( uint32_t endpoint, usb_packet_t *packet )
981 { 1001 {
1002 // Update expiry counter
1003 USBKeys_Idle_Expiry = systick_millis_count;
1004
982 // If we have been sleeping, try to wake up host 1005 // If we have been sleeping, try to wake up host
983 if ( usb_dev_sleep ) 1006 if ( usb_dev_sleep )
984 { 1007 {
985 // Force wake-up for 10 ms 1008 // Force wake-up for 10 ms
986 // According to the USB Spec a device must hold resume for at least 1 ms but no more than 15 ms 1009 // According to the USB Spec a device must hold resume for at least 1 ms but no more than 15 ms
987 USB0_CTL |= USB_CTL_RESUME; 1010 USB0_CTL |= USB_CTL_RESUME;
988 delay(10); 1011 delay(10);
989 USB0_CTL &= ~(USB_CTL_RESUME); 1012 USB0_CTL &= ~(USB_CTL_RESUME);
1013 delay(50); // Wait for at least 50 ms to make sure the bus is clear
1014 usb_dev_sleep = 0; // Make sure we don't call this again, may crash system
990 } 1015 }
991 1016
992 // Since we are transmitting data, USB will be brought out of sleep/suspend 1017 // Since we are transmitting data, USB will be brought out of sleep/suspend
993 // if it's in that state 1018 // if it's in that state
994 // Use the currently set descriptor value 1019 // Use the currently set descriptor value