summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan <fauxpark@gmail.com>2025-01-26 23:46:46 +1100
committerGitHub <noreply@github.com>2025-01-26 13:46:46 +0100
commit291d154d7b13efb83f24c76df26277f32b9f0a58 (patch)
treed7d2e05a18bdfbdd83258c8a91a70f16e57792e9
parente4f736e68518a09a922caf4638fca10f2ee3f38c (diff)
Unify UART headers (#24855)
* Remove deprecated defines * Move default config to .c files * Unify UART headers * Clean up docs * Reorganise PAL mode defaults
-rw-r--r--docs/drivers/uart.md26
-rw-r--r--drivers/uart.h62
-rw-r--r--platforms/avr/drivers/uart.h39
-rw-r--r--platforms/chibios/drivers/uart.h200
-rw-r--r--platforms/chibios/drivers/uart_serial.c83
-rw-r--r--platforms/chibios/drivers/uart_sio.c67
6 files changed, 225 insertions, 252 deletions
diff --git a/docs/drivers/uart.md b/docs/drivers/uart.md
index 7cc68727ee..b895266cab 100644
--- a/docs/drivers/uart.md
+++ b/docs/drivers/uart.md
@@ -45,17 +45,17 @@ To enable UART, modify your board's `mcuconf.h` to enable the peripheral you've
45 45
46Configuration-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. 46Configuration-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.
47 47
48| `config.h` override | Description | Default Value | 48|`config.h` Override|Description |Default|
49| --------------------------- | --------------------------------------------------------------- | ------------- | 49|-------------------|---------------------------------------------------------------|-------|
50| `#define UART_DRIVER` | USART peripheral to use - USART1 -> `SD1`, USART2 -> `SD2` etc. | `SD1` | 50|`UART_DRIVER` |USART peripheral to use - USART1 -> `SD1`, USART2 -> `SD2` etc.|`SD1` |
51| `#define UART_TX_PIN` | The pin to use for TX | `A9` | 51|`UART_TX_PIN` |The pin to use for TX |`A9` |
52| `#define UART_TX_PAL_MODE` | The alternate function mode for TX | `7` | 52|`UART_TX_PAL_MODE` |The alternate function mode for TX |`7` |
53| `#define UART_RX_PIN` | The pin to use for RX | `A10` | 53|`UART_RX_PIN` |The pin to use for RX |`A10` |
54| `#define UART_RX_PAL_MODE` | The alternate function mode for RX | `7` | 54|`UART_RX_PAL_MODE` |The alternate function mode for RX |`7` |
55| `#define UART_CTS_PIN` | The pin to use for CTS | `A11` | 55|`UART_CTS_PIN` |The pin to use for CTS |`A11` |
56| `#define UART_CTS_PAL_MODE` | The alternate function mode for CTS | `7` | 56|`UART_CTS_PAL_MODE`|The alternate function mode for CTS |`7` |
57| `#define UART_RTS_PIN` | The pin to use for RTS | `A12` | 57|`UART_RTS_PIN` |The pin to use for RTS |`A12` |
58| `#define UART_RTS_PAL_MODE` | The alternate function mode for RTS | `7` | 58|`UART_RTS_PAL_MODE`|The alternate function mode for RTS |`7` |
59 59
60## API {#api} 60## API {#api}
61 61
@@ -111,7 +111,7 @@ Receive multiple bytes.
111#### Arguments {#api-uart-receive-arguments} 111#### Arguments {#api-uart-receive-arguments}
112 112
113 - `uint8_t *data` 113 - `uint8_t *data`
114 A pointer to the buffer to read into. 114 A pointer to a buffer to read into.
115 - `uint16_t length` 115 - `uint16_t length`
116 The number of bytes to read. Take care not to overrun the length of `data`. 116 The number of bytes to read. Take care not to overrun the length of `data`.
117 117
@@ -123,4 +123,4 @@ Return whether the receive buffer contains data. Call this function to determine
123 123
124#### Return Value {#api-uart-available-return} 124#### Return Value {#api-uart-available-return}
125 125
126`true` if the receive buffer length is non-zero. 126`true` if there is data available to read.
diff --git a/drivers/uart.h b/drivers/uart.h
new file mode 100644
index 0000000000..c5068c86e1
--- /dev/null
+++ b/drivers/uart.h
@@ -0,0 +1,62 @@
1// Copyright 2025 QMK
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#pragma once
5
6#include <stdint.h>
7#include <stdbool.h>
8
9/**
10 * \file
11 *
12 * \defgroup uart UART API
13 *
14 * \brief API to communicate with UART devices.
15 * \{
16 */
17
18/**
19 * \brief Initialize the UART driver. This function must be called only once, before any of the below functions can be called.
20 *
21 * \param baud The baud rate to transmit and receive at. This may depend on the device you are communicating with. Common values are 1200, 2400, 4800, 9600, 19200, 38400, 57600, and 115200.
22 */
23void uart_init(uint32_t baud);
24
25/**
26 * \brief Transmit a single byte.
27 *
28 * \param data The byte to write.
29 */
30void uart_write(uint8_t data);
31
32/**
33 * \brief Receive a single byte.
34 *
35 * \return The byte read from the receive buffer. This function will block if the buffer is empty (ie. no data to read).
36 */
37uint8_t uart_read(void);
38
39/**
40 * \brief Transmit multiple bytes.
41 *
42 * \param data A pointer to the data to write from.
43 * \param length The number of bytes to write. Take care not to overrun the length of `data`.
44 */
45void uart_transmit(const uint8_t *data, uint16_t length);
46
47/**
48 * \brief Receive multiple bytes.
49 *
50 * \param data A pointer to a buffer to read into.
51 * \param length The number of bytes to read. Take care not to overrun the length of `data`.
52 */
53void uart_receive(uint8_t *data, uint16_t length);
54
55/**
56 * \brief Return whether the receive buffer contains data. Call this function to determine if `uart_read()` will return data immediately.
57 *
58 * \return true if there is data available to read.
59 */
60bool uart_available(void);
61
62/** \} */
diff --git a/platforms/avr/drivers/uart.h b/platforms/avr/drivers/uart.h
deleted file mode 100644
index e2dc664eda..0000000000
--- a/platforms/avr/drivers/uart.h
+++ /dev/null
@@ -1,39 +0,0 @@
1/* UART Example for Teensy USB Development Board
2 * http://www.pjrc.com/teensy/
3 * Copyright (c) 2009 PJRC.COM, LLC
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24#pragma once
25
26#include <stdint.h>
27#include <stdbool.h>
28
29void uart_init(uint32_t baud);
30
31void uart_write(uint8_t data);
32
33uint8_t uart_read(void);
34
35void uart_transmit(const uint8_t *data, uint16_t length);
36
37void uart_receive(uint8_t *data, uint16_t length);
38
39bool uart_available(void);
diff --git a/platforms/chibios/drivers/uart.h b/platforms/chibios/drivers/uart.h
deleted file mode 100644
index c1945575f1..0000000000
--- a/platforms/chibios/drivers/uart.h
+++ /dev/null
@@ -1,200 +0,0 @@
1// Copyright 2024 Stefan Kerkmann
2// Copyright 2021 QMK
3// Copyright 2024 Stefan Kerkmann
4// SPDX-License-Identifier: GPL-2.0-or-later
5
6#pragma once
7
8#include <stdint.h>
9#include <stdbool.h>
10
11#include <hal.h>
12
13#include "gpio.h"
14#include "chibios_config.h"
15
16// ======== DEPRECATED DEFINES - DO NOT USE ========
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
73#endif
74
75#ifndef UART_TX_PIN
76# define UART_TX_PIN A9
77#endif
78
79#ifndef UART_RX_PIN
80# define UART_RX_PIN A10
81#endif
82
83#ifndef UART_CTS_PIN
84# define UART_CTS_PIN A11
85#endif
86
87#ifndef UART_RTS_PIN
88# define UART_RTS_PIN A12
89#endif
90
91#ifdef USE_GPIOV1
92# ifndef UART_TX_PAL_MODE
93# define UART_TX_PAL_MODE PAL_MODE_ALTERNATE_PUSHPULL
94# endif
95# ifndef UART_RX_PAL_MODE
96# define UART_RX_PAL_MODE PAL_MODE_INPUT
97# endif
98# ifndef UART_CTS_PAL_MODE
99# define UART_CTS_PAL_MODE PAL_MODE_INPUT
100# endif
101# ifndef UART_RTS_PAL_MODE
102# define UART_RTS_PAL_MODE PAL_MODE_ALTERNATE_PUSHPULL
103# endif
104#else
105# ifndef UART_TX_PAL_MODE
106# define UART_TX_PAL_MODE 7
107# endif
108
109# ifndef UART_RX_PAL_MODE
110# define UART_RX_PAL_MODE 7
111# endif
112
113# ifndef UART_CTS_PAL_MODE
114# define UART_CTS_PAL_MODE 7
115# endif
116
117# ifndef UART_RTS_PAL_MODE
118# define UART_RTS_PAL_MODE 7
119# endif
120#endif
121
122#ifndef UART_CR1
123# define UART_CR1 0
124#endif
125
126#ifndef UART_CR2
127# define UART_CR2 0
128#endif
129
130#ifndef UART_CR3
131# define UART_CR3 0
132#endif
133
134#ifndef UART_WRDLEN
135# define UART_WRDLEN 3
136#endif
137
138#ifndef UART_STPBIT
139# define UART_STPBIT 0
140#endif
141
142#ifndef UART_PARITY
143# define UART_PARITY 0
144#endif
145
146#ifndef UART_ATFLCT
147# define UART_ATFLCT 0
148#endif
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 */
158void uart_init(uint32_t baud);
159
160/**
161 * @brief Transmit a single byte.
162 *
163 * @param data The byte to transmit.
164 */
165void uart_write(uint8_t data);
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 */
173uint8_t uart_read(void);
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 */
182void uart_transmit(const uint8_t *data, uint16_t length);
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 */
191void uart_receive(uint8_t *data, uint16_t length);
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 */
200bool uart_available(void);
diff --git a/platforms/chibios/drivers/uart_serial.c b/platforms/chibios/drivers/uart_serial.c
index 6aff4eae47..e0afb9768a 100644
--- a/platforms/chibios/drivers/uart_serial.c
+++ b/platforms/chibios/drivers/uart_serial.c
@@ -3,6 +3,89 @@
3// SPDX-License-Identifier: GPL-2.0-or-later 3// SPDX-License-Identifier: GPL-2.0-or-later
4 4
5#include "uart.h" 5#include "uart.h"
6#include "gpio.h"
7#include "chibios_config.h"
8#include <hal.h>
9
10#ifndef UART_DRIVER
11# define UART_DRIVER SD1
12#endif
13
14#ifndef UART_TX_PIN
15# define UART_TX_PIN A9
16#endif
17
18#ifndef UART_TX_PAL_MODE
19# ifdef USE_GPIOV1
20# define UART_TX_PAL_MODE PAL_MODE_ALTERNATE_PUSHPULL
21# else
22# define UART_TX_PAL_MODE 7
23# endif
24#endif
25
26#ifndef UART_RX_PIN
27# define UART_RX_PIN A10
28#endif
29
30#ifndef UART_RX_PAL_MODE
31# ifdef USE_GPIOV1
32# define UART_RX_PAL_MODE PAL_MODE_INPUT
33# else
34# define UART_RX_PAL_MODE 7
35# endif
36#endif
37
38#ifndef UART_CTS_PIN
39# define UART_CTS_PIN A11
40#endif
41
42#ifndef UART_CTS_PAL_MODE
43# ifdef USE_GPIOV1
44# define UART_CTS_PAL_MODE PAL_MODE_INPUT
45# else
46# define UART_CTS_PAL_MODE 7
47# endif
48#endif
49
50#ifndef UART_RTS_PIN
51# define UART_RTS_PIN A12
52#endif
53
54#ifndef UART_RTS_PAL_MODE
55# ifdef USE_GPIOV1
56# define UART_RTS_PAL_MODE PAL_MODE_ALTERNATE_PUSHPULL
57# else
58# define UART_RTS_PAL_MODE 7
59# endif
60#endif
61
62#ifndef UART_CR1
63# define UART_CR1 0
64#endif
65
66#ifndef UART_CR2
67# define UART_CR2 0
68#endif
69
70#ifndef UART_CR3
71# define UART_CR3 0
72#endif
73
74#ifndef UART_WRDLEN
75# define UART_WRDLEN 3
76#endif
77
78#ifndef UART_STPBIT
79# define UART_STPBIT 0
80#endif
81
82#ifndef UART_PARITY
83# define UART_PARITY 0
84#endif
85
86#ifndef UART_ATFLCT
87# define UART_ATFLCT 0
88#endif
6 89
7#if defined(MCU_KINETIS) 90#if defined(MCU_KINETIS)
8static SerialConfig serialConfig = {SERIAL_DEFAULT_BITRATE}; 91static SerialConfig serialConfig = {SERIAL_DEFAULT_BITRATE};
diff --git a/platforms/chibios/drivers/uart_sio.c b/platforms/chibios/drivers/uart_sio.c
index 442df1c54d..fc12f0abed 100644
--- a/platforms/chibios/drivers/uart_sio.c
+++ b/platforms/chibios/drivers/uart_sio.c
@@ -3,6 +3,73 @@
3// SPDX-License-Identifier: GPL-2.0-or-later 3// SPDX-License-Identifier: GPL-2.0-or-later
4 4
5#include "uart.h" 5#include "uart.h"
6#include "gpio.h"
7#include "chibios_config.h"
8#include <hal.h>
9
10#ifndef UART_DRIVER
11# define UART_DRIVER SIOD1
12#endif
13
14#ifndef UART_TX_PIN
15# define UART_TX_PIN A9
16#endif
17
18#ifndef UART_TX_PAL_MODE
19# ifdef USE_GPIOV1
20# define UART_TX_PAL_MODE PAL_MODE_ALTERNATE_PUSHPULL
21# else
22# define UART_TX_PAL_MODE 7
23# endif
24#endif
25
26#ifndef UART_RX_PIN
27# define UART_RX_PIN A10
28#endif
29
30#ifndef UART_RX_PAL_MODE
31# ifdef USE_GPIOV1
32# define UART_RX_PAL_MODE PAL_MODE_INPUT
33# else
34# define UART_RX_PAL_MODE 7
35# endif
36#endif
37
38#ifndef UART_CTS_PIN
39# define UART_CTS_PIN A11
40#endif
41
42#ifndef UART_CTS_PAL_MODE
43# ifdef USE_GPIOV1
44# define UART_CTS_PAL_MODE PAL_MODE_INPUT
45# else
46# define UART_CTS_PAL_MODE 7
47# endif
48#endif
49
50#ifndef UART_RTS_PIN
51# define UART_RTS_PIN A12
52#endif
53
54#ifndef UART_RTS_PAL_MODE
55# ifdef USE_GPIOV1
56# define UART_RTS_PAL_MODE PAL_MODE_ALTERNATE_PUSHPULL
57# else
58# define UART_RTS_PAL_MODE 7
59# endif
60#endif
61
62#ifndef UART_CR1
63# define UART_CR1 0
64#endif
65
66#ifndef UART_CR2
67# define UART_CR2 0
68#endif
69
70#ifndef UART_CR3
71# define UART_CR3 0
72#endif
6 73
7#if defined(MCU_RP) 74#if defined(MCU_RP)
8// 38400 baud, 8 data bits, 1 stop bit, no parity, no flow control 75// 38400 baud, 8 data bits, 1 stop bit, no parity, no flow control