matrix.c 5.7 KB

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