diff options
| author | Stefan Kerkmann <karlk90@pm.me> | 2024-02-20 11:34:24 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-20 11:34:24 +0100 |
| commit | 61fa6949fbb537f54d48a4fc0218ff2b6873e940 (patch) | |
| tree | 400f8f4920888097c0e22c68cb5fa742b0e340c8 /platforms | |
| parent | 865a8f42a6128dfc09a24fe749b0d78d8c69b70e (diff) | |
[Core] Allow ChibiOS `SIO` driver for `UART` driver (#22839)
* onekey: stm32f3_disco: add usart pins and activate peripheral
Signed-off-by: Stefan Kerkmann <karlk90@pm.me>
* chibios: uart: change SD1 prefix to UART
Signed-off-by: Stefan Kerkmann <karlk90@pm.me>
* chibios: uart: add SIO driver and RP2040 compatibility
Signed-off-by: Stefan Kerkmann <karlk90@pm.me>
Co-authored-by: Sergey Vlasov <sigprof@gmail.com>
* Update platforms/chibios/drivers/uart.h
Co-authored-by: Joel Challis <git@zvecr.com>
---------
Signed-off-by: Stefan Kerkmann <karlk90@pm.me>
Co-authored-by: Sergey Vlasov <sigprof@gmail.com>
Co-authored-by: Joel Challis <git@zvecr.com>
Diffstat (limited to 'platforms')
| -rw-r--r-- | platforms/chibios/boards/GENERIC_PROMICRO_RP2040/configs/config.h | 28 | ||||
| -rw-r--r-- | platforms/chibios/boards/QMK_BLOK/configs/config.h | 24 | ||||
| -rw-r--r-- | platforms/chibios/boards/QMK_PM2040/configs/config.h | 24 | ||||
| -rw-r--r-- | platforms/chibios/chibios_config.h | 14 | ||||
| -rw-r--r-- | platforms/chibios/drivers/uart.c | 70 | ||||
| -rw-r--r-- | platforms/chibios/drivers/uart.h | 194 | ||||
| -rw-r--r-- | platforms/chibios/drivers/uart_serial.c | 65 | ||||
| -rw-r--r-- | platforms/chibios/drivers/uart_sio.c | 77 | ||||
| -rw-r--r-- | platforms/chibios/platform.mk | 11 |
9 files changed, 380 insertions, 127 deletions
diff --git a/platforms/chibios/boards/GENERIC_PROMICRO_RP2040/configs/config.h b/platforms/chibios/boards/GENERIC_PROMICRO_RP2040/configs/config.h index 9209e99e76..73c2b40f46 100644 --- a/platforms/chibios/boards/GENERIC_PROMICRO_RP2040/configs/config.h +++ b/platforms/chibios/boards/GENERIC_PROMICRO_RP2040/configs/config.h | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | // Copyright 2022 Stefan Kerkmann | 1 | // Copyright 2024 Stefan Kerkmann |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | 3 | ||
| 4 | #pragma once | 4 | #pragma once |
| @@ -56,7 +56,31 @@ | |||
| 56 | #endif | 56 | #endif |
| 57 | 57 | ||
| 58 | /**====================== | 58 | /**====================== |
| 59 | ** UART Driver | ||
| 60 | *========================**/ | ||
| 61 | |||
| 62 | #if !defined(UART_DRIVER) | ||
| 63 | # define UART_DRIVER SIOD0 | ||
| 64 | #endif | ||
| 65 | |||
| 66 | #if !defined(UART_TX_PIN) | ||
| 67 | # define UART_TX_PIN GP0 | ||
| 68 | #endif | ||
| 69 | |||
| 70 | #if !defined(UART_RX_PIN) | ||
| 71 | # define UART_RX_PIN GP1 | ||
| 72 | #endif | ||
| 73 | |||
| 74 | #if !defined(UART_CTS_PIN) | ||
| 75 | # define UART_CTS_PIN GP2 | ||
| 76 | #endif | ||
| 77 | |||
| 78 | #if !defined(UART_RTS_PIN) | ||
| 79 | # define UART_RTS_PIN GP3 | ||
| 80 | #endif | ||
| 81 | |||
| 82 | /**====================== | ||
| 59 | ** Double-tap | 83 | ** Double-tap |
| 60 | *========================**/ | 84 | *========================**/ |
| 61 | 85 | ||
| 62 | #define RP2040_BOOTLOADER_DOUBLE_TAP_RESET | 86 | #define RP2040_BOOTLOADER_DOUBLE_TAP_RESET |
diff --git a/platforms/chibios/boards/QMK_BLOK/configs/config.h b/platforms/chibios/boards/QMK_BLOK/configs/config.h index 168afb1fc4..834dc497d3 100644 --- a/platforms/chibios/boards/QMK_BLOK/configs/config.h +++ b/platforms/chibios/boards/QMK_BLOK/configs/config.h | |||
| @@ -3,6 +3,10 @@ | |||
| 3 | 3 | ||
| 4 | #pragma once | 4 | #pragma once |
| 5 | 5 | ||
| 6 | /**====================== | ||
| 7 | ** I2C Driver | ||
| 8 | *========================**/ | ||
| 9 | |||
| 6 | #ifndef I2C_DRIVER | 10 | #ifndef I2C_DRIVER |
| 7 | # define I2C_DRIVER I2CD0 | 11 | # define I2C_DRIVER I2CD0 |
| 8 | #endif | 12 | #endif |
| @@ -13,6 +17,26 @@ | |||
| 13 | # define I2C1_SCL_PIN D0 | 17 | # define I2C1_SCL_PIN D0 |
| 14 | #endif | 18 | #endif |
| 15 | 19 | ||
| 20 | /**====================== | ||
| 21 | ** UART Driver | ||
| 22 | *========================**/ | ||
| 23 | |||
| 24 | #ifndef UART_DRIVER | ||
| 25 | # define UART_DRIVER SIOD0 | ||
| 26 | #endif | ||
| 27 | |||
| 28 | #ifndef UART_TX_PIN | ||
| 29 | # define UART_TX_PIN D3 | ||
| 30 | #endif | ||
| 31 | |||
| 32 | #ifndef UART_RX_PIN | ||
| 33 | # define UART_RX_PIN D2 | ||
| 34 | #endif | ||
| 35 | |||
| 36 | /**====================== | ||
| 37 | ** Double-tap | ||
| 38 | *========================**/ | ||
| 39 | |||
| 16 | #ifndef RP2040_BOOTLOADER_DOUBLE_TAP_RESET | 40 | #ifndef RP2040_BOOTLOADER_DOUBLE_TAP_RESET |
| 17 | # define RP2040_BOOTLOADER_DOUBLE_TAP_RESET | 41 | # define RP2040_BOOTLOADER_DOUBLE_TAP_RESET |
| 18 | #endif | 42 | #endif |
diff --git a/platforms/chibios/boards/QMK_PM2040/configs/config.h b/platforms/chibios/boards/QMK_PM2040/configs/config.h index ec85ae0cf4..f8b46b7fe4 100644 --- a/platforms/chibios/boards/QMK_PM2040/configs/config.h +++ b/platforms/chibios/boards/QMK_PM2040/configs/config.h | |||
| @@ -3,6 +3,10 @@ | |||
| 3 | 3 | ||
| 4 | #pragma once | 4 | #pragma once |
| 5 | 5 | ||
| 6 | /**====================== | ||
| 7 | ** I2C Driver | ||
| 8 | *========================**/ | ||
| 9 | |||
| 6 | #ifndef I2C_DRIVER | 10 | #ifndef I2C_DRIVER |
| 7 | # define I2C_DRIVER I2CD1 | 11 | # define I2C_DRIVER I2CD1 |
| 8 | #endif | 12 | #endif |
| @@ -13,6 +17,26 @@ | |||
| 13 | # define I2C1_SCL_PIN D0 | 17 | # define I2C1_SCL_PIN D0 |
| 14 | #endif | 18 | #endif |
| 15 | 19 | ||
| 20 | /**====================== | ||
| 21 | ** UART Driver | ||
| 22 | *========================**/ | ||
| 23 | |||
| 24 | #ifndef UART_DRIVER | ||
| 25 | # define UART_DRIVER SIOD0 | ||
| 26 | #endif | ||
| 27 | |||
| 28 | #ifndef UART_TX_PIN | ||
| 29 | # define UART_TX_PIN D3 | ||
| 30 | #endif | ||
| 31 | |||
| 32 | #ifndef UART_RX_PIN | ||
| 33 | # define UART_RX_PIN D2 | ||
| 34 | #endif | ||
| 35 | |||
| 36 | /**====================== | ||
| 37 | ** Double-tap | ||
| 38 | *========================**/ | ||
| 39 | |||
| 16 | #ifndef RP2040_BOOTLOADER_DOUBLE_TAP_RESET | 40 | #ifndef RP2040_BOOTLOADER_DOUBLE_TAP_RESET |
| 17 | # define RP2040_BOOTLOADER_DOUBLE_TAP_RESET | 41 | # define RP2040_BOOTLOADER_DOUBLE_TAP_RESET |
| 18 | #endif | 42 | #endif |
diff --git a/platforms/chibios/chibios_config.h b/platforms/chibios/chibios_config.h index f1636f9da0..759ac6943b 100644 --- a/platforms/chibios/chibios_config.h +++ b/platforms/chibios/chibios_config.h | |||
| @@ -69,6 +69,20 @@ | |||
| 69 | # ifndef SPI_MISO_PAL_MODE | 69 | # ifndef SPI_MISO_PAL_MODE |
| 70 | # define SPI_MISO_PAL_MODE (PAL_MODE_ALTERNATE_SPI | PAL_RP_PAD_SLEWFAST | PAL_RP_PAD_DRIVE4) | 70 | # define SPI_MISO_PAL_MODE (PAL_MODE_ALTERNATE_SPI | PAL_RP_PAD_SLEWFAST | PAL_RP_PAD_DRIVE4) |
| 71 | # endif | 71 | # endif |
| 72 | |||
| 73 | # ifndef UART_TX_PAL_MODE | ||
| 74 | # define UART_TX_PAL_MODE PAL_MODE_ALTERNATE_UART | ||
| 75 | # endif | ||
| 76 | # ifndef UART_RX_PAL_MODE | ||
| 77 | # define UART_RX_PAL_MODE PAL_MODE_ALTERNATE_UART | ||
| 78 | # endif | ||
| 79 | # ifndef UART_CTS_PAL_MODE | ||
| 80 | # define UART_CTS_PAL_MODE PAL_MODE_ALTERNATE_UART | ||
| 81 | # endif | ||
| 82 | # ifndef UART_RTS_PAL_MODE | ||
| 83 | # define UART_RTS_PAL_MODE PAL_MODE_ALTERNATE_UART | ||
| 84 | # endif | ||
| 85 | |||
| 72 | #endif | 86 | #endif |
| 73 | 87 | ||
| 74 | // STM32 compatibility | 88 | // STM32 compatibility |
diff --git a/platforms/chibios/drivers/uart.c b/platforms/chibios/drivers/uart.c deleted file mode 100644 index 39a59dd445..0000000000 --- a/platforms/chibios/drivers/uart.c +++ /dev/null | |||
| @@ -1,70 +0,0 @@ | |||
| 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 | #if defined(MCU_KINETIS) | ||
| 20 | static SerialConfig serialConfig = {SERIAL_DEFAULT_BITRATE}; | ||
| 21 | #elif defined(WB32F3G71xx) || defined(WB32FQ95xx) | ||
| 22 | static SerialConfig serialConfig = {SERIAL_DEFAULT_BITRATE, SD1_WRDLEN, SD1_STPBIT, SD1_PARITY, SD1_ATFLCT}; | ||
| 23 | #else | ||
| 24 | static SerialConfig serialConfig = {SERIAL_DEFAULT_BITRATE, SD1_CR1, SD1_CR2, SD1_CR3}; | ||
| 25 | #endif | ||
| 26 | |||
| 27 | void uart_init(uint32_t baud) { | ||
| 28 | static bool is_initialised = false; | ||
| 29 | |||
| 30 | if (!is_initialised) { | ||
| 31 | is_initialised = true; | ||
| 32 | |||
| 33 | #if defined(MCU_KINETIS) | ||
| 34 | serialConfig.sc_speed = baud; | ||
| 35 | #else | ||
| 36 | serialConfig.speed = baud; | ||
| 37 | #endif | ||
| 38 | |||
| 39 | #if defined(USE_GPIOV1) | ||
| 40 | palSetLineMode(SD1_TX_PIN, SD1_TX_PAL_MODE); | ||
| 41 | palSetLineMode(SD1_RX_PIN, SD1_RX_PAL_MODE); | ||
| 42 | #else | ||
| 43 | palSetLineMode(SD1_TX_PIN, PAL_MODE_ALTERNATE(SD1_TX_PAL_MODE) | PAL_OUTPUT_TYPE_PUSHPULL | PAL_OUTPUT_SPEED_HIGHEST); | ||
| 44 | palSetLineMode(SD1_RX_PIN, PAL_MODE_ALTERNATE(SD1_RX_PAL_MODE) | PAL_OUTPUT_TYPE_PUSHPULL | PAL_OUTPUT_SPEED_HIGHEST); | ||
| 45 | #endif | ||
| 46 | sdStart(&SERIAL_DRIVER, &serialConfig); | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | void uart_write(uint8_t data) { | ||
| 51 | sdPut(&SERIAL_DRIVER, data); | ||
| 52 | } | ||
| 53 | |||
| 54 | uint8_t uart_read(void) { | ||
| 55 | msg_t res = sdGet(&SERIAL_DRIVER); | ||
| 56 | |||
| 57 | return (uint8_t)res; | ||
| 58 | } | ||
| 59 | |||
| 60 | void uart_transmit(const uint8_t *data, uint16_t length) { | ||
| 61 | sdWrite(&SERIAL_DRIVER, data, length); | ||
| 62 | } | ||
| 63 | |||
| 64 | void uart_receive(uint8_t *data, uint16_t length) { | ||
| 65 | sdRead(&SERIAL_DRIVER, data, length); | ||
| 66 | } | ||
| 67 | |||
| 68 | bool uart_available(void) { | ||
| 69 | return !sdGetWouldBlock(&SERIAL_DRIVER); | ||
| 70 | } | ||
diff --git a/platforms/chibios/drivers/uart.h b/platforms/chibios/drivers/uart.h index 16983072ce..c1945575f1 100644 --- a/platforms/chibios/drivers/uart.h +++ b/platforms/chibios/drivers/uart.h | |||
| @@ -1,18 +1,7 @@ | |||
| 1 | /* Copyright 2021 | 1 | // Copyright 2024 Stefan Kerkmann |
| 2 | * | 2 | // Copyright 2021 QMK |
| 3 | * This program is free software: you can redistribute it and/or modify | 3 | // Copyright 2024 Stefan Kerkmann |
| 4 | * it under the terms of the GNU General Public License as published by | 4 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 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 | 5 | ||
| 17 | #pragma once | 6 | #pragma once |
| 18 | 7 | ||
| @@ -24,93 +13,188 @@ | |||
| 24 | #include "gpio.h" | 13 | #include "gpio.h" |
| 25 | #include "chibios_config.h" | 14 | #include "chibios_config.h" |
| 26 | 15 | ||
| 27 | #ifndef SERIAL_DRIVER | 16 | // ======== DEPRECATED DEFINES - DO NOT USE ======== |
| 28 | # define SERIAL_DRIVER SD1 | 17 | #ifdef SERIAL_DRIVER |
| 18 | # define UART_DRIVER SERIAL_DRIVER | ||
| 19 | #endif | ||
| 20 | #ifdef SD1_TX_PIN | ||
| 21 | # define UART_TX_PIN SD1_TX_PIN | ||
| 22 | #endif | ||
| 23 | #ifdef SD1_RX_PIN | ||
| 24 | # define UART_RX_PIN SD1_RX_PIN | ||
| 25 | #endif | ||
| 26 | #ifdef SD1_CTS_PIN | ||
| 27 | # define UART_CTS_PIN SD1_CTS_PIN | ||
| 28 | #endif | ||
| 29 | #ifdef SD1_RTS_PIN | ||
| 30 | # define UART_RTS_PIN SD1_RTS_PIN | ||
| 31 | #endif | ||
| 32 | #ifdef SD1_TX_PAL_MODE | ||
| 33 | # define UART_TX_PAL_MODE SD1_TX_PAL_MODE | ||
| 34 | #endif | ||
| 35 | #ifdef SD1_RX_PAL_MODE | ||
| 36 | # define UART_RX_PAL_MODE SD1_RX_PAL_MODE | ||
| 37 | #endif | ||
| 38 | #ifdef SD1_CTS_PAL_MODE | ||
| 39 | # define UART_RTS_PAL_MODE SD1_CTS_PAL_MODE | ||
| 40 | #endif | ||
| 41 | #ifdef SD1_RTS_PAL_MODE | ||
| 42 | # define UART_TX_PAL_MODE SD1_RTS_PAL_MODE | ||
| 43 | #endif | ||
| 44 | #ifdef SD1_CR1 | ||
| 45 | # define UART_CR1 SD1_CR1 | ||
| 46 | #endif | ||
| 47 | #ifdef SD1_CR2 | ||
| 48 | # define UART_CR2 SD1_CR2 | ||
| 49 | #endif | ||
| 50 | #ifdef SD1_CR3 | ||
| 51 | # define UART_CR3 SD1_CR3 | ||
| 52 | #endif | ||
| 53 | #ifdef SD1_WRDLEN | ||
| 54 | # define UART_WRDLEN SD1_WRDLEN | ||
| 55 | #endif | ||
| 56 | #ifdef SD1_STPBIT | ||
| 57 | # define UART_STPBIT SD1_STPBIT | ||
| 58 | #endif | ||
| 59 | #ifdef SD1_PARITY | ||
| 60 | # define UART_PARITY SD1_PARITY | ||
| 61 | #endif | ||
| 62 | #ifdef SD1_ATFLCT | ||
| 63 | # define UART_ATFLCT SD1_ATFLCT | ||
| 64 | #endif | ||
| 65 | // ======== | ||
| 66 | |||
| 67 | #ifndef UART_DRIVER | ||
| 68 | # if (HAL_USE_SERIAL == TRUE) | ||
| 69 | # define UART_DRIVER SD1 | ||
| 70 | # elif (HAL_USE_SIO == TRUE) | ||
| 71 | # define UART_DRIVER SIOD1 | ||
| 72 | # endif | ||
| 29 | #endif | 73 | #endif |
| 30 | 74 | ||
| 31 | #ifndef SD1_TX_PIN | 75 | #ifndef UART_TX_PIN |
| 32 | # define SD1_TX_PIN A9 | 76 | # define UART_TX_PIN A9 |
| 33 | #endif | 77 | #endif |
| 34 | 78 | ||
| 35 | #ifndef SD1_RX_PIN | 79 | #ifndef UART_RX_PIN |
| 36 | # define SD1_RX_PIN A10 | 80 | # define UART_RX_PIN A10 |
| 37 | #endif | 81 | #endif |
| 38 | 82 | ||
| 39 | #ifndef SD1_CTS_PIN | 83 | #ifndef UART_CTS_PIN |
| 40 | # define SD1_CTS_PIN A11 | 84 | # define UART_CTS_PIN A11 |
| 41 | #endif | 85 | #endif |
| 42 | 86 | ||
| 43 | #ifndef SD1_RTS_PIN | 87 | #ifndef UART_RTS_PIN |
| 44 | # define SD1_RTS_PIN A12 | 88 | # define UART_RTS_PIN A12 |
| 45 | #endif | 89 | #endif |
| 46 | 90 | ||
| 47 | #ifdef USE_GPIOV1 | 91 | #ifdef USE_GPIOV1 |
| 48 | # ifndef SD1_TX_PAL_MODE | 92 | # ifndef UART_TX_PAL_MODE |
| 49 | # define SD1_TX_PAL_MODE PAL_MODE_ALTERNATE_PUSHPULL | 93 | # define UART_TX_PAL_MODE PAL_MODE_ALTERNATE_PUSHPULL |
| 50 | # endif | 94 | # endif |
| 51 | # ifndef SD1_RX_PAL_MODE | 95 | # ifndef UART_RX_PAL_MODE |
| 52 | # define SD1_RX_PAL_MODE PAL_MODE_INPUT | 96 | # define UART_RX_PAL_MODE PAL_MODE_INPUT |
| 53 | # endif | 97 | # endif |
| 54 | # ifndef SD1_CTS_PAL_MODE | 98 | # ifndef UART_CTS_PAL_MODE |
| 55 | # define SD1_CTS_PAL_MODE PAL_MODE_INPUT | 99 | # define UART_CTS_PAL_MODE PAL_MODE_INPUT |
| 56 | # endif | 100 | # endif |
| 57 | # ifndef SD1_RTS_PAL_MODE | 101 | # ifndef UART_RTS_PAL_MODE |
| 58 | # define SD1_RTS_PAL_MODE PAL_MODE_ALTERNATE_PUSHPULL | 102 | # define UART_RTS_PAL_MODE PAL_MODE_ALTERNATE_PUSHPULL |
| 59 | # endif | 103 | # endif |
| 60 | #else | 104 | #else |
| 61 | # ifndef SD1_TX_PAL_MODE | 105 | # ifndef UART_TX_PAL_MODE |
| 62 | # define SD1_TX_PAL_MODE 7 | 106 | # define UART_TX_PAL_MODE 7 |
| 63 | # endif | 107 | # endif |
| 64 | 108 | ||
| 65 | # ifndef SD1_RX_PAL_MODE | 109 | # ifndef UART_RX_PAL_MODE |
| 66 | # define SD1_RX_PAL_MODE 7 | 110 | # define UART_RX_PAL_MODE 7 |
| 67 | # endif | 111 | # endif |
| 68 | 112 | ||
| 69 | # ifndef SD1_CTS_PAL_MODE | 113 | # ifndef UART_CTS_PAL_MODE |
| 70 | # define SD1_CTS_PAL_MODE 7 | 114 | # define UART_CTS_PAL_MODE 7 |
| 71 | # endif | 115 | # endif |
| 72 | 116 | ||
| 73 | # ifndef SD1_RTS_PAL_MODE | 117 | # ifndef UART_RTS_PAL_MODE |
| 74 | # define SD1_RTS_PAL_MODE 7 | 118 | # define UART_RTS_PAL_MODE 7 |
| 75 | # endif | 119 | # endif |
| 76 | #endif | 120 | #endif |
| 77 | 121 | ||
| 78 | #ifndef SD1_CR1 | 122 | #ifndef UART_CR1 |
| 79 | # define SD1_CR1 0 | 123 | # define UART_CR1 0 |
| 80 | #endif | 124 | #endif |
| 81 | 125 | ||
| 82 | #ifndef SD1_CR2 | 126 | #ifndef UART_CR2 |
| 83 | # define SD1_CR2 0 | 127 | # define UART_CR2 0 |
| 84 | #endif | 128 | #endif |
| 85 | 129 | ||
| 86 | #ifndef SD1_CR3 | 130 | #ifndef UART_CR3 |
| 87 | # define SD1_CR3 0 | 131 | # define UART_CR3 0 |
| 88 | #endif | 132 | #endif |
| 89 | 133 | ||
| 90 | #ifndef SD1_WRDLEN | 134 | #ifndef UART_WRDLEN |
| 91 | # define SD1_WRDLEN 3 | 135 | # define UART_WRDLEN 3 |
| 92 | #endif | 136 | #endif |
| 93 | 137 | ||
| 94 | #ifndef SD1_STPBIT | 138 | #ifndef UART_STPBIT |
| 95 | # define SD1_STPBIT 0 | 139 | # define UART_STPBIT 0 |
| 96 | #endif | 140 | #endif |
| 97 | 141 | ||
| 98 | #ifndef SD1_PARITY | 142 | #ifndef UART_PARITY |
| 99 | # define SD1_PARITY 0 | 143 | # define UART_PARITY 0 |
| 100 | #endif | 144 | #endif |
| 101 | 145 | ||
| 102 | #ifndef SD1_ATFLCT | 146 | #ifndef UART_ATFLCT |
| 103 | # define SD1_ATFLCT 0 | 147 | # define UART_ATFLCT 0 |
| 104 | #endif | 148 | #endif |
| 105 | 149 | ||
| 150 | /** | ||
| 151 | * @brief Initialize the UART driver. This function must be called only once, | ||
| 152 | * before any of the below functions can be called. | ||
| 153 | * | ||
| 154 | * @param baud The baud rate to transmit and receive at. This may depend on the | ||
| 155 | * device you are communicating with. Common values are 1200, 2400, 4800, 9600, | ||
| 156 | * 19200, 38400, 57600, and 115200. | ||
| 157 | */ | ||
| 106 | void uart_init(uint32_t baud); | 158 | void uart_init(uint32_t baud); |
| 107 | 159 | ||
| 160 | /** | ||
| 161 | * @brief Transmit a single byte. | ||
| 162 | * | ||
| 163 | * @param data The byte to transmit. | ||
| 164 | */ | ||
| 108 | void uart_write(uint8_t data); | 165 | void uart_write(uint8_t data); |
| 109 | 166 | ||
| 167 | /** | ||
| 168 | * @brief Receive a single byte. | ||
| 169 | * | ||
| 170 | * @return uint8_t The byte read from the receive buffer. This function will | ||
| 171 | * block if the buffer is empty (ie. no data to read). | ||
| 172 | */ | ||
| 110 | uint8_t uart_read(void); | 173 | uint8_t uart_read(void); |
| 111 | 174 | ||
| 175 | /** | ||
| 176 | * @brief Transmit multiple bytes. | ||
| 177 | * | ||
| 178 | * @param data A pointer to the data to write from. | ||
| 179 | * @param length The number of bytes to write. Take care not to overrun the | ||
| 180 | * length of `data`. | ||
| 181 | */ | ||
| 112 | void uart_transmit(const uint8_t *data, uint16_t length); | 182 | void uart_transmit(const uint8_t *data, uint16_t length); |
| 113 | 183 | ||
| 184 | /** | ||
| 185 | * @brief Receive multiple bytes. | ||
| 186 | * | ||
| 187 | * @param data A pointer to the buffer to read into. | ||
| 188 | * @param length The number of bytes to read. Take care not to overrun the | ||
| 189 | * length of `data`. | ||
| 190 | */ | ||
| 114 | void uart_receive(uint8_t *data, uint16_t length); | 191 | void uart_receive(uint8_t *data, uint16_t length); |
| 115 | 192 | ||
| 193 | /** | ||
| 194 | * @brief Return whether the receive buffer contains data. Call this function | ||
| 195 | * to determine if `uart_read()` will return data immediately. | ||
| 196 | * | ||
| 197 | * @return true If there is data available to read. | ||
| 198 | * @return false If there is no data available to read. | ||
| 199 | */ | ||
| 116 | bool uart_available(void); | 200 | bool uart_available(void); |
diff --git a/platforms/chibios/drivers/uart_serial.c b/platforms/chibios/drivers/uart_serial.c new file mode 100644 index 0000000000..6aff4eae47 --- /dev/null +++ b/platforms/chibios/drivers/uart_serial.c | |||
| @@ -0,0 +1,65 @@ | |||
| 1 | // Copyright 2024 Stefan Kerkmann (@KarlK90) | ||
| 2 | // Copyright 2021 QMK | ||
| 3 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 4 | |||
| 5 | #include "uart.h" | ||
| 6 | |||
| 7 | #if defined(MCU_KINETIS) | ||
| 8 | static SerialConfig serialConfig = {SERIAL_DEFAULT_BITRATE}; | ||
| 9 | #elif defined(WB32F3G71xx) || defined(WB32FQ95xx) | ||
| 10 | static SerialConfig serialConfig = { | ||
| 11 | SERIAL_DEFAULT_BITRATE, UART_WRDLEN, UART_STPBIT, UART_PARITY, UART_ATFLCT, | ||
| 12 | }; | ||
| 13 | #else | ||
| 14 | static SerialConfig serialConfig = { | ||
| 15 | SERIAL_DEFAULT_BITRATE, | ||
| 16 | UART_CR1, | ||
| 17 | UART_CR2, | ||
| 18 | UART_CR3, | ||
| 19 | }; | ||
| 20 | #endif | ||
| 21 | |||
| 22 | void uart_init(uint32_t baud) { | ||
| 23 | static bool is_initialised = false; | ||
| 24 | |||
| 25 | if (is_initialised) { | ||
| 26 | return; | ||
| 27 | } | ||
| 28 | is_initialised = true; | ||
| 29 | |||
| 30 | #if defined(MCU_KINETIS) | ||
| 31 | serialConfig.sc_speed = baud; | ||
| 32 | #else | ||
| 33 | serialConfig.speed = baud; | ||
| 34 | #endif | ||
| 35 | |||
| 36 | #if defined(USE_GPIOV1) | ||
| 37 | palSetLineMode(UART_TX_PIN, UART_TX_PAL_MODE); | ||
| 38 | palSetLineMode(UART_RX_PIN, UART_RX_PAL_MODE); | ||
| 39 | #else | ||
| 40 | palSetLineMode(UART_TX_PIN, PAL_MODE_ALTERNATE(UART_TX_PAL_MODE) | PAL_OUTPUT_TYPE_PUSHPULL | PAL_OUTPUT_SPEED_HIGHEST); | ||
| 41 | palSetLineMode(UART_RX_PIN, PAL_MODE_ALTERNATE(UART_RX_PAL_MODE) | PAL_OUTPUT_TYPE_PUSHPULL | PAL_OUTPUT_SPEED_HIGHEST); | ||
| 42 | #endif | ||
| 43 | |||
| 44 | sdStart(&UART_DRIVER, &serialConfig); | ||
| 45 | } | ||
| 46 | |||
| 47 | void uart_write(uint8_t data) { | ||
| 48 | sdPut(&UART_DRIVER, data); | ||
| 49 | } | ||
| 50 | |||
| 51 | uint8_t uart_read(void) { | ||
| 52 | return (uint8_t)sdGet(&UART_DRIVER); | ||
| 53 | } | ||
| 54 | |||
| 55 | void uart_transmit(const uint8_t *data, uint16_t length) { | ||
| 56 | sdWrite(&UART_DRIVER, data, length); | ||
| 57 | } | ||
| 58 | |||
| 59 | void uart_receive(uint8_t *data, uint16_t length) { | ||
| 60 | sdRead(&UART_DRIVER, data, length); | ||
| 61 | } | ||
| 62 | |||
| 63 | bool uart_available(void) { | ||
| 64 | return !sdGetWouldBlock(&UART_DRIVER); | ||
| 65 | } | ||
diff --git a/platforms/chibios/drivers/uart_sio.c b/platforms/chibios/drivers/uart_sio.c new file mode 100644 index 0000000000..ebf51ae5a8 --- /dev/null +++ b/platforms/chibios/drivers/uart_sio.c | |||
| @@ -0,0 +1,77 @@ | |||
| 1 | // Copyright 2024 Stefan Kerkmann (@KarlK90) | ||
| 2 | // Copyright 2021 QMK | ||
| 3 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 4 | |||
| 5 | #include "uart.h" | ||
| 6 | |||
| 7 | #if defined(MCU_RP) | ||
| 8 | // 38400 baud, 8 data bits, 1 stop bit, no parity, no flow control | ||
| 9 | static SIOConfig sioConfig = { | ||
| 10 | .baud = SIO_DEFAULT_BITRATE, | ||
| 11 | .UARTLCR_H = (UART_UARTLCR_H_WLEN_8BITS | UART_UARTLCR_H_FEN), | ||
| 12 | .UARTCR = 0U, | ||
| 13 | .UARTIFLS = (UART_UARTIFLS_RXIFLSEL_1_8F | UART_UARTIFLS_TXIFLSEL_1_8E), | ||
| 14 | .UARTDMACR = 0U, | ||
| 15 | }; | ||
| 16 | #else | ||
| 17 | static SIOConfig sioConfig = { | ||
| 18 | .baud = SIO_DEFAULT_BITRATE, | ||
| 19 | # if defined(MCU_STM32) && defined(CHIBIOS_HAL_USARTv3) | ||
| 20 | .presc = USART_PRESC1, | ||
| 21 | # endif | ||
| 22 | .cr1 = UART_CR1, | ||
| 23 | .cr2 = UART_CR2, | ||
| 24 | .cr3 = UART_CR3, | ||
| 25 | }; | ||
| 26 | #endif | ||
| 27 | |||
| 28 | void uart_init(uint32_t baud) { | ||
| 29 | static bool is_initialised = false; | ||
| 30 | |||
| 31 | if (is_initialised) { | ||
| 32 | return; | ||
| 33 | } | ||
| 34 | is_initialised = true; | ||
| 35 | |||
| 36 | sioConfig.baud = baud; | ||
| 37 | |||
| 38 | #if defined(USE_GPIOV1) | ||
| 39 | palSetLineMode(UART_TX_PIN, UART_TX_PAL_MODE); | ||
| 40 | palSetLineMode(UART_RX_PIN, UART_RX_PAL_MODE); | ||
| 41 | #else | ||
| 42 | palSetLineMode(UART_TX_PIN, PAL_MODE_ALTERNATE(UART_TX_PAL_MODE) | PAL_OUTPUT_TYPE_PUSHPULL | PAL_OUTPUT_SPEED_HIGHEST); | ||
| 43 | palSetLineMode(UART_RX_PIN, PAL_MODE_ALTERNATE(UART_RX_PAL_MODE) | PAL_OUTPUT_TYPE_PUSHPULL | PAL_OUTPUT_SPEED_HIGHEST); | ||
| 44 | #endif | ||
| 45 | |||
| 46 | sioStart(&UART_DRIVER, &sioConfig); | ||
| 47 | } | ||
| 48 | |||
| 49 | void uart_write(uint8_t data) { | ||
| 50 | chnPutTimeout(&UART_DRIVER, data, TIME_INFINITE); | ||
| 51 | } | ||
| 52 | |||
| 53 | uint8_t uart_read(void) { | ||
| 54 | msg_t result = chnGetTimeout(&UART_DRIVER, TIME_INFINITE); | ||
| 55 | |||
| 56 | if (sioHasRXErrorsX(&UART_DRIVER)) { | ||
| 57 | sioGetAndClearErrors(&UART_DRIVER); | ||
| 58 | } | ||
| 59 | |||
| 60 | return (uint8_t)result; | ||
| 61 | } | ||
| 62 | |||
| 63 | void uart_transmit(const uint8_t *data, uint16_t length) { | ||
| 64 | chnWrite(&UART_DRIVER, data, length); | ||
| 65 | } | ||
| 66 | |||
| 67 | void uart_receive(uint8_t *data, uint16_t length) { | ||
| 68 | chnRead(&UART_DRIVER, data, length); | ||
| 69 | |||
| 70 | if (sioHasRXErrorsX(&UART_DRIVER)) { | ||
| 71 | sioGetAndClearErrors(&UART_DRIVER); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | bool uart_available() { | ||
| 76 | return !sioIsRXEmptyX(&UART_DRIVER); | ||
| 77 | } | ||
diff --git a/platforms/chibios/platform.mk b/platforms/chibios/platform.mk index f38a888012..a2178412f3 100644 --- a/platforms/chibios/platform.mk +++ b/platforms/chibios/platform.mk | |||
| @@ -330,6 +330,17 @@ ifeq ($(strip $(USE_CHIBIOS_CONTRIB)),yes) | |||
| 330 | endif | 330 | endif |
| 331 | 331 | ||
| 332 | # | 332 | # |
| 333 | # Extract supported HAL drivers | ||
| 334 | ############################################################################## | ||
| 335 | |||
| 336 | define add_lld_driver_define | ||
| 337 | $(eval driver := $(word 2,$(subst /LLD/, ,$(1)))) | ||
| 338 | $(eval OPT_DEFS += -DCHIBIOS_HAL_$(driver)) | ||
| 339 | endef | ||
| 340 | |||
| 341 | $(foreach dir,$(EXTRAINCDIRS),$(if $(findstring /LLD/,$(dir)),$(call add_lld_driver_define,$(dir)))) | ||
| 342 | |||
| 343 | # | ||
| 333 | # Project, sources and paths | 344 | # Project, sources and paths |
| 334 | ############################################################################## | 345 | ############################################################################## |
| 335 | 346 | ||
