You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

217 lines
5.6 KiB

  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. // Timer resolution check
  28. #if (1000000/TIMER_RAW_FREQ > 20)
  29. # error "Timer resolution(>20us) is not enough for HHKB matrix scan tweak on V-USB."
  30. #endif
  31. /*
  32. * Pin configuration for ATMega32U4
  33. *
  34. * Row: PD4-6, 7(~EN)
  35. * Col: PB0-2, 3(Z5 ~EN), 4(Z4 ~EN)
  36. * Key: PC6(pull-uped)
  37. * Hys: PC7
  38. */
  39. static inline void KEY_ENABLE(void) { (PORTD &= ~(1<<7)); }
  40. static inline void KEY_UNABLE(void) { (PORTD |= (1<<7)); }
  41. static inline bool KEY_STATE(void) { return (PINC & (1<<6)); }
  42. static inline void KEY_HYS_ON(void) { (PORTC |= (1<<7)); }
  43. static inline void KEY_HYS_OFF(void) { (PORTC &= ~(1<<7)); }
  44. static inline void KEY_INIT(void)
  45. {
  46. /* Col */
  47. DDRB |= 0x1F;
  48. /* Key: input with pull-up */
  49. DDRC &= ~(1<<6);
  50. PORTC |= (1<<6);
  51. /* Hys */
  52. DDRC |= (1<<7);
  53. /* Row */
  54. DDRD |= 0xF0;
  55. KEY_UNABLE();
  56. KEY_HYS_OFF();
  57. }
  58. static inline void SET_ROW(uint8_t ROW)
  59. {
  60. // set row with unabling key
  61. PORTD = (PORTD & 0x0F) | (1<<7) | ((ROW & 0x07) << 4);
  62. }
  63. static inline void SET_COL(uint8_t COL)
  64. {
  65. // |PB3(Z5 ~EN)|PB4(Z4 ~EN)
  66. // --------|-----------|-----------
  67. // Col:0-7 |high |low
  68. // Col:8-F |low |high
  69. PORTB = (PORTB & 0xE0) | ((COL & 0x08) ? 1<<4 : 1<<3) | (COL & 0x07);
  70. }
  71. static uint32_t matrix_last_modified = 0;
  72. // matrix state buffer(1:on, 0:off)
  73. static matrix_row_t *matrix;
  74. static matrix_row_t *matrix_prev;
  75. static matrix_row_t _matrix0[MATRIX_ROWS];
  76. static matrix_row_t _matrix1[MATRIX_ROWS];
  77. __attribute__ ((weak))
  78. void matrix_init_kb(void) {
  79. matrix_init_user();
  80. }
  81. __attribute__ ((weak))
  82. void matrix_scan_kb(void) {
  83. matrix_scan_user();
  84. }
  85. __attribute__ ((weak))
  86. void matrix_init_user(void) {
  87. }
  88. __attribute__ ((weak))
  89. void matrix_scan_user(void) {
  90. }
  91. void matrix_init(void)
  92. {
  93. KEY_INIT();
  94. // LEDs on CapsLock and Insert
  95. DDRB |= (1<<5) | (1<<6);
  96. PORTB |= (1<<5) | (1<<6);
  97. // initialize matrix state: all keys off
  98. for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
  99. for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
  100. matrix = _matrix0;
  101. matrix_prev = _matrix1;
  102. matrix_init_quantum();
  103. }
  104. uint8_t matrix_scan(void)
  105. {
  106. matrix_row_t *tmp;
  107. tmp = matrix_prev;
  108. matrix_prev = matrix;
  109. matrix = tmp;
  110. uint8_t row, col;
  111. for (col = 0; col < MATRIX_COLS; col++) {
  112. SET_COL(col);
  113. for (row = 0; row < MATRIX_ROWS; row++) {
  114. //KEY_SELECT(row, col);
  115. SET_ROW(row);
  116. _delay_us(2);
  117. // Not sure this is needed. This just emulates HHKB controller's behaviour.
  118. if (matrix_prev[row] & (1<<col)) {
  119. KEY_HYS_ON();
  120. }
  121. _delay_us(10);
  122. // NOTE: KEY_STATE is valid only in 20us after KEY_ENABLE.
  123. // If V-USB interrupts in this section we could lose 40us or so
  124. // and would read invalid value from KEY_STATE.
  125. uint8_t last = TIMER_RAW;
  126. KEY_ENABLE();
  127. // Wait for KEY_STATE outputs its value.
  128. _delay_us(2);
  129. if (KEY_STATE()) {
  130. matrix[row] &= ~(1<<col);
  131. } else {
  132. matrix[row] |= (1<<col);
  133. }
  134. // Ignore if this code region execution time elapses more than 20us.
  135. // MEMO: 20[us] * (TIMER_RAW_FREQ / 1000000)[count per us]
  136. // MEMO: then change above using this rule: a/(b/c) = a*1/(b/c) = a*(c/b)
  137. if (TIMER_DIFF_RAW(TIMER_RAW, last) > 20/(1000000/TIMER_RAW_FREQ)) {
  138. matrix[row] = matrix_prev[row];
  139. }
  140. _delay_us(5);
  141. KEY_HYS_OFF();
  142. KEY_UNABLE();
  143. // NOTE: KEY_STATE keep its state in 20us after KEY_ENABLE.
  144. // This takes 25us or more to make sure KEY_STATE returns to idle state.
  145. _delay_us(75);
  146. }
  147. if (matrix[row] ^ matrix_prev[row]) {
  148. matrix_last_modified = timer_read32();
  149. }
  150. }
  151. matrix_scan_quantum();
  152. return 1;
  153. }
  154. inline
  155. matrix_row_t matrix_get_row(uint8_t row) {
  156. return matrix[row];
  157. }
  158. void matrix_print(void)
  159. {
  160. #if (MATRIX_COLS <= 8)
  161. print("r/c 01234567\n");
  162. #elif (MATRIX_COLS <= 16)
  163. print("r/c 0123456789ABCDEF\n");
  164. #elif (MATRIX_COLS <= 32)
  165. print("r/c 0123456789ABCDEF0123456789ABCDEF\n");
  166. #endif
  167. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  168. #if (MATRIX_COLS <= 8)
  169. xprintf("%02X: %08b%s\n", row, bitrev(matrix_get_row(row)),
  170. #elif (MATRIX_COLS <= 16)
  171. xprintf("%02X: %016b%s\n", row, bitrev16(matrix_get_row(row)),
  172. #elif (MATRIX_COLS <= 32)
  173. xprintf("%02X: %032b%s\n", row, bitrev32(matrix_get_row(row)),
  174. #endif
  175. #ifdef MATRIX_HAS_GHOST
  176. matrix_has_ghost_in_row(row) ? " <ghost" : ""
  177. #else
  178. ""
  179. #endif
  180. );
  181. }
  182. }