diff options
Diffstat (limited to 'keyboards/lily58/rev1')
| -rw-r--r-- | keyboards/lily58/rev1/config.h | 2 | ||||
| -rwxr-xr-x | keyboards/lily58/rev1/matrix.c | 357 | ||||
| -rwxr-xr-x | keyboards/lily58/rev1/serial_config.h | 4 | ||||
| -rwxr-xr-x | keyboards/lily58/rev1/split_util.c | 100 |
4 files changed, 2 insertions, 461 deletions
diff --git a/keyboards/lily58/rev1/config.h b/keyboards/lily58/rev1/config.h index 39b15fc5b..24dc41151 100644 --- a/keyboards/lily58/rev1/config.h +++ b/keyboards/lily58/rev1/config.h | |||
| @@ -35,6 +35,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
| 35 | #define MATRIX_ROW_PINS { C6, D7, E6, B4, B5 } | 35 | #define MATRIX_ROW_PINS { C6, D7, E6, B4, B5 } |
| 36 | #define MATRIX_COL_PINS { F6, F7, B1, B3, B2, B6 } | 36 | #define MATRIX_COL_PINS { F6, F7, B1, B3, B2, B6 } |
| 37 | 37 | ||
| 38 | #define SOFT_SERIAL_PIN D2 | ||
| 39 | #define SERIAL_USE_MULTI_TRANSACTION | ||
| 38 | 40 | ||
| 39 | /* define if matrix has ghost */ | 41 | /* define if matrix has ghost */ |
| 40 | //#define MATRIX_HAS_GHOST | 42 | //#define MATRIX_HAS_GHOST |
diff --git a/keyboards/lily58/rev1/matrix.c b/keyboards/lily58/rev1/matrix.c deleted file mode 100755 index d3ba1c924..000000000 --- a/keyboards/lily58/rev1/matrix.c +++ /dev/null | |||
| @@ -1,357 +0,0 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2012 Jun Wako <wakojun@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 <string.h> | ||
| 24 | #include <avr/io.h> | ||
| 25 | #include <avr/wdt.h> | ||
| 26 | #include <avr/interrupt.h> | ||
| 27 | #include <util/delay.h> | ||
| 28 | #include "print.h" | ||
| 29 | #include "debug.h" | ||
| 30 | #include "util.h" | ||
| 31 | #include "matrix.h" | ||
| 32 | #include "split_util.h" | ||
| 33 | #include "quantum.h" | ||
| 34 | |||
| 35 | #ifdef USE_MATRIX_I2C | ||
| 36 | # include "i2c.h" | ||
| 37 | #else // USE_SERIAL | ||
| 38 | # include "split_scomm.h" | ||
| 39 | #endif | ||
| 40 | |||
| 41 | #ifndef DEBOUNCE | ||
| 42 | # define DEBOUNCE 5 | ||
| 43 | #endif | ||
| 44 | |||
| 45 | #define ERROR_DISCONNECT_COUNT 5 | ||
| 46 | |||
| 47 | static uint8_t debouncing = DEBOUNCE; | ||
| 48 | static const int ROWS_PER_HAND = MATRIX_ROWS/2; | ||
| 49 | static uint8_t error_count = 0; | ||
| 50 | uint8_t is_master = 0 ; | ||
| 51 | |||
| 52 | static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; | ||
| 53 | static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; | ||
| 54 | |||
| 55 | /* matrix state(1:on, 0:off) */ | ||
| 56 | static matrix_row_t matrix[MATRIX_ROWS]; | ||
| 57 | static matrix_row_t matrix_debouncing[MATRIX_ROWS]; | ||
| 58 | |||
| 59 | static matrix_row_t read_cols(void); | ||
| 60 | static void init_cols(void); | ||
| 61 | static void unselect_rows(void); | ||
| 62 | static void select_row(uint8_t row); | ||
| 63 | static uint8_t matrix_master_scan(void); | ||
| 64 | |||
| 65 | |||
| 66 | __attribute__ ((weak)) | ||
| 67 | void matrix_init_kb(void) { | ||
| 68 | matrix_init_user(); | ||
| 69 | } | ||
| 70 | |||
| 71 | __attribute__ ((weak)) | ||
| 72 | void matrix_scan_kb(void) { | ||
| 73 | matrix_scan_user(); | ||
| 74 | } | ||
| 75 | |||
| 76 | __attribute__ ((weak)) | ||
| 77 | void matrix_init_user(void) { | ||
| 78 | } | ||
| 79 | |||
| 80 | __attribute__ ((weak)) | ||
| 81 | void matrix_scan_user(void) { | ||
| 82 | } | ||
| 83 | |||
| 84 | inline | ||
| 85 | uint8_t matrix_rows(void) | ||
| 86 | { | ||
| 87 | return MATRIX_ROWS; | ||
| 88 | } | ||
| 89 | |||
| 90 | inline | ||
| 91 | uint8_t matrix_cols(void) | ||
| 92 | { | ||
| 93 | return MATRIX_COLS; | ||
| 94 | } | ||
| 95 | |||
| 96 | void matrix_init(void) | ||
| 97 | { | ||
| 98 | split_keyboard_setup(); | ||
| 99 | |||
| 100 | // initialize row and col | ||
| 101 | unselect_rows(); | ||
| 102 | init_cols(); | ||
| 103 | |||
| 104 | setPinOutput(B0); | ||
| 105 | setPinOutput(D5); | ||
| 106 | writePinHigh(B0); | ||
| 107 | writePinHigh(D5); | ||
| 108 | |||
| 109 | // initialize matrix state: all keys off | ||
| 110 | for (uint8_t i=0; i < MATRIX_ROWS; i++) { | ||
| 111 | matrix[i] = 0; | ||
| 112 | matrix_debouncing[i] = 0; | ||
| 113 | } | ||
| 114 | |||
| 115 | is_master = has_usb(); | ||
| 116 | |||
| 117 | matrix_init_quantum(); | ||
| 118 | } | ||
| 119 | |||
| 120 | uint8_t _matrix_scan(void) | ||
| 121 | { | ||
| 122 | // Right hand is stored after the left in the matirx so, we need to offset it | ||
| 123 | int offset = isLeftHand ? 0 : (ROWS_PER_HAND); | ||
| 124 | |||
| 125 | for (uint8_t i = 0; i < ROWS_PER_HAND; i++) { | ||
| 126 | select_row(i); | ||
| 127 | _delay_us(30); // without this wait read unstable value. | ||
| 128 | matrix_row_t cols = read_cols(); | ||
| 129 | if (matrix_debouncing[i+offset] != cols) { | ||
| 130 | matrix_debouncing[i+offset] = cols; | ||
| 131 | debouncing = DEBOUNCE; | ||
| 132 | } | ||
| 133 | unselect_rows(); | ||
| 134 | } | ||
| 135 | |||
| 136 | if (debouncing) { | ||
| 137 | if (--debouncing) { | ||
| 138 | _delay_ms(1); | ||
| 139 | } else { | ||
| 140 | for (uint8_t i = 0; i < ROWS_PER_HAND; i++) { | ||
| 141 | matrix[i+offset] = matrix_debouncing[i+offset]; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | return 1; | ||
| 147 | } | ||
| 148 | |||
| 149 | #ifdef USE_MATRIX_I2C | ||
| 150 | |||
| 151 | // Get rows from other half over i2c | ||
| 152 | int i2c_transaction(void) { | ||
| 153 | int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0; | ||
| 154 | |||
| 155 | int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE); | ||
| 156 | if (err) goto i2c_error; | ||
| 157 | |||
| 158 | // start of matrix stored at 0x00 | ||
| 159 | err = i2c_master_write(0x00); | ||
| 160 | if (err) goto i2c_error; | ||
| 161 | |||
| 162 | // Start read | ||
| 163 | err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ); | ||
| 164 | if (err) goto i2c_error; | ||
| 165 | |||
| 166 | if (!err) { | ||
| 167 | int i; | ||
| 168 | for (i = 0; i < ROWS_PER_HAND-1; ++i) { | ||
| 169 | matrix[slaveOffset+i] = i2c_master_read(I2C_ACK); | ||
| 170 | } | ||
| 171 | matrix[slaveOffset+i] = i2c_master_read(I2C_NACK); | ||
| 172 | i2c_master_stop(); | ||
| 173 | } else { | ||
| 174 | i2c_error: // the cable is disconnceted, or something else went wrong | ||
| 175 | i2c_reset_state(); | ||
| 176 | return err; | ||
| 177 | } | ||
| 178 | |||
| 179 | return 0; | ||
| 180 | } | ||
| 181 | |||
| 182 | #else // USE_SERIAL | ||
| 183 | |||
| 184 | int serial_transaction(int master_changed) { | ||
| 185 | int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0; | ||
| 186 | #ifdef SERIAL_USE_MULTI_TRANSACTION | ||
| 187 | int ret=serial_update_buffers(master_changed); | ||
| 188 | #else | ||
| 189 | int ret=serial_update_buffers(); | ||
| 190 | #endif | ||
| 191 | if (ret ) { | ||
| 192 | if(ret==2) writePinLow(B0); | ||
| 193 | return 1; | ||
| 194 | } | ||
| 195 | writePinHigh(B0); | ||
| 196 | memcpy(&matrix[slaveOffset], | ||
| 197 | (void *)serial_slave_buffer, SERIAL_SLAVE_BUFFER_LENGTH); | ||
| 198 | return 0; | ||
| 199 | } | ||
| 200 | #endif | ||
| 201 | |||
| 202 | uint8_t matrix_scan(void) | ||
| 203 | { | ||
| 204 | if (is_master) { | ||
| 205 | matrix_master_scan(); | ||
| 206 | }else{ | ||
| 207 | matrix_slave_scan(); | ||
| 208 | int offset = (isLeftHand) ? ROWS_PER_HAND : 0; | ||
| 209 | memcpy(&matrix[offset], | ||
| 210 | (void *)serial_master_buffer, SERIAL_MASTER_BUFFER_LENGTH); | ||
| 211 | matrix_scan_quantum(); | ||
| 212 | } | ||
| 213 | return 1; | ||
| 214 | } | ||
| 215 | |||
| 216 | |||
| 217 | uint8_t matrix_master_scan(void) { | ||
| 218 | |||
| 219 | int ret = _matrix_scan(); | ||
| 220 | int mchanged = 1; | ||
| 221 | |||
| 222 | int offset = (isLeftHand) ? 0 : ROWS_PER_HAND; | ||
| 223 | |||
| 224 | #ifdef USE_MATRIX_I2C | ||
| 225 | // for (int i = 0; i < ROWS_PER_HAND; ++i) { | ||
| 226 | /* i2c_slave_buffer[i] = matrix[offset+i]; */ | ||
| 227 | // i2c_slave_buffer[i] = matrix[offset+i]; | ||
| 228 | // } | ||
| 229 | #else // USE_SERIAL | ||
| 230 | #ifdef SERIAL_USE_MULTI_TRANSACTION | ||
| 231 | mchanged = memcmp((void *)serial_master_buffer, | ||
| 232 | &matrix[offset], SERIAL_MASTER_BUFFER_LENGTH); | ||
| 233 | #endif | ||
| 234 | memcpy((void *)serial_master_buffer, | ||
| 235 | &matrix[offset], SERIAL_MASTER_BUFFER_LENGTH); | ||
| 236 | #endif | ||
| 237 | |||
| 238 | #ifdef USE_MATRIX_I2C | ||
| 239 | if( i2c_transaction() ) { | ||
| 240 | #else // USE_SERIAL | ||
| 241 | if( serial_transaction(mchanged) ) { | ||
| 242 | #endif | ||
| 243 | // turn on the indicator led when halves are disconnected | ||
| 244 | writePinLow(D5); | ||
| 245 | |||
| 246 | error_count++; | ||
| 247 | |||
| 248 | if (error_count > ERROR_DISCONNECT_COUNT) { | ||
| 249 | // reset other half if disconnected | ||
| 250 | int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0; | ||
| 251 | for (int i = 0; i < ROWS_PER_HAND; ++i) { | ||
| 252 | matrix[slaveOffset+i] = 0; | ||
| 253 | } | ||
| 254 | } | ||
| 255 | } else { | ||
| 256 | // turn off the indicator led on no error | ||
| 257 | writePinHigh(D5); | ||
| 258 | error_count = 0; | ||
| 259 | } | ||
| 260 | matrix_scan_quantum(); | ||
| 261 | return ret; | ||
| 262 | } | ||
| 263 | |||
| 264 | void matrix_slave_scan(void) { | ||
| 265 | _matrix_scan(); | ||
| 266 | |||
| 267 | int offset = (isLeftHand) ? 0 : ROWS_PER_HAND; | ||
| 268 | |||
| 269 | #ifdef USE_MATRIX_I2C | ||
| 270 | for (int i = 0; i < ROWS_PER_HAND; ++i) { | ||
| 271 | /* i2c_slave_buffer[i] = matrix[offset+i]; */ | ||
| 272 | i2c_slave_buffer[i] = matrix[offset+i]; | ||
| 273 | } | ||
| 274 | #else // USE_SERIAL | ||
| 275 | #ifdef SERIAL_USE_MULTI_TRANSACTION | ||
| 276 | int change = 0; | ||
| 277 | #endif | ||
| 278 | for (int i = 0; i < ROWS_PER_HAND; ++i) { | ||
| 279 | #ifdef SERIAL_USE_MULTI_TRANSACTION | ||
| 280 | if( serial_slave_buffer[i] != matrix[offset+i] ) | ||
| 281 | change = 1; | ||
| 282 | #endif | ||
| 283 | serial_slave_buffer[i] = matrix[offset+i]; | ||
| 284 | } | ||
| 285 | #ifdef SERIAL_USE_MULTI_TRANSACTION | ||
| 286 | slave_buffer_change_count += change; | ||
| 287 | #endif | ||
| 288 | #endif | ||
| 289 | } | ||
| 290 | |||
| 291 | bool matrix_is_modified(void) | ||
| 292 | { | ||
| 293 | if (debouncing) return false; | ||
| 294 | return true; | ||
| 295 | } | ||
| 296 | |||
| 297 | inline | ||
| 298 | bool matrix_is_on(uint8_t row, uint8_t col) | ||
| 299 | { | ||
| 300 | return (matrix[row] & ((matrix_row_t)1<<col)); | ||
| 301 | } | ||
| 302 | |||
| 303 | inline | ||
| 304 | matrix_row_t matrix_get_row(uint8_t row) | ||
| 305 | { | ||
| 306 | return matrix[row]; | ||
| 307 | } | ||
| 308 | |||
| 309 | void matrix_print(void) | ||
| 310 | { | ||
| 311 | print("\nr/c 0123456789ABCDEF\n"); | ||
| 312 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
| 313 | print_hex8(row); print(": "); | ||
| 314 | print_bin_reverse16(matrix_get_row(row)); | ||
| 315 | print("\n"); | ||
| 316 | } | ||
| 317 | } | ||
| 318 | |||
| 319 | uint8_t matrix_key_count(void) | ||
| 320 | { | ||
| 321 | uint8_t count = 0; | ||
| 322 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 323 | count += bitpop16(matrix[i]); | ||
| 324 | } | ||
| 325 | return count; | ||
| 326 | } | ||
| 327 | |||
| 328 | static void init_cols(void) | ||
| 329 | { | ||
| 330 | for(int x = 0; x < MATRIX_COLS; x++) { | ||
| 331 | _SFR_IO8((col_pins[x] >> 4) + 1) &= ~_BV(col_pins[x] & 0xF); | ||
| 332 | _SFR_IO8((col_pins[x] >> 4) + 2) |= _BV(col_pins[x] & 0xF); | ||
| 333 | } | ||
| 334 | } | ||
| 335 | |||
| 336 | static matrix_row_t read_cols(void) | ||
| 337 | { | ||
| 338 | matrix_row_t result = 0; | ||
| 339 | for(int x = 0; x < MATRIX_COLS; x++) { | ||
| 340 | result |= (_SFR_IO8(col_pins[x] >> 4) & _BV(col_pins[x] & 0xF)) ? 0 : (1 << x); | ||
| 341 | } | ||
| 342 | return result; | ||
| 343 | } | ||
| 344 | |||
| 345 | static void unselect_rows(void) | ||
| 346 | { | ||
| 347 | for(int x = 0; x < ROWS_PER_HAND; x++) { | ||
| 348 | _SFR_IO8((row_pins[x] >> 4) + 1) &= ~_BV(row_pins[x] & 0xF); | ||
| 349 | _SFR_IO8((row_pins[x] >> 4) + 2) |= _BV(row_pins[x] & 0xF); | ||
| 350 | } | ||
| 351 | } | ||
| 352 | |||
| 353 | static void select_row(uint8_t row) | ||
| 354 | { | ||
| 355 | _SFR_IO8((row_pins[row] >> 4) + 1) |= _BV(row_pins[row] & 0xF); | ||
| 356 | _SFR_IO8((row_pins[row] >> 4) + 2) &= ~_BV(row_pins[row] & 0xF); | ||
| 357 | } | ||
diff --git a/keyboards/lily58/rev1/serial_config.h b/keyboards/lily58/rev1/serial_config.h deleted file mode 100755 index 4fab8e8dd..000000000 --- a/keyboards/lily58/rev1/serial_config.h +++ /dev/null | |||
| @@ -1,4 +0,0 @@ | |||
| 1 | #ifndef SOFT_SERIAL_PIN | ||
| 2 | #define SOFT_SERIAL_PIN D2 | ||
| 3 | #define SERIAL_USE_MULTI_TRANSACTION | ||
| 4 | #endif | ||
diff --git a/keyboards/lily58/rev1/split_util.c b/keyboards/lily58/rev1/split_util.c deleted file mode 100755 index 316c1c389..000000000 --- a/keyboards/lily58/rev1/split_util.c +++ /dev/null | |||
| @@ -1,100 +0,0 @@ | |||
| 1 | #include <avr/io.h> | ||
| 2 | #include <avr/wdt.h> | ||
| 3 | #include <avr/power.h> | ||
| 4 | #include <avr/interrupt.h> | ||
| 5 | #include <util/delay.h> | ||
| 6 | #include <avr/eeprom.h> | ||
| 7 | #include "split_util.h" | ||
| 8 | #include "matrix.h" | ||
| 9 | #include "keyboard.h" | ||
| 10 | #include "wait.h" | ||
| 11 | |||
| 12 | #ifdef USE_MATRIX_I2C | ||
| 13 | # include "i2c.h" | ||
| 14 | #else | ||
| 15 | # include "split_scomm.h" | ||
| 16 | #endif | ||
| 17 | |||
| 18 | #ifndef SPLIT_USB_TIMEOUT | ||
| 19 | # define SPLIT_USB_TIMEOUT 2500 | ||
| 20 | #endif | ||
| 21 | |||
| 22 | volatile bool isLeftHand = true; | ||
| 23 | |||
| 24 | bool waitForUsb(void) { | ||
| 25 | for (uint8_t i = 0; i < (SPLIT_USB_TIMEOUT / 100); i++) { | ||
| 26 | // This will return true of a USB connection has been established | ||
| 27 | if (UDADDR & _BV(ADDEN)) { | ||
| 28 | return true; | ||
| 29 | } | ||
| 30 | wait_ms(100); | ||
| 31 | } | ||
| 32 | |||
| 33 | // Avoid NO_USB_STARTUP_CHECK - Disable USB as the previous checks seem to enable it somehow | ||
| 34 | (USBCON &= ~(_BV(USBE) | _BV(OTGPADE))); | ||
| 35 | |||
| 36 | return false; | ||
| 37 | } | ||
| 38 | |||
| 39 | __attribute__((weak)) bool is_keyboard_left(void) { | ||
| 40 | #if defined(SPLIT_HAND_PIN) | ||
| 41 | // Test pin SPLIT_HAND_PIN for High/Low, if low it's right hand | ||
| 42 | setPinInput(SPLIT_HAND_PIN); | ||
| 43 | return readPin(SPLIT_HAND_PIN); | ||
| 44 | #elif defined(EE_HANDS) | ||
| 45 | return eeconfig_read_handedness(); | ||
| 46 | #elif defined(MASTER_RIGHT) | ||
| 47 | return !has_usb(); | ||
| 48 | #endif | ||
| 49 | |||
| 50 | return has_usb(); | ||
| 51 | } | ||
| 52 | |||
| 53 | __attribute__((weak)) bool has_usb(void) { | ||
| 54 | static enum { UNKNOWN, MASTER, SLAVE } usbstate = UNKNOWN; | ||
| 55 | |||
| 56 | // only check once, as this is called often | ||
| 57 | if (usbstate == UNKNOWN) { | ||
| 58 | #if defined(SPLIT_USB_DETECT) | ||
| 59 | usbstate = waitForUsb() ? MASTER : SLAVE; | ||
| 60 | #elif defined(__AVR__) | ||
| 61 | USBCON |= (1 << OTGPADE); // enables VBUS pad | ||
| 62 | wait_us(5); | ||
| 63 | |||
| 64 | usbstate = (USBSTA & (1 << VBUS)) ? MASTER : SLAVE; // checks state of VBUS | ||
| 65 | #else | ||
| 66 | usbstate = MASTER; | ||
| 67 | #endif | ||
| 68 | } | ||
| 69 | |||
| 70 | return (usbstate == MASTER); | ||
| 71 | } | ||
| 72 | |||
| 73 | static void keyboard_master_setup(void) { | ||
| 74 | |||
| 75 | #ifdef USE_MATRIX_I2C | ||
| 76 | i2c_master_init(); | ||
| 77 | #else | ||
| 78 | serial_master_init(); | ||
| 79 | #endif | ||
| 80 | } | ||
| 81 | |||
| 82 | static void keyboard_slave_setup(void) { | ||
| 83 | |||
| 84 | #ifdef USE_MATRIX_I2C | ||
| 85 | i2c_slave_init(SLAVE_I2C_ADDRESS); | ||
| 86 | #else | ||
| 87 | serial_slave_init(); | ||
| 88 | #endif | ||
| 89 | } | ||
| 90 | |||
| 91 | void split_keyboard_setup(void) { | ||
| 92 | isLeftHand = is_keyboard_left(); | ||
| 93 | |||
| 94 | if (has_usb()) { | ||
| 95 | keyboard_master_setup(); | ||
| 96 | } else { | ||
| 97 | keyboard_slave_setup(); | ||
| 98 | } | ||
| 99 | sei(); | ||
| 100 | } | ||
