diff options
Diffstat (limited to 'drivers/chibios')
| -rw-r--r-- | drivers/chibios/serial_usart.c | 13 | ||||
| -rw-r--r-- | drivers/chibios/uart.c | 59 | ||||
| -rw-r--r-- | drivers/chibios/uart.h | 77 |
3 files changed, 144 insertions, 5 deletions
diff --git a/drivers/chibios/serial_usart.c b/drivers/chibios/serial_usart.c index a3e21f90b..7c81b1646 100644 --- a/drivers/chibios/serial_usart.c +++ b/drivers/chibios/serial_usart.c | |||
| @@ -58,7 +58,10 @@ | |||
| 58 | # error invalid SELECT_SOFT_SERIAL_SPEED value | 58 | # error invalid SELECT_SOFT_SERIAL_SPEED value |
| 59 | #endif | 59 | #endif |
| 60 | 60 | ||
| 61 | #define TIMEOUT 100 | 61 | #ifndef SERIAL_USART_TIMEOUT |
| 62 | # define SERIAL_USART_TIMEOUT 100 | ||
| 63 | #endif | ||
| 64 | |||
| 62 | #define HANDSHAKE_MAGIC 7 | 65 | #define HANDSHAKE_MAGIC 7 |
| 63 | 66 | ||
| 64 | static inline msg_t sdWriteHalfDuplex(SerialDriver* driver, uint8_t* data, uint8_t size) { | 67 | static inline msg_t sdWriteHalfDuplex(SerialDriver* driver, uint8_t* data, uint8_t size) { |
| @@ -201,21 +204,21 @@ int soft_serial_transaction(int index) { | |||
| 201 | sdClear(&SERIAL_USART_DRIVER); | 204 | sdClear(&SERIAL_USART_DRIVER); |
| 202 | 205 | ||
| 203 | // First chunk is always transaction id | 206 | // First chunk is always transaction id |
| 204 | sdWriteTimeout(&SERIAL_USART_DRIVER, &sstd_index, sizeof(sstd_index), TIME_MS2I(TIMEOUT)); | 207 | sdWriteTimeout(&SERIAL_USART_DRIVER, &sstd_index, sizeof(sstd_index), TIME_MS2I(SERIAL_USART_TIMEOUT)); |
| 205 | 208 | ||
| 206 | uint8_t sstd_index_shake = 0xFF; | 209 | uint8_t sstd_index_shake = 0xFF; |
| 207 | 210 | ||
| 208 | // Which we always read back first so that we can error out correctly | 211 | // Which we always read back first so that we can error out correctly |
| 209 | // - due to the half duplex limitations on return codes, we always have to read *something* | 212 | // - due to the half duplex limitations on return codes, we always have to read *something* |
| 210 | // - without the read, write only transactions *always* succeed, even during the boot process where the slave is not ready | 213 | // - without the read, write only transactions *always* succeed, even during the boot process where the slave is not ready |
| 211 | res = sdReadTimeout(&SERIAL_USART_DRIVER, &sstd_index_shake, sizeof(sstd_index_shake), TIME_MS2I(TIMEOUT)); | 214 | res = sdReadTimeout(&SERIAL_USART_DRIVER, &sstd_index_shake, sizeof(sstd_index_shake), TIME_MS2I(SERIAL_USART_TIMEOUT)); |
| 212 | if (res < 0 || (sstd_index_shake != (sstd_index ^ HANDSHAKE_MAGIC))) { | 215 | if (res < 0 || (sstd_index_shake != (sstd_index ^ HANDSHAKE_MAGIC))) { |
| 213 | dprintf("serial::usart_shake NO_RESPONSE\n"); | 216 | dprintf("serial::usart_shake NO_RESPONSE\n"); |
| 214 | return TRANSACTION_NO_RESPONSE; | 217 | return TRANSACTION_NO_RESPONSE; |
| 215 | } | 218 | } |
| 216 | 219 | ||
| 217 | if (trans->initiator2target_buffer_size) { | 220 | if (trans->initiator2target_buffer_size) { |
| 218 | res = sdWriteTimeout(&SERIAL_USART_DRIVER, trans->initiator2target_buffer, trans->initiator2target_buffer_size, TIME_MS2I(TIMEOUT)); | 221 | res = sdWriteTimeout(&SERIAL_USART_DRIVER, trans->initiator2target_buffer, trans->initiator2target_buffer_size, TIME_MS2I(SERIAL_USART_TIMEOUT)); |
| 219 | if (res < 0) { | 222 | if (res < 0) { |
| 220 | dprintf("serial::usart_transmit NO_RESPONSE\n"); | 223 | dprintf("serial::usart_transmit NO_RESPONSE\n"); |
| 221 | return TRANSACTION_NO_RESPONSE; | 224 | return TRANSACTION_NO_RESPONSE; |
| @@ -223,7 +226,7 @@ int soft_serial_transaction(int index) { | |||
| 223 | } | 226 | } |
| 224 | 227 | ||
| 225 | if (trans->target2initiator_buffer_size) { | 228 | if (trans->target2initiator_buffer_size) { |
| 226 | res = sdReadTimeout(&SERIAL_USART_DRIVER, trans->target2initiator_buffer, trans->target2initiator_buffer_size, TIME_MS2I(TIMEOUT)); | 229 | res = sdReadTimeout(&SERIAL_USART_DRIVER, trans->target2initiator_buffer, trans->target2initiator_buffer_size, TIME_MS2I(SERIAL_USART_TIMEOUT)); |
| 227 | if (res < 0) { | 230 | if (res < 0) { |
| 228 | dprintf("serial::usart_receive NO_RESPONSE\n"); | 231 | dprintf("serial::usart_receive NO_RESPONSE\n"); |
| 229 | return TRANSACTION_NO_RESPONSE; | 232 | return TRANSACTION_NO_RESPONSE; |
diff --git a/drivers/chibios/uart.c b/drivers/chibios/uart.c new file mode 100644 index 000000000..6e94899b9 --- /dev/null +++ b/drivers/chibios/uart.c | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | /* Copyright 2021 | ||
| 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 <https://www.gnu.org/licenses/>. | ||
| 15 | */ | ||
| 16 | |||
| 17 | #include "uart.h" | ||
| 18 | |||
| 19 | #include "quantum.h" | ||
| 20 | |||
| 21 | static SerialConfig serialConfig = { | ||
| 22 | SERIAL_DEFAULT_BITRATE, | ||
| 23 | SD1_CR1, | ||
| 24 | SD1_CR2, | ||
| 25 | SD1_CR3 | ||
| 26 | }; | ||
| 27 | |||
| 28 | void uart_init(uint32_t baud) { | ||
| 29 | static bool is_initialised = false; | ||
| 30 | |||
| 31 | if (!is_initialised) { | ||
| 32 | is_initialised = true; | ||
| 33 | |||
| 34 | serialConfig.speed = baud; | ||
| 35 | |||
| 36 | #if defined(USE_GPIOV1) | ||
| 37 | palSetLineMode(SD1_TX_PIN, PAL_MODE_STM32_ALTERNATE_OPENDRAIN); | ||
| 38 | palSetLineMode(SD1_RX_PIN, PAL_MODE_STM32_ALTERNATE_OPENDRAIN); | ||
| 39 | #else | ||
| 40 | palSetLineMode(SD1_TX_PIN, PAL_MODE_ALTERNATE(SD1_TX_PAL_MODE) | PAL_STM32_OTYPE_OPENDRAIN); | ||
| 41 | palSetLineMode(SD1_RX_PIN, PAL_MODE_ALTERNATE(SD1_RX_PAL_MODE) | PAL_STM32_OTYPE_OPENDRAIN); | ||
| 42 | #endif | ||
| 43 | sdStart(&SERIAL_DRIVER, &serialConfig); | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | void uart_putchar(uint8_t c) { | ||
| 48 | sdPut(&SERIAL_DRIVER, c); | ||
| 49 | } | ||
| 50 | |||
| 51 | uint8_t uart_getchar(void) { | ||
| 52 | msg_t res = sdGet(&SERIAL_DRIVER); | ||
| 53 | |||
| 54 | return (uint8_t)res; | ||
| 55 | } | ||
| 56 | |||
| 57 | bool uart_available(void) { | ||
| 58 | return !sdGetWouldBlock(&SERIAL_DRIVER); | ||
| 59 | } | ||
diff --git a/drivers/chibios/uart.h b/drivers/chibios/uart.h new file mode 100644 index 000000000..b4e20e9fd --- /dev/null +++ b/drivers/chibios/uart.h | |||
| @@ -0,0 +1,77 @@ | |||
| 1 | /* Copyright 2021 | ||
| 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 <https://www.gnu.org/licenses/>. | ||
| 15 | */ | ||
| 16 | |||
| 17 | #pragma once | ||
| 18 | |||
| 19 | #include <stdint.h> | ||
| 20 | |||
| 21 | #include <hal.h> | ||
| 22 | |||
| 23 | #ifndef SERIAL_DRIVER | ||
| 24 | # define SERIAL_DRIVER SD1 | ||
| 25 | #endif | ||
| 26 | |||
| 27 | #ifndef SD1_TX_PIN | ||
| 28 | # define SD1_TX_PIN A9 | ||
| 29 | #endif | ||
| 30 | |||
| 31 | #ifndef SD1_TX_PAL_MODE | ||
| 32 | # define SD1_TX_PAL_MODE 7 | ||
| 33 | #endif | ||
| 34 | |||
| 35 | #ifndef SD1_RX_PIN | ||
| 36 | # define SD1_RX_PIN A10 | ||
| 37 | #endif | ||
| 38 | |||
| 39 | #ifndef SD1_RX_PAL_MODE | ||
| 40 | # define SD1_RX_PAL_MODE 7 | ||
| 41 | #endif | ||
| 42 | |||
| 43 | #ifndef SD1_CTS_PIN | ||
| 44 | # define SD1_CTS_PIN A11 | ||
| 45 | #endif | ||
| 46 | |||
| 47 | #ifndef SD1_CTS_PAL_MODE | ||
| 48 | # define SD1_CTS_PAL_MODE 7 | ||
| 49 | #endif | ||
| 50 | |||
| 51 | #ifndef SD1_RTS_PIN | ||
| 52 | # define SD1_RTS_PIN A12 | ||
| 53 | #endif | ||
| 54 | |||
| 55 | #ifndef SD1_RTS_PAL_MODE | ||
| 56 | # define SD1_RTS_PAL_MODE 7 | ||
| 57 | #endif | ||
| 58 | |||
| 59 | #ifndef SD1_CR1 | ||
| 60 | # define SD1_CR1 0 | ||
| 61 | #endif | ||
| 62 | |||
| 63 | #ifndef SD1_CR2 | ||
| 64 | # define SD1_CR2 0 | ||
| 65 | #endif | ||
| 66 | |||
| 67 | #ifndef SD1_CR3 | ||
| 68 | # define SD1_CR3 0 | ||
| 69 | #endif | ||
| 70 | |||
| 71 | void uart_init(uint32_t baud); | ||
| 72 | |||
| 73 | void uart_putchar(uint8_t c); | ||
| 74 | |||
| 75 | uint8_t uart_getchar(void); | ||
| 76 | |||
| 77 | bool uart_available(void); | ||
