aboutsummaryrefslogtreecommitdiff
path: root/quantum/split_common
diff options
context:
space:
mode:
Diffstat (limited to 'quantum/split_common')
-rw-r--r--quantum/split_common/i2c.c3
-rw-r--r--quantum/split_common/matrix.c160
-rw-r--r--quantum/split_common/matrix.h31
-rw-r--r--quantum/split_common/serial.c548
-rw-r--r--quantum/split_common/serial.h78
-rw-r--r--quantum/split_common/split_util.c51
6 files changed, 629 insertions, 242 deletions
diff --git a/quantum/split_common/i2c.c b/quantum/split_common/i2c.c
index b3d7fcc68..45e958b39 100644
--- a/quantum/split_common/i2c.c
+++ b/quantum/split_common/i2c.c
@@ -7,8 +7,6 @@
7#include "i2c.h" 7#include "i2c.h"
8#include "split_flags.h" 8#include "split_flags.h"
9 9
10#if defined(USE_I2C) || defined(EH)
11
12// Limits the amount of we wait for any one i2c transaction. 10// Limits the amount of we wait for any one i2c transaction.
13// Since were running SCL line 100kHz (=> 10μs/bit), and each transactions is 11// Since were running SCL line 100kHz (=> 10μs/bit), and each transactions is
14// 9 bits, a single transaction will take around 90μs to complete. 12// 9 bits, a single transaction will take around 90μs to complete.
@@ -184,4 +182,3 @@ ISR(TWI_vect) {
184 // Reset everything, so we are ready for the next TWI interrupt 182 // Reset everything, so we are ready for the next TWI interrupt
185 TWCR |= (1<<TWIE) | (1<<TWINT) | (ack<<TWEA) | (1<<TWEN); 183 TWCR |= (1<<TWIE) | (1<<TWINT) | (ack<<TWEA) | (1<<TWEN);
186} 184}
187#endif
diff --git a/quantum/split_common/matrix.c b/quantum/split_common/matrix.c
index 4af90209f..2c37053f8 100644
--- a/quantum/split_common/matrix.c
+++ b/quantum/split_common/matrix.c
@@ -20,21 +20,15 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */ 20 */
21#include <stdint.h> 21#include <stdint.h>
22#include <stdbool.h> 22#include <stdbool.h>
23#include <avr/io.h>
24#include "wait.h" 23#include "wait.h"
25#include "print.h"
26#include "debug.h"
27#include "util.h" 24#include "util.h"
28#include "matrix.h" 25#include "matrix.h"
29#include "split_util.h" 26#include "split_util.h"
30#include "pro_micro.h"
31#include "config.h" 27#include "config.h"
32#include "timer.h" 28#include "timer.h"
33#include "split_flags.h" 29#include "split_flags.h"
30#include "quantum.h"
34 31
35#ifdef RGBLIGHT_ENABLE
36# include "rgblight.h"
37#endif
38#ifdef BACKLIGHT_ENABLE 32#ifdef BACKLIGHT_ENABLE
39# include "backlight.h" 33# include "backlight.h"
40 extern backlight_config_t backlight_config; 34 extern backlight_config_t backlight_config;
@@ -55,6 +49,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
55 static bool debouncing = false; 49 static bool debouncing = false;
56#endif 50#endif
57 51
52#if defined(USE_I2C) || defined(EH)
53
58#if (MATRIX_COLS <= 8) 54#if (MATRIX_COLS <= 8)
59# define print_matrix_header() print("\nr/c 01234567\n") 55# define print_matrix_header() print("\nr/c 01234567\n")
60# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) 56# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
@@ -63,6 +59,27 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
63#else 59#else
64# error "Currently only supports 8 COLS" 60# error "Currently only supports 8 COLS"
65#endif 61#endif
62
63#else // USE_SERIAL
64
65#if (MATRIX_COLS <= 8)
66# define print_matrix_header() print("\nr/c 01234567\n")
67# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
68# define matrix_bitpop(i) bitpop(matrix[i])
69# define ROW_SHIFTER ((uint8_t)1)
70#elif (MATRIX_COLS <= 16)
71# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
72# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
73# define matrix_bitpop(i) bitpop16(matrix[i])
74# define ROW_SHIFTER ((uint16_t)1)
75#elif (MATRIX_COLS <= 32)
76# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
77# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
78# define matrix_bitpop(i) bitpop32(matrix[i])
79# define ROW_SHIFTER ((uint32_t)1)
80#endif
81
82#endif
66static matrix_row_t matrix_debouncing[MATRIX_ROWS]; 83static matrix_row_t matrix_debouncing[MATRIX_ROWS];
67 84
68#define ERROR_DISCONNECT_COUNT 5 85#define ERROR_DISCONNECT_COUNT 5
@@ -71,8 +88,8 @@ static matrix_row_t matrix_debouncing[MATRIX_ROWS];
71 88
72static uint8_t error_count = 0; 89static uint8_t error_count = 0;
73 90
74static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; 91static pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
75static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; 92static pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
76 93
77/* matrix state(1:on, 0:off) */ 94/* matrix state(1:on, 0:off) */
78static matrix_row_t matrix[MATRIX_ROWS]; 95static matrix_row_t matrix[MATRIX_ROWS];
@@ -128,15 +145,24 @@ uint8_t matrix_cols(void)
128 145
129void matrix_init(void) 146void matrix_init(void)
130{ 147{
131#ifdef DISABLE_JTAG
132 // JTAG disable for PORT F. write JTD bit twice within four cycles.
133 MCUCR |= (1<<JTD);
134 MCUCR |= (1<<JTD);
135#endif
136
137 debug_enable = true; 148 debug_enable = true;
138 debug_matrix = true; 149 debug_matrix = true;
139 debug_mouse = true; 150 debug_mouse = true;
151
152 // Set pinout for right half if pinout for that half is defined
153 if (!isLeftHand) {
154#ifdef MATRIX_ROW_PINS_RIGHT
155 const uint8_t row_pins_right[MATRIX_ROWS] = MATRIX_ROW_PINS_RIGHT;
156 for (uint8_t i = 0; i < MATRIX_ROWS; i++)
157 row_pins[i] = row_pins_right[i];
158#endif
159#ifdef MATRIX_COL_PINS_RIGHT
160 const uint8_t col_pins_right[MATRIX_COLS] = MATRIX_COL_PINS_RIGHT;
161 for (uint8_t i = 0; i < MATRIX_COLS; i++)
162 col_pins[i] = col_pins_right[i];
163#endif
164 }
165
140 // initialize row and col 166 // initialize row and col
141#if (DIODE_DIRECTION == COL2ROW) 167#if (DIODE_DIRECTION == COL2ROW)
142 unselect_rows(); 168 unselect_rows();
@@ -277,24 +303,48 @@ i2c_error: // the cable is disconnceted, or something else went wrong
277 303
278#else // USE_SERIAL 304#else // USE_SERIAL
279 305
306
307typedef struct _Serial_s2m_buffer_t {
308 // TODO: if MATRIX_COLS > 8 change to uint8_t packed_matrix[] for pack/unpack
309 matrix_row_t smatrix[ROWS_PER_HAND];
310} Serial_s2m_buffer_t;
311
312volatile Serial_s2m_buffer_t serial_s2m_buffer = {};
313volatile Serial_m2s_buffer_t serial_m2s_buffer = {};
314uint8_t volatile status0 = 0;
315
316SSTD_t transactions[] = {
317 { (uint8_t *)&status0,
318 sizeof(serial_m2s_buffer), (uint8_t *)&serial_m2s_buffer,
319 sizeof(serial_s2m_buffer), (uint8_t *)&serial_s2m_buffer
320 }
321};
322
323void serial_master_init(void)
324{ soft_serial_initiator_init(transactions, TID_LIMIT(transactions)); }
325
326void serial_slave_init(void)
327{ soft_serial_target_init(transactions, TID_LIMIT(transactions)); }
328
280int serial_transaction(void) { 329int serial_transaction(void) {
281 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0; 330 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
282 331
283 if (serial_update_buffers()) { 332 if (soft_serial_transaction()) {
284 return 1; 333 return 1;
285 } 334 }
286 335
336 // TODO: if MATRIX_COLS > 8 change to unpack()
287 for (int i = 0; i < ROWS_PER_HAND; ++i) { 337 for (int i = 0; i < ROWS_PER_HAND; ++i) {
288 matrix[slaveOffset+i] = serial_slave_buffer[i]; 338 matrix[slaveOffset+i] = serial_s2m_buffer.smatrix[i];
289 } 339 }
290 340
291 #ifdef RGBLIGHT_ENABLE 341 #if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
292 // Code to send RGB over serial goes here (not implemented yet) 342 // Code to send RGB over serial goes here (not implemented yet)
293 #endif 343 #endif
294 344
295 #ifdef BACKLIGHT_ENABLE 345 #ifdef BACKLIGHT_ENABLE
296 // Write backlight level for slave to read 346 // Write backlight level for slave to read
297 serial_master_buffer[SERIAL_BACKLIT_START] = backlight_config.enable ? backlight_config.level : 0; 347 serial_m2s_buffer.backlight_level = backlight_config.enable ? backlight_config.level : 0;
298 #endif 348 #endif
299 349
300 return 0; 350 return 0;
@@ -337,27 +387,10 @@ void matrix_slave_scan(void) {
337 i2c_slave_buffer[I2C_KEYMAP_START+i] = matrix[offset+i]; 387 i2c_slave_buffer[I2C_KEYMAP_START+i] = matrix[offset+i];
338 } 388 }
339#else // USE_SERIAL 389#else // USE_SERIAL
390 // TODO: if MATRIX_COLS > 8 change to pack()
340 for (int i = 0; i < ROWS_PER_HAND; ++i) { 391 for (int i = 0; i < ROWS_PER_HAND; ++i) {
341 serial_slave_buffer[i] = matrix[offset+i]; 392 serial_s2m_buffer.smatrix[i] = matrix[offset+i];
342 }
343#endif
344#ifdef USE_I2C
345#ifdef BACKLIGHT_ENABLE
346 // Read backlight level sent from master and update level on slave
347 backlight_set(i2c_slave_buffer[0]);
348#endif
349 for (int i = 0; i < ROWS_PER_HAND; ++i) {
350 i2c_slave_buffer[i+1] = matrix[offset+i];
351 }
352#else // USE_SERIAL
353 for (int i = 0; i < ROWS_PER_HAND; ++i) {
354 serial_slave_buffer[i] = matrix[offset+i];
355 } 393 }
356
357#ifdef BACKLIGHT_ENABLE
358 // Read backlight level sent from master and update level on slave
359 backlight_set(serial_master_buffer[SERIAL_BACKLIT_START]);
360#endif
361#endif 394#endif
362 matrix_slave_scan_user(); 395 matrix_slave_scan_user();
363} 396}
@@ -404,9 +437,7 @@ uint8_t matrix_key_count(void)
404static void init_cols(void) 437static void init_cols(void)
405{ 438{
406 for(uint8_t x = 0; x < MATRIX_COLS; x++) { 439 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
407 uint8_t pin = col_pins[x]; 440 setPinInputHigh(col_pins[x]);
408 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
409 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
410 } 441 }
411} 442}
412 443
@@ -424,13 +455,8 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
424 455
425 // For each col... 456 // For each col...
426 for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { 457 for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
427
428 // Select the col pin to read (active low)
429 uint8_t pin = col_pins[col_index];
430 uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
431
432 // Populate the matrix row with the state of the col pin 458 // Populate the matrix row with the state of the col pin
433 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index); 459 current_matrix[current_row] |= readPin(col_pins[col_index]) ? 0 : (ROW_SHIFTER << col_index);
434 } 460 }
435 461
436 // Unselect row 462 // Unselect row
@@ -441,24 +467,19 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
441 467
442static void select_row(uint8_t row) 468static void select_row(uint8_t row)
443{ 469{
444 uint8_t pin = row_pins[row]; 470 writePinLow(row_pins[row]);
445 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT 471 setPinOutput(row_pins[row]);
446 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
447} 472}
448 473
449static void unselect_row(uint8_t row) 474static void unselect_row(uint8_t row)
450{ 475{
451 uint8_t pin = row_pins[row]; 476 setPinInputHigh(row_pins[row]);
452 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
453 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
454} 477}
455 478
456static void unselect_rows(void) 479static void unselect_rows(void)
457{ 480{
458 for(uint8_t x = 0; x < ROWS_PER_HAND; x++) { 481 for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
459 uint8_t pin = row_pins[x]; 482 setPinInputHigh(row_pins[x]);
460 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
461 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
462 } 483 }
463} 484}
464 485
@@ -467,9 +488,7 @@ static void unselect_rows(void)
467static void init_rows(void) 488static void init_rows(void)
468{ 489{
469 for(uint8_t x = 0; x < ROWS_PER_HAND; x++) { 490 for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
470 uint8_t pin = row_pins[x]; 491 setPinInputHigh(row_pins[x]);
471 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
472 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
473 } 492 }
474} 493}
475 494
@@ -489,15 +508,15 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
489 matrix_row_t last_row_value = current_matrix[row_index]; 508 matrix_row_t last_row_value = current_matrix[row_index];
490 509
491 // Check row pin state 510 // Check row pin state
492 if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0) 511 if (readPin(row_pins[row_index]))
493 { 512 {
494 // Pin LO, set col bit 513 // Pin HI, clear col bit
495 current_matrix[row_index] |= (ROW_SHIFTER << current_col); 514 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
496 } 515 }
497 else 516 else
498 { 517 {
499 // Pin HI, clear col bit 518 // Pin LO, set col bit
500 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col); 519 current_matrix[row_index] |= (ROW_SHIFTER << current_col);
501 } 520 }
502 521
503 // Determine if the matrix changed state 522 // Determine if the matrix changed state
@@ -515,24 +534,19 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
515 534
516static void select_col(uint8_t col) 535static void select_col(uint8_t col)
517{ 536{
518 uint8_t pin = col_pins[col]; 537 writePinLow(col_pins[col]);
519 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT 538 setPinOutput(col_pins[col]);
520 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
521} 539}
522 540
523static void unselect_col(uint8_t col) 541static void unselect_col(uint8_t col)
524{ 542{
525 uint8_t pin = col_pins[col]; 543 setPinInputHigh(col_pins[col]);
526 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
527 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
528} 544}
529 545
530static void unselect_cols(void) 546static void unselect_cols(void)
531{ 547{
532 for(uint8_t x = 0; x < MATRIX_COLS; x++) { 548 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
533 uint8_t pin = col_pins[x]; 549 setPinInputHigh(col_pins[x]);
534 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
535 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
536 } 550 }
537} 551}
538 552
diff --git a/quantum/split_common/matrix.h b/quantum/split_common/matrix.h
new file mode 100644
index 000000000..b5cb45bae
--- /dev/null
+++ b/quantum/split_common/matrix.h
@@ -0,0 +1,31 @@
1#ifndef SPLIT_COMMON_MATRIX_H
2#define SPLIT_COMMON_MATRIX_H
3
4#include <common/matrix.h>
5
6#ifdef RGBLIGHT_ENABLE
7# include "rgblight.h"
8#endif
9
10typedef struct _Serial_m2s_buffer_t {
11#ifdef BACKLIGHT_ENABLE
12 uint8_t backlight_level;
13#endif
14#if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
15 rgblight_config_t rgblight_config; //not yet use
16 //
17 // When MCUs on both sides drive their respective RGB LED chains,
18 // it is necessary to synchronize, so it is necessary to communicate RGB information.
19 // In that case, define the RGBLIGHT_SPLIT macro.
20 //
21 // Otherwise, if the master side MCU drives both sides RGB LED chains,
22 // there is no need to communicate.
23#endif
24} Serial_m2s_buffer_t;
25
26extern volatile Serial_m2s_buffer_t serial_m2s_buffer;
27
28void serial_master_init(void);
29void serial_slave_init(void);
30
31#endif
diff --git a/quantum/split_common/serial.c b/quantum/split_common/serial.c
index 74bcbb6bf..1315377a3 100644
--- a/quantum/split_common/serial.c
+++ b/quantum/split_common/serial.c
@@ -1,5 +1,10 @@
1/* 1/*
2 * WARNING: be careful changing this code, it is very timing dependent 2 * WARNING: be careful changing this code, it is very timing dependent
3 *
4 * 2018-10-28 checked
5 * avr-gcc 4.9.2
6 * avr-gcc 5.4.0
7 * avr-gcc 7.3.0
3 */ 8 */
4 9
5#ifndef F_CPU 10#ifndef F_CPU
@@ -9,220 +14,533 @@
9#include <avr/io.h> 14#include <avr/io.h>
10#include <avr/interrupt.h> 15#include <avr/interrupt.h>
11#include <util/delay.h> 16#include <util/delay.h>
17#include <stddef.h>
12#include <stdbool.h> 18#include <stdbool.h>
13#include "serial.h" 19#include "serial.h"
20//#include <pro_micro.h>
21
22#ifdef SOFT_SERIAL_PIN
23
24#ifdef __AVR_ATmega32U4__
25 // if using ATmega32U4 I2C, can not use PD0 and PD1 in soft serial.
26 #ifdef USE_AVR_I2C
27 #if SOFT_SERIAL_PIN == D0 || SOFT_SERIAL_PIN == D1
28 #error Using ATmega32U4 I2C, so can not use PD0, PD1
29 #endif
30 #endif
31
32 #if SOFT_SERIAL_PIN >= D0 && SOFT_SERIAL_PIN <= D3
33 #define SERIAL_PIN_DDR DDRD
34 #define SERIAL_PIN_PORT PORTD
35 #define SERIAL_PIN_INPUT PIND
36 #if SOFT_SERIAL_PIN == D0
37 #define SERIAL_PIN_MASK _BV(PD0)
38 #define EIMSK_BIT _BV(INT0)
39 #define EICRx_BIT (~(_BV(ISC00) | _BV(ISC01)))
40 #define SERIAL_PIN_INTERRUPT INT0_vect
41 #elif SOFT_SERIAL_PIN == D1
42 #define SERIAL_PIN_MASK _BV(PD1)
43 #define EIMSK_BIT _BV(INT1)
44 #define EICRx_BIT (~(_BV(ISC10) | _BV(ISC11)))
45 #define SERIAL_PIN_INTERRUPT INT1_vect
46 #elif SOFT_SERIAL_PIN == D2
47 #define SERIAL_PIN_MASK _BV(PD2)
48 #define EIMSK_BIT _BV(INT2)
49 #define EICRx_BIT (~(_BV(ISC20) | _BV(ISC21)))
50 #define SERIAL_PIN_INTERRUPT INT2_vect
51 #elif SOFT_SERIAL_PIN == D3
52 #define SERIAL_PIN_MASK _BV(PD3)
53 #define EIMSK_BIT _BV(INT3)
54 #define EICRx_BIT (~(_BV(ISC30) | _BV(ISC31)))
55 #define SERIAL_PIN_INTERRUPT INT3_vect
56 #endif
57 #elif SOFT_SERIAL_PIN == E6
58 #define SERIAL_PIN_DDR DDRE
59 #define SERIAL_PIN_PORT PORTE
60 #define SERIAL_PIN_INPUT PINE
61 #define SERIAL_PIN_MASK _BV(PE6)
62 #define EIMSK_BIT _BV(INT6)
63 #define EICRx_BIT (~(_BV(ISC60) | _BV(ISC61)))
64 #define SERIAL_PIN_INTERRUPT INT6_vect
65 #else
66 #error invalid SOFT_SERIAL_PIN value
67 #endif
68
69#else
70 #error serial.c now support ATmega32U4 only
71#endif
14 72
15#ifndef USE_I2C 73#define ALWAYS_INLINE __attribute__((always_inline))
74#define NO_INLINE __attribute__((noinline))
75#define _delay_sub_us(x) __builtin_avr_delay_cycles(x)
76
77// parity check
78#define ODD_PARITY 1
79#define EVEN_PARITY 0
80#define PARITY EVEN_PARITY
81
82#ifdef SERIAL_DELAY
83 // custom setup in config.h
84 // #define TID_SEND_ADJUST 2
85 // #define SERIAL_DELAY 6 // micro sec
86 // #define READ_WRITE_START_ADJUST 30 // cycles
87 // #define READ_WRITE_WIDTH_ADJUST 8 // cycles
88#else
89// ============ Standard setups ============
90
91#ifndef SELECT_SOFT_SERIAL_SPEED
92#define SELECT_SOFT_SERIAL_SPEED 1
93// 0: about 189kbps (Experimental only)
94// 1: about 137kbps (default)
95// 2: about 75kbps
96// 3: about 39kbps
97// 4: about 26kbps
98// 5: about 20kbps
99#endif
16 100
17// Serial pulse period in microseconds. Its probably a bad idea to lower this 101#if __GNUC__ < 6
18// value. 102 #define TID_SEND_ADJUST 14
19#define SERIAL_DELAY 24 103#else
104 #define TID_SEND_ADJUST 2
105#endif
20 106
21uint8_t volatile serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH] = {0}; 107#if SELECT_SOFT_SERIAL_SPEED == 0
22uint8_t volatile serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH] = {0}; 108 // Very High speed
109 #define SERIAL_DELAY 4 // micro sec
110 #if __GNUC__ < 6
111 #define READ_WRITE_START_ADJUST 33 // cycles
112 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
113 #else
114 #define READ_WRITE_START_ADJUST 34 // cycles
115 #define READ_WRITE_WIDTH_ADJUST 7 // cycles
116 #endif
117#elif SELECT_SOFT_SERIAL_SPEED == 1
118 // High speed
119 #define SERIAL_DELAY 6 // micro sec
120 #if __GNUC__ < 6
121 #define READ_WRITE_START_ADJUST 30 // cycles
122 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
123 #else
124 #define READ_WRITE_START_ADJUST 33 // cycles
125 #define READ_WRITE_WIDTH_ADJUST 7 // cycles
126 #endif
127#elif SELECT_SOFT_SERIAL_SPEED == 2
128 // Middle speed
129 #define SERIAL_DELAY 12 // micro sec
130 #define READ_WRITE_START_ADJUST 30 // cycles
131 #if __GNUC__ < 6
132 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
133 #else
134 #define READ_WRITE_WIDTH_ADJUST 7 // cycles
135 #endif
136#elif SELECT_SOFT_SERIAL_SPEED == 3
137 // Low speed
138 #define SERIAL_DELAY 24 // micro sec
139 #define READ_WRITE_START_ADJUST 30 // cycles
140 #if __GNUC__ < 6
141 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
142 #else
143 #define READ_WRITE_WIDTH_ADJUST 7 // cycles
144 #endif
145#elif SELECT_SOFT_SERIAL_SPEED == 4
146 // Very Low speed
147 #define SERIAL_DELAY 36 // micro sec
148 #define READ_WRITE_START_ADJUST 30 // cycles
149 #if __GNUC__ < 6
150 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
151 #else
152 #define READ_WRITE_WIDTH_ADJUST 7 // cycles
153 #endif
154#elif SELECT_SOFT_SERIAL_SPEED == 5
155 // Ultra Low speed
156 #define SERIAL_DELAY 48 // micro sec
157 #define READ_WRITE_START_ADJUST 30 // cycles
158 #if __GNUC__ < 6
159 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
160 #else
161 #define READ_WRITE_WIDTH_ADJUST 7 // cycles
162 #endif
163#else
164#error invalid SELECT_SOFT_SERIAL_SPEED value
165#endif /* SELECT_SOFT_SERIAL_SPEED */
166#endif /* SERIAL_DELAY */
167
168#define SERIAL_DELAY_HALF1 (SERIAL_DELAY/2)
169#define SERIAL_DELAY_HALF2 (SERIAL_DELAY - SERIAL_DELAY/2)
170
171#define SLAVE_INT_WIDTH_US 1
172#ifndef SERIAL_USE_MULTI_TRANSACTION
173 #define SLAVE_INT_RESPONSE_TIME SERIAL_DELAY
174#else
175 #define SLAVE_INT_ACK_WIDTH_UNIT 2
176 #define SLAVE_INT_ACK_WIDTH 4
177#endif
23 178
24#define SLAVE_DATA_CORRUPT (1<<0) 179static SSTD_t *Transaction_table = NULL;
25volatile uint8_t status = 0; 180static uint8_t Transaction_table_size = 0;
26 181
182inline static void serial_delay(void) ALWAYS_INLINE;
27inline static 183inline static
28void serial_delay(void) { 184void serial_delay(void) {
29 _delay_us(SERIAL_DELAY); 185 _delay_us(SERIAL_DELAY);
30} 186}
31 187
188inline static void serial_delay_half1(void) ALWAYS_INLINE;
189inline static
190void serial_delay_half1(void) {
191 _delay_us(SERIAL_DELAY_HALF1);
192}
193
194inline static void serial_delay_half2(void) ALWAYS_INLINE;
195inline static
196void serial_delay_half2(void) {
197 _delay_us(SERIAL_DELAY_HALF2);
198}
199
200inline static void serial_output(void) ALWAYS_INLINE;
32inline static 201inline static
33void serial_output(void) { 202void serial_output(void) {
34 SERIAL_PIN_DDR |= SERIAL_PIN_MASK; 203 SERIAL_PIN_DDR |= SERIAL_PIN_MASK;
35} 204}
36 205
37// make the serial pin an input with pull-up resistor 206// make the serial pin an input with pull-up resistor
207inline static void serial_input_with_pullup(void) ALWAYS_INLINE;
38inline static 208inline static
39void serial_input(void) { 209void serial_input_with_pullup(void) {
40 SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK; 210 SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK;
41 SERIAL_PIN_PORT |= SERIAL_PIN_MASK; 211 SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
42} 212}
43 213
214inline static uint8_t serial_read_pin(void) ALWAYS_INLINE;
44inline static 215inline static
45uint8_t serial_read_pin(void) { 216uint8_t serial_read_pin(void) {
46 return !!(SERIAL_PIN_INPUT & SERIAL_PIN_MASK); 217 return !!(SERIAL_PIN_INPUT & SERIAL_PIN_MASK);
47} 218}
48 219
220inline static void serial_low(void) ALWAYS_INLINE;
49inline static 221inline static
50void serial_low(void) { 222void serial_low(void) {
51 SERIAL_PIN_PORT &= ~SERIAL_PIN_MASK; 223 SERIAL_PIN_PORT &= ~SERIAL_PIN_MASK;
52} 224}
53 225
226inline static void serial_high(void) ALWAYS_INLINE;
54inline static 227inline static
55void serial_high(void) { 228void serial_high(void) {
56 SERIAL_PIN_PORT |= SERIAL_PIN_MASK; 229 SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
57} 230}
58 231
59void serial_master_init(void) { 232void soft_serial_initiator_init(SSTD_t *sstd_table, int sstd_table_size)
60 serial_output(); 233{
61 serial_high(); 234 Transaction_table = sstd_table;
235 Transaction_table_size = (uint8_t)sstd_table_size;
236 serial_output();
237 serial_high();
62} 238}
63 239
64void serial_slave_init(void) { 240void soft_serial_target_init(SSTD_t *sstd_table, int sstd_table_size)
65 serial_input(); 241{
66 242 Transaction_table = sstd_table;
67 // Enable INT0 243 Transaction_table_size = (uint8_t)sstd_table_size;
68 EIMSK |= _BV(INT0); 244 serial_input_with_pullup();
69 // Trigger on falling edge of INT0 245
70 EICRA &= ~(_BV(ISC00) | _BV(ISC01)); 246 // Enable INT0-INT3,INT6
247 EIMSK |= EIMSK_BIT;
248#if SERIAL_PIN_MASK == _BV(PE6)
249 // Trigger on falling edge of INT6
250 EICRB &= EICRx_BIT;
251#else
252 // Trigger on falling edge of INT0-INT3
253 EICRA &= EICRx_BIT;
254#endif
71} 255}
72 256
73// Used by the master to synchronize timing with the slave. 257// Used by the sender to synchronize timing with the reciver.
258static void sync_recv(void) NO_INLINE;
74static 259static
75void sync_recv(void) { 260void sync_recv(void) {
76 serial_input(); 261 for (uint8_t i = 0; i < SERIAL_DELAY*5 && serial_read_pin(); i++ ) {
77 // This shouldn't hang if the slave disconnects because the 262 }
78 // serial line will float to high if the slave does disconnect. 263 // This shouldn't hang if the target disconnects because the
264 // serial line will float to high if the target does disconnect.
79 while (!serial_read_pin()); 265 while (!serial_read_pin());
80 serial_delay();
81} 266}
82 267
83// Used by the slave to send a synchronization signal to the master. 268// Used by the reciver to send a synchronization signal to the sender.
269static void sync_send(void) NO_INLINE;
84static 270static
85void sync_send(void) { 271void sync_send(void) {
86 serial_output();
87
88 serial_low(); 272 serial_low();
89 serial_delay(); 273 serial_delay();
90
91 serial_high(); 274 serial_high();
92} 275}
93 276
94// Reads a byte from the serial line 277// Reads a byte from the serial line
95static 278static uint8_t serial_read_chunk(uint8_t *pterrcount, uint8_t bit) NO_INLINE;
96uint8_t serial_read_byte(void) { 279static uint8_t serial_read_chunk(uint8_t *pterrcount, uint8_t bit) {
97 uint8_t byte = 0; 280 uint8_t byte, i, p, pb;
98 serial_input(); 281
99 for ( uint8_t i = 0; i < 8; ++i) { 282 _delay_sub_us(READ_WRITE_START_ADJUST);
100 byte = (byte << 1) | serial_read_pin(); 283 for( i = 0, byte = 0, p = PARITY; i < bit; i++ ) {
101 serial_delay(); 284 serial_delay_half1(); // read the middle of pulses
102 _delay_us(1); 285 if( serial_read_pin() ) {
286 byte = (byte << 1) | 1; p ^= 1;
287 } else {
288 byte = (byte << 1) | 0; p ^= 0;
289 }
290 _delay_sub_us(READ_WRITE_WIDTH_ADJUST);
291 serial_delay_half2();
103 } 292 }
293 /* recive parity bit */
294 serial_delay_half1(); // read the middle of pulses
295 pb = serial_read_pin();
296 _delay_sub_us(READ_WRITE_WIDTH_ADJUST);
297 serial_delay_half2();
298
299 *pterrcount += (p != pb)? 1 : 0;
104 300
105 return byte; 301 return byte;
106} 302}
107 303
108// Sends a byte with MSB ordering 304// Sends a byte with MSB ordering
109static 305void serial_write_chunk(uint8_t data, uint8_t bit) NO_INLINE;
110void serial_write_byte(uint8_t data) { 306void serial_write_chunk(uint8_t data, uint8_t bit) {
111 uint8_t b = 8; 307 uint8_t b, p;
112 serial_output(); 308 for( p = PARITY, b = 1<<(bit-1); b ; b >>= 1) {
113 while( b-- ) { 309 if(data & b) {
114 if(data & (1 << b)) { 310 serial_high(); p ^= 1;
115 serial_high(); 311 } else {
116 } else { 312 serial_low(); p ^= 0;
117 serial_low(); 313 }
314 serial_delay();
118 } 315 }
316 /* send parity bit */
317 if(p & 1) { serial_high(); }
318 else { serial_low(); }
119 serial_delay(); 319 serial_delay();
120 }
121}
122 320
123// interrupt handle to be used by the slave device 321 serial_low(); // sync_send() / senc_recv() need raise edge
124ISR(SERIAL_PIN_INTERRUPT) { 322}
125 sync_send();
126 323
127 uint8_t checksum = 0; 324static void serial_send_packet(uint8_t *buffer, uint8_t size) NO_INLINE;
128 for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) { 325static
129 serial_write_byte(serial_slave_buffer[i]); 326void serial_send_packet(uint8_t *buffer, uint8_t size) {
327 for (uint8_t i = 0; i < size; ++i) {
328 uint8_t data;
329 data = buffer[i];
130 sync_send(); 330 sync_send();
131 checksum += serial_slave_buffer[i]; 331 serial_write_chunk(data,8);
132 } 332 }
133 serial_write_byte(checksum); 333}
134 sync_send();
135 334
136 // wait for the sync to finish sending 335static uint8_t serial_recive_packet(uint8_t *buffer, uint8_t size) NO_INLINE;
137 serial_delay(); 336static
337uint8_t serial_recive_packet(uint8_t *buffer, uint8_t size) {
338 uint8_t pecount = 0;
339 for (uint8_t i = 0; i < size; ++i) {
340 uint8_t data;
341 sync_recv();
342 data = serial_read_chunk(&pecount, 8);
343 buffer[i] = data;
344 }
345 return pecount == 0;
346}
138 347
139 // read the middle of pulses 348inline static
140 _delay_us(SERIAL_DELAY/2); 349void change_sender2reciver(void) {
350 sync_send(); //0
351 serial_delay_half1(); //1
352 serial_low(); //2
353 serial_input_with_pullup(); //2
354 serial_delay_half1(); //3
355}
141 356
142 uint8_t checksum_computed = 0; 357inline static
143 for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) { 358void change_reciver2sender(void) {
144 serial_master_buffer[i] = serial_read_byte(); 359 sync_recv(); //0
145 sync_send(); 360 serial_delay(); //1
146 checksum_computed += serial_master_buffer[i]; 361 serial_low(); //3
362 serial_output(); //3
363 serial_delay_half1(); //4
364}
365
366static inline uint8_t nibble_bits_count(uint8_t bits)
367{
368 bits = (bits & 0x5) + (bits >> 1 & 0x5);
369 bits = (bits & 0x3) + (bits >> 2 & 0x3);
370 return bits;
371}
372
373// interrupt handle to be used by the target device
374ISR(SERIAL_PIN_INTERRUPT) {
375
376#ifndef SERIAL_USE_MULTI_TRANSACTION
377 serial_low();
378 serial_output();
379 SSTD_t *trans = Transaction_table;
380#else
381 // recive transaction table index
382 uint8_t tid, bits;
383 uint8_t pecount = 0;
384 sync_recv();
385 bits = serial_read_chunk(&pecount,7);
386 tid = bits>>3;
387 bits = (bits&7) != nibble_bits_count(tid);
388 if( bits || pecount> 0 || tid > Transaction_table_size ) {
389 return;
147 } 390 }
148 uint8_t checksum_received = serial_read_byte(); 391 serial_delay_half1();
149 sync_send();
150 392
151 serial_input(); // end transaction 393 serial_high(); // response step1 low->high
394 serial_output();
395 _delay_sub_us(SLAVE_INT_ACK_WIDTH_UNIT*SLAVE_INT_ACK_WIDTH);
396 SSTD_t *trans = &Transaction_table[tid];
397 serial_low(); // response step2 ack high->low
398#endif
152 399
153 if ( checksum_computed != checksum_received ) { 400 // target send phase
154 status |= SLAVE_DATA_CORRUPT; 401 if( trans->target2initiator_buffer_size > 0 )
402 serial_send_packet((uint8_t *)trans->target2initiator_buffer,
403 trans->target2initiator_buffer_size);
404 // target switch to input
405 change_sender2reciver();
406
407 // target recive phase
408 if( trans->initiator2target_buffer_size > 0 ) {
409 if (serial_recive_packet((uint8_t *)trans->initiator2target_buffer,
410 trans->initiator2target_buffer_size) ) {
411 *trans->status = TRANSACTION_ACCEPTED;
412 } else {
413 *trans->status = TRANSACTION_DATA_ERROR;
414 }
155 } else { 415 } else {
156 status &= ~SLAVE_DATA_CORRUPT; 416 *trans->status = TRANSACTION_ACCEPTED;
157 } 417 }
158}
159 418
160inline 419 sync_recv(); //weit initiator output to high
161bool serial_slave_DATA_CORRUPT(void) {
162 return status & SLAVE_DATA_CORRUPT;
163} 420}
164 421
165// Copies the serial_slave_buffer to the master and sends the 422/////////
166// serial_master_buffer to the slave. 423// start transaction by initiator
424//
425// int soft_serial_transaction(int sstd_index)
167// 426//
168// Returns: 427// Returns:
169// 0 => no error 428// TRANSACTION_END
170// 1 => slave did not respond 429// TRANSACTION_NO_RESPONSE
171int serial_update_buffers(void) { 430// TRANSACTION_DATA_ERROR
172 // this code is very time dependent, so we need to disable interrupts 431// this code is very time dependent, so we need to disable interrupts
432#ifndef SERIAL_USE_MULTI_TRANSACTION
433int soft_serial_transaction(void) {
434 SSTD_t *trans = Transaction_table;
435#else
436int soft_serial_transaction(int sstd_index) {
437 if( sstd_index > Transaction_table_size )
438 return TRANSACTION_TYPE_ERROR;
439 SSTD_t *trans = &Transaction_table[sstd_index];
440#endif
173 cli(); 441 cli();
174 442
175 // signal to the slave that we want to start a transaction 443 // signal to the target that we want to start a transaction
176 serial_output(); 444 serial_output();
177 serial_low(); 445 serial_low();
178 _delay_us(1); 446 _delay_us(SLAVE_INT_WIDTH_US);
179 447
180 // wait for the slaves response 448#ifndef SERIAL_USE_MULTI_TRANSACTION
181 serial_input(); 449 // wait for the target response
182 serial_high(); 450 serial_input_with_pullup();
183 _delay_us(SERIAL_DELAY); 451 _delay_us(SLAVE_INT_RESPONSE_TIME);
184 452
185 // check if the slave is present 453 // check if the target is present
186 if (serial_read_pin()) { 454 if (serial_read_pin()) {
187 // slave failed to pull the line low, assume not present 455 // target failed to pull the line low, assume not present
456 serial_output();
457 serial_high();
458 *trans->status = TRANSACTION_NO_RESPONSE;
188 sei(); 459 sei();
189 return 1; 460 return TRANSACTION_NO_RESPONSE;
190 } 461 }
191 462
192 // if the slave is present syncronize with it 463#else
193 sync_recv(); 464 // send transaction table index
194 465 int tid = (sstd_index<<3) | (7 & nibble_bits_count(sstd_index));
195 uint8_t checksum_computed = 0; 466 sync_send();
196 // receive data from the slave 467 _delay_sub_us(TID_SEND_ADJUST);
197 for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) { 468 serial_write_chunk(tid, 7);
198 serial_slave_buffer[i] = serial_read_byte(); 469 serial_delay_half1();
199 sync_recv(); 470
200 checksum_computed += serial_slave_buffer[i]; 471 // wait for the target response (step1 low->high)
472 serial_input_with_pullup();
473 while( !serial_read_pin() ) {
474 _delay_sub_us(2);
201 } 475 }
202 uint8_t checksum_received = serial_read_byte();
203 sync_recv();
204 476
205 if (checksum_computed != checksum_received) { 477 // check if the target is present (step2 high->low)
206 sei(); 478 for( int i = 0; serial_read_pin(); i++ ) {
207 return 1; 479 if (i > SLAVE_INT_ACK_WIDTH + 1) {
480 // slave failed to pull the line low, assume not present
481 serial_output();
482 serial_high();
483 *trans->status = TRANSACTION_NO_RESPONSE;
484 sei();
485 return TRANSACTION_NO_RESPONSE;
486 }
487 _delay_sub_us(SLAVE_INT_ACK_WIDTH_UNIT);
208 } 488 }
489#endif
209 490
210 uint8_t checksum = 0; 491 // initiator recive phase
211 // send data to the slave 492 // if the target is present syncronize with it
212 for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) { 493 if( trans->target2initiator_buffer_size > 0 ) {
213 serial_write_byte(serial_master_buffer[i]); 494 if (!serial_recive_packet((uint8_t *)trans->target2initiator_buffer,
214 sync_recv(); 495 trans->target2initiator_buffer_size) ) {
215 checksum += serial_master_buffer[i]; 496 serial_output();
497 serial_high();
498 *trans->status = TRANSACTION_DATA_ERROR;
499 sei();
500 return TRANSACTION_DATA_ERROR;
501 }
502 }
503
504 // initiator switch to output
505 change_reciver2sender();
506
507 // initiator send phase
508 if( trans->initiator2target_buffer_size > 0 ) {
509 serial_send_packet((uint8_t *)trans->initiator2target_buffer,
510 trans->initiator2target_buffer_size);
216 } 511 }
217 serial_write_byte(checksum);
218 sync_recv();
219 512
220 // always, release the line when not in use 513 // always, release the line when not in use
221 serial_output(); 514 sync_send();
222 serial_high();
223 515
516 *trans->status = TRANSACTION_END;
224 sei(); 517 sei();
225 return 0; 518 return TRANSACTION_END;
226} 519}
227 520
521#ifdef SERIAL_USE_MULTI_TRANSACTION
522int soft_serial_get_and_clean_status(int sstd_index) {
523 SSTD_t *trans = &Transaction_table[sstd_index];
524 cli();
525 int retval = *trans->status;
526 *trans->status = 0;;
527 sei();
528 return retval;
529}
530#endif
531
228#endif 532#endif
533
534// Helix serial.c history
535// 2018-1-29 fork from let's split and add PD2, modify sync_recv() (#2308, bceffdefc)
536// 2018-6-28 bug fix master to slave comm and speed up (#3255, 1038bbef4)
537// (adjusted with avr-gcc 4.9.2)
538// 2018-7-13 remove USE_SERIAL_PD2 macro (#3374, f30d6dd78)
539// (adjusted with avr-gcc 4.9.2)
540// 2018-8-11 add support multi-type transaction (#3608, feb5e4aae)
541// (adjusted with avr-gcc 4.9.2)
542// 2018-10-21 fix serial and RGB animation conflict (#4191, 4665e4fff)
543// (adjusted with avr-gcc 7.3.0)
544// 2018-10-28 re-adjust compiler depend value of delay (#4269, 8517f8a66)
545// (adjusted with avr-gcc 5.4.0, 7.3.0)
546// 2018-12-17 copy to TOP/quantum/split_common/ and remove backward compatibility code (#4669)
diff --git a/quantum/split_common/serial.h b/quantum/split_common/serial.h
index e566eb8a0..b6638b3bd 100644
--- a/quantum/split_common/serial.h
+++ b/quantum/split_common/serial.h
@@ -1,29 +1,65 @@
1#ifndef MY_SERIAL_H 1#ifndef SOFT_SERIAL_H
2#define MY_SERIAL_H 2#define SOFT_SERIAL_H
3 3
4#include "config.h"
5#include <stdbool.h> 4#include <stdbool.h>
6 5
7/* TODO: some defines for interrupt setup */ 6// /////////////////////////////////////////////////////////////////
8#define SERIAL_PIN_DDR DDRD 7// Need Soft Serial defines in config.h
9#define SERIAL_PIN_PORT PORTD 8// /////////////////////////////////////////////////////////////////
10#define SERIAL_PIN_INPUT PIND 9// ex.
11#define SERIAL_PIN_MASK _BV(PD0) 10// #define SOFT_SERIAL_PIN ?? // ?? = D0,D1,D2,D3,E6
12#define SERIAL_PIN_INTERRUPT INT0_vect 11// OPTIONAL: #define SELECT_SOFT_SERIAL_SPEED ? // ? = 1,2,3,4,5
12// // 1: about 137kbps (default)
13// // 2: about 75kbps
14// // 3: about 39kbps
15// // 4: about 26kbps
16// // 5: about 20kbps
17//
18// //// USE simple API (using signle-type transaction function)
19// /* nothing */
20// //// USE flexible API (using multi-type transaction function)
21// #define SERIAL_USE_MULTI_TRANSACTION
22//
23// /////////////////////////////////////////////////////////////////
13 24
14#define SERIAL_SLAVE_BUFFER_LENGTH MATRIX_ROWS/2 25// Soft Serial Transaction Descriptor
15#define SERIAL_MASTER_BUFFER_LENGTH 1 26typedef struct _SSTD_t {
27 uint8_t *status;
28 uint8_t initiator2target_buffer_size;
29 uint8_t *initiator2target_buffer;
30 uint8_t target2initiator_buffer_size;
31 uint8_t *target2initiator_buffer;
32} SSTD_t;
33#define TID_LIMIT( table ) (sizeof(table) / sizeof(SSTD_t))
16 34
17// Address location defines 35// initiator is transaction start side
18#define SERIAL_BACKLIT_START 0x00 36void soft_serial_initiator_init(SSTD_t *sstd_table, int sstd_table_size);
37// target is interrupt accept side
38void soft_serial_target_init(SSTD_t *sstd_table, int sstd_table_size);
19 39
20// Buffers for master - slave communication 40// initiator resullt
21extern volatile uint8_t serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH]; 41#define TRANSACTION_END 0
22extern volatile uint8_t serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH]; 42#define TRANSACTION_NO_RESPONSE 0x1
23 43#define TRANSACTION_DATA_ERROR 0x2
24void serial_master_init(void); 44#define TRANSACTION_TYPE_ERROR 0x4
25void serial_slave_init(void); 45#ifndef SERIAL_USE_MULTI_TRANSACTION
26int serial_update_buffers(void); 46int soft_serial_transaction(void);
27bool serial_slave_data_corrupt(void); 47#else
48int soft_serial_transaction(int sstd_index);
49#endif
28 50
51// target status
52// *SSTD_t.status has
53// initiator:
54// TRANSACTION_END
55// or TRANSACTION_NO_RESPONSE
56// or TRANSACTION_DATA_ERROR
57// target:
58// TRANSACTION_DATA_ERROR
59// or TRANSACTION_ACCEPTED
60#define TRANSACTION_ACCEPTED 0x8
61#ifdef SERIAL_USE_MULTI_TRANSACTION
62int soft_serial_get_and_clean_status(int sstd_index);
29#endif 63#endif
64
65#endif /* SOFT_SERIAL_H */
diff --git a/quantum/split_common/split_util.c b/quantum/split_common/split_util.c
index 13b09d5b8..e41b6f638 100644
--- a/quantum/split_common/split_util.c
+++ b/quantum/split_common/split_util.c
@@ -1,31 +1,21 @@
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" 1#include "split_util.h"
8#include "matrix.h" 2#include "matrix.h"
9#include "keyboard.h" 3#include "keyboard.h"
10#include "config.h" 4#include "config.h"
11#include "timer.h" 5#include "timer.h"
12#include "split_flags.h" 6#include "split_flags.h"
7#include "quantum.h"
13 8
14#ifdef RGBLIGHT_ENABLE 9#ifdef EE_HANDS
15# include "rgblight.h" 10# include "tmk_core/common/eeprom.h"
16#endif 11#endif
12
17#ifdef BACKLIGHT_ENABLE 13#ifdef BACKLIGHT_ENABLE
18# include "backlight.h" 14# include "backlight.h"
19#endif 15#endif
20 16
21#ifdef SPLIT_HAND_PIN
22# include "pincontrol.h"
23#endif
24
25#if defined(USE_I2C) || defined(EH) 17#if defined(USE_I2C) || defined(EH)
26# include "i2c.h" 18# include "i2c.h"
27#else
28# include "serial.h"
29#endif 19#endif
30 20
31volatile bool isLeftHand = true; 21volatile bool isLeftHand = true;
@@ -35,14 +25,13 @@ volatile uint8_t setTries = 0;
35static void setup_handedness(void) { 25static void setup_handedness(void) {
36 #ifdef SPLIT_HAND_PIN 26 #ifdef SPLIT_HAND_PIN
37 // Test pin SPLIT_HAND_PIN for High/Low, if low it's right hand 27 // Test pin SPLIT_HAND_PIN for High/Low, if low it's right hand
38 pinMode(SPLIT_HAND_PIN, PinDirectionInput); 28 setPinInput(SPLIT_HAND_PIN);
39 isLeftHand = digitalRead(SPLIT_HAND_PIN); 29 isLeftHand = readPin(SPLIT_HAND_PIN);
40 #else 30 #else
41 #ifdef EE_HANDS 31 #ifdef EE_HANDS
42 isLeftHand = eeprom_read_byte(EECONFIG_HANDEDNESS); 32 isLeftHand = eeprom_read_byte(EECONFIG_HANDEDNESS);
43 #else 33 #else
44 // I2C_MASTER_RIGHT is deprecated, use MASTER_RIGHT instead, since this works for both serial and i2c 34 #ifdef MASTER_RIGHT
45 #if defined(I2C_MASTER_RIGHT) || defined(MASTER_RIGHT)
46 isLeftHand = !has_usb(); 35 isLeftHand = !has_usb();
47 #else 36 #else
48 isLeftHand = has_usb(); 37 isLeftHand = has_usb();
@@ -94,7 +83,7 @@ void split_keyboard_setup(void) {
94 83
95void keyboard_slave_loop(void) { 84void keyboard_slave_loop(void) {
96 matrix_init(); 85 matrix_init();
97 86
98 //Init RGB 87 //Init RGB
99 #ifdef RGBLIGHT_ENABLE 88 #ifdef RGBLIGHT_ENABLE
100 rgblight_init(); 89 rgblight_init();
@@ -103,17 +92,17 @@ void keyboard_slave_loop(void) {
103 while (1) { 92 while (1) {
104 // Matrix Slave Scan 93 // Matrix Slave Scan
105 matrix_slave_scan(); 94 matrix_slave_scan();
106 95
107 // Read Backlight Info 96 // Read Backlight Info
108 #ifdef BACKLIGHT_ENABLE 97 #ifdef BACKLIGHT_ENABLE
109 if (BACKLIT_DIRTY) { 98 #ifdef USE_I2C
110 #ifdef USE_I2C 99 if (BACKLIT_DIRTY) {
111 backlight_set(i2c_slave_buffer[I2C_BACKLIT_START]); 100 backlight_set(i2c_slave_buffer[I2C_BACKLIT_START]);
112 #else // USE_SERIAL 101 BACKLIT_DIRTY = false;
113 backlight_set(serial_master_buffer[SERIAL_BACKLIT_START]); 102 }
114 #endif 103 #else // USE_SERIAL
115 BACKLIT_DIRTY = false; 104 backlight_set(serial_m2s_buffer.backlight_level);
116 } 105 #endif
117 #endif 106 #endif
118 // Read RGB Info 107 // Read RGB Info
119 #ifdef RGBLIGHT_ENABLE 108 #ifdef RGBLIGHT_ENABLE
@@ -122,14 +111,14 @@ void keyboard_slave_loop(void) {
122 // Disable interupts (RGB data is big) 111 // Disable interupts (RGB data is big)
123 cli(); 112 cli();
124 // Create new DWORD for RGB data 113 // Create new DWORD for RGB data
125 uint32_t dword; 114 uint32_t dword;
126 115
127 // Fill the new DWORD with the data that was sent over 116 // Fill the new DWORD with the data that was sent over
128 uint8_t *dword_dat = (uint8_t *)(&dword); 117 uint8_t *dword_dat = (uint8_t *)(&dword);
129 for (int i = 0; i < 4; i++) { 118 for (int i = 0; i < 4; i++) {
130 dword_dat[i] = i2c_slave_buffer[I2C_RGB_START+i]; 119 dword_dat[i] = i2c_slave_buffer[I2C_RGB_START+i];
131 } 120 }
132 121
133 // Update the RGB now with the new data and set RGB_DIRTY to false 122 // Update the RGB now with the new data and set RGB_DIRTY to false
134 rgblight_update_dword(dword); 123 rgblight_update_dword(dword);
135 RGB_DIRTY = false; 124 RGB_DIRTY = false;
@@ -137,7 +126,9 @@ void keyboard_slave_loop(void) {
137 sei(); 126 sei();
138 } 127 }
139 #else // USE_SERIAL 128 #else // USE_SERIAL
129 #ifdef RGBLIGHT_SPLIT
140 // Add serial implementation for RGB here 130 // Add serial implementation for RGB here
131 #endif
141 #endif 132 #endif
142 #endif 133 #endif
143 } 134 }