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"
36 #include QMK_KEYBOARD_H
37 #ifdef DEBUG_MATRIX_SCAN_RATE
42 * This constant define not debouncing time in msecs, but amount of matrix
43 * scan loops which should be made to get stable debounced results.
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 matrix[MATRIX_ROWS];
61 // Debouncing: store for each key the number of scans until it's eligible to
62 // change. When scanning the matrix, ignore any changes in keys that have
63 // already changed in the last DEBOUNCE scans.
64 static uint8_t debounce_matrix[MATRIX_ROWS * MATRIX_COLS];
66 static matrix_row_t read_cols(uint8_t row);
67 static void init_cols(void);
68 static void unselect_rows(void);
69 static void select_row(uint8_t row);
71 static uint8_t mcp23018_reset_loop;
72 // static uint16_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
112 mcp23018_status = init_mcp23018();
118 // initialize matrix state: all keys off
119 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
121 for (uint8_t j=0; j < MATRIX_COLS; ++j) {
122 debounce_matrix[i * MATRIX_COLS + j] = 0;
126 #ifdef DEBUG_MATRIX_SCAN_RATE
127 matrix_timer = timer_read32();
128 matrix_scan_count = 0;
131 matrix_init_quantum();
135 void matrix_power_up(void) {
136 mcp23018_status = init_mcp23018();
141 // initialize matrix state: all keys off
142 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
146 #ifdef DEBUG_MATRIX_SCAN_RATE
147 matrix_timer = timer_read32();
148 matrix_scan_count = 0;
152 // Returns a matrix_row_t whose bits are set if the corresponding key should be
153 // eligible to change in this scan.
154 matrix_row_t debounce_mask(uint8_t row) {
155 matrix_row_t result = 0;
156 for (uint8_t j=0; j < MATRIX_COLS; ++j) {
157 if (debounce_matrix[row * MATRIX_COLS + j]) {
158 --debounce_matrix[row * MATRIX_COLS + j];
166 // Report changed keys in the given row. Resets the debounce countdowns
167 // corresponding to each set bit in 'change' to DEBOUNCE.
168 void debounce_report(matrix_row_t change, uint8_t row) {
169 for (uint8_t i = 0; i < MATRIX_COLS; ++i) {
170 if (change & (1 << i)) {
171 debounce_matrix[row * MATRIX_COLS + i] = DEBOUNCE;
176 uint8_t matrix_scan(void)
178 if (mcp23018_status) { // if there was an error
179 if (++mcp23018_reset_loop == 0) {
180 // if (++mcp23018_reset_loop >= 1300) {
181 // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
182 // this will be approx bit more frequent than once per second
183 print("trying to reset mcp23018\n");
184 mcp23018_status = init_mcp23018();
185 if (mcp23018_status) {
186 print("left side not responding\n");
188 print("left side attached\n");
189 ergodox_blink_all_leds();
194 #ifdef DEBUG_MATRIX_SCAN_RATE
197 uint32_t timer_now = timer_read32();
198 if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
199 print("matrix scan frequency: ");
200 pdec(matrix_scan_count);
203 matrix_timer = timer_now;
204 matrix_scan_count = 0;
209 mcp23018_status = ergodox_left_leds_update();
211 for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) {
213 // and select on left hand
214 select_row(i + MATRIX_ROWS_PER_SIDE);
215 // we don't need a 30us delay anymore, because selecting a
216 // left-hand row requires more than 30us for i2c.
217 matrix_row_t mask = debounce_mask(i);
218 matrix_row_t cols = (read_cols(i) & mask) | (matrix[i] & ~mask);
219 debounce_report(cols ^ matrix[i], i);
221 // grab cols from right hand
222 mask = debounce_mask(i + MATRIX_ROWS_PER_SIDE);
223 cols = (read_cols(i + MATRIX_ROWS_PER_SIDE) & mask) | (matrix[i + MATRIX_ROWS_PER_SIDE] & ~mask);
224 debounce_report(cols ^ matrix[i + MATRIX_ROWS_PER_SIDE], i + MATRIX_ROWS_PER_SIDE);
225 matrix[i + MATRIX_ROWS_PER_SIDE] = cols;
229 matrix_scan_quantum();
234 bool matrix_is_modified(void) // deprecated and evidently not called.
240 bool matrix_is_on(uint8_t row, uint8_t col)
242 return (matrix[row] & ((matrix_row_t)1<<col));
246 matrix_row_t matrix_get_row(uint8_t row)
251 void matrix_print(void)
253 print("\nr/c 0123456789ABCDEF\n");
254 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
255 phex(row); print(": ");
256 pbin_reverse16(matrix_get_row(row));
261 uint8_t matrix_key_count(void)
264 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
265 count += bitpop16(matrix[i]);
270 /* Column pin configuration
274 * pin: F0 F1 F4 F5 F6 F7
278 * pin: B5 B4 B3 B2 B1 B0
280 static void init_cols(void)
283 // not needed, already done as part of init_mcp23018()
286 // Input with pull-up(DDR:0, PORT:1)
287 DDRF &= ~(1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
288 PORTF |= (1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
291 static matrix_row_t read_cols(uint8_t row)
294 if (mcp23018_status) { // if there was an error
298 mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
299 mcp23018_status = i2c_write(GPIOB, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
300 mcp23018_status = i2c_start(I2C_ADDR_READ, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
301 mcp23018_status = i2c_read_nack(ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status < 0) goto out;
302 data = ~((uint8_t)mcp23018_status);
303 mcp23018_status = I2C_STATUS_SUCCESS;
305 i2c_stop(ERGODOX_EZ_I2C_TIMEOUT);
310 * bitmask is 0b11110011, but we want those all
311 * in the lower six bits.
312 * we'll return 1s for the top two, but that's harmless.
315 return ~((PINF & 0x03) | ((PINF & 0xF0) >> 2));
319 /* Row pin configuration
322 * row: 7 8 9 10 11 12 13
323 * pin: B0 B1 B2 B3 D2 D3 C6
327 * pin: A0 A1 A2 A3 A4 A5 A6
329 static void unselect_rows(void)
331 // no need to unselect on mcp23018, because the select step sets all
332 // the other row bits high, and it's not changing to a different
335 // unselect on teensy
336 // Hi-Z(DDR:0, PORT:0) to unselect
337 DDRB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
338 PORTB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
339 DDRD &= ~(1<<2 | 1<<3);
340 PORTD &= ~(1<<2 | 1<<3);
345 static void select_row(uint8_t row)
348 // select on mcp23018
349 if (mcp23018_status) { // if there was an error
352 // set active row low : 0
353 // set other rows hi-Z : 1
354 mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
355 mcp23018_status = i2c_write(GPIOA, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
356 mcp23018_status = i2c_write(0xFF & ~(1<<row), ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
358 i2c_stop(ERGODOX_EZ_I2C_TIMEOUT);
362 // Output low(DDR:1, PORT:0) to select