diff options
Diffstat (limited to 'keyboard/ergodox/matrix.c')
| -rw-r--r-- | keyboard/ergodox/matrix.c | 405 |
1 files changed, 405 insertions, 0 deletions
diff --git a/keyboard/ergodox/matrix.c b/keyboard/ergodox/matrix.c new file mode 100644 index 000000000..cc10e2941 --- /dev/null +++ b/keyboard/ergodox/matrix.c | |||
| @@ -0,0 +1,405 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com> | ||
| 3 | |||
| 4 | This program is free software: you can redistribute it and/or modify | ||
| 5 | it under the terms of the GNU General Public License as published by | ||
| 6 | the Free Software Foundation, either version 2 of the License, or | ||
| 7 | (at your option) any later version. | ||
| 8 | |||
| 9 | This program is distributed in the hope that it will be useful, | ||
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | GNU General Public License for more details. | ||
| 13 | |||
| 14 | You should have received a copy of the GNU General Public License | ||
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | /* | ||
| 19 | * scan matrix | ||
| 20 | */ | ||
| 21 | #include <stdint.h> | ||
| 22 | #include <stdbool.h> | ||
| 23 | #include <avr/io.h> | ||
| 24 | #include <util/delay.h> | ||
| 25 | #include "action_layer.h" | ||
| 26 | #include "print.h" | ||
| 27 | #include "debug.h" | ||
| 28 | #include "util.h" | ||
| 29 | #include "matrix.h" | ||
| 30 | #include "ergodox.h" | ||
| 31 | #include "i2cmaster.h" | ||
| 32 | #ifdef DEBUG_MATRIX_SCAN_RATE | ||
| 33 | #include "timer.h" | ||
| 34 | #endif | ||
| 35 | |||
| 36 | #ifndef DEBOUNCE | ||
| 37 | # define DEBOUNCE 5 | ||
| 38 | #endif | ||
| 39 | static uint8_t debouncing = DEBOUNCE; | ||
| 40 | |||
| 41 | /* matrix state(1:on, 0:off) */ | ||
| 42 | static matrix_row_t matrix[MATRIX_ROWS]; | ||
| 43 | static matrix_row_t matrix_debouncing[MATRIX_ROWS]; | ||
| 44 | |||
| 45 | static matrix_row_t read_cols(uint8_t row); | ||
| 46 | static void init_cols(void); | ||
| 47 | static void unselect_rows(); | ||
| 48 | static void select_row(uint8_t row); | ||
| 49 | |||
| 50 | static uint8_t mcp23018_reset_loop; | ||
| 51 | |||
| 52 | #ifdef DEBUG_MATRIX_SCAN_RATE | ||
| 53 | uint32_t matrix_timer; | ||
| 54 | uint32_t matrix_scan_count; | ||
| 55 | #endif | ||
| 56 | |||
| 57 | |||
| 58 | __attribute__ ((weak)) | ||
| 59 | void * matrix_init_kb(void) { | ||
| 60 | }; | ||
| 61 | |||
| 62 | __attribute__ ((weak)) | ||
| 63 | void * matrix_scan_kb(void) { | ||
| 64 | }; | ||
| 65 | |||
| 66 | inline | ||
| 67 | uint8_t matrix_rows(void) | ||
| 68 | { | ||
| 69 | return MATRIX_ROWS; | ||
| 70 | } | ||
| 71 | |||
| 72 | inline | ||
| 73 | uint8_t matrix_cols(void) | ||
| 74 | { | ||
| 75 | return MATRIX_COLS; | ||
| 76 | } | ||
| 77 | |||
| 78 | void matrix_init(void) | ||
| 79 | { | ||
| 80 | // initialize row and col | ||
| 81 | |||
| 82 | mcp23018_status = init_mcp23018(); | ||
| 83 | |||
| 84 | |||
| 85 | unselect_rows(); | ||
| 86 | init_cols(); | ||
| 87 | |||
| 88 | // initialize matrix state: all keys off | ||
| 89 | for (uint8_t i=0; i < MATRIX_ROWS; i++) { | ||
| 90 | matrix[i] = 0; | ||
| 91 | matrix_debouncing[i] = 0; | ||
| 92 | } | ||
| 93 | |||
| 94 | #ifdef DEBUG_MATRIX_SCAN_RATE | ||
| 95 | matrix_timer = timer_read32(); | ||
| 96 | matrix_scan_count = 0; | ||
| 97 | #endif | ||
| 98 | |||
| 99 | if (matrix_init_kb) { | ||
| 100 | (*matrix_init_kb)(); | ||
| 101 | } | ||
| 102 | |||
| 103 | } | ||
| 104 | |||
| 105 | uint8_t matrix_scan(void) | ||
| 106 | { | ||
| 107 | if (mcp23018_status) { // if there was an error | ||
| 108 | if (++mcp23018_reset_loop == 0) { | ||
| 109 | // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans | ||
| 110 | // this will be approx bit more frequent than once per second | ||
| 111 | print("trying to reset mcp23018\n"); | ||
| 112 | mcp23018_status = init_mcp23018(); | ||
| 113 | if (mcp23018_status) { | ||
| 114 | print("left side not responding\n"); | ||
| 115 | } else { | ||
| 116 | print("left side attached\n"); | ||
| 117 | ergodox_blink_all_leds(); | ||
| 118 | } | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | #ifdef DEBUG_MATRIX_SCAN_RATE | ||
| 123 | matrix_scan_count++; | ||
| 124 | |||
| 125 | uint32_t timer_now = timer_read32(); | ||
| 126 | if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) { | ||
| 127 | print("matrix scan frequency: "); | ||
| 128 | pdec(matrix_scan_count); | ||
| 129 | print("\n"); | ||
| 130 | |||
| 131 | matrix_timer = timer_now; | ||
| 132 | matrix_scan_count = 0; | ||
| 133 | } | ||
| 134 | #endif | ||
| 135 | |||
| 136 | #ifdef KEYMAP_CUB | ||
| 137 | uint8_t layer = biton32(layer_state); | ||
| 138 | |||
| 139 | ergodox_board_led_off(); | ||
| 140 | ergodox_left_led_1_off(); | ||
| 141 | ergodox_left_led_2_off(); | ||
| 142 | ergodox_left_led_3_off(); | ||
| 143 | switch (layer) { | ||
| 144 | case 1: | ||
| 145 | // all | ||
| 146 | ergodox_left_led_1_on(); | ||
| 147 | ergodox_left_led_2_on(); | ||
| 148 | ergodox_left_led_3_on(); | ||
| 149 | break; | ||
| 150 | case 2: | ||
| 151 | // blue | ||
| 152 | ergodox_left_led_2_on(); | ||
| 153 | break; | ||
| 154 | case 8: | ||
| 155 | // blue and green | ||
| 156 | ergodox_left_led_2_on(); | ||
| 157 | // break missed intentionally | ||
| 158 | case 3: | ||
| 159 | // green | ||
| 160 | ergodox_left_led_3_on(); | ||
| 161 | break; | ||
| 162 | case 6: | ||
| 163 | ergodox_board_led_on(); | ||
| 164 | // break missed intentionally | ||
| 165 | case 4: | ||
| 166 | case 5: | ||
| 167 | case 7: | ||
| 168 | // white | ||
| 169 | ergodox_left_led_1_on(); | ||
| 170 | break; | ||
| 171 | case 9: | ||
| 172 | // white+green | ||
| 173 | ergodox_left_led_1_on(); | ||
| 174 | ergodox_left_led_3_on(); | ||
| 175 | break; | ||
| 176 | default: | ||
| 177 | // none | ||
| 178 | break; | ||
| 179 | } | ||
| 180 | |||
| 181 | mcp23018_status = ergodox_left_leds_update(); | ||
| 182 | #endif | ||
| 183 | |||
| 184 | #ifdef KEYMAP_SIMON | ||
| 185 | uint8_t layer = biton32(layer_state); | ||
| 186 | |||
| 187 | ergodox_board_led_off(); | ||
| 188 | switch (layer) { | ||
| 189 | case 0: | ||
| 190 | // none | ||
| 191 | |||
| 192 | break; | ||
| 193 | default: | ||
| 194 | ergodox_board_led_on(); | ||
| 195 | break; | ||
| 196 | } | ||
| 197 | #endif | ||
| 198 | |||
| 199 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 200 | select_row(i); | ||
| 201 | matrix_row_t cols = read_cols(i); | ||
| 202 | if (matrix_debouncing[i] != cols) { | ||
| 203 | matrix_debouncing[i] = cols; | ||
| 204 | if (debouncing) { | ||
| 205 | debug("bounce!: "); debug_hex(debouncing); debug("\n"); | ||
| 206 | } | ||
| 207 | debouncing = DEBOUNCE; | ||
| 208 | } | ||
| 209 | unselect_rows(); | ||
| 210 | } | ||
| 211 | |||
| 212 | if (debouncing) { | ||
| 213 | if (--debouncing) { | ||
| 214 | _delay_ms(1); | ||
| 215 | } else { | ||
| 216 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 217 | matrix[i] = matrix_debouncing[i]; | ||
| 218 | } | ||
| 219 | } | ||
| 220 | } | ||
| 221 | |||
| 222 | |||
| 223 | if (matrix_scan_kb) { | ||
| 224 | (*matrix_scan_kb)(); | ||
| 225 | } | ||
| 226 | |||
| 227 | return 1; | ||
| 228 | } | ||
| 229 | |||
| 230 | bool matrix_is_modified(void) | ||
| 231 | { | ||
| 232 | if (debouncing) return false; | ||
| 233 | return true; | ||
| 234 | } | ||
| 235 | |||
| 236 | inline | ||
| 237 | bool matrix_is_on(uint8_t row, uint8_t col) | ||
| 238 | { | ||
| 239 | return (matrix[row] & ((matrix_row_t)1<<col)); | ||
| 240 | } | ||
| 241 | |||
| 242 | inline | ||
| 243 | matrix_row_t matrix_get_row(uint8_t row) | ||
| 244 | { | ||
| 245 | return matrix[row]; | ||
| 246 | } | ||
| 247 | |||
| 248 | void matrix_print(void) | ||
| 249 | { | ||
| 250 | print("\nr/c 0123456789ABCDEF\n"); | ||
| 251 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
| 252 | phex(row); print(": "); | ||
| 253 | pbin_reverse16(matrix_get_row(row)); | ||
| 254 | print("\n"); | ||
| 255 | } | ||
| 256 | } | ||
| 257 | |||
| 258 | uint8_t matrix_key_count(void) | ||
| 259 | { | ||
| 260 | uint8_t count = 0; | ||
| 261 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 262 | count += bitpop16(matrix[i]); | ||
| 263 | } | ||
| 264 | return count; | ||
| 265 | } | ||
| 266 | |||
| 267 | /* Column pin configuration | ||
| 268 | * | ||
| 269 | * Teensy | ||
| 270 | * col: 0 1 2 3 4 5 | ||
| 271 | * pin: F0 F1 F4 F5 F6 F7 | ||
| 272 | * | ||
| 273 | * MCP23018 | ||
| 274 | * col: 0 1 2 3 4 5 | ||
| 275 | * pin: B5 B4 B3 B2 B1 B0 | ||
| 276 | */ | ||
| 277 | static void init_cols(void) | ||
| 278 | { | ||
| 279 | // init on mcp23018 | ||
| 280 | // not needed, already done as part of init_mcp23018() | ||
| 281 | |||
| 282 | // init on teensy | ||
| 283 | // Input with pull-up(DDR:0, PORT:1) | ||
| 284 | DDRF &= ~(1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0); | ||
| 285 | PORTF |= (1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0); | ||
| 286 | } | ||
| 287 | |||
| 288 | static matrix_row_t read_cols(uint8_t row) | ||
| 289 | { | ||
| 290 | if (row < 7) { | ||
| 291 | if (mcp23018_status) { // if there was an error | ||
| 292 | return 0; | ||
| 293 | } else { | ||
| 294 | uint8_t data = 0; | ||
| 295 | mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out; | ||
| 296 | mcp23018_status = i2c_write(GPIOB); if (mcp23018_status) goto out; | ||
| 297 | mcp23018_status = i2c_start(I2C_ADDR_READ); if (mcp23018_status) goto out; | ||
| 298 | data = i2c_readNak(); | ||
| 299 | data = ~data; | ||
| 300 | out: | ||
| 301 | i2c_stop(); | ||
| 302 | return data; | ||
| 303 | } | ||
| 304 | } else { | ||
| 305 | _delay_us(30); // without this wait read unstable value. | ||
| 306 | // read from teensy | ||
| 307 | return | ||
| 308 | (PINF&(1<<0) ? 0 : (1<<0)) | | ||
| 309 | (PINF&(1<<1) ? 0 : (1<<1)) | | ||
| 310 | (PINF&(1<<4) ? 0 : (1<<2)) | | ||
| 311 | (PINF&(1<<5) ? 0 : (1<<3)) | | ||
| 312 | (PINF&(1<<6) ? 0 : (1<<4)) | | ||
| 313 | (PINF&(1<<7) ? 0 : (1<<5)) ; | ||
| 314 | } | ||
| 315 | } | ||
| 316 | |||
| 317 | /* Row pin configuration | ||
| 318 | * | ||
| 319 | * Teensy | ||
| 320 | * row: 7 8 9 10 11 12 13 | ||
| 321 | * pin: B0 B1 B2 B3 D2 D3 C6 | ||
| 322 | * | ||
| 323 | * MCP23018 | ||
| 324 | * row: 0 1 2 3 4 5 6 | ||
| 325 | * pin: A0 A1 A2 A3 A4 A5 A6 | ||
| 326 | */ | ||
| 327 | static void unselect_rows(void) | ||
| 328 | { | ||
| 329 | // unselect on mcp23018 | ||
| 330 | if (mcp23018_status) { // if there was an error | ||
| 331 | // do nothing | ||
| 332 | } else { | ||
| 333 | // set all rows hi-Z : 1 | ||
| 334 | mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out; | ||
| 335 | mcp23018_status = i2c_write(GPIOA); if (mcp23018_status) goto out; | ||
| 336 | mcp23018_status = i2c_write( 0xFF | ||
| 337 | & ~(ergodox_left_led_3<<LEFT_LED_3_SHIFT) | ||
| 338 | ); if (mcp23018_status) goto out; | ||
| 339 | out: | ||
| 340 | i2c_stop(); | ||
| 341 | } | ||
| 342 | |||
| 343 | // unselect on teensy | ||
| 344 | // Hi-Z(DDR:0, PORT:0) to unselect | ||
| 345 | DDRB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3); | ||
| 346 | PORTB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3); | ||
| 347 | DDRD &= ~(1<<2 | 1<<3); | ||
| 348 | PORTD &= ~(1<<2 | 1<<3); | ||
| 349 | DDRC &= ~(1<<6); | ||
| 350 | PORTC &= ~(1<<6); | ||
| 351 | } | ||
| 352 | |||
| 353 | static void select_row(uint8_t row) | ||
| 354 | { | ||
| 355 | if (row < 7) { | ||
| 356 | // select on mcp23018 | ||
| 357 | if (mcp23018_status) { // if there was an error | ||
| 358 | // do nothing | ||
| 359 | } else { | ||
| 360 | // set active row low : 0 | ||
| 361 | // set other rows hi-Z : 1 | ||
| 362 | mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out; | ||
| 363 | mcp23018_status = i2c_write(GPIOA); if (mcp23018_status) goto out; | ||
| 364 | mcp23018_status = i2c_write( 0xFF & ~(1<<row) | ||
| 365 | & ~(ergodox_left_led_3<<LEFT_LED_3_SHIFT) | ||
| 366 | ); if (mcp23018_status) goto out; | ||
| 367 | out: | ||
| 368 | i2c_stop(); | ||
| 369 | } | ||
| 370 | } else { | ||
| 371 | // select on teensy | ||
| 372 | // Output low(DDR:1, PORT:0) to select | ||
| 373 | switch (row) { | ||
| 374 | case 7: | ||
| 375 | DDRB |= (1<<0); | ||
| 376 | PORTB &= ~(1<<0); | ||
| 377 | break; | ||
| 378 | case 8: | ||
| 379 | DDRB |= (1<<1); | ||
| 380 | PORTB &= ~(1<<1); | ||
| 381 | break; | ||
| 382 | case 9: | ||
| 383 | DDRB |= (1<<2); | ||
| 384 | PORTB &= ~(1<<2); | ||
| 385 | break; | ||
| 386 | case 10: | ||
| 387 | DDRB |= (1<<3); | ||
| 388 | PORTB &= ~(1<<3); | ||
| 389 | break; | ||
| 390 | case 11: | ||
| 391 | DDRD |= (1<<2); | ||
| 392 | PORTD &= ~(1<<3); | ||
| 393 | break; | ||
| 394 | case 12: | ||
| 395 | DDRD |= (1<<3); | ||
| 396 | PORTD &= ~(1<<3); | ||
| 397 | break; | ||
| 398 | case 13: | ||
| 399 | DDRC |= (1<<6); | ||
| 400 | PORTC &= ~(1<<6); | ||
| 401 | break; | ||
| 402 | } | ||
| 403 | } | ||
| 404 | } | ||
| 405 | |||
