matrix.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. Copyright 2017 Balz Guenat
  3. based on work by Jun Wako <wakojun@gmail.com>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /*
  16. * scan matrix
  17. */
  18. #include <stdint.h>
  19. #include <stdbool.h>
  20. #include "wait.h"
  21. #include "print.h"
  22. #include "debug.h"
  23. #include "util.h"
  24. #include "timer.h"
  25. #include "matrix.h"
  26. #include "led.h"
  27. // #include "fc980c.h"
  28. // Timer resolution check
  29. #if (1000000/TIMER_RAW_FREQ > 20)
  30. # error "Timer resolution(>20us) is not enough for HHKB matrix scan tweak on V-USB."
  31. #endif
  32. /*
  33. * Pin configuration for ATMega32U4
  34. *
  35. * Row: PD4-6, PD7(~EN)
  36. * Col: PB0-3
  37. * Key: PC6(pull-uped)
  38. * Hys: PC7
  39. */
  40. static inline void KEY_ENABLE(void) { (PORTD &= ~(1<<7)); }
  41. static inline void KEY_UNABLE(void) { (PORTD |= (1<<7)); }
  42. static inline bool KEY_STATE(void) { return (PINC & (1<<6)); }
  43. static inline void KEY_HYS_ON(void) { (PORTC |= (1<<7)); }
  44. static inline void KEY_HYS_OFF(void) { (PORTC &= ~(1<<7)); }
  45. static inline void KEY_INIT(void)
  46. {
  47. /* Col */
  48. DDRB |= 0x0F;
  49. /* Key: input with pull-up */
  50. DDRC &= ~(1<<6);
  51. PORTC |= (1<<6);
  52. /* Hys */
  53. DDRC |= (1<<7);
  54. /* Row */
  55. DDRD |= 0xF0;
  56. KEY_UNABLE();
  57. KEY_HYS_OFF();
  58. }
  59. static inline void SET_ROW(uint8_t ROW)
  60. {
  61. // PD4-6
  62. PORTD = (PORTD & 0x8F) | ((ROW & 0x07) << 4);
  63. }
  64. static inline void SET_COL(uint8_t COL)
  65. {
  66. // PB0-3
  67. PORTB = (PORTB & 0xF0) | (COL & 0x0F);
  68. }
  69. static uint32_t matrix_last_modified = 0;
  70. // matrix state buffer(1:on, 0:off)
  71. static matrix_row_t *matrix;
  72. static matrix_row_t *matrix_prev;
  73. static matrix_row_t _matrix0[MATRIX_ROWS];
  74. static matrix_row_t _matrix1[MATRIX_ROWS];
  75. __attribute__ ((weak))
  76. void matrix_init_quantum(void) {
  77. matrix_init_kb();
  78. }
  79. __attribute__ ((weak))
  80. void matrix_scan_quantum(void) {
  81. matrix_scan_kb();
  82. }
  83. __attribute__ ((weak))
  84. void matrix_init_kb(void) {
  85. matrix_init_user();
  86. }
  87. __attribute__ ((weak))
  88. void matrix_scan_kb(void) {
  89. matrix_scan_user();
  90. }
  91. __attribute__ ((weak))
  92. void matrix_init_user(void) {
  93. }
  94. __attribute__ ((weak))
  95. void matrix_scan_user(void) {
  96. }
  97. void matrix_init(void)
  98. {
  99. debug_enable = true;
  100. debug_matrix = true;
  101. KEY_INIT();
  102. // LEDs on NumLock, CapsLock and ScrollLock(PB4, PB5, PB6)
  103. DDRB |= (1<<4) | (1<<5) | (1<<6);
  104. PORTB |= (1<<4) | (1<<5) | (1<<6);
  105. // initialize matrix state: all keys off
  106. for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
  107. for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
  108. matrix = _matrix0;
  109. matrix_prev = _matrix1;
  110. matrix_init_quantum();
  111. }
  112. uint8_t matrix_scan(void)
  113. {
  114. matrix_row_t *tmp;
  115. tmp = matrix_prev;
  116. matrix_prev = matrix;
  117. matrix = tmp;
  118. uint8_t row, col;
  119. for (col = 0; col < MATRIX_COLS; col++) {
  120. SET_COL(col);
  121. for (row = 0; row < MATRIX_ROWS; row++) {
  122. //KEY_SELECT(row, col);
  123. SET_ROW(row);
  124. _delay_us(2);
  125. // Not sure this is needed. This just emulates HHKB controller's behaviour.
  126. if (matrix_prev[row] & (1<<col)) {
  127. KEY_HYS_ON();
  128. }
  129. _delay_us(10);
  130. // NOTE: KEY_STATE is valid only in 20us after KEY_ENABLE.
  131. // If V-USB interrupts in this section we could lose 40us or so
  132. // and would read invalid value from KEY_STATE.
  133. uint8_t last = TIMER_RAW;
  134. KEY_ENABLE();
  135. // Wait for KEY_STATE outputs its value.
  136. _delay_us(2);
  137. if (KEY_STATE()) {
  138. matrix[row] &= ~(1<<col);
  139. } else {
  140. matrix[row] |= (1<<col);
  141. }
  142. // Ignore if this code region execution time elapses more than 20us.
  143. // MEMO: 20[us] * (TIMER_RAW_FREQ / 1000000)[count per us]
  144. // MEMO: then change above using this rule: a/(b/c) = a*1/(b/c) = a*(c/b)
  145. if (TIMER_DIFF_RAW(TIMER_RAW, last) > 20/(1000000/TIMER_RAW_FREQ)) {
  146. matrix[row] = matrix_prev[row];
  147. }
  148. _delay_us(5);
  149. KEY_HYS_OFF();
  150. KEY_UNABLE();
  151. // NOTE: KEY_STATE keep its state in 20us after KEY_ENABLE.
  152. // This takes 25us or more to make sure KEY_STATE returns to idle state.
  153. _delay_us(75);
  154. }
  155. if (matrix[row] ^ matrix_prev[row]) {
  156. matrix_last_modified = timer_read32();
  157. }
  158. }
  159. matrix_scan_quantum();
  160. return 1;
  161. }
  162. inline
  163. matrix_row_t matrix_get_row(uint8_t row) {
  164. return matrix[row];
  165. }
  166. void matrix_print(void)
  167. {
  168. #if (MATRIX_COLS <= 8)
  169. print("r/c 01234567\n");
  170. #elif (MATRIX_COLS <= 16)
  171. print("r/c 0123456789ABCDEF\n");
  172. #elif (MATRIX_COLS <= 32)
  173. print("r/c 0123456789ABCDEF0123456789ABCDEF\n");
  174. #endif
  175. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  176. #if (MATRIX_COLS <= 8)
  177. xprintf("%02X: %08b%s\n", row, bitrev(matrix_get_row(row)),
  178. #elif (MATRIX_COLS <= 16)
  179. xprintf("%02X: %016b%s\n", row, bitrev16(matrix_get_row(row)),
  180. #elif (MATRIX_COLS <= 32)
  181. xprintf("%02X: %032b%s\n", row, bitrev32(matrix_get_row(row)),
  182. #endif
  183. #ifdef MATRIX_HAS_GHOST
  184. matrix_has_ghost_in_row(row) ? " <ghost" : ""
  185. #else
  186. ""
  187. #endif
  188. );
  189. }
  190. }