]> git.donarmstrong.com Git - kiibohd-controller.git/blob - main.c
Matrix scanning for ARM now functional.
[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 // ----- Variables -----
53
54 // Timer Interrupt for flagging a send of the sampled key detection data to the USB host
55 uint16_t sendKeypressCounter = 0;
56
57 // Flag generated by the timer interrupt
58 volatile uint8_t sendKeypresses = 0;
59
60
61
62 // ----- Functions -----
63
64 inline void usbTimerSetup()
65 {
66 // AVR
67 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
68
69         // Setup with 16 MHz clock
70         CPU_PRESCALE( 0 );
71
72         // Setup ISR Timer for flagging a kepress send to USB
73         // Set to 256 * 1024 (8 bit timer with Clock/1024 prescalar) timer
74         TCCR0A = 0x00;
75         TCCR0B = 0x03;
76         TIMSK0 = (1 << TOIE0);
77
78 // ARM
79 #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // ARM
80         // 48 MHz clock by default
81
82         // System Clock Gating Register Disable
83         SIM_SCGC6 |= SIM_SCGC6_PIT;
84
85         // Enable Timers
86         PIT_MCR = 0x00;
87
88         // Setup ISR Timer for flagging a kepress send to USB
89         // 1 ms / (1 / 48 MHz) - 1 = 47999 cycles -> 0xBB7F
90         PIT_LDVAL0 = 0x0000BB7F;
91         PIT_TCTRL0 = 0x3; // Enable Timer 0 interrupts, and Enable Timer 0
92
93         // Insert the required vector for Timer 0
94         NVIC_ENABLE_IRQ( IRQ_PIT_CH0 );
95 #endif
96 }
97
98
99 int main()
100 {
101         // Enable CLI
102         CLI_init();
103
104         // Setup Modules
105         Output_setup();
106         Macro_setup();
107         Scan_setup();
108
109         // Setup ISR Timer for flagging a kepress send to USB
110         usbTimerSetup();
111
112         // Main Detection Loop
113         while ( 1 )
114         {
115                 // Process CLI
116                 CLI_process();
117
118                 // Acquire Key Indices
119                 // Loop continuously until scan_loop returns 0
120                 cli();
121                 while ( Scan_loop() );
122                 sei();
123
124                 // Run Macros over Key Indices and convert to USB Keys
125                 Macro_process();
126
127                 // Send keypresses over USB if the ISR has signalled that it's time
128                 if ( !sendKeypresses )
129                         continue;
130
131                 // Send USB Data
132                 Output_send();
133
134                 // Clear sendKeypresses Flag
135                 sendKeypresses = 0;
136         }
137 }
138
139
140 // ----- Interrupts -----
141
142 // USB Keyboard Data Send Counter Interrupt
143 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
144 ISR( TIMER0_OVF_vect )
145 #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // ARM
146 void pit0_isr()
147 #endif
148 {
149         sendKeypressCounter++;
150         if ( sendKeypressCounter > USB_TRANSFER_DIVIDER ) {
151                 sendKeypressCounter = 0;
152                 sendKeypresses = 1;
153         }
154
155 #if defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) // ARM
156         // Clear the interrupt flag
157         PIT_TFLG0 = 1;
158 #endif
159 }
160