3 Note to self: adapted from ergodox EZ matrix
4 The "column" and "row" in here actually refers to the opposite on the keyboard
5 see definition of KEYMAP in v1.h, the grid is transposed so that a "row" in here is actually a "column" on the physical keyboard
8 Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
9 Copyright 2013 Nicolas Poirey <nicolas.poirey@gmail.com>
11 This program is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #include "action_layer.h"
37 #include "frenchdev.h"
38 #ifdef DEBUG_MATRIX_SCAN_RATE
43 * This constant define not debouncing time in msecs, but amount of matrix
44 * scan loops which should be made to get stable debounced results.
46 * On Ergodox matrix scan rate is relatively low, because of slow I2C.
47 * Now it's only 317 scans/second, or about 3.15 msec/scan.
48 * According to Cherry specs, debouncing time is 5 msec.
50 * And so, there is no sense to have DEBOUNCE higher than 2.
56 static uint8_t debouncing = DEBOUNCE;
58 /* matrix state(1:on, 0:off) */
59 static matrix_row_t matrix[MATRIX_ROWS];
60 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
62 static matrix_row_t read_cols(uint8_t row);
63 static void init_cols(void);
64 static void unselect_rows(void);
65 static void select_row(uint8_t row);
67 static uint8_t mcp23018_reset_loop;
69 #ifdef DEBUG_MATRIX_SCAN_RATE
70 uint32_t matrix_timer;
71 uint32_t matrix_scan_count;
75 __attribute__ ((weak))
76 void matrix_init_user(void) {}
78 __attribute__ ((weak))
79 void matrix_scan_user(void) {}
81 __attribute__ ((weak))
82 void matrix_init_kb(void) {
86 __attribute__ ((weak))
87 void matrix_scan_kb(void) {
92 uint8_t matrix_rows(void)
98 uint8_t matrix_cols(void)
103 void matrix_init(void)
105 // initialize row and col
108 debug_keyboard = true;
111 mcp23018_status = init_mcp23018();
117 // initialize matrix state: all keys off
118 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
120 matrix_debouncing[i] = 0;
123 #ifdef DEBUG_MATRIX_SCAN_RATE
124 matrix_timer = timer_read32();
125 matrix_scan_count = 0;
128 matrix_init_quantum();
132 void matrix_power_up(void) {
133 mcp23018_status = init_mcp23018();
138 // initialize matrix state: all keys off
139 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
141 matrix_debouncing[i] = 0;
144 #ifdef DEBUG_MATRIX_SCAN_RATE
145 matrix_timer = timer_read32();
146 matrix_scan_count = 0;
151 uint8_t matrix_scan(void)
153 if (mcp23018_status) { // if there was an error
154 if (++mcp23018_reset_loop == 0) {
155 // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
156 // this will be approx bit more frequent than once per second
157 print("trying to reset mcp23018\n");
158 mcp23018_status = init_mcp23018();
159 if (mcp23018_status) {
160 print("left side not responding\n");
162 print("left side attached\n");
163 frenchdev_blink_all_leds();
168 #ifdef DEBUG_MATRIX_SCAN_RATE
171 uint32_t timer_now = timer_read32();
172 if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
173 print("matrix scan frequency: ");
174 pdec(matrix_scan_count);
177 matrix_timer = timer_now;
178 matrix_scan_count = 0;
182 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
184 wait_us(30); // without this wait read unstable value.
185 matrix_row_t cols = read_cols(i);
186 if (matrix_debouncing[i] != cols) {
187 matrix_debouncing[i] = cols;
189 debug("bounce!: "); debug_hex(debouncing); debug("\n");
191 debouncing = DEBOUNCE;
199 // this should be wait_ms(1) but has been left as-is at EZ's request
201 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
202 matrix[i] = matrix_debouncing[i];
207 matrix_scan_quantum();
212 bool matrix_is_modified(void)
214 if (debouncing) return false;
219 bool matrix_is_on(uint8_t row, uint8_t col)
221 return (matrix[row] & ((matrix_row_t)1<<col));
225 matrix_row_t matrix_get_row(uint8_t row)
230 void matrix_print(void)
232 print("\nr/c 0123456789ABCDEF\n");
233 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
234 phex(row); print(": ");
235 pbin_reverse16(matrix_get_row(row));
240 uint8_t matrix_key_count(void)
243 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
244 count += bitpop16(matrix[i]);
249 /* Column pin configuration
253 * pin: F0 F1 F4 F5 F6 F7
257 * pin: B5 B4 B3 B2 B1 B0
259 static void init_cols(void)
262 // not needed, already done as part of init_mcp23018()
265 // Input with pull-up(DDR:0, PORT:1)
266 DDRF &= ~(1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
267 PORTF |= (1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
270 static matrix_row_t read_cols(uint8_t row)
273 if (mcp23018_status) { // if there was an error
277 mcp23018_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (mcp23018_status) goto out;
278 mcp23018_status = i2c_write(GPIOB, I2C_TIMEOUT); if (mcp23018_status) goto out;
279 mcp23018_status = i2c_start(I2C_ADDR_READ, I2C_TIMEOUT); if (mcp23018_status) goto out;
280 data = i2c_read_nack(I2C_TIMEOUT); if (mcp23018_status < 0) goto out;
281 data = ~((uint8_t)mcp23018_status);
282 mcp23018_status = I2C_STATUS_SUCCESS;
290 (PINF&(1<<0) ? 0 : (1<<0)) |
291 (PINF&(1<<1) ? 0 : (1<<1)) |
292 (PINF&(1<<4) ? 0 : (1<<2)) |
293 (PINF&(1<<5) ? 0 : (1<<3)) |
294 (PINF&(1<<6) ? 0 : (1<<4)) |
295 (PINF&(1<<7) ? 0 : (1<<5)) ;
299 /* Row pin configuration
302 * row: 7 8 9 10 11 12 13
303 * pin: B0 B1 B2 B3 D2 D3 C6
307 * pin: A0 A1 A2 A3 A4 A5 A6
309 static void unselect_rows(void)
311 // unselect on mcp23018
312 if (mcp23018_status) { // if there was an error
315 // set all rows hi-Z : 1
316 mcp23018_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (mcp23018_status) goto out;
317 mcp23018_status = i2c_write(GPIOA, I2C_TIMEOUT); if (mcp23018_status) goto out;
318 mcp23018_status = i2c_write( 0xFF & ~(0<<8), I2C_TIMEOUT); if (mcp23018_status) goto out;
323 // unselect on teensy
324 // Hi-Z(DDR:0, PORT:0) to unselect
325 DDRB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
326 PORTB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
327 DDRD &= ~(1<<2 | 1<<3);
328 PORTD &= ~(1<<2 | 1<<3);
329 DDRC &= ~(1<<6 | 1<<7);
330 PORTC &= ~(1<<6 | 1<<7);
333 static void select_row(uint8_t row)
336 // select on mcp23018
337 if (mcp23018_status) { // if there was an error
340 // set active row low : 0
341 // set other rows hi-Z : 1
342 mcp23018_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (mcp23018_status) goto out;
343 mcp23018_status = i2c_write(GPIOA, I2C_TIMEOUT); if (mcp23018_status) goto out;
344 mcp23018_status = i2c_write( 0xFF & ~(1<<row) & ~(0<<8), I2C_TIMEOUT); if (mcp23018_status) goto out;
350 // Output low(DDR:1, PORT:0) to select