aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docs/_summary.md1
-rw-r--r--docs/uart_driver.md90
-rw-r--r--drivers/avr/uart.c (renamed from tmk_core/common/uart.c)42
-rw-r--r--drivers/avr/uart.h34
-rw-r--r--drivers/chibios/uart.c59
-rw-r--r--drivers/chibios/uart.h77
-rw-r--r--keyboards/mschwingen/modelm/rules.mk2
-rw-r--r--keyboards/nullbitsco/nibble/remote_kb.c1
-rw-r--r--keyboards/nullbitsco/nibble/remote_kb.h1
-rw-r--r--keyboards/nullbitsco/nibble/rules.mk7
-rw-r--r--platforms/chibios/QMK_PROTON_C/configs/mcuconf.h4
-rw-r--r--tmk_core/common/uart.h8
12 files changed, 287 insertions, 39 deletions
diff --git a/docs/_summary.md b/docs/_summary.md
index 9bc0b193a..526caf926 100644
--- a/docs/_summary.md
+++ b/docs/_summary.md
@@ -138,6 +138,7 @@
138 * [WS2812 Driver](ws2812_driver.md) 138 * [WS2812 Driver](ws2812_driver.md)
139 * [EEPROM Driver](eeprom_driver.md) 139 * [EEPROM Driver](eeprom_driver.md)
140 * ['serial' Driver](serial_driver.md) 140 * ['serial' Driver](serial_driver.md)
141 * [UART Driver](uart_driver.md)
141 * [GPIO Controls](internals_gpio_control.md) 142 * [GPIO Controls](internals_gpio_control.md)
142 * [Keyboard Guidelines](hardware_keyboard_guidelines.md) 143 * [Keyboard Guidelines](hardware_keyboard_guidelines.md)
143 144
diff --git a/docs/uart_driver.md b/docs/uart_driver.md
new file mode 100644
index 000000000..4d1716975
--- /dev/null
+++ b/docs/uart_driver.md
@@ -0,0 +1,90 @@
1# UART Driver
2
3The UART drivers used in QMK have a set of common functions to allow portability between MCUs.
4
5Currently, this driver does not support enabling hardware flow control (the `RTS` and `CTS` pins) if available, but may do so in future.
6
7## AVR Configuration
8
9No special setup is required - just connect the `RX` and `TX` pins of your UART device to the opposite pins on the MCU:
10
11|MCU |`TX`|`RX`|`CTS`|`RTS`|
12|-------------|----|----|-----|-----|
13|ATmega16/32U2|`D3`|`D2`|`D7` |`D6` |
14|ATmega16/32U4|`D3`|`D2`|`D5` |`B7` |
15|AT90USB64/128|`D3`|`D2`|*n/a*|*n/a*|
16|ATmega32A |`D1`|`D0`|*n/a*|*n/a*|
17|ATmega328/P |`D1`|`D0`|*n/a*|*n/a*|
18
19## ChibiOS/ARM Configuration
20
21You'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.
22
23To enable UART, modify your board's `halconf.h` to enable the serial driver:
24
25```c
26#define HAL_USE_SERIAL TRUE
27```
28
29Then, modify your board's `mcuconf.h` to enable the peripheral you've chosen, for example:
30
31```c
32#undef STM32_SERIAL_USE_USART2
33#define STM32_SERIAL_USE_USART2 TRUE
34```
35
36Configuration-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.
37
38|`config.h` override |Description |Default Value|
39|--------------------------|---------------------------------------------------------------|-------------|
40|`#define SERIAL_DRIVER` |USART peripheral to use - USART1 -> `SD1`, USART2 -> `SD2` etc.|`SD1` |
41|`#define SD1_TX_PIN` |The pin to use for TX |`A9` |
42|`#define SD1_TX_PAL_MODE` |The alternate function mode for TX |`7` |
43|`#define SD1_RX_PIN` |The pin to use for RX |`A10` |
44|`#define SD1_RX_PAL_MODE` |The alternate function mode for RX |`7` |
45|`#define SD1_CTS_PIN` |The pin to use for CTS |`A11` |
46|`#define SD1_CTS_PAL_MODE`|The alternate function mode for CTS |`7` |
47|`#define SD1_RTS_PIN` |The pin to use for RTS |`A12` |
48|`#define SD1_RTS_PAL_MODE`|The alternate function mode for RTS |`7` |
49
50## Functions
51
52### `void uart_init(uint32_t baud)`
53
54Initialize the UART driver. This function must be called only once, before any of the below functions can be called.
55
56#### Arguments
57
58 - `uint32_t baud`
59 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.
60
61---
62
63### `void uart_putchar(uint8_t c)`
64
65Transmit a single byte.
66
67#### Arguments
68
69 - `uint8_t c`
70 The byte (character) to send, from 0 to 255.
71
72---
73
74### `uint8_t uart_getchar(void)`
75
76Receive a single byte.
77
78#### Return Value
79
80The byte read from the receive buffer.
81
82---
83
84### `bool uart_available(void)`
85
86Return whether the receive buffer contains data. Call this function to determine if `uart_getchar()` will return meaningful data.
87
88#### Return Value
89
90`true` if the receive buffer length is non-zero.
diff --git a/tmk_core/common/uart.c b/drivers/avr/uart.c
index 150e256c8..e866a9e4f 100644
--- a/tmk_core/common/uart.c
+++ b/drivers/avr/uart.c
@@ -1,5 +1,3 @@
1// TODO: Teensy support(ATMega32u4/AT90USB128)
2// Fixed for Arduino Duemilanove ATmega168p by Jun Wako
3/* UART Example for Teensy USB Development Board 1/* UART Example for Teensy USB Development Board
4 * http://www.pjrc.com/teensy/ 2 * http://www.pjrc.com/teensy/
5 * Copyright (c) 2009 PJRC.COM, LLC 3 * Copyright (c) 2009 PJRC.COM, LLC
@@ -31,22 +29,7 @@
31 29
32#include "uart.h" 30#include "uart.h"
33 31
34#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328__) 32#if defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega32U2__) || defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__)
35# define UDRn UDR0
36# define UBRRnL UBRR0L
37# define UCSRnA UCSR0A
38# define UCSRnB UCSR0B
39# define UCSRnC UCSR0C
40# define U2Xn U2X0
41# define RXENn RXEN0
42# define TXENn TXEN0
43# define RXCIEn RXCIE0
44# define UCSZn1 UCSZ01
45# define UCSZn0 UCSZ00
46# define UDRIEn UDRIE0
47# define USARTn_UDRE_vect USART_UDRE_vect
48# define USARTn_RX_vect USART_RX_vect
49#elif defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega32U2__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__)
50# define UDRn UDR1 33# define UDRn UDR1
51# define UBRRnL UBRR1L 34# define UBRRnL UBRR1L
52# define UCSRnA UCSR1A 35# define UCSRnA UCSR1A
@@ -76,6 +59,21 @@
76# define UDRIEn UDRIE 59# define UDRIEn UDRIE
77# define USARTn_UDRE_vect USART_UDRE_vect 60# define USARTn_UDRE_vect USART_UDRE_vect
78# define USARTn_RX_vect USART_RX_vect 61# define USARTn_RX_vect USART_RX_vect
62#elif defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__)
63# define UDRn UDR0
64# define UBRRnL UBRR0L
65# define UCSRnA UCSR0A
66# define UCSRnB UCSR0B
67# define UCSRnC UCSR0C
68# define U2Xn U2X0
69# define RXENn RXEN0
70# define TXENn TXEN0
71# define RXCIEn RXCIE0
72# define UCSZn1 UCSZ01
73# define UCSZn0 UCSZ00
74# define UDRIEn UDRIE0
75# define USARTn_UDRE_vect USART_UDRE_vect
76# define USARTn_RX_vect USART_RX_vect
79#endif 77#endif
80 78
81// These buffers may be any size from 2 to 256 bytes. 79// These buffers may be any size from 2 to 256 bytes.
@@ -131,16 +129,16 @@ uint8_t uart_getchar(void) {
131 return c; 129 return c;
132} 130}
133 131
134// Return the number of bytes waiting in the receive buffer. 132// Return whether the number of bytes waiting in the receive buffer is nonzero.
135// Call this before uart_getchar() to check if it will need 133// Call this before uart_getchar() to check if it will need
136// to wait for a byte to arrive. 134// to wait for a byte to arrive.
137uint8_t uart_available(void) { 135bool uart_available(void) {
138 uint8_t head, tail; 136 uint8_t head, tail;
139 137
140 head = rx_buffer_head; 138 head = rx_buffer_head;
141 tail = rx_buffer_tail; 139 tail = rx_buffer_tail;
142 if (head >= tail) return head - tail; 140 if (head >= tail) return (head - tail) > 0;
143 return RX_BUFFER_SIZE + head - tail; 141 return (RX_BUFFER_SIZE + head - tail) > 0;
144} 142}
145 143
146// Transmit Interrupt 144// Transmit Interrupt
diff --git a/drivers/avr/uart.h b/drivers/avr/uart.h
new file mode 100644
index 000000000..d5ec563db
--- /dev/null
+++ b/drivers/avr/uart.h
@@ -0,0 +1,34 @@
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
28void uart_init(uint32_t baud);
29
30void uart_putchar(uint8_t c);
31
32uint8_t uart_getchar(void);
33
34bool uart_available(void);
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
21static SerialConfig serialConfig = {
22 SERIAL_DEFAULT_BITRATE,
23 SD1_CR1,
24 SD1_CR2,
25 SD1_CR3
26};
27
28void 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
47void uart_putchar(uint8_t c) {
48 sdPut(&SERIAL_DRIVER, c);
49}
50
51uint8_t uart_getchar(void) {
52 msg_t res = sdGet(&SERIAL_DRIVER);
53
54 return (uint8_t)res;
55}
56
57bool 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
71void uart_init(uint32_t baud);
72
73void uart_putchar(uint8_t c);
74
75uint8_t uart_getchar(void);
76
77bool uart_available(void);
diff --git a/keyboards/mschwingen/modelm/rules.mk b/keyboards/mschwingen/modelm/rules.mk
index f3af26eee..6775dfa20 100644
--- a/keyboards/mschwingen/modelm/rules.mk
+++ b/keyboards/mschwingen/modelm/rules.mk
@@ -29,7 +29,7 @@ DYNAMIC_MACRO_ENABLE = yes
29UART_DEBUG = no 29UART_DEBUG = no
30 30
31SRC += matrix.c 31SRC += matrix.c
32QUANTUM_LIB_SRC += $(COMMON_DIR)/uart.c \ 32QUANTUM_LIB_SRC += uart.c \
33 spi_master.c 33 spi_master.c
34 34
35OPT_DEFS += -DSLEEP_LED_ENABLE # we need our own sleep callbacks to turn of WS2812 LEDs 35OPT_DEFS += -DSLEEP_LED_ENABLE # we need our own sleep callbacks to turn of WS2812 LEDs
diff --git a/keyboards/nullbitsco/nibble/remote_kb.c b/keyboards/nullbitsco/nibble/remote_kb.c
index 2e36f5f22..7a914993f 100644
--- a/keyboards/nullbitsco/nibble/remote_kb.c
+++ b/keyboards/nullbitsco/nibble/remote_kb.c
@@ -27,6 +27,7 @@ This will require a new communication protocol, as the current one is limited.
27*/ 27*/
28 28
29#include "remote_kb.h" 29#include "remote_kb.h"
30#include "uart.h"
30 31
31uint8_t 32uint8_t
32 msg[UART_MSG_LEN], 33 msg[UART_MSG_LEN],
diff --git a/keyboards/nullbitsco/nibble/remote_kb.h b/keyboards/nullbitsco/nibble/remote_kb.h
index e2b24655b..f4b5c43f5 100644
--- a/keyboards/nullbitsco/nibble/remote_kb.h
+++ b/keyboards/nullbitsco/nibble/remote_kb.h
@@ -16,7 +16,6 @@
16#pragma once 16#pragma once
17 17
18#include "quantum.h" 18#include "quantum.h"
19#include "tmk_core/common/uart.h"
20 19
21#define SERIAL_UART_BAUD 153600 //low error rate for 32u4 @ 16MHz 20#define SERIAL_UART_BAUD 153600 //low error rate for 32u4 @ 16MHz
22 21
diff --git a/keyboards/nullbitsco/nibble/rules.mk b/keyboards/nullbitsco/nibble/rules.mk
index 683e29086..934021402 100644
--- a/keyboards/nullbitsco/nibble/rules.mk
+++ b/keyboards/nullbitsco/nibble/rules.mk
@@ -1,9 +1,6 @@
1# MCU name 1# MCU name
2MCU = atmega32u4 2MCU = atmega32u4
3 3
4# Interrupt driven control endpoint task(+60)
5OPT_DEFS += -DINTERRUPT_CONTROL_ENDPOINT
6
7# Bootloader selection 4# Bootloader selection
8BOOTLOADER = atmel-dfu 5BOOTLOADER = atmel-dfu
9 6
@@ -31,5 +28,5 @@ CUSTOM_MATRIX = lite # Lite custom matrix
31SRC += matrix.c \ 28SRC += matrix.c \
32 bitc_led.c \ 29 bitc_led.c \
33 big_led.c \ 30 big_led.c \
34 remote_kb.c \ 31 remote_kb.c
35 tmk_core/common/uart.c 32QUANTUM_LIB_SRC += uart.c
diff --git a/platforms/chibios/QMK_PROTON_C/configs/mcuconf.h b/platforms/chibios/QMK_PROTON_C/configs/mcuconf.h
index ac2d9a1ee..4d7b586c0 100644
--- a/platforms/chibios/QMK_PROTON_C/configs/mcuconf.h
+++ b/platforms/chibios/QMK_PROTON_C/configs/mcuconf.h
@@ -212,8 +212,8 @@
212/* 212/*
213 * SERIAL driver system settings. 213 * SERIAL driver system settings.
214 */ 214 */
215#define STM32_SERIAL_USE_USART1 FALSE 215#define STM32_SERIAL_USE_USART1 TRUE
216#define STM32_SERIAL_USE_USART2 TRUE 216#define STM32_SERIAL_USE_USART2 FALSE
217#define STM32_SERIAL_USE_USART3 FALSE 217#define STM32_SERIAL_USE_USART3 FALSE
218#define STM32_SERIAL_USE_UART4 FALSE 218#define STM32_SERIAL_USE_UART4 FALSE
219#define STM32_SERIAL_USE_UART5 FALSE 219#define STM32_SERIAL_USE_UART5 FALSE
diff --git a/tmk_core/common/uart.h b/tmk_core/common/uart.h
deleted file mode 100644
index ea247b17b..000000000
--- a/tmk_core/common/uart.h
+++ /dev/null
@@ -1,8 +0,0 @@
1#pragma once
2
3#include <stdint.h>
4
5void uart_init(uint32_t baud);
6void uart_putchar(uint8_t c);
7uint8_t uart_getchar(void);
8uint8_t uart_available(void);