diff options
Diffstat (limited to 'drivers/chibios/serial_usart_duplex.c')
| -rw-r--r-- | drivers/chibios/serial_usart_duplex.c | 261 |
1 files changed, 261 insertions, 0 deletions
diff --git a/drivers/chibios/serial_usart_duplex.c b/drivers/chibios/serial_usart_duplex.c new file mode 100644 index 000000000..cc9b889ac --- /dev/null +++ b/drivers/chibios/serial_usart_duplex.c | |||
| @@ -0,0 +1,261 @@ | |||
| 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 | } | ||
