diff options
Diffstat (limited to 'quantum/split_common/transport.c')
| -rw-r--r-- | quantum/split_common/transport.c | 136 |
1 files changed, 123 insertions, 13 deletions
diff --git a/quantum/split_common/transport.c b/quantum/split_common/transport.c index 467ff81a9..61b61ea08 100644 --- a/quantum/split_common/transport.c +++ b/quantum/split_common/transport.c | |||
| @@ -6,6 +6,7 @@ | |||
| 6 | #include "quantum.h" | 6 | #include "quantum.h" |
| 7 | 7 | ||
| 8 | #define ROWS_PER_HAND (MATRIX_ROWS / 2) | 8 | #define ROWS_PER_HAND (MATRIX_ROWS / 2) |
| 9 | #define SYNC_TIMER_OFFSET 2 | ||
| 9 | 10 | ||
| 10 | #ifdef RGBLIGHT_ENABLE | 11 | #ifdef RGBLIGHT_ENABLE |
| 11 | # include "rgblight.h" | 12 | # include "rgblight.h" |
| @@ -27,8 +28,23 @@ static pin_t encoders_pad[] = ENCODERS_PAD_A; | |||
| 27 | # include "i2c_slave.h" | 28 | # include "i2c_slave.h" |
| 28 | 29 | ||
| 29 | typedef struct _I2C_slave_buffer_t { | 30 | typedef struct _I2C_slave_buffer_t { |
| 31 | # ifndef DISABLE_SYNC_TIMER | ||
| 32 | uint32_t sync_timer; | ||
| 33 | # endif | ||
| 34 | # ifdef SPLIT_TRANSPORT_MIRROR | ||
| 35 | matrix_row_t mmatrix[ROWS_PER_HAND]; | ||
| 36 | # endif | ||
| 30 | matrix_row_t smatrix[ROWS_PER_HAND]; | 37 | matrix_row_t smatrix[ROWS_PER_HAND]; |
| 31 | uint8_t backlight_level; | 38 | # ifdef SPLIT_MODS_ENABLE |
| 39 | uint8_t real_mods; | ||
| 40 | uint8_t weak_mods; | ||
| 41 | # ifndef NO_ACTION_ONESHOT | ||
| 42 | uint8_t oneshot_mods; | ||
| 43 | # endif | ||
| 44 | # endif | ||
| 45 | # ifdef BACKLIGHT_ENABLE | ||
| 46 | uint8_t backlight_level; | ||
| 47 | # endif | ||
| 32 | # if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT) | 48 | # if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT) |
| 33 | rgblight_syncinfo_t rgblight_sync; | 49 | rgblight_syncinfo_t rgblight_sync; |
| 34 | # endif | 50 | # endif |
| @@ -42,9 +58,14 @@ typedef struct _I2C_slave_buffer_t { | |||
| 42 | 58 | ||
| 43 | static I2C_slave_buffer_t *const i2c_buffer = (I2C_slave_buffer_t *)i2c_slave_reg; | 59 | static I2C_slave_buffer_t *const i2c_buffer = (I2C_slave_buffer_t *)i2c_slave_reg; |
| 44 | 60 | ||
| 61 | # define I2C_SYNC_TIME_START offsetof(I2C_slave_buffer_t, sync_timer) | ||
| 62 | # define I2C_KEYMAP_MASTER_START offsetof(I2C_slave_buffer_t, mmatrix) | ||
| 63 | # define I2C_KEYMAP_SLAVE_START offsetof(I2C_slave_buffer_t, smatrix) | ||
| 64 | # define I2C_REAL_MODS_START offsetof(I2C_slave_buffer_t, real_mods) | ||
| 65 | # define I2C_WEAK_MODS_START offsetof(I2C_slave_buffer_t, weak_mods) | ||
| 66 | # define I2C_ONESHOT_MODS_START offsetof(I2C_slave_buffer_t, oneshot_mods) | ||
| 45 | # define I2C_BACKLIGHT_START offsetof(I2C_slave_buffer_t, backlight_level) | 67 | # define I2C_BACKLIGHT_START offsetof(I2C_slave_buffer_t, backlight_level) |
| 46 | # define I2C_RGB_START offsetof(I2C_slave_buffer_t, rgblight_sync) | 68 | # define I2C_RGB_START offsetof(I2C_slave_buffer_t, rgblight_sync) |
| 47 | # define I2C_KEYMAP_START offsetof(I2C_slave_buffer_t, smatrix) | ||
| 48 | # define I2C_ENCODER_START offsetof(I2C_slave_buffer_t, encoder_state) | 69 | # define I2C_ENCODER_START offsetof(I2C_slave_buffer_t, encoder_state) |
| 49 | # define I2C_WPM_START offsetof(I2C_slave_buffer_t, current_wpm) | 70 | # define I2C_WPM_START offsetof(I2C_slave_buffer_t, current_wpm) |
| 50 | 71 | ||
| @@ -55,8 +76,11 @@ static I2C_slave_buffer_t *const i2c_buffer = (I2C_slave_buffer_t *)i2c_slave_re | |||
| 55 | # endif | 76 | # endif |
| 56 | 77 | ||
| 57 | // Get rows from other half over i2c | 78 | // Get rows from other half over i2c |
| 58 | bool transport_master(matrix_row_t matrix[]) { | 79 | bool transport_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) { |
| 59 | i2c_readReg(SLAVE_I2C_ADDRESS, I2C_KEYMAP_START, (void *)matrix, sizeof(i2c_buffer->smatrix), TIMEOUT); | 80 | i2c_readReg(SLAVE_I2C_ADDRESS, I2C_KEYMAP_SLAVE_START, (void *)slave_matrix, sizeof(i2c_buffer->smatrix), TIMEOUT); |
| 81 | # ifdef SPLIT_TRANSPORT_MIRROR | ||
| 82 | i2c_writeReg(SLAVE_I2C_ADDRESS, I2C_KEYMAP_MASTER_START, (void *)master_matrix, sizeof(i2c_buffer->mmatrix), TIMEOUT); | ||
| 83 | # endif | ||
| 60 | 84 | ||
| 61 | // write backlight info | 85 | // write backlight info |
| 62 | # ifdef BACKLIGHT_ENABLE | 86 | # ifdef BACKLIGHT_ENABLE |
| @@ -91,12 +115,48 @@ bool transport_master(matrix_row_t matrix[]) { | |||
| 91 | } | 115 | } |
| 92 | } | 116 | } |
| 93 | # endif | 117 | # endif |
| 118 | |||
| 119 | # ifdef SPLIT_MODS_ENABLE | ||
| 120 | uint8_t real_mods = get_mods(); | ||
| 121 | if (real_mods != i2c_buffer->real_mods) { | ||
| 122 | if (i2c_writeReg(SLAVE_I2C_ADDRESS, I2C_REAL_MODS_START, (void *)&real_mods, sizeof(real_mods), TIMEOUT) >= 0) { | ||
| 123 | i2c_buffer->real_mods = real_mods; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | uint8_t weak_mods = get_weak_mods(); | ||
| 128 | if (weak_mods != i2c_buffer->weak_mods) { | ||
| 129 | if (i2c_writeReg(SLAVE_I2C_ADDRESS, I2C_WEAK_MODS_START, (void *)&weak_mods, sizeof(weak_mods), TIMEOUT) >= 0) { | ||
| 130 | i2c_buffer->weak_mods = weak_mods; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | # ifndef NO_ACTION_ONESHOT | ||
| 135 | uint8_t oneshot_mods = get_oneshot_mods(); | ||
| 136 | if (oneshot_mods != i2c_buffer->oneshot_mods) { | ||
| 137 | if (i2c_writeReg(SLAVE_I2C_ADDRESS, I2C_ONESHOT_MODS_START, (void *)&oneshot_mods, sizeof(oneshot_mods), TIMEOUT) >= 0) { | ||
| 138 | i2c_buffer->oneshot_mods = oneshot_mods; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | # endif | ||
| 142 | # endif | ||
| 143 | |||
| 144 | # ifndef DISABLE_SYNC_TIMER | ||
| 145 | i2c_buffer->sync_timer = sync_timer_read32() + SYNC_TIMER_OFFSET; | ||
| 146 | i2c_writeReg(SLAVE_I2C_ADDRESS, I2C_SYNC_TIME_START, (void *)&i2c_buffer->sync_timer, sizeof(i2c_buffer->sync_timer), TIMEOUT); | ||
| 147 | # endif | ||
| 94 | return true; | 148 | return true; |
| 95 | } | 149 | } |
| 96 | 150 | ||
| 97 | void transport_slave(matrix_row_t matrix[]) { | 151 | void transport_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) { |
| 152 | # ifndef DISABLE_SYNC_TIMER | ||
| 153 | sync_timer_update(i2c_buffer->sync_timer); | ||
| 154 | # endif | ||
| 98 | // Copy matrix to I2C buffer | 155 | // Copy matrix to I2C buffer |
| 99 | memcpy((void *)i2c_buffer->smatrix, (void *)matrix, sizeof(i2c_buffer->smatrix)); | 156 | memcpy((void *)i2c_buffer->smatrix, (void *)slave_matrix, sizeof(i2c_buffer->smatrix)); |
| 157 | # ifdef SPLIT_TRANSPORT_MIRROR | ||
| 158 | memcpy((void *)master_matrix, (void *)i2c_buffer->mmatrix, sizeof(i2c_buffer->mmatrix)); | ||
| 159 | # endif | ||
| 100 | 160 | ||
| 101 | // Read Backlight Info | 161 | // Read Backlight Info |
| 102 | # ifdef BACKLIGHT_ENABLE | 162 | # ifdef BACKLIGHT_ENABLE |
| @@ -118,6 +178,14 @@ void transport_slave(matrix_row_t matrix[]) { | |||
| 118 | # ifdef WPM_ENABLE | 178 | # ifdef WPM_ENABLE |
| 119 | set_current_wpm(i2c_buffer->current_wpm); | 179 | set_current_wpm(i2c_buffer->current_wpm); |
| 120 | # endif | 180 | # endif |
| 181 | |||
| 182 | # ifdef SPLIT_MODS_ENABLE | ||
| 183 | set_mods(i2c_buffer->real_mods); | ||
| 184 | set_weak_mods(i2c_buffer->weak_mods); | ||
| 185 | # ifndef NO_ACTION_ONESHOT | ||
| 186 | set_oneshot_mods(i2c_buffer->oneshot_mods); | ||
| 187 | # endif | ||
| 188 | # endif | ||
| 121 | } | 189 | } |
| 122 | 190 | ||
| 123 | void transport_master_init(void) { i2c_init(); } | 191 | void transport_master_init(void) { i2c_init(); } |
| @@ -139,11 +207,24 @@ typedef struct _Serial_s2m_buffer_t { | |||
| 139 | } Serial_s2m_buffer_t; | 207 | } Serial_s2m_buffer_t; |
| 140 | 208 | ||
| 141 | typedef struct _Serial_m2s_buffer_t { | 209 | typedef struct _Serial_m2s_buffer_t { |
| 210 | # ifdef SPLIT_MODS_ENABLE | ||
| 211 | uint8_t real_mods; | ||
| 212 | uint8_t weak_mods; | ||
| 213 | # ifndef NO_ACTION_ONESHOT | ||
| 214 | uint8_t oneshot_mods; | ||
| 215 | # endif | ||
| 216 | # endif | ||
| 217 | # ifndef DISABLE_SYNC_TIMER | ||
| 218 | uint32_t sync_timer; | ||
| 219 | # endif | ||
| 220 | # ifdef SPLIT_TRANSPORT_MIRROR | ||
| 221 | matrix_row_t mmatrix[ROWS_PER_HAND]; | ||
| 222 | # endif | ||
| 142 | # ifdef BACKLIGHT_ENABLE | 223 | # ifdef BACKLIGHT_ENABLE |
| 143 | uint8_t backlight_level; | 224 | uint8_t backlight_level; |
| 144 | # endif | 225 | # endif |
| 145 | # ifdef WPM_ENABLE | 226 | # ifdef WPM_ENABLE |
| 146 | uint8_t current_wpm; | 227 | uint8_t current_wpm; |
| 147 | # endif | 228 | # endif |
| 148 | } Serial_m2s_buffer_t; | 229 | } Serial_m2s_buffer_t; |
| 149 | 230 | ||
| @@ -221,7 +302,7 @@ void transport_rgblight_slave(void) { | |||
| 221 | # define transport_rgblight_slave() | 302 | # define transport_rgblight_slave() |
| 222 | # endif | 303 | # endif |
| 223 | 304 | ||
| 224 | bool transport_master(matrix_row_t matrix[]) { | 305 | bool transport_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) { |
| 225 | # ifndef SERIAL_USE_MULTI_TRANSACTION | 306 | # ifndef SERIAL_USE_MULTI_TRANSACTION |
| 226 | if (soft_serial_transaction() != TRANSACTION_END) { | 307 | if (soft_serial_transaction() != TRANSACTION_END) { |
| 227 | return false; | 308 | return false; |
| @@ -235,7 +316,10 @@ bool transport_master(matrix_row_t matrix[]) { | |||
| 235 | 316 | ||
| 236 | // TODO: if MATRIX_COLS > 8 change to unpack() | 317 | // TODO: if MATRIX_COLS > 8 change to unpack() |
| 237 | for (int i = 0; i < ROWS_PER_HAND; ++i) { | 318 | for (int i = 0; i < ROWS_PER_HAND; ++i) { |
| 238 | matrix[i] = serial_s2m_buffer.smatrix[i]; | 319 | slave_matrix[i] = serial_s2m_buffer.smatrix[i]; |
| 320 | # ifdef SPLIT_TRANSPORT_MIRROR | ||
| 321 | serial_m2s_buffer.mmatrix[i] = master_matrix[i]; | ||
| 322 | # endif | ||
| 239 | } | 323 | } |
| 240 | 324 | ||
| 241 | # ifdef BACKLIGHT_ENABLE | 325 | # ifdef BACKLIGHT_ENABLE |
| @@ -249,16 +333,34 @@ bool transport_master(matrix_row_t matrix[]) { | |||
| 249 | 333 | ||
| 250 | # ifdef WPM_ENABLE | 334 | # ifdef WPM_ENABLE |
| 251 | // Write wpm to slave | 335 | // Write wpm to slave |
| 252 | serial_m2s_buffer.current_wpm = get_current_wpm(); | 336 | serial_m2s_buffer.current_wpm = get_current_wpm(); |
| 337 | # endif | ||
| 338 | |||
| 339 | # ifdef SPLIT_MODS_ENABLE | ||
| 340 | serial_m2s_buffer.real_mods = get_mods(); | ||
| 341 | serial_m2s_buffer.weak_mods = get_weak_mods(); | ||
| 342 | # ifndef NO_ACTION_ONESHOT | ||
| 343 | serial_m2s_buffer.oneshot_mods = get_oneshot_mods(); | ||
| 344 | # endif | ||
| 345 | # endif | ||
| 346 | # ifndef DISABLE_SYNC_TIMER | ||
| 347 | serial_m2s_buffer.sync_timer = sync_timer_read32() + SYNC_TIMER_OFFSET; | ||
| 253 | # endif | 348 | # endif |
| 254 | return true; | 349 | return true; |
| 255 | } | 350 | } |
| 256 | 351 | ||
| 257 | void transport_slave(matrix_row_t matrix[]) { | 352 | void transport_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) { |
| 258 | transport_rgblight_slave(); | 353 | transport_rgblight_slave(); |
| 354 | # ifndef DISABLE_SYNC_TIMER | ||
| 355 | sync_timer_update(serial_m2s_buffer.sync_timer); | ||
| 356 | # endif | ||
| 357 | |||
| 259 | // TODO: if MATRIX_COLS > 8 change to pack() | 358 | // TODO: if MATRIX_COLS > 8 change to pack() |
| 260 | for (int i = 0; i < ROWS_PER_HAND; ++i) { | 359 | for (int i = 0; i < ROWS_PER_HAND; ++i) { |
| 261 | serial_s2m_buffer.smatrix[i] = matrix[i]; | 360 | serial_s2m_buffer.smatrix[i] = slave_matrix[i]; |
| 361 | # ifdef SPLIT_TRANSPORT_MIRROR | ||
| 362 | master_matrix[i] = serial_m2s_buffer.mmatrix[i]; | ||
| 363 | # endif | ||
| 262 | } | 364 | } |
| 263 | # ifdef BACKLIGHT_ENABLE | 365 | # ifdef BACKLIGHT_ENABLE |
| 264 | backlight_set(serial_m2s_buffer.backlight_level); | 366 | backlight_set(serial_m2s_buffer.backlight_level); |
| @@ -271,6 +373,14 @@ void transport_slave(matrix_row_t matrix[]) { | |||
| 271 | # ifdef WPM_ENABLE | 373 | # ifdef WPM_ENABLE |
| 272 | set_current_wpm(serial_m2s_buffer.current_wpm); | 374 | set_current_wpm(serial_m2s_buffer.current_wpm); |
| 273 | # endif | 375 | # endif |
| 376 | |||
| 377 | # ifdef SPLIT_MODS_ENABLE | ||
| 378 | set_mods(serial_m2s_buffer.real_mods); | ||
| 379 | set_weak_mods(serial_m2s_buffer.weak_mods); | ||
| 380 | # ifndef NO_ACTION_ONESHOT | ||
| 381 | set_oneshot_mods(serial_m2s_buffer.oneshot_mods); | ||
| 382 | # endif | ||
| 383 | # endif | ||
| 274 | } | 384 | } |
| 275 | 385 | ||
| 276 | #endif | 386 | #endif |
