summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--builddefs/common_features.mk13
-rw-r--r--docs/platformdev_rp2040.md15
-rw-r--r--docs/uart_driver.md30
-rw-r--r--keyboards/handwired/onekey/stm32f3_disco/config.h6
-rw-r--r--keyboards/handwired/onekey/stm32f3_disco/mcuconf.h6
-rw-r--r--platforms/chibios/boards/GENERIC_PROMICRO_RP2040/configs/config.h28
-rw-r--r--platforms/chibios/boards/QMK_BLOK/configs/config.h24
-rw-r--r--platforms/chibios/boards/QMK_PM2040/configs/config.h24
-rw-r--r--platforms/chibios/chibios_config.h14
-rw-r--r--platforms/chibios/drivers/uart.c70
-rw-r--r--platforms/chibios/drivers/uart.h194
-rw-r--r--platforms/chibios/drivers/uart_serial.c65
-rw-r--r--platforms/chibios/drivers/uart_sio.c77
-rw-r--r--platforms/chibios/platform.mk11
14 files changed, 428 insertions, 149 deletions
diff --git a/builddefs/common_features.mk b/builddefs/common_features.mk
index 58e41f5230..7227a5558e 100644
--- a/builddefs/common_features.mk
+++ b/builddefs/common_features.mk
@@ -960,6 +960,15 @@ ifeq ($(strip $(SPI_DRIVER_REQUIRED)), yes)
960endif 960endif
961 961
962ifeq ($(strip $(UART_DRIVER_REQUIRED)), yes) 962ifeq ($(strip $(UART_DRIVER_REQUIRED)), yes)
963 OPT_DEFS += -DHAL_USE_SERIAL=TRUE 963 ifeq ($(strip $(PLATFORM)), CHIBIOS)
964 QUANTUM_LIB_SRC += uart.c 964 ifneq ($(filter $(MCU_SERIES),RP2040),)
965 OPT_DEFS += -DHAL_USE_SIO=TRUE
966 QUANTUM_LIB_SRC += uart_sio.c
967 else
968 OPT_DEFS += -DHAL_USE_SERIAL=TRUE
969 QUANTUM_LIB_SRC += uart_serial.c
970 endif
971 else
972 QUANTUM_LIB_SRC += uart.c
973 endif
965endif 974endif
diff --git a/docs/platformdev_rp2040.md b/docs/platformdev_rp2040.md
index 890dadb6f0..593a8198eb 100644
--- a/docs/platformdev_rp2040.md
+++ b/docs/platformdev_rp2040.md
@@ -2,7 +2,7 @@
2 2
3The following table shows the current driver status for peripherals on RP2040 MCUs: 3The following table shows the current driver status for peripherals on RP2040 MCUs:
4 4
5| System | Support | 5| System | Support |
6| ---------------------------------------------------------------- | ---------------------------------------------- | 6| ---------------------------------------------------------------- | ---------------------------------------------- |
7| [ADC driver](adc_driver.md) | :heavy_check_mark: | 7| [ADC driver](adc_driver.md) | :heavy_check_mark: |
8| [Audio](audio_driver.md#pwm-hardware) | :heavy_check_mark: | 8| [Audio](audio_driver.md#pwm-hardware) | :heavy_check_mark: |
@@ -13,7 +13,7 @@ The following table shows the current driver status for peripherals on RP2040 MC
13| [External EEPROMs](eeprom_driver.md) | :heavy_check_mark: using `I2C` or `SPI` driver | 13| [External EEPROMs](eeprom_driver.md) | :heavy_check_mark: using `I2C` or `SPI` driver |
14| [EEPROM emulation](eeprom_driver.md#wear_leveling-configuration) | :heavy_check_mark: | 14| [EEPROM emulation](eeprom_driver.md#wear_leveling-configuration) | :heavy_check_mark: |
15| [serial driver](serial_driver.md) | :heavy_check_mark: using `SIO` or `PIO` driver | 15| [serial driver](serial_driver.md) | :heavy_check_mark: using `SIO` or `PIO` driver |
16| [UART driver](uart_driver.md) | Support planned (no ETA) | 16| [UART driver](uart_driver.md) | :heavy_check_mark: using `SIO` driver |
17 17
18## GPIO 18## GPIO
19 19
@@ -52,6 +52,13 @@ To configure the I2C driver please read the [ChibiOS/ARM](i2c_driver.md#arm-conf
52 52
53To configure the SPI driver please read the [ChibiOS/ARM](spi_driver.md#chibiosarm-configuration) section. 53To configure the SPI driver please read the [ChibiOS/ARM](spi_driver.md#chibiosarm-configuration) section.
54 54
55### UART Driver
56
57| RP2040 Peripheral | `mcuconf.h` values | `UART_DRIVER` |
58| ----------------- | ------------------ | ------------- |
59| `UART0` | `RP_SIO_USE_UART0` | `SIOD0` |
60| `UART1` | `RP_SIO_USE_UART1` | `SIOD1` |
61
55## Double-tap reset boot-loader entry :id=double-tap 62## Double-tap reset boot-loader entry :id=double-tap
56 63
57The double-tap reset mechanism is an alternate way in QMK to enter the embedded mass storage UF2 boot-loader of the RP2040. It enables bootloader entry by a fast double-tap of the reset pin on start up, which is similar to the behavior of AVR Pro Micros. This feature activated by default for the Pro Micro RP2040 board, but has to be configured for other boards. To activate it, add the following options to your keyboards `config.h` file: 64The double-tap reset mechanism is an alternate way in QMK to enter the embedded mass storage UF2 boot-loader of the RP2040. It enables bootloader entry by a fast double-tap of the reset pin on start up, which is similar to the behavior of AVR Pro Micros. This feature activated by default for the Pro Micro RP2040 board, but has to be configured for other boards. To activate it, add the following options to your keyboards `config.h` file:
@@ -87,6 +94,10 @@ This is the default board that is chosen, unless any other RP2040 board is selec
87| `SOFT_SERIAL_PIN` | undefined, use `SERIAL_USART_TX_PIN` | 94| `SOFT_SERIAL_PIN` | undefined, use `SERIAL_USART_TX_PIN` |
88| `SERIAL_USART_TX_PIN` | `GP0` | 95| `SERIAL_USART_TX_PIN` | `GP0` |
89| `SERIAL_USART_RX_PIN` | `GP1` | 96| `SERIAL_USART_RX_PIN` | `GP1` |
97| **UART driver** | |
98| `UART_DRIVER` | `SIOD0` |
99| `UART_TX_PIN` | `GP0` |
100| `UART_RX_PIN` | `GP1` |
90 101
91?> The pin-outs of Adafruit's KB2040 and Boardsource's Blok both deviate from the Sparkfun Pro Micro RP2040. Lookup the pin-out of these boards and adjust your keyboards pin definition accordingly if you want to use these boards. 102?> The pin-outs of Adafruit's KB2040 and Boardsource's Blok both deviate from the Sparkfun Pro Micro RP2040. Lookup the pin-out of these boards and adjust your keyboards pin definition accordingly if you want to use these boards.
92 103
diff --git a/docs/uart_driver.md b/docs/uart_driver.md
index a88278d543..9b0a92d23d 100644
--- a/docs/uart_driver.md
+++ b/docs/uart_driver.md
@@ -32,13 +32,7 @@ No special setup is required - just connect the `RX` and `TX` pins of your UART
32 32
33You'll need to determine which pins can be used for UART -- as an example, STM32 parts generally have multiple UART peripherals, labeled USART1, USART2, USART3 etc. 33You'll need to determine which pins can be used for UART -- as an example, STM32 parts generally have multiple UART peripherals, labeled USART1, USART2, USART3 etc.
34 34
35To enable UART, modify your board's `halconf.h` to enable the serial driver: 35To enable UART, modify your board's `mcuconf.h` to enable the peripheral you've chosen, for example:
36
37```c
38#define HAL_USE_SERIAL TRUE
39```
40
41Then, modify your board's `mcuconf.h` to enable the peripheral you've chosen, for example:
42 36
43```c 37```c
44#undef STM32_SERIAL_USE_USART2 38#undef STM32_SERIAL_USE_USART2
@@ -47,17 +41,17 @@ Then, modify your board's `mcuconf.h` to enable the peripheral you've chosen, fo
47 41
48Configuration-wise, you'll need to set up the peripheral as per your MCU's datasheet -- the defaults match the pins for a Proton-C, i.e. STM32F303. 42Configuration-wise, you'll need to set up the peripheral as per your MCU's datasheet -- the defaults match the pins for a Proton-C, i.e. STM32F303.
49 43
50|`config.h` override |Description |Default Value| 44| `config.h` override | Description | Default Value |
51|--------------------------|---------------------------------------------------------------|-------------| 45| --------------------------- | --------------------------------------------------------------- | ------------- |
52|`#define SERIAL_DRIVER` |USART peripheral to use - USART1 -> `SD1`, USART2 -> `SD2` etc.|`SD1` | 46| `#define UART_DRIVER` | USART peripheral to use - USART1 -> `SD1`, USART2 -> `SD2` etc. | `SD1` |
53|`#define SD1_TX_PIN` |The pin to use for TX |`A9` | 47| `#define UART_TX_PIN` | The pin to use for TX | `A9` |
54|`#define SD1_TX_PAL_MODE` |The alternate function mode for TX |`7` | 48| `#define UART_TX_PAL_MODE` | The alternate function mode for TX | `7` |
55|`#define SD1_RX_PIN` |The pin to use for RX |`A10` | 49| `#define UART_RX_PIN` | The pin to use for RX | `A10` |
56|`#define SD1_RX_PAL_MODE` |The alternate function mode for RX |`7` | 50| `#define UART_RX_PAL_MODE` | The alternate function mode for RX | `7` |
57|`#define SD1_CTS_PIN` |The pin to use for CTS |`A11` | 51| `#define UART_CTS_PIN` | The pin to use for CTS | `A11` |
58|`#define SD1_CTS_PAL_MODE`|The alternate function mode for CTS |`7` | 52| `#define UART_CTS_PAL_MODE` | The alternate function mode for CTS | `7` |
59|`#define SD1_RTS_PIN` |The pin to use for RTS |`A12` | 53| `#define UART_RTS_PIN` | The pin to use for RTS | `A12` |
60|`#define SD1_RTS_PAL_MODE`|The alternate function mode for RTS |`7` | 54| `#define UART_RTS_PAL_MODE` | The alternate function mode for RTS | `7` |
61 55
62## API :id=api 56## API :id=api
63 57
diff --git a/keyboards/handwired/onekey/stm32f3_disco/config.h b/keyboards/handwired/onekey/stm32f3_disco/config.h
index a0e3c54ec1..0927767772 100644
--- a/keyboards/handwired/onekey/stm32f3_disco/config.h
+++ b/keyboards/handwired/onekey/stm32f3_disco/config.h
@@ -4,3 +4,9 @@
4#pragma once 4#pragma once
5 5
6#define ADC_PIN A0 6#define ADC_PIN A0
7
8#define UART_TX_PIN C4
9#define UART_RX_PIN C5
10
11#define SERIAL_USART_TX_PIN C4
12#define SERIAL_USART_RX_PIN C4
diff --git a/keyboards/handwired/onekey/stm32f3_disco/mcuconf.h b/keyboards/handwired/onekey/stm32f3_disco/mcuconf.h
index 9172860b0f..520a5dc304 100644
--- a/keyboards/handwired/onekey/stm32f3_disco/mcuconf.h
+++ b/keyboards/handwired/onekey/stm32f3_disco/mcuconf.h
@@ -7,3 +7,9 @@
7 7
8#undef STM32_ADC_USE_ADC1 8#undef STM32_ADC_USE_ADC1
9#define STM32_ADC_USE_ADC1 TRUE 9#define STM32_ADC_USE_ADC1 TRUE
10
11#undef STM32_SIO_USE_USART1
12#define STM32_SIO_USE_USART1 TRUE
13
14#undef STM32_SERIAL_USE_USART1
15#define STM32_SERIAL_USE_USART1 TRUE
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)
20static SerialConfig serialConfig = {SERIAL_DEFAULT_BITRATE};
21#elif defined(WB32F3G71xx) || defined(WB32FQ95xx)
22static SerialConfig serialConfig = {SERIAL_DEFAULT_BITRATE, SD1_WRDLEN, SD1_STPBIT, SD1_PARITY, SD1_ATFLCT};
23#else
24static SerialConfig serialConfig = {SERIAL_DEFAULT_BITRATE, SD1_CR1, SD1_CR2, SD1_CR3};
25#endif
26
27void 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
50void uart_write(uint8_t data) {
51 sdPut(&SERIAL_DRIVER, data);
52}
53
54uint8_t uart_read(void) {
55 msg_t res = sdGet(&SERIAL_DRIVER);
56
57 return (uint8_t)res;
58}
59
60void uart_transmit(const uint8_t *data, uint16_t length) {
61 sdWrite(&SERIAL_DRIVER, data, length);
62}
63
64void uart_receive(uint8_t *data, uint16_t length) {
65 sdRead(&SERIAL_DRIVER, data, length);
66}
67
68bool 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 */
106void uart_init(uint32_t baud); 158void uart_init(uint32_t baud);
107 159
160/**
161 * @brief Transmit a single byte.
162 *
163 * @param data The byte to transmit.
164 */
108void uart_write(uint8_t data); 165void 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 */
110uint8_t uart_read(void); 173uint8_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 */
112void uart_transmit(const uint8_t *data, uint16_t length); 182void 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 */
114void uart_receive(uint8_t *data, uint16_t length); 191void 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 */
116bool uart_available(void); 200bool 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)
8static SerialConfig serialConfig = {SERIAL_DEFAULT_BITRATE};
9#elif defined(WB32F3G71xx) || defined(WB32FQ95xx)
10static SerialConfig serialConfig = {
11 SERIAL_DEFAULT_BITRATE, UART_WRDLEN, UART_STPBIT, UART_PARITY, UART_ATFLCT,
12};
13#else
14static SerialConfig serialConfig = {
15 SERIAL_DEFAULT_BITRATE,
16 UART_CR1,
17 UART_CR2,
18 UART_CR3,
19};
20#endif
21
22void 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
47void uart_write(uint8_t data) {
48 sdPut(&UART_DRIVER, data);
49}
50
51uint8_t uart_read(void) {
52 return (uint8_t)sdGet(&UART_DRIVER);
53}
54
55void uart_transmit(const uint8_t *data, uint16_t length) {
56 sdWrite(&UART_DRIVER, data, length);
57}
58
59void uart_receive(uint8_t *data, uint16_t length) {
60 sdRead(&UART_DRIVER, data, length);
61}
62
63bool 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
9static 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
17static 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
28void 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
49void uart_write(uint8_t data) {
50 chnPutTimeout(&UART_DRIVER, data, TIME_INFINITE);
51}
52
53uint8_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
63void uart_transmit(const uint8_t *data, uint16_t length) {
64 chnWrite(&UART_DRIVER, data, length);
65}
66
67void 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
75bool 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)
330endif 330endif
331 331
332# 332#
333# Extract supported HAL drivers
334##############################################################################
335
336define add_lld_driver_define
337 $(eval driver := $(word 2,$(subst /LLD/, ,$(1))))
338 $(eval OPT_DEFS += -DCHIBIOS_HAL_$(driver))
339endef
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