diff options
Diffstat (limited to 'quantum/matrix.c')
| -rw-r--r-- | quantum/matrix.c | 206 |
1 files changed, 96 insertions, 110 deletions
diff --git a/quantum/matrix.c b/quantum/matrix.c index 95bf4c097..3174e0739 100644 --- a/quantum/matrix.c +++ b/quantum/matrix.c | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | /* | 1 | /* |
| 2 | Copyright 2012 Jun Wako | 2 | Copyright 2012 Jun Wako |
| 3 | Generated by planckkeyboard.com (2014 Jack Humbert) | 3 | Copyright 2014 Jack Humbert |
| 4 | 4 | ||
| 5 | This program is free software: you can redistribute it and/or modify | 5 | This program is free software: you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by | 6 | it under the terms of the GNU General Public License as published by |
| @@ -15,23 +15,26 @@ GNU General Public License for more details. | |||
| 15 | You should have received a copy of the GNU General Public License | 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ | 17 | */ |
| 18 | |||
| 19 | /* | ||
| 20 | * scan matrix | ||
| 21 | */ | ||
| 22 | #include <stdint.h> | 18 | #include <stdint.h> |
| 23 | #include <stdbool.h> | 19 | #include <stdbool.h> |
| 20 | #if defined(__AVR__) | ||
| 24 | #include <avr/io.h> | 21 | #include <avr/io.h> |
| 25 | #include <util/delay.h> | 22 | #endif |
| 23 | #include "wait.h" | ||
| 26 | #include "print.h" | 24 | #include "print.h" |
| 27 | #include "debug.h" | 25 | #include "debug.h" |
| 28 | #include "util.h" | 26 | #include "util.h" |
| 29 | #include "matrix.h" | 27 | #include "matrix.h" |
| 30 | 28 | ||
| 31 | #ifndef DEBOUNCE | 29 | /* Set 0 if debouncing isn't needed */ |
| 32 | # define DEBOUNCE 10 | 30 | |
| 31 | #ifndef DEBOUNCING_DELAY | ||
| 32 | # define DEBOUNCING_DELAY 5 | ||
| 33 | #endif | 33 | #endif |
| 34 | static uint8_t debouncing = DEBOUNCE; | 34 | static uint8_t debouncing = DEBOUNCING_DELAY; |
| 35 | |||
| 36 | static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; | ||
| 37 | static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; | ||
| 35 | 38 | ||
| 36 | /* matrix state(1:on, 0:off) */ | 39 | /* matrix state(1:on, 0:off) */ |
| 37 | static matrix_row_t matrix[MATRIX_ROWS]; | 40 | static matrix_row_t matrix[MATRIX_ROWS]; |
| @@ -42,39 +45,85 @@ static matrix_row_t matrix_debouncing[MATRIX_ROWS]; | |||
| 42 | static matrix_row_t matrix_reversed_debouncing[MATRIX_COLS]; | 45 | static matrix_row_t matrix_reversed_debouncing[MATRIX_COLS]; |
| 43 | #endif | 46 | #endif |
| 44 | 47 | ||
| 48 | #if MATRIX_COLS > 16 | ||
| 49 | #define SHIFTER 1UL | ||
| 50 | #else | ||
| 51 | #define SHIFTER 1 | ||
| 52 | #endif | ||
| 53 | |||
| 45 | static matrix_row_t read_cols(void); | 54 | static matrix_row_t read_cols(void); |
| 46 | static void init_cols(void); | 55 | static void init_cols(void); |
| 47 | static void unselect_rows(void); | 56 | static void unselect_rows(void); |
| 48 | static void select_row(uint8_t row); | 57 | static void select_row(uint8_t row); |
| 49 | 58 | ||
| 50 | __attribute__ ((weak)) | 59 | __attribute__ ((weak)) |
| 51 | void * matrix_init_kb(void) { | 60 | void matrix_init_quantum(void) { |
| 61 | matrix_init_kb(); | ||
| 62 | } | ||
| 63 | |||
| 64 | __attribute__ ((weak)) | ||
| 65 | void matrix_scan_quantum(void) { | ||
| 66 | matrix_scan_kb(); | ||
| 67 | } | ||
| 52 | 68 | ||
| 53 | }; | 69 | __attribute__ ((weak)) |
| 70 | void matrix_init_kb(void) { | ||
| 71 | matrix_init_user(); | ||
| 72 | } | ||
| 73 | |||
| 74 | __attribute__ ((weak)) | ||
| 75 | void matrix_scan_kb(void) { | ||
| 76 | matrix_scan_user(); | ||
| 77 | } | ||
| 54 | 78 | ||
| 55 | __attribute__ ((weak)) | 79 | __attribute__ ((weak)) |
| 56 | void * matrix_scan_kb(void) { | 80 | void matrix_init_user(void) { |
| 81 | } | ||
| 57 | 82 | ||
| 58 | }; | 83 | __attribute__ ((weak)) |
| 84 | void matrix_scan_user(void) { | ||
| 85 | } | ||
| 59 | 86 | ||
| 60 | inline | 87 | inline |
| 61 | uint8_t matrix_rows(void) | 88 | uint8_t matrix_rows(void) { |
| 62 | { | ||
| 63 | return MATRIX_ROWS; | 89 | return MATRIX_ROWS; |
| 64 | } | 90 | } |
| 65 | 91 | ||
| 66 | inline | 92 | inline |
| 67 | uint8_t matrix_cols(void) | 93 | uint8_t matrix_cols(void) { |
| 68 | { | ||
| 69 | return MATRIX_COLS; | 94 | return MATRIX_COLS; |
| 70 | } | 95 | } |
| 71 | 96 | ||
| 72 | void matrix_init(void) | 97 | // void matrix_power_up(void) { |
| 73 | { | 98 | // #if DIODE_DIRECTION == COL2ROW |
| 99 | // for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) { | ||
| 100 | // /* DDRxn */ | ||
| 101 | // _SFR_IO8((row_pins[r] >> 4) + 1) |= _BV(row_pins[r] & 0xF); | ||
| 102 | // toggle_row(r); | ||
| 103 | // } | ||
| 104 | // for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) { | ||
| 105 | // /* PORTxn */ | ||
| 106 | // _SFR_IO8((col_pins[c] >> 4) + 2) |= _BV(col_pins[c] & 0xF); | ||
| 107 | // } | ||
| 108 | // #else | ||
| 109 | // for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) { | ||
| 110 | // /* DDRxn */ | ||
| 111 | // _SFR_IO8((col_pins[c] >> 4) + 1) |= _BV(col_pins[c] & 0xF); | ||
| 112 | // toggle_col(c); | ||
| 113 | // } | ||
| 114 | // for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) { | ||
| 115 | // /* PORTxn */ | ||
| 116 | // _SFR_IO8((row_pins[r] >> 4) + 2) |= _BV(row_pins[r] & 0xF); | ||
| 117 | // } | ||
| 118 | // #endif | ||
| 119 | // } | ||
| 120 | |||
| 121 | void matrix_init(void) { | ||
| 74 | // To use PORTF disable JTAG with writing JTD bit twice within four cycles. | 122 | // To use PORTF disable JTAG with writing JTD bit twice within four cycles. |
| 75 | MCUCR |= (1<<JTD); | 123 | #ifdef __AVR_ATmega32U4__ |
| 76 | MCUCR |= (1<<JTD); | 124 | MCUCR |= _BV(JTD); |
| 77 | 125 | MCUCR |= _BV(JTD); | |
| 126 | #endif | ||
| 78 | 127 | ||
| 79 | // initialize row and col | 128 | // initialize row and col |
| 80 | unselect_rows(); | 129 | unselect_rows(); |
| @@ -86,33 +135,30 @@ void matrix_init(void) | |||
| 86 | matrix_debouncing[i] = 0; | 135 | matrix_debouncing[i] = 0; |
| 87 | } | 136 | } |
| 88 | 137 | ||
| 89 | if (matrix_init_kb) { | 138 | matrix_init_quantum(); |
| 90 | (*matrix_init_kb)(); | ||
| 91 | } | ||
| 92 | } | 139 | } |
| 93 | 140 | ||
| 94 | |||
| 95 | uint8_t matrix_scan(void) | 141 | uint8_t matrix_scan(void) |
| 96 | { | 142 | { |
| 97 | 143 | ||
| 98 | #if DIODE_DIRECTION == COL2ROW | 144 | #if DIODE_DIRECTION == COL2ROW |
| 99 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | 145 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { |
| 100 | select_row(i); | 146 | select_row(i); |
| 101 | _delay_us(30); // without this wait read unstable value. | 147 | wait_us(30); // without this wait read unstable value. |
| 102 | matrix_row_t cols = read_cols(); | 148 | matrix_row_t cols = read_cols(); |
| 103 | if (matrix_debouncing[i] != cols) { | 149 | if (matrix_debouncing[i] != cols) { |
| 104 | matrix_debouncing[i] = cols; | 150 | matrix_debouncing[i] = cols; |
| 105 | if (debouncing) { | 151 | if (debouncing) { |
| 106 | debug("bounce!: "); debug_hex(debouncing); debug("\n"); | 152 | debug("bounce!: "); debug_hex(debouncing); debug("\n"); |
| 107 | } | 153 | } |
| 108 | debouncing = DEBOUNCE; | 154 | debouncing = DEBOUNCING_DELAY; |
| 109 | } | 155 | } |
| 110 | unselect_rows(); | 156 | unselect_rows(); |
| 111 | } | 157 | } |
| 112 | 158 | ||
| 113 | if (debouncing) { | 159 | if (debouncing) { |
| 114 | if (--debouncing) { | 160 | if (--debouncing) { |
| 115 | _delay_ms(1); | 161 | wait_ms(1); |
| 116 | } else { | 162 | } else { |
| 117 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | 163 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { |
| 118 | matrix[i] = matrix_debouncing[i]; | 164 | matrix[i] = matrix_debouncing[i]; |
| @@ -122,21 +168,21 @@ uint8_t matrix_scan(void) | |||
| 122 | #else | 168 | #else |
| 123 | for (uint8_t i = 0; i < MATRIX_COLS; i++) { | 169 | for (uint8_t i = 0; i < MATRIX_COLS; i++) { |
| 124 | select_row(i); | 170 | select_row(i); |
| 125 | _delay_us(30); // without this wait read unstable value. | 171 | wait_us(30); // without this wait read unstable value. |
| 126 | matrix_row_t rows = read_cols(); | 172 | matrix_row_t rows = read_cols(); |
| 127 | if (matrix_reversed_debouncing[i] != rows) { | 173 | if (matrix_reversed_debouncing[i] != rows) { |
| 128 | matrix_reversed_debouncing[i] = rows; | 174 | matrix_reversed_debouncing[i] = rows; |
| 129 | if (debouncing) { | 175 | if (debouncing) { |
| 130 | debug("bounce!: "); debug_hex(debouncing); debug("\n"); | 176 | debug("bounce!: "); debug_hex(debouncing); debug("\n"); |
| 131 | } | 177 | } |
| 132 | debouncing = DEBOUNCE; | 178 | debouncing = DEBOUNCING_DELAY; |
| 133 | } | 179 | } |
| 134 | unselect_rows(); | 180 | unselect_rows(); |
| 135 | } | 181 | } |
| 136 | 182 | ||
| 137 | if (debouncing) { | 183 | if (debouncing) { |
| 138 | if (--debouncing) { | 184 | if (--debouncing) { |
| 139 | _delay_ms(1); | 185 | wait_ms(1); |
| 140 | } else { | 186 | } else { |
| 141 | for (uint8_t i = 0; i < MATRIX_COLS; i++) { | 187 | for (uint8_t i = 0; i < MATRIX_COLS; i++) { |
| 142 | matrix_reversed[i] = matrix_reversed_debouncing[i]; | 188 | matrix_reversed[i] = matrix_reversed_debouncing[i]; |
| @@ -152,9 +198,7 @@ uint8_t matrix_scan(void) | |||
| 152 | } | 198 | } |
| 153 | #endif | 199 | #endif |
| 154 | 200 | ||
| 155 | if (matrix_scan_kb) { | 201 | matrix_scan_quantum(); |
| 156 | (*matrix_scan_kb)(); | ||
| 157 | } | ||
| 158 | 202 | ||
| 159 | return 1; | 203 | return 1; |
| 160 | } | 204 | } |
| @@ -198,32 +242,16 @@ uint8_t matrix_key_count(void) | |||
| 198 | 242 | ||
| 199 | static void init_cols(void) | 243 | static void init_cols(void) |
| 200 | { | 244 | { |
| 201 | int B = 0, C = 0, D = 0, E = 0, F = 0; | ||
| 202 | |||
| 203 | #if DIODE_DIRECTION == COL2ROW | 245 | #if DIODE_DIRECTION == COL2ROW |
| 204 | for(int x = 0; x < MATRIX_COLS; x++) { | 246 | for(int x = 0; x < MATRIX_COLS; x++) { |
| 205 | int col = COLS[x]; | 247 | int pin = col_pins[x]; |
| 206 | #else | 248 | #else |
| 207 | for(int x = 0; x < MATRIX_ROWS; x++) { | 249 | for(int x = 0; x < MATRIX_ROWS; x++) { |
| 208 | int col = ROWS[x]; | 250 | int pin = row_pins[x]; |
| 209 | #endif | 251 | #endif |
| 210 | if ((col & 0xF0) == 0x20) { | 252 | _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); |
| 211 | B |= (1<<(col & 0x0F)); | 253 | _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); |
| 212 | } else if ((col & 0xF0) == 0x30) { | ||
| 213 | C |= (1<<(col & 0x0F)); | ||
| 214 | } else if ((col & 0xF0) == 0x40) { | ||
| 215 | D |= (1<<(col & 0x0F)); | ||
| 216 | } else if ((col & 0xF0) == 0x50) { | ||
| 217 | E |= (1<<(col & 0x0F)); | ||
| 218 | } else if ((col & 0xF0) == 0x60) { | ||
| 219 | F |= (1<<(col & 0x0F)); | ||
| 220 | } | ||
| 221 | } | 254 | } |
| 222 | DDRB &= ~(B); PORTB |= (B); | ||
| 223 | DDRC &= ~(C); PORTC |= (C); | ||
| 224 | DDRD &= ~(D); PORTD |= (D); | ||
| 225 | DDRE &= ~(E); PORTE |= (E); | ||
| 226 | DDRF &= ~(F); PORTF |= (F); | ||
| 227 | } | 255 | } |
| 228 | 256 | ||
| 229 | static matrix_row_t read_cols(void) | 257 | static matrix_row_t read_cols(void) |
| @@ -232,80 +260,38 @@ static matrix_row_t read_cols(void) | |||
| 232 | 260 | ||
| 233 | #if DIODE_DIRECTION == COL2ROW | 261 | #if DIODE_DIRECTION == COL2ROW |
| 234 | for(int x = 0; x < MATRIX_COLS; x++) { | 262 | for(int x = 0; x < MATRIX_COLS; x++) { |
| 235 | int col = COLS[x]; | 263 | int pin = col_pins[x]; |
| 236 | #else | 264 | #else |
| 237 | for(int x = 0; x < MATRIX_ROWS; x++) { | 265 | for(int x = 0; x < MATRIX_ROWS; x++) { |
| 238 | int col = ROWS[x]; | 266 | int pin = row_pins[x]; |
| 239 | #endif | 267 | #endif |
| 240 | 268 | result |= (_SFR_IO8(pin >> 4) & _BV(pin & 0xF)) ? 0 : (SHIFTER << x); | |
| 241 | if ((col & 0xF0) == 0x20) { | ||
| 242 | result |= (PINB&(1<<(col & 0x0F)) ? 0 : (1<<x)); | ||
| 243 | } else if ((col & 0xF0) == 0x30) { | ||
| 244 | result |= (PINC&(1<<(col & 0x0F)) ? 0 : (1<<x)); | ||
| 245 | } else if ((col & 0xF0) == 0x40) { | ||
| 246 | result |= (PIND&(1<<(col & 0x0F)) ? 0 : (1<<x)); | ||
| 247 | } else if ((col & 0xF0) == 0x50) { | ||
| 248 | result |= (PINE&(1<<(col & 0x0F)) ? 0 : (1<<x)); | ||
| 249 | } else if ((col & 0xF0) == 0x60) { | ||
| 250 | result |= (PINF&(1<<(col & 0x0F)) ? 0 : (1<<x)); | ||
| 251 | } | ||
| 252 | } | 269 | } |
| 253 | return result; | 270 | return result; |
| 254 | } | 271 | } |
| 255 | 272 | ||
| 256 | static void unselect_rows(void) | 273 | static void unselect_rows(void) |
| 257 | { | 274 | { |
| 258 | int B = 0, C = 0, D = 0, E = 0, F = 0; | ||
| 259 | |||
| 260 | #if DIODE_DIRECTION == COL2ROW | 275 | #if DIODE_DIRECTION == COL2ROW |
| 261 | for(int x = 0; x < MATRIX_ROWS; x++) { | 276 | for(int x = 0; x < MATRIX_ROWS; x++) { |
| 262 | int row = ROWS[x]; | 277 | int pin = row_pins[x]; |
| 263 | #else | 278 | #else |
| 264 | for(int x = 0; x < MATRIX_COLS; x++) { | 279 | for(int x = 0; x < MATRIX_COLS; x++) { |
| 265 | int row = COLS[x]; | 280 | int pin = col_pins[x]; |
| 266 | #endif | 281 | #endif |
| 267 | if ((row & 0xF0) == 0x20) { | 282 | _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); |
| 268 | B |= (1<<(row & 0x0F)); | 283 | _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); |
| 269 | } else if ((row & 0xF0) == 0x30) { | ||
| 270 | C |= (1<<(row & 0x0F)); | ||
| 271 | } else if ((row & 0xF0) == 0x40) { | ||
| 272 | D |= (1<<(row & 0x0F)); | ||
| 273 | } else if ((row & 0xF0) == 0x50) { | ||
| 274 | E |= (1<<(row & 0x0F)); | ||
| 275 | } else if ((row & 0xF0) == 0x60) { | ||
| 276 | F |= (1<<(row & 0x0F)); | ||
| 277 | } | ||
| 278 | } | 284 | } |
| 279 | DDRB &= ~(B); PORTB |= (B); | ||
| 280 | DDRC &= ~(C); PORTC |= (C); | ||
| 281 | DDRD &= ~(D); PORTD |= (D); | ||
| 282 | DDRE &= ~(E); PORTE |= (E); | ||
| 283 | DDRF &= ~(F); PORTF |= (F); | ||
| 284 | } | 285 | } |
| 285 | 286 | ||
| 286 | static void select_row(uint8_t row) | 287 | static void select_row(uint8_t row) |
| 287 | { | 288 | { |
| 288 | 289 | ||
| 289 | #if DIODE_DIRECTION == COL2ROW | 290 | #if DIODE_DIRECTION == COL2ROW |
| 290 | int row_pin = ROWS[row]; | 291 | int pin = row_pins[row]; |
| 291 | #else | 292 | #else |
| 292 | int row_pin = COLS[row]; | 293 | int pin = col_pins[row]; |
| 293 | #endif | 294 | #endif |
| 294 | 295 | _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); | |
| 295 | if ((row_pin & 0xF0) == 0x20) { | 296 | _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); |
| 296 | DDRB |= (1<<(row_pin & 0x0F)); | 297 | } |
| 297 | PORTB &= ~(1<<(row_pin & 0x0F)); | ||
| 298 | } else if ((row_pin & 0xF0) == 0x30) { | ||
| 299 | DDRC |= (1<<(row_pin & 0x0F)); | ||
| 300 | PORTC &= ~(1<<(row_pin & 0x0F)); | ||
| 301 | } else if ((row_pin & 0xF0) == 0x40) { | ||
| 302 | DDRD |= (1<<(row_pin & 0x0F)); | ||
| 303 | PORTD &= ~(1<<(row_pin & 0x0F)); | ||
| 304 | } else if ((row_pin & 0xF0) == 0x50) { | ||
| 305 | DDRE |= (1<<(row_pin & 0x0F)); | ||
| 306 | PORTE &= ~(1<<(row_pin & 0x0F)); | ||
| 307 | } else if ((row_pin & 0xF0) == 0x60) { | ||
| 308 | DDRF |= (1<<(row_pin & 0x0F)); | ||
| 309 | PORTF &= ~(1<<(row_pin & 0x0F)); | ||
| 310 | } | ||
| 311 | } \ No newline at end of file | ||
