]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - hhkb/matrix.c
a425439ccab0887184096f8aa8f5a4c001849e19
[max/tmk_keyboard.git] / hhkb / matrix.c
1 /*
2  * scan matrix
3  */
4 #include <avr/io.h>
5 #include <util/delay.h>
6 #include "keymap.h"
7 #include "matrix.h"
8 #include "print.h"
9
10 // matrix is active low. (key on: 0/key off: 1)
11 //
12 // HHKB has no ghost and no bounce.
13 // row: HC4051 select input channel(0-8)
14 //      PB0, PB1, PB2(A, B, C)
15 // col: LS145 select low output line(0-8)
16 //      PB3, PB4, PB5, PB6(A, B, C, D)
17 //      use D as ENABLE: (enable: 0/unenable: 1)
18 // key: KEY: (on: 0/ off:1)
19 //      UNKNOWN: unknown whether input or output
20 //      PE6,PE7(KEY, UNKNOWN)
21 #define COL_ENABLE              (1<<6)
22 #define KEY_SELELCT(ROW, COL)   (PORTB = COL_ENABLE|(((COL)&0x07)<<3)|((ROW)&0x07))
23 #define KEY_ENABLE              (PORTB &= ~COL_ENABLE)
24 #define KEY_UNABLE              (PORTB |=  COL_ENABLE)
25 #define KEY_ON                  ((PINE&(1<<6)) ? false : true)
26
27 // matrix state buffer
28 uint8_t *matrix;
29 uint8_t *matrix_prev;
30 static uint8_t _matrix0[MATRIX_ROWS];
31 static uint8_t _matrix1[MATRIX_ROWS];
32
33
34 static bool matrix_has_ghost_in_row(int row);
35 static int bit_pop(uint8_t bits);
36
37
38 inline
39 int matrix_rows(void) {
40     return MATRIX_ROWS;
41 }
42
43 inline
44 int matrix_cols(void) {
45     return MATRIX_COLS;
46 }
47
48 // this must be called once before matrix_scan.
49 void matrix_init(void)
50 {
51     // row & col output(PB0-6)
52     DDRB = 0xFF;
53     PORTB = KEY_SELELCT(0, 0);
54     // KEY & VALID input with pullup(PE6,7)
55     DDRE = 0x3F;
56     PORTE = 0xC0;
57
58     // initialize matrix state: all keys off
59     for (int i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0xFF;
60     for (int i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0xFF;
61     matrix = _matrix0;
62     matrix_prev = _matrix1;
63 }
64
65 int matrix_scan(void)
66 {
67     uint8_t *tmp;
68
69     tmp = matrix_prev;
70     matrix_prev = matrix;
71     matrix = tmp;
72
73     for (int row = 0; row < MATRIX_ROWS; row++) {
74         for (int col = 0; col < MATRIX_COLS; col++) {
75             KEY_SELELCT(row, col);
76             _delay_us(50);  // from logic analyzer chart
77             KEY_ENABLE;
78             _delay_us(10);  // from logic analyzer chart
79             if (KEY_ON) {
80                 matrix[row] &= ~(1<<col);
81             } else {
82                 matrix[row] |= (1<<col);
83             }
84             KEY_UNABLE;
85             _delay_us(150);  // from logic analyzer chart
86         }
87     }
88     return 1;
89 }
90
91 bool matrix_is_modified(void) {
92     for (int i = 0; i < MATRIX_ROWS; i++) {
93         if (matrix[i] != matrix_prev[i])
94             return true;
95     }
96     return false;
97 }
98
99 inline
100 bool matrix_has_ghost(void) {
101     return false;
102 }
103
104 inline
105 uint16_t matrix_get_row(int row) {
106     return matrix[row];
107 }
108
109 void matrix_print(void) {
110     print("\nr/c 01234567\n");
111     for (int row = 0; row < matrix_rows(); row++) {
112         phex(row); print(": ");
113         pbin_reverse(matrix_get_row(row));
114         if (matrix_has_ghost_in_row(row)) {
115             print(" <ghost");
116         }
117         print("\n");
118     }
119 }
120
121 int matrix_key_count(void) {
122     int count = 0;
123     for (int i = 0; i < MATRIX_ROWS; i++) {
124         count += bit_pop(~matrix[i]);
125     }
126     return count;
127 }
128
129 inline
130 static bool matrix_has_ghost_in_row(int row) {
131     return false;
132 }
133
134 static int bit_pop(uint8_t bits) {
135     int c;
136     for (c = 0; bits; c++)
137         bits &= bits -1;
138     return c;
139 }