Kiibohd Controller
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

scan_loop.c 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. // ----- Includes -----
  22. // Compiler Includes
  23. #include <Lib/ScanLib.h>
  24. // Project Includes
  25. #include <cli.h>
  26. #include <led.h>
  27. #include <print.h>
  28. // Local Includes
  29. #include "scan_loop.h"
  30. // ----- Defines -----
  31. // ADC Clock divisor settings (F_BUS == 48000000)
  32. #define ADC_CFG1_6MHZ ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1)
  33. #define ADC_CFG1_12MHZ ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1)
  34. #define ADC_CFG1_24MHZ ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(1)
  35. // ----- Macros -----
  36. // ----- Function Declarations -----
  37. void cliFunc_adc ( char* args );
  38. void cliFunc_adcInit( char* args );
  39. void cliFunc_dac ( char* args );
  40. void cliFunc_dacVref( char* args );
  41. void cliFunc_echo ( char* args );
  42. // ----- Variables -----
  43. // Buffer used to inform the macro processing module which keys have been detected as pressed
  44. volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
  45. volatile uint8_t KeyIndex_BufferUsed;
  46. // Scan Module command dictionary
  47. char* scanCLIDictName = "ADC Test Module Commands";
  48. CLIDictItem scanCLIDict[] = {
  49. { "adc", "Read the specified number of values from the ADC.", cliFunc_adc },
  50. { "adcInit", "Intialize/calibrate ADC. Arg 1 specifies the pin.", cliFunc_adcInit },
  51. #if defined(_mk20dx256_) // DAC is only supported on Teensy 3.1
  52. { "dac", "Set DAC output value, from 0 to 4095 (1/4096 Vref to Vref).", cliFunc_dac },
  53. { "dacVref", "Set DAC Vref. 0 is 1.2V. 1 is 3.3V.", cliFunc_dacVref },
  54. #endif
  55. { "echo", "Example command, echos the arguments.", cliFunc_echo },
  56. { 0, 0, 0 } // Null entry for dictionary end
  57. };
  58. // ----- Functions -----
  59. // Setup
  60. inline void Scan_setup()
  61. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  62. {
  63. // Register Scan CLI dictionary
  64. CLI_registerDictionary( scanCLIDict, scanCLIDictName );
  65. }
  66. #elif defined(_mk20dx128_) || defined(_mk20dx256_) // ARM
  67. {
  68. // Register Scan CLI dictionary
  69. CLI_registerDictionary( scanCLIDict, scanCLIDictName );
  70. // ADC Setup
  71. VREF_TRM = 0x60;
  72. VREF_SC = 0xE1; // Enable 1.2V Vref
  73. #if defined(_mk20dx256_) // DAC is only supported on Teensy 3.1
  74. // DAC Setup
  75. SIM_SCGC2 |= SIM_SCGC2_DAC0;
  76. DAC0_C0 = DAC_C0_DACEN | DAC_C0_DACRFS; // 3.3V VDDA is DACREF_2
  77. #endif
  78. }
  79. #endif
  80. // Main Detection Loop
  81. inline uint8_t Scan_loop()
  82. {
  83. return 0;
  84. }
  85. // Signal KeyIndex_Buffer that it has been properly read
  86. void Scan_finishedWithBuffer( uint8_t sentKeys )
  87. {
  88. }
  89. // Signal that the keys have been properly sent over USB
  90. void Scan_finishedWithUSBBuffer( uint8_t sentKeys )
  91. {
  92. }
  93. // Reset Keyboard
  94. void Scan_resetKeyboard()
  95. {
  96. }
  97. // ----- CLI Command Functions -----
  98. // XXX Just an example command showing how to parse arguments (more complex than generally needed)
  99. void cliFunc_echo( char* args )
  100. {
  101. char* curArgs;
  102. char* arg1Ptr;
  103. char* arg2Ptr = args;
  104. print( NL ); // No \n by default after the command is entered
  105. // Parse args until a \0 is found
  106. while ( 1 )
  107. {
  108. curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
  109. CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
  110. // Stop processing args if no more are found
  111. if ( *arg1Ptr == '\0' )
  112. break;
  113. // Print out the arg
  114. dPrint( arg1Ptr );
  115. }
  116. }
  117. void cliFunc_adc( char* args )
  118. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  119. {
  120. }
  121. #elif defined(_mk20dx128_) || defined(_mk20dx256_) // ARM
  122. {
  123. // Parse code from argument
  124. // NOTE: Only first argument is used
  125. char* arg1Ptr;
  126. char* arg2Ptr;
  127. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  128. // Set the ADC Channel
  129. uint8_t channel = decToInt( arg1Ptr );
  130. __disable_irq();
  131. ADC0_SC1A = channel;
  132. __enable_irq();
  133. // Number of ADC samples to display
  134. CLI_argumentIsolation( arg2Ptr, &arg1Ptr, &arg2Ptr );
  135. int displayedADC = decToInt( arg1Ptr );
  136. // Poll ADC until it gets a value, making sure to serve interrupts on each attempt
  137. while ( displayedADC > 0 )
  138. {
  139. __disable_irq();
  140. // ADC Sample is ready
  141. if ( (ADC0_SC1A & ADC_SC1_COCO) )
  142. {
  143. int result = ADC0_RA;
  144. printInt32( result );
  145. displayedADC--;
  146. }
  147. __enable_irq();
  148. yield(); // Make sure interrupts actually get serviced
  149. }
  150. }
  151. #endif
  152. void cliFunc_adcInit( char* args )
  153. #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
  154. {
  155. }
  156. #elif defined(_mk20dx128_) || defined(_mk20dx256_) // ARM
  157. {
  158. // Parse code from argument
  159. // NOTE: Only first argument is used
  160. char* arg1Ptr;
  161. char* arg2Ptr;
  162. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  163. // Make sure calibration has stopped
  164. ADC0_SC3 = 0;
  165. // Select bit resolution
  166. int bitResolution = decToInt( arg1Ptr );
  167. switch ( bitResolution )
  168. {
  169. case 8: // 8-bit
  170. ADC0_CFG1 = ADC_CFG1_24MHZ + ADC_CFG1_MODE(0);
  171. ADC0_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(3);
  172. break;
  173. case 10: // 10-bit
  174. ADC0_CFG1 = ADC_CFG1_12MHZ + ADC_CFG1_MODE(2) + ADC_CFG1_ADLSMP;
  175. ADC0_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(3);
  176. break;
  177. case 12: // 12-bit
  178. ADC0_CFG1 = ADC_CFG1_12MHZ + ADC_CFG1_MODE(1) + ADC_CFG1_ADLSMP;
  179. ADC0_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(2);
  180. break;
  181. case 16: // 16-bit
  182. ADC0_CFG1 = ADC_CFG1_12MHZ + ADC_CFG1_MODE(3) + ADC_CFG1_ADLSMP;
  183. ADC0_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(2);
  184. break;
  185. default: return; // Do nothing, invalid arg
  186. }
  187. // Select Vref
  188. CLI_argumentIsolation( arg2Ptr, &arg1Ptr, &arg2Ptr );
  189. int vRef = decToInt( arg1Ptr );
  190. switch ( vRef )
  191. {
  192. case 0: // 1.2V internal Vref
  193. ADC0_SC2 = ADC_SC2_REFSEL(1);
  194. break;
  195. case 1: // Vcc/Ext Vref
  196. ADC0_SC2 = ADC_SC2_REFSEL(0);
  197. break;
  198. default: return; // Do nothing, invalid arg
  199. }
  200. // Hardware averaging (and start calibration)
  201. CLI_argumentIsolation( arg2Ptr, &arg1Ptr, &arg2Ptr );
  202. int hardwareAvg = decToInt( arg1Ptr );
  203. switch ( hardwareAvg )
  204. {
  205. case 0: // No hardware averaging
  206. ADC0_SC3 = ADC_SC3_CAL; // Just start calibration
  207. break;
  208. case 4: // 4 sample averaging
  209. ADC0_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(0);
  210. break;
  211. case 8: // 8 sample averaging
  212. ADC0_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(1);
  213. break;
  214. case 16: // 16 sample averaging
  215. ADC0_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(2);
  216. break;
  217. case 32: // 32 sample averaging
  218. ADC0_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(3);
  219. break;
  220. default: return; // Do nothing, invalid arg
  221. }
  222. // Wait for calibration
  223. while ( ADC0_SC3 & ADC_SC3_CAL );
  224. // Set calibration
  225. uint16_t sum;
  226. // XXX Why is PJRC doing this? Is the self-calibration not good enough? -HaaTa
  227. // ADC Plus-Side Gain Register
  228. __disable_irq(); // Disable interrupts
  229. sum = ADC0_CLPS + ADC0_CLP4 + ADC0_CLP3 + ADC0_CLP2 + ADC0_CLP1 + ADC0_CLP0;
  230. sum = (sum / 2) | 0x8000;
  231. ADC0_PG = sum;
  232. info_msg("Calibration ADC0_PG (Plus-Side Gain Register) set to: ");
  233. printInt16( sum );
  234. print( NL );
  235. // ADC Minus-Side Gain Register
  236. // XXX I don't think this is necessary when doing single-ended (as opposed to differential) -HaaTa
  237. // K20P64M72SF1RM.pdf 31.3.10 pg. 666
  238. sum = ADC0_CLMS + ADC0_CLM4 + ADC0_CLM3 + ADC0_CLM2 + ADC0_CLM1 + ADC0_CLM0;
  239. sum = (sum / 2) | 0x8000;
  240. ADC0_MG = sum;
  241. info_msg("Calibration ADC0_MG (Minus-Side Gain Register) set to: ");
  242. printInt16( sum );
  243. print( NL );
  244. __enable_irq(); // Re-enable interrupts
  245. }
  246. #endif
  247. void cliFunc_dac( char* args )
  248. {
  249. #if defined(_mk20dx256_) // DAC is only supported on Teensy 3.1
  250. // Parse code from argument
  251. // NOTE: Only first argument is used
  252. char* arg1Ptr;
  253. char* arg2Ptr;
  254. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  255. int dacOut = decToInt( arg1Ptr );
  256. // Make sure the value is between 0 and 4096, otherwise ignore
  257. if ( dacOut >= 0 && dacOut <= 4095 )
  258. {
  259. *(int16_t *) &(DAC0_DAT0L) = dacOut;
  260. }
  261. #endif
  262. }
  263. void cliFunc_dacVref( char* args )
  264. {
  265. #if defined(_mk20dx256_) // DAC is only supported on Teensy 3.1
  266. // Parse code from argument
  267. // NOTE: Only first argument is used
  268. char* arg1Ptr;
  269. char* arg2Ptr;
  270. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  271. switch ( decToInt( arg1Ptr ) )
  272. {
  273. case 0:
  274. DAC0_C0 = DAC_C0_DACEN; // 1.2V Vref is DACREF_1
  275. break;
  276. case 1:
  277. DAC0_C0 = DAC_C0_DACEN | DAC_C0_DACRFS; // 3.3V VDDA is DACREF_2
  278. break;
  279. }
  280. #endif
  281. }