1 /* Copyright (C) 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/ScanLib.h>
33 #include "scan_loop.h"
37 // ----- Defines -----
39 // ADC Clock divisor settings (F_BUS == 48000000)
40 #define ADC_CFG1_6MHZ ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1)
41 #define ADC_CFG1_12MHZ ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1)
42 #define ADC_CFG1_24MHZ ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(1)
50 // ----- Function Declarations -----
52 void cliFunc_adc ( char* args );
53 void cliFunc_adcInit( char* args );
54 void cliFunc_dac ( char* args );
55 void cliFunc_dacVref( char* args );
56 void cliFunc_echo ( char* args );
60 // ----- Variables -----
62 // Buffer used to inform the macro processing module which keys have been detected as pressed
63 volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
64 volatile uint8_t KeyIndex_BufferUsed;
67 // Scan Module command dictionary
68 char scanCLIDictName[] = "ADC Test Module Commands";
69 const CLIDictItem scanCLIDict[] = {
70 #if defined(_mk20dx128_) || defined(_mk20dx256_) // ARM
71 { "adc", "Read the specified number of values from the ADC at the given pin: <pin> [# of reads]"
72 NL "\t\t See \033[35mLib/pin_map.teensy3\033[0m for ADC0 channel number.", cliFunc_adc },
73 { "adcInit", "Intialize/calibrate ADC: <ADC Resolution> <Vref> <Hardware averaging samples>"
74 NL "\t\tADC Resolution -> 8, 10, 12, 16 (bit)"
75 NL "\t\t Vref -> 0 (1.2 V), 1 (External)"
76 NL "\t\tHw Avg Samples -> 0 (disabled), 4, 8, 16, 32", cliFunc_adcInit },
78 #if defined(_mk20dx256_) // DAC is only supported on Teensy 3.1
79 { "dac", "Set DAC output value, from 0 to 4095 (1/4096 Vref to Vref).", cliFunc_dac },
80 { "dacVref", "Set DAC Vref. 0 is 1.2V. 1 is 3.3V.", cliFunc_dacVref },
82 { "echo", "Example command, echos the arguments.", cliFunc_echo },
83 { 0, 0, 0 } // Null entry for dictionary end
88 // ----- Functions -----
91 inline void Scan_setup()
92 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
94 // Register Scan CLI dictionary
95 CLI_registerDictionary( scanCLIDict, scanCLIDictName );
97 #elif defined(_mk20dx128_) || defined(_mk20dx256_) // ARM
99 // Register Scan CLI dictionary
100 CLI_registerDictionary( scanCLIDict, scanCLIDictName );
104 VREF_SC = 0xE1; // Enable 1.2V Vref
106 #if defined(_mk20dx256_) // DAC is only supported on Teensy 3.1
108 SIM_SCGC2 |= SIM_SCGC2_DAC0;
109 DAC0_C0 = DAC_C0_DACEN | DAC_C0_DACRFS; // 3.3V VDDA is DACREF_2
115 // Main Detection Loop
116 inline uint8_t Scan_loop()
122 // Signal KeyIndex_Buffer that it has been properly read
123 void Scan_finishedWithBuffer( uint8_t sentKeys )
128 // Signal that the keys have been properly sent over USB
129 void Scan_finishedWithUSBBuffer( uint8_t sentKeys )
135 void Scan_resetKeyboard()
140 // ----- CLI Command Functions -----
142 // XXX Just an example command showing how to parse arguments (more complex than generally needed)
143 void cliFunc_echo( char* args )
147 char* arg2Ptr = args;
149 // Parse args until a \0 is found
152 print( NL ); // No \r\n by default after the command is entered
154 curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
155 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
157 // Stop processing args if no more are found
158 if ( *arg1Ptr == '\0' )
166 void cliFunc_adc( char* args )
167 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
170 #elif defined(_mk20dx128_) || defined(_mk20dx256_) // ARM
172 // Parse code from argument
173 // NOTE: Only first argument is used
176 CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
178 // Set the ADC Channel
179 uint8_t channel = decToInt( arg1Ptr );
184 // Number of ADC samples to display
185 CLI_argumentIsolation( arg2Ptr, &arg1Ptr, &arg2Ptr );
187 int displayedADC = 1; // Default to 1 read
188 if ( arg1Ptr ) // If there is an argument, use that instead
190 displayedADC = decToInt( arg1Ptr );
193 // Poll ADC until it gets a value, making sure to serve interrupts on each attempt
194 while ( displayedADC > 0 )
198 // ADC Sample is ready
199 if ( (ADC0_SC1A & ADC_SC1_COCO) )
201 int result = ADC0_RA;
203 printInt32( result );
206 // Prepare for another read
207 if ( displayedADC > 0 )
214 yield(); // Make sure interrupts actually get serviced
219 void cliFunc_adcInit( char* args )
220 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
223 #elif defined(_mk20dx128_) || defined(_mk20dx256_) // ARM
225 // Parse code from argument
226 // NOTE: Only first argument is used
229 CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
231 // Make sure calibration has stopped
234 // Select bit resolution
235 int bitResolution = decToInt( arg1Ptr );
236 switch ( bitResolution )
239 ADC0_CFG1 = ADC_CFG1_24MHZ + ADC_CFG1_MODE(0);
240 ADC0_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(3);
244 ADC0_CFG1 = ADC_CFG1_12MHZ + ADC_CFG1_MODE(2) + ADC_CFG1_ADLSMP;
245 ADC0_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(3);
249 ADC0_CFG1 = ADC_CFG1_12MHZ + ADC_CFG1_MODE(1) + ADC_CFG1_ADLSMP;
250 ADC0_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(2);
254 ADC0_CFG1 = ADC_CFG1_12MHZ + ADC_CFG1_MODE(3) + ADC_CFG1_ADLSMP;
255 ADC0_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(2);
258 default: return; // Do nothing, invalid arg
262 CLI_argumentIsolation( arg2Ptr, &arg1Ptr, &arg2Ptr );
263 int vRef = decToInt( arg1Ptr );
266 case 0: // 1.2V internal Vref
267 ADC0_SC2 = ADC_SC2_REFSEL(1);
270 case 1: // Vcc/Ext Vref
271 ADC0_SC2 = ADC_SC2_REFSEL(0);
274 default: return; // Do nothing, invalid arg
277 // Hardware averaging (and start calibration)
278 CLI_argumentIsolation( arg2Ptr, &arg1Ptr, &arg2Ptr );
279 int hardwareAvg = decToInt( arg1Ptr );
280 switch ( hardwareAvg )
282 case 0: // No hardware averaging
283 ADC0_SC3 = ADC_SC3_CAL; // Just start calibration
286 case 4: // 4 sample averaging
287 ADC0_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(0);
290 case 8: // 8 sample averaging
291 ADC0_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(1);
294 case 16: // 16 sample averaging
295 ADC0_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(2);
298 case 32: // 32 sample averaging
299 ADC0_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(3);
302 default: return; // Do nothing, invalid arg
305 // Wait for calibration
306 while ( ADC0_SC3 & ADC_SC3_CAL );
311 // XXX Why is PJRC doing this? Is the self-calibration not good enough? -HaaTa
312 // ADC Plus-Side Gain Register
313 __disable_irq(); // Disable interrupts
314 sum = ADC0_CLPS + ADC0_CLP4 + ADC0_CLP3 + ADC0_CLP2 + ADC0_CLP1 + ADC0_CLP0;
315 sum = (sum / 2) | 0x8000;
319 info_msg("Calibration ADC0_PG (Plus-Side Gain Register) set to: ");
322 // ADC Minus-Side Gain Register
323 // XXX I don't think this is necessary when doing single-ended (as opposed to differential) -HaaTa
324 // K20P64M72SF1RM.pdf 31.3.10 pg. 666
325 sum = ADC0_CLMS + ADC0_CLM4 + ADC0_CLM3 + ADC0_CLM2 + ADC0_CLM1 + ADC0_CLM0;
326 sum = (sum / 2) | 0x8000;
330 info_msg("Calibration ADC0_MG (Minus-Side Gain Register) set to: ");
332 __enable_irq(); // Re-enable interrupts
336 void cliFunc_dac( char* args )
338 #if defined(_mk20dx256_) // DAC is only supported on Teensy 3.1
339 // Parse code from argument
340 // NOTE: Only first argument is used
343 CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
345 int dacOut = decToInt( arg1Ptr );
347 // Make sure the value is between 0 and 4096, otherwise ignore
348 if ( dacOut >= 0 && dacOut <= 4095 )
350 *(int16_t *) &(DAC0_DAT0L) = dacOut;
355 void cliFunc_dacVref( char* args )
357 #if defined(_mk20dx256_) // DAC is only supported on Teensy 3.1
358 // Parse code from argument
359 // NOTE: Only first argument is used
362 CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
364 switch ( decToInt( arg1Ptr ) )
367 DAC0_C0 = DAC_C0_DACEN; // 1.2V Vref is DACREF_1
370 DAC0_C0 = DAC_C0_DACEN | DAC_C0_DACRFS; // 3.3V VDDA is DACREF_2