]> git.donarmstrong.com Git - kiibohd-controller.git/blob - main.c
Initial Force Gauge CLI commands.
[kiibohd-controller.git] / main.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/MainLib.h>
26
27 // Project Includes
28 #include <macro.h>
29 #include <scan_loop.h>
30 #include <output_com.h>
31
32 #include <cli.h>
33 #include <led.h>
34 #include <print.h>
35
36
37
38 // ----- Defines -----
39
40 // Verified Keypress Defines
41 #define USB_TRANSFER_DIVIDER 10 // 1024 == 1 Send of keypresses per second, 1 == 1 Send of keypresses per ~1 millisecond
42
43
44
45 // ----- Macros -----
46 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
47 #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
48 #endif
49
50
51
52 // ----- Function Declarations -----
53
54 void cliFunc_free        ( char* args );
55 void cliFunc_gaugeHelp   ( char* args );
56 void cliFunc_single      ( char* args );
57 void cliFunc_start       ( char* args );
58 void cliFunc_zeroForce   ( char* args );
59 void cliFunc_zeroPosition( char* args );
60
61
62
63 // ----- Variables -----
64
65 // Timer Interrupt for flagging a send of the sampled key detection data to the USB host
66 uint16_t sendKeypressCounter = 0;
67
68 // Flag generated by the timer interrupt
69 volatile uint8_t sendKeypresses = 0;
70
71
72
73 // ----- Functions -----
74
75 // Initial Pin Setup, make sure they are sane
76 inline void pinSetup(void)
77 {
78
79 // AVR
80 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
81
82         // For each pin, 0=input, 1=output
83 #if defined(__AVR_AT90USB1286__)
84         DDRA = 0x00;
85 #endif
86         DDRB = 0x00;
87         DDRC = 0x00;
88         DDRD = 0x00;
89         DDRE = 0x00;
90         DDRF = 0x00;
91
92
93         // Setting pins to either high or pull-up resistor
94 #if defined(__AVR_AT90USB1286__)
95         PORTA = 0x00;
96 #endif
97         PORTB = 0x00;
98         PORTC = 0x00;
99         PORTD = 0x00;
100         PORTE = 0x00;
101         PORTF = 0x00;
102
103 // ARM
104 #elif defined(_mk20dx128_)
105         // TODO - Should be cleared, but not that necessary due to the pin layout
106 #endif
107 }
108
109
110 inline void usbTimerSetup(void)
111 {
112 // AVR
113 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
114
115         // Setup with 16 MHz clock
116         CPU_PRESCALE( 0 );
117
118         // Setup ISR Timer for flagging a kepress send to USB
119         // Set to 256 * 1024 (8 bit timer with Clock/1024 prescalar) timer
120         TCCR0A = 0x00;
121         TCCR0B = 0x03;
122         TIMSK0 = (1 << TOIE0);
123
124 // ARM
125 #elif defined(_mk20dx128_)
126         // 48 MHz clock by default
127
128         // System Clock Gating Register Disable
129         SIM_SCGC6 |= SIM_SCGC6_PIT;
130
131         // Enable Timers
132         PIT_MCR = 0x00;
133
134         // Setup ISR Timer for flagging a kepress send to USB
135         // 1 ms / (1 / 48 MHz) - 1 = 47999 cycles -> 0xBB7F
136         PIT_LDVAL0 = 0x0000BB7F;
137         PIT_TCTRL0 = 0x3; // Enable Timer 0 interrupts, and Enable Timer 0
138
139         // Insert the required vector for Timer 0
140         NVIC_ENABLE_IRQ( IRQ_PIT_CH0 );
141 #endif
142 }
143
144
145 int main(void)
146 {
147         // Configuring Pins
148         pinSetup();
149         init_errorLED();
150
151         // Setup Output Module
152         output_setup();
153
154         // Enable CLI
155         init_cli();
156
157         // Setup ISR Timer for flagging a kepress send to USB
158         usbTimerSetup();
159
160         // Main Detection Loop
161         uint8_t ledTimer = F_CPU / 1000000; // Enable LED for a short time
162         while ( 1 )
163         {
164                 // Setup the scanning module
165                 scan_setup();
166
167                 while ( 1 )
168                 {
169                         // Acquire Key Indices
170                         // Loop continuously until scan_loop returns 0
171                         cli();
172                         while ( scan_loop() );
173                         sei();
174
175                         // Run Macros over Key Indices and convert to USB Keys
176                         process_macros();
177
178                         // Send keypresses over USB if the ISR has signalled that it's time
179                         if ( !sendKeypresses )
180                                 continue;
181
182                         // Send USB Data
183                         usb_send();
184
185                         // Clear sendKeypresses Flag
186                         sendKeypresses = 0;
187
188                         // Indicate Error, if valid
189                         errorLED( ledTimer );
190
191                         if ( ledTimer > 0 )
192                                 ledTimer--;
193                 }
194
195                 // Loop should never get here (indicate error)
196                 ledTimer = 255;
197
198                 // HID Debug Error message
199                 erro_print("Detection loop error, this is very bad...bug report!");
200         }
201 }
202
203
204 // ----- Interrupts -----
205
206 // USB Keyboard Data Send Counter Interrupt
207 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
208 ISR( TIMER0_OVF_vect )
209 #elif defined(_mk20dx128_) // ARM
210 void pit0_isr(void)
211 #endif
212 {
213         sendKeypressCounter++;
214         if ( sendKeypressCounter > USB_TRANSFER_DIVIDER ) {
215                 sendKeypressCounter = 0;
216                 sendKeypresses = 1;
217         }
218
219 #if defined(_mk20dx128_) // ARM
220         // Clear the interrupt flag
221         PIT_TFLG0 = 1;
222 #endif
223 }
224
225
226 // ----- CLI Command Functions -----
227
228 void cliFunc_free( char* args )
229 {
230 }
231
232
233 void cliFunc_gaugeHelp( char* args )
234 {
235 }
236
237
238 void cliFunc_single( char* args )
239 {
240 }
241
242
243 void cliFunc_start( char* args )
244 {
245 }
246
247
248 void cliFunc_zeroForce( char* args )
249 {
250 }
251
252
253 void cliFunc_zeroPosition( char* args )
254 {
255 }
256