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.

220 lines
5.5 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. // #include QMK_KEYBOARD_H
  29. // Timer resolution check
  30. #if (1000000/TIMER_RAW_FREQ > 20)
  31. # error "Timer resolution(>20us) is not enough for HHKB matrix scan tweak on V-USB."
  32. #endif
  33. /*
  34. * Pin configuration for ATMega32U4
  35. *
  36. * Row: PD4-6, PD7(~EN)
  37. * Col: PB0-3
  38. * Key: PC6(pull-uped)
  39. * Hys: PC7
  40. */
  41. static inline void KEY_ENABLE(void) { (PORTD &= ~(1<<7)); }
  42. static inline void KEY_UNABLE(void) { (PORTD |= (1<<7)); }
  43. static inline bool KEY_STATE(void) { return (PINC & (1<<6)); }
  44. static inline void KEY_HYS_ON(void) { (PORTC |= (1<<7)); }
  45. static inline void KEY_HYS_OFF(void) { (PORTC &= ~(1<<7)); }
  46. static inline void KEY_INIT(void)
  47. {
  48. /* Col */
  49. DDRB |= 0x0F;
  50. /* Key: input with pull-up */
  51. DDRC &= ~(1<<6);
  52. PORTC |= (1<<6);
  53. /* Hys */
  54. DDRC |= (1<<7);
  55. /* Row */
  56. DDRD |= 0xF0;
  57. KEY_UNABLE();
  58. KEY_HYS_OFF();
  59. }
  60. static inline void SET_ROW(uint8_t ROW)
  61. {
  62. // PD4-6
  63. PORTD = (PORTD & 0x8F) | ((ROW & 0x07) << 4);
  64. }
  65. static inline void SET_COL(uint8_t COL)
  66. {
  67. // PB0-3
  68. PORTB = (PORTB & 0xF0) | (COL & 0x0F);
  69. }
  70. static uint32_t matrix_last_modified = 0;
  71. // matrix state buffer(1:on, 0:off)
  72. static matrix_row_t *matrix;
  73. static matrix_row_t *matrix_prev;
  74. static matrix_row_t _matrix0[MATRIX_ROWS];
  75. static matrix_row_t _matrix1[MATRIX_ROWS];
  76. __attribute__ ((weak))
  77. void matrix_init_kb(void) {
  78. matrix_init_user();
  79. }
  80. __attribute__ ((weak))
  81. void matrix_scan_kb(void) {
  82. matrix_scan_user();
  83. }
  84. __attribute__ ((weak))
  85. void matrix_init_user(void) {
  86. }
  87. __attribute__ ((weak))
  88. void matrix_scan_user(void) {
  89. }
  90. void matrix_init(void)
  91. {
  92. debug_enable = true;
  93. debug_matrix = true;
  94. KEY_INIT();
  95. // LEDs on NumLock, CapsLock and ScrollLock(PB4, PB5, PB6)
  96. DDRB |= (1<<4) | (1<<5) | (1<<6);
  97. PORTB |= (1<<4) | (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_quantum();
  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_quantum();
  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. }