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.

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