2 Copyright 2011 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.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <util/delay.h>
35 // matrix power saving
36 #define MATRIX_POWER_SAVE 10000
37 static uint32_t matrix_last_modified = 0;
39 // matrix state buffer(1:on, 0:off)
40 static matrix_row_t *matrix;
41 static matrix_row_t *matrix_prev;
42 static matrix_row_t _matrix0[MATRIX_ROWS];
43 static matrix_row_t _matrix1[MATRIX_ROWS];
47 uint8_t matrix_rows(void)
53 uint8_t matrix_cols(void)
58 void matrix_init(void)
62 debug_keyboard = true;
67 // initialize matrix state: all keys off
68 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
69 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
71 matrix_prev = _matrix1;
74 __attribute__ ((weak))
75 void matrix_scan_user(void) {
78 void matrix_scan_kb(void) {
82 uint8_t matrix_scan(void)
91 if (!KEY_POWER_STATE()) KEY_POWER_ON();
92 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
93 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
97 // Not sure this is needed. This just emulates HHKB controller's behaviour.
98 if (matrix_prev[row] & (1<<col)) {
103 // NOTE: KEY_STATE is valid only in 20us after KEY_ENABLE.
104 // If V-USB interrupts in this section we could lose 40us or so
105 // and would read invalid value from KEY_STATE.
106 uint8_t last = TIMER_RAW;
110 // Wait for KEY_STATE outputs its value.
111 // 1us was ok on one HHKB, but not worked on another.
112 // no wait doesn't work on Teensy++ with pro(1us works)
113 // no wait does work on tmk PCB(8MHz) with pro2
114 // 1us wait does work on both of above
115 // 1us wait doesn't work on tmk(16MHz)
116 // 5us wait does work on tmk(16MHz)
117 // 5us wait does work on tmk(16MHz/2)
118 // 5us wait does work on tmk(8MHz)
119 // 10us wait does work on Teensy++ with pro
120 // 10us wait does work on 328p+iwrap with pro
121 // 10us wait doesn't work on tmk PCB(8MHz) with pro2(very lagged scan)
125 matrix[row] &= ~(1<<col);
127 matrix[row] |= (1<<col);
130 // Ignore if this code region execution time elapses more than 20us.
131 // MEMO: 20[us] * (TIMER_RAW_FREQ / 1000000)[count per us]
132 // MEMO: then change above using this rule: a/(b/c) = a*1/(b/c) = a*(c/b)
133 if (TIMER_DIFF_RAW(TIMER_RAW, last) > 20/(1000000/TIMER_RAW_FREQ)) {
134 matrix[row] = matrix_prev[row];
141 // NOTE: KEY_STATE keep its state in 20us after KEY_ENABLE.
142 // This takes 25us or more to make sure KEY_STATE returns to idle state.
144 // Looks like JP needs faster scan due to its twice larger matrix
145 // or it can drop keys in fast key typing
151 if (matrix[row] ^ matrix_prev[row]) matrix_last_modified = timer_read32();
154 if (KEY_POWER_STATE() &&
155 (USB_DeviceState == DEVICE_STATE_Suspended ||
156 USB_DeviceState == DEVICE_STATE_Unattached ) &&
157 timer_elapsed32(matrix_last_modified) > MATRIX_POWER_SAVE) {
159 suspend_power_down();
162 matrix_scan_quantum();
167 bool matrix_is_modified(void)
169 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
170 if (matrix[i] != matrix_prev[i])
177 bool matrix_has_ghost(void)
183 bool matrix_is_on(uint8_t row, uint8_t col)
185 return (matrix[row] & (1<<col));
189 matrix_row_t matrix_get_row(uint8_t row)
194 void matrix_print(void)
196 print("\nr/c 01234567\n");
197 for (uint8_t row = 0; row < matrix_rows(); row++) {
198 xprintf("%02X: %08b\n", row, bitrev(matrix_get_row(row)));
202 uint8_t matrix_key_count(void) {
204 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
205 count += bitpop16(matrix_get_row(r));
210 void matrix_power_up(void) {
213 void matrix_power_down(void) {