annotate Output/pjrcUSB/output_com.h @ 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 d3cef419c849
children 45feb80a2ad1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
420
23a1868b4ac2 Adding dynamic USB power support
Jacob Alexander <haata@kiibohd.com>
parents: 370
diff changeset
1 /* Copyright (C) 2013-2016 by Jacob Alexander
122
2bb16439e6ca Preparing Teensy 3.1 and CLI merge for DPH controller code.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
2 *
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
4 * of this software and associated documentation files (the "Software"), to deal
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
5 * in the Software without restriction, including without limitation the rights
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
7 * copies of the Software, and to permit persons to whom the Software is
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
8 * furnished to do so, subject to the following conditions:
122
2bb16439e6ca Preparing Teensy 3.1 and CLI merge for DPH controller code.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
9 *
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
10 * The above copyright notice and this permission notice shall be included in
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
11 * all copies or substantial portions of the Software.
122
2bb16439e6ca Preparing Teensy 3.1 and CLI merge for DPH controller code.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
12 *
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
19 * THE SOFTWARE.
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
20 */
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
21
341
66eccdd9ced5 Code cleanup
Jacob Alexander <haata@kiibohd.com>
parents: 322
diff changeset
22 #pragma once
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
23
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
24 // ----- Includes -----
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
25
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
26 // Compiler Includes
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
27 #include <stdint.h>
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
28
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
29 // Local Includes
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
30 #include <buildvars.h> // Defines USB Parameters, partially generated by CMake
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
31
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
32
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
33
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
34 // ----- Defines -----
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
35
223
fc3b9cb190cc Adding NKRO support to AVR/DPH.
Jacob Alexander <haata@kiibohd.com>
parents: 218
diff changeset
36 // Max size of key buffer needed for NKRO
fc3b9cb190cc Adding NKRO support to AVR/DPH.
Jacob Alexander <haata@kiibohd.com>
parents: 218
diff changeset
37 // Boot mode uses only the first 6 bytes
237
01b686cc94e8 Fixing USB NKRO key buffer bug
Jacob Alexander <haata@kiibohd.com>
parents: 236
diff changeset
38 #define USB_NKRO_BITFIELD_SIZE_KEYS 27
223
fc3b9cb190cc Adding NKRO support to AVR/DPH.
Jacob Alexander <haata@kiibohd.com>
parents: 218
diff changeset
39 #define USB_BOOT_MAX_KEYS 6
fc3b9cb190cc Adding NKRO support to AVR/DPH.
Jacob Alexander <haata@kiibohd.com>
parents: 218
diff changeset
40
fc3b9cb190cc Adding NKRO support to AVR/DPH.
Jacob Alexander <haata@kiibohd.com>
parents: 218
diff changeset
41
fc3b9cb190cc Adding NKRO support to AVR/DPH.
Jacob Alexander <haata@kiibohd.com>
parents: 218
diff changeset
42
fc3b9cb190cc Adding NKRO support to AVR/DPH.
Jacob Alexander <haata@kiibohd.com>
parents: 218
diff changeset
43 // ----- Enumerations -----
fc3b9cb190cc Adding NKRO support to AVR/DPH.
Jacob Alexander <haata@kiibohd.com>
parents: 218
diff changeset
44
fc3b9cb190cc Adding NKRO support to AVR/DPH.
Jacob Alexander <haata@kiibohd.com>
parents: 218
diff changeset
45 // USB NKRO state transitions (indicates which Report ID's need refreshing)
fc3b9cb190cc Adding NKRO support to AVR/DPH.
Jacob Alexander <haata@kiibohd.com>
parents: 218
diff changeset
46 // Boot mode just checks if any keys were changed (as everything is sent every time)
fc3b9cb190cc Adding NKRO support to AVR/DPH.
Jacob Alexander <haata@kiibohd.com>
parents: 218
diff changeset
47 typedef enum USBKeyChangeState {
fc3b9cb190cc Adding NKRO support to AVR/DPH.
Jacob Alexander <haata@kiibohd.com>
parents: 218
diff changeset
48 USBKeyChangeState_None = 0x00,
fc3b9cb190cc Adding NKRO support to AVR/DPH.
Jacob Alexander <haata@kiibohd.com>
parents: 218
diff changeset
49 USBKeyChangeState_Modifiers = 0x01,
fc3b9cb190cc Adding NKRO support to AVR/DPH.
Jacob Alexander <haata@kiibohd.com>
parents: 218
diff changeset
50 USBKeyChangeState_MainKeys = 0x02,
fc3b9cb190cc Adding NKRO support to AVR/DPH.
Jacob Alexander <haata@kiibohd.com>
parents: 218
diff changeset
51 USBKeyChangeState_SecondaryKeys = 0x04,
236
d494b74d5242 Fixed Linux NKRO bitmap bug.
Jacob Alexander <haata@kiibohd.com>
parents: 234
diff changeset
52 USBKeyChangeState_TertiaryKeys = 0x08,
248
2e098cf8f71d Fixing Linux NKRO Delete bug
Jacob Alexander <haata@kiibohd.com>
parents: 244
diff changeset
53 USBKeyChangeState_QuartiaryKeys = 0x10,
2e098cf8f71d Fixing Linux NKRO Delete bug
Jacob Alexander <haata@kiibohd.com>
parents: 244
diff changeset
54 USBKeyChangeState_System = 0x20,
2e098cf8f71d Fixing Linux NKRO Delete bug
Jacob Alexander <haata@kiibohd.com>
parents: 244
diff changeset
55 USBKeyChangeState_Consumer = 0x40,
2e098cf8f71d Fixing Linux NKRO Delete bug
Jacob Alexander <haata@kiibohd.com>
parents: 244
diff changeset
56 USBKeyChangeState_All = 0x7F,
223
fc3b9cb190cc Adding NKRO support to AVR/DPH.
Jacob Alexander <haata@kiibohd.com>
parents: 218
diff changeset
57 } USBKeyChangeState;
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
58
430
d3cef419c849 Adding relative movement mouse key support
Jacob Alexander <haata@kiibohd.com>
parents: 429
diff changeset
59 // Allows for selective USB descriptor pushes
d3cef419c849 Adding relative movement mouse key support
Jacob Alexander <haata@kiibohd.com>
parents: 429
diff changeset
60 // However, in most cases everything is updated for each packet push
d3cef419c849 Adding relative movement mouse key support
Jacob Alexander <haata@kiibohd.com>
parents: 429
diff changeset
61 typedef enum USBMouseChangeState {
d3cef419c849 Adding relative movement mouse key support
Jacob Alexander <haata@kiibohd.com>
parents: 429
diff changeset
62 USBMouseChangeState_None = 0x00,
d3cef419c849 Adding relative movement mouse key support
Jacob Alexander <haata@kiibohd.com>
parents: 429
diff changeset
63 USBMouseChangeState_Buttons = 0x01,
d3cef419c849 Adding relative movement mouse key support
Jacob Alexander <haata@kiibohd.com>
parents: 429
diff changeset
64 USBMouseChangeState_Relative = 0x02,
447
56237ba5da6f Adding auto-restart support whenever USB gets into an odd state
Jacob Alexander <haata@kiibohd.com>
parents: 430
diff changeset
65 USBMouseChangeState_All = 0x03,
430
d3cef419c849 Adding relative movement mouse key support
Jacob Alexander <haata@kiibohd.com>
parents: 429
diff changeset
66 } USBMouseChangeState;
d3cef419c849 Adding relative movement mouse key support
Jacob Alexander <haata@kiibohd.com>
parents: 429
diff changeset
67
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
68
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
69
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
70 // ----- Variables -----
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
71
125
8dab4014c398 Major code cleanup and preparation for PartialMap Macro Module
Jacob Alexander <haata@kiibohd.com>
parents: 123
diff changeset
72 // Variables used to communciate to the output module
8dab4014c398 Major code cleanup and preparation for PartialMap Macro Module
Jacob Alexander <haata@kiibohd.com>
parents: 123
diff changeset
73 // XXX Even if the output module is not USB, this is internally understood keymapping scheme
223
fc3b9cb190cc Adding NKRO support to AVR/DPH.
Jacob Alexander <haata@kiibohd.com>
parents: 218
diff changeset
74 extern uint8_t USBKeys_Modifiers;
fc3b9cb190cc Adding NKRO support to AVR/DPH.
Jacob Alexander <haata@kiibohd.com>
parents: 218
diff changeset
75 extern uint8_t USBKeys_Keys[USB_NKRO_BITFIELD_SIZE_KEYS];
fc3b9cb190cc Adding NKRO support to AVR/DPH.
Jacob Alexander <haata@kiibohd.com>
parents: 218
diff changeset
76 extern uint8_t USBKeys_Sent;
fc3b9cb190cc Adding NKRO support to AVR/DPH.
Jacob Alexander <haata@kiibohd.com>
parents: 218
diff changeset
77 extern volatile uint8_t USBKeys_LEDs;
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
78
223
fc3b9cb190cc Adding NKRO support to AVR/DPH.
Jacob Alexander <haata@kiibohd.com>
parents: 218
diff changeset
79 extern uint8_t USBKeys_SysCtrl; // 1KRO container for System Control HID table
fc3b9cb190cc Adding NKRO support to AVR/DPH.
Jacob Alexander <haata@kiibohd.com>
parents: 218
diff changeset
80 extern uint16_t USBKeys_ConsCtrl; // 1KRO container for Consumer Control HID table
fc3b9cb190cc Adding NKRO support to AVR/DPH.
Jacob Alexander <haata@kiibohd.com>
parents: 218
diff changeset
81
fc3b9cb190cc Adding NKRO support to AVR/DPH.
Jacob Alexander <haata@kiibohd.com>
parents: 218
diff changeset
82 extern volatile uint8_t USBKeys_Protocol; // 0 - Boot Mode, 1 - NKRO Mode
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
83
429
970dab727f47 Adding basic mouse button support
Jacob Alexander <haata@kiibohd.com>
parents: 420
diff changeset
84 extern volatile uint16_t USBMouse_Buttons; // Bitmask for mouse buttons
430
d3cef419c849 Adding relative movement mouse key support
Jacob Alexander <haata@kiibohd.com>
parents: 429
diff changeset
85 extern volatile uint16_t USBMouse_Relative_x;
d3cef419c849 Adding relative movement mouse key support
Jacob Alexander <haata@kiibohd.com>
parents: 429
diff changeset
86 extern volatile uint16_t USBMouse_Relative_y;
429
970dab727f47 Adding basic mouse button support
Jacob Alexander <haata@kiibohd.com>
parents: 420
diff changeset
87
447
56237ba5da6f Adding auto-restart support whenever USB gets into an odd state
Jacob Alexander <haata@kiibohd.com>
parents: 430
diff changeset
88 // Keeps track of the idle timeout refresh (used on Mac OSX)
223
fc3b9cb190cc Adding NKRO support to AVR/DPH.
Jacob Alexander <haata@kiibohd.com>
parents: 218
diff changeset
89 extern uint8_t USBKeys_Idle_Config;
447
56237ba5da6f Adding auto-restart support whenever USB gets into an odd state
Jacob Alexander <haata@kiibohd.com>
parents: 430
diff changeset
90 extern uint32_t USBKeys_Idle_Expiry;
56237ba5da6f Adding auto-restart support whenever USB gets into an odd state
Jacob Alexander <haata@kiibohd.com>
parents: 430
diff changeset
91 extern uint8_t USBKeys_Idle_Count; // AVR only
223
fc3b9cb190cc Adding NKRO support to AVR/DPH.
Jacob Alexander <haata@kiibohd.com>
parents: 218
diff changeset
92
430
d3cef419c849 Adding relative movement mouse key support
Jacob Alexander <haata@kiibohd.com>
parents: 429
diff changeset
93 extern USBKeyChangeState USBKeys_Changed;
d3cef419c849 Adding relative movement mouse key support
Jacob Alexander <haata@kiibohd.com>
parents: 429
diff changeset
94 extern USBMouseChangeState USBMouse_Changed;
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
95
347
136e47478441 UARTConnect enumeration working!
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
96 extern volatile uint8_t Output_Available; // 0 - Output module not fully functional, 1 - Output module working
275
d5bf41d7f7ef Initial code for USB cable detection
Jacob Alexander <haata@kiibohd.com>
parents: 248
diff changeset
97
285
1495b4504a59 Adding outputDebug command
Jacob Alexander <haata@kiibohd.com>
parents: 275
diff changeset
98 extern uint8_t Output_DebugMode; // 0 - Debug disabled, 1 - Debug enabled
1495b4504a59 Adding outputDebug command
Jacob Alexander <haata@kiibohd.com>
parents: 275
diff changeset
99
420
23a1868b4ac2 Adding dynamic USB power support
Jacob Alexander <haata@kiibohd.com>
parents: 370
diff changeset
100 extern uint16_t Output_ExtCurrent_Available; // mA - Set by outside module if not using USB (i.e. Interconnect)
23a1868b4ac2 Adding dynamic USB power support
Jacob Alexander <haata@kiibohd.com>
parents: 370
diff changeset
101
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
102
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
103
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
104 // ----- Functions -----
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
105
125
8dab4014c398 Major code cleanup and preparation for PartialMap Macro Module
Jacob Alexander <haata@kiibohd.com>
parents: 123
diff changeset
106 void Output_setup();
8dab4014c398 Major code cleanup and preparation for PartialMap Macro Module
Jacob Alexander <haata@kiibohd.com>
parents: 123
diff changeset
107 void Output_send();
122
2bb16439e6ca Preparing Teensy 3.1 and CLI merge for DPH controller code.
Jacob Alexander <haata@kiibohd.com>
parents: 109
diff changeset
108
234
aaf14110ea7b Adding NKRO and Boot mode capabilities.
Jacob Alexander <haata@kiibohd.com>
parents: 223
diff changeset
109 void Output_flushBuffers();
aaf14110ea7b Adding NKRO and Boot mode capabilities.
Jacob Alexander <haata@kiibohd.com>
parents: 223
diff changeset
110
125
8dab4014c398 Major code cleanup and preparation for PartialMap Macro Module
Jacob Alexander <haata@kiibohd.com>
parents: 123
diff changeset
111 void Output_firmwareReload();
8dab4014c398 Major code cleanup and preparation for PartialMap Macro Module
Jacob Alexander <haata@kiibohd.com>
parents: 123
diff changeset
112 void Output_softReset();
123
0c5d1fe99302 Adding CLI and CDC Serial support for Teensy 2.0 and Teensy 2.0++
Jacob Alexander <haata@kiibohd.com>
parents: 122
diff changeset
113
0c5d1fe99302 Adding CLI and CDC Serial support for Teensy 2.0 and Teensy 2.0++
Jacob Alexander <haata@kiibohd.com>
parents: 122
diff changeset
114 // Relies on USB serial module
125
8dab4014c398 Major code cleanup and preparation for PartialMap Macro Module
Jacob Alexander <haata@kiibohd.com>
parents: 123
diff changeset
115 unsigned int Output_availablechar();
123
0c5d1fe99302 Adding CLI and CDC Serial support for Teensy 2.0 and Teensy 2.0++
Jacob Alexander <haata@kiibohd.com>
parents: 122
diff changeset
116
420
23a1868b4ac2 Adding dynamic USB power support
Jacob Alexander <haata@kiibohd.com>
parents: 370
diff changeset
117 // Returns the total mA available (total, if used in a chain, each device will have to use a slice of it)
23a1868b4ac2 Adding dynamic USB power support
Jacob Alexander <haata@kiibohd.com>
parents: 370
diff changeset
118 unsigned int Output_current_available();
23a1868b4ac2 Adding dynamic USB power support
Jacob Alexander <haata@kiibohd.com>
parents: 370
diff changeset
119
23a1868b4ac2 Adding dynamic USB power support
Jacob Alexander <haata@kiibohd.com>
parents: 370
diff changeset
120 void Output_update_external_current( unsigned int current );
23a1868b4ac2 Adding dynamic USB power support
Jacob Alexander <haata@kiibohd.com>
parents: 370
diff changeset
121 void Output_update_usb_current( unsigned int current );
23a1868b4ac2 Adding dynamic USB power support
Jacob Alexander <haata@kiibohd.com>
parents: 370
diff changeset
122
125
8dab4014c398 Major code cleanup and preparation for PartialMap Macro Module
Jacob Alexander <haata@kiibohd.com>
parents: 123
diff changeset
123 int Output_getchar();
8dab4014c398 Major code cleanup and preparation for PartialMap Macro Module
Jacob Alexander <haata@kiibohd.com>
parents: 123
diff changeset
124 int Output_putchar( char c );
8dab4014c398 Major code cleanup and preparation for PartialMap Macro Module
Jacob Alexander <haata@kiibohd.com>
parents: 123
diff changeset
125 int Output_putstr( char* str );
104
e84738d174bc Moving USB to Output in preparation for additional Output types.
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
126