]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Scan/ADCTest/scan_loop.c
Merge branch 'master' of github.com:kiibohd/controller
[kiibohd-controller.git] / Scan / ADCTest / scan_loop.c
1 /* Copyright (C) 2014 by Jacob Alexander
2  *
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:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
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
19  * THE SOFTWARE.
20  */
21
22 // ----- Includes -----
23
24 // Compiler Includes
25 #include <Lib/ScanLib.h>
26
27 // Project Includes
28 #include <cli.h>
29 #include <led.h>
30 #include <print.h>
31
32 // Local Includes
33 #include "scan_loop.h"
34
35
36
37 // ----- Defines -----
38
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)
43
44
45
46 // ----- Macros -----
47
48
49
50 // ----- Function Declarations -----
51
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 );
57
58
59
60 // ----- Variables -----
61
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;
65
66
67 // Scan Module command dictionary
68 char scanCLIDictName[] = "ADC Test Module Commands";
69 const CLIDictItem scanCLIDict[] = {
70 #if defined(_mk20dx128_) || defined(_mk20dx256_) || defined(_mk20dx256vlh7_) // 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 },
77 #endif
78 #if defined(_mk20dx256_) || defined(_mk20dx256vlh7_) // 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 },
81 #endif
82         { "echo",    "Example command, echos the arguments.", cliFunc_echo },
83         { 0, 0, 0 } // Null entry for dictionary end
84 };
85
86
87
88 // ----- Functions -----
89
90 // Setup
91 inline void Scan_setup()
92 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
93 {
94         // Register Scan CLI dictionary
95         CLI_registerDictionary( scanCLIDict, scanCLIDictName );
96 }
97 #elif defined(_mk20dx128_) || defined(_mk20dx256_) || defined(_mk20dx256vlh7_) // ARM
98 {
99         // Register Scan CLI dictionary
100         CLI_registerDictionary( scanCLIDict, scanCLIDictName );
101
102         // ADC Setup
103         VREF_TRM = 0x60;
104         VREF_SC  = 0xE1; // Enable 1.2V Vref
105
106 #if defined(_mk20dx256_) || defined(_mk20dx256vlh7_) // DAC is only supported on Teensy 3.1
107         // DAC Setup
108         SIM_SCGC2 |= SIM_SCGC2_DAC0;
109         DAC0_C0 = DAC_C0_DACEN | DAC_C0_DACRFS; // 3.3V VDDA is DACREF_2
110 #endif
111 }
112 #endif
113
114
115 // Main Detection Loop
116 inline uint8_t Scan_loop()
117 {
118         return 0;
119 }
120
121
122 // Signal KeyIndex_Buffer that it has been properly read
123 void Scan_finishedWithBuffer( uint8_t sentKeys )
124 {
125 }
126
127
128 // Signal that the keys have been properly sent over USB
129 void Scan_finishedWithUSBBuffer( uint8_t sentKeys )
130 {
131 }
132
133
134 // Reset Keyboard
135 void Scan_resetKeyboard()
136 {
137 }
138
139
140 // ----- CLI Command Functions -----
141
142 // XXX Just an example command showing how to parse arguments (more complex than generally needed)
143 void cliFunc_echo( char* args )
144 {
145         char* curArgs;
146         char* arg1Ptr;
147         char* arg2Ptr = args;
148
149         // Parse args until a \0 is found
150         while ( 1 )
151         {
152                 print( NL ); // No \r\n by default after the command is entered
153
154                 curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
155                 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
156
157                 // Stop processing args if no more are found
158                 if ( *arg1Ptr == '\0' )
159                         break;
160
161                 // Print out the arg
162                 dPrint( arg1Ptr );
163         }
164 }
165
166 void cliFunc_adc( char* args )
167 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
168 {
169 }
170 #elif defined(_mk20dx128_) || defined(_mk20dx256_) || defined(_mk20dx256vlh7_) // ARM
171 {
172         // Parse code from argument
173         //  NOTE: Only first argument is used
174         char* arg1Ptr;
175         char* arg2Ptr;
176         CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
177
178         // Set the ADC Channel
179         uint8_t channel = numToInt( arg1Ptr );
180         __disable_irq();
181         ADC0_SC1A = channel;
182         __enable_irq();
183
184         // Number of ADC samples to display
185         CLI_argumentIsolation( arg2Ptr, &arg1Ptr, &arg2Ptr );
186
187         int displayedADC = 1; // Default to 1 read
188         if ( arg1Ptr ) // If there is an argument, use that instead
189         {
190                 displayedADC = numToInt( arg1Ptr );
191         }
192
193         // Poll ADC until it gets a value, making sure to serve interrupts on each attempt
194         while ( displayedADC > 0 )
195         {
196                 __disable_irq();
197
198                 // ADC Sample is ready
199                 if ( (ADC0_SC1A & ADC_SC1_COCO) )
200                 {
201                         int result = ADC0_RA;
202                         print( NL );
203                         printInt32( result );
204                         displayedADC--;
205
206                         // Prepare for another read
207                         if ( displayedADC > 0 )
208                         {
209                                 ADC0_SC1A = channel;
210                         }
211                 }
212
213                 __enable_irq();
214                 yield(); // Make sure interrupts actually get serviced
215         }
216 }
217 #endif
218
219 void cliFunc_adcInit( char* args )
220 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
221 {
222 }
223 #elif defined(_mk20dx128_) || defined(_mk20dx256_) || defined(_mk20dx256vlh7_) // ARM
224 {
225         // Parse code from argument
226         //  NOTE: Only first argument is used
227         char* arg1Ptr;
228         char* arg2Ptr;
229         CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
230
231         // Make sure calibration has stopped
232         ADC0_SC3 = 0;
233
234         // Select bit resolution
235         int bitResolution = numToInt( arg1Ptr );
236         switch ( bitResolution )
237         {
238         case 8: // 8-bit
239                 ADC0_CFG1 = ADC_CFG1_24MHZ + ADC_CFG1_MODE(0);
240                 ADC0_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(3);
241                 break;
242
243         case 10: // 10-bit
244                 ADC0_CFG1 = ADC_CFG1_12MHZ + ADC_CFG1_MODE(2) + ADC_CFG1_ADLSMP;
245                 ADC0_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(3);
246                 break;
247
248         case 12: // 12-bit
249                 ADC0_CFG1 = ADC_CFG1_12MHZ + ADC_CFG1_MODE(1) + ADC_CFG1_ADLSMP;
250                 ADC0_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(2);
251                 break;
252
253         case 16: // 16-bit
254                 ADC0_CFG1 = ADC_CFG1_12MHZ + ADC_CFG1_MODE(3) + ADC_CFG1_ADLSMP;
255                 ADC0_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(2);
256                 break;
257
258         default: return; // Do nothing, invalid arg
259         }
260
261         // Select Vref
262         CLI_argumentIsolation( arg2Ptr, &arg1Ptr, &arg2Ptr );
263         int vRef = numToInt( arg1Ptr );
264         switch ( vRef )
265         {
266         case 0: // 1.2V internal Vref
267                 ADC0_SC2 = ADC_SC2_REFSEL(1);
268                 break;
269
270         case 1: // Vcc/Ext Vref
271                 ADC0_SC2 = ADC_SC2_REFSEL(0);
272                 break;
273
274         default: return; // Do nothing, invalid arg
275         }
276
277         // Hardware averaging (and start calibration)
278         CLI_argumentIsolation( arg2Ptr, &arg1Ptr, &arg2Ptr );
279         int hardwareAvg = numToInt( arg1Ptr );
280         switch ( hardwareAvg )
281         {
282         case 0:  // No hardware averaging
283                 ADC0_SC3 = ADC_SC3_CAL; // Just start calibration
284                 break;
285
286         case 4:  // 4 sample averaging
287                 ADC0_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(0);
288                 break;
289
290         case 8:  // 8 sample averaging
291                 ADC0_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(1);
292                 break;
293
294         case 16: // 16 sample averaging
295                 ADC0_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(2);
296                 break;
297
298         case 32: // 32 sample averaging
299                 ADC0_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(3);
300                 break;
301
302         default: return; // Do nothing, invalid arg
303         }
304
305         // Wait for calibration
306         while ( ADC0_SC3 & ADC_SC3_CAL );
307
308         // Set calibration
309         uint16_t sum;
310
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;
316         ADC0_PG = sum;
317
318         print( NL );
319         info_msg("Calibration ADC0_PG (Plus-Side Gain Register)  set to: ");
320         printInt16( sum );
321
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;
327         ADC0_MG = sum;
328
329         print( NL );
330         info_msg("Calibration ADC0_MG (Minus-Side Gain Register) set to: ");
331         printInt16( sum );
332         __enable_irq(); // Re-enable interrupts
333 }
334 #endif
335
336 void cliFunc_dac( char* args )
337 {
338 #if defined(_mk20dx256_) || defined(_mk20dx256vlh7_) // DAC is only supported on Teensy 3.1
339         // Parse code from argument
340         //  NOTE: Only first argument is used
341         char* arg1Ptr;
342         char* arg2Ptr;
343         CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
344
345         int dacOut = numToInt( arg1Ptr );
346
347         // Make sure the value is between 0 and 4096, otherwise ignore
348         if ( dacOut >= 0 && dacOut <= 4095 )
349         {
350                 *(int16_t *) &(DAC0_DAT0L) = dacOut;
351         }
352 #endif
353 }
354
355 void cliFunc_dacVref( char* args )
356 {
357 #if defined(_mk20dx256_) || defined(_mk20dx256vlh7_) // DAC is only supported on Teensy 3.1
358         // Parse code from argument
359         //  NOTE: Only first argument is used
360         char* arg1Ptr;
361         char* arg2Ptr;
362         CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
363
364         switch ( numToInt( arg1Ptr ) )
365         {
366         case 0:
367                 DAC0_C0 = DAC_C0_DACEN; // 1.2V Vref is DACREF_1
368                 break;
369         case 1:
370                 DAC0_C0 = DAC_C0_DACEN | DAC_C0_DACRFS; // 3.3V VDDA is DACREF_2
371                 break;
372         }
373 #endif
374 }
375