diff options
Diffstat (limited to 'drivers/chibios/serial_usart.c')
-rw-r--r-- | drivers/chibios/serial_usart.c | 337 |
1 files changed, 229 insertions, 108 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 | } |