matrix.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. unselect_rows();
  67. init_cols();
  68. // initialize matrix state: all keys off
  69. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  70. matrix[i] = 0;
  71. for (uint8_t j=0; j < MATRIX_COLS; ++j) {
  72. debounce_matrix[i * MATRIX_COLS + j] = 0;
  73. }
  74. }
  75. #ifdef DEBUG_MATRIX_SCAN_RATE
  76. matrix_timer = timer_read32();
  77. matrix_scan_count = 0;
  78. #endif
  79. matrix_init_quantum();
  80. }
  81. void matrix_power_up(void) {
  82. unselect_rows();
  83. init_cols();
  84. // initialize matrix state: all keys off
  85. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  86. matrix[i] = 0;
  87. }
  88. #ifdef DEBUG_MATRIX_SCAN_RATE
  89. matrix_timer = timer_read32();
  90. matrix_scan_count = 0;
  91. #endif
  92. }
  93. // Returns a matrix_row_t whose bits are set if the corresponding key should be
  94. // eligible to change in this scan.
  95. matrix_row_t debounce_mask(uint8_t row) {
  96. matrix_row_t result = 0;
  97. for (uint8_t j=0; j < MATRIX_COLS; ++j) {
  98. if (debounce_matrix[row * MATRIX_COLS + j]) {
  99. --debounce_matrix[row * MATRIX_COLS + j];
  100. } else {
  101. result |= (1 << j);
  102. }
  103. }
  104. return result;
  105. }
  106. // Report changed keys in the given row. Resets the debounce countdowns
  107. // corresponding to each set bit in 'change' to DEBOUNCE.
  108. void debounce_report(matrix_row_t change, uint8_t row) {
  109. for (uint8_t i = 0; i < MATRIX_COLS; ++i) {
  110. if (change & (1 << i)) {
  111. debounce_matrix[row * MATRIX_COLS + i] = DEBOUNCE;
  112. }
  113. }
  114. }
  115. uint8_t matrix_scan(void)
  116. {
  117. expander_scan();
  118. #ifdef DEBUG_MATRIX_SCAN_RATE
  119. matrix_scan_count++;
  120. uint32_t timer_now = timer_read32();
  121. if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
  122. print("matrix scan frequency: ");
  123. pdec(matrix_scan_count);
  124. print("\n");
  125. matrix_print();
  126. matrix_timer = timer_now;
  127. matrix_scan_count = 0;
  128. }
  129. #endif
  130. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  131. select_row(i);
  132. wait_us(30); // without this wait read unstable value.
  133. matrix_row_t mask = debounce_mask(i);
  134. matrix_row_t cols = (read_cols(i) & mask) | (matrix[i] & ~mask);
  135. debounce_report(cols ^ matrix[i], i);
  136. matrix[i] = cols;
  137. unselect_rows();
  138. }
  139. matrix_scan_quantum();
  140. return 1;
  141. }
  142. inline
  143. bool matrix_is_on(uint8_t row, uint8_t col)
  144. {
  145. return (matrix[row] & ((matrix_row_t)1<<col));
  146. }
  147. inline
  148. matrix_row_t matrix_get_row(uint8_t row)
  149. {
  150. return matrix[row];
  151. }
  152. void matrix_print(void)
  153. {
  154. print("\nr/c 0123456789ABCDEF\n");
  155. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  156. phex(row); print(": ");
  157. pbin_reverse16(matrix_get_row(row));
  158. print("\n");
  159. }
  160. }
  161. uint8_t matrix_key_count(void)
  162. {
  163. uint8_t count = 0;
  164. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  165. count += bitpop16(matrix[i]);
  166. }
  167. return count;
  168. }
  169. /* Column pin configuration
  170. *
  171. * Pro Micro: 6 5 4 3 2 1 0
  172. * PD3 PD2 PD4 PC6 PD7 PE6 PB4
  173. *
  174. * Expander: 13 12 11 10 9 8 7
  175. */
  176. static void init_cols(void)
  177. {
  178. // Pro Micro
  179. DDRE &= ~(1<<PE6);
  180. PORTE |= (1<<PE6);
  181. DDRD &= ~(1<<PD2 | 1<<PD3 | 1<<PD4 | 1<<PD7);
  182. PORTD |= (1<<PD2 | 1<<PD3 | 1<<PD4 | 1<<PD7);
  183. DDRC &= ~(1<<PC6);
  184. PORTC |= (1<<PC6);
  185. DDRB &= ~(1<<PB4);
  186. PORTB |= (1<<PB4);
  187. // MCP23017
  188. expander_init();
  189. }
  190. static matrix_row_t read_cols(uint8_t row)
  191. {
  192. return expander_read_row() |
  193. (PIND&(1<<PD3) ? 0 : (1<<6)) |
  194. (PIND&(1<<PD2) ? 0 : (1<<5)) |
  195. (PIND&(1<<PD4) ? 0 : (1<<4)) |
  196. (PINC&(1<<PC6) ? 0 : (1<<3)) |
  197. (PIND&(1<<PD7) ? 0 : (1<<2)) |
  198. (PINE&(1<<PE6) ? 0 : (1<<1)) |
  199. (PINB&(1<<PB4) ? 0 : (1<<0)) ;
  200. }
  201. /* Row pin configuration
  202. *
  203. * Pro Micro: 0 1 2 3 4 5
  204. * F4 F5 F6 F7 B1 B2
  205. *
  206. * Expander: 0 1 2 3 4 5
  207. */
  208. static void unselect_rows(void)
  209. {
  210. // Pro Micro
  211. DDRF &= ~(1<<PF4 | 1<<PF5 | 1<<PF6 | 1<<PF7);
  212. PORTF &= ~(1<<PF4 | 1<<PF5 | 1<<PF6 | 1<<PF7);
  213. DDRB &= ~(1<<PB1 | 1<<PB2);
  214. PORTB &= ~(1<<PB1 | 1<<PB2);
  215. // Expander
  216. expander_unselect_rows();
  217. }
  218. static void select_row(uint8_t row)
  219. {
  220. // Pro Micro
  221. switch (row) {
  222. case 0:
  223. DDRF |= (1<<PF4);
  224. PORTF &= ~(1<<PF4);
  225. break;
  226. case 1:
  227. DDRF |= (1<<PF5);
  228. PORTF &= ~(1<<PF5);
  229. break;
  230. case 2:
  231. DDRF |= (1<<PF6);
  232. PORTF &= ~(1<<PF6);
  233. break;
  234. case 3:
  235. DDRF |= (1<<PF7);
  236. PORTF &= ~(1<<PF7);
  237. break;
  238. case 4:
  239. DDRB |= (1<<PB1);
  240. PORTB &= ~(1<<PB1);
  241. break;
  242. case 5:
  243. DDRB |= (1<<PB2);
  244. PORTB &= ~(1<<PB2);
  245. break;
  246. }
  247. expander_select_row(row);
  248. }