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_stop ( char* args );
60 void cliFunc_zeroForce ( char* args );
61 void cliFunc_zeroPosition( char* args );
63 char receiveUART0Char();
65 void transmitUART0String( char* str );
67 uint32_t readDistanceGauge();
71 // ----- Variables -----
73 // Timer Interrupt for flagging a send of the sampled key detection data to the USB host
74 uint16_t sendKeypressCounter = 0;
76 // Flag generated by the timer interrupt
77 volatile uint8_t sendKeypresses = 0;
81 // ----- Functions -----
83 // Initial Pin Setup, make sure they are sane
84 inline void pinSetup(void)
88 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
90 // For each pin, 0=input, 1=output
91 #if defined(__AVR_AT90USB1286__)
101 // Setting pins to either high or pull-up resistor
102 #if defined(__AVR_AT90USB1286__)
112 #elif defined(_mk20dx128_)
113 // TODO - Should be cleared, but not that necessary due to the pin layout
118 inline void usbTimerSetup(void)
121 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
123 // Setup with 16 MHz clock
126 // Setup ISR Timer for flagging a kepress send to USB
127 // Set to 256 * 1024 (8 bit timer with Clock/1024 prescalar) timer
130 TIMSK0 = (1 << TOIE0);
133 #elif defined(_mk20dx128_)
134 // 48 MHz clock by default
136 // System Clock Gating Register Disable
137 SIM_SCGC6 |= SIM_SCGC6_PIT;
142 // Setup ISR Timer for flagging a kepress send to USB
143 // 1 ms / (1 / 48 MHz) - 1 = 47999 cycles -> 0xBB7F
144 PIT_LDVAL0 = 0x0000BB7F;
145 PIT_TCTRL0 = 0x3; // Enable Timer 0 interrupts, and Enable Timer 0
147 // Insert the required vector for Timer 0
148 NVIC_ENABLE_IRQ( IRQ_PIT_CH0 );
159 // Setup Output Module
165 // Setup ISR Timer for flagging a kepress send to USB
168 // Main Detection Loop
169 uint8_t ledTimer = F_CPU / 1000000; // Enable LED for a short time
172 // Setup the scanning module
177 // Acquire Key Indices
178 // Loop continuously until scan_loop returns 0
180 while ( scan_loop() );
183 // Run Macros over Key Indices and convert to USB Keys
186 // Send keypresses over USB if the ISR has signalled that it's time
187 if ( !sendKeypresses )
193 // Clear sendKeypresses Flag
196 // Indicate Error, if valid
197 errorLED( ledTimer );
203 // Loop should never get here (indicate error)
206 // HID Debug Error message
207 erro_print("Detection loop error, this is very bad...bug report!");
212 // ----- Interrupts -----
214 // USB Keyboard Data Send Counter Interrupt
215 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
216 ISR( TIMER0_OVF_vect )
217 #elif defined(_mk20dx128_) // ARM
221 sendKeypressCounter++;
222 if ( sendKeypressCounter > USB_TRANSFER_DIVIDER ) {
223 sendKeypressCounter = 0;
227 #if defined(_mk20dx128_) // ARM
228 // Clear the interrupt flag
234 // ----- CLI Command Functions -----
236 uint32_t readDistanceGauge()
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
248 uint32_t distInput = 0;
250 // Make sure clock is low initially
251 GPIOC_PCOR |= (1<<2); // Set Clock low
253 // Scan each of the bits
254 for ( uint8_t bit = 0; bit < bits; bit++ )
257 GPIOC_PSOR |= (1<<2); // Set Clock high
259 // Delay for duty cycle
260 delayMicroseconds( high_delay );
263 GPIOC_PCOR |= (1<<2); // Set Clock low
266 distInput |= GPIOC_PDIR & (1<<1) ? (1 << bit) : 0;
268 // Delay for duty cycle
269 delayMicroseconds( low_delay );
275 void cliFunc_distRead( char* args )
277 // Parse number from argument
278 // NOTE: Only first argument is used
281 argumentIsolation_cli( args, &arg1Ptr, &arg2Ptr );
283 // Convert the argument into an int
284 int read_count = decToInt( arg1Ptr ) + 1;
286 // If no argument specified, default to 1 read
287 if ( *arg1Ptr == '\0' )
292 // Repeat reading as many times as specified in the argument
294 while ( --read_count > 0 )
296 // Prepare to print output
297 info_msg("Distance: ");
300 uint32_t distInput = readDistanceGauge() - distanceOffset;
303 printInt32( distInput );
306 // As per http://www.shumatech.com/web/21bit_protocol?page=0,1
307 // 21 bits is 2560 CPI (counts per inch) (C/inch)
309 // 2560 / 25.4 = 100.7874016... CPMM (C/mm)
311 // 1 count is 1/2560 = 0.000390625... inches
312 // 1 count is (1/2560) * 25.4 = 0.00992187500000000 mm = 9.92187500000000 um = 9921.87500000000 nm
313 // Since there are 21 bits (2 097 152 positions) converting to um is possible by multiplying by 1000
314 // which is 2 097 152 000, and within 32 bits (4 294 967 295).
315 // However, um is still not convenient, so 64 bits (18 446 744 073 709 551 615) is a more accurate alternative.
316 // For each nm there are 2 097 152 000 000 positions.
318 // mm is 2 097 152 : 0.009 921 875 000 mm : 32 bit
319 // um is 2 097 152 000 : 9.921 875 000 um : 32 bit (ideal acc. for 32 bit)
320 // nm is 2 097 152 000 000 : 9 921.875 000 nm : 64 bit
321 // pm is 2 097 152 000 000 000 : 9 921 875.000 pm : 64 bit (ideal acc. for 64 bit)
323 // XXX Apparently shumatech was sorta wrong about the 21 bits of usage
324 // Yes there are 21 bits, but the values only go from ~338 to ~30681 which is less than 16 bits...
325 // This means that the conversion at NM can use 32 bits :D
326 // It's been noted that the multiplier should be 100.6 (and that it could vary from scale to scale)
327 uint32_t distNM = distInput * 9921;;
328 uint32_t distUM = distNM / 1000;
329 uint32_t distMM = distUM / 1000;
332 printInt32( distMM );
334 printInt32( distUM );
336 printInt32( distNM );
341 // Only delay if still counting
342 if ( read_count > 1 )
348 void cliFunc_free( char* args )
350 // Set the forceDistanceRead to 1, which will read until start has passed twice
351 forceDistanceRead = 1;
355 void cliFunc_gaugeHelp( char* args )
358 "\033[1;32mForce Curve Gauge Help\033[0m" NL
359 " \033[1;33mUsage Overview\033[0m" NL
361 " \033[1;33mAdditional Command Details\033[0m" NL
362 " \033[1;35mdistRead\033[0m" NL
363 " Reads the current value from the distance gauge." NL
364 " If specified it will N repeated reads with a delay after each read. Useful for testing the distance gauge." NL
365 " e.g. \033[35mdistRead 250\033[0m" NL
366 " \033[1;35mfree\033[0m" NL
367 " Start free scanning force/distance reads." NL
368 " Will continue until the [start] distance point has been past twice." NL
369 " \033[1;35mimadaComm\033[0m" NL
370 " Sends a command to the Imada force gauge." NL
371 " e.g. \033[35mimadaComm D\033[0m" NL
372 " The commands supported by the gauge depends on the model. Listed below is for the DS2." NL
373 " K Select g units (default)" NL
374 " N Select N units" NL
375 " O Select oz units" NL
376 " P Select peak mode" NL
377 " T Select real time mode (default)" NL
378 " Z Zero out display/reading" NL
379 " Q Turn off power" NL
380 " E Read high/low set points" NL
381 " D Read data from force gauge" NL
382 " E\033[35mHHHHLLLL\033[0m" NL
383 " Set the high/low setpoints, ignore decimals" NL
384 " \033[35mHHHH\033[0m is 4 digit high, \033[35mLLLL\033[0m is 4 digit low" NL
385 " Responses from the above commands." NL
386 " R Command successful" NL
387 " E Error/Invalid Command" NL
388 " E\033[35mHHHHLLLL\033[0m" NL
389 " Current high/low setpoints" NL
390 " \033[35mHHHH\033[0m is 4 digit high, \033[35mLLLL\033[0m is 4 digit low" NL
391 " \033[35m[value][units][mode]\033[0m" NL
392 " Data read response" NL
393 " \033[35m[value]\033[0m is force currently showing on the display (peak or realtime)" NL
394 " \033[35m[units]\033[0m is the configured force units" NL
395 " \033[35m[mode]\033[0m is the current mode (peak or realtime)" NL
396 " \033[1;35mread\033[0m" NL
397 " Read the current force/distance value." NL
398 " If specified it will N repeated reads with a delay after each read." NL
399 " e.g. \033[35mread 125\033[0m" NL
400 " \033[1;35mstart\033[0m" NL
401 " Distance marker \033[35m[start]\033[0m for the start/end of a force curve measurement." NL
402 " While in free running mode, a special message is displayed when reaching the \033[35m[start]\033[0m point." NL
403 " \033[35m[start]\033[0m is defined by positioning the distance sensor at the position to start and running this command." NL
408 void cliFunc_read( char* args )
410 // Parse number from argument
411 // NOTE: Only first argument is used
414 argumentIsolation_cli( args, &arg1Ptr, &arg2Ptr );
416 // Convert the argument into an int
417 int read_count = decToInt( arg1Ptr ) + 1;
419 // If no argument specified, default to 1 read
420 if ( *arg1Ptr == '\0' )
425 // Set the overall read count to read_count
426 forceDistanceReadCount = read_count;
430 void cliFunc_start( char* args )
432 // Read the current distance and set the new start/end position
433 distanceStart = readDistanceGauge();
436 info_msg("New start/end position: ");
437 printInt32( distanceStart - distanceOffset );
441 void cliFunc_stop( char* args )
443 // Reset the forceDistanceRead and forceDistanceReadCount
444 forceDistanceRead = 0;
445 forceDistanceReadCount = 0;
449 void cliFunc_zeroForce( char* args )
451 // Just use the imadaComm command sending the needed argument
452 char* commandArg = "Z";
453 imadaVerboseRead( commandArg );
457 void cliFunc_zeroPosition( char* args )
459 // Read the current distance and set the new offset
460 distanceOffset = readDistanceGauge();
463 info_msg("New distance offset: ");
464 printInt32( distanceOffset );