aboutsummaryrefslogtreecommitdiff
path: root/drivers/chibios/serial_usart.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/chibios/serial_usart.c')
-rw-r--r--drivers/chibios/serial_usart.c352
1 files changed, 231 insertions, 121 deletions
diff --git a/drivers/chibios/serial_usart.c b/drivers/chibios/serial_usart.c
index cae29388c..ea4473791 100644
--- a/drivers/chibios/serial_usart.c
+++ b/drivers/chibios/serial_usart.c
@@ -16,190 +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 20static SerialConfig serial_config = SERIAL_USART_CONFIG;
21# ifndef SERIAL_USART_TX_PAL_MODE 21#else
22# define SERIAL_USART_TX_PAL_MODE 7 22static 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 34static 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
34static 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 36static inline bool react_to_transactions(void);
38 uint8_t dump[size]; 37static inline bool __attribute__((nonnull)) receive(uint8_t* destination, const size_t size);
39 sdRead(driver, dump, size); 38static inline bool __attribute__((nonnull)) send(const uint8_t* source, const size_t size);
39static inline int initiate_transaction(uint8_t sstd_index);
40static inline void usart_clear(void);
40 41
41 return ret; 42/**
43 * @brief Clear the receive input queue.
44 */
45static 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 65
46static inline msg_t sdWriteTimeoutHalfDuplex(SerialDriver* driver, uint8_t* data, uint8_t size, uint32_t timeout) { 66/**
47 msg_t ret = sdWriteTimeout(driver, data, size, timeout); 67 * @brief Blocking send of buffer with timeout.
48 68 *
49 // Half duplex requires us to read back the data we just wrote - just throw it away 69 * @return true Send success.
50 uint8_t dump[size]; 70 * @return false Send failed.
51 sdReadTimeout(driver, dump, size, timeout); 71 */
72static 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
58static 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 */
94static 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
64static 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
71void 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 */
77static THD_WORKING_AREA(waSlaveThread, 2048); 104__attribute__((weak)) void usart_init(void) {
78static 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;
96#endif 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
97} 141}
98 142
99void usart_master_init(void) { 143#endif
100 usart_init();
101 144
102 sdcfg.cr3 |= USART_CR3_HDSEL; 145/**
103 sdStart(&SERIAL_USART_DRIVER, &sdcfg); 146 * @brief Overridable master specific initializations.
147 */
148__attribute__((weak, nonnull)) void usart_master_init(SerialDriver** driver) {
149 (void)driver;
150 usart_init();
104} 151}
105 152
106void usart_slave_init(void) { 153/**
154 * @brief Overridable slave specific initializations.
155 */
156__attribute__((weak, nonnull)) void usart_slave_init(SerialDriver** driver) {
157 (void)driver;
107 usart_init(); 158 usart_init();
159}
108 160
109 sdcfg.cr3 |= USART_CR3_HDSEL; 161/**
110 sdStart(&SERIAL_USART_DRIVER, &sdcfg); 162 * @brief This thread runs on the slave and responds to transactions initiated
163 * by the master.
164 */
165static THD_WORKING_AREA(waSlaveThread, 1024);
166static THD_FUNCTION(SlaveThread, arg) {
167 (void)arg;
168 chRegSetThreadName("usart_tx_rx");
111 169
112 // Start transport thread 170 while (true) {
113 chThdCreateStatic(waSlaveThread, sizeof(waSlaveThread), HIGHPRIO, SlaveThread, NULL); 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 }
114} 177}
115 178
116static SSTD_t* Transaction_table = NULL; 179/**
117static uint8_t Transaction_table_size = 0; 180 * @brief Slave specific initializations.
181 */
182void soft_serial_target_init(void) {
183 usart_slave_init(&serial_driver);
118 184
119void soft_serial_initiator_init(SSTD_t* sstd_table, int sstd_table_size) { 185 sdStart(serial_driver, &serial_config);
120 Transaction_table = sstd_table;
121 Transaction_table_size = (uint8_t)sstd_table_size;
122 186
123 usart_master_init(); 187 /* Start transport thread. */
188 chThdCreateStatic(waSlaveThread, sizeof(waSlaveThread), HIGHPRIO, SlaveThread, NULL);
124} 189}
125 190
126void soft_serial_target_init(SSTD_t* sstd_table, int sstd_table_size) { 191/**
127 Transaction_table = sstd_table; 192 * @brief React to transactions started by the master.
128 Transaction_table_size = (uint8_t)sstd_table_size; 193 */
194static 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);
129 197
130 usart_slave_init(); 198 /* Sanity check that we are actually responding to a valid transaction. */
131} 199 if (sstd_index >= NUM_TOTAL_TRANSACTIONS) {
200 return false;
201 }
132 202
133void handle_soft_serial_slave(void) { 203 split_transaction_desc_t* trans = &split_transaction_table[sstd_index];
134 uint8_t sstd_index = sdGet(&SERIAL_USART_DRIVER); // first chunk is always transaction id
135 SSTD_t* trans = &Transaction_table[sstd_index];
136 204
137 // 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 */
138 sstd_index ^= HANDSHAKE_MAGIC; 207 sstd_index ^= HANDSHAKE_MAGIC;
139 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 }
140 212
213 /* Receive transaction buffer from the master. If this transaction requires it.*/
141 if (trans->initiator2target_buffer_size) { 214 if (trans->initiator2target_buffer_size) {
142 sdRead(&SERIAL_USART_DRIVER, trans->initiator2target_buffer, 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 }
143 } 219 }
144 220
145 if (trans->target2initiator_buffer_size) { 221 /* Allow any slave processing to occur. */
146 sdWrite(&SERIAL_USART_DRIVER, trans->target2initiator_buffer, trans->target2initiator_buffer_size); 222 if (trans->slave_callback) {
223 trans->slave_callback(trans->initiator2target_buffer_size, split_trans_initiator2target_buffer(trans), trans->initiator2target_buffer_size, split_trans_target2initiator_buffer(trans));
147 } 224 }
148 225
149 if (trans->status) { 226 /* Send transaction buffer to the master. If this transaction requires it. */
150 *trans->status = TRANSACTION_ACCEPTED; 227 if (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 }
151 } 232 }
233
234 *trans->status = TRANSACTION_ACCEPTED;
235 return true;
152} 236}
153 237
154///////// 238/**
155// start transaction by initiator 239 * @brief Master specific initializations.
156// 240 */
157// int soft_serial_transaction(int sstd_index) 241void soft_serial_initiator_init(void) {
158// 242 usart_master_init(&serial_driver);
159// Returns: 243
160// TRANSACTION_END 244#if defined(MCU_STM32) && defined(SERIAL_USART_PIN_SWAP)
161// TRANSACTION_NO_RESPONSE 245 serial_config.cr2 |= USART_CR2_SWAP; // master has swapped TX/RX pins
162// TRANSACTION_DATA_ERROR
163#ifndef SERIAL_USE_MULTI_TRANSACTION
164int soft_serial_transaction(void) {
165 uint8_t sstd_index = 0;
166#else
167int soft_serial_transaction(int index) {
168 uint8_t sstd_index = index;
169#endif 246#endif
170 247
171 if (sstd_index > Transaction_table_size) return TRANSACTION_TYPE_ERROR; 248 sdStart(serial_driver, &serial_config);
172 SSTD_t* trans = &Transaction_table[sstd_index]; 249}
173 msg_t res = 0; 250
251/**
252 * @brief Start transaction from the master half to the slave half.
253 *
254 * @param index Transaction Table index of the transaction to start.
255 * @return int TRANSACTION_NO_RESPONSE in case of Timeout.
256 * TRANSACTION_TYPE_ERROR in case of invalid transaction index.
257 * TRANSACTION_END in case of success.
258 */
259int soft_serial_transaction(int 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}
265
266/**
267 * @brief Initiate transaction to slave half.
268 */
269static 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 }
275
276 split_transaction_desc_t* trans = &split_transaction_table[sstd_index];
174 277
175 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 }
176 283
177 // First chunk is always transaction id 284 /* Send transaction table index to the slave, which doubles as basic handshake token. */
178 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 }
179 289
180 uint8_t sstd_index_shake = 0xFF; 290 uint8_t sstd_index_shake = 0xFF;
181 291
182 // 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.
183 // - 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*.
184 // - 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.
185 res = sdReadTimeout(&SERIAL_USART_DRIVER, &sstd_index_shake, sizeof(sstd_index_shake), TIME_MS2I(SERIAL_USART_TIMEOUT)); 295 */
186 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))) {
187 dprintf("serial::usart_shake NO_RESPONSE\n"); 297 dprintln("USART: Handshake failed.");
188 return TRANSACTION_NO_RESPONSE; 298 return TRANSACTION_NO_RESPONSE;
189 } 299 }
190 300
301 /* Send transaction buffer to the slave. If this transaction requires it. */
191 if (trans->initiator2target_buffer_size) { 302 if (trans->initiator2target_buffer_size) {
192 res = sdWriteTimeout(&SERIAL_USART_DRIVER, trans->initiator2target_buffer, trans->initiator2target_buffer_size, TIME_MS2I(SERIAL_USART_TIMEOUT)); 303 if (!send(split_trans_initiator2target_buffer(trans), trans->initiator2target_buffer_size)) {
193 if (res < 0) { 304 dprintln("USART: Send failed.");
194 dprintf("serial::usart_transmit NO_RESPONSE\n");
195 return TRANSACTION_NO_RESPONSE; 305 return TRANSACTION_NO_RESPONSE;
196 } 306 }
197 } 307 }
198 308
309 /* Receive transaction buffer from the slave. If this transaction requires it. */
199 if (trans->target2initiator_buffer_size) { 310 if (trans->target2initiator_buffer_size) {
200 res = sdReadTimeout(&SERIAL_USART_DRIVER, trans->target2initiator_buffer, trans->target2initiator_buffer_size, TIME_MS2I(SERIAL_USART_TIMEOUT)); 311 if (!receive(split_trans_target2initiator_buffer(trans), trans->target2initiator_buffer_size)) {
201 if (res < 0) { 312 dprintln("USART: Receive failed.");
202 dprintf("serial::usart_receive NO_RESPONSE\n");
203 return TRANSACTION_NO_RESPONSE; 313 return TRANSACTION_NO_RESPONSE;
204 } 314 }
205 } 315 }