3 Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "action_layer.h"
28 #include QMK_KEYBOARD_H
29 #ifdef DEBUG_MATRIX_SCAN_RATE
48 #define BMASK (COL7 | COL8 | COL9 | COL6)
49 #define DMASK (COL10 | COL11)
52 /* matrix state(1:on, 0:off) */
53 static matrix_row_t matrix[MATRIX_ROWS];
55 * matrix state(1:on, 0:off)
56 * contains the raw values without debounce filtering of the last read cycle.
58 static matrix_row_t raw_matrix[MATRIX_ROWS];
60 // Debouncing: store for each key the number of scans until it's eligible to
61 // change. When scanning the matrix, ignore any changes in keys that have
62 // already changed in the last DEBOUNCE scans.
63 static uint8_t debounce_matrix[MATRIX_ROWS * MATRIX_COLS];
65 static matrix_row_t read_cols(uint8_t row);
66 static void init_cols(void);
67 static void unselect_rows(void);
68 static void select_row(uint8_t row);
70 static uint8_t mcp23018_reset_loop;
71 // static uint16_t mcp23018_reset_loop;
73 #ifdef DEBUG_MATRIX_SCAN_RATE
74 uint32_t matrix_timer;
75 uint32_t matrix_scan_count;
79 __attribute__ ((weak))
80 void matrix_init_user(void) {}
82 __attribute__ ((weak))
83 void matrix_scan_user(void) {}
85 __attribute__ ((weak))
86 void matrix_init_kb(void) {
90 __attribute__ ((weak))
91 void matrix_scan_kb(void) {
96 uint8_t matrix_rows(void)
102 uint8_t matrix_cols(void)
108 void matrix_init(void)
110 // initialize row and col
111 mcp23018_status = init_mcp23018();
115 // initialize matrix state: all keys off
116 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
119 for (uint8_t j=0; j < MATRIX_COLS; ++j) {
120 debounce_matrix[i * MATRIX_COLS + j] = 0;
124 #ifdef DEBUG_MATRIX_SCAN_RATE
125 matrix_timer = timer_read32();
126 matrix_scan_count = 0;
128 matrix_init_quantum();
131 void matrix_power_up(void) {
132 mcp23018_status = init_mcp23018();
137 // initialize matrix state: all keys off
138 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
142 #ifdef DEBUG_MATRIX_SCAN_RATE
143 matrix_timer = timer_read32();
144 matrix_scan_count = 0;
149 // Returns a matrix_row_t whose bits are set if the corresponding key should be
150 // eligible to change in this scan.
151 matrix_row_t debounce_mask(matrix_row_t rawcols, uint8_t row) {
152 matrix_row_t result = 0;
153 matrix_row_t change = rawcols ^ raw_matrix[row];
154 raw_matrix[row] = rawcols;
155 for (uint8_t i = 0; i < MATRIX_COLS; ++i) {
156 if (debounce_matrix[row * MATRIX_COLS + i]) {
157 --debounce_matrix[row * MATRIX_COLS + i];
161 if (change & (1 << i)) {
162 debounce_matrix[row * MATRIX_COLS + i] = DEBOUNCE;
168 matrix_row_t debounce_read_cols(uint8_t row) {
169 // Read the row without debouncing filtering and store it for later usage.
170 matrix_row_t cols = read_cols(row);
171 // Get the Debounce mask.
172 matrix_row_t mask = debounce_mask(cols, row);
173 // debounce the row and return the result.
174 return (cols & mask) | (matrix[row] & ~mask);;
177 uint8_t matrix_scan(void)
180 if (mcp23018_status) { // if there was an error
181 if (++mcp23018_reset_loop == 0) {
182 // if (++mcp23018_reset_loop >= 1300) {
183 // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
184 // this will be approx bit more frequent than once per second
185 print("trying to reset mcp23018\n");
186 mcp23018_status = init_mcp23018();
187 if (mcp23018_status) {
188 print("left side not responding\n");
190 print("left side attached\n");
195 #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;
207 for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) {
209 // and select on left hand
210 select_row(i + MATRIX_ROWS_PER_SIDE);
211 // we don't need a 30us delay anymore, because selecting a
212 // left-hand row requires more than 30us for i2c.
214 // grab cols from left hand
215 matrix[i] = debounce_read_cols(i);
216 // grab cols from right hand
217 matrix[i + MATRIX_ROWS_PER_SIDE] = debounce_read_cols(i + MATRIX_ROWS_PER_SIDE);
222 matrix_scan_quantum();
225 for (uint8_t c = 0; c < MATRIX_COLS; c++)
226 for (uint8_t r = 0; r < MATRIX_ROWS; r++)
227 if (matrix_is_on(r, c)) xprintf("r:%d c:%d \n", r, c);
233 bool matrix_is_modified(void) // deprecated and evidently not called.
239 bool matrix_is_on(uint8_t row, uint8_t col)
241 return (matrix[row] & ((matrix_row_t)1<<col));
245 matrix_row_t matrix_get_row(uint8_t row)
250 void matrix_print(void)
252 print("\nr/c 0123456789ABCDEF\n");
253 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
254 phex(row); print(": ");
255 pbin_reverse16(matrix_get_row(row));
260 uint8_t matrix_key_count(void)
263 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
264 count += bitpop16(matrix[i]);
269 // Remember this means ROWS
270 static void init_cols(void)
273 // not needed, already done as part of init_mcp23018()
275 // Input with pull-up(DDR:0, PORT:1)
280 static matrix_row_t read_cols(uint8_t row)
283 if (mcp23018_status) { // if there was an error
287 mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
288 mcp23018_status = i2c_write(GPIOB, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
289 mcp23018_status = i2c_start(I2C_ADDR_READ, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
290 mcp23018_status = i2c_read_nack(ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status < 0) goto out;
291 data = (~((uint8_t)mcp23018_status) >> 2) & 0x01 ;
292 mcp23018_status = I2C_STATUS_SUCCESS;
297 if (data != 0x00) xprintf("I2C: %d\n", data);
302 // Read using bitmask
303 return ~((((PINF & ROW1) >> 5)) & 0x1);
307 // Row pin configuration
308 static void unselect_rows(void)
310 // no need to unselect on mcp23018, because the select step sets all
311 // the other row bits high, and it's not changing to a different
313 // Hi-Z(DDR:0, PORT:0) to unselect
320 static void select_row(uint8_t row)
323 // select on mcp23018
324 if (mcp23018_status) { // do nothing on error
325 // Read using bitmask
326 } else { // set active row low : 0 // set other rows hi-Z : 1
327 mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
328 mcp23018_status = i2c_write(GPIOA, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
329 mcp23018_status = i2c_write(~(1<<row), ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
334 // Output low(DDR:1, PORT:0) to select