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"
37 #include "i2cmaster.h"
38 #ifdef DEBUG_MATRIX_SCAN_RATE
43 * This constant define not debouncing time in msecs, but amount of matrix
44 * scan loops which should be made to get stable debounced results.
46 * On Ergodox matrix scan rate is relatively low, because of slow I2C.
47 * Now it's only 317 scans/second, or about 3.15 msec/scan.
48 * According to Cherry specs, debouncing time is 5 msec.
50 * And so, there is no sense to have DEBOUNCE higher than 2.
56 static uint8_t debouncing = DEBOUNCE;
58 /* matrix state(1:on, 0:off) */
59 static matrix_row_t matrix[MATRIX_ROWS];
60 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
62 static matrix_row_t read_cols(uint8_t row);
63 static void init_cols(void);
64 static void unselect_rows(void);
65 static void select_row(uint8_t row);
67 static uint8_t mcp23018_reset_loop;
69 #ifdef DEBUG_MATRIX_SCAN_RATE
70 uint32_t matrix_timer;
71 uint32_t matrix_scan_count;
75 __attribute__ ((weak))
76 void matrix_init_user(void) {}
78 __attribute__ ((weak))
79 void matrix_scan_user(void) {}
81 __attribute__ ((weak))
82 void matrix_init_kb(void) {
86 __attribute__ ((weak))
87 void matrix_scan_kb(void) {
92 uint8_t matrix_rows(void)
98 uint8_t matrix_cols(void)
103 void matrix_init(void)
105 // initialize row and col
107 mcp23018_status = init_mcp23018();
113 // initialize matrix state: all keys off
114 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
116 matrix_debouncing[i] = 0;
119 #ifdef DEBUG_MATRIX_SCAN_RATE
120 matrix_timer = timer_read32();
121 matrix_scan_count = 0;
128 void matrix_power_up(void) {
129 mcp23018_status = init_mcp23018();
134 // initialize matrix state: all keys off
135 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
137 matrix_debouncing[i] = 0;
140 #ifdef DEBUG_MATRIX_SCAN_RATE
141 matrix_timer = timer_read32();
142 matrix_scan_count = 0;
147 uint8_t matrix_scan(void)
149 if (mcp23018_status) { // if there was an error
150 if (++mcp23018_reset_loop == 0) {
151 // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
152 // this will be approx bit more frequent than once per second
153 print("trying to reset mcp23018\n");
154 mcp23018_status = init_mcp23018();
155 if (mcp23018_status) {
156 print("left side not responding\n");
158 print("left side attached\n");
159 ergodox_blink_all_leds();
164 #ifdef DEBUG_MATRIX_SCAN_RATE
167 uint32_t timer_now = timer_read32();
168 if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
169 print("matrix scan frequency: ");
170 pdec(matrix_scan_count);
173 matrix_timer = timer_now;
174 matrix_scan_count = 0;
178 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
180 wait_us(30); // without this wait read unstable value.
181 matrix_row_t cols = read_cols(i);
182 if (matrix_debouncing[i] != cols) {
183 matrix_debouncing[i] = cols;
185 debug("bounce!: "); debug_hex(debouncing); debug("\n");
187 debouncing = DEBOUNCE;
195 // this should be wait_ms(1) but has been left as-is at EZ's request
197 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
198 matrix[i] = matrix_debouncing[i];
203 matrix_scan_quantum();
208 bool matrix_is_modified(void)
210 if (debouncing) return false;
215 bool matrix_is_on(uint8_t row, uint8_t col)
217 return (matrix[row] & ((matrix_row_t)1<<col));
221 matrix_row_t matrix_get_row(uint8_t row)
226 void matrix_print(void)
228 print("\nr/c 0123456789ABCDEF\n");
229 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
230 phex(row); print(": ");
231 pbin_reverse16(matrix_get_row(row));
236 uint8_t matrix_key_count(void)
239 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
240 count += bitpop16(matrix[i]);
245 /* Column pin configuration
249 * pin: F0 F1 F4 F5 F6 F7
253 * pin: B5 B4 B3 B2 B1 B0
255 static void init_cols(void)
258 // not needed, already done as part of init_mcp23018()
261 // Input with pull-up(DDR:0, PORT:1)
262 DDRF &= ~(1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
263 PORTF |= (1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
266 static matrix_row_t read_cols(uint8_t row)
269 if (mcp23018_status) { // if there was an error
273 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
274 mcp23018_status = i2c_write(GPIOB); if (mcp23018_status) goto out;
275 mcp23018_status = i2c_start(I2C_ADDR_READ); if (mcp23018_status) goto out;
276 data = i2c_readNak();
285 (PINF&(1<<0) ? 0 : (1<<0)) |
286 (PINF&(1<<1) ? 0 : (1<<1)) |
287 (PINF&(1<<4) ? 0 : (1<<2)) |
288 (PINF&(1<<5) ? 0 : (1<<3)) |
289 (PINF&(1<<6) ? 0 : (1<<4)) |
290 (PINF&(1<<7) ? 0 : (1<<5)) ;
294 /* Row pin configuration
297 * row: 7 8 9 10 11 12 13
298 * pin: B0 B1 B2 B3 D2 D3 C6
302 * pin: A0 A1 A2 A3 A4 A5 A6
304 static void unselect_rows(void)
306 // unselect on mcp23018
307 if (mcp23018_status) { // if there was an error
310 // set all rows hi-Z : 1
311 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
312 mcp23018_status = i2c_write(GPIOA); if (mcp23018_status) goto out;
313 mcp23018_status = i2c_write( 0xFF
315 ); if (mcp23018_status) goto out;
320 // unselect on teensy
321 // Hi-Z(DDR:0, PORT:0) to unselect
322 DDRB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
323 PORTB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
324 DDRD &= ~(1<<2 | 1<<3);
325 PORTD &= ~(1<<2 | 1<<3);
330 static void select_row(uint8_t row)
333 // select on mcp23018
334 if (mcp23018_status) { // if there was an error
337 // set active row low : 0
338 // set other rows hi-Z : 1
339 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
340 mcp23018_status = i2c_write(GPIOA); if (mcp23018_status) goto out;
341 mcp23018_status = i2c_write( 0xFF & ~(1<<row)
343 ); if (mcp23018_status) goto out;
349 // Output low(DDR:1, PORT:0) to select