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 Note to self, the "column" and "row" in here actually refers to the opposite on the keyboard
9 see definition of KEYMAP in v1.h, the grid is transposed so that a "row" in here is actually a "column" on the physical keyboard
12 Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
14 This program is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 2 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
35 #include "action_layer.h"
40 #include "frenchdev.h"
41 #include "i2cmaster.h"
42 #ifdef DEBUG_MATRIX_SCAN_RATE
47 * This constant define not debouncing time in msecs, but amount of matrix
48 * scan loops which should be made to get stable debounced results.
50 * On Ergodox matrix scan rate is relatively low, because of slow I2C.
51 * Now it's only 317 scans/second, or about 3.15 msec/scan.
52 * According to Cherry specs, debouncing time is 5 msec.
54 * And so, there is no sense to have DEBOUNCE higher than 2.
60 static uint8_t debouncing = DEBOUNCE;
62 /* matrix state(1:on, 0:off) */
63 static matrix_row_t matrix[MATRIX_ROWS];
64 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
66 static matrix_row_t read_cols(uint8_t row);
67 static void init_cols(void);
68 static void unselect_rows(void);
69 static void select_row(uint8_t row);
71 static uint8_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)
107 void matrix_init(void)
109 // initialize row and col
112 debug_keyboard = true;
115 mcp23018_status = init_mcp23018();
121 // initialize matrix state: all keys off
122 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
124 matrix_debouncing[i] = 0;
127 #ifdef DEBUG_MATRIX_SCAN_RATE
128 matrix_timer = timer_read32();
129 matrix_scan_count = 0;
132 matrix_init_quantum();
136 void matrix_power_up(void) {
137 mcp23018_status = init_mcp23018();
142 // initialize matrix state: all keys off
143 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
145 matrix_debouncing[i] = 0;
148 #ifdef DEBUG_MATRIX_SCAN_RATE
149 matrix_timer = timer_read32();
150 matrix_scan_count = 0;
155 uint8_t matrix_scan(void)
157 if (mcp23018_status) { // if there was an error
158 if (++mcp23018_reset_loop == 0) {
159 // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
160 // this will be approx bit more frequent than once per second
161 print("trying to reset mcp23018\n");
162 mcp23018_status = init_mcp23018();
163 if (mcp23018_status) {
164 print("left side not responding\n");
166 print("left side attached\n");
167 ergodox_blink_all_leds();
172 #ifdef DEBUG_MATRIX_SCAN_RATE
175 uint32_t timer_now = timer_read32();
176 if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
177 print("matrix scan frequency: ");
178 pdec(matrix_scan_count);
181 matrix_timer = timer_now;
182 matrix_scan_count = 0;
186 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
188 wait_us(30); // without this wait read unstable value.
189 matrix_row_t cols = read_cols(i);
190 if (matrix_debouncing[i] != cols) {
191 matrix_debouncing[i] = cols;
193 debug("bounce!: "); debug_hex(debouncing); debug("\n");
195 debouncing = DEBOUNCE;
203 // this should be wait_ms(1) but has been left as-is at EZ's request
205 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
206 matrix[i] = matrix_debouncing[i];
211 matrix_scan_quantum();
216 bool matrix_is_modified(void)
218 if (debouncing) return false;
223 bool matrix_is_on(uint8_t row, uint8_t col)
225 return (matrix[row] & ((matrix_row_t)1<<col));
229 matrix_row_t matrix_get_row(uint8_t row)
234 void matrix_print(void)
236 print("\nr/c 0123456789ABCDEF\n");
237 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
238 phex(row); print(": ");
239 pbin_reverse16(matrix_get_row(row));
244 uint8_t matrix_key_count(void)
247 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
248 count += bitpop16(matrix[i]);
253 /* Column pin configuration
257 * pin: F0 F1 F4 F5 F6 F7
261 * pin: B5 B4 B3 B2 B1 B0
263 static void init_cols(void)
266 // not needed, already done as part of init_mcp23018()
269 // Input with pull-up(DDR:0, PORT:1)
270 DDRF &= ~(1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
271 PORTF |= (1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
274 static matrix_row_t read_cols(uint8_t row)
277 if (mcp23018_status) { // if there was an error
281 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
282 mcp23018_status = i2c_write(GPIOB); if (mcp23018_status) goto out;
283 mcp23018_status = i2c_start(I2C_ADDR_READ); if (mcp23018_status) goto out;
284 data = i2c_readNak();
293 (PINF&(1<<0) ? 0 : (1<<0)) |
294 (PINF&(1<<1) ? 0 : (1<<1)) |
295 (PINF&(1<<4) ? 0 : (1<<2)) |
296 (PINF&(1<<5) ? 0 : (1<<3)) |
297 (PINF&(1<<6) ? 0 : (1<<4)) |
298 (PINF&(1<<7) ? 0 : (1<<5)) ;
302 /* Row pin configuration
305 * row: 7 8 9 10 11 12 13
306 * pin: B0 B1 B2 B3 D2 D3 C6
310 * pin: A0 A1 A2 A3 A4 A5 A6
312 static void unselect_rows(void)
314 // unselect on mcp23018
315 if (mcp23018_status) { // if there was an error
318 // set all rows hi-Z : 1
319 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
320 mcp23018_status = i2c_write(GPIOA); if (mcp23018_status) goto out;
321 mcp23018_status = i2c_write( 0xFF
323 ); if (mcp23018_status) goto out;
328 // unselect on teensy
329 // Hi-Z(DDR:0, PORT:0) to unselect
330 DDRB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
331 PORTB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
332 DDRD &= ~(1<<2 | 1<<3);
333 PORTD &= ~(1<<2 | 1<<3);
334 DDRC &= ~(1<<6 | 1<<7);
335 PORTC &= ~(1<<6 | 1<<7);
338 static void select_row(uint8_t row)
341 // select on mcp23018
342 if (mcp23018_status) { // if there was an error
345 // set active row low : 0
346 // set other rows hi-Z : 1
347 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
348 mcp23018_status = i2c_write(GPIOA); if (mcp23018_status) goto out;
349 mcp23018_status = i2c_write( 0xFF & ~(1<<row)
351 ); if (mcp23018_status) goto out;
357 // Output low(DDR:1, PORT:0) to select