matrix.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #include <stdint.h>
  2. #include <stdbool.h>
  3. #include <avr/io.h>
  4. #include "wait.h"
  5. #include "action_layer.h"
  6. #include "print.h"
  7. #include "debug.h"
  8. #include "util.h"
  9. #include "matrix.h"
  10. #include "ergodone.h"
  11. #include "expander.h"
  12. #ifdef DEBUG_MATRIX_SCAN_RATE
  13. #include "timer.h"
  14. #endif
  15. /*
  16. * This constant define not debouncing time in msecs, but amount of matrix
  17. * scan loops which should be made to get stable debounced results.
  18. *
  19. * On Ergodox matrix scan rate is relatively low, because of slow I2C.
  20. * Now it's only 317 scans/second, or about 3.15 msec/scan.
  21. * According to Cherry specs, debouncing time is 5 msec.
  22. *
  23. * And so, there is no sense to have DEBOUNCE higher than 2.
  24. */
  25. #ifndef DEBOUNCE
  26. # define DEBOUNCE 5
  27. #endif
  28. /* matrix state(1:on, 0:off) */
  29. static matrix_row_t matrix[MATRIX_ROWS];
  30. // Debouncing: store for each key the number of scans until it's eligible to
  31. // change. When scanning the matrix, ignore any changes in keys that have
  32. // already changed in the last DEBOUNCE scans.
  33. static uint8_t debounce_matrix[MATRIX_ROWS * MATRIX_COLS];
  34. static matrix_row_t read_cols(uint8_t row);
  35. static void init_cols(void);
  36. static void unselect_rows(void);
  37. static void select_row(uint8_t row);
  38. #ifdef DEBUG_MATRIX_SCAN_RATE
  39. uint32_t matrix_timer;
  40. uint32_t matrix_scan_count;
  41. #endif
  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. {
  57. return MATRIX_ROWS;
  58. }
  59. inline
  60. uint8_t matrix_cols(void)
  61. {
  62. return MATRIX_COLS;
  63. }
  64. void matrix_init(void)
  65. {
  66. // disable JTAG
  67. MCUCR = (1<<JTD);
  68. MCUCR = (1<<JTD);
  69. unselect_rows();
  70. init_cols();
  71. // initialize matrix state: all keys off
  72. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  73. matrix[i] = 0;
  74. for (uint8_t j=0; j < MATRIX_COLS; ++j) {
  75. debounce_matrix[i * MATRIX_COLS + j] = 0;
  76. }
  77. }
  78. #ifdef DEBUG_MATRIX_SCAN_RATE
  79. matrix_timer = timer_read32();
  80. matrix_scan_count = 0;
  81. #endif
  82. matrix_init_quantum();
  83. }
  84. void matrix_power_up(void) {
  85. unselect_rows();
  86. init_cols();
  87. // initialize matrix state: all keys off
  88. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  89. matrix[i] = 0;
  90. }
  91. #ifdef DEBUG_MATRIX_SCAN_RATE
  92. matrix_timer = timer_read32();
  93. matrix_scan_count = 0;
  94. #endif
  95. }
  96. // Returns a matrix_row_t whose bits are set if the corresponding key should be
  97. // eligible to change in this scan.
  98. matrix_row_t debounce_mask(uint8_t row) {
  99. matrix_row_t result = 0;
  100. for (uint8_t j=0; j < MATRIX_COLS; ++j) {
  101. if (debounce_matrix[row * MATRIX_COLS + j]) {
  102. --debounce_matrix[row * MATRIX_COLS + j];
  103. } else {
  104. result |= (1 << j);
  105. }
  106. }
  107. return result;
  108. }
  109. // Report changed keys in the given row. Resets the debounce countdowns
  110. // corresponding to each set bit in 'change' to DEBOUNCE.
  111. void debounce_report(matrix_row_t change, uint8_t row) {
  112. for (uint8_t i = 0; i < MATRIX_COLS; ++i) {
  113. if (change & (1 << i)) {
  114. debounce_matrix[row * MATRIX_COLS + i] = DEBOUNCE;
  115. }
  116. }
  117. }
  118. uint8_t matrix_scan(void)
  119. {
  120. expander_scan();
  121. #ifdef DEBUG_MATRIX_SCAN_RATE
  122. matrix_scan_count++;
  123. uint32_t timer_now = timer_read32();
  124. if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
  125. print("matrix scan frequency: ");
  126. pdec(matrix_scan_count);
  127. print("\n");
  128. matrix_print();
  129. matrix_timer = timer_now;
  130. matrix_scan_count = 0;
  131. }
  132. #endif
  133. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  134. select_row(i);
  135. wait_us(30); // without this wait read unstable value.
  136. matrix_row_t mask = debounce_mask(i);
  137. matrix_row_t cols = (read_cols(i) & mask) | (matrix[i] & ~mask);
  138. debounce_report(cols ^ matrix[i], i);
  139. matrix[i] = cols;
  140. unselect_rows();
  141. }
  142. matrix_scan_quantum();
  143. return 1;
  144. }
  145. inline
  146. bool matrix_is_on(uint8_t row, uint8_t col)
  147. {
  148. return (matrix[row] & ((matrix_row_t)1<<col));
  149. }
  150. inline
  151. matrix_row_t matrix_get_row(uint8_t row)
  152. {
  153. return matrix[row];
  154. }
  155. void matrix_print(void)
  156. {
  157. print("\nr/c 0123456789ABCDEF\n");
  158. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  159. phex(row); print(": ");
  160. pbin_reverse16(matrix_get_row(row));
  161. print("\n");
  162. }
  163. }
  164. uint8_t matrix_key_count(void)
  165. {
  166. uint8_t count = 0;
  167. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  168. count += bitpop16(matrix[i]);
  169. }
  170. return count;
  171. }
  172. /* Column pin configuration
  173. *
  174. * Pro Micro: 6 5 4 3 2 1 0
  175. * PD3 PD2 PD4 PC6 PD7 PE6 PB4
  176. *
  177. * Expander: 13 12 11 10 9 8 7
  178. */
  179. static void init_cols(void)
  180. {
  181. // Pro Micro
  182. DDRE &= ~(1<<PE6);
  183. PORTE |= (1<<PE6);
  184. DDRD &= ~(1<<PD2 | 1<<PD3 | 1<<PD4 | 1<<PD7);
  185. PORTD |= (1<<PD2 | 1<<PD3 | 1<<PD4 | 1<<PD7);
  186. DDRC &= ~(1<<PC6);
  187. PORTC |= (1<<PC6);
  188. DDRB &= ~(1<<PB4);
  189. PORTB |= (1<<PB4);
  190. // MCP23017
  191. expander_init();
  192. }
  193. static matrix_row_t read_cols(uint8_t row)
  194. {
  195. return expander_read_row() |
  196. (PIND&(1<<PD3) ? 0 : (1<<6)) |
  197. (PIND&(1<<PD2) ? 0 : (1<<5)) |
  198. (PIND&(1<<PD4) ? 0 : (1<<4)) |
  199. (PINC&(1<<PC6) ? 0 : (1<<3)) |
  200. (PIND&(1<<PD7) ? 0 : (1<<2)) |
  201. (PINE&(1<<PE6) ? 0 : (1<<1)) |
  202. (PINB&(1<<PB4) ? 0 : (1<<0)) ;
  203. }
  204. /* Row pin configuration
  205. *
  206. * Pro Micro: 0 1 2 3 4 5
  207. * F4 F5 F6 F7 B1 B2
  208. *
  209. * Expander: 0 1 2 3 4 5
  210. */
  211. static void unselect_rows(void)
  212. {
  213. // Pro Micro
  214. DDRF &= ~(1<<PF4 | 1<<PF5 | 1<<PF6 | 1<<PF7);
  215. PORTF &= ~(1<<PF4 | 1<<PF5 | 1<<PF6 | 1<<PF7);
  216. DDRB &= ~(1<<PB1 | 1<<PB2);
  217. PORTB &= ~(1<<PB1 | 1<<PB2);
  218. // Expander
  219. expander_unselect_rows();
  220. }
  221. static void select_row(uint8_t row)
  222. {
  223. // Pro Micro
  224. switch (row) {
  225. case 0:
  226. DDRF |= (1<<PF4);
  227. PORTF &= ~(1<<PF4);
  228. break;
  229. case 1:
  230. DDRF |= (1<<PF5);
  231. PORTF &= ~(1<<PF5);
  232. break;
  233. case 2:
  234. DDRF |= (1<<PF6);
  235. PORTF &= ~(1<<PF6);
  236. break;
  237. case 3:
  238. DDRF |= (1<<PF7);
  239. PORTF &= ~(1<<PF7);
  240. break;
  241. case 4:
  242. DDRB |= (1<<PB1);
  243. PORTB &= ~(1<<PB1);
  244. break;
  245. case 5:
  246. DDRB |= (1<<PB2);
  247. PORTB &= ~(1<<PB2);
  248. break;
  249. }
  250. expander_select_row(row);
  251. }