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.

matrix_scan.c 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* Copyright (C) 2016 by Jacob Alexander
  2. *
  3. * This file is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This file is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this file. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. // ----- Includes -----
  17. // Compiler Includes
  18. #include <Lib/ScanLib.h>
  19. // Project Includes
  20. #include <cli.h>
  21. #include <kll_defs.h>
  22. #include <led.h>
  23. #include <print.h>
  24. #include <macro.h>
  25. #include <Lib/delay.h>
  26. // Local Includes
  27. #include "matrix_scan.h"
  28. // Matrix Configuration
  29. //#include <matrix.h>
  30. // ----- Defines -----
  31. // ----- Function Declarations -----
  32. // CLI Functions
  33. void cliFunc_matrixDebug( char* args );
  34. void cliFunc_matrixInfo( char* args );
  35. void cliFunc_matrixState( char* args );
  36. // ----- Variables -----
  37. // Scan Module command dictionary
  38. CLIDict_Entry( matrixDebug, "Enables matrix debug mode, prints out each scan code." NL "\t\tIf argument \033[35mT\033[0m is given, prints out each scan code state transition." );
  39. CLIDict_Entry( matrixInfo, "Print info about the configured matrix." );
  40. CLIDict_Entry( matrixState, "Prints out the current scan table N times." NL "\t\t \033[1mO\033[0m - Off, \033[1;33mP\033[0m - Press, \033[1;32mH\033[0m - Hold, \033[1;35mR\033[0m - Release, \033[1;31mI\033[0m - Invalid" );
  41. CLIDict_Def( matrixCLIDict, "Matrix Module Commands" ) = {
  42. CLIDict_Item( matrixDebug ),
  43. CLIDict_Item( matrixInfo ),
  44. CLIDict_Item( matrixState ),
  45. { 0, 0, 0 } // Null entry for dictionary end
  46. };
  47. // ----- Functions -----
  48. // TODO
  49. // - Support multiple ADCs
  50. // - Channel/Mux setup
  51. void ADC_setup( ADC adc )
  52. {
  53. // Enable ADC clock
  54. #if defined(_mk20dx128_) || defined(_mk20dx128vlf5_)
  55. SIM_SCGC6 |= SIM_SCGC6_ADC0;
  56. #elif defined(_mk20dx256_) || defined(_mk20dx256vlh7_)
  57. SIM_SCGC6 |= SIM_SCGC6_ADC0;
  58. SIM_SCGC3 |= SIM_SCGC3_ADC1;
  59. #endif
  60. // Lookup base ADC register
  61. volatile unsigned int *ADC_SC1A = (unsigned int*)(&ADC_reg_offset_map[adc]);
  62. // Calculate Register offsets
  63. volatile unsigned int *ADC_CFG1 = (unsigned int*)(&ADC_SC1A) + 0x08;
  64. volatile unsigned int *ADC_CFG2 = (unsigned int*)(&ADC_SC1A) + 0x0C;
  65. volatile unsigned int *ADC_SC2 = (unsigned int*)(&ADC_SC1A) + 0x20;
  66. volatile unsigned int *ADC_SC3 = (unsigned int*)(&ADC_SC1A) + 0x24;
  67. volatile unsigned int *ADC_PG = (unsigned int*)(&ADC_SC1A) + 0x2C;
  68. volatile unsigned int *ADC_CLPS = (unsigned int*)(&ADC_SC1A) + 0x38;
  69. volatile unsigned int *ADC_CLP4 = (unsigned int*)(&ADC_SC1A) + 0x3C;
  70. volatile unsigned int *ADC_CLP3 = (unsigned int*)(&ADC_SC1A) + 0x40;
  71. volatile unsigned int *ADC_CLP2 = (unsigned int*)(&ADC_SC1A) + 0x44;
  72. volatile unsigned int *ADC_CLP1 = (unsigned int*)(&ADC_SC1A) + 0x48;
  73. volatile unsigned int *ADC_CLP0 = (unsigned int*)(&ADC_SC1A) + 0x4C;
  74. // Make sure calibration has stopped
  75. *ADC_SC3 = 0;
  76. // - CFG1 -
  77. // ADIV: (input)/2 divider
  78. // ADICLK: (bus)/2 divider
  79. // MODE: 16-bit
  80. // ADLSMP: Long sample
  81. //ADC_CFG1 = ADC_CFG1_ADIV(1) | ADC_CFG1_ADICLK(1) | ADC_CFG1_MODE(3) | ADC_CFG1_ADLSMP;
  82. // ADIV: (input)/8 divider
  83. *ADC_CFG1 = ADC_CFG1_ADIV(3) | ADC_CFG1_ADICLK(1) | ADC_CFG1_MODE(3) | ADC_CFG1_ADLSMP;
  84. // - CFG2 -
  85. // ADLSTS: 6 extra ADCK cycles; 10 ADCK cycles total sample time
  86. //ADC_CFG2 = ADC_CFG2_ADLSTS(2);
  87. // ADLSTS: 20 extra ADCK cycles; 24 ADCK cycles total sample time
  88. *ADC_CFG2 = ADC_CFG2_ADLSTS(0);
  89. // - SC2 -
  90. // REFSEL: Use default 3.3V reference
  91. *ADC_SC2 = ADC_SC2_REFSEL(0);
  92. /*
  93. // Setup VREF to 1.2 V
  94. VREF_TRM = 0x60;
  95. VREF_SC = 0xE1; // Enable 1.2 volt ref
  96. // REFSEL: Use 1.2V reference VREF
  97. *ADC_SC2 = ADC_SC2_REFSEL(1);
  98. */
  99. // - SC3 -
  100. // CAL: Start calibration
  101. // AVGE: Enable hardware averaging
  102. // AVGS: 32 samples averaged
  103. // 32 sample averaging
  104. *ADC_SC3 = ADC_SC3_CAL | ADC_SC3_AVGE | ADC_SC3_AVGS(3);
  105. // Wait for calibration
  106. while ( *ADC_SC3 & ADC_SC3_CAL );
  107. // Apply computed calibration offset
  108. // XXX Note, for single-ended, only the plus side offsets have to be applied
  109. // For differential the minus side also has to be set as well
  110. __disable_irq(); // Disable interrupts while reading/setting offsets
  111. // Set calibration
  112. // ADC Plus-Side Gain Register
  113. // See Section 31.4.7 in the datasheet (mk20dx256vlh7) for details
  114. uint16_t sum = *ADC_CLPS + *ADC_CLP4 + *ADC_CLP3 + *ADC_CLP2 + *ADC_CLP1 + *ADC_CLP0;
  115. sum = (sum / 2) | 0x8000;
  116. *ADC_PG = sum;
  117. __enable_irq(); // Re-enable interrupts
  118. // Start ADC reading loop
  119. // - SC1A -
  120. // ADCH: Channel DAD0 (A10)
  121. // AIEN: Enable interrupt
  122. //*ADC_SC1A = ADC_SC1_AIEN | ADC_SC1_ADCH(0);
  123. // Enable ADC0 IRQ Vector
  124. //NVIC_ENABLE_IRQ( IRQ_ADC0 );
  125. }
  126. // TODO
  127. // - Enable/Disable strobe detection (IBM)
  128. // - Setup strobe matrix
  129. void Strobe_setup()
  130. {
  131. }
  132. // TODO
  133. // - Setup ADCs
  134. // - Setup ADC muxes
  135. // - Setup voltage stab
  136. void Sense_setup()
  137. {
  138. }
  139. void Matrix_setup()
  140. {
  141. // Register Matrix CLI dictionary
  142. CLI_registerDictionary( matrixCLIDict, matrixCLIDictName );
  143. // Setup sense
  144. Sense_setup();
  145. // Setup strobes
  146. Strobe_setup();
  147. }
  148. // Scan the matrix for keypresses
  149. // NOTE: scanNum should be reset to 0 after a USB send (to reset all the counters)
  150. void Matrix_scan( uint16_t scanNum )
  151. {
  152. }
  153. // Called by parent scan module whenever the available current changes
  154. // current - mA
  155. void Matrix_currentChange( unsigned int current )
  156. {
  157. // TODO - Any potential power savings?
  158. }
  159. // ----- CLI Command Functions -----
  160. void cliFunc_matrixInfo( char* args )
  161. {
  162. }
  163. void cliFunc_matrixDebug( char* args )
  164. {
  165. // Parse number from argument
  166. // NOTE: Only first argument is used
  167. char* arg1Ptr;
  168. char* arg2Ptr;
  169. CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
  170. // Set the matrix debug flag depending on the argument
  171. // If no argument, set to scan code only
  172. // If set to T, set to state transition
  173. switch ( arg1Ptr[0] )
  174. {
  175. // T as argument
  176. case 'T':
  177. case 't':
  178. break;
  179. // No argument
  180. case '\0':
  181. break;
  182. // Invalid argument
  183. default:
  184. return;
  185. }
  186. }
  187. void cliFunc_matrixState( char* args )
  188. {
  189. }