]> git.donarmstrong.com Git - kiibohd-controller.git/blob - main.c
Adding initial Teensy 3 support, compiles, but not fully functional yet.
[kiibohd-controller.git] / main.c
1 /* Copyright (C) 2011-2013 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 <usb_com.h>
31
32 #include <led.h>
33 #include <print.h>
34
35
36
37 // ----- Defines -----
38
39 // Verified Keypress Defines
40 #define USB_TRANSFER_DIVIDER 10 // 1024 == 1 Send of keypresses per second, 1 == 1 Send of keypresses per ~1 millisecond
41
42
43
44 // ----- Macros -----
45 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
46 #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
47 #endif
48
49
50
51 // ----- Variables -----
52
53 // Timer Interrupt for flagging a send of the sampled key detection data to the USB host
54 uint16_t sendKeypressCounter = 0;
55
56 // Flag generated by the timer interrupt
57 volatile uint8_t sendKeypresses = 0;
58
59
60
61 // ----- Functions -----
62
63 // Initial Pin Setup, make sure they are sane
64 inline void pinSetup(void)
65 {
66
67 // AVR
68 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
69
70         // For each pin, 0=input, 1=output
71 #if defined(__AVR_AT90USB1286__)
72         DDRA = 0x00;
73 #endif
74         DDRB = 0x00;
75         DDRC = 0x00;
76         DDRD = 0x00;
77         DDRE = 0x00;
78         DDRF = 0x00;
79
80
81         // Setting pins to either high or pull-up resistor
82 #if defined(__AVR_AT90USB1286__)
83         PORTA = 0x00;
84 #endif
85         PORTB = 0x00;
86         PORTC = 0x00;
87         PORTD = 0x00;
88         PORTE = 0x00;
89         PORTF = 0x00;
90
91 // ARM
92 #elif defined(_mk20dx128_)
93         // TODO
94 #endif
95 }
96
97
98 inline void usbTimerSetup(void)
99 {
100 // AVR
101 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
102
103         // Setup with 16 MHz clock
104         CPU_PRESCALE( 0 );
105
106         // Setup ISR Timer for flagging a kepress send to USB
107         // Set to 256 * 1024 (8 bit timer with Clock/1024 prescalar) timer
108         TCCR0A = 0x00;
109         TCCR0B = 0x03;
110         TIMSK0 = (1 << TOIE0);
111
112 // ARM
113 #elif defined(_mk20dx128_)
114         // 48 MHz clock by default
115
116         // Enable Timers
117         /* TODO Fixme!!
118         PIT_MCR = 0x00;
119
120         // Setup ISR Timer for flagging a kepress send to USB
121         // 1 ms / (1 / 48 MHz) - 1 = 47999 cycles -> 0xBB7F
122         PIT_LDVAL0 = 0x0000BB7F;
123         PIT_TCTRL0 = 0x3; // Enable Timer 0 interrupts, and Enable Timer 0
124         */
125 #endif
126 }
127
128
129 int main(void)
130 {
131         // Configuring Pins
132         pinSetup();
133         init_errorLED();
134
135         // Setup USB Module
136         usb_setup();
137
138         print("TEST");
139         // Setup ISR Timer for flagging a kepress send to USB
140         usbTimerSetup();
141
142         // Main Detection Loop
143         uint8_t ledTimer = 15; // Enable LED for a short time
144         while ( 1 )
145         {
146                 // Setup the scanning module
147                 scan_setup();
148
149                 while ( 1 )
150                 {
151                         // Acquire Key Indices
152                         // Loop continuously until scan_loop returns 0
153                         cli();
154                         while ( scan_loop() );
155                         sei();
156
157                         // XXX DEBUG
158                         dPrint("AAAAAAA\r\n");
159                         print("AAAAAAB\r\n");
160
161                         // Run Macros over Key Indices and convert to USB Keys
162                         process_macros();
163
164                         // Send keypresses over USB if the ISR has signalled that it's time
165                         if ( !sendKeypresses )
166                                 continue;
167
168                         // Send USB Data
169                         usb_send();
170
171                         // Clear sendKeypresses Flag
172                         sendKeypresses = 0;
173
174                         // Indicate Error, if valid
175                         errorLED( ledTimer );
176
177                         if ( ledTimer > 0 )
178                                 ledTimer--;
179                 }
180
181                 // Loop should never get here (indicate error)
182                 ledTimer = 255;
183
184                 // HID Debug Error message
185                 erro_print("Detection loop error, this is very bad...bug report!");
186         }
187 }
188
189
190 // ----- Interrupts -----
191
192 // USB Keyboard Data Send Counter Interrupt
193 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
194 ISR( TIMER0_OVF_vect )
195 #elif defined(_mk20dx128_) // ARM
196 void pit0_isr(void)
197 #endif
198 {
199         sendKeypressCounter++;
200         if ( sendKeypressCounter > USB_TRANSFER_DIVIDER ) {
201                 sendKeypressCounter = 0;
202                 sendKeypresses = 1;
203         }
204 }
205