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_distRead ( char* args );
55 void cliFunc_free ( char* args );
56 void cliFunc_gaugeHelp ( char* args );
57 void cliFunc_single ( char* args );
58 void cliFunc_start ( char* args );
59 void cliFunc_zeroForce ( char* args );
60 void cliFunc_zeroPosition( char* args );
64 // ----- Variables -----
66 // Timer Interrupt for flagging a send of the sampled key detection data to the USB host
67 uint16_t sendKeypressCounter = 0;
69 // Flag generated by the timer interrupt
70 volatile uint8_t sendKeypresses = 0;
74 // ----- Functions -----
76 // Initial Pin Setup, make sure they are sane
77 inline void pinSetup(void)
81 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
83 // For each pin, 0=input, 1=output
84 #if defined(__AVR_AT90USB1286__)
94 // Setting pins to either high or pull-up resistor
95 #if defined(__AVR_AT90USB1286__)
105 #elif defined(_mk20dx128_)
106 // TODO - Should be cleared, but not that necessary due to the pin layout
111 inline void usbTimerSetup(void)
114 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
116 // Setup with 16 MHz clock
119 // Setup ISR Timer for flagging a kepress send to USB
120 // Set to 256 * 1024 (8 bit timer with Clock/1024 prescalar) timer
123 TIMSK0 = (1 << TOIE0);
126 #elif defined(_mk20dx128_)
127 // 48 MHz clock by default
129 // System Clock Gating Register Disable
130 SIM_SCGC6 |= SIM_SCGC6_PIT;
135 // Setup ISR Timer for flagging a kepress send to USB
136 // 1 ms / (1 / 48 MHz) - 1 = 47999 cycles -> 0xBB7F
137 PIT_LDVAL0 = 0x0000BB7F;
138 PIT_TCTRL0 = 0x3; // Enable Timer 0 interrupts, and Enable Timer 0
140 // Insert the required vector for Timer 0
141 NVIC_ENABLE_IRQ( IRQ_PIT_CH0 );
152 // Setup Output Module
158 // Setup ISR Timer for flagging a kepress send to USB
161 // Main Detection Loop
162 uint8_t ledTimer = F_CPU / 1000000; // Enable LED for a short time
165 // Setup the scanning module
170 // Acquire Key Indices
171 // Loop continuously until scan_loop returns 0
173 while ( scan_loop() );
176 // Run Macros over Key Indices and convert to USB Keys
179 // Send keypresses over USB if the ISR has signalled that it's time
180 if ( !sendKeypresses )
186 // Clear sendKeypresses Flag
189 // Indicate Error, if valid
190 errorLED( ledTimer );
196 // Loop should never get here (indicate error)
199 // HID Debug Error message
200 erro_print("Detection loop error, this is very bad...bug report!");
205 // ----- Interrupts -----
207 // USB Keyboard Data Send Counter Interrupt
208 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
209 ISR( TIMER0_OVF_vect )
210 #elif defined(_mk20dx128_) // ARM
214 sendKeypressCounter++;
215 if ( sendKeypressCounter > USB_TRANSFER_DIVIDER ) {
216 sendKeypressCounter = 0;
220 #if defined(_mk20dx128_) // ARM
221 // Clear the interrupt flag
227 // ----- CLI Command Functions -----
229 void cliFunc_distRead( char* args )
231 // Prepare to print output
233 info_msg("Distance: ");
236 uint32_t distInput = 0;
238 // Setup distance read parameters for iGaging Distance Scale
241 // high_delay = (1/freq) * (duty_cycle/100)
242 // low_delay = (1/freq) * ((100-duty_cycle)/100)
243 uint8_t bits = 21; // 21 clock pulses, for 21 bits
244 //uint32_t high_delay = 22; // Clock high time per pulse
245 //uint32_t low_delay = 89; // Clock low time per pulse
246 uint32_t high_delay = 40; // Clock high time per pulse
247 uint32_t low_delay = 60; // Clock low time per pulse
249 // Make sure clock is low initially
250 GPIOC_PCOR |= (1<<2); // Set Clock low
255 // Scan each of the bits
256 for ( uint8_t bit = bits; bit > 0; bit-- )
259 GPIOC_PSOR |= (1<<2); // Set Clock high
261 // Delay for duty cycle
262 delayMicroseconds( high_delay );
265 GPIOC_PCOR |= (1<<2); // Set Clock low
268 //distInput |= GPIOD_PDIR & (1<<6) ? (1 << (bit - 1)) : 0;
270 if ( GPIOD_PDIR & (1<<6) )
279 // Delay for duty cycle
280 delayMicroseconds( low_delay );
285 printInt32( distInput );
288 // As per http://www.shumatech.com/web/21bit_protocol?page=0,1
289 // 21 bits is 2560 CPI (counts per inch) (C/inch)
291 // 2560 / 25.4 = 100.7874016... CPMM (C/mm)
293 // 1 count is 1/2560 = 0.000390625... inches
294 // 1 count is (1/2560) * 25.4 = 0.0000153789370078740 mm = 0.0153789370078740 um = 15.3789370078740 nm
295 // Since there are 21 bits (2 097 152 positions) converting to um is possible by multiplying by 1000
296 // which is 2 097 152 000, and within 32 bits (4 294 967 295).
297 // However, um is still not convenient, so 64 bits (18 446 744 073 709 551 615) is a more accurate alternative.
298 // For each nm there are 2 097 152 000 000 positions.
300 // pm is 2 097 152 : 0.000 015 378 937 007 874 0 mm : 32 bit
301 // pm is 2 097 152 000 : 0.015 378 937 007 874 0 um : 32 bit (ideal acc. for 32 bit)
302 // pm is 2 097 152 000 000 : 15.378 937 007 874 0 nm : 64 bit
303 // pm is 2 097 152 000 000 000 : 15 378.937 007 874 0 pm : 64 bit
304 // fm is 2 097 152 000 000 000 000 : 15 378 937.007 874 0 fm : 64 bit (ideal acc. for 64 bit)
305 //uint64_t distNM = distInput * 15;
306 //uint64_t distPM = distInput * 15378;
307 uint64_t distFM = distInput * 15378937;
309 // Calculate um and mm
310 //uint32_t distNM = distInput * 15; // XXX
311 //uint32_t distUM = distNM / 1000;
312 //uint32_t distMM = distNM / 1000000;
313 uint32_t distNM = distFM * 1000000;
314 uint32_t distUM = distNM / 1000;
315 uint32_t distMM = distUM / 1000;
318 printInt32( distMM );
320 printInt32( distUM );
322 printInt32( distNM );
335 void cliFunc_free( char* args )
340 void cliFunc_gaugeHelp( char* args )
345 void cliFunc_single( char* args )
350 void cliFunc_start( char* args )
355 void cliFunc_zeroForce( char* args )
360 void cliFunc_zeroPosition( char* args )