]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Output/pjrcUSB/output_com.c
Initial work for McHCK mk20dx128vlf5 port.
[kiibohd-controller.git] / Output / pjrcUSB / output_com.c
1 /* Copyright (C) 2011-2014 by Jacob Alexander
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19  * THE SOFTWARE.
20  */
21
22 // ----- Includes -----
23
24 // Compiler Includes
25 #include <Lib/OutputLib.h>
26
27 // Project Includes
28 #include <cli.h>
29 #include <print.h>
30 #include <scan_loop.h>
31
32 // USB Includes
33 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
34 #include "avr/usb_keyboard_serial.h"
35 #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_)
36 #include "arm/usb_dev.h"
37 #include "arm/usb_keyboard.h"
38 #include "arm/usb_serial.h"
39 #endif
40
41 // Local Includes
42 #include "output_com.h"
43
44
45
46 // ----- Function Declarations -----
47
48 void cliFunc_kbdProtocol( char* args );
49 void cliFunc_readLEDs   ( char* args );
50 void cliFunc_sendKeys   ( char* args );
51 void cliFunc_setKeys    ( char* args );
52 void cliFunc_setMod     ( char* args );
53
54
55 // ----- Variables -----
56
57 // Output Module command dictionary
58 char*       outputCLIDictName = "USB Module Commands";
59 CLIDictItem outputCLIDict[] = {
60         { "kbdProtocol", "Keyboard Protocol Mode: 0 - Boot, 1 - OS/NKRO Mode", cliFunc_kbdProtocol },
61         { "readLEDs",    "Read LED byte:" NL "\t\t1 NumLck, 2 CapsLck, 4 ScrlLck, 16 Kana, etc.", cliFunc_readLEDs },
62         { "sendKeys",    "Send the prepared list of USB codes and modifier byte.", cliFunc_sendKeys },
63         { "setKeys",     "Prepare a space separated list of USB codes (decimal). Waits until \033[35msendKeys\033[0m.", cliFunc_setKeys },
64         { "setMod",      "Set the modfier byte:" NL "\t\t1 LCtrl, 2 LShft, 4 LAlt, 8 LGUI, 16 RCtrl, 32 RShft, 64 RAlt, 128 RGUI", cliFunc_setMod },
65         { 0, 0, 0 } // Null entry for dictionary end
66 };
67
68
69 // Which modifier keys are currently pressed
70 // 1=left ctrl,    2=left shift,   4=left alt,    8=left gui
71 // 16=right ctrl, 32=right shift, 64=right alt, 128=right gui
72          uint8_t USBKeys_Modifiers    = 0;
73          uint8_t USBKeys_ModifiersCLI = 0; // Separate CLI send buffer
74
75 // Currently pressed keys, max is defined by USB_MAX_KEY_SEND
76          uint8_t USBKeys_Array   [USB_MAX_KEY_SEND];
77          uint8_t USBKeys_ArrayCLI[USB_MAX_KEY_SEND]; // Separate CLI send buffer
78
79 // The number of keys sent to the usb in the array
80          uint8_t USBKeys_Sent    = 0;
81          uint8_t USBKeys_SentCLI = 0;
82
83 // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
84 volatile uint8_t USBKeys_LEDs = 0;
85
86 // Protocol setting from the host.
87 // 0 - Boot Mode (Default, until set by the host)
88 // 1 - NKRO Mode
89 volatile uint8_t USBKeys_Protocol = 1;
90
91 // the idle configuration, how often we send the report to the
92 // host (ms * 4) even when it hasn't changed
93          uint8_t USBKeys_Idle_Config = 125;
94
95 // count until idle timeout
96          uint8_t USBKeys_Idle_Count = 0;
97
98
99 // ----- Functions -----
100
101 // USB Module Setup
102 inline void Output_setup()
103 {
104         // Initialize the USB, and then wait for the host to set configuration.
105         // If the Teensy is powered without a PC connected to the USB port,
106         // this will wait forever.
107         usb_init();
108         while ( !usb_configured() ) /* wait */ ;
109
110         // Register USB Output CLI dictionary
111         CLI_registerDictionary( outputCLIDict, outputCLIDictName );
112
113         // Wait an extra second for the PC's operating system to load drivers
114         // and do whatever it does to actually be ready for input
115         //_delay_ms(1000); // TODO (is this actually necessary?)
116 }
117
118
119 // USB Data Send
120 inline void Output_send(void)
121 {
122                 // TODO undo potentially old keys
123                 for ( uint8_t c = USBKeys_Sent; c < USBKeys_MaxSize; c++ )
124                         USBKeys_Array[c] = 0;
125
126                 // Send keypresses
127                 usb_keyboard_send();
128
129                 // Clear modifiers and keys
130                 USBKeys_Modifiers = 0;
131                 USBKeys_Sent      = 0;
132
133                 // Signal Scan Module we are finishedA
134                 Scan_finishedWithUSBBuffer( USBKeys_Sent <= USBKeys_MaxSize ? USBKeys_Sent : USBKeys_MaxSize );
135 }
136
137
138 // Sets the device into firmware reload mode
139 inline void Output_firmwareReload()
140 {
141 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
142         usb_debug_reload();
143 #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_)
144         usb_device_reload();
145 #endif
146 }
147
148
149 // USB Input buffer available
150 inline unsigned int Output_availablechar()
151 {
152         return usb_serial_available();
153 }
154
155
156 // USB Get Character from input buffer
157 inline int Output_getchar()
158 {
159 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
160         // XXX Make sure to check output_availablechar() first! Information is lost with the cast (error codes)
161         return (int)usb_serial_getchar();
162 #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_)
163         return usb_serial_getchar();
164 #endif
165 }
166
167
168 // USB Send Character to output buffer
169 inline int Output_putchar( char c )
170 {
171         return usb_serial_putchar( c );
172 }
173
174
175 // USB Send String to output buffer, null terminated
176 inline int Output_putstr( char* str )
177 {
178 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
179         uint16_t count = 0;
180 #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // ARM
181         uint32_t count = 0;
182 #endif
183         // Count characters until NULL character, then send the amount counted
184         while ( str[count] != '\0' )
185                 count++;
186
187         return usb_serial_write( str, count );
188 }
189
190
191 // Soft Chip Reset
192 inline void Output_softReset()
193 {
194 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
195         usb_debug_software_reset();
196 #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_)
197         SOFTWARE_RESET();
198 #endif
199 }
200
201
202 // ----- CLI Command Functions -----
203
204 void cliFunc_kbdProtocol( char* args )
205 {
206         print( NL );
207         info_msg("Keyboard Protocol: ");
208         printInt8( USBKeys_Protocol );
209 }
210
211
212 void cliFunc_readLEDs( char* args )
213 {
214         print( NL );
215         info_msg("LED State (This doesn't work yet...): ");
216         printInt8( USBKeys_LEDs );
217 }
218
219
220 void cliFunc_sendKeys( char* args )
221 {
222         // Copy USBKeys_ArrayCLI to USBKeys_Array
223         for ( uint8_t key = 0; key < USBKeys_SentCLI; ++key )
224         {
225                 USBKeys_Array[key] = USBKeys_ArrayCLI[key];
226         }
227         USBKeys_Sent = USBKeys_SentCLI;
228
229         // Set modifier byte
230         USBKeys_Modifiers = USBKeys_ModifiersCLI;
231 }
232
233
234 void cliFunc_setKeys( char* args )
235 {
236         char* curArgs;
237         char* arg1Ptr;
238         char* arg2Ptr = args;
239
240         // Parse up to USBKeys_MaxSize args (whichever is least)
241         for ( USBKeys_SentCLI = 0; USBKeys_SentCLI < USBKeys_MaxSize; ++USBKeys_SentCLI )
242         {
243                 curArgs = arg2Ptr;
244                 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
245
246                 // Stop processing args if no more are found
247                 if ( *arg1Ptr == '\0' )
248                         break;
249
250                 // Add the USB code to be sent
251                 USBKeys_ArrayCLI[USBKeys_SentCLI] = decToInt( arg1Ptr );
252         }
253 }
254
255
256 void cliFunc_setMod( char* args )
257 {
258         // Parse number from argument
259         //  NOTE: Only first argument is used
260         char* arg1Ptr;
261         char* arg2Ptr;
262         CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
263
264         USBKeys_ModifiersCLI = decToInt( arg1Ptr );
265 }
266