diff options
Diffstat (limited to 'keyboards/ergodox/ez/matrix.c')
| -rw-r--r-- | keyboards/ergodox/ez/matrix.c | 382 |
1 files changed, 382 insertions, 0 deletions
diff --git a/keyboards/ergodox/ez/matrix.c b/keyboards/ergodox/ez/matrix.c new file mode 100644 index 000000000..a19bab90b --- /dev/null +++ b/keyboards/ergodox/ez/matrix.c | |||
| @@ -0,0 +1,382 @@ | |||
| 1 | /* | ||
| 2 | |||
| 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/ :) | ||
| 6 | Love, Erez | ||
| 7 | |||
| 8 | Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com> | ||
| 9 | |||
| 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. | ||
| 14 | |||
| 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. | ||
| 19 | |||
| 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/>. | ||
| 22 | */ | ||
| 23 | |||
| 24 | /* | ||
| 25 | * scan matrix | ||
| 26 | */ | ||
| 27 | #include <stdint.h> | ||
| 28 | #include <stdbool.h> | ||
| 29 | #include <avr/io.h> | ||
| 30 | #include "wait.h" | ||
| 31 | #include "action_layer.h" | ||
| 32 | #include "print.h" | ||
| 33 | #include "debug.h" | ||
| 34 | #include "util.h" | ||
| 35 | #include "matrix.h" | ||
| 36 | #include "ez.h" | ||
| 37 | #include "i2cmaster.h" | ||
| 38 | #ifdef DEBUG_MATRIX_SCAN_RATE | ||
| 39 | #include "timer.h" | ||
| 40 | #endif | ||
| 41 | |||
| 42 | /* | ||
| 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. | ||
| 45 | * | ||
| 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. | ||
| 49 | * | ||
| 50 | * And so, there is no sense to have DEBOUNCE higher than 2. | ||
| 51 | */ | ||
| 52 | |||
| 53 | #ifndef DEBOUNCE | ||
| 54 | # define DEBOUNCE 5 | ||
| 55 | #endif | ||
| 56 | static uint8_t debouncing = DEBOUNCE; | ||
| 57 | |||
| 58 | /* matrix state(1:on, 0:off) */ | ||
| 59 | static matrix_row_t matrix[MATRIX_ROWS]; | ||
| 60 | static matrix_row_t matrix_debouncing[MATRIX_ROWS]; | ||
| 61 | |||
| 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); | ||
| 66 | |||
| 67 | static uint8_t mcp23018_reset_loop; | ||
| 68 | |||
| 69 | #ifdef DEBUG_MATRIX_SCAN_RATE | ||
| 70 | uint32_t matrix_timer; | ||
| 71 | uint32_t matrix_scan_count; | ||
| 72 | #endif | ||
| 73 | |||
| 74 | |||
| 75 | __attribute__ ((weak)) | ||
| 76 | void matrix_init_user(void) {} | ||
| 77 | |||
| 78 | __attribute__ ((weak)) | ||
| 79 | void matrix_scan_user(void) {} | ||
| 80 | |||
| 81 | __attribute__ ((weak)) | ||
| 82 | void matrix_init_kb(void) { | ||
| 83 | matrix_init_user(); | ||
| 84 | } | ||
| 85 | |||
| 86 | __attribute__ ((weak)) | ||
| 87 | void matrix_scan_kb(void) { | ||
| 88 | matrix_scan_user(); | ||
| 89 | } | ||
| 90 | |||
| 91 | inline | ||
| 92 | uint8_t matrix_rows(void) | ||
| 93 | { | ||
| 94 | return MATRIX_ROWS; | ||
| 95 | } | ||
| 96 | |||
| 97 | inline | ||
| 98 | uint8_t matrix_cols(void) | ||
| 99 | { | ||
| 100 | return MATRIX_COLS; | ||
| 101 | } | ||
| 102 | |||
| 103 | void matrix_init(void) | ||
| 104 | { | ||
| 105 | // initialize row and col | ||
| 106 | |||
| 107 | mcp23018_status = init_mcp23018(); | ||
| 108 | |||
| 109 | |||
| 110 | unselect_rows(); | ||
| 111 | init_cols(); | ||
| 112 | |||
| 113 | // initialize matrix state: all keys off | ||
| 114 | for (uint8_t i=0; i < MATRIX_ROWS; i++) { | ||
| 115 | matrix[i] = 0; | ||
| 116 | matrix_debouncing[i] = 0; | ||
| 117 | } | ||
| 118 | |||
| 119 | #ifdef DEBUG_MATRIX_SCAN_RATE | ||
| 120 | matrix_timer = timer_read32(); | ||
| 121 | matrix_scan_count = 0; | ||
| 122 | #endif | ||
| 123 | |||
| 124 | matrix_init_kb(); | ||
| 125 | |||
| 126 | } | ||
| 127 | |||
| 128 | void matrix_power_up(void) { | ||
| 129 | mcp23018_status = init_mcp23018(); | ||
| 130 | |||
| 131 | unselect_rows(); | ||
| 132 | init_cols(); | ||
| 133 | |||
| 134 | // initialize matrix state: all keys off | ||
| 135 | for (uint8_t i=0; i < MATRIX_ROWS; i++) { | ||
| 136 | matrix[i] = 0; | ||
| 137 | matrix_debouncing[i] = 0; | ||
| 138 | } | ||
| 139 | |||
| 140 | #ifdef DEBUG_MATRIX_SCAN_RATE | ||
| 141 | matrix_timer = timer_read32(); | ||
| 142 | matrix_scan_count = 0; | ||
| 143 | #endif | ||
| 144 | |||
| 145 | } | ||
| 146 | |||
| 147 | uint8_t matrix_scan(void) | ||
| 148 | { | ||
| 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"); | ||
| 157 | } else { | ||
| 158 | print("left side attached\n"); | ||
| 159 | ergodox_blink_all_leds(); | ||
| 160 | } | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | #ifdef DEBUG_MATRIX_SCAN_RATE | ||
| 165 | matrix_scan_count++; | ||
| 166 | |||
| 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); | ||
| 171 | print("\n"); | ||
| 172 | |||
| 173 | matrix_timer = timer_now; | ||
| 174 | matrix_scan_count = 0; | ||
| 175 | } | ||
| 176 | #endif | ||
| 177 | |||
| 178 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 179 | select_row(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; | ||
| 184 | if (debouncing) { | ||
| 185 | debug("bounce!: "); debug_hex(debouncing); debug("\n"); | ||
| 186 | } | ||
| 187 | debouncing = DEBOUNCE; | ||
| 188 | } | ||
| 189 | unselect_rows(); | ||
| 190 | } | ||
| 191 | |||
| 192 | if (debouncing) { | ||
| 193 | if (--debouncing) { | ||
| 194 | wait_us(1); | ||
| 195 | // this should be wait_ms(1) but has been left as-is at EZ's request | ||
| 196 | } else { | ||
| 197 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 198 | matrix[i] = matrix_debouncing[i]; | ||
| 199 | } | ||
| 200 | } | ||
| 201 | } | ||
| 202 | |||
| 203 | matrix_scan_quantum(); | ||
| 204 | |||
| 205 | return 1; | ||
| 206 | } | ||
| 207 | |||
| 208 | bool matrix_is_modified(void) | ||
| 209 | { | ||
| 210 | if (debouncing) return false; | ||
| 211 | return true; | ||
| 212 | } | ||
| 213 | |||
| 214 | inline | ||
| 215 | bool matrix_is_on(uint8_t row, uint8_t col) | ||
| 216 | { | ||
| 217 | return (matrix[row] & ((matrix_row_t)1<<col)); | ||
| 218 | } | ||
| 219 | |||
| 220 | inline | ||
| 221 | matrix_row_t matrix_get_row(uint8_t row) | ||
| 222 | { | ||
| 223 | return matrix[row]; | ||
| 224 | } | ||
| 225 | |||
| 226 | void matrix_print(void) | ||
| 227 | { | ||
| 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)); | ||
| 232 | print("\n"); | ||
| 233 | } | ||
| 234 | } | ||
| 235 | |||
| 236 | uint8_t matrix_key_count(void) | ||
| 237 | { | ||
| 238 | uint8_t count = 0; | ||
| 239 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 240 | count += bitpop16(matrix[i]); | ||
| 241 | } | ||
| 242 | return count; | ||
| 243 | } | ||
| 244 | |||
| 245 | /* Column pin configuration | ||
| 246 | * | ||
| 247 | * Teensy | ||
| 248 | * col: 0 1 2 3 4 5 | ||
| 249 | * pin: F0 F1 F4 F5 F6 F7 | ||
| 250 | * | ||
| 251 | * MCP23018 | ||
| 252 | * col: 0 1 2 3 4 5 | ||
| 253 | * pin: B5 B4 B3 B2 B1 B0 | ||
| 254 | */ | ||
| 255 | static void init_cols(void) | ||
| 256 | { | ||
| 257 | // init on mcp23018 | ||
| 258 | // not needed, already done as part of init_mcp23018() | ||
| 259 | |||
| 260 | // init on teensy | ||
| 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); | ||
| 264 | } | ||
| 265 | |||
| 266 | static matrix_row_t read_cols(uint8_t row) | ||
| 267 | { | ||
| 268 | if (row < 7) { | ||
| 269 | if (mcp23018_status) { // if there was an error | ||
| 270 | return 0; | ||
| 271 | } else { | ||
| 272 | uint8_t data = 0; | ||
| 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(); | ||
| 277 | data = ~data; | ||
| 278 | out: | ||
| 279 | i2c_stop(); | ||
| 280 | return data; | ||
| 281 | } | ||
| 282 | } else { | ||
| 283 | // read from teensy | ||
| 284 | return | ||
| 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)) ; | ||
| 291 | } | ||
| 292 | } | ||
| 293 | |||
| 294 | /* Row pin configuration | ||
| 295 | * | ||
| 296 | * Teensy | ||
| 297 | * row: 7 8 9 10 11 12 13 | ||
| 298 | * pin: B0 B1 B2 B3 D2 D3 C6 | ||
| 299 | * | ||
| 300 | * MCP23018 | ||
| 301 | * row: 0 1 2 3 4 5 6 | ||
| 302 | * pin: A0 A1 A2 A3 A4 A5 A6 | ||
| 303 | */ | ||
| 304 | static void unselect_rows(void) | ||
| 305 | { | ||
| 306 | // unselect on mcp23018 | ||
| 307 | if (mcp23018_status) { // if there was an error | ||
| 308 | // do nothing | ||
| 309 | } else { | ||
| 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 | ||
| 314 | & ~(0<<7) | ||
| 315 | ); if (mcp23018_status) goto out; | ||
| 316 | out: | ||
| 317 | i2c_stop(); | ||
| 318 | } | ||
| 319 | |||
| 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); | ||
| 326 | DDRC &= ~(1<<6); | ||
| 327 | PORTC &= ~(1<<6); | ||
| 328 | } | ||
| 329 | |||
| 330 | static void select_row(uint8_t row) | ||
| 331 | { | ||
| 332 | if (row < 7) { | ||
| 333 | // select on mcp23018 | ||
| 334 | if (mcp23018_status) { // if there was an error | ||
| 335 | // do nothing | ||
| 336 | } else { | ||
| 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) | ||
| 342 | & ~(0<<7) | ||
| 343 | ); if (mcp23018_status) goto out; | ||
| 344 | out: | ||
| 345 | i2c_stop(); | ||
| 346 | } | ||
| 347 | } else { | ||
| 348 | // select on teensy | ||
| 349 | // Output low(DDR:1, PORT:0) to select | ||
| 350 | switch (row) { | ||
| 351 | case 7: | ||
| 352 | DDRB |= (1<<0); | ||
| 353 | PORTB &= ~(1<<0); | ||
| 354 | break; | ||
| 355 | case 8: | ||
| 356 | DDRB |= (1<<1); | ||
| 357 | PORTB &= ~(1<<1); | ||
| 358 | break; | ||
| 359 | case 9: | ||
| 360 | DDRB |= (1<<2); | ||
| 361 | PORTB &= ~(1<<2); | ||
| 362 | break; | ||
| 363 | case 10: | ||
| 364 | DDRB |= (1<<3); | ||
| 365 | PORTB &= ~(1<<3); | ||
| 366 | break; | ||
| 367 | case 11: | ||
| 368 | DDRD |= (1<<2); | ||
| 369 | PORTD &= ~(1<<3); | ||
| 370 | break; | ||
| 371 | case 12: | ||
| 372 | DDRD |= (1<<3); | ||
| 373 | PORTD &= ~(1<<3); | ||
| 374 | break; | ||
| 375 | case 13: | ||
| 376 | DDRC |= (1<<6); | ||
| 377 | PORTC &= ~(1<<6); | ||
| 378 | break; | ||
| 379 | } | ||
| 380 | } | ||
| 381 | } | ||
| 382 | |||
