]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Output/uartOut/output_com.c
Initial code for ARM UART output module (mainly for CLI)
[kiibohd-controller.git] / Output / uartOut / output_com.c
1 /* Copyright (C) 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/uart_serial.h"
35 #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_)
36 #include "arm/uart_serial.h"
37 #endif
38
39 // Local Includes
40 #include "output_com.h"
41
42
43
44 // ----- Function Declarations -----
45
46 void cliFunc_kbdProtocol( char* args );
47 void cliFunc_readLEDs   ( char* args );
48 void cliFunc_sendKeys   ( char* args );
49 void cliFunc_setKeys    ( char* args );
50 void cliFunc_setMod     ( char* args );
51
52
53 // ----- Variables -----
54
55 // Output Module command dictionary
56 char*       outputCLIDictName = "USB Module Commands - NOT WORKING";
57 CLIDictItem outputCLIDict[] = {
58         { "kbdProtocol", "Keyboard Protocol Mode: 0 - Boot, 1 - OS/NKRO Mode", cliFunc_kbdProtocol },
59         { "readLEDs",    "Read LED byte:" NL "\t\t1 NumLck, 2 CapsLck, 4 ScrlLck, 16 Kana, etc.", cliFunc_readLEDs },
60         { "sendKeys",    "Send the prepared list of USB codes and modifier byte.", cliFunc_sendKeys },
61         { "setKeys",     "Prepare a space separated list of USB codes (decimal). Waits until \033[35msendKeys\033[0m.", cliFunc_setKeys },
62         { "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 },
63         { 0, 0, 0 } // Null entry for dictionary end
64 };
65
66
67 // Which modifier keys are currently pressed
68 // 1=left ctrl,    2=left shift,   4=left alt,    8=left gui
69 // 16=right ctrl, 32=right shift, 64=right alt, 128=right gui
70          uint8_t USBKeys_Modifiers    = 0;
71          uint8_t USBKeys_ModifiersCLI = 0; // Separate CLI send buffer
72
73 // Currently pressed keys, max is defined by USB_MAX_KEY_SEND
74          uint8_t USBKeys_Array   [USB_MAX_KEY_SEND];
75          uint8_t USBKeys_ArrayCLI[USB_MAX_KEY_SEND]; // Separate CLI send buffer
76
77 // The number of keys sent to the usb in the array
78          uint8_t USBKeys_Sent    = 0;
79          uint8_t USBKeys_SentCLI = 0;
80
81 // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
82 volatile uint8_t USBKeys_LEDs = 0;
83
84 // Protocol setting from the host.
85 // 0 - Boot Mode (Default, until set by the host)
86 // 1 - NKRO Mode
87 volatile uint8_t USBKeys_Protocol = 1;
88
89 // the idle configuration, how often we send the report to the
90 // host (ms * 4) even when it hasn't changed
91          uint8_t USBKeys_Idle_Config = 125;
92
93 // count until idle timeout
94          uint8_t USBKeys_Idle_Count = 0;
95
96
97 // ----- Functions -----
98
99 // USB Module Setup
100 inline void Output_setup()
101 {
102         // Setup UART
103         uart_serial_setup();
104
105         // Register USB Output CLI dictionary
106         CLI_registerDictionary( outputCLIDict, outputCLIDictName );
107 }
108
109
110 // USB Data Send
111 inline void Output_send(void)
112 {
113         // TODO
114 }
115
116
117 // Sets the device into firmware reload mode
118 inline void Output_firmwareReload()
119 {
120 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
121         uart_debug_reload();
122 #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_)
123         uart_device_reload();
124 #endif
125 }
126
127
128 // USB Input buffer available
129 inline unsigned int Output_availablechar()
130 {
131         return uart_serial_available();
132 }
133
134
135 // USB Get Character from input buffer
136 inline int Output_getchar()
137 {
138 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
139         // XXX Make sure to check output_availablechar() first! Information is lost with the cast (error codes)
140         return (int)uart_serial_getchar();
141 #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_)
142         return uart_serial_getchar();
143 #endif
144 }
145
146
147 // USB Send Character to output buffer
148 inline int Output_putchar( char c )
149 {
150         return uart_serial_putchar( c );
151 }
152
153
154 // USB Send String to output buffer, null terminated
155 inline int Output_putstr( char* str )
156 {
157 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
158         uint16_t count = 0;
159 #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // ARM
160         uint32_t count = 0;
161 #endif
162         // Count characters until NULL character, then send the amount counted
163         while ( str[count] != '\0' )
164                 count++;
165
166         return uart_serial_write( str, count );
167 }
168
169
170 // Soft Chip Reset
171 inline void Output_softReset()
172 {
173 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
174         uart_debug_software_reset();
175 #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_)
176         SOFTWARE_RESET();
177 #endif
178 }
179
180
181 // ----- CLI Command Functions -----
182
183 void cliFunc_kbdProtocol( char* args )
184 {
185         print( NL );
186         info_msg("Keyboard Protocol: ");
187         printInt8( USBKeys_Protocol );
188 }
189
190
191 void cliFunc_readLEDs( char* args )
192 {
193         print( NL );
194         info_msg("LED State (This doesn't work yet...): ");
195         printInt8( USBKeys_LEDs );
196 }
197
198
199 void cliFunc_sendKeys( char* args )
200 {
201         // Copy USBKeys_ArrayCLI to USBKeys_Array
202         for ( uint8_t key = 0; key < USBKeys_SentCLI; ++key )
203         {
204                 USBKeys_Array[key] = USBKeys_ArrayCLI[key];
205         }
206         USBKeys_Sent = USBKeys_SentCLI;
207
208         // Set modifier byte
209         USBKeys_Modifiers = USBKeys_ModifiersCLI;
210 }
211
212
213 void cliFunc_setKeys( char* args )
214 {
215         char* curArgs;
216         char* arg1Ptr;
217         char* arg2Ptr = args;
218
219         // Parse up to USBKeys_MaxSize args (whichever is least)
220         for ( USBKeys_SentCLI = 0; USBKeys_SentCLI < USBKeys_MaxSize; ++USBKeys_SentCLI )
221         {
222                 curArgs = arg2Ptr;
223                 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
224
225                 // Stop processing args if no more are found
226                 if ( *arg1Ptr == '\0' )
227                         break;
228
229                 // Add the USB code to be sent
230                 USBKeys_ArrayCLI[USBKeys_SentCLI] = decToInt( arg1Ptr );
231         }
232 }
233
234
235 void cliFunc_setMod( char* args )
236 {
237         // Parse number from argument
238         //  NOTE: Only first argument is used
239         char* arg1Ptr;
240         char* arg2Ptr;
241         CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
242
243         USBKeys_ModifiersCLI = decToInt( arg1Ptr );
244 }
245