diff options
author | Takeshi ISHII <2170248+mtei@users.noreply.github.com> | 2018-10-23 03:38:05 +0900 |
---|---|---|
committer | Drashna Jaelre <drashna@live.com> | 2018-10-22 11:38:05 -0700 |
commit | 4665e4ffffcdfc6fe6f498928963adc64f6fefd7 (patch) | |
tree | 84965f640896bdbf28977eb98abeeded509a614d | |
parent | fbc6bd82664f7286e358e14da6c7945aa061ee3b (diff) | |
download | qmk_firmware-4665e4ffffcdfc6fe6f498928963adc64f6fefd7.tar.gz qmk_firmware-4665e4ffffcdfc6fe6f498928963adc64f6fefd7.zip |
Keyboard: Helix serial.c, split_scom.c bug fix and update (#4191)
* helix/serial.c add support PD1,PD3,PE6 and configuration simplify
* Add comment about ATmega32U4 I2C
* Add compile time check for ATmega32U4 I2C
* change TAB code to 8 SPACE code
* Helix serial.c PORTD,PD0 test. OK
OK PD0
- PD1
OK PD2
- PD3
- PE6
* Helix serial.c PORTD,PD1 test. OK
OK PD0
OK PD1
OK PD2
- PD3
- PE6
* Helix serial.c PORTD,PD3 test. OK
OK PD0
OK PD1
OK PD2
OK PD3
- PE6
* Helix serial.c PORTE,PD6 test. OK
OK PD0
OK PD1
OK PD2
OK PD3
OK PE6
* Helix serial.c: PD0,PD1,PD3,PE6 all test end
* Helix serial.c: rename SOFT_SERIAL_PORT to SOFT_SERIAL_PIN
* Helix serial.c add debug code
* Helix serial.c: add transaction ID range check
* Helix serial.c debug code update
* Helix serial.c debug code update
* Helix serial.c: Strict checking of the value of tid.
* adjust the delay of serial.c
* Helix serial.c: remove debug code
* remove EXTRAFLAGS += -DCONSOLE_ENABLE from five_rows/rules.mk
tmk_core/common.mk has
>ifeq ($(strip $(CONSOLE_ENABLE)), yes)
> TMK_COMMON_DEFS += -DCONSOLE_ENABLE
* Fix error handling in split_scomm.c
* add some comment to serial.c
* add some comment about SELECT_SOFT_SERIAL_SPEED
-rw-r--r-- | keyboards/helix/pico/serial_config.h | 15 | ||||
-rw-r--r-- | keyboards/helix/rev1/keymaps/OLED_sample/serial_config.h | 15 | ||||
-rw-r--r-- | keyboards/helix/rev1/serial_config.h | 15 | ||||
-rw-r--r-- | keyboards/helix/rev2/keymaps/five_rows/rules.mk | 4 | ||||
-rw-r--r-- | keyboards/helix/rev2/serial_config.h | 15 | ||||
-rw-r--r-- | keyboards/helix/rev2/split_scomm.c | 48 | ||||
-rw-r--r-- | keyboards/helix/serial.c | 228 | ||||
-rw-r--r-- | keyboards/helix/serial.h | 22 |
8 files changed, 227 insertions, 135 deletions
diff --git a/keyboards/helix/pico/serial_config.h b/keyboards/helix/pico/serial_config.h index 82c6e4e83..fc8736d47 100644 --- a/keyboards/helix/pico/serial_config.h +++ b/keyboards/helix/pico/serial_config.h | |||
@@ -1,16 +1,9 @@ | |||
1 | #ifndef SOFT_SERIAL_CONFIG_H | 1 | //// #error rev2 serial config |
2 | #define SOFT_SERIAL_CONFIG_H | ||
3 | 2 | ||
3 | #ifndef SOFT_SERIAL_PIN | ||
4 | /* Soft Serial defines */ | 4 | /* Soft Serial defines */ |
5 | #define SERIAL_PIN_DDR DDRD | 5 | #define SOFT_SERIAL_PIN D2 |
6 | #define SERIAL_PIN_PORT PORTD | ||
7 | #define SERIAL_PIN_INPUT PIND | ||
8 | #define SERIAL_PIN_MASK _BV(PD2) | ||
9 | #define SERIAL_PIN_INTERRUPT INT2_vect | ||
10 | 6 | ||
11 | #define SERIAL_SLAVE_BUFFER_LENGTH MATRIX_ROWS/2 | 7 | #define SERIAL_SLAVE_BUFFER_LENGTH MATRIX_ROWS/2 |
12 | #define SERIAL_MASTER_BUFFER_LENGTH MATRIX_ROWS/2 | 8 | #define SERIAL_MASTER_BUFFER_LENGTH MATRIX_ROWS/2 |
13 | 9 | #endif | |
14 | //// #error rev2 serial config | ||
15 | |||
16 | #endif /* SOFT_SERIAL_CONFIG_H */ | ||
diff --git a/keyboards/helix/rev1/keymaps/OLED_sample/serial_config.h b/keyboards/helix/rev1/keymaps/OLED_sample/serial_config.h index b991b874b..f56951e70 100644 --- a/keyboards/helix/rev1/keymaps/OLED_sample/serial_config.h +++ b/keyboards/helix/rev1/keymaps/OLED_sample/serial_config.h | |||
@@ -1,16 +1,9 @@ | |||
1 | #ifndef SOFT_SERIAL_CONFIG_H | 1 | //// #error rev1/keymaps/OLED_sample serial config |
2 | #define SOFT_SERIAL_CONFIG_H | ||
3 | 2 | ||
3 | #ifndef SOFT_SERIAL_PIN | ||
4 | /* Soft Serial defines */ | 4 | /* Soft Serial defines */ |
5 | #define SERIAL_PIN_DDR DDRD | 5 | #define SOFT_SERIAL_PIN D2 |
6 | #define SERIAL_PIN_PORT PORTD | ||
7 | #define SERIAL_PIN_INPUT PIND | ||
8 | #define SERIAL_PIN_MASK _BV(PD2) | ||
9 | #define SERIAL_PIN_INTERRUPT INT2_vect | ||
10 | 6 | ||
11 | #define SERIAL_SLAVE_BUFFER_LENGTH MATRIX_ROWS/2 | 7 | #define SERIAL_SLAVE_BUFFER_LENGTH MATRIX_ROWS/2 |
12 | #define SERIAL_MASTER_BUFFER_LENGTH 0 | 8 | #define SERIAL_MASTER_BUFFER_LENGTH 0 |
13 | 9 | #endif | |
14 | //// #error rev1/keymaps/OLED_sample serial config | ||
15 | |||
16 | #endif /* SOFT_SERIAL_CONFIG_H */ | ||
diff --git a/keyboards/helix/rev1/serial_config.h b/keyboards/helix/rev1/serial_config.h index 51c6aa375..32218f9bb 100644 --- a/keyboards/helix/rev1/serial_config.h +++ b/keyboards/helix/rev1/serial_config.h | |||
@@ -1,16 +1,9 @@ | |||
1 | #ifndef SOFT_SERIAL_CONFIG_H | 1 | /// #error rev1 serial config |
2 | #define SOFT_SERIAL_CONFIG_H | ||
3 | 2 | ||
3 | #ifndef SOFT_SERIAL_PIN | ||
4 | /* Soft Serial defines */ | 4 | /* Soft Serial defines */ |
5 | #define SERIAL_PIN_DDR DDRD | 5 | #define SOFT_SERIAL_PIN D0 |
6 | #define SERIAL_PIN_PORT PORTD | ||
7 | #define SERIAL_PIN_INPUT PIND | ||
8 | #define SERIAL_PIN_MASK _BV(PD0) | ||
9 | #define SERIAL_PIN_INTERRUPT INT0_vect | ||
10 | 6 | ||
11 | #define SERIAL_SLAVE_BUFFER_LENGTH MATRIX_ROWS/2 | 7 | #define SERIAL_SLAVE_BUFFER_LENGTH MATRIX_ROWS/2 |
12 | #define SERIAL_MASTER_BUFFER_LENGTH 0 | 8 | #define SERIAL_MASTER_BUFFER_LENGTH 0 |
13 | 9 | #endif | |
14 | /// #error rev1 serial config | ||
15 | |||
16 | #endif /* SOFT_SERIAL_CONFIG_H */ | ||
diff --git a/keyboards/helix/rev2/keymaps/five_rows/rules.mk b/keyboards/helix/rev2/keymaps/five_rows/rules.mk index 63106df76..f771c94c9 100644 --- a/keyboards/helix/rev2/keymaps/five_rows/rules.mk +++ b/keyboards/helix/rev2/keymaps/five_rows/rules.mk | |||
@@ -122,10 +122,6 @@ ifeq ($(strip $(Link_Time_Optimization)),yes) | |||
122 | EXTRAFLAGS += -flto -DUSE_Link_Time_Optimization | 122 | EXTRAFLAGS += -flto -DUSE_Link_Time_Optimization |
123 | endif | 123 | endif |
124 | 124 | ||
125 | ifeq ($(strip $(CONSOLE_ENABLE)),yes) | ||
126 | EXTRAFLAGS += -DCONSOLE_ENABLE | ||
127 | endif | ||
128 | |||
129 | # Do not enable SLEEP_LED_ENABLE. it uses the same timer as BACKLIGHT_ENABLE | 125 | # Do not enable SLEEP_LED_ENABLE. it uses the same timer as BACKLIGHT_ENABLE |
130 | SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend | 126 | SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend |
131 | 127 | ||
diff --git a/keyboards/helix/rev2/serial_config.h b/keyboards/helix/rev2/serial_config.h index 8d7e62837..37135213d 100644 --- a/keyboards/helix/rev2/serial_config.h +++ b/keyboards/helix/rev2/serial_config.h | |||
@@ -1,15 +1,8 @@ | |||
1 | #ifndef SOFT_SERIAL_CONFIG_H | 1 | //// #error rev2 serial config |
2 | #define SOFT_SERIAL_CONFIG_H | ||
3 | 2 | ||
3 | #ifndef SOFT_SERIAL_PIN | ||
4 | /* Soft Serial defines */ | 4 | /* Soft Serial defines */ |
5 | #define SERIAL_PIN_DDR DDRD | 5 | #define SOFT_SERIAL_PIN D2 |
6 | #define SERIAL_PIN_PORT PORTD | ||
7 | #define SERIAL_PIN_INPUT PIND | ||
8 | #define SERIAL_PIN_MASK _BV(PD2) | ||
9 | #define SERIAL_PIN_INTERRUPT INT2_vect | ||
10 | 6 | ||
11 | #define SERIAL_USE_MULTI_TRANSACTION | 7 | #define SERIAL_USE_MULTI_TRANSACTION |
12 | 8 | #endif | |
13 | //// #error rev2 serial config | ||
14 | |||
15 | #endif /* SOFT_SERIAL_CONFIG_H */ | ||
diff --git a/keyboards/helix/rev2/split_scomm.c b/keyboards/helix/rev2/split_scomm.c index 9719eb22e..50d233ce9 100644 --- a/keyboards/helix/rev2/split_scomm.c +++ b/keyboards/helix/rev2/split_scomm.c | |||
@@ -10,6 +10,9 @@ | |||
10 | #ifdef SERIAL_DEBUG_MODE | 10 | #ifdef SERIAL_DEBUG_MODE |
11 | #include <avr/io.h> | 11 | #include <avr/io.h> |
12 | #endif | 12 | #endif |
13 | #ifdef CONSOLE_ENABLE | ||
14 | #include <print.h> | ||
15 | #endif | ||
13 | 16 | ||
14 | uint8_t volatile serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH] = {0}; | 17 | uint8_t volatile serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH] = {0}; |
15 | uint8_t volatile serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH] = {0}; | 18 | uint8_t volatile serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH] = {0}; |
@@ -17,6 +20,7 @@ uint8_t volatile status_com = 0; | |||
17 | uint8_t volatile status1 = 0; | 20 | uint8_t volatile status1 = 0; |
18 | uint8_t slave_buffer_change_count = 0; | 21 | uint8_t slave_buffer_change_count = 0; |
19 | uint8_t s_change_old = 0xff; | 22 | uint8_t s_change_old = 0xff; |
23 | uint8_t s_change_new = 0xff; | ||
20 | 24 | ||
21 | SSTD_t transactions[] = { | 25 | SSTD_t transactions[] = { |
22 | #define GET_SLAVE_STATUS 0 | 26 | #define GET_SLAVE_STATUS 0 |
@@ -41,12 +45,12 @@ SSTD_t transactions[] = { | |||
41 | 45 | ||
42 | void serial_master_init(void) | 46 | void serial_master_init(void) |
43 | { | 47 | { |
44 | soft_serial_initiator_init(transactions); | 48 | soft_serial_initiator_init(transactions, TID_LIMIT(transactions)); |
45 | } | 49 | } |
46 | 50 | ||
47 | void serial_slave_init(void) | 51 | void serial_slave_init(void) |
48 | { | 52 | { |
49 | soft_serial_target_init(transactions); | 53 | soft_serial_target_init(transactions, TID_LIMIT(transactions)); |
50 | } | 54 | } |
51 | 55 | ||
52 | // 0 => no error | 56 | // 0 => no error |
@@ -54,19 +58,37 @@ void serial_slave_init(void) | |||
54 | // 2 => checksum error | 58 | // 2 => checksum error |
55 | int serial_update_buffers(int master_update) | 59 | int serial_update_buffers(int master_update) |
56 | { | 60 | { |
57 | int status; | 61 | int status, smatstatus; |
58 | static int need_retry = 0; | 62 | static int need_retry = 0; |
59 | if( s_change_old != slave_buffer_change_count ) { | 63 | |
60 | status = soft_serial_transaction(GET_SLAVE_BUFFER); | 64 | if( s_change_old != s_change_new ) { |
61 | if( status == TRANSACTION_END ) | 65 | smatstatus = soft_serial_transaction(GET_SLAVE_BUFFER); |
62 | s_change_old = slave_buffer_change_count; | 66 | if( smatstatus == TRANSACTION_END ) { |
67 | s_change_old = s_change_new; | ||
68 | #ifdef CONSOLE_ENABLE | ||
69 | uprintf("slave matrix = %b %b %b %b %b\n", | ||
70 | serial_slave_buffer[0], serial_slave_buffer[1], | ||
71 | serial_slave_buffer[2], serial_slave_buffer[3], | ||
72 | serial_slave_buffer[4] ); | ||
73 | #endif | ||
74 | } | ||
75 | } else { | ||
76 | // serial_slave_buffer dosen't change | ||
77 | smatstatus = TRANSACTION_END; // dummy status | ||
78 | } | ||
79 | |||
80 | if( !master_update && !need_retry) { | ||
81 | status = soft_serial_transaction(GET_SLAVE_STATUS); | ||
82 | } else { | ||
83 | status = soft_serial_transaction(PUT_MASTER_GET_SLAVE_STATUS); | ||
84 | } | ||
85 | if( status == TRANSACTION_END ) { | ||
86 | s_change_new = slave_buffer_change_count; | ||
87 | need_retry = 0; | ||
88 | } else { | ||
89 | need_retry = 1; | ||
63 | } | 90 | } |
64 | if( !master_update && !need_retry) | 91 | return smatstatus; |
65 | status = soft_serial_transaction(GET_SLAVE_STATUS); | ||
66 | else | ||
67 | status = soft_serial_transaction(PUT_MASTER_GET_SLAVE_STATUS); | ||
68 | need_retry = ( status == TRANSACTION_END ) ? 0 : 1; | ||
69 | return status; | ||
70 | } | 92 | } |
71 | 93 | ||
72 | #endif // SERIAL_USE_MULTI_TRANSACTION | 94 | #endif // SERIAL_USE_MULTI_TRANSACTION |
diff --git a/keyboards/helix/serial.c b/keyboards/helix/serial.c index 11ceff0b3..830f86b55 100644 --- a/keyboards/helix/serial.c +++ b/keyboards/helix/serial.c | |||
@@ -14,7 +14,56 @@ | |||
14 | #include "serial.h" | 14 | #include "serial.h" |
15 | //#include <pro_micro.h> | 15 | //#include <pro_micro.h> |
16 | 16 | ||
17 | #ifdef USE_SERIAL | 17 | #ifdef SOFT_SERIAL_PIN |
18 | |||
19 | #ifdef __AVR_ATmega32U4__ | ||
20 | // if using ATmega32U4 I2C, can not use PD0 and PD1 in soft serial. | ||
21 | #ifdef USE_I2C | ||
22 | #if SOFT_SERIAL_PIN == D0 || SOFT_SERIAL_PIN == D1 | ||
23 | #error Using ATmega32U4 I2C, so can not use PD0, PD1 | ||
24 | #endif | ||
25 | #endif | ||
26 | |||
27 | #if SOFT_SERIAL_PIN >= D0 && SOFT_SERIAL_PIN <= D3 | ||
28 | #define SERIAL_PIN_DDR DDRD | ||
29 | #define SERIAL_PIN_PORT PORTD | ||
30 | #define SERIAL_PIN_INPUT PIND | ||
31 | #if SOFT_SERIAL_PIN == D0 | ||
32 | #define SERIAL_PIN_MASK _BV(PD0) | ||
33 | #define EIMSK_BIT _BV(INT0) | ||
34 | #define EICRx_BIT (~(_BV(ISC00) | _BV(ISC01))) | ||
35 | #define SERIAL_PIN_INTERRUPT INT0_vect | ||
36 | #elif SOFT_SERIAL_PIN == D1 | ||
37 | #define SERIAL_PIN_MASK _BV(PD1) | ||
38 | #define EIMSK_BIT _BV(INT1) | ||
39 | #define EICRx_BIT (~(_BV(ISC10) | _BV(ISC11))) | ||
40 | #define SERIAL_PIN_INTERRUPT INT1_vect | ||
41 | #elif SOFT_SERIAL_PIN == D2 | ||
42 | #define SERIAL_PIN_MASK _BV(PD2) | ||
43 | #define EIMSK_BIT _BV(INT2) | ||
44 | #define EICRx_BIT (~(_BV(ISC20) | _BV(ISC21))) | ||
45 | #define SERIAL_PIN_INTERRUPT INT2_vect | ||
46 | #elif SOFT_SERIAL_PIN == D3 | ||
47 | #define SERIAL_PIN_MASK _BV(PD3) | ||
48 | #define EIMSK_BIT _BV(INT3) | ||
49 | #define EICRx_BIT (~(_BV(ISC30) | _BV(ISC31))) | ||
50 | #define SERIAL_PIN_INTERRUPT INT3_vect | ||
51 | #endif | ||
52 | #elif SOFT_SERIAL_PIN == E6 | ||
53 | #define SERIAL_PIN_DDR DDRE | ||
54 | #define SERIAL_PIN_PORT PORTE | ||
55 | #define SERIAL_PIN_INPUT PINE | ||
56 | #define SERIAL_PIN_MASK _BV(PE6) | ||
57 | #define EIMSK_BIT _BV(INT6) | ||
58 | #define EICRx_BIT (~(_BV(ISC60) | _BV(ISC61))) | ||
59 | #define SERIAL_PIN_INTERRUPT INT6_vect | ||
60 | #else | ||
61 | #error invalid SOFT_SERIAL_PIN value | ||
62 | #endif | ||
63 | |||
64 | #else | ||
65 | #error serial.c now support ATmega32U4 only | ||
66 | #endif | ||
18 | 67 | ||
19 | #ifndef SERIAL_USE_MULTI_TRANSACTION | 68 | #ifndef SERIAL_USE_MULTI_TRANSACTION |
20 | /* --- USE Simple API (OLD API, compatible with let's split serial.c) */ | 69 | /* --- USE Simple API (OLD API, compatible with let's split serial.c) */ |
@@ -42,16 +91,20 @@ SSTD_t transactions[] = { | |||
42 | }; | 91 | }; |
43 | 92 | ||
44 | void serial_master_init(void) | 93 | void serial_master_init(void) |
45 | { soft_serial_initiator_init(transactions); } | 94 | { soft_serial_initiator_init(transactions, TID_LIMIT(transactions)); } |
46 | 95 | ||
47 | void serial_slave_init(void) | 96 | void serial_slave_init(void) |
48 | { soft_serial_target_init(transactions); } | 97 | { soft_serial_target_init(transactions, TID_LIMIT(transactions)); } |
49 | 98 | ||
50 | // 0 => no error | 99 | // 0 => no error |
51 | // 1 => slave did not respond | 100 | // 1 => slave did not respond |
52 | // 2 => checksum error | 101 | // 2 => checksum error |
53 | int serial_update_buffers() | 102 | int serial_update_buffers() |
54 | { return soft_serial_transaction(); } | 103 | { |
104 | int result; | ||
105 | result = soft_serial_transaction(); | ||
106 | return result; | ||
107 | } | ||
55 | 108 | ||
56 | #endif // Simple API (OLD API, compatible with let's split serial.c) | 109 | #endif // Simple API (OLD API, compatible with let's split serial.c) |
57 | 110 | ||
@@ -59,39 +112,66 @@ int serial_update_buffers() | |||
59 | #define NO_INLINE __attribute__((noinline)) | 112 | #define NO_INLINE __attribute__((noinline)) |
60 | #define _delay_sub_us(x) __builtin_avr_delay_cycles(x) | 113 | #define _delay_sub_us(x) __builtin_avr_delay_cycles(x) |
61 | 114 | ||
62 | // Serial pulse period in microseconds. | 115 | // parity check |
63 | #define TID_SEND_ADJUST 14 | 116 | #define ODD_PARITY 1 |
117 | #define EVEN_PARITY 0 | ||
118 | #define PARITY EVEN_PARITY | ||
119 | |||
120 | #ifdef SERIAL_DELAY | ||
121 | // custom setup in config.h | ||
122 | // #define TID_SEND_ADJUST 2 | ||
123 | // #define SERIAL_DELAY 6 // micro sec | ||
124 | // #define READ_WRITE_START_ADJUST 30 // cycles | ||
125 | // #define READ_WRITE_WIDTH_ADJUST 8 // cycles | ||
126 | #else | ||
127 | // ============ Standard setups ============ | ||
128 | |||
129 | #ifndef SELECT_SOFT_SERIAL_SPEED | ||
130 | #define SELECT_SOFT_SERIAL_SPEED 1 | ||
131 | // 0: about 189kbps | ||
132 | // 1: about 137kbps (default) | ||
133 | // 2: about 75kbps | ||
134 | // 3: about 39kbps | ||
135 | // 4: about 26kbps | ||
136 | // 5: about 20kbps | ||
137 | #endif | ||
138 | |||
139 | #define TID_SEND_ADJUST 2 | ||
64 | 140 | ||
65 | #define SELECT_SERIAL_SPEED 1 | 141 | #if SELECT_SOFT_SERIAL_SPEED == 0 |
66 | #if SELECT_SERIAL_SPEED == 0 | ||
67 | // Very High speed | 142 | // Very High speed |
68 | #define SERIAL_DELAY 4 // micro sec | 143 | #define SERIAL_DELAY 4 // micro sec |
69 | #define READ_WRITE_START_ADJUST 33 // cycles | 144 | #define READ_WRITE_START_ADJUST 33 // cycles |
70 | #define READ_WRITE_WIDTH_ADJUST 3 // cycles | 145 | #define READ_WRITE_WIDTH_ADJUST 6 // cycles |
71 | #elif SELECT_SERIAL_SPEED == 1 | 146 | #elif SELECT_SOFT_SERIAL_SPEED == 1 |
72 | // High speed | 147 | // High speed |
73 | #define SERIAL_DELAY 6 // micro sec | 148 | #define SERIAL_DELAY 6 // micro sec |
74 | #define READ_WRITE_START_ADJUST 30 // cycles | 149 | #define READ_WRITE_START_ADJUST 30 // cycles |
75 | #define READ_WRITE_WIDTH_ADJUST 3 // cycles | 150 | #define READ_WRITE_WIDTH_ADJUST 7 // cycles |
76 | #elif SELECT_SERIAL_SPEED == 2 | 151 | #elif SELECT_SOFT_SERIAL_SPEED == 2 |
77 | // Middle speed | 152 | // Middle speed |
78 | #define SERIAL_DELAY 12 // micro sec | 153 | #define SERIAL_DELAY 12 // micro sec |
79 | #define READ_WRITE_START_ADJUST 30 // cycles | 154 | #define READ_WRITE_START_ADJUST 30 // cycles |
80 | #define READ_WRITE_WIDTH_ADJUST 3 // cycles | 155 | #define READ_WRITE_WIDTH_ADJUST 7 // cycles |
81 | #elif SELECT_SERIAL_SPEED == 3 | 156 | #elif SELECT_SOFT_SERIAL_SPEED == 3 |
82 | // Low speed | 157 | // Low speed |
83 | #define SERIAL_DELAY 24 // micro sec | 158 | #define SERIAL_DELAY 24 // micro sec |
84 | #define READ_WRITE_START_ADJUST 30 // cycles | 159 | #define READ_WRITE_START_ADJUST 30 // cycles |
85 | #define READ_WRITE_WIDTH_ADJUST 3 // cycles | 160 | #define READ_WRITE_WIDTH_ADJUST 7 // cycles |
86 | #elif SELECT_SERIAL_SPEED == 4 | 161 | #elif SELECT_SOFT_SERIAL_SPEED == 4 |
87 | // Very Low speed | 162 | // Very Low speed |
88 | #define SERIAL_DELAY 50 // micro sec | 163 | #define SERIAL_DELAY 36 // micro sec |
164 | #define READ_WRITE_START_ADJUST 30 // cycles | ||
165 | #define READ_WRITE_WIDTH_ADJUST 7 // cycles | ||
166 | #elif SELECT_SOFT_SERIAL_SPEED == 5 | ||
167 | // Ultra Low speed | ||
168 | #define SERIAL_DELAY 48 // micro sec | ||
89 | #define READ_WRITE_START_ADJUST 30 // cycles | 169 | #define READ_WRITE_START_ADJUST 30 // cycles |
90 | #define READ_WRITE_WIDTH_ADJUST 3 // cycles | 170 | #define READ_WRITE_WIDTH_ADJUST 7 // cycles |
91 | #else | 171 | #else |
92 | #error Illegal Serial Speed | 172 | #error invalid SELECT_SOFT_SERIAL_SPEED value |
93 | #endif | 173 | #endif /* SELECT_SOFT_SERIAL_SPEED */ |
94 | 174 | #endif /* SERIAL_DELAY */ | |
95 | 175 | ||
96 | #define SERIAL_DELAY_HALF1 (SERIAL_DELAY/2) | 176 | #define SERIAL_DELAY_HALF1 (SERIAL_DELAY/2) |
97 | #define SERIAL_DELAY_HALF2 (SERIAL_DELAY - SERIAL_DELAY/2) | 177 | #define SERIAL_DELAY_HALF2 (SERIAL_DELAY - SERIAL_DELAY/2) |
@@ -105,6 +185,7 @@ int serial_update_buffers() | |||
105 | #endif | 185 | #endif |
106 | 186 | ||
107 | static SSTD_t *Transaction_table = NULL; | 187 | static SSTD_t *Transaction_table = NULL; |
188 | static uint8_t Transaction_table_size = 0; | ||
108 | 189 | ||
109 | inline static | 190 | inline static |
110 | void serial_delay(void) { | 191 | void serial_delay(void) { |
@@ -152,30 +233,28 @@ void serial_high(void) { | |||
152 | SERIAL_PIN_PORT |= SERIAL_PIN_MASK; | 233 | SERIAL_PIN_PORT |= SERIAL_PIN_MASK; |
153 | } | 234 | } |
154 | 235 | ||
155 | void soft_serial_initiator_init(SSTD_t *sstd_table) | 236 | void soft_serial_initiator_init(SSTD_t *sstd_table, int sstd_table_size) |
156 | { | 237 | { |
157 | Transaction_table = sstd_table; | 238 | Transaction_table = sstd_table; |
239 | Transaction_table_size = (uint8_t)sstd_table_size; | ||
158 | serial_output(); | 240 | serial_output(); |
159 | serial_high(); | 241 | serial_high(); |
160 | } | 242 | } |
161 | 243 | ||
162 | void soft_serial_target_init(SSTD_t *sstd_table) | 244 | void soft_serial_target_init(SSTD_t *sstd_table, int sstd_table_size) |
163 | { | 245 | { |
164 | Transaction_table = sstd_table; | 246 | Transaction_table = sstd_table; |
247 | Transaction_table_size = (uint8_t)sstd_table_size; | ||
165 | serial_input_with_pullup(); | 248 | serial_input_with_pullup(); |
166 | 249 | ||
167 | #if SERIAL_PIN_MASK == _BV(PD0) | 250 | // Enable INT0-INT3,INT6 |
168 | // Enable INT0 | 251 | EIMSK |= EIMSK_BIT; |
169 | EIMSK |= _BV(INT0); | 252 | #if SERIAL_PIN_MASK == _BV(PE6) |
170 | // Trigger on falling edge of INT0 | 253 | // Trigger on falling edge of INT6 |
171 | EICRA &= ~(_BV(ISC00) | _BV(ISC01)); | 254 | EICRB &= EICRx_BIT; |
172 | #elif SERIAL_PIN_MASK == _BV(PD2) | ||
173 | // Enable INT2 | ||
174 | EIMSK |= _BV(INT2); | ||
175 | // Trigger on falling edge of INT2 | ||
176 | EICRA &= ~(_BV(ISC20) | _BV(ISC21)); | ||
177 | #else | 255 | #else |
178 | #error unknown SERIAL_PIN_MASK value | 256 | // Trigger on falling edge of INT0-INT3 |
257 | EICRA &= EICRx_BIT; | ||
179 | #endif | 258 | #endif |
180 | } | 259 | } |
181 | 260 | ||
@@ -205,12 +284,12 @@ static uint8_t serial_read_chunk(uint8_t *pterrcount, uint8_t bit) { | |||
205 | uint8_t byte, i, p, pb; | 284 | uint8_t byte, i, p, pb; |
206 | 285 | ||
207 | _delay_sub_us(READ_WRITE_START_ADJUST); | 286 | _delay_sub_us(READ_WRITE_START_ADJUST); |
208 | for( i = 0, byte = 0, p = 0; i < bit; i++ ) { | 287 | for( i = 0, byte = 0, p = PARITY; i < bit; i++ ) { |
209 | serial_delay_half1(); // read the middle of pulses | 288 | serial_delay_half1(); // read the middle of pulses |
210 | if( serial_read_pin() ) { | 289 | if( serial_read_pin() ) { |
211 | byte = (byte << 1) | 1; p ^= 1; | 290 | byte = (byte << 1) | 1; p ^= 1; |
212 | } else { | 291 | } else { |
213 | byte = (byte << 1) | 0; p ^= 0; | 292 | byte = (byte << 1) | 0; p ^= 0; |
214 | } | 293 | } |
215 | _delay_sub_us(READ_WRITE_WIDTH_ADJUST); | 294 | _delay_sub_us(READ_WRITE_WIDTH_ADJUST); |
216 | serial_delay_half2(); | 295 | serial_delay_half2(); |
@@ -230,13 +309,13 @@ static uint8_t serial_read_chunk(uint8_t *pterrcount, uint8_t bit) { | |||
230 | void serial_write_chunk(uint8_t data, uint8_t bit) NO_INLINE; | 309 | void serial_write_chunk(uint8_t data, uint8_t bit) NO_INLINE; |
231 | void serial_write_chunk(uint8_t data, uint8_t bit) { | 310 | void serial_write_chunk(uint8_t data, uint8_t bit) { |
232 | uint8_t b, p; | 311 | uint8_t b, p; |
233 | for( p = 0, b = 1<<(bit-1); b ; b >>= 1) { | 312 | for( p = PARITY, b = 1<<(bit-1); b ; b >>= 1) { |
234 | if(data & b) { | 313 | if(data & b) { |
235 | serial_high(); p ^= 1; | 314 | serial_high(); p ^= 1; |
236 | } else { | 315 | } else { |
237 | serial_low(); p ^= 0; | 316 | serial_low(); p ^= 0; |
238 | } | 317 | } |
239 | serial_delay(); | 318 | serial_delay(); |
240 | } | 319 | } |
241 | /* send parity bit */ | 320 | /* send parity bit */ |
242 | if(p & 1) { serial_high(); } | 321 | if(p & 1) { serial_high(); } |
@@ -288,6 +367,13 @@ void change_reciver2sender(void) { | |||
288 | serial_delay_half1(); //4 | 367 | serial_delay_half1(); //4 |
289 | } | 368 | } |
290 | 369 | ||
370 | static inline uint8_t nibble_bits_count(uint8_t bits) | ||
371 | { | ||
372 | bits = (bits & 0x5) + (bits >> 1 & 0x5); | ||
373 | bits = (bits & 0x3) + (bits >> 2 & 0x3); | ||
374 | return bits; | ||
375 | } | ||
376 | |||
291 | // interrupt handle to be used by the target device | 377 | // interrupt handle to be used by the target device |
292 | ISR(SERIAL_PIN_INTERRUPT) { | 378 | ISR(SERIAL_PIN_INTERRUPT) { |
293 | 379 | ||
@@ -297,12 +383,15 @@ ISR(SERIAL_PIN_INTERRUPT) { | |||
297 | SSTD_t *trans = Transaction_table; | 383 | SSTD_t *trans = Transaction_table; |
298 | #else | 384 | #else |
299 | // recive transaction table index | 385 | // recive transaction table index |
300 | uint8_t tid; | 386 | uint8_t tid, bits; |
301 | uint8_t pecount = 0; | 387 | uint8_t pecount = 0; |
302 | sync_recv(); | 388 | sync_recv(); |
303 | tid = serial_read_chunk(&pecount,4); | 389 | bits = serial_read_chunk(&pecount,7); |
304 | if(pecount> 0) | 390 | tid = bits>>3; |
391 | bits = (bits&7) != nibble_bits_count(tid); | ||
392 | if( bits || pecount> 0 || tid > Transaction_table_size ) { | ||
305 | return; | 393 | return; |
394 | } | ||
306 | serial_delay_half1(); | 395 | serial_delay_half1(); |
307 | 396 | ||
308 | serial_high(); // response step1 low->high | 397 | serial_high(); // response step1 low->high |
@@ -315,17 +404,17 @@ ISR(SERIAL_PIN_INTERRUPT) { | |||
315 | // target send phase | 404 | // target send phase |
316 | if( trans->target2initiator_buffer_size > 0 ) | 405 | if( trans->target2initiator_buffer_size > 0 ) |
317 | serial_send_packet((uint8_t *)trans->target2initiator_buffer, | 406 | serial_send_packet((uint8_t *)trans->target2initiator_buffer, |
318 | trans->target2initiator_buffer_size); | 407 | trans->target2initiator_buffer_size); |
319 | // target switch to input | 408 | // target switch to input |
320 | change_sender2reciver(); | 409 | change_sender2reciver(); |
321 | 410 | ||
322 | // target recive phase | 411 | // target recive phase |
323 | if( trans->initiator2target_buffer_size > 0 ) { | 412 | if( trans->initiator2target_buffer_size > 0 ) { |
324 | if (serial_recive_packet((uint8_t *)trans->initiator2target_buffer, | 413 | if (serial_recive_packet((uint8_t *)trans->initiator2target_buffer, |
325 | trans->initiator2target_buffer_size) ) { | 414 | trans->initiator2target_buffer_size) ) { |
326 | *trans->status = TRANSACTION_ACCEPTED; | 415 | *trans->status = TRANSACTION_ACCEPTED; |
327 | } else { | 416 | } else { |
328 | *trans->status = TRANSACTION_DATA_ERROR; | 417 | *trans->status = TRANSACTION_DATA_ERROR; |
329 | } | 418 | } |
330 | } else { | 419 | } else { |
331 | *trans->status = TRANSACTION_ACCEPTED; | 420 | *trans->status = TRANSACTION_ACCEPTED; |
@@ -349,6 +438,8 @@ int soft_serial_transaction(void) { | |||
349 | SSTD_t *trans = Transaction_table; | 438 | SSTD_t *trans = Transaction_table; |
350 | #else | 439 | #else |
351 | int soft_serial_transaction(int sstd_index) { | 440 | int soft_serial_transaction(int sstd_index) { |
441 | if( sstd_index > Transaction_table_size ) | ||
442 | return TRANSACTION_TYPE_ERROR; | ||
352 | SSTD_t *trans = &Transaction_table[sstd_index]; | 443 | SSTD_t *trans = &Transaction_table[sstd_index]; |
353 | #endif | 444 | #endif |
354 | cli(); | 445 | cli(); |
@@ -375,9 +466,10 @@ int soft_serial_transaction(int sstd_index) { | |||
375 | 466 | ||
376 | #else | 467 | #else |
377 | // send transaction table index | 468 | // send transaction table index |
469 | int tid = (sstd_index<<3) | (7 & nibble_bits_count(sstd_index)); | ||
378 | sync_send(); | 470 | sync_send(); |
379 | _delay_sub_us(TID_SEND_ADJUST); | 471 | _delay_sub_us(TID_SEND_ADJUST); |
380 | serial_write_chunk(sstd_index, 4); | 472 | serial_write_chunk(tid, 7); |
381 | serial_delay_half1(); | 473 | serial_delay_half1(); |
382 | 474 | ||
383 | // wait for the target response (step1 low->high) | 475 | // wait for the target response (step1 low->high) |
@@ -389,12 +481,12 @@ int soft_serial_transaction(int sstd_index) { | |||
389 | // check if the target is present (step2 high->low) | 481 | // check if the target is present (step2 high->low) |
390 | for( int i = 0; serial_read_pin(); i++ ) { | 482 | for( int i = 0; serial_read_pin(); i++ ) { |
391 | if (i > SLAVE_INT_ACK_WIDTH + 1) { | 483 | if (i > SLAVE_INT_ACK_WIDTH + 1) { |
392 | // slave failed to pull the line low, assume not present | 484 | // slave failed to pull the line low, assume not present |
393 | serial_output(); | 485 | serial_output(); |
394 | serial_high(); | 486 | serial_high(); |
395 | *trans->status = TRANSACTION_NO_RESPONSE; | 487 | *trans->status = TRANSACTION_NO_RESPONSE; |
396 | sei(); | 488 | sei(); |
397 | return TRANSACTION_NO_RESPONSE; | 489 | return TRANSACTION_NO_RESPONSE; |
398 | } | 490 | } |
399 | _delay_sub_us(SLAVE_INT_ACK_WIDTH_UNIT); | 491 | _delay_sub_us(SLAVE_INT_ACK_WIDTH_UNIT); |
400 | } | 492 | } |
@@ -404,12 +496,12 @@ int soft_serial_transaction(int sstd_index) { | |||
404 | // if the target is present syncronize with it | 496 | // if the target is present syncronize with it |
405 | if( trans->target2initiator_buffer_size > 0 ) { | 497 | if( trans->target2initiator_buffer_size > 0 ) { |
406 | if (!serial_recive_packet((uint8_t *)trans->target2initiator_buffer, | 498 | if (!serial_recive_packet((uint8_t *)trans->target2initiator_buffer, |
407 | trans->target2initiator_buffer_size) ) { | 499 | trans->target2initiator_buffer_size) ) { |
408 | serial_output(); | 500 | serial_output(); |
409 | serial_high(); | 501 | serial_high(); |
410 | *trans->status = TRANSACTION_DATA_ERROR; | 502 | *trans->status = TRANSACTION_DATA_ERROR; |
411 | sei(); | 503 | sei(); |
412 | return TRANSACTION_DATA_ERROR; | 504 | return TRANSACTION_DATA_ERROR; |
413 | } | 505 | } |
414 | } | 506 | } |
415 | 507 | ||
@@ -419,7 +511,7 @@ int soft_serial_transaction(int sstd_index) { | |||
419 | // initiator send phase | 511 | // initiator send phase |
420 | if( trans->initiator2target_buffer_size > 0 ) { | 512 | if( trans->initiator2target_buffer_size > 0 ) { |
421 | serial_send_packet((uint8_t *)trans->initiator2target_buffer, | 513 | serial_send_packet((uint8_t *)trans->initiator2target_buffer, |
422 | trans->initiator2target_buffer_size); | 514 | trans->initiator2target_buffer_size); |
423 | } | 515 | } |
424 | 516 | ||
425 | // always, release the line when not in use | 517 | // always, release the line when not in use |
@@ -442,3 +534,9 @@ int soft_serial_get_and_clean_status(int sstd_index) { | |||
442 | #endif | 534 | #endif |
443 | 535 | ||
444 | #endif | 536 | #endif |
537 | |||
538 | // Helix serial.c history | ||
539 | // 2018-1-29 fork from let's split (#2308) | ||
540 | // 2018-6-28 bug fix master to slave comm (#3255) | ||
541 | // 2018-8-11 improvements (#3608) | ||
542 | // 2018-10-21 fix serial and RGB animation conflict (#4191) | ||
diff --git a/keyboards/helix/serial.h b/keyboards/helix/serial.h index d2b7fd8e6..7e0c0847a 100644 --- a/keyboards/helix/serial.h +++ b/keyboards/helix/serial.h | |||
@@ -4,14 +4,16 @@ | |||
4 | #include <stdbool.h> | 4 | #include <stdbool.h> |
5 | 5 | ||
6 | // ///////////////////////////////////////////////////////////////// | 6 | // ///////////////////////////////////////////////////////////////// |
7 | // Need Soft Serial defines in serial_config.h | 7 | // Need Soft Serial defines in config.h |
8 | // ///////////////////////////////////////////////////////////////// | 8 | // ///////////////////////////////////////////////////////////////// |
9 | // ex. | 9 | // ex. |
10 | // #define SERIAL_PIN_DDR DDRD | 10 | // #define SOFT_SERIAL_PIN ?? // ?? = D0,D1,D2,D3,E6 |
11 | // #define SERIAL_PIN_PORT PORTD | 11 | // OPTIONAL: #define SELECT_SOFT_SERIAL_SPEED ? // ? = 1,2,3,4,5 |
12 | // #define SERIAL_PIN_INPUT PIND | 12 | // // 1: about 137kbps (default) |
13 | // #define SERIAL_PIN_MASK _BV(PD?) ?=0,2 | 13 | // // 2: about 75kbps |
14 | // #define SERIAL_PIN_INTERRUPT INT?_vect ?=0,2 | 14 | // // 3: about 39kbps |
15 | // // 4: about 26kbps | ||
16 | // // 5: about 20kbps | ||
15 | // | 17 | // |
16 | // //// USE Simple API (OLD API, compatible with let's split serial.c) | 18 | // //// USE Simple API (OLD API, compatible with let's split serial.c) |
17 | // ex. | 19 | // ex. |
@@ -47,16 +49,18 @@ typedef struct _SSTD_t { | |||
47 | uint8_t target2initiator_buffer_size; | 49 | uint8_t target2initiator_buffer_size; |
48 | uint8_t *target2initiator_buffer; | 50 | uint8_t *target2initiator_buffer; |
49 | } SSTD_t; | 51 | } SSTD_t; |
52 | #define TID_LIMIT( table ) (sizeof(table) / sizeof(SSTD_t)) | ||
50 | 53 | ||
51 | // initiator is transaction start side | 54 | // initiator is transaction start side |
52 | void soft_serial_initiator_init(SSTD_t *sstd_table); | 55 | void soft_serial_initiator_init(SSTD_t *sstd_table, int sstd_table_size); |
53 | // target is interrupt accept side | 56 | // target is interrupt accept side |
54 | void soft_serial_target_init(SSTD_t *sstd_table); | 57 | void soft_serial_target_init(SSTD_t *sstd_table, int sstd_table_size); |
55 | 58 | ||
56 | // initiator resullt | 59 | // initiator resullt |
57 | #define TRANSACTION_END 0 | 60 | #define TRANSACTION_END 0 |
58 | #define TRANSACTION_NO_RESPONSE 0x1 | 61 | #define TRANSACTION_NO_RESPONSE 0x1 |
59 | #define TRANSACTION_DATA_ERROR 0x2 | 62 | #define TRANSACTION_DATA_ERROR 0x2 |
63 | #define TRANSACTION_TYPE_ERROR 0x4 | ||
60 | #ifndef SERIAL_USE_MULTI_TRANSACTION | 64 | #ifndef SERIAL_USE_MULTI_TRANSACTION |
61 | int soft_serial_transaction(void); | 65 | int soft_serial_transaction(void); |
62 | #else | 66 | #else |
@@ -72,7 +76,7 @@ int soft_serial_transaction(int sstd_index); | |||
72 | // target: | 76 | // target: |
73 | // TRANSACTION_DATA_ERROR | 77 | // TRANSACTION_DATA_ERROR |
74 | // or TRANSACTION_ACCEPTED | 78 | // or TRANSACTION_ACCEPTED |
75 | #define TRANSACTION_ACCEPTED 0x4 | 79 | #define TRANSACTION_ACCEPTED 0x8 |
76 | #ifdef SERIAL_USE_MULTI_TRANSACTION | 80 | #ifdef SERIAL_USE_MULTI_TRANSACTION |
77 | int soft_serial_get_and_clean_status(int sstd_index); | 81 | int soft_serial_get_and_clean_status(int sstd_index); |
78 | #endif | 82 | #endif |