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 #include "i2cmaster.h"
44 #ifdef DEBUG_MATRIX_SCAN_RATE
49 * This constant define not debouncing time in msecs, but amount of matrix
50 * scan loops which should be made to get stable debounced results.
52 * On Ergodox matrix scan rate is relatively low, because of slow I2C.
53 * Now it's only 317 scans/second, or about 3.15 msec/scan.
54 * According to Cherry specs, debouncing time is 5 msec.
56 * And so, there is no sense to have DEBOUNCE higher than 2.
62 static uint8_t debouncing = DEBOUNCE;
64 /* matrix state(1:on, 0:off) */
65 static matrix_row_t matrix[MATRIX_ROWS];
66 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
68 static matrix_row_t read_cols(uint8_t row);
69 static void init_cols(void);
70 static void unselect_rows(void);
71 static void select_row(uint8_t row);
73 static uint8_t mcp23018_reset_loop;
75 #ifdef DEBUG_MATRIX_SCAN_RATE
76 uint32_t matrix_timer;
77 uint32_t matrix_scan_count;
81 __attribute__ ((weak))
82 void matrix_init_user(void) {}
84 __attribute__ ((weak))
85 void matrix_scan_user(void) {}
87 __attribute__ ((weak))
88 void matrix_init_kb(void) {
92 __attribute__ ((weak))
93 void matrix_scan_kb(void) {
98 uint8_t matrix_rows(void)
104 uint8_t matrix_cols(void)
109 void matrix_init(void)
111 // initialize row and col
114 debug_keyboard = true;
117 mcp23018_status = init_mcp23018();
123 // initialize matrix state: all keys off
124 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
126 matrix_debouncing[i] = 0;
129 #ifdef DEBUG_MATRIX_SCAN_RATE
130 matrix_timer = timer_read32();
131 matrix_scan_count = 0;
134 matrix_init_quantum();
138 void matrix_power_up(void) {
139 mcp23018_status = init_mcp23018();
144 // initialize matrix state: all keys off
145 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
147 matrix_debouncing[i] = 0;
150 #ifdef DEBUG_MATRIX_SCAN_RATE
151 matrix_timer = timer_read32();
152 matrix_scan_count = 0;
157 uint8_t matrix_scan(void)
159 if (mcp23018_status) { // if there was an error
160 if (++mcp23018_reset_loop == 0) {
161 // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
162 // this will be approx bit more frequent than once per second
163 print("trying to reset mcp23018\n");
164 mcp23018_status = init_mcp23018();
165 if (mcp23018_status) {
166 print("left side not responding\n");
168 print("left side attached\n");
169 frenchdev_blink_all_leds();
174 #ifdef DEBUG_MATRIX_SCAN_RATE
177 uint32_t timer_now = timer_read32();
178 if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
179 print("matrix scan frequency: ");
180 pdec(matrix_scan_count);
183 matrix_timer = timer_now;
184 matrix_scan_count = 0;
188 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
190 wait_us(30); // without this wait read unstable value.
191 matrix_row_t cols = read_cols(i);
192 if (matrix_debouncing[i] != cols) {
193 matrix_debouncing[i] = cols;
195 debug("bounce!: "); debug_hex(debouncing); debug("\n");
197 debouncing = DEBOUNCE;
205 // this should be wait_ms(1) but has been left as-is at EZ's request
207 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
208 matrix[i] = matrix_debouncing[i];
213 matrix_scan_quantum();
218 bool matrix_is_modified(void)
220 if (debouncing) return false;
225 bool matrix_is_on(uint8_t row, uint8_t col)
227 return (matrix[row] & ((matrix_row_t)1<<col));
231 matrix_row_t matrix_get_row(uint8_t row)
236 void matrix_print(void)
238 print("\nr/c 0123456789ABCDEF\n");
239 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
240 phex(row); print(": ");
241 pbin_reverse16(matrix_get_row(row));
246 uint8_t matrix_key_count(void)
249 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
250 count += bitpop16(matrix[i]);
255 /* Column pin configuration
259 * pin: F0 F1 F4 F5 F6 F7
263 * pin: B5 B4 B3 B2 B1 B0
265 static void init_cols(void)
268 // not needed, already done as part of init_mcp23018()
271 // Input with pull-up(DDR:0, PORT:1)
272 DDRF &= ~(1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
273 PORTF |= (1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
276 static matrix_row_t read_cols(uint8_t row)
279 if (mcp23018_status) { // if there was an error
283 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
284 mcp23018_status = i2c_write(GPIOB); if (mcp23018_status) goto out;
285 mcp23018_status = i2c_start(I2C_ADDR_READ); if (mcp23018_status) goto out;
286 data = i2c_readNak();
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); if (mcp23018_status) goto out;
322 mcp23018_status = i2c_write(GPIOA); if (mcp23018_status) goto out;
323 mcp23018_status = i2c_write( 0xFF
325 ); if (mcp23018_status) goto out;
330 // unselect on teensy
331 // Hi-Z(DDR:0, PORT:0) to unselect
332 DDRB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
333 PORTB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
334 DDRD &= ~(1<<2 | 1<<3);
335 PORTD &= ~(1<<2 | 1<<3);
336 DDRC &= ~(1<<6 | 1<<7);
337 PORTC &= ~(1<<6 | 1<<7);
340 static void select_row(uint8_t row)
343 // select on mcp23018
344 if (mcp23018_status) { // if there was an error
347 // set active row low : 0
348 // set other rows hi-Z : 1
349 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
350 mcp23018_status = i2c_write(GPIOA); if (mcp23018_status) goto out;
351 mcp23018_status = i2c_write( 0xFF & ~(1<<row)
353 ); if (mcp23018_status) goto out;
359 // Output low(DDR:1, PORT:0) to select