diff options
-rw-r--r-- | common_features.mk | 5 | ||||
-rw-r--r-- | keyboards/miniaxe/matrix.c | 59 | ||||
-rw-r--r-- | quantum/split_common/matrix.c | 61 | ||||
-rw-r--r-- | quantum/split_common/matrix.h | 31 | ||||
-rw-r--r-- | quantum/split_common/serial.c | 505 | ||||
-rw-r--r-- | quantum/split_common/serial.h | 65 | ||||
-rw-r--r-- | quantum/split_common/serial_backward_compatibility.h | 11 | ||||
-rw-r--r-- | quantum/split_common/split_util.c | 9 | ||||
-rw-r--r-- | quantum/split_common/split_util.h | 3 |
9 files changed, 578 insertions, 171 deletions
diff --git a/common_features.mk b/common_features.mk index 73aab5d84..883681324 100644 --- a/common_features.mk +++ b/common_features.mk | |||
@@ -263,11 +263,6 @@ ifneq ($(strip $(CUSTOM_MATRIX)), yes) | |||
263 | endif | 263 | endif |
264 | 264 | ||
265 | ifeq ($(strip $(SPLIT_KEYBOARD)), yes) | 265 | ifeq ($(strip $(SPLIT_KEYBOARD)), yes) |
266 | SERIAL_BACKWARD_COMPAT := $(wildcard $(QUANTUM_DIR)/split_common/serial_backward_compatibility.h) | ||
267 | ifneq ($(SERIAL_BACKWARD_COMPAT),) | ||
268 | CONFIG_H += $(SERIAL_BACKWARD_COMPAT) | ||
269 | # $(info CONFIG_H=$(CONFIG_H)) | ||
270 | endif | ||
271 | OPT_DEFS += -DSPLIT_KEYBOARD | 266 | OPT_DEFS += -DSPLIT_KEYBOARD |
272 | QUANTUM_SRC += $(QUANTUM_DIR)/split_common/split_flags.c \ | 267 | QUANTUM_SRC += $(QUANTUM_DIR)/split_common/split_flags.c \ |
273 | $(QUANTUM_DIR)/split_common/split_util.c | 268 | $(QUANTUM_DIR)/split_common/split_util.c |
diff --git a/keyboards/miniaxe/matrix.c b/keyboards/miniaxe/matrix.c index 55c458e1f..5fec1281d 100644 --- a/keyboards/miniaxe/matrix.c +++ b/keyboards/miniaxe/matrix.c | |||
@@ -32,9 +32,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
32 | #include "timer.h" | 32 | #include "timer.h" |
33 | #include "split_flags.h" | 33 | #include "split_flags.h" |
34 | 34 | ||
35 | #ifdef RGBLIGHT_ENABLE | ||
36 | # include "rgblight.h" | ||
37 | #endif | ||
38 | #ifdef BACKLIGHT_ENABLE | 35 | #ifdef BACKLIGHT_ENABLE |
39 | # include "backlight.h" | 36 | # include "backlight.h" |
40 | extern backlight_config_t backlight_config; | 37 | extern backlight_config_t backlight_config; |
@@ -55,6 +52,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
55 | static bool debouncing = false; | 52 | static bool debouncing = false; |
56 | #endif | 53 | #endif |
57 | 54 | ||
55 | #if defined(USE_I2C) || defined(EH) | ||
56 | |||
58 | #if (MATRIX_COLS <= 8) | 57 | #if (MATRIX_COLS <= 8) |
59 | # define print_matrix_header() print("\nr/c 01234567\n") | 58 | # define print_matrix_header() print("\nr/c 01234567\n") |
60 | # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) | 59 | # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) |
@@ -63,6 +62,27 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
63 | #else | 62 | #else |
64 | # error "Currently only supports 8 COLS" | 63 | # error "Currently only supports 8 COLS" |
65 | #endif | 64 | #endif |
65 | |||
66 | #else // USE_SERIAL | ||
67 | |||
68 | #if (MATRIX_COLS <= 8) | ||
69 | # define print_matrix_header() print("\nr/c 01234567\n") | ||
70 | # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) | ||
71 | # define matrix_bitpop(i) bitpop(matrix[i]) | ||
72 | # define ROW_SHIFTER ((uint8_t)1) | ||
73 | #elif (MATRIX_COLS <= 16) | ||
74 | # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n") | ||
75 | # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row)) | ||
76 | # define matrix_bitpop(i) bitpop16(matrix[i]) | ||
77 | # define ROW_SHIFTER ((uint16_t)1) | ||
78 | #elif (MATRIX_COLS <= 32) | ||
79 | # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n") | ||
80 | # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row)) | ||
81 | # define matrix_bitpop(i) bitpop32(matrix[i]) | ||
82 | # define ROW_SHIFTER ((uint32_t)1) | ||
83 | #endif | ||
84 | |||
85 | #endif | ||
66 | static matrix_row_t matrix_debouncing[MATRIX_ROWS]; | 86 | static matrix_row_t matrix_debouncing[MATRIX_ROWS]; |
67 | 87 | ||
68 | #define ERROR_DISCONNECT_COUNT 5 | 88 | #define ERROR_DISCONNECT_COUNT 5 |
@@ -315,15 +335,39 @@ i2c_error: // the cable is disconnceted, or something else went wrong | |||
315 | 335 | ||
316 | #else // USE_SERIAL | 336 | #else // USE_SERIAL |
317 | 337 | ||
338 | |||
339 | typedef struct _Serial_s2m_buffer_t { | ||
340 | // TODO: if MATRIX_COLS > 8 change to uint8_t packed_matrix[] for pack/unpack | ||
341 | matrix_row_t smatrix[ROWS_PER_HAND]; | ||
342 | } Serial_s2m_buffer_t; | ||
343 | |||
344 | volatile Serial_s2m_buffer_t serial_s2m_buffer = {}; | ||
345 | volatile Serial_m2s_buffer_t serial_m2s_buffer = {}; | ||
346 | uint8_t volatile status0 = 0; | ||
347 | |||
348 | SSTD_t transactions[] = { | ||
349 | { (uint8_t *)&status0, | ||
350 | sizeof(serial_m2s_buffer), (uint8_t *)&serial_m2s_buffer, | ||
351 | sizeof(serial_s2m_buffer), (uint8_t *)&serial_s2m_buffer | ||
352 | } | ||
353 | }; | ||
354 | |||
355 | void serial_master_init(void) | ||
356 | { soft_serial_initiator_init(transactions, TID_LIMIT(transactions)); } | ||
357 | |||
358 | void serial_slave_init(void) | ||
359 | { soft_serial_target_init(transactions, TID_LIMIT(transactions)); } | ||
360 | |||
318 | int serial_transaction(void) { | 361 | int serial_transaction(void) { |
319 | int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0; | 362 | int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0; |
320 | 363 | ||
321 | if (serial_update_buffers()) { | 364 | if (soft_serial_transaction()) { |
322 | return 1; | 365 | return 1; |
323 | } | 366 | } |
324 | 367 | ||
368 | // TODO: if MATRIX_COLS > 8 change to unpack() | ||
325 | for (int i = 0; i < ROWS_PER_HAND; ++i) { | 369 | for (int i = 0; i < ROWS_PER_HAND; ++i) { |
326 | matrix[slaveOffset+i] = serial_slave_buffer[i]; | 370 | matrix[slaveOffset+i] = serial_s2m_buffer.smatrix[i]; |
327 | } | 371 | } |
328 | 372 | ||
329 | #ifdef RGBLIGHT_ENABLE | 373 | #ifdef RGBLIGHT_ENABLE |
@@ -332,7 +376,7 @@ int serial_transaction(void) { | |||
332 | 376 | ||
333 | #ifdef BACKLIGHT_ENABLE | 377 | #ifdef BACKLIGHT_ENABLE |
334 | // Write backlight level for slave to read | 378 | // Write backlight level for slave to read |
335 | serial_master_buffer[SERIAL_BACKLIT_START] = backlight_config.enable ? backlight_config.level : 0; | 379 | serial_m2s_buffer.backlight_level = backlight_config.enable ? backlight_config.level : 0; |
336 | #endif | 380 | #endif |
337 | 381 | ||
338 | return 0; | 382 | return 0; |
@@ -375,8 +419,9 @@ void matrix_slave_scan(void) { | |||
375 | i2c_slave_buffer[I2C_KEYMAP_START+i] = matrix[offset+i]; | 419 | i2c_slave_buffer[I2C_KEYMAP_START+i] = matrix[offset+i]; |
376 | } | 420 | } |
377 | #else // USE_SERIAL | 421 | #else // USE_SERIAL |
422 | // TODO: if MATRIX_COLS > 8 change to pack() | ||
378 | for (int i = 0; i < ROWS_PER_HAND; ++i) { | 423 | for (int i = 0; i < ROWS_PER_HAND; ++i) { |
379 | serial_slave_buffer[i] = matrix[offset+i]; | 424 | serial_s2m_buffer.smatrix[i] = matrix[offset+i]; |
380 | } | 425 | } |
381 | #endif | 426 | #endif |
382 | matrix_slave_scan_user(); | 427 | matrix_slave_scan_user(); |
diff --git a/quantum/split_common/matrix.c b/quantum/split_common/matrix.c index d6359b51f..261137638 100644 --- a/quantum/split_common/matrix.c +++ b/quantum/split_common/matrix.c | |||
@@ -32,9 +32,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
32 | #include "timer.h" | 32 | #include "timer.h" |
33 | #include "split_flags.h" | 33 | #include "split_flags.h" |
34 | 34 | ||
35 | #ifdef RGBLIGHT_ENABLE | ||
36 | # include "rgblight.h" | ||
37 | #endif | ||
38 | #ifdef BACKLIGHT_ENABLE | 35 | #ifdef BACKLIGHT_ENABLE |
39 | # include "backlight.h" | 36 | # include "backlight.h" |
40 | extern backlight_config_t backlight_config; | 37 | extern backlight_config_t backlight_config; |
@@ -55,6 +52,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
55 | static bool debouncing = false; | 52 | static bool debouncing = false; |
56 | #endif | 53 | #endif |
57 | 54 | ||
55 | #if defined(USE_I2C) || defined(EH) | ||
56 | |||
58 | #if (MATRIX_COLS <= 8) | 57 | #if (MATRIX_COLS <= 8) |
59 | # define print_matrix_header() print("\nr/c 01234567\n") | 58 | # define print_matrix_header() print("\nr/c 01234567\n") |
60 | # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) | 59 | # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) |
@@ -63,6 +62,27 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
63 | #else | 62 | #else |
64 | # error "Currently only supports 8 COLS" | 63 | # error "Currently only supports 8 COLS" |
65 | #endif | 64 | #endif |
65 | |||
66 | #else // USE_SERIAL | ||
67 | |||
68 | #if (MATRIX_COLS <= 8) | ||
69 | # define print_matrix_header() print("\nr/c 01234567\n") | ||
70 | # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) | ||
71 | # define matrix_bitpop(i) bitpop(matrix[i]) | ||
72 | # define ROW_SHIFTER ((uint8_t)1) | ||
73 | #elif (MATRIX_COLS <= 16) | ||
74 | # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n") | ||
75 | # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row)) | ||
76 | # define matrix_bitpop(i) bitpop16(matrix[i]) | ||
77 | # define ROW_SHIFTER ((uint16_t)1) | ||
78 | #elif (MATRIX_COLS <= 32) | ||
79 | # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n") | ||
80 | # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row)) | ||
81 | # define matrix_bitpop(i) bitpop32(matrix[i]) | ||
82 | # define ROW_SHIFTER ((uint32_t)1) | ||
83 | #endif | ||
84 | |||
85 | #endif | ||
66 | static matrix_row_t matrix_debouncing[MATRIX_ROWS]; | 86 | static matrix_row_t matrix_debouncing[MATRIX_ROWS]; |
67 | 87 | ||
68 | #define ERROR_DISCONNECT_COUNT 5 | 88 | #define ERROR_DISCONNECT_COUNT 5 |
@@ -286,24 +306,48 @@ i2c_error: // the cable is disconnceted, or something else went wrong | |||
286 | 306 | ||
287 | #else // USE_SERIAL | 307 | #else // USE_SERIAL |
288 | 308 | ||
309 | |||
310 | typedef struct _Serial_s2m_buffer_t { | ||
311 | // TODO: if MATRIX_COLS > 8 change to uint8_t packed_matrix[] for pack/unpack | ||
312 | matrix_row_t smatrix[ROWS_PER_HAND]; | ||
313 | } Serial_s2m_buffer_t; | ||
314 | |||
315 | volatile Serial_s2m_buffer_t serial_s2m_buffer = {}; | ||
316 | volatile Serial_m2s_buffer_t serial_m2s_buffer = {}; | ||
317 | uint8_t volatile status0 = 0; | ||
318 | |||
319 | SSTD_t transactions[] = { | ||
320 | { (uint8_t *)&status0, | ||
321 | sizeof(serial_m2s_buffer), (uint8_t *)&serial_m2s_buffer, | ||
322 | sizeof(serial_s2m_buffer), (uint8_t *)&serial_s2m_buffer | ||
323 | } | ||
324 | }; | ||
325 | |||
326 | void serial_master_init(void) | ||
327 | { soft_serial_initiator_init(transactions, TID_LIMIT(transactions)); } | ||
328 | |||
329 | void serial_slave_init(void) | ||
330 | { soft_serial_target_init(transactions, TID_LIMIT(transactions)); } | ||
331 | |||
289 | int serial_transaction(void) { | 332 | int serial_transaction(void) { |
290 | int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0; | 333 | int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0; |
291 | 334 | ||
292 | if (serial_update_buffers()) { | 335 | if (soft_serial_transaction()) { |
293 | return 1; | 336 | return 1; |
294 | } | 337 | } |
295 | 338 | ||
339 | // TODO: if MATRIX_COLS > 8 change to unpack() | ||
296 | for (int i = 0; i < ROWS_PER_HAND; ++i) { | 340 | for (int i = 0; i < ROWS_PER_HAND; ++i) { |
297 | matrix[slaveOffset+i] = serial_slave_buffer[i]; | 341 | matrix[slaveOffset+i] = serial_s2m_buffer.smatrix[i]; |
298 | } | 342 | } |
299 | 343 | ||
300 | #ifdef RGBLIGHT_ENABLE | 344 | #if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT) |
301 | // Code to send RGB over serial goes here (not implemented yet) | 345 | // Code to send RGB over serial goes here (not implemented yet) |
302 | #endif | 346 | #endif |
303 | 347 | ||
304 | #ifdef BACKLIGHT_ENABLE | 348 | #ifdef BACKLIGHT_ENABLE |
305 | // Write backlight level for slave to read | 349 | // Write backlight level for slave to read |
306 | serial_master_buffer[SERIAL_BACKLIT_START] = backlight_config.enable ? backlight_config.level : 0; | 350 | serial_m2s_buffer.backlight_level = backlight_config.enable ? backlight_config.level : 0; |
307 | #endif | 351 | #endif |
308 | 352 | ||
309 | return 0; | 353 | return 0; |
@@ -346,8 +390,9 @@ void matrix_slave_scan(void) { | |||
346 | i2c_slave_buffer[I2C_KEYMAP_START+i] = matrix[offset+i]; | 390 | i2c_slave_buffer[I2C_KEYMAP_START+i] = matrix[offset+i]; |
347 | } | 391 | } |
348 | #else // USE_SERIAL | 392 | #else // USE_SERIAL |
393 | // TODO: if MATRIX_COLS > 8 change to pack() | ||
349 | for (int i = 0; i < ROWS_PER_HAND; ++i) { | 394 | for (int i = 0; i < ROWS_PER_HAND; ++i) { |
350 | serial_slave_buffer[i] = matrix[offset+i]; | 395 | serial_s2m_buffer.smatrix[i] = matrix[offset+i]; |
351 | } | 396 | } |
352 | #endif | 397 | #endif |
353 | matrix_slave_scan_user(); | 398 | matrix_slave_scan_user(); |
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 | |||
10 | typedef 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 | |||
26 | extern volatile Serial_m2s_buffer_t serial_m2s_buffer; | ||
27 | |||
28 | void serial_master_init(void); | ||
29 | void serial_slave_init(void); | ||
30 | |||
31 | #endif | ||
diff --git a/quantum/split_common/serial.c b/quantum/split_common/serial.c index 13e58d825..4df8d103b 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,17 +14,19 @@ | |||
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> | ||
14 | 21 | ||
15 | #ifdef SOFT_SERIAL_PIN | 22 | #ifdef SOFT_SERIAL_PIN |
16 | 23 | ||
17 | #ifdef __AVR_ATmega32U4__ | 24 | #ifdef __AVR_ATmega32U4__ |
18 | // if using ATmega32U4 I2C, can not use PD0 and PD1 in soft serial. | 25 | // if using ATmega32U4 I2C, can not use PD0 and PD1 in soft serial. |
19 | #ifdef USE_AVR_I2C | 26 | #ifdef USE_AVR_I2C |
20 | #if SOFT_SERIAL_PIN == D0 || SOFT_SERIAL_PIN == D1 | 27 | #if SOFT_SERIAL_PIN == D0 || SOFT_SERIAL_PIN == D1 |
21 | #error Using ATmega32U4 I2C, so can not use PD0, PD1 | 28 | #error Using ATmega32U4 I2C, so can not use PD0, PD1 |
22 | #endif | 29 | #endif |
23 | #endif | 30 | #endif |
24 | 31 | ||
25 | #if SOFT_SERIAL_PIN >= D0 && SOFT_SERIAL_PIN <= D3 | 32 | #if SOFT_SERIAL_PIN >= D0 && SOFT_SERIAL_PIN <= D3 |
@@ -63,215 +70,477 @@ | |||
63 | #error serial.c now support ATmega32U4 only | 70 | #error serial.c now support ATmega32U4 only |
64 | #endif | 71 | #endif |
65 | 72 | ||
66 | // Serial pulse period in microseconds. Its probably a bad idea to lower this | 73 | #define ALWAYS_INLINE __attribute__((always_inline)) |
67 | // value. | 74 | #define NO_INLINE __attribute__((noinline)) |
68 | #define SERIAL_DELAY 24 | 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 (Experiment only) | ||
94 | // 1: about 137kbps (default) | ||
95 | // 2: about 75kbps | ||
96 | // 3: about 39kbps | ||
97 | // 4: about 26kbps | ||
98 | // 5: about 20kbps | ||
99 | #endif | ||
69 | 100 | ||
70 | uint8_t volatile serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH] = {0}; | 101 | #if __GNUC__ < 6 |
71 | uint8_t volatile serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH] = {0}; | 102 | #define TID_SEND_ADJUST 14 |
103 | #else | ||
104 | #define TID_SEND_ADJUST 2 | ||
105 | #endif | ||
72 | 106 | ||
73 | #define SLAVE_DATA_CORRUPT (1<<0) | 107 | #if SELECT_SOFT_SERIAL_SPEED == 0 |
74 | volatile uint8_t status = 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) | ||
75 | 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 | ||
178 | |||
179 | static SSTD_t *Transaction_table = NULL; | ||
180 | static uint8_t Transaction_table_size = 0; | ||
181 | |||
182 | inline static void serial_delay(void) ALWAYS_INLINE; | ||
76 | inline static | 183 | inline static |
77 | void serial_delay(void) { | 184 | void serial_delay(void) { |
78 | _delay_us(SERIAL_DELAY); | 185 | _delay_us(SERIAL_DELAY); |
79 | } | 186 | } |
80 | 187 | ||
188 | inline static void serial_delay_half1(void) ALWAYS_INLINE; | ||
189 | inline static | ||
190 | void serial_delay_half1(void) { | ||
191 | _delay_us(SERIAL_DELAY_HALF1); | ||
192 | } | ||
193 | |||
194 | inline static void serial_delay_half2(void) ALWAYS_INLINE; | ||
195 | inline static | ||
196 | void serial_delay_half2(void) { | ||
197 | _delay_us(SERIAL_DELAY_HALF2); | ||
198 | } | ||
199 | |||
200 | inline static void serial_output(void) ALWAYS_INLINE; | ||
81 | inline static | 201 | inline static |
82 | void serial_output(void) { | 202 | void serial_output(void) { |
83 | SERIAL_PIN_DDR |= SERIAL_PIN_MASK; | 203 | SERIAL_PIN_DDR |= SERIAL_PIN_MASK; |
84 | } | 204 | } |
85 | 205 | ||
86 | // make the serial pin an input with pull-up resistor | 206 | // make the serial pin an input with pull-up resistor |
207 | inline static void serial_input_with_pullup(void) ALWAYS_INLINE; | ||
87 | inline static | 208 | inline static |
88 | void serial_input(void) { | 209 | void serial_input_with_pullup(void) { |
89 | SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK; | 210 | SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK; |
90 | SERIAL_PIN_PORT |= SERIAL_PIN_MASK; | 211 | SERIAL_PIN_PORT |= SERIAL_PIN_MASK; |
91 | } | 212 | } |
92 | 213 | ||
214 | inline static uint8_t serial_read_pin(void) ALWAYS_INLINE; | ||
93 | inline static | 215 | inline static |
94 | uint8_t serial_read_pin(void) { | 216 | uint8_t serial_read_pin(void) { |
95 | return !!(SERIAL_PIN_INPUT & SERIAL_PIN_MASK); | 217 | return !!(SERIAL_PIN_INPUT & SERIAL_PIN_MASK); |
96 | } | 218 | } |
97 | 219 | ||
220 | inline static void serial_low(void) ALWAYS_INLINE; | ||
98 | inline static | 221 | inline static |
99 | void serial_low(void) { | 222 | void serial_low(void) { |
100 | SERIAL_PIN_PORT &= ~SERIAL_PIN_MASK; | 223 | SERIAL_PIN_PORT &= ~SERIAL_PIN_MASK; |
101 | } | 224 | } |
102 | 225 | ||
226 | inline static void serial_high(void) ALWAYS_INLINE; | ||
103 | inline static | 227 | inline static |
104 | void serial_high(void) { | 228 | void serial_high(void) { |
105 | SERIAL_PIN_PORT |= SERIAL_PIN_MASK; | 229 | SERIAL_PIN_PORT |= SERIAL_PIN_MASK; |
106 | } | 230 | } |
107 | 231 | ||
108 | void serial_master_init(void) { | 232 | void soft_serial_initiator_init(SSTD_t *sstd_table, int sstd_table_size) |
109 | serial_output(); | 233 | { |
110 | serial_high(); | 234 | Transaction_table = sstd_table; |
235 | Transaction_table_size = (uint8_t)sstd_table_size; | ||
236 | serial_output(); | ||
237 | serial_high(); | ||
111 | } | 238 | } |
112 | 239 | ||
113 | void serial_slave_init(void) { | 240 | void soft_serial_target_init(SSTD_t *sstd_table, int sstd_table_size) |
114 | serial_input(); | 241 | { |
115 | 242 | Transaction_table = sstd_table; | |
116 | // Enable INT0 | 243 | Transaction_table_size = (uint8_t)sstd_table_size; |
117 | EIMSK |= _BV(INT0); | 244 | serial_input_with_pullup(); |
118 | // Trigger on falling edge of INT0 | 245 | |
119 | 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 | ||
120 | } | 255 | } |
121 | 256 | ||
122 | // Used by the master to synchronize timing with the slave. | 257 | // Used by the sender to synchronize timing with the reciver. |
258 | static void sync_recv(void) NO_INLINE; | ||
123 | static | 259 | static |
124 | void sync_recv(void) { | 260 | void sync_recv(void) { |
125 | serial_input(); | 261 | for (uint8_t i = 0; i < SERIAL_DELAY*5 && serial_read_pin(); i++ ) { |
126 | // This shouldn't hang if the slave disconnects because the | 262 | } |
127 | // 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. | ||
128 | while (!serial_read_pin()); | 265 | while (!serial_read_pin()); |
129 | serial_delay(); | ||
130 | } | 266 | } |
131 | 267 | ||
132 | // 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. |
269 | static void sync_send(void) NO_INLINE; | ||
133 | static | 270 | static |
134 | void sync_send(void) { | 271 | void sync_send(void) { |
135 | serial_output(); | ||
136 | |||
137 | serial_low(); | 272 | serial_low(); |
138 | serial_delay(); | 273 | serial_delay(); |
139 | |||
140 | serial_high(); | 274 | serial_high(); |
141 | } | 275 | } |
142 | 276 | ||
143 | // Reads a byte from the serial line | 277 | // Reads a byte from the serial line |
144 | static | 278 | static uint8_t serial_read_chunk(uint8_t *pterrcount, uint8_t bit) NO_INLINE; |
145 | uint8_t serial_read_byte(void) { | 279 | static uint8_t serial_read_chunk(uint8_t *pterrcount, uint8_t bit) { |
146 | uint8_t byte = 0; | 280 | uint8_t byte, i, p, pb; |
147 | serial_input(); | 281 | |
148 | for ( uint8_t i = 0; i < 8; ++i) { | 282 | _delay_sub_us(READ_WRITE_START_ADJUST); |
149 | byte = (byte << 1) | serial_read_pin(); | 283 | for( i = 0, byte = 0, p = PARITY; i < bit; i++ ) { |
150 | serial_delay(); | 284 | serial_delay_half1(); // read the middle of pulses |
151 | _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(); | ||
152 | } | 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; | ||
153 | 300 | ||
154 | return byte; | 301 | return byte; |
155 | } | 302 | } |
156 | 303 | ||
157 | // Sends a byte with MSB ordering | 304 | // Sends a byte with MSB ordering |
158 | static | 305 | void serial_write_chunk(uint8_t data, uint8_t bit) NO_INLINE; |
159 | void serial_write_byte(uint8_t data) { | 306 | void serial_write_chunk(uint8_t data, uint8_t bit) { |
160 | uint8_t b = 8; | 307 | uint8_t b, p; |
161 | serial_output(); | 308 | for( p = PARITY, b = 1<<(bit-1); b ; b >>= 1) { |
162 | while( b-- ) { | 309 | if(data & b) { |
163 | if(data & (1 << b)) { | 310 | serial_high(); p ^= 1; |
164 | serial_high(); | 311 | } else { |
165 | } else { | 312 | serial_low(); p ^= 0; |
166 | serial_low(); | 313 | } |
314 | serial_delay(); | ||
167 | } | 315 | } |
316 | /* send parity bit */ | ||
317 | if(p & 1) { serial_high(); } | ||
318 | else { serial_low(); } | ||
168 | serial_delay(); | 319 | serial_delay(); |
169 | } | ||
170 | } | ||
171 | 320 | ||
172 | // interrupt handle to be used by the slave device | 321 | serial_low(); // sync_send() / senc_recv() need raise edge |
173 | ISR(SERIAL_PIN_INTERRUPT) { | 322 | } |
174 | sync_send(); | ||
175 | 323 | ||
176 | uint8_t checksum = 0; | 324 | static void serial_send_packet(uint8_t *buffer, uint8_t size) NO_INLINE; |
177 | for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) { | 325 | static |
178 | serial_write_byte(serial_slave_buffer[i]); | 326 | void 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]; | ||
179 | sync_send(); | 330 | sync_send(); |
180 | checksum += serial_slave_buffer[i]; | 331 | serial_write_chunk(data,8); |
181 | } | 332 | } |
182 | serial_write_byte(checksum); | 333 | } |
183 | sync_send(); | ||
184 | 334 | ||
185 | // wait for the sync to finish sending | 335 | static uint8_t serial_recive_packet(uint8_t *buffer, uint8_t size) NO_INLINE; |
186 | serial_delay(); | 336 | static |
337 | uint8_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 | } | ||
187 | 347 | ||
188 | // read the middle of pulses | 348 | inline static |
189 | _delay_us(SERIAL_DELAY/2); | 349 | void 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 | } | ||
190 | 356 | ||
191 | uint8_t checksum_computed = 0; | 357 | inline static |
192 | for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) { | 358 | void change_reciver2sender(void) { |
193 | serial_master_buffer[i] = serial_read_byte(); | 359 | sync_recv(); //0 |
194 | sync_send(); | 360 | serial_delay(); //1 |
195 | checksum_computed += serial_master_buffer[i]; | 361 | serial_low(); //3 |
362 | serial_output(); //3 | ||
363 | serial_delay_half1(); //4 | ||
364 | } | ||
365 | |||
366 | static 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 | ||
374 | ISR(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; | ||
196 | } | 390 | } |
197 | uint8_t checksum_received = serial_read_byte(); | 391 | serial_delay_half1(); |
198 | sync_send(); | ||
199 | 392 | ||
200 | 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 | ||
201 | 399 | ||
202 | if ( checksum_computed != checksum_received ) { | 400 | // target send phase |
203 | 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 | } | ||
204 | } else { | 415 | } else { |
205 | status &= ~SLAVE_DATA_CORRUPT; | 416 | *trans->status = TRANSACTION_ACCEPTED; |
206 | } | 417 | } |
207 | } | ||
208 | 418 | ||
209 | inline | 419 | sync_recv(); //weit initiator output to high |
210 | bool serial_slave_DATA_CORRUPT(void) { | ||
211 | return status & SLAVE_DATA_CORRUPT; | ||
212 | } | 420 | } |
213 | 421 | ||
214 | // Copies the serial_slave_buffer to the master and sends the | 422 | ///////// |
215 | // serial_master_buffer to the slave. | 423 | // start transaction by initiator |
424 | // | ||
425 | // int soft_serial_transaction(int sstd_index) | ||
216 | // | 426 | // |
217 | // Returns: | 427 | // Returns: |
218 | // 0 => no error | 428 | // TRANSACTION_END |
219 | // 1 => slave did not respond | 429 | // TRANSACTION_NO_RESPONSE |
220 | int serial_update_buffers(void) { | 430 | // TRANSACTION_DATA_ERROR |
221 | // 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 | ||
433 | int soft_serial_transaction(void) { | ||
434 | SSTD_t *trans = Transaction_table; | ||
435 | #else | ||
436 | int 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 | ||
222 | cli(); | 441 | cli(); |
223 | 442 | ||
224 | // signal to the slave that we want to start a transaction | 443 | // signal to the target that we want to start a transaction |
225 | serial_output(); | 444 | serial_output(); |
226 | serial_low(); | 445 | serial_low(); |
227 | _delay_us(1); | 446 | _delay_us(SLAVE_INT_WIDTH_US); |
228 | 447 | ||
229 | // wait for the slaves response | 448 | #ifndef SERIAL_USE_MULTI_TRANSACTION |
230 | serial_input(); | 449 | // wait for the target response |
231 | serial_high(); | 450 | serial_input_with_pullup(); |
232 | _delay_us(SERIAL_DELAY); | 451 | _delay_us(SLAVE_INT_RESPONSE_TIME); |
233 | 452 | ||
234 | // check if the slave is present | 453 | // check if the target is present |
235 | if (serial_read_pin()) { | 454 | if (serial_read_pin()) { |
236 | // 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; | ||
237 | sei(); | 459 | sei(); |
238 | return 1; | 460 | return TRANSACTION_NO_RESPONSE; |
239 | } | 461 | } |
240 | 462 | ||
241 | // if the slave is present syncronize with it | 463 | #else |
242 | sync_recv(); | 464 | // send transaction table index |
243 | 465 | int tid = (sstd_index<<3) | (7 & nibble_bits_count(sstd_index)); | |
244 | uint8_t checksum_computed = 0; | 466 | sync_send(); |
245 | // receive data from the slave | 467 | _delay_sub_us(TID_SEND_ADJUST); |
246 | for (int i = 0; i < SERIAL_SLAVE_BUFFER_LENGTH; ++i) { | 468 | serial_write_chunk(tid, 7); |
247 | serial_slave_buffer[i] = serial_read_byte(); | 469 | serial_delay_half1(); |
248 | sync_recv(); | 470 | |
249 | 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); | ||
250 | } | 475 | } |
251 | uint8_t checksum_received = serial_read_byte(); | ||
252 | sync_recv(); | ||
253 | 476 | ||
254 | if (checksum_computed != checksum_received) { | 477 | // check if the target is present (step2 high->low) |
255 | sei(); | 478 | for( int i = 0; serial_read_pin(); i++ ) { |
256 | 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); | ||
257 | } | 488 | } |
489 | #endif | ||
258 | 490 | ||
259 | uint8_t checksum = 0; | 491 | // initiator recive phase |
260 | // send data to the slave | 492 | // if the target is present syncronize with it |
261 | for (int i = 0; i < SERIAL_MASTER_BUFFER_LENGTH; ++i) { | 493 | if( trans->target2initiator_buffer_size > 0 ) { |
262 | serial_write_byte(serial_master_buffer[i]); | 494 | if (!serial_recive_packet((uint8_t *)trans->target2initiator_buffer, |
263 | sync_recv(); | 495 | trans->target2initiator_buffer_size) ) { |
264 | 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); | ||
265 | } | 511 | } |
266 | serial_write_byte(checksum); | ||
267 | sync_recv(); | ||
268 | 512 | ||
269 | // always, release the line when not in use | 513 | // always, release the line when not in use |
270 | serial_output(); | 514 | sync_send(); |
271 | serial_high(); | ||
272 | 515 | ||
516 | *trans->status = TRANSACTION_END; | ||
273 | sei(); | 517 | sei(); |
274 | return 0; | 518 | return TRANSACTION_END; |
275 | } | 519 | } |
276 | 520 | ||
277 | #endif /* SOFT_SERIAL_PIN */ | 521 | #ifdef SERIAL_USE_MULTI_TRANSACTION |
522 | int 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 | |||
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 0b99f352d..b6638b3bd 100644 --- a/quantum/split_common/serial.h +++ b/quantum/split_common/serial.h | |||
@@ -1,5 +1,5 @@ | |||
1 | #ifndef MY_SERIAL_H | 1 | #ifndef SOFT_SERIAL_H |
2 | #define MY_SERIAL_H | 2 | #define SOFT_SERIAL_H |
3 | 3 | ||
4 | #include <stdbool.h> | 4 | #include <stdbool.h> |
5 | 5 | ||
@@ -7,20 +7,59 @@ | |||
7 | // Need Soft Serial defines in config.h | 7 | // Need Soft Serial defines in config.h |
8 | // ///////////////////////////////////////////////////////////////// | 8 | // ///////////////////////////////////////////////////////////////// |
9 | // ex. | 9 | // ex. |
10 | // /* Configuration of lower interface with the lower layer(hardware) of serial.c */ | ||
11 | // #define SOFT_SERIAL_PIN ?? // ?? = D0,D1,D2,D3,E6 | 10 | // #define SOFT_SERIAL_PIN ?? // ?? = D0,D1,D2,D3,E6 |
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 | ||
12 | // | 17 | // |
13 | // /* Configuration of upper interface with the upper layer of serial.c */ | 18 | // //// USE simple API (using signle-type transaction function) |
14 | // #define SERIAL_SLAVE_BUFFER_LENGTH MATRIX_ROWS/2 | 19 | // /* nothing */ |
15 | // #define SERIAL_MASTER_BUFFER_LENGTH 1 | 20 | // //// USE flexible API (using multi-type transaction function) |
21 | // #define SERIAL_USE_MULTI_TRANSACTION | ||
22 | // | ||
23 | // ///////////////////////////////////////////////////////////////// | ||
16 | 24 | ||
17 | // Buffers for master - slave communication | 25 | // Soft Serial Transaction Descriptor |
18 | extern volatile uint8_t serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH]; | 26 | typedef struct _SSTD_t { |
19 | extern volatile uint8_t serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH]; | 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)) | ||
20 | 34 | ||
21 | void serial_master_init(void); | 35 | // initiator is transaction start side |
22 | void serial_slave_init(void); | 36 | void soft_serial_initiator_init(SSTD_t *sstd_table, int sstd_table_size); |
23 | int serial_update_buffers(void); | 37 | // target is interrupt accept side |
24 | bool serial_slave_data_corrupt(void); | 38 | void soft_serial_target_init(SSTD_t *sstd_table, int sstd_table_size); |
25 | 39 | ||
40 | // initiator resullt | ||
41 | #define TRANSACTION_END 0 | ||
42 | #define TRANSACTION_NO_RESPONSE 0x1 | ||
43 | #define TRANSACTION_DATA_ERROR 0x2 | ||
44 | #define TRANSACTION_TYPE_ERROR 0x4 | ||
45 | #ifndef SERIAL_USE_MULTI_TRANSACTION | ||
46 | int soft_serial_transaction(void); | ||
47 | #else | ||
48 | int soft_serial_transaction(int sstd_index); | ||
26 | #endif | 49 | #endif |
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 | ||
62 | int soft_serial_get_and_clean_status(int sstd_index); | ||
63 | #endif | ||
64 | |||
65 | #endif /* SOFT_SERIAL_H */ | ||
diff --git a/quantum/split_common/serial_backward_compatibility.h b/quantum/split_common/serial_backward_compatibility.h deleted file mode 100644 index 216b6c41f..000000000 --- a/quantum/split_common/serial_backward_compatibility.h +++ /dev/null | |||
@@ -1,11 +0,0 @@ | |||
1 | /* serial.h backward compatibility */ | ||
2 | |||
3 | // #ifndef SOFT_SERIAL_PIN | ||
4 | // #define SOFT_SERIAL_PIN D0 | ||
5 | // #endif | ||
6 | |||
7 | #ifndef SERIAL_SLAVE_BUFFER_LENGTH | ||
8 | #define SERIAL_SLAVE_BUFFER_LENGTH MATRIX_ROWS/2 | ||
9 | #define SERIAL_MASTER_BUFFER_LENGTH 1 | ||
10 | #endif | ||
11 | |||
diff --git a/quantum/split_common/split_util.c b/quantum/split_common/split_util.c index 56077e4f2..e4475c535 100644 --- a/quantum/split_common/split_util.c +++ b/quantum/split_common/split_util.c | |||
@@ -11,9 +11,6 @@ | |||
11 | #include "timer.h" | 11 | #include "timer.h" |
12 | #include "split_flags.h" | 12 | #include "split_flags.h" |
13 | 13 | ||
14 | #ifdef RGBLIGHT_ENABLE | ||
15 | # include "rgblight.h" | ||
16 | #endif | ||
17 | #ifdef BACKLIGHT_ENABLE | 14 | #ifdef BACKLIGHT_ENABLE |
18 | # include "backlight.h" | 15 | # include "backlight.h" |
19 | #endif | 16 | #endif |
@@ -24,8 +21,6 @@ | |||
24 | 21 | ||
25 | #if defined(USE_I2C) || defined(EH) | 22 | #if defined(USE_I2C) || defined(EH) |
26 | # include "i2c.h" | 23 | # include "i2c.h" |
27 | #else | ||
28 | # include "serial.h" | ||
29 | #endif | 24 | #endif |
30 | 25 | ||
31 | volatile bool isLeftHand = true; | 26 | volatile bool isLeftHand = true; |
@@ -112,7 +107,7 @@ void keyboard_slave_loop(void) { | |||
112 | BACKLIT_DIRTY = false; | 107 | BACKLIT_DIRTY = false; |
113 | } | 108 | } |
114 | #else // USE_SERIAL | 109 | #else // USE_SERIAL |
115 | backlight_set(serial_master_buffer[SERIAL_BACKLIT_START]); | 110 | backlight_set(serial_m2s_buffer.backlight_level); |
116 | #endif | 111 | #endif |
117 | #endif | 112 | #endif |
118 | // Read RGB Info | 113 | // Read RGB Info |
@@ -137,7 +132,9 @@ void keyboard_slave_loop(void) { | |||
137 | sei(); | 132 | sei(); |
138 | } | 133 | } |
139 | #else // USE_SERIAL | 134 | #else // USE_SERIAL |
135 | #ifdef RGBLIGHT_SPLIT | ||
140 | // Add serial implementation for RGB here | 136 | // Add serial implementation for RGB here |
137 | #endif | ||
141 | #endif | 138 | #endif |
142 | #endif | 139 | #endif |
143 | } | 140 | } |
diff --git a/quantum/split_common/split_util.h b/quantum/split_common/split_util.h index 45e8db0f2..d6cf3e72a 100644 --- a/quantum/split_common/split_util.h +++ b/quantum/split_common/split_util.h | |||
@@ -7,9 +7,6 @@ | |||
7 | #include <stdlib.h> | 7 | #include <stdlib.h> |
8 | #include "eeconfig.h" | 8 | #include "eeconfig.h" |
9 | 9 | ||
10 | // backlight level store index in serial_master_buffer[] for slave to read | ||
11 | #define SERIAL_BACKLIT_START 0x00 | ||
12 | |||
13 | #define SLAVE_I2C_ADDRESS 0x32 | 10 | #define SLAVE_I2C_ADDRESS 0x32 |
14 | 11 | ||
15 | extern volatile bool isLeftHand; | 12 | extern volatile bool isLeftHand; |