4 Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "action_layer.h"
33 #include QMK_KEYBOARD_H
34 #ifdef DEBUG_MATRIX_SCAN_RATE
39 * This constant define not debouncing time in msecs, assuming eager_pr.
41 * On Ergodox matrix scan rate is relatively low, because of slow I2C.
42 * Now it's only 317 scans/second, or about 3.15 msec/scan.
43 * According to Cherry specs, debouncing time is 5 msec.
45 * However, some switches seem to have higher debouncing requirements, or
46 * something else might be wrong. (Also, the scan speed has improved since
47 * that comment was written.)
54 /* matrix state(1:on, 0:off) */
55 static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
56 static matrix_row_t matrix[MATRIX_ROWS]; // debounced values
58 static matrix_row_t read_cols(uint8_t row);
59 static void init_cols(void);
60 static void unselect_rows(void);
61 static void select_row(uint8_t row);
63 static uint8_t mcp23018_reset_loop;
64 // static uint16_t mcp23018_reset_loop;
66 #ifdef DEBUG_MATRIX_SCAN_RATE
67 uint32_t matrix_timer;
68 uint32_t matrix_scan_count;
71 __attribute__((weak)) void matrix_init_user(void) {}
73 __attribute__((weak)) void matrix_scan_user(void) {}
75 __attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); }
77 __attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); }
79 inline uint8_t matrix_rows(void) { return MATRIX_ROWS; }
81 inline uint8_t matrix_cols(void) { return MATRIX_COLS; }
83 void matrix_init(void) {
84 // initialize row and col
86 mcp23018_status = init_mcp23018();
91 // initialize matrix state: all keys off
92 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
97 #ifdef DEBUG_MATRIX_SCAN_RATE
98 matrix_timer = timer_read32();
99 matrix_scan_count = 0;
101 debounce_init(MATRIX_ROWS);
102 matrix_init_quantum();
105 void matrix_power_up(void) {
106 mcp23018_status = init_mcp23018();
111 // initialize matrix state: all keys off
112 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
116 #ifdef DEBUG_MATRIX_SCAN_RATE
117 matrix_timer = timer_read32();
118 matrix_scan_count = 0;
122 // Reads and stores a row, returning
123 // whether a change occurred.
124 static inline bool store_raw_matrix_row(uint8_t index) {
125 matrix_row_t temp = read_cols(index);
126 if (raw_matrix[index] != temp) {
127 raw_matrix[index] = temp;
133 uint8_t matrix_scan(void) {
134 if (mcp23018_status) { // if there was an error
135 if (++mcp23018_reset_loop == 0) {
136 // if (++mcp23018_reset_loop >= 1300) {
137 // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
138 // this will be approx bit more frequent than once per second
139 print("trying to reset mcp23018\n");
140 mcp23018_status = init_mcp23018();
141 if (mcp23018_status) {
142 print("left side not responding\n");
144 print("left side attached\n");
145 ergodox_blink_all_leds();
150 #ifdef DEBUG_MATRIX_SCAN_RATE
153 uint32_t timer_now = timer_read32();
154 if (TIMER_DIFF_32(timer_now, matrix_timer) > 1000) {
155 print("matrix scan frequency: ");
156 pdec(matrix_scan_count);
159 matrix_timer = timer_now;
160 matrix_scan_count = 0;
165 mcp23018_status = ergodox_left_leds_update();
167 bool changed = false;
168 for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) {
169 // select rows from left and right hands
170 uint8_t left_index = i;
171 uint8_t right_index = i + MATRIX_ROWS_PER_SIDE;
172 select_row(left_index);
173 select_row(right_index);
175 // we don't need a 30us delay anymore, because selecting a
176 // left-hand row requires more than 30us for i2c.
178 changed |= store_raw_matrix_row(left_index);
179 changed |= store_raw_matrix_row(right_index);
184 debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
185 matrix_scan_quantum();
190 bool matrix_is_modified(void) // deprecated and evidently not called.
195 inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); }
197 inline matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; }
199 void matrix_print(void) {
200 print("\nr/c 0123456789ABCDEF\n");
201 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
204 pbin_reverse16(matrix_get_row(row));
209 uint8_t matrix_key_count(void) {
211 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
212 count += bitpop16(matrix[i]);
217 /* Column pin configuration
221 * pin: F0 F1 F4 F5 F6 F7
225 * pin: B5 B4 B3 B2 B1 B0
227 static void init_cols(void) {
229 // not needed, already done as part of init_mcp23018()
232 // Input with pull-up(DDR:0, PORT:1)
233 DDRF &= ~(1 << 7 | 1 << 6 | 1 << 5 | 1 << 4 | 1 << 1 | 1 << 0);
234 PORTF |= (1 << 7 | 1 << 6 | 1 << 5 | 1 << 4 | 1 << 1 | 1 << 0);
237 static matrix_row_t read_cols(uint8_t row) {
239 if (mcp23018_status) { // if there was an error
243 mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT);
244 if (mcp23018_status) goto out;
245 mcp23018_status = i2c_write(GPIOB, ERGODOX_EZ_I2C_TIMEOUT);
246 if (mcp23018_status) goto out;
247 mcp23018_status = i2c_start(I2C_ADDR_READ, ERGODOX_EZ_I2C_TIMEOUT);
248 if (mcp23018_status) goto out;
249 mcp23018_status = i2c_read_nack(ERGODOX_EZ_I2C_TIMEOUT);
250 if (mcp23018_status < 0) goto out;
251 data = ~((uint8_t)mcp23018_status);
252 mcp23018_status = I2C_STATUS_SUCCESS;
259 * bitmask is 0b11110011, but we want those all
260 * in the lower six bits.
261 * we'll return 1s for the top two, but that's harmless.
264 return ~((PINF & 0x03) | ((PINF & 0xF0) >> 2));
268 /* Row pin configuration
271 * row: 7 8 9 10 11 12 13
272 * pin: B0 B1 B2 B3 D2 D3 C6
276 * pin: A0 A1 A2 A3 A4 A5 A6
278 static void unselect_rows(void) {
279 // no need to unselect on mcp23018, because the select step sets all
280 // the other row bits high, and it's not changing to a different
283 // unselect on teensy
284 // Hi-Z(DDR:0, PORT:0) to unselect
285 DDRB &= ~(1 << 0 | 1 << 1 | 1 << 2 | 1 << 3);
286 PORTB &= ~(1 << 0 | 1 << 1 | 1 << 2 | 1 << 3);
287 DDRD &= ~(1 << 2 | 1 << 3);
288 PORTD &= ~(1 << 2 | 1 << 3);
293 static void select_row(uint8_t row) {
295 // select on mcp23018
296 if (mcp23018_status) { // if there was an error
299 // set active row low : 0
300 // set other rows hi-Z : 1
301 mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT);
302 if (mcp23018_status) goto out;
303 mcp23018_status = i2c_write(GPIOA, ERGODOX_EZ_I2C_TIMEOUT);
304 if (mcp23018_status) goto out;
305 mcp23018_status = i2c_write(0xFF & ~(1 << row), ERGODOX_EZ_I2C_TIMEOUT);
306 if (mcp23018_status) goto out;
312 // Output low(DDR:1, PORT:0) to select