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 uint32_t readDistanceGauge()
231 // Setup distance read parameters for iGaging Distance Scale
234 // high_delay = (1/freq) * (duty_cycle/100)
235 // low_delay = (1/freq) * ((100-duty_cycle)/100)
236 uint8_t bits = 21; // 21 clock pulses, for 21 bits
237 uint32_t high_delay = 22; // Clock high time per pulse
238 uint32_t low_delay = 89; // Clock low time per pulse
241 uint32_t distInput = 0;
243 // Make sure clock is low initially
244 GPIOC_PCOR |= (1<<2); // Set Clock low
246 // Scan each of the bits
247 for ( uint8_t bit = 0; bit < bits; bit++ )
250 GPIOC_PSOR |= (1<<2); // Set Clock high
252 // Delay for duty cycle
253 delayMicroseconds( high_delay );
256 GPIOC_PCOR |= (1<<2); // Set Clock low
259 distInput |= GPIOC_PDIR & (1<<1) ? (1 << bit) : 0;
261 // Delay for duty cycle
262 delayMicroseconds( low_delay );
268 void cliFunc_distRead( char* args )
270 // Parse number from argument
271 // NOTE: Only first argument is used
274 argumentIsolation_cli( args, &arg1Ptr, &arg2Ptr );
276 // Convert the argument into an int
277 int read_count = decToInt( arg1Ptr ) + 1;
279 // If no argument specified, default to 1 read
280 if ( *arg1Ptr == '\0' )
285 // Repeat reading as many times as specified in the argument
287 while ( --read_count > 0 )
289 // Prepare to print output
290 info_msg("Distance: ");
293 uint32_t distInput = readDistanceGauge();
296 printInt32( distInput );
299 // As per http://www.shumatech.com/web/21bit_protocol?page=0,1
300 // 21 bits is 2560 CPI (counts per inch) (C/inch)
302 // 2560 / 25.4 = 100.7874016... CPMM (C/mm)
304 // 1 count is 1/2560 = 0.000390625... inches
305 // 1 count is (1/2560) * 25.4 = 0.00992187500000000 mm = 9.92187500000000 um = 9921.87500000000 nm
306 // Since there are 21 bits (2 097 152 positions) converting to um is possible by multiplying by 1000
307 // which is 2 097 152 000, and within 32 bits (4 294 967 295).
308 // However, um is still not convenient, so 64 bits (18 446 744 073 709 551 615) is a more accurate alternative.
309 // For each nm there are 2 097 152 000 000 positions.
311 // mm is 2 097 152 : 0.009 921 875 000 mm : 32 bit
312 // um is 2 097 152 000 : 9.921 875 000 um : 32 bit (ideal acc. for 32 bit)
313 // nm is 2 097 152 000 000 : 9 921.875 000 nm : 64 bit
314 // pm is 2 097 152 000 000 000 : 9 921 875.000 pm : 64 bit (ideal acc. for 64 bit)
316 // XXX Apparently shumatech was sorta wrong about the 21 bits of usage
317 // Yes there are 21 bits, but the values only go from ~338 to ~30681 which is less than 16 bits...
318 // This means that the conversion at NM can use 32 bits :D
319 // It's been noted that the multiplier should be 100.6 (and that it could vary from scale to scale)
320 uint32_t distNM = distInput * 9921;;
321 uint32_t distUM = distNM / 1000;
322 uint32_t distMM = distUM / 1000;
325 printInt32( distMM );
327 printInt32( distUM );
329 printInt32( distNM );
334 // Only delay if still counting
335 if ( read_count > 1 )
341 void cliFunc_free( char* args )
346 void cliFunc_gaugeHelp( char* args )
351 void cliFunc_single( char* args )
356 void cliFunc_start( char* args )
361 void cliFunc_zeroForce( char* args )
366 void cliFunc_zeroPosition( char* args )