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 * matrix state(1:on, 0:off)
62 * contains the raw values without debounce filtering of the last read cycle.
64 static matrix_row_t raw_matrix[MATRIX_ROWS];
66 // Debouncing: store for each key the number of scans until it's eligible to
67 // change. When scanning the matrix, ignore any changes in keys that have
68 // already changed in the last DEBOUNCE scans.
69 static uint8_t debounce_matrix[MATRIX_ROWS * MATRIX_COLS];
71 static matrix_row_t read_cols(uint8_t row);
72 static void init_cols(void);
73 static void unselect_rows(void);
74 static void select_row(uint8_t row);
76 static uint8_t mcp23018_reset_loop;
77 // static uint16_t mcp23018_reset_loop;
79 #ifdef DEBUG_MATRIX_SCAN_RATE
80 uint32_t matrix_timer;
81 uint32_t matrix_scan_count;
85 __attribute__ ((weak))
86 void matrix_init_user(void) {}
88 __attribute__ ((weak))
89 void matrix_scan_user(void) {}
91 __attribute__ ((weak))
92 void matrix_init_kb(void) {
96 __attribute__ ((weak))
97 void matrix_scan_kb(void) {
102 uint8_t matrix_rows(void)
108 uint8_t matrix_cols(void)
113 void matrix_init(void)
115 // initialize row and col
117 mcp23018_status = init_mcp23018();
123 // initialize matrix state: all keys off
124 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
127 for (uint8_t j=0; j < MATRIX_COLS; ++j) {
128 debounce_matrix[i * MATRIX_COLS + j] = 0;
132 #ifdef DEBUG_MATRIX_SCAN_RATE
133 matrix_timer = timer_read32();
134 matrix_scan_count = 0;
137 matrix_init_quantum();
141 void matrix_power_up(void) {
142 mcp23018_status = init_mcp23018();
147 // initialize matrix state: all keys off
148 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
152 #ifdef DEBUG_MATRIX_SCAN_RATE
153 matrix_timer = timer_read32();
154 matrix_scan_count = 0;
158 // Returns a matrix_row_t whose bits are set if the corresponding key should be
159 // eligible to change in this scan.
160 matrix_row_t debounce_mask(matrix_row_t rawcols, uint8_t row) {
161 matrix_row_t result = 0;
162 matrix_row_t change = rawcols ^ raw_matrix[row];
163 raw_matrix[row] = rawcols;
164 for (uint8_t i = 0; i < MATRIX_COLS; ++i) {
165 if (debounce_matrix[row * MATRIX_COLS + i]) {
166 --debounce_matrix[row * MATRIX_COLS + i];
170 if (change & (1 << i)) {
171 debounce_matrix[row * MATRIX_COLS + i] = DEBOUNCE;
177 matrix_row_t debounce_read_cols(uint8_t row) {
178 // Read the row without debouncing filtering and store it for later usage.
179 matrix_row_t cols = read_cols(row);
180 // Get the Debounce mask.
181 matrix_row_t mask = debounce_mask(cols, row);
182 // debounce the row and return the result.
183 return (cols & mask) | (matrix[row] & ~mask);;
186 uint8_t matrix_scan(void)
188 if (mcp23018_status) { // if there was an error
189 if (++mcp23018_reset_loop == 0) {
190 // if (++mcp23018_reset_loop >= 1300) {
191 // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
192 // this will be approx bit more frequent than once per second
193 print("trying to reset mcp23018\n");
194 mcp23018_status = init_mcp23018();
195 if (mcp23018_status) {
196 print("left side not responding\n");
198 print("left side attached\n");
199 ergodox_blink_all_leds();
204 #ifdef DEBUG_MATRIX_SCAN_RATE
207 uint32_t timer_now = timer_read32();
208 if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
209 print("matrix scan frequency: ");
210 pdec(matrix_scan_count);
213 matrix_timer = timer_now;
214 matrix_scan_count = 0;
219 mcp23018_status = ergodox_left_leds_update();
221 for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) {
223 // and select on left hand
224 select_row(i + MATRIX_ROWS_PER_SIDE);
225 // we don't need a 30us delay anymore, because selecting a
226 // left-hand row requires more than 30us for i2c.
228 // grab cols from left hand
229 matrix[i] = debounce_read_cols(i);
230 // grab cols from right hand
231 matrix[i + MATRIX_ROWS_PER_SIDE] = debounce_read_cols(i + MATRIX_ROWS_PER_SIDE);
236 matrix_scan_quantum();
241 bool matrix_is_modified(void) // deprecated and evidently not called.
247 bool matrix_is_on(uint8_t row, uint8_t col)
249 return (matrix[row] & ((matrix_row_t)1<<col));
253 matrix_row_t matrix_get_row(uint8_t row)
258 void matrix_print(void)
260 print("\nr/c 0123456789ABCDEF\n");
261 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
262 phex(row); print(": ");
263 pbin_reverse16(matrix_get_row(row));
268 uint8_t matrix_key_count(void)
271 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
272 count += bitpop16(matrix[i]);
277 /* Column pin configuration
281 * pin: F0 F1 F4 F5 F6 F7
285 * pin: B5 B4 B3 B2 B1 B0
287 static void init_cols(void)
290 // not needed, already done as part of init_mcp23018()
293 // Input with pull-up(DDR:0, PORT:1)
294 DDRF &= ~(1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
295 PORTF |= (1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
298 static matrix_row_t read_cols(uint8_t row)
301 if (mcp23018_status) { // if there was an error
305 mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
306 mcp23018_status = i2c_write(GPIOB, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
307 mcp23018_status = i2c_start(I2C_ADDR_READ, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
308 mcp23018_status = i2c_read_nack(ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status < 0) goto out;
309 data = ~((uint8_t)mcp23018_status);
310 mcp23018_status = I2C_STATUS_SUCCESS;
312 i2c_stop(ERGODOX_EZ_I2C_TIMEOUT);
317 * bitmask is 0b11110011, but we want those all
318 * in the lower six bits.
319 * we'll return 1s for the top two, but that's harmless.
322 return ~((PINF & 0x03) | ((PINF & 0xF0) >> 2));
326 /* Row pin configuration
329 * row: 7 8 9 10 11 12 13
330 * pin: B0 B1 B2 B3 D2 D3 C6
334 * pin: A0 A1 A2 A3 A4 A5 A6
336 static void unselect_rows(void)
338 // no need to unselect on mcp23018, because the select step sets all
339 // the other row bits high, and it's not changing to a different
342 // unselect on teensy
343 // Hi-Z(DDR:0, PORT:0) to unselect
344 DDRB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
345 PORTB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
346 DDRD &= ~(1<<2 | 1<<3);
347 PORTD &= ~(1<<2 | 1<<3);
352 static void select_row(uint8_t row)
355 // select on mcp23018
356 if (mcp23018_status) { // if there was an error
359 // set active row low : 0
360 // set other rows hi-Z : 1
361 mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
362 mcp23018_status = i2c_write(GPIOA, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
363 mcp23018_status = i2c_write(0xFF & ~(1<<row), ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
365 i2c_stop(ERGODOX_EZ_I2C_TIMEOUT);
369 // Output low(DDR:1, PORT:0) to select