changeset 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 f570ab7c861a
children 077a1dfd8529
files Output/pjrcUSB/arm/usb_dev.c Output/pjrcUSB/arm/usb_dev.h Output/pjrcUSB/arm/usb_keyboard.c Output/pjrcUSB/output_com.c Output/pjrcUSB/output_com.h Output/usbMuxUart/output_com.c
diffstat 6 files changed, 81 insertions(+), 15 deletions(-) [+]
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
--- a/Output/pjrcUSB/arm/usb_dev.h	Thu May 26 11:13:35 2016 -0700
+++ b/Output/pjrcUSB/arm/usb_dev.h	Fri May 27 01:21:57 2016 -0700
@@ -1,7 +1,7 @@
 /* Teensyduino Core Library
  * http://www.pjrc.com/teensy/
  * Copyright (c) 2013 PJRC.COM, LLC.
- * Modifications by Jacob Alexander 2014-2015
+ * Modifications by Jacob Alexander 2014-2016
  *
  * Permission is hereby granted, free of charge, to any person obtaining
  * a copy of this software and associated documentation files (the
@@ -61,6 +61,7 @@
 
 uint8_t usb_configured(); // is the USB port configured
 uint8_t usb_init(); // Returns 1 on success, 0 if no cable is attached
+void usb_reinit(); // Force restart USB interface, simulates disconnect
 
 void usb_isr();
 void usb_tx( uint32_t endpoint, usb_packet_t *packet );
--- a/Output/pjrcUSB/arm/usb_keyboard.c	Thu May 26 11:13:35 2016 -0700
+++ b/Output/pjrcUSB/arm/usb_keyboard.c	Fri May 27 01:21:57 2016 -0700
@@ -126,13 +126,16 @@
 		if ( ++wait_count > TX_TIMEOUT || transmit_previous_timeout )
 		{
 			transmit_previous_timeout = 1;
-			warn_print("USB Transmit Timeout...");
+			warn_print("USB Transmit Timeout...restarting device");
 			USBKeys_Changed = USBKeyChangeState_None; // Indicate packet lost
+			usb_device_software_reset();
 			return;
 		}
 		yield();
 	}
 
+	transmit_previous_timeout = 0;
+
 	// Pointer to USB tx packet buffer
 	uint8_t *tx_buf = tx_packet->buf;
 
--- a/Output/pjrcUSB/output_com.c	Thu May 26 11:13:35 2016 -0700
+++ b/Output/pjrcUSB/output_com.c	Fri May 27 01:21:57 2016 -0700
@@ -137,9 +137,11 @@
 
 // the idle configuration, how often we send the report to the
 // host (ms * 4) even when it hasn't changed
-uint8_t  USBKeys_Idle_Config = 125;
+// 0 - Disables
+uint8_t  USBKeys_Idle_Config = 0;
 
-// count until idle timeout
+// Count until idle timeout
+uint32_t USBKeys_Idle_Expiry = 0;
 uint8_t  USBKeys_Idle_Count = 0;
 
 // Indicates whether the Output module is fully functional
@@ -646,6 +648,19 @@
 		for ( uint8_t c = USBKeys_Sent; c < USB_BOOT_MAX_KEYS; c++ )
 			USBKeys_Keys[c] = 0;
 
+	// XXX - Behaves oddly on Mac OSX, might help with corrupted packets specific to OSX? -HaaTa
+	/*
+	// Check if idle count has been exceed, this forces usb_keyboard_send and usb_mouse_send to update
+	// TODO Add joystick as well (may be endpoint specific, currently not kept track of)
+	if ( usb_configuration && USBKeys_Idle_Config && (
+		USBKeys_Idle_Expiry < systick_millis_count ||
+		USBKeys_Idle_Expiry + USBKeys_Idle_Config * 4 >= systick_millis_count ) )
+	{
+		USBKeys_Changed = USBKeyChangeState_All;
+		USBMouse_Changed = USBMouseChangeState_All;
+	}
+	*/
+
 	// Process mouse actions
 	while ( USBMouse_Changed )
 		usb_mouse_send();
--- a/Output/pjrcUSB/output_com.h	Thu May 26 11:13:35 2016 -0700
+++ b/Output/pjrcUSB/output_com.h	Fri May 27 01:21:57 2016 -0700
@@ -62,6 +62,7 @@
 	USBMouseChangeState_None     = 0x00,
 	USBMouseChangeState_Buttons  = 0x01,
 	USBMouseChangeState_Relative = 0x02,
+	USBMouseChangeState_All      = 0x03,
 } USBMouseChangeState;
 
 
@@ -84,9 +85,10 @@
 extern volatile uint16_t USBMouse_Relative_x;
 extern volatile uint16_t USBMouse_Relative_y;
 
-// Misc variables (XXX Some are only properly utilized using AVR)
+// Keeps track of the idle timeout refresh (used on Mac OSX)
 extern          uint8_t  USBKeys_Idle_Config;
-extern          uint8_t  USBKeys_Idle_Count;
+extern          uint32_t USBKeys_Idle_Expiry;
+extern          uint8_t  USBKeys_Idle_Count; // AVR only
 
 extern USBKeyChangeState   USBKeys_Changed;
 extern USBMouseChangeState USBMouse_Changed;
--- a/Output/usbMuxUart/output_com.c	Thu May 26 11:13:35 2016 -0700
+++ b/Output/usbMuxUart/output_com.c	Fri May 27 01:21:57 2016 -0700
@@ -37,6 +37,7 @@
 #include <arm/usb_dev.h>
 #include <arm/usb_keyboard.h>
 #include <arm/usb_serial.h>
+#include "arm/usb_mouse.h"
 #endif
 
 // KLL
@@ -141,9 +142,11 @@
 
 // the idle configuration, how often we send the report to the
 // host (ms * 4) even when it hasn't changed
-uint8_t  USBKeys_Idle_Config = 125;
+// 0 - Disables
+uint8_t  USBKeys_Idle_Config = 0;
 
-// count until idle timeout
+// Count until idle timeout
+uint32_t USBKeys_Idle_Expiry = 0;
 uint8_t  USBKeys_Idle_Count = 0;
 
 // Indicates whether the Output module is fully functional
@@ -653,6 +656,23 @@
 		for ( uint8_t c = USBKeys_Sent; c < USB_BOOT_MAX_KEYS; c++ )
 			USBKeys_Keys[c] = 0;
 
+	// XXX - Behaves oddly on Mac OSX, might help with corrupted packets specific to OSX? -HaaTa
+	/*
+	// Check if idle count has been exceed, this forces usb_keyboard_send and usb_mouse_send to update
+	// TODO Add joystick as well (may be endpoint specific, currently not kept track of)
+	if ( usb_configuration && USBKeys_Idle_Config && (
+		USBKeys_Idle_Expiry < systick_millis_count ||
+		USBKeys_Idle_Expiry + USBKeys_Idle_Config * 4 >= systick_millis_count ) )
+	{
+		USBKeys_Changed = USBKeyChangeState_All;
+		USBMouse_Changed = USBMouseChangeState_All;
+	}
+	*/
+
+	// Process mouse actions
+	while ( USBMouse_Changed )
+		usb_mouse_send();
+
 	// Send keypresses while there are pending changes
 	while ( USBKeys_Changed )
 		usb_keyboard_send();