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 Note for ErgoDox EZ customizers: Here be dragons!
9 This is not a file you want to be messing with.
10 All of the interesting stuff for you is under keymaps/ :)
13 Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
14 Copyright 2013 Nicolas Poirey <nicolas.poirey@gmail.com>
16 This program is free software: you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation, either version 2 of the License, or
19 (at your option) any later version.
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>.
37 #include "action_layer.h"
42 #include "frenchdev.h"
43 #ifdef DEBUG_MATRIX_SCAN_RATE
48 * This constant define not debouncing time in msecs, but amount of matrix
49 * scan loops which should be made to get stable debounced results.
51 * On Ergodox matrix scan rate is relatively low, because of slow I2C.
52 * Now it's only 317 scans/second, or about 3.15 msec/scan.
53 * According to Cherry specs, debouncing time is 5 msec.
55 * And so, there is no sense to have DEBOUNCE higher than 2.
61 static uint8_t debouncing = DEBOUNCE;
63 /* matrix state(1:on, 0:off) */
64 static matrix_row_t matrix[MATRIX_ROWS];
65 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
67 static matrix_row_t read_cols(uint8_t row);
68 static void init_cols(void);
69 static void unselect_rows(void);
70 static void select_row(uint8_t row);
72 static uint8_t mcp23018_reset_loop;
74 #ifdef DEBUG_MATRIX_SCAN_RATE
75 uint32_t matrix_timer;
76 uint32_t matrix_scan_count;
80 __attribute__ ((weak))
81 void matrix_init_user(void) {}
83 __attribute__ ((weak))
84 void matrix_scan_user(void) {}
86 __attribute__ ((weak))
87 void matrix_init_kb(void) {
91 __attribute__ ((weak))
92 void matrix_scan_kb(void) {
97 uint8_t matrix_rows(void)
103 uint8_t matrix_cols(void)
108 void matrix_init(void)
110 // initialize row and col
113 debug_keyboard = true;
116 mcp23018_status = init_mcp23018();
122 // initialize matrix state: all keys off
123 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
125 matrix_debouncing[i] = 0;
128 #ifdef DEBUG_MATRIX_SCAN_RATE
129 matrix_timer = timer_read32();
130 matrix_scan_count = 0;
133 matrix_init_quantum();
137 void matrix_power_up(void) {
138 mcp23018_status = init_mcp23018();
143 // initialize matrix state: all keys off
144 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
146 matrix_debouncing[i] = 0;
149 #ifdef DEBUG_MATRIX_SCAN_RATE
150 matrix_timer = timer_read32();
151 matrix_scan_count = 0;
156 uint8_t matrix_scan(void)
158 if (mcp23018_status) { // if there was an error
159 if (++mcp23018_reset_loop == 0) {
160 // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
161 // this will be approx bit more frequent than once per second
162 print("trying to reset mcp23018\n");
163 mcp23018_status = init_mcp23018();
164 if (mcp23018_status) {
165 print("left side not responding\n");
167 print("left side attached\n");
168 frenchdev_blink_all_leds();
173 #ifdef DEBUG_MATRIX_SCAN_RATE
176 uint32_t timer_now = timer_read32();
177 if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
178 print("matrix scan frequency: ");
179 pdec(matrix_scan_count);
182 matrix_timer = timer_now;
183 matrix_scan_count = 0;
187 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
189 wait_us(30); // without this wait read unstable value.
190 matrix_row_t cols = read_cols(i);
191 if (matrix_debouncing[i] != cols) {
192 matrix_debouncing[i] = cols;
194 debug("bounce!: "); debug_hex(debouncing); debug("\n");
196 debouncing = DEBOUNCE;
204 // this should be wait_ms(1) but has been left as-is at EZ's request
206 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
207 matrix[i] = matrix_debouncing[i];
212 matrix_scan_quantum();
217 bool matrix_is_modified(void)
219 if (debouncing) return false;
224 bool matrix_is_on(uint8_t row, uint8_t col)
226 return (matrix[row] & ((matrix_row_t)1<<col));
230 matrix_row_t matrix_get_row(uint8_t row)
235 void matrix_print(void)
237 print("\nr/c 0123456789ABCDEF\n");
238 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
239 phex(row); print(": ");
240 pbin_reverse16(matrix_get_row(row));
245 uint8_t matrix_key_count(void)
248 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
249 count += bitpop16(matrix[i]);
254 /* Column pin configuration
258 * pin: F0 F1 F4 F5 F6 F7
262 * pin: B5 B4 B3 B2 B1 B0
264 static void init_cols(void)
267 // not needed, already done as part of init_mcp23018()
270 // Input with pull-up(DDR:0, PORT:1)
271 DDRF &= ~(1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
272 PORTF |= (1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
275 static matrix_row_t read_cols(uint8_t row)
278 if (mcp23018_status) { // if there was an error
282 mcp23018_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (mcp23018_status) goto out;
283 mcp23018_status = i2c_write(GPIOB, I2C_TIMEOUT); if (mcp23018_status) goto out;
284 mcp23018_status = i2c_start(I2C_ADDR_READ, I2C_TIMEOUT); if (mcp23018_status) goto out;
285 data = i2c_read_nack(I2C_TIMEOUT); if (mcp23018_status < 0) goto out;
286 data = ~((uint8_t)mcp23018_status);
287 mcp23018_status = I2C_STATUS_SUCCESS;
295 (PINF&(1<<0) ? 0 : (1<<0)) |
296 (PINF&(1<<1) ? 0 : (1<<1)) |
297 (PINF&(1<<4) ? 0 : (1<<2)) |
298 (PINF&(1<<5) ? 0 : (1<<3)) |
299 (PINF&(1<<6) ? 0 : (1<<4)) |
300 (PINF&(1<<7) ? 0 : (1<<5)) ;
304 /* Row pin configuration
307 * row: 7 8 9 10 11 12 13
308 * pin: B0 B1 B2 B3 D2 D3 C6
312 * pin: A0 A1 A2 A3 A4 A5 A6
314 static void unselect_rows(void)
316 // unselect on mcp23018
317 if (mcp23018_status) { // if there was an error
320 // set all rows hi-Z : 1
321 mcp23018_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (mcp23018_status) goto out;
322 mcp23018_status = i2c_write(GPIOA, I2C_TIMEOUT); if (mcp23018_status) goto out;
323 mcp23018_status = i2c_write( 0xFF & ~(0<<8), I2C_TIMEOUT); if (mcp23018_status) goto out;
328 // unselect on teensy
329 // Hi-Z(DDR:0, PORT:0) to unselect
330 DDRB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
331 PORTB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
332 DDRD &= ~(1<<2 | 1<<3);
333 PORTD &= ~(1<<2 | 1<<3);
334 DDRC &= ~(1<<6 | 1<<7);
335 PORTC &= ~(1<<6 | 1<<7);
338 static void select_row(uint8_t row)
341 // select on mcp23018
342 if (mcp23018_status) { // if there was an error
345 // set active row low : 0
346 // set other rows hi-Z : 1
347 mcp23018_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (mcp23018_status) goto out;
348 mcp23018_status = i2c_write(GPIOA, I2C_TIMEOUT); if (mcp23018_status) goto out;
349 mcp23018_status = i2c_write( 0xFF & ~(1<<row) & ~(0<<8), I2C_TIMEOUT); if (mcp23018_status) goto out;
355 // Output low(DDR:1, PORT:0) to select