From 3f4cef6c84cc6ae9f28efe31b3b9fcd038ece541 Mon Sep 17 00:00:00 2001 From: mtei <2170248+mtei@users.noreply.github.com> Date: Sun, 22 Jul 2018 22:11:44 +0900 Subject: [PATCH] helix serial master-slave transaction optimize Using multi-type transaction feature of serial.c, communication contents between master slaves were optimized. --- .../rev1/keymaps/OLED_sample/serial_config.h | 2 - keyboards/helix/rev1/serial_config.h | 2 - keyboards/helix/rev2/matrix.c | 23 +++++++++-- keyboards/helix/rev2/serial_config.h | 4 +- keyboards/helix/rev2/split_scomm.c | 40 ++++++++++++++++--- keyboards/helix/rev2/split_scomm.h | 3 ++ keyboards/helix/serial.c | 10 ++--- keyboards/helix/serial.h | 4 +- 8 files changed, 66 insertions(+), 22 deletions(-) diff --git a/keyboards/helix/rev1/keymaps/OLED_sample/serial_config.h b/keyboards/helix/rev1/keymaps/OLED_sample/serial_config.h index 009052fa8a..289a165fa3 100644 --- a/keyboards/helix/rev1/keymaps/OLED_sample/serial_config.h +++ b/keyboards/helix/rev1/keymaps/OLED_sample/serial_config.h @@ -8,8 +8,6 @@ #define SERIAL_PIN_MASK _BV(PD2) #define SERIAL_PIN_INTERRUPT INT2_vect -#define SERIAL_USE_SIMPLE_TRANSACTION - //// #error rev1/keymaps/OLED_sample serial config #endif /* SOFT_SERIAL_CONFIG_H */ diff --git a/keyboards/helix/rev1/serial_config.h b/keyboards/helix/rev1/serial_config.h index 36e60305e2..f021d6e04b 100644 --- a/keyboards/helix/rev1/serial_config.h +++ b/keyboards/helix/rev1/serial_config.h @@ -8,8 +8,6 @@ #define SERIAL_PIN_MASK _BV(PD0) #define SERIAL_PIN_INTERRUPT INT0_vect -#define SERIAL_USE_SIMPLE_TRANSACTION - /// #error rev1 serial config #endif /* SOFT_SERIAL_CONFIG_H */ diff --git a/keyboards/helix/rev2/matrix.c b/keyboards/helix/rev2/matrix.c index 763f97d4ba..fd64c37ab1 100644 --- a/keyboards/helix/rev2/matrix.c +++ b/keyboards/helix/rev2/matrix.c @@ -181,11 +181,11 @@ i2c_error: // the cable is disconnceted, or something else went wrong #else // USE_SERIAL -int serial_transaction(void) { +int serial_transaction(int master_changed) { int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0; - int ret=serial_update_buffers(1); + int ret=serial_update_buffers(master_changed); if (ret ) { - if(ret==2)RXLED1; + if(ret==2) RXLED1; return 1; } RXLED0; @@ -213,6 +213,7 @@ uint8_t matrix_scan(void) uint8_t matrix_master_scan(void) { int ret = _matrix_scan(); + int mchanged = 1; #ifndef KEYBOARD_helix_rev1 int offset = (isLeftHand) ? 0 : ROWS_PER_HAND; @@ -223,6 +224,10 @@ uint8_t matrix_master_scan(void) { // i2c_slave_buffer[i] = matrix[offset+i]; // } #else // USE_SERIAL + #ifdef SERIAL_USE_MULTI_TRANSACTION + mchanged = memcmp((void *)serial_master_buffer, + &matrix[offset], sizeof(serial_master_buffer)); + #endif memcpy((void *)serial_master_buffer, &matrix[offset], sizeof(serial_master_buffer)); #endif @@ -231,7 +236,7 @@ uint8_t matrix_master_scan(void) { #ifdef USE_MATRIX_I2C if( i2c_transaction() ) { #else // USE_SERIAL - if( serial_transaction() ) { + if( serial_transaction(mchanged) ) { #endif // turn on the indicator led when halves are disconnected TXLED1; @@ -265,9 +270,19 @@ void matrix_slave_scan(void) { i2c_slave_buffer[i] = matrix[offset+i]; } #else // USE_SERIAL + #ifdef SERIAL_USE_MULTI_TRANSACTION + int change = 0; + #endif for (int i = 0; i < ROWS_PER_HAND; ++i) { + #ifdef SERIAL_USE_MULTI_TRANSACTION + if( serial_slave_buffer[i] != matrix[offset+i] ) + change = 1; + #endif serial_slave_buffer[i] = matrix[offset+i]; } + #ifdef SERIAL_USE_MULTI_TRANSACTION + slave_buffer_change_count += change; + #endif #endif } diff --git a/keyboards/helix/rev2/serial_config.h b/keyboards/helix/rev2/serial_config.h index 9664ba159f..d19dbea1ec 100644 --- a/keyboards/helix/rev2/serial_config.h +++ b/keyboards/helix/rev2/serial_config.h @@ -8,8 +8,8 @@ #define SERIAL_PIN_MASK _BV(PD2) #define SERIAL_PIN_INTERRUPT INT2_vect -// if you need more program area, use this macro -// #define SERIAL_USE_SIMPLE_TRANSACTION +// if you need more program area, comment out this macro +#define SERIAL_USE_MULTI_TRANSACTION //// #error rev2 serial config diff --git a/keyboards/helix/rev2/split_scomm.c b/keyboards/helix/rev2/split_scomm.c index 1ddd9b8b7d..ba02a14eac 100644 --- a/keyboards/helix/rev2/split_scomm.c +++ b/keyboards/helix/rev2/split_scomm.c @@ -8,8 +8,12 @@ uint8_t volatile serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH] = {0}; uint8_t volatile serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH] = {0}; uint8_t volatile status0 = 0; +#ifdef SERIAL_USE_MULTI_TRANSACTION +uint8_t slave_buffer_change_count = 0; +uint8_t s_change_old = 0xff; +#endif -#ifdef SERIAL_USE_SIMPLE_TRANSACTION +#ifndef SERIAL_USE_MULTI_TRANSACTION SSTD_t transactions[] = { { (uint8_t *)&status0, sizeof(serial_master_buffer), (uint8_t *)serial_master_buffer, @@ -17,10 +21,24 @@ SSTD_t transactions[] = { } }; #else + SSTD_t transactions[] = { -#define WHOLE_MATRIX_EXCHANGE 0 +#define GET_SLAVE_STATUS 0 + /* master buffer not changed, only recive slave_buffer_change_count */ + { (uint8_t *)&status0, + 0, NULL, + sizeof(slave_buffer_change_count), &slave_buffer_change_count, + }, +#define PUT_MASTER_GET_SLAVE_STATUS 1 + /* master buffer changed need send, and recive slave_buffer_change_count */ { (uint8_t *)&status0, sizeof(serial_master_buffer), (uint8_t *)serial_master_buffer, + sizeof(slave_buffer_change_count), &slave_buffer_change_count, + }, +#define GET_SLAVE_BUFFER 2 + /* recive serial_slave_buffer */ + { (uint8_t *)&status0, + 0, NULL, sizeof(serial_slave_buffer), (uint8_t *)serial_slave_buffer } }; @@ -41,10 +59,22 @@ void serial_slave_init(void) // 2 => checksum error int serial_update_buffers(int master_update) { -#ifdef SERIAL_USE_SIMPLE_TRANSACTION - return soft_serial_transaction(); +#ifdef SERIAL_USE_MULTI_TRANSACTION + int status; + static int need_retry = 0; + if( s_change_old != slave_buffer_change_count ) { + status = soft_serial_transaction(GET_SLAVE_BUFFER); + if( status == TRANSACTION_END ) + s_change_old = slave_buffer_change_count; + } + if( !master_update && !need_retry) + status = soft_serial_transaction(GET_SLAVE_STATUS); + else + status = soft_serial_transaction(PUT_MASTER_GET_SLAVE_STATUS); + need_retry = ( status == TRANSACTION_END ) ? 0 : 1; + return status; #else - return soft_serial_transaction(WHOLE_MATRIX_EXCHANGE); + return soft_serial_transaction(); #endif } #endif /* USE_SERIAL */ diff --git a/keyboards/helix/rev2/split_scomm.h b/keyboards/helix/rev2/split_scomm.h index 902de02132..daa680f8d2 100644 --- a/keyboards/helix/rev2/split_scomm.h +++ b/keyboards/helix/rev2/split_scomm.h @@ -7,6 +7,9 @@ extern volatile uint8_t serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH]; extern volatile uint8_t serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH]; +#ifdef SERIAL_USE_MULTI_TRANSACTION +extern uint8_t slave_buffer_change_count; +#endif void serial_master_init(void); void serial_slave_init(void); diff --git a/keyboards/helix/serial.c b/keyboards/helix/serial.c index 0870ddf106..09f7c6c0f0 100644 --- a/keyboards/helix/serial.c +++ b/keyboards/helix/serial.c @@ -58,7 +58,7 @@ #define SERIAL_DELAY_HALF2 (SERIAL_DELAY - SERIAL_DELAY/2) #define SLAVE_INT_WIDTH_US 1 -#ifdef SERIAL_USE_SIMPLE_TRANSACTION +#ifndef SERIAL_USE_MULTI_TRANSACTION #define SLAVE_INT_RESPONSE_TIME SERIAL_DELAY #else #define SLAVE_INT_ACK_WIDTH_UNIT 2 @@ -282,7 +282,7 @@ ISR(SERIAL_PIN_INTERRUPT) { debug_recvsample(); debug_recvsample(); // indicate intterupt entry debug_sync_start(); debug_sync_end(); // indicate intterupt entry -#ifdef SERIAL_USE_SIMPLE_TRANSACTION +#ifndef SERIAL_USE_MULTI_TRANSACTION serial_low(); serial_output(); SSTD_t *trans = Transaction_table; @@ -338,7 +338,7 @@ ISR(SERIAL_PIN_INTERRUPT) { // TRANSACTION_NO_RESPONSE // TRANSACTION_DATA_ERROR // this code is very time dependent, so we need to disable interrupts -#ifdef SERIAL_USE_SIMPLE_TRANSACTION +#ifndef SERIAL_USE_MULTI_TRANSACTION int soft_serial_transaction(void) { SSTD_t *trans = Transaction_table; #else @@ -352,7 +352,7 @@ int soft_serial_transaction(int sstd_index) { serial_low(); _delay_us(SLAVE_INT_WIDTH_US); -#ifdef SERIAL_USE_SIMPLE_TRANSACTION +#ifndef SERIAL_USE_MULTI_TRANSACTION // wait for the target response serial_input_with_pullup(); _delay_us(SLAVE_INT_RESPONSE_TIME); @@ -433,7 +433,7 @@ int soft_serial_transaction(int sstd_index) { return TRANSACTION_END; } -#ifndef SERIAL_USE_SIMPLE_TRANSACTION +#ifdef SERIAL_USE_MULTI_TRANSACTION int soft_serial_get_and_clean_status(int sstd_index) { SSTD_t *trans = &Transaction_table[sstd_index]; cli(); diff --git a/keyboards/helix/serial.h b/keyboards/helix/serial.h index 8664f12eb9..67d6472205 100644 --- a/keyboards/helix/serial.h +++ b/keyboards/helix/serial.h @@ -31,7 +31,7 @@ void soft_serial_target_init(SSTD_t *sstd_table); #define TRANSACTION_END 0 #define TRANSACTION_NO_RESPONSE 0x1 #define TRANSACTION_DATA_ERROR 0x2 -#ifdef SERIAL_USE_SIMPLE_TRANSACTION +#ifndef SERIAL_USE_MULTI_TRANSACTION int soft_serial_transaction(void); #else int soft_serial_transaction(int sstd_index); @@ -47,7 +47,7 @@ int soft_serial_transaction(int sstd_index); // TRANSACTION_DATA_ERROR // or TRANSACTION_ACCEPTED #define TRANSACTION_ACCEPTED 0x4 -#ifndef SERIAL_USE_SIMPLE_TRANSACTION +#ifdef SERIAL_USE_MULTI_TRANSACTION int soft_serial_get_and_clean_status(int sstd_index); #endif