logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

matrix.c (8352B)


  1. /* Copyright 2018 Evy Dekkers
  2. *
  3. * This program 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 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program 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 program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <stdint.h>
  17. #include <stdbool.h>
  18. #if defined(__AVR__)
  19. #include <avr/io.h>
  20. #endif
  21. #include "wait.h"
  22. #include "print.h"
  23. #include "debug.h"
  24. #include "util.h"
  25. #include "matrix.h"
  26. #include "timer.h"
  27. /* Set 0 if debouncing isn't needed */
  28. #ifndef DEBOUNCE
  29. # define DEBOUNCE 5
  30. #endif
  31. #define COL_SHIFTER ((uint32_t)1)
  32. static uint16_t debouncing_time;
  33. static bool debouncing = false;
  34. static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  35. /* matrix state(1:on, 0:off) */
  36. static matrix_row_t matrix[MATRIX_ROWS];
  37. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  38. static void init_rows(void);
  39. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
  40. static void unselect_cols(void);
  41. static void select_col(uint8_t col);
  42. __attribute__ ((weak))
  43. void matrix_init_user(void) {}
  44. __attribute__ ((weak))
  45. void matrix_scan_user(void) {}
  46. __attribute__ ((weak))
  47. void matrix_init_kb(void) {
  48. matrix_init_user();
  49. }
  50. __attribute__ ((weak))
  51. void matrix_scan_kb(void) {
  52. matrix_scan_user();
  53. }
  54. inline
  55. uint8_t matrix_rows(void) {
  56. return MATRIX_ROWS;
  57. }
  58. inline
  59. uint8_t matrix_cols(void) {
  60. return MATRIX_COLS;
  61. }
  62. void matrix_init(void) {
  63. unselect_cols();
  64. init_rows();
  65. // initialize matrix state: all keys off
  66. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  67. matrix[i] = 0;
  68. matrix_debouncing[i] = 0;
  69. }
  70. matrix_init_kb();
  71. }
  72. uint8_t matrix_scan(void)
  73. {
  74. // Set col, read rows
  75. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  76. bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col);
  77. if (matrix_changed) {
  78. debouncing = true;
  79. debouncing_time = timer_read();
  80. }
  81. }
  82. if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCE)) {
  83. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  84. matrix[i] = matrix_debouncing[i];
  85. }
  86. debouncing = false;
  87. }
  88. matrix_scan_kb();
  89. return 1;
  90. }
  91. inline
  92. bool matrix_is_on(uint8_t row, uint8_t col)
  93. {
  94. return (matrix[row] & ((matrix_row_t)1<<col));
  95. }
  96. inline
  97. matrix_row_t matrix_get_row(uint8_t row)
  98. {
  99. return matrix[row];
  100. }
  101. void matrix_print(void)
  102. {
  103. print("\nr/c 0123456789ABCDEFGHIJKLMNOPQRSTUV \n");
  104. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  105. print_hex8(row); print(": ");
  106. print_bin_reverse32(matrix_get_row(row));
  107. print("\n");
  108. }
  109. }
  110. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
  111. {
  112. bool matrix_changed = false;
  113. // Select col and wait for col selecton to stabilize
  114. select_col(current_col);
  115. wait_us(30);
  116. // For each row...
  117. for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
  118. {
  119. // Store last value of row prior to reading
  120. matrix_row_t last_row_value = current_matrix[row_index];
  121. // Check row pin state
  122. // Use the otherwise unused row: 3, col: 0 for caps lock
  123. if (row_index == 2 && current_col == 2) {
  124. // Pin E2 uses active low
  125. if ((_SFR_IO8(E2 >> 4) & _BV(E2 & 0xF)) == 0)
  126. {
  127. // Pin LO, set col bit
  128. current_matrix[row_index] |= (COL_SHIFTER << current_col);
  129. }
  130. else
  131. {
  132. // Pin HI, clear col bit
  133. current_matrix[row_index] &= ~(COL_SHIFTER << current_col);
  134. }
  135. }
  136. else {
  137. if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)))
  138. {
  139. // Pin HI, set col bit
  140. current_matrix[row_index] |= (COL_SHIFTER << current_col);
  141. }
  142. else
  143. {
  144. // Pin LO, clear col bit
  145. current_matrix[row_index] &= ~(COL_SHIFTER << current_col);
  146. }
  147. }
  148. // Determine if the matrix changed state
  149. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
  150. {
  151. matrix_changed = true;
  152. }
  153. }
  154. // Unselect cols
  155. unselect_cols();
  156. return matrix_changed;
  157. }
  158. /* Row pin configuration
  159. * row: 0 1 2 3 4
  160. * pin: D0 D1 D2 D3 D5
  161. *
  162. * Caps lock uses its own pin E2
  163. */
  164. static void init_rows(void) {
  165. gpio_set_pin_input(D0);
  166. gpio_set_pin_input(D1);
  167. gpio_set_pin_input(D2);
  168. gpio_set_pin_input(D3);
  169. gpio_set_pin_input(D5);
  170. gpio_set_pin_input_high(E2);
  171. }
  172. /* Columns 0 - 16
  173. * col 0: B5
  174. * col 1: B6
  175. * These columns use a 74HC237D 3 to 8 bit demultiplexer.
  176. * A B C GL1
  177. * col / pin: PF0 PF1 PC7 PC6
  178. * 2: 0 0 0 1
  179. * 3: 1 0 0 1
  180. * 4: 0 1 0 1
  181. * 5: 1 1 0 1
  182. * 6: 0 0 1 1
  183. * 7: 1 0 1 1
  184. * 8: 0 1 1 1
  185. * 9: 1 1 1 1
  186. * col 10: E6
  187. * col 11: B0
  188. * col 12: B7
  189. * col 13: D4
  190. * col 14: D6
  191. * col 15: D7
  192. * col 16: B4
  193. */
  194. static void unselect_cols(void) {
  195. gpio_set_pin_output(B0);
  196. gpio_set_pin_output(B4);
  197. gpio_set_pin_output(B5);
  198. gpio_set_pin_output(B6);
  199. gpio_set_pin_output(B7);
  200. gpio_write_pin_low(B0);
  201. gpio_write_pin_low(B4);
  202. gpio_write_pin_low(B5);
  203. gpio_write_pin_low(B6);
  204. gpio_write_pin_low(B7);
  205. gpio_set_pin_output(D4);
  206. gpio_set_pin_output(D6);
  207. gpio_set_pin_output(D7);
  208. gpio_write_pin_low(D4);
  209. gpio_write_pin_low(D6);
  210. gpio_write_pin_low(D7);
  211. gpio_set_pin_output(E6);
  212. gpio_write_pin_low(E6);
  213. gpio_set_pin_output(F0);
  214. gpio_set_pin_output(F1);
  215. gpio_write_pin_low(F0);
  216. gpio_write_pin_low(F1);
  217. gpio_set_pin_output(C6);
  218. gpio_set_pin_output(C7);
  219. gpio_write_pin_low(C6);
  220. gpio_write_pin_low(C7);
  221. }
  222. static void select_col(uint8_t col)
  223. {
  224. switch (col) {
  225. case 0:
  226. gpio_write_pin_high(B5); // HI
  227. break;
  228. case 1:
  229. gpio_write_pin_high(B6); // HI
  230. break;
  231. case 2:
  232. gpio_write_pin_high(C6); // HI
  233. break;
  234. case 3:
  235. gpio_write_pin_high(C6); // HI
  236. gpio_write_pin_high(F0); // HI
  237. break;
  238. case 4:
  239. gpio_write_pin_high(C6); // HI
  240. gpio_write_pin_high(F1); // HI
  241. break;
  242. case 5:
  243. gpio_write_pin_high(C6); // HI
  244. gpio_write_pin_high(F0); // HI
  245. gpio_write_pin_high(F1); // HI
  246. break;
  247. case 6:
  248. gpio_write_pin_high(C6); // HI
  249. gpio_write_pin_high(C7); // HI
  250. break;
  251. case 7:
  252. gpio_write_pin_high(C6); // HI
  253. gpio_write_pin_high(F0); // HI
  254. gpio_write_pin_high(C7); // HI
  255. break;
  256. case 8:
  257. gpio_write_pin_high(C6); // HI
  258. gpio_write_pin_high(F1); // HI
  259. gpio_write_pin_high(C7); // HI
  260. break;
  261. case 9:
  262. gpio_write_pin_high(C6); // HI
  263. gpio_write_pin_high(F0); // HI
  264. gpio_write_pin_high(F1); // HI
  265. gpio_write_pin_high(C7); // HI
  266. break;
  267. case 10:
  268. gpio_write_pin_high(E6); // HI
  269. break;
  270. case 11:
  271. gpio_write_pin_high(B0); // HI
  272. break;
  273. case 12:
  274. gpio_write_pin_high(B7); // HI
  275. break;
  276. case 13:
  277. gpio_write_pin_high(D4); // HI
  278. break;
  279. case 14:
  280. gpio_write_pin_high(D6); // HI
  281. break;
  282. case 15:
  283. gpio_write_pin_high(D7); // HI
  284. break;
  285. case 16:
  286. gpio_write_pin_high(B4); // HI
  287. break;
  288. }
  289. }