diff 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
line wrap: on
line diff
--- a/Output/pjrcUSB/arm/usb_dev.c	Thu May 26 11:13:35 2016 -0700
+++ b/Output/pjrcUSB/arm/usb_dev.c	Fri May 27 01:21:57 2016 -0700
@@ -193,6 +193,15 @@
 	ep0_tx_bdt_bank ^= 1;
 }
 
+void usb_reinit()
+{
+	power_neg_delay = 0;
+	usb_configuration = 0; // Clear USB configuration if we have one
+	USB0_CONTROL = 0; // Disable D+ Pullup to simulate disconnect
+	delay(10); // Delay is necessary to simulate disconnect
+	usb_init();
+}
+
 // Used to check any USB state changes that may not have a proper interrupt
 // Called once per scan loop, should take minimal processing time or it may affect other modules
 void usb_device_check()
@@ -212,11 +221,7 @@
 			*usb_bMaxPower = 50;
 
 			// Re-initialize USB
-			power_neg_delay = 0;
-			usb_configuration = 0; // Clear USB configuration if we have one
-			USB0_CONTROL = 0; // Disable D+ Pullup to simulate disconnect
-			delay(10); // Delay is necessary to simulate disconnect
-			usb_init();
+			usb_reinit();
 		}
 	}
 }
@@ -545,7 +550,7 @@
 			break;
 		}
 
-		return;
+		goto send;
 
 	case 0x01A1: // HID GET_REPORT
 		#ifdef UART_DEBUG
@@ -574,12 +579,27 @@
 		#ifdef UART_DEBUG
 		print("SET_IDLE - ");
 		printHex( setup.wValue );
+		print(" - ");
+		printHex( setup.wValue >> 8 );
 		print(NL);
 		#endif
 		USBKeys_Idle_Config = (setup.wValue >> 8);
-		USBKeys_Idle_Count = 0;
+		USBKeys_Idle_Expiry = 0;
 		goto send;
 
+	case 0x02A1: // HID GET_IDLE
+		#ifdef UART_DEBUG
+		print("SET_IDLE - ");
+		printHex( setup.wValue );
+		print(" - ");
+		printHex( USBKeys_Idle_Config );
+		print(NL);
+		#endif
+		reply_buffer[0] = USBKeys_Idle_Config;
+		datalen = 1;
+		goto send;
+
+
 	case 0x0B21: // HID SET_PROTOCOL
 		#ifdef UART_DEBUG
 		print("SET_PROTOCOL - ");
@@ -979,6 +999,9 @@
 
 void usb_tx( uint32_t endpoint, usb_packet_t *packet )
 {
+	// Update expiry counter
+	USBKeys_Idle_Expiry = systick_millis_count;
+
 	// If we have been sleeping, try to wake up host
 	if ( usb_dev_sleep )
 	{
@@ -987,6 +1010,8 @@
 		USB0_CTL |= USB_CTL_RESUME;
 		delay(10);
 		USB0_CTL &= ~(USB_CTL_RESUME);
+		delay(50); // Wait for at least 50 ms to make sure the bus is clear
+		usb_dev_sleep = 0; // Make sure we don't call this again, may crash system
 	}
 
 	// Since we are transmitting data, USB will be brought out of sleep/suspend