2 Note for ErgoDox EZ customizers: Here be dragons!
3 This is not a file you want to be messing with.
4 All of the interesting stuff for you is under keymaps/ :)
7 Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
9 This program is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include "action_layer.h"
32 #include QMK_KEYBOARD_H
33 #ifdef DEBUG_MATRIX_SCAN_RATE
52 #define BMASK (COL7 | COL8 | COL9 | COL6)
53 #define DMASK (COL10 | COL11)
56 /* matrix state(1:on, 0:off) */
57 static matrix_row_t matrix[MATRIX_ROWS];
59 * matrix state(1:on, 0:off)
60 * contains the raw values without debounce filtering of the last read cycle.
62 static matrix_row_t raw_matrix[MATRIX_ROWS];
64 // Debouncing: store for each key the number of scans until it's eligible to
65 // change. When scanning the matrix, ignore any changes in keys that have
66 // already changed in the last DEBOUNCE scans.
67 static uint8_t debounce_matrix[MATRIX_ROWS * MATRIX_COLS];
69 static matrix_row_t read_cols(uint8_t row);
70 static void init_cols(void);
71 static void unselect_rows(void);
72 static void select_row(uint8_t row);
74 static uint8_t mcp23018_reset_loop;
75 // static uint16_t mcp23018_reset_loop;
77 #ifdef DEBUG_MATRIX_SCAN_RATE
78 uint32_t matrix_timer;
79 uint32_t matrix_scan_count;
83 __attribute__ ((weak))
84 void matrix_init_user(void) {}
86 __attribute__ ((weak))
87 void matrix_scan_user(void) {}
89 __attribute__ ((weak))
90 void matrix_init_kb(void) {
94 __attribute__ ((weak))
95 void matrix_scan_kb(void) {
100 uint8_t matrix_rows(void)
106 uint8_t matrix_cols(void)
112 void matrix_init(void)
114 // initialize row and col
115 mcp23018_status = init_mcp23018();
119 // initialize matrix state: all keys off
120 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
123 for (uint8_t j=0; j < MATRIX_COLS; ++j) {
124 debounce_matrix[i * MATRIX_COLS + j] = 0;
128 #ifdef DEBUG_MATRIX_SCAN_RATE
129 matrix_timer = timer_read32();
130 matrix_scan_count = 0;
132 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;
153 // Returns a matrix_row_t whose bits are set if the corresponding key should be
154 // eligible to change in this scan.
155 matrix_row_t debounce_mask(matrix_row_t rawcols, uint8_t row) {
156 matrix_row_t result = 0;
157 matrix_row_t change = rawcols ^ raw_matrix[row];
158 raw_matrix[row] = rawcols;
159 for (uint8_t i = 0; i < MATRIX_COLS; ++i) {
160 if (debounce_matrix[row * MATRIX_COLS + i]) {
161 --debounce_matrix[row * MATRIX_COLS + i];
165 if (change & (1 << i)) {
166 debounce_matrix[row * MATRIX_COLS + i] = DEBOUNCE;
172 matrix_row_t debounce_read_cols(uint8_t row) {
173 // Read the row without debouncing filtering and store it for later usage.
174 matrix_row_t cols = read_cols(row);
175 // Get the Debounce mask.
176 matrix_row_t mask = debounce_mask(cols, row);
177 // debounce the row and return the result.
178 return (cols & mask) | (matrix[row] & ~mask);;
181 uint8_t matrix_scan(void)
184 if (mcp23018_status) { // if there was an error
185 if (++mcp23018_reset_loop == 0) {
186 // if (++mcp23018_reset_loop >= 1300) {
187 // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
188 // this will be approx bit more frequent than once per second
189 print("trying to reset mcp23018\n");
190 mcp23018_status = init_mcp23018();
191 if (mcp23018_status) {
192 print("left side not responding\n");
194 print("left side attached\n");
199 #ifdef DEBUG_MATRIX_SCAN_RATE
201 uint32_t timer_now = timer_read32();
202 if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
203 print("matrix scan frequency: ");
204 pdec(matrix_scan_count);
207 matrix_timer = timer_now;
208 matrix_scan_count = 0;
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.
218 // grab cols from left hand
219 matrix[i] = debounce_read_cols(i);
220 // grab cols from right hand
221 matrix[i + MATRIX_ROWS_PER_SIDE] = debounce_read_cols(i + MATRIX_ROWS_PER_SIDE);
226 matrix_scan_quantum();
229 for (uint8_t c = 0; c < MATRIX_COLS; c++)
230 for (uint8_t r = 0; r < MATRIX_ROWS; r++)
231 if (matrix_is_on(r, c)) xprintf("r:%d c:%d \n", r, c);
237 bool matrix_is_modified(void) // deprecated and evidently not called.
243 bool matrix_is_on(uint8_t row, uint8_t col)
245 return (matrix[row] & ((matrix_row_t)1<<col));
249 matrix_row_t matrix_get_row(uint8_t row)
254 void matrix_print(void)
256 print("\nr/c 0123456789ABCDEF\n");
257 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
258 phex(row); print(": ");
259 pbin_reverse16(matrix_get_row(row));
264 uint8_t matrix_key_count(void)
267 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
268 count += bitpop16(matrix[i]);
273 // Remember this means ROWS
274 static void init_cols(void)
277 // not needed, already done as part of init_mcp23018()
279 // Input with pull-up(DDR:0, PORT:1)
284 static matrix_row_t read_cols(uint8_t row)
287 if (mcp23018_status) { // if there was an error
291 mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
292 mcp23018_status = i2c_write(GPIOB, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
293 mcp23018_status = i2c_start(I2C_ADDR_READ, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
294 mcp23018_status = i2c_read_nack(ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status < 0) goto out;
295 data = (~((uint8_t)mcp23018_status) >> 2) & 0x01 ;
296 mcp23018_status = I2C_STATUS_SUCCESS;
301 if (data != 0x00) xprintf("I2C: %d\n", data);
306 // Read using bitmask
307 return ~((((PINF & ROW1) >> 5)) & 0x1);
311 // Row pin configuration
312 static void unselect_rows(void)
314 // no need to unselect on mcp23018, because the select step sets all
315 // the other row bits high, and it's not changing to a different
317 // Hi-Z(DDR:0, PORT:0) to unselect
324 static void select_row(uint8_t row)
327 // select on mcp23018
328 if (mcp23018_status) { // do nothing on error
329 // Read using bitmask
330 } else { // set active row low : 0 // set other rows hi-Z : 1
331 mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
332 mcp23018_status = i2c_write(GPIOA, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
333 mcp23018_status = i2c_write(~(1<<row), ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
338 // Output low(DDR:1, PORT:0) to select