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 "keymap_steno.h"
33 #include QMK_KEYBOARD_H
34 #ifdef DEBUG_MATRIX_SCAN_RATE
71 #define BMASK (COL7 | COL8 | COL9 | COL10)
73 #define DMASK (COL11 | COL12)
74 #define FMASK (ROW1 | ROW2 | ROW3 | ROW4)
75 #define RROWMASK (RROW1 | RROW2 | RROW3 | RROW4)
76 #define MCPMASK (COL0 | COL1 | COL2 | COL3 | COL4 | COL5 | COL6)
78 /* matrix state(1:on, 0:off) */
79 static matrix_row_t matrix[MATRIX_ROWS];
81 * matrix state(1:on, 0:off)
82 * contains the raw values without debounce filtering of the last read cycle.
84 static matrix_row_t raw_matrix[MATRIX_ROWS];
86 // Debouncing: store for each key the number of scans until it's eligible to
87 // change. When scanning the matrix, ignore any changes in keys that have
88 // already changed in the last DEBOUNCE scans.
89 static uint8_t debounce_matrix[MATRIX_ROWS * MATRIX_COLS];
91 static matrix_row_t read_cols(uint8_t row);
92 static void init_cols(void);
93 static void unselect_rows(void);
94 static void select_row(uint8_t row);
96 static uint8_t mcp23018_reset_loop;
97 // static uint16_t mcp23018_reset_loop;
99 #ifdef DEBUG_MATRIX_SCAN_RATE
100 uint32_t matrix_timer;
101 uint32_t matrix_scan_count;
105 __attribute__ ((weak))
106 void matrix_init_user(void) {}
108 __attribute__ ((weak))
109 void matrix_scan_user(void) {}
111 __attribute__ ((weak))
112 void matrix_init_kb(void) {
116 __attribute__ ((weak))
117 void matrix_scan_kb(void) {
122 uint8_t matrix_rows(void)
128 uint8_t matrix_cols(void)
134 void matrix_init(void)
136 // initialize row and col
137 mcp23018_status = init_mcp23018();
141 // initialize matrix state: all keys off
142 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
145 for (uint8_t j=0; j < MATRIX_COLS; ++j) {
146 debounce_matrix[i * MATRIX_COLS + j] = 0;
150 #ifdef DEBUG_MATRIX_SCAN_RATE
151 matrix_timer = timer_read32();
152 matrix_scan_count = 0;
154 matrix_init_quantum();
157 void matrix_power_up(void) {
158 mcp23018_status = init_mcp23018();
163 // initialize matrix state: all keys off
164 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
168 #ifdef DEBUG_MATRIX_SCAN_RATE
169 matrix_timer = timer_read32();
170 matrix_scan_count = 0;
175 // Returns a matrix_row_t whose bits are set if the corresponding key should be
176 // eligible to change in this scan.
177 matrix_row_t debounce_mask(matrix_row_t rawcols, uint8_t row) {
178 matrix_row_t result = 0;
179 matrix_row_t change = rawcols ^ raw_matrix[row];
180 raw_matrix[row] = rawcols;
181 for (uint8_t i = 0; i < MATRIX_COLS; ++i) {
182 if (debounce_matrix[row * MATRIX_COLS + i]) {
183 --debounce_matrix[row * MATRIX_COLS + i];
187 if (change & (1 << i)) {
188 debounce_matrix[row * MATRIX_COLS + i] = DEBOUNCE;
194 matrix_row_t debounce_read_cols(uint8_t row) {
195 // Read the row without debouncing filtering and store it for later usage.
196 matrix_row_t cols = read_cols(row);
197 // Get the Debounce mask.
198 matrix_row_t mask = debounce_mask(cols, row);
199 // debounce the row and return the result.
200 return (cols & mask) | (matrix[row] & ~mask);;
203 uint8_t matrix_scan(void)
206 if (mcp23018_status) { // if there was an error
207 if (++mcp23018_reset_loop == 0) {
208 // if (++mcp23018_reset_loop >= 1300) {
209 // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
210 // this will be approx bit more frequent than once per second
211 print("trying to reset mcp23018\n");
212 mcp23018_status = init_mcp23018();
213 if (mcp23018_status) {
214 print("left side not responding\n");
216 print("left side attached\n");
221 #ifdef DEBUG_MATRIX_SCAN_RATE
223 uint32_t timer_now = timer_read32();
224 if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
225 print("matrix scan frequency: ");
226 pdec(matrix_scan_count);
229 matrix_timer = timer_now;
230 matrix_scan_count = 0;
233 for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) {
235 // and select on left hand
236 select_row(i + MATRIX_ROWS_PER_SIDE);
237 // we don't need a 30us delay anymore, because selecting a
238 // left-hand row requires more than 30us for i2c.
240 // grab cols from left hand
241 matrix[i] = debounce_read_cols(i);
242 // grab cols from right hand
243 matrix[i + MATRIX_ROWS_PER_SIDE] = debounce_read_cols(i + MATRIX_ROWS_PER_SIDE);
248 matrix_scan_quantum();
251 for (uint8_t c = 0; c < MATRIX_COLS; c++)
252 for (uint8_t r = 0; r < MATRIX_ROWS; r++)
253 if (matrix_is_on(r, c)) xprintf("r:%d c:%d \n", r, c);
259 bool matrix_is_modified(void) // deprecated and evidently not called.
265 bool matrix_is_on(uint8_t row, uint8_t col)
267 return (matrix[row] & ((matrix_row_t)1<<col));
271 matrix_row_t matrix_get_row(uint8_t row)
276 void matrix_print(void)
278 print("\nr/c 0123456789ABCDEF\n");
279 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
280 phex(row); print(": ");
281 pbin_reverse16(matrix_get_row(row));
286 uint8_t matrix_key_count(void)
289 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
290 count += bitpop16(matrix[i]);
295 // Remember this means ROWS
296 static void init_cols(void)
299 // not needed, already done as part of init_mcp23018()
301 // Input with pull-up(DDR:0, PORT:1)
306 static matrix_row_t read_cols(uint8_t row)
309 if (mcp23018_status) { // if there was an error
313 mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
314 mcp23018_status = i2c_write(GPIOB, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
315 mcp23018_status = i2c_start(I2C_ADDR_READ, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
316 mcp23018_status = i2c_read_nack(ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status < 0) goto out;
317 data = ~((uint8_t)mcp23018_status);
318 mcp23018_status = I2C_STATUS_SUCCESS;
323 if (data != 0x00) xprintf("I2C: %d\n", data);
329 * bitmask is 0b0111001, but we want the lower four
330 * we'll return 1s for the top two, but that's harmless.
332 // So I need to confuckulate all this
333 //return ~(((PIND & DMASK) >> 1 | ((PINC & CMASK) >> 6) | (PIN)));
334 //return ~((PINF & 0x03) | ((PINF & 0xF0) >> 2));
336 (((PINF & ROW4) >> 1)
337 | ((PINF & (ROW1 | ROW2 | ROW3)) >> 3))
342 // Row pin configuration
343 static void unselect_rows(void)
345 // no need to unselect on mcp23018, because the select step sets all
346 // the other row bits high, and it's not changing to a different
348 // Hi-Z(DDR:0, PORT:0) to unselect
357 static void select_row(uint8_t row)
360 // select on mcp23018
361 if (mcp23018_status) { // do nothing on error
362 } else { // set active row low : 0 // set other rows hi-Z : 1
363 mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
364 mcp23018_status = i2c_write(GPIOA, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
365 mcp23018_status = i2c_write(0xFF & ~(1<<row), ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
370 // Output low(DDR:1, PORT:0) to select