3 Note for ErgoDox EZ customizers: Here be dragons!
4 This is not a file you want to be messing with.
5 All of the interesting stuff for you is under keymaps/ :)
8 Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
10 This program is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
31 #include "action_layer.h"
37 #include QMK_KEYBOARD_H
38 #ifdef DEBUG_MATRIX_SCAN_RATE
43 * This constant define not debouncing time in msecs, assuming eager_pr.
45 * On Ergodox matrix scan rate is relatively low, because of slow I2C.
46 * Now it's only 317 scans/second, or about 3.15 msec/scan.
47 * According to Cherry specs, debouncing time is 5 msec.
49 * However, some switches seem to have higher debouncing requirements, or
50 * something else might be wrong. (Also, the scan speed has improved since
51 * that comment was written.)
58 /* matrix state(1:on, 0:off) */
59 static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
60 static matrix_row_t matrix[MATRIX_ROWS]; // debounced values
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;
68 // static uint16_t mcp23018_reset_loop;
70 #ifdef DEBUG_MATRIX_SCAN_RATE
71 uint32_t matrix_timer;
72 uint32_t matrix_scan_count;
75 __attribute__((weak)) void matrix_init_user(void) {}
77 __attribute__((weak)) void matrix_scan_user(void) {}
79 __attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); }
81 __attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); }
83 inline uint8_t matrix_rows(void) { return MATRIX_ROWS; }
85 inline uint8_t matrix_cols(void) { return MATRIX_COLS; }
87 void matrix_init(void) {
88 // initialize row and col
90 mcp23018_status = init_mcp23018();
95 // initialize matrix state: all keys off
96 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
101 #ifdef DEBUG_MATRIX_SCAN_RATE
102 matrix_timer = timer_read32();
103 matrix_scan_count = 0;
105 debounce_init(MATRIX_ROWS);
106 matrix_init_quantum();
109 void matrix_power_up(void) {
110 mcp23018_status = init_mcp23018();
115 // initialize matrix state: all keys off
116 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
120 #ifdef DEBUG_MATRIX_SCAN_RATE
121 matrix_timer = timer_read32();
122 matrix_scan_count = 0;
126 uint8_t matrix_scan(void) {
127 if (mcp23018_status) { // if there was an error
128 if (++mcp23018_reset_loop == 0) {
129 // if (++mcp23018_reset_loop >= 1300) {
130 // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
131 // this will be approx bit more frequent than once per second
132 print("trying to reset mcp23018\n");
133 mcp23018_status = init_mcp23018();
134 if (mcp23018_status) {
135 print("left side not responding\n");
137 print("left side attached\n");
138 ergodox_blink_all_leds();
143 #ifdef DEBUG_MATRIX_SCAN_RATE
146 uint32_t timer_now = timer_read32();
147 if (TIMER_DIFF_32(timer_now, matrix_timer) > 1000) {
148 print("matrix scan frequency: ");
149 pdec(matrix_scan_count);
152 matrix_timer = timer_now;
153 matrix_scan_count = 0;
158 mcp23018_status = ergodox_left_leds_update();
160 for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) {
161 // select rows from left and right hands
163 select_row(i + MATRIX_ROWS_PER_SIDE);
165 // we don't need a 30us delay anymore, because selecting a
166 // left-hand row requires more than 30us for i2c.
168 // grab left + right cols.
169 raw_matrix[i] = read_cols(i);
170 raw_matrix[i+MATRIX_ROWS_PER_SIDE] = read_cols(i+MATRIX_ROWS_PER_SIDE);
175 debounce(raw_matrix, matrix, MATRIX_ROWS, true);
176 matrix_scan_quantum();
181 bool matrix_is_modified(void) // deprecated and evidently not called.
186 inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); }
188 inline matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; }
190 void matrix_print(void) {
191 print("\nr/c 0123456789ABCDEF\n");
192 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
195 pbin_reverse16(matrix_get_row(row));
200 uint8_t matrix_key_count(void) {
202 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
203 count += bitpop16(matrix[i]);
208 /* Column pin configuration
212 * pin: F0 F1 F4 F5 F6 F7
216 * pin: B5 B4 B3 B2 B1 B0
218 static void init_cols(void) {
220 // not needed, already done as part of init_mcp23018()
223 // Input with pull-up(DDR:0, PORT:1)
224 DDRF &= ~(1 << 7 | 1 << 6 | 1 << 5 | 1 << 4 | 1 << 1 | 1 << 0);
225 PORTF |= (1 << 7 | 1 << 6 | 1 << 5 | 1 << 4 | 1 << 1 | 1 << 0);
228 static matrix_row_t read_cols(uint8_t row) {
230 if (mcp23018_status) { // if there was an error
234 mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT);
235 if (mcp23018_status) goto out;
236 mcp23018_status = i2c_write(GPIOB, ERGODOX_EZ_I2C_TIMEOUT);
237 if (mcp23018_status) goto out;
238 mcp23018_status = i2c_start(I2C_ADDR_READ, ERGODOX_EZ_I2C_TIMEOUT);
239 if (mcp23018_status) goto out;
240 mcp23018_status = i2c_read_nack(ERGODOX_EZ_I2C_TIMEOUT);
241 if (mcp23018_status < 0) goto out;
242 data = ~((uint8_t)mcp23018_status);
243 mcp23018_status = I2C_STATUS_SUCCESS;
250 * bitmask is 0b11110011, but we want those all
251 * in the lower six bits.
252 * we'll return 1s for the top two, but that's harmless.
255 return ~((PINF & 0x03) | ((PINF & 0xF0) >> 2));
259 /* Row pin configuration
262 * row: 7 8 9 10 11 12 13
263 * pin: B0 B1 B2 B3 D2 D3 C6
267 * pin: A0 A1 A2 A3 A4 A5 A6
269 static void unselect_rows(void) {
270 // no need to unselect on mcp23018, because the select step sets all
271 // the other row bits high, and it's not changing to a different
274 // unselect on teensy
275 // Hi-Z(DDR:0, PORT:0) to unselect
276 DDRB &= ~(1 << 0 | 1 << 1 | 1 << 2 | 1 << 3);
277 PORTB &= ~(1 << 0 | 1 << 1 | 1 << 2 | 1 << 3);
278 DDRD &= ~(1 << 2 | 1 << 3);
279 PORTD &= ~(1 << 2 | 1 << 3);
284 static void select_row(uint8_t row) {
286 // select on mcp23018
287 if (mcp23018_status) { // if there was an error
290 // set active row low : 0
291 // set other rows hi-Z : 1
292 mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT);
293 if (mcp23018_status) goto out;
294 mcp23018_status = i2c_write(GPIOA, ERGODOX_EZ_I2C_TIMEOUT);
295 if (mcp23018_status) goto out;
296 mcp23018_status = i2c_write(0xFF & ~(1 << row), ERGODOX_EZ_I2C_TIMEOUT);
297 if (mcp23018_status) goto out;
303 // Output low(DDR:1, PORT:0) to select