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/>.
30 #include <util/delay.h>
31 #include "action_layer.h"
36 #include "ergodox_ez.h"
37 #include "i2cmaster.h"
38 #ifdef DEBUG_MATRIX_SCAN_RATE
45 static uint8_t debouncing = DEBOUNCE;
47 /* matrix state(1:on, 0:off) */
48 static matrix_row_t matrix[MATRIX_ROWS];
49 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
51 static matrix_row_t read_cols(uint8_t row);
52 static void init_cols(void);
53 static void unselect_rows(void);
54 static void select_row(uint8_t row);
56 static uint8_t mcp23018_reset_loop;
58 #ifdef DEBUG_MATRIX_SCAN_RATE
59 uint32_t matrix_timer;
60 uint32_t matrix_scan_count;
64 __attribute__ ((weak))
65 void matrix_init_kb(void) {
68 __attribute__ ((weak))
69 void matrix_scan_kb(void) {
73 uint8_t matrix_rows(void)
79 uint8_t matrix_cols(void)
84 void matrix_init(void)
86 // initialize row and col
88 mcp23018_status = init_mcp23018();
94 // initialize matrix state: all keys off
95 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
97 matrix_debouncing[i] = 0;
100 #ifdef DEBUG_MATRIX_SCAN_RATE
101 matrix_timer = timer_read32();
102 matrix_scan_count = 0;
109 void matrix_power_up(void) {
110 mcp23018_status = init_mcp23018();
115 // initialize matrix state: all keys off
116 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
118 matrix_debouncing[i] = 0;
121 #ifdef DEBUG_MATRIX_SCAN_RATE
122 matrix_timer = timer_read32();
123 matrix_scan_count = 0;
128 uint8_t matrix_scan(void)
130 if (mcp23018_status) { // if there was an error
131 if (++mcp23018_reset_loop == 0) {
132 // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
133 // this will be approx bit more frequent than once per second
134 print("trying to reset mcp23018\n");
135 mcp23018_status = init_mcp23018();
136 if (mcp23018_status) {
137 print("left side not responding\n");
139 print("left side attached\n");
140 ergodox_blink_all_leds();
145 #ifdef DEBUG_MATRIX_SCAN_RATE
148 uint32_t timer_now = timer_read32();
149 if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
150 print("matrix scan frequency: ");
151 pdec(matrix_scan_count);
154 matrix_timer = timer_now;
155 matrix_scan_count = 0;
159 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
161 matrix_row_t cols = read_cols(i);
162 if (matrix_debouncing[i] != cols) {
163 matrix_debouncing[i] = cols;
165 debug("bounce!: "); debug_hex(debouncing); debug("\n");
167 debouncing = DEBOUNCE;
176 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
177 matrix[i] = matrix_debouncing[i];
188 bool matrix_is_modified(void)
190 if (debouncing) return false;
195 bool matrix_is_on(uint8_t row, uint8_t col)
197 return (matrix[row] & ((matrix_row_t)1<<col));
201 matrix_row_t matrix_get_row(uint8_t row)
206 void matrix_print(void)
208 print("\nr/c 0123456789ABCDEF\n");
209 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
210 phex(row); print(": ");
211 pbin_reverse16(matrix_get_row(row));
216 uint8_t matrix_key_count(void)
219 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
220 count += bitpop16(matrix[i]);
225 /* Column pin configuration
229 * pin: F0 F1 F4 F5 F6 F7
233 * pin: B5 B4 B3 B2 B1 B0
235 static void init_cols(void)
238 // not needed, already done as part of init_mcp23018()
241 // Input with pull-up(DDR:0, PORT:1)
242 DDRF &= ~(1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
243 PORTF |= (1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
246 static matrix_row_t read_cols(uint8_t row)
249 if (mcp23018_status) { // if there was an error
253 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
254 mcp23018_status = i2c_write(GPIOB); if (mcp23018_status) goto out;
255 mcp23018_status = i2c_start(I2C_ADDR_READ); if (mcp23018_status) goto out;
256 data = i2c_readNak();
263 _delay_us(30); // without this wait read unstable value.
266 (PINF&(1<<0) ? 0 : (1<<0)) |
267 (PINF&(1<<1) ? 0 : (1<<1)) |
268 (PINF&(1<<4) ? 0 : (1<<2)) |
269 (PINF&(1<<5) ? 0 : (1<<3)) |
270 (PINF&(1<<6) ? 0 : (1<<4)) |
271 (PINF&(1<<7) ? 0 : (1<<5)) ;
275 /* Row pin configuration
278 * row: 7 8 9 10 11 12 13
279 * pin: B0 B1 B2 B3 D2 D3 C6
283 * pin: A0 A1 A2 A3 A4 A5 A6
285 static void unselect_rows(void)
287 // unselect on mcp23018
288 if (mcp23018_status) { // if there was an error
291 // set all rows hi-Z : 1
292 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
293 mcp23018_status = i2c_write(GPIOA); if (mcp23018_status) goto out;
294 mcp23018_status = i2c_write( 0xFF
296 ); if (mcp23018_status) goto out;
301 // unselect on teensy
302 // Hi-Z(DDR:0, PORT:0) to unselect
303 DDRB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
304 PORTB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
305 DDRD &= ~(1<<2 | 1<<3);
306 PORTD &= ~(1<<2 | 1<<3);
311 static void select_row(uint8_t row)
314 // select on mcp23018
315 if (mcp23018_status) { // if there was an error
318 // set active row low : 0
319 // set other rows hi-Z : 1
320 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
321 mcp23018_status = i2c_write(GPIOA); if (mcp23018_status) goto out;
322 mcp23018_status = i2c_write( 0xFF & ~(1<<row)
324 ); if (mcp23018_status) goto out;
330 // Output low(DDR:1, PORT:0) to select