diff options
Diffstat (limited to 'drivers/chibios/serial_usart.c')
| -rw-r--r-- | drivers/chibios/serial_usart.c | 318 |
1 files changed, 0 insertions, 318 deletions
diff --git a/drivers/chibios/serial_usart.c b/drivers/chibios/serial_usart.c deleted file mode 100644 index ea4473791..000000000 --- a/drivers/chibios/serial_usart.c +++ /dev/null | |||
| @@ -1,318 +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 | #if defined(SERIAL_USART_CONFIG) | ||
| 20 | static SerialConfig serial_config = SERIAL_USART_CONFIG; | ||
| 21 | #else | ||
| 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) | ||
| 30 | # endif | ||
| 31 | }; | ||
| 32 | #endif | ||
| 33 | |||
| 34 | static SerialDriver* serial_driver = &SERIAL_USART_DRIVER; | ||
| 35 | |||
| 36 | static inline bool react_to_transactions(void); | ||
| 37 | static inline bool __attribute__((nonnull)) receive(uint8_t* destination, const size_t 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); | ||
| 41 | |||
| 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 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | /** | ||
| 67 | * @brief Blocking send of buffer with 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 | ||
| 84 | |||
| 85 | return success; | ||
| 86 | } | ||
| 87 | |||
| 88 | /** | ||
| 89 | * @brief Blocking receive of size * bytes with timeout. | ||
| 90 | * | ||
| 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; | ||
| 97 | } | ||
| 98 | |||
| 99 | #if !defined(SERIAL_USART_FULL_DUPLEX) | ||
| 100 | |||
| 101 | /** | ||
| 102 | * @brief Initiate pins for USART peripheral. Half-duplex configuration. | ||
| 103 | */ | ||
| 104 | __attribute__((weak)) void usart_init(void) { | ||
| 105 | # if defined(MCU_STM32) | ||
| 106 | # if defined(USE_GPIOV1) | ||
| 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 | ||
| 111 | |||
| 112 | # if defined(USART_REMAP) | ||
| 113 | USART_REMAP; | ||
| 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 | ||
| 118 | } | ||
| 119 | |||
| 120 | #else | ||
| 121 | |||
| 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) | ||
| 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 | |||
| 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(); | ||
| 151 | } | ||
| 152 | |||
| 153 | /** | ||
| 154 | * @brief Overridable slave specific initializations. | ||
| 155 | */ | ||
| 156 | __attribute__((weak, nonnull)) void usart_slave_init(SerialDriver** driver) { | ||
| 157 | (void)driver; | ||
| 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"); | ||
| 169 | |||
| 170 | while (true) { | ||
| 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 | } | ||
| 177 | } | ||
| 178 | |||
| 179 | /** | ||
| 180 | * @brief Slave specific initializations. | ||
| 181 | */ | ||
| 182 | void soft_serial_target_init(void) { | ||
| 183 | usart_slave_init(&serial_driver); | ||
| 184 | |||
| 185 | sdStart(serial_driver, &serial_config); | ||
| 186 | |||
| 187 | /* Start transport thread. */ | ||
| 188 | chThdCreateStatic(waSlaveThread, sizeof(waSlaveThread), HIGHPRIO, SlaveThread, NULL); | ||
| 189 | } | ||
| 190 | |||
| 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); | ||
| 197 | |||
| 198 | /* Sanity check that we are actually responding to a valid transaction. */ | ||
| 199 | if (sstd_index >= NUM_TOTAL_TRANSACTIONS) { | ||
| 200 | return false; | ||
| 201 | } | ||
| 202 | |||
| 203 | split_transaction_desc_t* trans = &split_transaction_table[sstd_index]; | ||
| 204 | |||
| 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 */ | ||
| 207 | sstd_index ^= HANDSHAKE_MAGIC; | ||
| 208 | if (!send(&sstd_index, sizeof(sstd_index))) { | ||
| 209 | *trans->status = TRANSACTION_DATA_ERROR; | ||
| 210 | return false; | ||
| 211 | } | ||
| 212 | |||
| 213 | /* Receive transaction buffer from the master. If this transaction requires it.*/ | ||
| 214 | if (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 | } | ||
| 219 | } | ||
| 220 | |||
| 221 | /* Allow any slave processing to occur. */ | ||
| 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)); | ||
| 224 | } | ||
| 225 | |||
| 226 | /* Send transaction buffer to the master. If this transaction requires it. */ | ||
| 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 | } | ||
| 232 | } | ||
| 233 | |||
| 234 | *trans->status = TRANSACTION_ACCEPTED; | ||
| 235 | return true; | ||
| 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); | ||
| 249 | } | ||
| 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 | */ | ||
| 259 | int 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 | */ | ||
| 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 | } | ||
| 275 | |||
| 276 | split_transaction_desc_t* trans = &split_transaction_table[sstd_index]; | ||
| 277 | |||
| 278 | /* Transaction is not registered. Abort. */ | ||
| 279 | if (!trans->status) { | ||
| 280 | dprintln("USART: Transaction not registered."); | ||
| 281 | return TRANSACTION_TYPE_ERROR; | ||
| 282 | } | ||
| 283 | |||
| 284 | /* Send transaction table index to the slave, which doubles as basic handshake token. */ | ||
| 285 | if (!send(&sstd_index, sizeof(sstd_index))) { | ||
| 286 | dprintln("USART: Send Handshake failed."); | ||
| 287 | return TRANSACTION_TYPE_ERROR; | ||
| 288 | } | ||
| 289 | |||
| 290 | uint8_t sstd_index_shake = 0xFF; | ||
| 291 | |||
| 292 | /* Which we always read back first so that we can error out correctly. | ||
| 293 | * - due to the half duplex limitations on return codes, we always have to read *something*. | ||
| 294 | * - without the read, write only transactions *always* succeed, even during the boot process where the slave is not ready. | ||
| 295 | */ | ||
| 296 | if (!receive(&sstd_index_shake, sizeof(sstd_index_shake)) || (sstd_index_shake != (sstd_index ^ HANDSHAKE_MAGIC))) { | ||
| 297 | dprintln("USART: Handshake failed."); | ||
| 298 | return TRANSACTION_NO_RESPONSE; | ||
| 299 | } | ||
| 300 | |||
| 301 | /* Send transaction buffer to the slave. If this transaction requires it. */ | ||
| 302 | if (trans->initiator2target_buffer_size) { | ||
| 303 | if (!send(split_trans_initiator2target_buffer(trans), trans->initiator2target_buffer_size)) { | ||
| 304 | dprintln("USART: Send failed."); | ||
| 305 | return TRANSACTION_NO_RESPONSE; | ||
| 306 | } | ||
| 307 | } | ||
| 308 | |||
| 309 | /* Receive transaction buffer from the slave. If this transaction requires it. */ | ||
| 310 | if (trans->target2initiator_buffer_size) { | ||
| 311 | if (!receive(split_trans_target2initiator_buffer(trans), trans->target2initiator_buffer_size)) { | ||
| 312 | dprintln("USART: Receive failed."); | ||
| 313 | return TRANSACTION_NO_RESPONSE; | ||
| 314 | } | ||
| 315 | } | ||
| 316 | |||
| 317 | return TRANSACTION_END; | ||
| 318 | } | ||
