matrix.c 5.8 KB

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