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