1 /* Copyright (C) 2011-2014 by Jacob Alexander
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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
22 // ----- Includes -----
25 #include <Lib/MainLib.h>
29 #include <scan_loop.h>
30 #include <output_com.h>
38 // ----- Defines -----
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
46 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
47 #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
52 // ----- Function Declarations -----
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 );
63 // ----- Variables -----
65 // Timer Interrupt for flagging a send of the sampled key detection data to the USB host
66 uint16_t sendKeypressCounter = 0;
68 // Flag generated by the timer interrupt
69 volatile uint8_t sendKeypresses = 0;
73 // ----- Functions -----
75 // Initial Pin Setup, make sure they are sane
76 inline void pinSetup(void)
80 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
82 // For each pin, 0=input, 1=output
83 #if defined(__AVR_AT90USB1286__)
93 // Setting pins to either high or pull-up resistor
94 #if defined(__AVR_AT90USB1286__)
104 #elif defined(_mk20dx128_)
105 // TODO - Should be cleared, but not that necessary due to the pin layout
110 inline void usbTimerSetup(void)
113 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
115 // Setup with 16 MHz clock
118 // Setup ISR Timer for flagging a kepress send to USB
119 // Set to 256 * 1024 (8 bit timer with Clock/1024 prescalar) timer
122 TIMSK0 = (1 << TOIE0);
125 #elif defined(_mk20dx128_)
126 // 48 MHz clock by default
128 // System Clock Gating Register Disable
129 SIM_SCGC6 |= SIM_SCGC6_PIT;
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
139 // Insert the required vector for Timer 0
140 NVIC_ENABLE_IRQ( IRQ_PIT_CH0 );
151 // Setup Output Module
157 // Setup ISR Timer for flagging a kepress send to USB
160 // Main Detection Loop
161 uint8_t ledTimer = F_CPU / 1000000; // Enable LED for a short time
164 // Setup the scanning module
169 // Acquire Key Indices
170 // Loop continuously until scan_loop returns 0
172 while ( scan_loop() );
175 // Run Macros over Key Indices and convert to USB Keys
178 // Send keypresses over USB if the ISR has signalled that it's time
179 if ( !sendKeypresses )
185 // Clear sendKeypresses Flag
188 // Indicate Error, if valid
189 errorLED( ledTimer );
195 // Loop should never get here (indicate error)
198 // HID Debug Error message
199 erro_print("Detection loop error, this is very bad...bug report!");
204 // ----- Interrupts -----
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
213 sendKeypressCounter++;
214 if ( sendKeypressCounter > USB_TRANSFER_DIVIDER ) {
215 sendKeypressCounter = 0;
219 #if defined(_mk20dx128_) // ARM
220 // Clear the interrupt flag
226 // ----- CLI Command Functions -----
228 void cliFunc_free( char* args )
233 void cliFunc_gaugeHelp( char* args )
238 void cliFunc_single( char* args )
243 void cliFunc_start( char* args )
248 void cliFunc_zeroForce( char* args )
253 void cliFunc_zeroPosition( char* args )