diff options
author | Stefan Kerkmann <karlk90@pm.me> | 2021-07-02 00:24:08 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-02 08:24:08 +1000 |
commit | 117bff17ba89a70dd85163b499c262b879f52afd (patch) | |
tree | 055d7704c28711bd3c30ba8f55a2b3524395578b /drivers | |
parent | 47b12470e762732638a79e6752069564d6fb9649 (diff) | |
download | qmk_firmware-117bff17ba89a70dd85163b499c262b879f52afd.tar.gz qmk_firmware-117bff17ba89a70dd85163b499c262b879f52afd.zip |
[Core] Unite half-duplex and full-duplex serial drivers (#13081)
* Unite half-duplex and full-duplex serial driver.
* Add full duplex operation mode to the interrupt based driver
* Delete DMA UART based full duplex driver
* The new driver targets #11930
* Fix freezes with failing transactions in half-duplex
* Increase default serial TX/RX buffer size to 128 bytes
* Correctly use bool instead of size_t
Co-authored-by: Nick Brassel <nick@tzarc.org>
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/chibios/serial_usart.c | 337 | ||||
-rw-r--r-- | drivers/chibios/serial_usart.h | 40 | ||||
-rw-r--r-- | drivers/chibios/serial_usart_duplex.c | 261 |
3 files changed, 262 insertions, 376 deletions
diff --git a/drivers/chibios/serial_usart.c b/drivers/chibios/serial_usart.c index 9f180d2d7..ea4473791 100644 --- a/drivers/chibios/serial_usart.c +++ b/drivers/chibios/serial_usart.c | |||
@@ -16,179 +16,300 @@ | |||
16 | 16 | ||
17 | #include "serial_usart.h" | 17 | #include "serial_usart.h" |
18 | 18 | ||
19 | #ifndef USE_GPIOV1 | 19 | #if defined(SERIAL_USART_CONFIG) |
20 | // The default PAL alternate modes are used to signal that the pins are used for USART | 20 | static SerialConfig serial_config = SERIAL_USART_CONFIG; |
21 | # ifndef SERIAL_USART_TX_PAL_MODE | 21 | #else |
22 | # define SERIAL_USART_TX_PAL_MODE 7 | 22 | static SerialConfig serial_config = { |
23 | .speed = (SERIAL_USART_SPEED), /* speed - mandatory */ | ||
24 | .cr1 = (SERIAL_USART_CR1), | ||
25 | .cr2 = (SERIAL_USART_CR2), | ||
26 | # if !defined(SERIAL_USART_FULL_DUPLEX) | ||
27 | .cr3 = ((SERIAL_USART_CR3) | USART_CR3_HDSEL) /* activate half-duplex mode */ | ||
28 | # else | ||
29 | .cr3 = (SERIAL_USART_CR3) | ||
23 | # endif | 30 | # endif |
31 | }; | ||
24 | #endif | 32 | #endif |
25 | 33 | ||
26 | #ifndef SERIAL_USART_DRIVER | 34 | static SerialDriver* serial_driver = &SERIAL_USART_DRIVER; |
27 | # define SERIAL_USART_DRIVER SD1 | ||
28 | #endif | ||
29 | |||
30 | #ifdef SOFT_SERIAL_PIN | ||
31 | # define SERIAL_USART_TX_PIN SOFT_SERIAL_PIN | ||
32 | #endif | ||
33 | |||
34 | static inline msg_t sdWriteHalfDuplex(SerialDriver* driver, uint8_t* data, uint8_t size) { | ||
35 | msg_t ret = sdWrite(driver, data, size); | ||
36 | 35 | ||
37 | // Half duplex requires us to read back the data we just wrote - just throw it away | 36 | static inline bool react_to_transactions(void); |
38 | uint8_t dump[size]; | 37 | static inline bool __attribute__((nonnull)) receive(uint8_t* destination, const size_t size); |
39 | sdRead(driver, dump, size); | 38 | static inline bool __attribute__((nonnull)) send(const uint8_t* source, const size_t size); |
39 | static inline int initiate_transaction(uint8_t sstd_index); | ||
40 | static inline void usart_clear(void); | ||
40 | 41 | ||
41 | return ret; | 42 | /** |
43 | * @brief Clear the receive input queue. | ||
44 | */ | ||
45 | static inline void usart_clear(void) { | ||
46 | osalSysLock(); | ||
47 | bool volatile queue_not_empty = !iqIsEmptyI(&serial_driver->iqueue); | ||
48 | osalSysUnlock(); | ||
49 | |||
50 | while (queue_not_empty) { | ||
51 | osalSysLock(); | ||
52 | /* Hard reset the input queue. */ | ||
53 | iqResetI(&serial_driver->iqueue); | ||
54 | osalSysUnlock(); | ||
55 | /* Allow pending interrupts to preempt. | ||
56 | * Do not merge the lock/unlock blocks into one | ||
57 | * or the code will not work properly. | ||
58 | * The empty read adds a tiny amount of delay. */ | ||
59 | (void)queue_not_empty; | ||
60 | osalSysLock(); | ||
61 | queue_not_empty = !iqIsEmptyI(&serial_driver->iqueue); | ||
62 | osalSysUnlock(); | ||
63 | } | ||
42 | } | 64 | } |
43 | #undef sdWrite | ||
44 | #define sdWrite sdWriteHalfDuplex | ||
45 | |||
46 | static inline msg_t sdWriteTimeoutHalfDuplex(SerialDriver* driver, uint8_t* data, uint8_t size, uint32_t timeout) { | ||
47 | msg_t ret = sdWriteTimeout(driver, data, size, timeout); | ||
48 | 65 | ||
49 | // Half duplex requires us to read back the data we just wrote - just throw it away | 66 | /** |
50 | uint8_t dump[size]; | 67 | * @brief Blocking send of buffer with timeout. |
51 | sdReadTimeout(driver, dump, size, timeout); | 68 | * |
69 | * @return true Send success. | ||
70 | * @return false Send failed. | ||
71 | */ | ||
72 | static inline bool send(const uint8_t* source, const size_t size) { | ||
73 | bool success = (size_t)sdWriteTimeout(serial_driver, source, size, TIME_MS2I(SERIAL_USART_TIMEOUT)) == size; | ||
74 | |||
75 | #if !defined(SERIAL_USART_FULL_DUPLEX) | ||
76 | if (success) { | ||
77 | /* Half duplex fills the input queue with the data we wrote - just throw it away. | ||
78 | Under the right circumstances (e.g. bad cables paired with high baud rates) | ||
79 | less bytes can be present in the input queue, therefore a timeout is needed. */ | ||
80 | uint8_t dump[size]; | ||
81 | return receive(dump, size); | ||
82 | } | ||
83 | #endif | ||
52 | 84 | ||
53 | return ret; | 85 | return success; |
54 | } | 86 | } |
55 | #undef sdWriteTimeout | ||
56 | #define sdWriteTimeout sdWriteTimeoutHalfDuplex | ||
57 | 87 | ||
58 | static inline void sdClear(SerialDriver* driver) { | 88 | /** |
59 | while (sdGetTimeout(driver, TIME_IMMEDIATE) != MSG_TIMEOUT) { | 89 | * @brief Blocking receive of size * bytes with timeout. |
60 | // Do nothing with the data | 90 | * |
61 | } | 91 | * @return true Receive success. |
92 | * @return false Receive failed. | ||
93 | */ | ||
94 | static inline bool receive(uint8_t* destination, const size_t size) { | ||
95 | bool success = (size_t)sdReadTimeout(serial_driver, destination, size, TIME_MS2I(SERIAL_USART_TIMEOUT)) == size; | ||
96 | return success; | ||
62 | } | 97 | } |
63 | 98 | ||
64 | static SerialConfig sdcfg = { | 99 | #if !defined(SERIAL_USART_FULL_DUPLEX) |
65 | (SERIAL_USART_SPEED), // speed - mandatory | ||
66 | (SERIAL_USART_CR1), // CR1 | ||
67 | (SERIAL_USART_CR2), // CR2 | ||
68 | (SERIAL_USART_CR3) // CR3 | ||
69 | }; | ||
70 | |||
71 | void handle_soft_serial_slave(void); | ||
72 | 100 | ||
73 | /* | 101 | /** |
74 | * This thread runs on the slave and responds to transactions initiated | 102 | * @brief Initiate pins for USART peripheral. Half-duplex configuration. |
75 | * by the master | ||
76 | */ | 103 | */ |
77 | static THD_WORKING_AREA(waSlaveThread, 2048); | 104 | __attribute__((weak)) void usart_init(void) { |
78 | static THD_FUNCTION(SlaveThread, arg) { | 105 | # if defined(MCU_STM32) |
79 | (void)arg; | 106 | # if defined(USE_GPIOV1) |
80 | chRegSetThreadName("slave_transport"); | 107 | palSetLineMode(SERIAL_USART_TX_PIN, PAL_MODE_STM32_ALTERNATE_OPENDRAIN); |
108 | # else | ||
109 | palSetLineMode(SERIAL_USART_TX_PIN, PAL_MODE_ALTERNATE(SERIAL_USART_TX_PAL_MODE) | PAL_STM32_OTYPE_OPENDRAIN); | ||
110 | # endif | ||
81 | 111 | ||
82 | while (true) { | 112 | # if defined(USART_REMAP) |
83 | handle_soft_serial_slave(); | 113 | USART_REMAP; |
84 | } | 114 | # endif |
115 | # else | ||
116 | # pragma message "usart_init: MCU Familiy not supported by default, please supply your own init code by implementing usart_init() in your keyboard files." | ||
117 | # endif | ||
85 | } | 118 | } |
86 | 119 | ||
87 | __attribute__((weak)) void usart_init(void) { | ||
88 | #if defined(USE_GPIOV1) | ||
89 | palSetLineMode(SERIAL_USART_TX_PIN, PAL_MODE_STM32_ALTERNATE_OPENDRAIN); | ||
90 | #else | 120 | #else |
91 | palSetLineMode(SERIAL_USART_TX_PIN, PAL_MODE_ALTERNATE(SERIAL_USART_TX_PAL_MODE) | PAL_STM32_OTYPE_OPENDRAIN); | ||
92 | #endif | ||
93 | 121 | ||
94 | #if defined(USART_REMAP) | 122 | /** |
123 | * @brief Initiate pins for USART peripheral. Full-duplex configuration. | ||
124 | */ | ||
125 | __attribute__((weak)) void usart_init(void) { | ||
126 | # if defined(MCU_STM32) | ||
127 | # if defined(USE_GPIOV1) | ||
128 | palSetLineMode(SERIAL_USART_TX_PIN, PAL_MODE_STM32_ALTERNATE_PUSHPULL); | ||
129 | palSetLineMode(SERIAL_USART_RX_PIN, PAL_MODE_INPUT); | ||
130 | # else | ||
131 | palSetLineMode(SERIAL_USART_TX_PIN, PAL_MODE_ALTERNATE(SERIAL_USART_TX_PAL_MODE) | PAL_STM32_OTYPE_PUSHPULL | PAL_STM32_OSPEED_HIGHEST); | ||
132 | palSetLineMode(SERIAL_USART_RX_PIN, PAL_MODE_ALTERNATE(SERIAL_USART_RX_PAL_MODE) | PAL_STM32_OTYPE_PUSHPULL | PAL_STM32_OSPEED_HIGHEST); | ||
133 | # endif | ||
134 | |||
135 | # if defined(USART_REMAP) | ||
95 | USART_REMAP; | 136 | USART_REMAP; |
137 | # endif | ||
138 | # else | ||
139 | # pragma message "usart_init: MCU Familiy not supported by default, please supply your own init code by implementing usart_init() in your keyboard files." | ||
140 | # endif | ||
141 | } | ||
142 | |||
96 | #endif | 143 | #endif |
144 | |||
145 | /** | ||
146 | * @brief Overridable master specific initializations. | ||
147 | */ | ||
148 | __attribute__((weak, nonnull)) void usart_master_init(SerialDriver** driver) { | ||
149 | (void)driver; | ||
150 | usart_init(); | ||
97 | } | 151 | } |
98 | 152 | ||
99 | void usart_master_init(void) { | 153 | /** |
154 | * @brief Overridable slave specific initializations. | ||
155 | */ | ||
156 | __attribute__((weak, nonnull)) void usart_slave_init(SerialDriver** driver) { | ||
157 | (void)driver; | ||
100 | usart_init(); | 158 | usart_init(); |
159 | } | ||
160 | |||
161 | /** | ||
162 | * @brief This thread runs on the slave and responds to transactions initiated | ||
163 | * by the master. | ||
164 | */ | ||
165 | static THD_WORKING_AREA(waSlaveThread, 1024); | ||
166 | static THD_FUNCTION(SlaveThread, arg) { | ||
167 | (void)arg; | ||
168 | chRegSetThreadName("usart_tx_rx"); | ||
101 | 169 | ||
102 | sdcfg.cr3 |= USART_CR3_HDSEL; | 170 | while (true) { |
103 | sdStart(&SERIAL_USART_DRIVER, &sdcfg); | 171 | if (!react_to_transactions()) { |
172 | /* Clear the receive queue, to start with a clean slate. | ||
173 | * Parts of failed transactions or spurious bytes could still be in it. */ | ||
174 | usart_clear(); | ||
175 | } | ||
176 | } | ||
104 | } | 177 | } |
105 | 178 | ||
106 | void usart_slave_init(void) { | 179 | /** |
107 | usart_init(); | 180 | * @brief Slave specific initializations. |
181 | */ | ||
182 | void soft_serial_target_init(void) { | ||
183 | usart_slave_init(&serial_driver); | ||
108 | 184 | ||
109 | sdcfg.cr3 |= USART_CR3_HDSEL; | 185 | sdStart(serial_driver, &serial_config); |
110 | sdStart(&SERIAL_USART_DRIVER, &sdcfg); | ||
111 | 186 | ||
112 | // Start transport thread | 187 | /* Start transport thread. */ |
113 | chThdCreateStatic(waSlaveThread, sizeof(waSlaveThread), HIGHPRIO, SlaveThread, NULL); | 188 | chThdCreateStatic(waSlaveThread, sizeof(waSlaveThread), HIGHPRIO, SlaveThread, NULL); |
114 | } | 189 | } |
115 | 190 | ||
116 | void soft_serial_initiator_init(void) { usart_master_init(); } | 191 | /** |
192 | * @brief React to transactions started by the master. | ||
193 | */ | ||
194 | static inline bool react_to_transactions(void) { | ||
195 | /* Wait until there is a transaction for us. */ | ||
196 | uint8_t sstd_index = (uint8_t)sdGet(serial_driver); | ||
117 | 197 | ||
118 | void soft_serial_target_init(void) { usart_slave_init(); } | 198 | /* Sanity check that we are actually responding to a valid transaction. */ |
199 | if (sstd_index >= NUM_TOTAL_TRANSACTIONS) { | ||
200 | return false; | ||
201 | } | ||
119 | 202 | ||
120 | void handle_soft_serial_slave(void) { | 203 | split_transaction_desc_t* trans = &split_transaction_table[sstd_index]; |
121 | uint8_t sstd_index = sdGet(&SERIAL_USART_DRIVER); // first chunk is always transaction id | ||
122 | split_transaction_desc_t* trans = &split_transaction_table[sstd_index]; | ||
123 | 204 | ||
124 | // Always write back the sstd_index as part of a basic handshake | 205 | /* Send back the handshake which is XORed as a simple checksum, |
206 | to signal that the slave is ready to receive possible transaction buffers */ | ||
125 | sstd_index ^= HANDSHAKE_MAGIC; | 207 | sstd_index ^= HANDSHAKE_MAGIC; |
126 | sdWrite(&SERIAL_USART_DRIVER, &sstd_index, sizeof(sstd_index)); | 208 | if (!send(&sstd_index, sizeof(sstd_index))) { |
209 | *trans->status = TRANSACTION_DATA_ERROR; | ||
210 | return false; | ||
211 | } | ||
127 | 212 | ||
213 | /* Receive transaction buffer from the master. If this transaction requires it.*/ | ||
128 | if (trans->initiator2target_buffer_size) { | 214 | if (trans->initiator2target_buffer_size) { |
129 | sdRead(&SERIAL_USART_DRIVER, split_trans_initiator2target_buffer(trans), trans->initiator2target_buffer_size); | 215 | if (!receive(split_trans_initiator2target_buffer(trans), trans->initiator2target_buffer_size)) { |
216 | *trans->status = TRANSACTION_DATA_ERROR; | ||
217 | return false; | ||
218 | } | ||
130 | } | 219 | } |
131 | 220 | ||
132 | // Allow any slave processing to occur | 221 | /* Allow any slave processing to occur. */ |
133 | if (trans->slave_callback) { | 222 | if (trans->slave_callback) { |
134 | trans->slave_callback(trans->initiator2target_buffer_size, split_trans_initiator2target_buffer(trans), trans->target2initiator_buffer_size, split_trans_target2initiator_buffer(trans)); | 223 | trans->slave_callback(trans->initiator2target_buffer_size, split_trans_initiator2target_buffer(trans), trans->initiator2target_buffer_size, split_trans_target2initiator_buffer(trans)); |
135 | } | 224 | } |
136 | 225 | ||
226 | /* Send transaction buffer to the master. If this transaction requires it. */ | ||
137 | if (trans->target2initiator_buffer_size) { | 227 | if (trans->target2initiator_buffer_size) { |
138 | sdWrite(&SERIAL_USART_DRIVER, split_trans_target2initiator_buffer(trans), trans->target2initiator_buffer_size); | 228 | if (!send(split_trans_target2initiator_buffer(trans), trans->target2initiator_buffer_size)) { |
229 | *trans->status = TRANSACTION_DATA_ERROR; | ||
230 | return false; | ||
231 | } | ||
139 | } | 232 | } |
140 | 233 | ||
141 | if (trans->status) { | 234 | *trans->status = TRANSACTION_ACCEPTED; |
142 | *trans->status = TRANSACTION_ACCEPTED; | 235 | return true; |
143 | } | 236 | } |
237 | |||
238 | /** | ||
239 | * @brief Master specific initializations. | ||
240 | */ | ||
241 | void soft_serial_initiator_init(void) { | ||
242 | usart_master_init(&serial_driver); | ||
243 | |||
244 | #if defined(MCU_STM32) && defined(SERIAL_USART_PIN_SWAP) | ||
245 | serial_config.cr2 |= USART_CR2_SWAP; // master has swapped TX/RX pins | ||
246 | #endif | ||
247 | |||
248 | sdStart(serial_driver, &serial_config); | ||
144 | } | 249 | } |
145 | 250 | ||
146 | ///////// | 251 | /** |
147 | // start transaction by initiator | 252 | * @brief Start transaction from the master half to the slave half. |
148 | // | 253 | * |
149 | // int soft_serial_transaction(int sstd_index) | 254 | * @param index Transaction Table index of the transaction to start. |
150 | // | 255 | * @return int TRANSACTION_NO_RESPONSE in case of Timeout. |
151 | // Returns: | 256 | * TRANSACTION_TYPE_ERROR in case of invalid transaction index. |
152 | // TRANSACTION_END | 257 | * TRANSACTION_END in case of success. |
153 | // TRANSACTION_NO_RESPONSE | 258 | */ |
154 | // TRANSACTION_DATA_ERROR | ||
155 | int soft_serial_transaction(int index) { | 259 | int soft_serial_transaction(int index) { |
156 | uint8_t sstd_index = index; | 260 | /* Clear the receive queue, to start with a clean slate. |
261 | * Parts of failed transactions or spurious bytes could still be in it. */ | ||
262 | usart_clear(); | ||
263 | return initiate_transaction((uint8_t)index); | ||
264 | } | ||
157 | 265 | ||
158 | if (sstd_index > NUM_TOTAL_TRANSACTIONS) return TRANSACTION_TYPE_ERROR; | 266 | /** |
159 | split_transaction_desc_t* trans = &split_transaction_table[sstd_index]; | 267 | * @brief Initiate transaction to slave half. |
160 | msg_t res = 0; | 268 | */ |
269 | static inline int initiate_transaction(uint8_t sstd_index) { | ||
270 | /* Sanity check that we are actually starting a valid transaction. */ | ||
271 | if (sstd_index >= NUM_TOTAL_TRANSACTIONS) { | ||
272 | dprintln("USART: Illegal transaction Id."); | ||
273 | return TRANSACTION_TYPE_ERROR; | ||
274 | } | ||
161 | 275 | ||
162 | if (!trans->status) return TRANSACTION_TYPE_ERROR; // not registered | 276 | split_transaction_desc_t* trans = &split_transaction_table[sstd_index]; |
163 | 277 | ||
164 | sdClear(&SERIAL_USART_DRIVER); | 278 | /* Transaction is not registered. Abort. */ |
279 | if (!trans->status) { | ||
280 | dprintln("USART: Transaction not registered."); | ||
281 | return TRANSACTION_TYPE_ERROR; | ||
282 | } | ||
165 | 283 | ||
166 | // First chunk is always transaction id | 284 | /* Send transaction table index to the slave, which doubles as basic handshake token. */ |
167 | sdWriteTimeout(&SERIAL_USART_DRIVER, &sstd_index, sizeof(sstd_index), TIME_MS2I(SERIAL_USART_TIMEOUT)); | 285 | if (!send(&sstd_index, sizeof(sstd_index))) { |
286 | dprintln("USART: Send Handshake failed."); | ||
287 | return TRANSACTION_TYPE_ERROR; | ||
288 | } | ||
168 | 289 | ||
169 | uint8_t sstd_index_shake = 0xFF; | 290 | uint8_t sstd_index_shake = 0xFF; |
170 | 291 | ||
171 | // Which we always read back first so that we can error out correctly | 292 | /* Which we always read back first so that we can error out correctly. |
172 | // - due to the half duplex limitations on return codes, we always have to read *something* | 293 | * - due to the half duplex limitations on return codes, we always have to read *something*. |
173 | // - without the read, write only transactions *always* succeed, even during the boot process where the slave is not ready | 294 | * - without the read, write only transactions *always* succeed, even during the boot process where the slave is not ready. |
174 | res = sdReadTimeout(&SERIAL_USART_DRIVER, &sstd_index_shake, sizeof(sstd_index_shake), TIME_MS2I(SERIAL_USART_TIMEOUT)); | 295 | */ |
175 | if (res < 0 || (sstd_index_shake != (sstd_index ^ HANDSHAKE_MAGIC))) { | 296 | if (!receive(&sstd_index_shake, sizeof(sstd_index_shake)) || (sstd_index_shake != (sstd_index ^ HANDSHAKE_MAGIC))) { |
176 | dprintf("serial::usart_shake NO_RESPONSE\n"); | 297 | dprintln("USART: Handshake failed."); |
177 | return TRANSACTION_NO_RESPONSE; | 298 | return TRANSACTION_NO_RESPONSE; |
178 | } | 299 | } |
179 | 300 | ||
301 | /* Send transaction buffer to the slave. If this transaction requires it. */ | ||
180 | if (trans->initiator2target_buffer_size) { | 302 | if (trans->initiator2target_buffer_size) { |
181 | res = sdWriteTimeout(&SERIAL_USART_DRIVER, split_trans_initiator2target_buffer(trans), trans->initiator2target_buffer_size, TIME_MS2I(SERIAL_USART_TIMEOUT)); | 303 | if (!send(split_trans_initiator2target_buffer(trans), trans->initiator2target_buffer_size)) { |
182 | if (res < 0) { | 304 | dprintln("USART: Send failed."); |
183 | dprintf("serial::usart_transmit NO_RESPONSE\n"); | ||
184 | return TRANSACTION_NO_RESPONSE; | 305 | return TRANSACTION_NO_RESPONSE; |
185 | } | 306 | } |
186 | } | 307 | } |
187 | 308 | ||
309 | /* Receive transaction buffer from the slave. If this transaction requires it. */ | ||
188 | if (trans->target2initiator_buffer_size) { | 310 | if (trans->target2initiator_buffer_size) { |
189 | res = sdReadTimeout(&SERIAL_USART_DRIVER, split_trans_target2initiator_buffer(trans), trans->target2initiator_buffer_size, TIME_MS2I(SERIAL_USART_TIMEOUT)); | 311 | if (!receive(split_trans_target2initiator_buffer(trans), trans->target2initiator_buffer_size)) { |
190 | if (res < 0) { | 312 | dprintln("USART: Receive failed."); |
191 | dprintf("serial::usart_receive NO_RESPONSE\n"); | ||
192 | return TRANSACTION_NO_RESPONSE; | 313 | return TRANSACTION_NO_RESPONSE; |
193 | } | 314 | } |
194 | } | 315 | } |
diff --git a/drivers/chibios/serial_usart.h b/drivers/chibios/serial_usart.h index fee7b4d15..c64e15566 100644 --- a/drivers/chibios/serial_usart.h +++ b/drivers/chibios/serial_usart.h | |||
@@ -23,19 +23,45 @@ | |||
23 | #include <ch.h> | 23 | #include <ch.h> |
24 | #include <hal.h> | 24 | #include <hal.h> |
25 | 25 | ||
26 | #ifndef USART_CR1_M0 | 26 | #if !defined(SERIAL_USART_DRIVER) |
27 | # define SERIAL_USART_DRIVER SD1 | ||
28 | #endif | ||
29 | |||
30 | #if !defined(USE_GPIOV1) | ||
31 | /* The default PAL alternate modes are used to signal that the pins are used for USART. */ | ||
32 | # if !defined(SERIAL_USART_TX_PAL_MODE) | ||
33 | # define SERIAL_USART_TX_PAL_MODE 7 | ||
34 | # endif | ||
35 | # if !defined(SERIAL_USART_RX_PAL_MODE) | ||
36 | # define SERIAL_USART_RX_PAL_MODE 7 | ||
37 | # endif | ||
38 | #endif | ||
39 | |||
40 | #if defined(SOFT_SERIAL_PIN) | ||
41 | # define SERIAL_USART_TX_PIN SOFT_SERIAL_PIN | ||
42 | #endif | ||
43 | |||
44 | #if !defined(SERIAL_USART_TX_PIN) | ||
45 | # define SERIAL_USART_TX_PIN A9 | ||
46 | #endif | ||
47 | |||
48 | #if !defined(SERIAL_USART_RX_PIN) | ||
49 | # define SERIAL_USART_RX_PIN A10 | ||
50 | #endif | ||
51 | |||
52 | #if !defined(USART_CR1_M0) | ||
27 | # define USART_CR1_M0 USART_CR1_M // some platforms (f1xx) dont have this so | 53 | # define USART_CR1_M0 USART_CR1_M // some platforms (f1xx) dont have this so |
28 | #endif | 54 | #endif |
29 | 55 | ||
30 | #ifndef SERIAL_USART_CR1 | 56 | #if !defined(SERIAL_USART_CR1) |
31 | # define SERIAL_USART_CR1 (USART_CR1_PCE | USART_CR1_PS | USART_CR1_M0) // parity enable, odd parity, 9 bit length | 57 | # define SERIAL_USART_CR1 (USART_CR1_PCE | USART_CR1_PS | USART_CR1_M0) // parity enable, odd parity, 9 bit length |
32 | #endif | 58 | #endif |
33 | 59 | ||
34 | #ifndef SERIAL_USART_CR2 | 60 | #if !defined(SERIAL_USART_CR2) |
35 | # define SERIAL_USART_CR2 (USART_CR2_STOP_1) // 2 stop bits | 61 | # define SERIAL_USART_CR2 (USART_CR2_STOP_1) // 2 stop bits |
36 | #endif | 62 | #endif |
37 | 63 | ||
38 | #ifndef SERIAL_USART_CR3 | 64 | #if !defined(SERIAL_USART_CR3) |
39 | # define SERIAL_USART_CR3 0 | 65 | # define SERIAL_USART_CR3 0 |
40 | #endif | 66 | #endif |
41 | 67 | ||
@@ -61,11 +87,11 @@ | |||
61 | } while (0) | 87 | } while (0) |
62 | #endif | 88 | #endif |
63 | 89 | ||
64 | #ifndef SELECT_SOFT_SERIAL_SPEED | 90 | #if !defined(SELECT_SOFT_SERIAL_SPEED) |
65 | # define SELECT_SOFT_SERIAL_SPEED 1 | 91 | # define SELECT_SOFT_SERIAL_SPEED 1 |
66 | #endif | 92 | #endif |
67 | 93 | ||
68 | #ifdef SERIAL_USART_SPEED | 94 | #if defined(SERIAL_USART_SPEED) |
69 | // Allow advanced users to directly set SERIAL_USART_SPEED | 95 | // Allow advanced users to directly set SERIAL_USART_SPEED |
70 | #elif SELECT_SOFT_SERIAL_SPEED == 0 | 96 | #elif SELECT_SOFT_SERIAL_SPEED == 0 |
71 | # define SERIAL_USART_SPEED 460800 | 97 | # define SERIAL_USART_SPEED 460800 |
@@ -83,7 +109,7 @@ | |||
83 | # error invalid SELECT_SOFT_SERIAL_SPEED value | 109 | # error invalid SELECT_SOFT_SERIAL_SPEED value |
84 | #endif | 110 | #endif |
85 | 111 | ||
86 | #ifndef SERIAL_USART_TIMEOUT | 112 | #if !defined(SERIAL_USART_TIMEOUT) |
87 | # define SERIAL_USART_TIMEOUT 100 | 113 | # define SERIAL_USART_TIMEOUT 100 |
88 | #endif | 114 | #endif |
89 | 115 | ||
diff --git a/drivers/chibios/serial_usart_duplex.c b/drivers/chibios/serial_usart_duplex.c deleted file mode 100644 index cc9b889ac..000000000 --- a/drivers/chibios/serial_usart_duplex.c +++ /dev/null | |||
@@ -1,261 +0,0 @@ | |||
1 | /* Copyright 2021 QMK | ||
2 | * | ||
3 | * This program is free software: you can redistribute it and/or modify | ||
4 | * it under the terms of the GNU General Public License as published by | ||
5 | * the Free Software Foundation, either version 3 of the License, or | ||
6 | * (at your option) any later version. | ||
7 | * | ||
8 | * This program is distributed in the hope that it will be useful, | ||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
11 | * GNU General Public License for more details. | ||
12 | * | ||
13 | * You should have received a copy of the GNU General Public License | ||
14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
15 | */ | ||
16 | |||
17 | #include "serial_usart.h" | ||
18 | |||
19 | #include <stdatomic.h> | ||
20 | |||
21 | #if !defined(USE_GPIOV1) | ||
22 | // The default PAL alternate modes are used to signal that the pins are used for USART | ||
23 | # if !defined(SERIAL_USART_TX_PAL_MODE) | ||
24 | # define SERIAL_USART_TX_PAL_MODE 7 | ||
25 | # endif | ||
26 | # if !defined(SERIAL_USART_RX_PAL_MODE) | ||
27 | # define SERIAL_USART_RX_PAL_MODE 7 | ||
28 | # endif | ||
29 | #endif | ||
30 | |||
31 | #if !defined(SERIAL_USART_DRIVER) | ||
32 | # define SERIAL_USART_DRIVER UARTD1 | ||
33 | #endif | ||
34 | |||
35 | #if !defined(SERIAL_USART_TX_PIN) | ||
36 | # define SERIAL_USART_TX_PIN A9 | ||
37 | #endif | ||
38 | |||
39 | #if !defined(SERIAL_USART_RX_PIN) | ||
40 | # define SERIAL_USART_RX_PIN A10 | ||
41 | #endif | ||
42 | |||
43 | #define SIGNAL_HANDSHAKE_RECEIVED 0x1 | ||
44 | |||
45 | void handle_transactions_slave(uint8_t sstd_index); | ||
46 | static void receive_transaction_handshake(UARTDriver* uartp, uint16_t received_handshake); | ||
47 | |||
48 | /* | ||
49 | * UART driver configuration structure. We use the blocking DMA enabled API and | ||
50 | * the rxchar callback to receive handshake tokens but only on the slave halve. | ||
51 | */ | ||
52 | // clang-format off | ||
53 | static UARTConfig uart_config = { | ||
54 | .txend1_cb = NULL, | ||
55 | .txend2_cb = NULL, | ||
56 | .rxend_cb = NULL, | ||
57 | .rxchar_cb = NULL, | ||
58 | .rxerr_cb = NULL, | ||
59 | .timeout_cb = NULL, | ||
60 | .speed = (SERIAL_USART_SPEED), | ||
61 | .cr1 = (SERIAL_USART_CR1), | ||
62 | .cr2 = (SERIAL_USART_CR2), | ||
63 | .cr3 = (SERIAL_USART_CR3) | ||
64 | }; | ||
65 | // clang-format on | ||
66 | |||
67 | static SSTD_t* Transaction_table = NULL; | ||
68 | static uint8_t Transaction_table_size = 0; | ||
69 | static atomic_uint_least8_t handshake = 0xFF; | ||
70 | static thread_reference_t tp_target = NULL; | ||
71 | |||
72 | /* | ||
73 | * This callback is invoked when a character is received but the application | ||
74 | * was not ready to receive it, the character is passed as parameter. | ||
75 | * Receive transaction table index from initiator, which doubles as basic handshake token. */ | ||
76 | static void receive_transaction_handshake(UARTDriver* uartp, uint16_t received_handshake) { | ||
77 | /* Check if received handshake is not a valid transaction id. | ||
78 | * Please note that we can still catch a seemingly valid handshake | ||
79 | * i.e. a byte from a ongoing transfer which is in the allowed range. | ||
80 | * So this check mainly prevents any obviously wrong handshakes and | ||
81 | * subsequent wakeups of the receiving thread, which is a costly operation. */ | ||
82 | if (received_handshake > Transaction_table_size) { | ||
83 | return; | ||
84 | } | ||
85 | |||
86 | handshake = (uint8_t)received_handshake; | ||
87 | chSysLockFromISR(); | ||
88 | /* Wakeup receiving thread to start a transaction. */ | ||
89 | chEvtSignalI(tp_target, (eventmask_t)SIGNAL_HANDSHAKE_RECEIVED); | ||
90 | chSysUnlockFromISR(); | ||
91 | } | ||
92 | |||
93 | __attribute__((weak)) void usart_init(void) { | ||
94 | #if defined(USE_GPIOV1) | ||
95 | palSetLineMode(SERIAL_USART_TX_PIN, PAL_MODE_STM32_ALTERNATE_PUSHPULL); | ||
96 | palSetLineMode(SERIAL_USART_RX_PIN, PAL_MODE_INPUT); | ||
97 | #else | ||
98 | palSetLineMode(SERIAL_USART_TX_PIN, PAL_MODE_ALTERNATE(SERIAL_USART_TX_PAL_MODE) | PAL_STM32_OTYPE_PUSHPULL | PAL_STM32_OSPEED_HIGHEST); | ||
99 | palSetLineMode(SERIAL_USART_RX_PIN, PAL_MODE_ALTERNATE(SERIAL_USART_RX_PAL_MODE) | PAL_STM32_OTYPE_PUSHPULL | PAL_STM32_OSPEED_HIGHEST); | ||
100 | #endif | ||
101 | } | ||
102 | |||
103 | /* | ||
104 | * This thread runs on the slave half and reacts to transactions initiated from the master. | ||
105 | */ | ||
106 | static THD_WORKING_AREA(waSlaveThread, 1024); | ||
107 | static THD_FUNCTION(SlaveThread, arg) { | ||
108 | (void)arg; | ||
109 | chRegSetThreadName("slave_usart_tx_rx"); | ||
110 | |||
111 | while (true) { | ||
112 | /* We sleep as long as there is no handshake waiting for us. */ | ||
113 | chEvtWaitAny((eventmask_t)SIGNAL_HANDSHAKE_RECEIVED); | ||
114 | handle_transactions_slave(handshake); | ||
115 | } | ||
116 | } | ||
117 | |||
118 | void soft_serial_target_init(SSTD_t* const sstd_table, int sstd_table_size) { | ||
119 | Transaction_table = sstd_table; | ||
120 | Transaction_table_size = (uint8_t)sstd_table_size; | ||
121 | usart_init(); | ||
122 | |||
123 | #if defined(USART_REMAP) | ||
124 | USART_REMAP; | ||
125 | #endif | ||
126 | |||
127 | tp_target = chThdCreateStatic(waSlaveThread, sizeof(waSlaveThread), HIGHPRIO, SlaveThread, NULL); | ||
128 | |||
129 | // Start receiving handshake tokens on slave halve | ||
130 | uart_config.rxchar_cb = receive_transaction_handshake; | ||
131 | uartStart(&SERIAL_USART_DRIVER, &uart_config); | ||
132 | } | ||
133 | |||
134 | /** | ||
135 | * @brief React to transactions started by the master. | ||
136 | * This version uses duplex send and receive usart pheriphals and DMA backed transfers. | ||
137 | */ | ||
138 | void inline handle_transactions_slave(uint8_t sstd_index) { | ||
139 | size_t buffer_size = 0; | ||
140 | msg_t msg = 0; | ||
141 | SSTD_t* trans = &Transaction_table[sstd_index]; | ||
142 | |||
143 | /* Send back the handshake which is XORed as a simple checksum, | ||
144 | to signal that the slave is ready to receive possible transaction buffers */ | ||
145 | sstd_index ^= HANDSHAKE_MAGIC; | ||
146 | buffer_size = (size_t)sizeof(sstd_index); | ||
147 | msg = uartSendTimeout(&SERIAL_USART_DRIVER, &buffer_size, &sstd_index, TIME_MS2I(SERIAL_USART_TIMEOUT)); | ||
148 | |||
149 | if (msg != MSG_OK) { | ||
150 | if (trans->status) { | ||
151 | *trans->status = TRANSACTION_NO_RESPONSE; | ||
152 | } | ||
153 | return; | ||
154 | } | ||
155 | |||
156 | /* Receive transaction buffer from the master. If this transaction requires it.*/ | ||
157 | buffer_size = (size_t)trans->initiator2target_buffer_size; | ||
158 | if (buffer_size) { | ||
159 | msg = uartReceiveTimeout(&SERIAL_USART_DRIVER, &buffer_size, trans->initiator2target_buffer, TIME_MS2I(SERIAL_USART_TIMEOUT)); | ||
160 | if (msg != MSG_OK) { | ||
161 | if (trans->status) { | ||
162 | *trans->status = TRANSACTION_NO_RESPONSE; | ||
163 | } | ||
164 | return; | ||
165 | } | ||
166 | } | ||
167 | |||
168 | /* Send transaction buffer to the master. If this transaction requires it. */ | ||
169 | buffer_size = (size_t)trans->target2initiator_buffer_size; | ||
170 | if (buffer_size) { | ||
171 | msg = uartSendFullTimeout(&SERIAL_USART_DRIVER, &buffer_size, trans->target2initiator_buffer, TIME_MS2I(SERIAL_USART_TIMEOUT)); | ||
172 | if (msg != MSG_OK) { | ||
173 | if (trans->status) { | ||
174 | *trans->status = TRANSACTION_NO_RESPONSE; | ||
175 | } | ||
176 | return; | ||
177 | } | ||
178 | } | ||
179 | |||
180 | if (trans->status) { | ||
181 | *trans->status = TRANSACTION_ACCEPTED; | ||
182 | } | ||
183 | } | ||
184 | |||
185 | void soft_serial_initiator_init(SSTD_t* const sstd_table, int sstd_table_size) { | ||
186 | Transaction_table = sstd_table; | ||
187 | Transaction_table_size = (uint8_t)sstd_table_size; | ||
188 | usart_init(); | ||
189 | |||
190 | #if defined(SERIAL_USART_PIN_SWAP) | ||
191 | uart_config.cr2 |= USART_CR2_SWAP; // master has swapped TX/RX pins | ||
192 | #endif | ||
193 | |||
194 | #if defined(USART_REMAP) | ||
195 | USART_REMAP; | ||
196 | #endif | ||
197 | |||
198 | uartStart(&SERIAL_USART_DRIVER, &uart_config); | ||
199 | } | ||
200 | |||
201 | /** | ||
202 | * @brief Start transaction from the master to the slave. | ||
203 | * This version uses duplex send and receive usart pheriphals and DMA backed transfers. | ||
204 | * | ||
205 | * @param index Transaction Table index of the transaction to start. | ||
206 | * @return int TRANSACTION_NO_RESPONSE in case of Timeout. | ||
207 | * TRANSACTION_TYPE_ERROR in case of invalid transaction index. | ||
208 | * TRANSACTION_END in case of success. | ||
209 | */ | ||
210 | #if !defined(SERIAL_USE_MULTI_TRANSACTION) | ||
211 | int soft_serial_transaction(void) { | ||
212 | uint8_t sstd_index = 0; | ||
213 | #else | ||
214 | int soft_serial_transaction(int index) { | ||
215 | uint8_t sstd_index = index; | ||
216 | #endif | ||
217 | |||
218 | if (sstd_index > Transaction_table_size) { | ||
219 | return TRANSACTION_TYPE_ERROR; | ||
220 | } | ||
221 | |||
222 | SSTD_t* const trans = &Transaction_table[sstd_index]; | ||
223 | msg_t msg = 0; | ||
224 | size_t buffer_size = (size_t)sizeof(sstd_index); | ||
225 | |||
226 | /* Send transaction table index to the slave, which doubles as basic handshake token. */ | ||
227 | uartSendFullTimeout(&SERIAL_USART_DRIVER, &buffer_size, &sstd_index, TIME_MS2I(SERIAL_USART_TIMEOUT)); | ||
228 | |||
229 | uint8_t sstd_index_shake = 0xFF; | ||
230 | buffer_size = (size_t)sizeof(sstd_index_shake); | ||
231 | |||
232 | /* Receive the handshake token from the slave. The token was XORed by the slave as a simple checksum. | ||
233 | If the tokens match, the master will start to send and receive possible transaction buffers. */ | ||
234 | msg = uartReceiveTimeout(&SERIAL_USART_DRIVER, &buffer_size, &sstd_index_shake, TIME_MS2I(SERIAL_USART_TIMEOUT)); | ||
235 | if (msg != MSG_OK || (sstd_index_shake != (sstd_index ^ HANDSHAKE_MAGIC))) { | ||
236 | dprintln("USART: Handshake Failed"); | ||
237 | return TRANSACTION_NO_RESPONSE; | ||
238 | } | ||
239 | |||
240 | /* Send transaction buffer to the slave. If this transaction requires it. */ | ||
241 | buffer_size = (size_t)trans->initiator2target_buffer_size; | ||
242 | if (buffer_size) { | ||
243 | msg = uartSendFullTimeout(&SERIAL_USART_DRIVER, &buffer_size, trans->initiator2target_buffer, TIME_MS2I(SERIAL_USART_TIMEOUT)); | ||
244 | if (msg != MSG_OK) { | ||
245 | dprintln("USART: Send Failed"); | ||
246 | return TRANSACTION_NO_RESPONSE; | ||
247 | } | ||
248 | } | ||
249 | |||
250 | /* Receive transaction buffer from the slave. If this transaction requires it. */ | ||
251 | buffer_size = (size_t)trans->target2initiator_buffer_size; | ||
252 | if (buffer_size) { | ||
253 | msg = uartReceiveTimeout(&SERIAL_USART_DRIVER, &buffer_size, trans->target2initiator_buffer, TIME_MS2I(SERIAL_USART_TIMEOUT)); | ||
254 | if (msg != MSG_OK) { | ||
255 | dprintln("USART: Receive Failed"); | ||
256 | return TRANSACTION_NO_RESPONSE; | ||
257 | } | ||
258 | } | ||
259 | |||
260 | return TRANSACTION_END; | ||
261 | } | ||