From b3e0139f2bd1f94a58c7a2032b0cd70046ca951f Mon Sep 17 00:00:00 2001 From: fauxpark Date: Wed, 20 Jan 2021 20:30:33 +1100 Subject: [PATCH 1/8] UART driver refactor --- {tmk_core/common => drivers/avr}/uart.c | 0 {tmk_core/common => drivers/avr}/uart.h | 7 ++- drivers/chibios/uart.c | 60 +++++++++++++++++++++ drivers/chibios/uart.h | 70 +++++++++++++++++++++++++ keyboards/mschwingen/modelm/rules.mk | 2 +- keyboards/nullbitsco/nibble/remote_kb.c | 1 + keyboards/nullbitsco/nibble/remote_kb.h | 1 - keyboards/nullbitsco/nibble/rules.mk | 7 +-- 8 files changed, 139 insertions(+), 9 deletions(-) rename {tmk_core/common => drivers/avr}/uart.c (100%) rename {tmk_core/common => drivers/avr}/uart.h (58%) create mode 100644 drivers/chibios/uart.c create mode 100644 drivers/chibios/uart.h diff --git a/tmk_core/common/uart.c b/drivers/avr/uart.c similarity index 100% rename from tmk_core/common/uart.c rename to drivers/avr/uart.c diff --git a/tmk_core/common/uart.h b/drivers/avr/uart.h similarity index 58% rename from tmk_core/common/uart.h rename to drivers/avr/uart.h index ea247b17b8db..81a47689c83c 100644 --- a/tmk_core/common/uart.h +++ b/drivers/avr/uart.h @@ -2,7 +2,10 @@ #include -void uart_init(uint32_t baud); -void uart_putchar(uint8_t c); +void uart_init(uint32_t baud); + +void uart_putchar(uint8_t c); + uint8_t uart_getchar(void); + uint8_t uart_available(void); diff --git a/drivers/chibios/uart.c b/drivers/chibios/uart.c new file mode 100644 index 000000000000..7cb05e8b5d41 --- /dev/null +++ b/drivers/chibios/uart.c @@ -0,0 +1,60 @@ +/* Copyright 2021 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "uart.h" + +#include "quantum.h" + +static SerialConfig serialConfig = { + SERIAL_DEFAULT_BITRATE, + 0, + USART_CR2_STOP_1, + 0 +}; + +void uart_init(uint32_t baud) { + static bool is_initialised = false; + + if (!is_initialised) { + is_initialised = true; + + serialConfig.speed = baud; + +#if defined(USE_GPIOV1) + palSetPadMode(SD1_TX_BANK, SD1_TX, PAL_MODE_STM32_ALTERNATE_OPENDRAIN); + palSetPadMode(SD1_RX_BANK, SD1_RX, PAL_MODE_STM32_ALTERNATE_OPENDRAIN); +#else + palSetPadMode(SD1_TX_BANK, SD1_TX, PAL_MODE_ALTERNATE(SD1_TX_PAL_MODE) | PAL_STM32_OTYPE_OPENDRAIN); + palSetPadMode(SD1_RX_BANK, SD1_RX, PAL_MODE_ALTERNATE(SD1_RX_PAL_MODE) | PAL_STM32_OTYPE_OPENDRAIN); +#endif + sdStart(&SERIAL_DRIVER, &serialConfig); + } +} + +void uart_putchar(uint8_t c) { + sdPut(&SERIAL_DRIVER, c); +} + +uint8_t uart_getchar(void) { + msg_t res = sdGet(&SERIAL_DRIVER); + + return (uint8_t)res; +} + +uint8_t uart_available(void) { + // TODO + return 0; +} diff --git a/drivers/chibios/uart.h b/drivers/chibios/uart.h new file mode 100644 index 000000000000..e3c4641dfff9 --- /dev/null +++ b/drivers/chibios/uart.h @@ -0,0 +1,70 @@ +/* Copyright 2021 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include + +#include + +#ifdef SD1_BANK +# define SD1_TX_BANK SD1_BANK +# define SD1_RX_BANK SD1_BANK +#endif + +#ifndef SD1_TX_BANK +# define SD1_TX_BANK GPIOA +#endif + +#ifndef SD1_RX_BANK +# define SD1_RX_BANK GPIOA +#endif + +#ifndef SD1_TX +# define SD1_TX 9 +#endif +#ifndef SD1_RX +# define SD1_RX 10 +#endif + +#ifndef SERIAL_DRIVER +# define SERIAL_DRIVER SD1 +#endif + +#ifdef USE_GPIOV1 +# ifndef SD1_TX_PAL_MODE +# define SD1_TX_PAL_MODE PAL_MODE_STM32_ALTERNATE_OPENDRAIN +# endif +# ifndef SD1_RX_PAL_MODE +# define SD1_RX_PAL_MODE PAL_MODE_STM32_ALTERNATE_OPENDRAIN +# endif +#else +// The default PAL alternate modes are used to signal that the pins are used for USART +# ifndef SD1_TX_PAL_MODE +# define SD1_TX_PAL_MODE 7 +# endif +# ifndef SD1_RX_PAL_MODE +# define SD1_RX_PAL_MODE 7 +# endif +#endif + +void uart_init(uint32_t baud); + +void uart_putchar(uint8_t c); + +uint8_t uart_getchar(void); + +uint8_t uart_available(void); diff --git a/keyboards/mschwingen/modelm/rules.mk b/keyboards/mschwingen/modelm/rules.mk index f3af26eeeb0f..6775dfa203b6 100644 --- a/keyboards/mschwingen/modelm/rules.mk +++ b/keyboards/mschwingen/modelm/rules.mk @@ -29,7 +29,7 @@ DYNAMIC_MACRO_ENABLE = yes UART_DEBUG = no SRC += matrix.c -QUANTUM_LIB_SRC += $(COMMON_DIR)/uart.c \ +QUANTUM_LIB_SRC += uart.c \ spi_master.c OPT_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 2e36f5f22e76..7a914993f310 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. */ #include "remote_kb.h" +#include "uart.h" uint8_t msg[UART_MSG_LEN], diff --git a/keyboards/nullbitsco/nibble/remote_kb.h b/keyboards/nullbitsco/nibble/remote_kb.h index e2b24655b5db..f4b5c43f5dae 100644 --- a/keyboards/nullbitsco/nibble/remote_kb.h +++ b/keyboards/nullbitsco/nibble/remote_kb.h @@ -16,7 +16,6 @@ #pragma once #include "quantum.h" -#include "tmk_core/common/uart.h" #define SERIAL_UART_BAUD 153600 //low error rate for 32u4 @ 16MHz diff --git a/keyboards/nullbitsco/nibble/rules.mk b/keyboards/nullbitsco/nibble/rules.mk index 683e29086698..934021402786 100644 --- a/keyboards/nullbitsco/nibble/rules.mk +++ b/keyboards/nullbitsco/nibble/rules.mk @@ -1,9 +1,6 @@ # MCU name MCU = atmega32u4 -# Interrupt driven control endpoint task(+60) -OPT_DEFS += -DINTERRUPT_CONTROL_ENDPOINT - # Bootloader selection BOOTLOADER = atmel-dfu @@ -31,5 +28,5 @@ CUSTOM_MATRIX = lite # Lite custom matrix SRC += matrix.c \ bitc_led.c \ big_led.c \ - remote_kb.c \ - tmk_core/common/uart.c + remote_kb.c +QUANTUM_LIB_SRC += uart.c From edd0adf6f23ab9b959a061df89cb97f4c374163f Mon Sep 17 00:00:00 2001 From: fauxpark Date: Wed, 20 Jan 2021 21:30:54 +1100 Subject: [PATCH 2/8] Turn on USART1 for Proton-C board, rather than USART2 This matches up with A9/A10 as TX/RX on the pinout --- platforms/chibios/QMK_PROTON_C/configs/mcuconf.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/platforms/chibios/QMK_PROTON_C/configs/mcuconf.h b/platforms/chibios/QMK_PROTON_C/configs/mcuconf.h index ac2d9a1eed5d..4d7b586c085e 100644 --- a/platforms/chibios/QMK_PROTON_C/configs/mcuconf.h +++ b/platforms/chibios/QMK_PROTON_C/configs/mcuconf.h @@ -212,8 +212,8 @@ /* * SERIAL driver system settings. */ -#define STM32_SERIAL_USE_USART1 FALSE -#define STM32_SERIAL_USE_USART2 TRUE +#define STM32_SERIAL_USE_USART1 TRUE +#define STM32_SERIAL_USE_USART2 FALSE #define STM32_SERIAL_USE_USART3 FALSE #define STM32_SERIAL_USE_UART4 FALSE #define STM32_SERIAL_USE_UART5 FALSE From 5d1954742e821bf9b8694fbaab4ff7c03c01e5c1 Mon Sep 17 00:00:00 2001 From: fauxpark Date: Thu, 21 Jan 2021 14:47:55 +1100 Subject: [PATCH 3/8] Make CR1/2/3 configurable --- drivers/chibios/uart.c | 6 +++--- drivers/chibios/uart.h | 12 ++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/drivers/chibios/uart.c b/drivers/chibios/uart.c index 7cb05e8b5d41..e4bf3a5ea63c 100644 --- a/drivers/chibios/uart.c +++ b/drivers/chibios/uart.c @@ -20,9 +20,9 @@ static SerialConfig serialConfig = { SERIAL_DEFAULT_BITRATE, - 0, - USART_CR2_STOP_1, - 0 + SD1_CR1, + SD1_CR2, + SD1_CR3 }; void uart_init(uint32_t baud) { diff --git a/drivers/chibios/uart.h b/drivers/chibios/uart.h index e3c4641dfff9..735f81a0ca9c 100644 --- a/drivers/chibios/uart.h +++ b/drivers/chibios/uart.h @@ -44,6 +44,18 @@ # define SERIAL_DRIVER SD1 #endif +#ifndef SD1_CR1 +# define SD1_CR1 0 +#endif + +#ifndef SD1_CR2 +# define SD1_CR2 0 +#endif + +#ifndef SD1_CR3 +# define SD1_CR3 0 +#endif + #ifdef USE_GPIOV1 # ifndef SD1_TX_PAL_MODE # define SD1_TX_PAL_MODE PAL_MODE_STM32_ALTERNATE_OPENDRAIN From 558077338c064002b340748efaad18b84e355361 Mon Sep 17 00:00:00 2001 From: fauxpark Date: Thu, 21 Jan 2021 17:26:26 +1100 Subject: [PATCH 4/8] Change return value of `uart_available()` to bool, implement for ChibiOS --- drivers/avr/uart.c | 8 ++++---- drivers/avr/uart.h | 2 +- drivers/chibios/uart.c | 5 ++--- drivers/chibios/uart.h | 2 +- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/drivers/avr/uart.c b/drivers/avr/uart.c index 150e256c8f2f..0b74b203a2da 100644 --- a/drivers/avr/uart.c +++ b/drivers/avr/uart.c @@ -131,16 +131,16 @@ uint8_t uart_getchar(void) { return c; } -// Return the number of bytes waiting in the receive buffer. +// Return whether the number of bytes waiting in the receive buffer is nonzero. // Call this before uart_getchar() to check if it will need // to wait for a byte to arrive. -uint8_t uart_available(void) { +bool uart_available(void) { uint8_t head, tail; head = rx_buffer_head; tail = rx_buffer_tail; - if (head >= tail) return head - tail; - return RX_BUFFER_SIZE + head - tail; + if (head >= tail) return (head - tail) > 0; + return (RX_BUFFER_SIZE + head - tail) > 0; } // Transmit Interrupt diff --git a/drivers/avr/uart.h b/drivers/avr/uart.h index 81a47689c83c..aaae0a125dcc 100644 --- a/drivers/avr/uart.h +++ b/drivers/avr/uart.h @@ -8,4 +8,4 @@ void uart_putchar(uint8_t c); uint8_t uart_getchar(void); -uint8_t uart_available(void); +bool uart_available(void); diff --git a/drivers/chibios/uart.c b/drivers/chibios/uart.c index e4bf3a5ea63c..152a5bb0ec4f 100644 --- a/drivers/chibios/uart.c +++ b/drivers/chibios/uart.c @@ -54,7 +54,6 @@ uint8_t uart_getchar(void) { return (uint8_t)res; } -uint8_t uart_available(void) { - // TODO - return 0; +bool uart_available(void) { + return !sdGetWouldBlock(&SERIAL_DRIVER); } diff --git a/drivers/chibios/uart.h b/drivers/chibios/uart.h index 735f81a0ca9c..e9520ba8fe16 100644 --- a/drivers/chibios/uart.h +++ b/drivers/chibios/uart.h @@ -79,4 +79,4 @@ void uart_putchar(uint8_t c); uint8_t uart_getchar(void); -uint8_t uart_available(void); +bool uart_available(void); From 9a307002edc54c87897f899516aa546bd0d521e8 Mon Sep 17 00:00:00 2001 From: fauxpark Date: Thu, 21 Jan 2021 20:16:03 +1100 Subject: [PATCH 5/8] Add docs, simplify ChibiOS pin config --- docs/_summary.md | 1 + docs/uart_driver.md | 79 ++++++++++++++++++++++++++++++++++++++++++ drivers/chibios/uart.c | 8 ++--- drivers/chibios/uart.h | 55 +++++++++++++---------------- 4 files changed, 109 insertions(+), 34 deletions(-) create mode 100644 docs/uart_driver.md diff --git a/docs/_summary.md b/docs/_summary.md index 8d401bb40ce2..a74882b043c4 100644 --- a/docs/_summary.md +++ b/docs/_summary.md @@ -138,6 +138,7 @@ * [WS2812 Driver](ws2812_driver.md) * [EEPROM Driver](eeprom_driver.md) * ['serial' Driver](serial_driver.md) + * [UART Driver](uart_driver.md) * [GPIO Controls](internals_gpio_control.md) * [Keyboard Guidelines](hardware_keyboard_guidelines.md) diff --git a/docs/uart_driver.md b/docs/uart_driver.md new file mode 100644 index 000000000000..200586d8c49d --- /dev/null +++ b/docs/uart_driver.md @@ -0,0 +1,79 @@ +# UART Driver + +The UART drivers used in QMK have a set of common functions to allow portability between MCUs. + +Currently, this driver does not support enabling hardware flow control (the `RTS` and `CTS` pins) if available, but will do so in future. + +## AVR Configuration + +No special setup is required - just connect the `RX` and `TX` pins of your UART device to the opposite pins on the MCU: + +|MCU |`TX`|`RX`|`CTS`|`RTS`| +|-------------|----|----|-----|-----| +|ATmega16/32U2|`D3`|`D2`|`D7` |`D6` | +|ATmega16/32U4|`D3`|`D2`|`D5` |`B7` | +|AT90USB64/128|`D3`|`D2`|*n/a*|*n/a*| +|ATmega32A |`D1`|`D0`|*n/a*|*n/a*| +|ATmega328/P |`D1`|`D0`|*n/a*|*n/a*| + +## ChibiOS/ARM Configuration + +You'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. + +To enable UART, modify your board's `mcuconf.h` to enable the peripheral you've chosen -- in the case of using USART2, modify `STM32_SERIAL_USE_USART2` to be `TRUE`. + +Configuration-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. + +|`config.h` override |Description |Default Value| +|--------------------------|---------------------------------------------------------------|-------------| +|`#define SERIAL_DRIVER` |USART peripheral to use - USART1 => `SD1`, USART2 => `SD2` etc.|`SD1` | +|`#define SD1_TX_PIN` |The pin to use for the TX line |`A9` | +|`#define SD1_TX_PAL_MODE` |The alternate function mode for the TX pin |`7` | +|`#define SD1_RX_PIN` |The pin to use for the RX line |`A10` | +|`#define SD1_RX_PAL_MODE` |The alternate function mode for the RX pin |`7` | +|`#define SD1_CTS_PIN` |The pin to use for the CTS line |`A11` | +|`#define SD1_CTS_PAL_MODE`|The alternate function mode for the CTS pin |`7` | +|`#define SD1_RTS_PIN` |The pin to use for the RTS line |`A12` | +|`#define SD1_RTS_PAL_MODE`|The alternate function mode for the RTS pin |`7` | + +## Functions + +### `void uart_init(uint32_t baud)` + +Initialize the UART driver. This function must be called only once, before any of the below functions can be called. + +#### Arguments + + - `uint32_t 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. + +--- + +### `void uart_putchar(uint8_t c)` + +Transmit a single byte. + +#### Arguments + + - `uint8_t c` + The byte (character) to send, from 0 to 255. + +--- + +### `uint8_t uart_getchar(void)` + +Receive a single byte. + +#### Return Value + +The byte read from the receive buffer. + +--- + +### `bool uart_available(void)` + +Return whether the receive buffer contains data. Call this function to determine if `uart_getchar()` will return meaningful data. + +#### Return Value + +`true` if the receive buffer length is non-zero. diff --git a/drivers/chibios/uart.c b/drivers/chibios/uart.c index 152a5bb0ec4f..6e94899b9d6f 100644 --- a/drivers/chibios/uart.c +++ b/drivers/chibios/uart.c @@ -34,11 +34,11 @@ void uart_init(uint32_t baud) { serialConfig.speed = baud; #if defined(USE_GPIOV1) - palSetPadMode(SD1_TX_BANK, SD1_TX, PAL_MODE_STM32_ALTERNATE_OPENDRAIN); - palSetPadMode(SD1_RX_BANK, SD1_RX, PAL_MODE_STM32_ALTERNATE_OPENDRAIN); + palSetLineMode(SD1_TX_PIN, PAL_MODE_STM32_ALTERNATE_OPENDRAIN); + palSetLineMode(SD1_RX_PIN, PAL_MODE_STM32_ALTERNATE_OPENDRAIN); #else - palSetPadMode(SD1_TX_BANK, SD1_TX, PAL_MODE_ALTERNATE(SD1_TX_PAL_MODE) | PAL_STM32_OTYPE_OPENDRAIN); - palSetPadMode(SD1_RX_BANK, SD1_RX, PAL_MODE_ALTERNATE(SD1_RX_PAL_MODE) | PAL_STM32_OTYPE_OPENDRAIN); + palSetLineMode(SD1_TX_PIN, PAL_MODE_ALTERNATE(SD1_TX_PAL_MODE) | PAL_STM32_OTYPE_OPENDRAIN); + palSetLineMode(SD1_RX_PIN, PAL_MODE_ALTERNATE(SD1_RX_PAL_MODE) | PAL_STM32_OTYPE_OPENDRAIN); #endif sdStart(&SERIAL_DRIVER, &serialConfig); } diff --git a/drivers/chibios/uart.h b/drivers/chibios/uart.h index e9520ba8fe16..b4e20e9fd3d0 100644 --- a/drivers/chibios/uart.h +++ b/drivers/chibios/uart.h @@ -20,28 +20,40 @@ #include -#ifdef SD1_BANK -# define SD1_TX_BANK SD1_BANK -# define SD1_RX_BANK SD1_BANK +#ifndef SERIAL_DRIVER +# define SERIAL_DRIVER SD1 #endif -#ifndef SD1_TX_BANK -# define SD1_TX_BANK GPIOA +#ifndef SD1_TX_PIN +# define SD1_TX_PIN A9 #endif -#ifndef SD1_RX_BANK -# define SD1_RX_BANK GPIOA +#ifndef SD1_TX_PAL_MODE +# define SD1_TX_PAL_MODE 7 #endif -#ifndef SD1_TX -# define SD1_TX 9 +#ifndef SD1_RX_PIN +# define SD1_RX_PIN A10 #endif -#ifndef SD1_RX -# define SD1_RX 10 + +#ifndef SD1_RX_PAL_MODE +# define SD1_RX_PAL_MODE 7 #endif -#ifndef SERIAL_DRIVER -# define SERIAL_DRIVER SD1 +#ifndef SD1_CTS_PIN +# define SD1_CTS_PIN A11 +#endif + +#ifndef SD1_CTS_PAL_MODE +# define SD1_CTS_PAL_MODE 7 +#endif + +#ifndef SD1_RTS_PIN +# define SD1_RTS_PIN A12 +#endif + +#ifndef SD1_RTS_PAL_MODE +# define SD1_RTS_PAL_MODE 7 #endif #ifndef SD1_CR1 @@ -56,23 +68,6 @@ # define SD1_CR3 0 #endif -#ifdef USE_GPIOV1 -# ifndef SD1_TX_PAL_MODE -# define SD1_TX_PAL_MODE PAL_MODE_STM32_ALTERNATE_OPENDRAIN -# endif -# ifndef SD1_RX_PAL_MODE -# define SD1_RX_PAL_MODE PAL_MODE_STM32_ALTERNATE_OPENDRAIN -# endif -#else -// The default PAL alternate modes are used to signal that the pins are used for USART -# ifndef SD1_TX_PAL_MODE -# define SD1_TX_PAL_MODE 7 -# endif -# ifndef SD1_RX_PAL_MODE -# define SD1_RX_PAL_MODE 7 -# endif -#endif - void uart_init(uint32_t baud); void uart_putchar(uint8_t c); From 3b59cdfe2faf50519cd9d4851a04d54e877eaf21 Mon Sep 17 00:00:00 2001 From: fauxpark Date: Thu, 21 Jan 2021 20:26:23 +1100 Subject: [PATCH 6/8] Docs tweaks --- docs/uart_driver.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/uart_driver.md b/docs/uart_driver.md index 200586d8c49d..14fc939e0798 100644 --- a/docs/uart_driver.md +++ b/docs/uart_driver.md @@ -2,7 +2,7 @@ The UART drivers used in QMK have a set of common functions to allow portability between MCUs. -Currently, this driver does not support enabling hardware flow control (the `RTS` and `CTS` pins) if available, but will do so in future. +Currently, this driver does not support enabling hardware flow control (the `RTS` and `CTS` pins) if available, but may do so in future. ## AVR Configuration @@ -20,7 +20,8 @@ No special setup is required - just connect the `RX` and `TX` pins of your UART You'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. -To enable UART, modify your board's `mcuconf.h` to enable the peripheral you've chosen -- in the case of using USART2, modify `STM32_SERIAL_USE_USART2` to be `TRUE`. +To enable UART, modify your board's `halconf.h` to enable the serial driver - `HAL_USE_SERIAL` should be `TRUE`. +Then, modify your board's `mcuconf.h` to enable the peripheral you've chosen -- in the case of using USART2, modify `STM32_SERIAL_USE_USART2` to be `TRUE`. Configuration-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. From 6a371297d3ca44e5b83175f9915396351d7e1a45 Mon Sep 17 00:00:00 2001 From: fauxpark Date: Thu, 21 Jan 2021 20:43:05 +1100 Subject: [PATCH 7/8] Copy license header to .h, adjust defines for AVR --- drivers/avr/uart.c | 34 ++++++++++++++++------------------ drivers/avr/uart.h | 23 +++++++++++++++++++++++ 2 files changed, 39 insertions(+), 18 deletions(-) diff --git a/drivers/avr/uart.c b/drivers/avr/uart.c index 0b74b203a2da..e866a9e4f884 100644 --- a/drivers/avr/uart.c +++ b/drivers/avr/uart.c @@ -1,5 +1,3 @@ -// TODO: Teensy support(ATMega32u4/AT90USB128) -// Fixed for Arduino Duemilanove ATmega168p by Jun Wako /* UART Example for Teensy USB Development Board * http://www.pjrc.com/teensy/ * Copyright (c) 2009 PJRC.COM, LLC @@ -31,22 +29,7 @@ #include "uart.h" -#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328__) -# define UDRn UDR0 -# define UBRRnL UBRR0L -# define UCSRnA UCSR0A -# define UCSRnB UCSR0B -# define UCSRnC UCSR0C -# define U2Xn U2X0 -# define RXENn RXEN0 -# define TXENn TXEN0 -# define RXCIEn RXCIE0 -# define UCSZn1 UCSZ01 -# define UCSZn0 UCSZ00 -# define UDRIEn UDRIE0 -# define USARTn_UDRE_vect USART_UDRE_vect -# define USARTn_RX_vect USART_RX_vect -#elif defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega32U2__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) +#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__) # define UDRn UDR1 # define UBRRnL UBRR1L # define UCSRnA UCSR1A @@ -76,6 +59,21 @@ # define UDRIEn UDRIE # define USARTn_UDRE_vect USART_UDRE_vect # define USARTn_RX_vect USART_RX_vect +#elif defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__) +# define UDRn UDR0 +# define UBRRnL UBRR0L +# define UCSRnA UCSR0A +# define UCSRnB UCSR0B +# define UCSRnC UCSR0C +# define U2Xn U2X0 +# define RXENn RXEN0 +# define TXENn TXEN0 +# define RXCIEn RXCIE0 +# define UCSZn1 UCSZ01 +# define UCSZn0 UCSZ00 +# define UDRIEn UDRIE0 +# define USARTn_UDRE_vect USART_UDRE_vect +# define USARTn_RX_vect USART_RX_vect #endif // These buffers may be any size from 2 to 256 bytes. diff --git a/drivers/avr/uart.h b/drivers/avr/uart.h index aaae0a125dcc..d5ec563db279 100644 --- a/drivers/avr/uart.h +++ b/drivers/avr/uart.h @@ -1,3 +1,26 @@ +/* UART Example for Teensy USB Development Board + * http://www.pjrc.com/teensy/ + * Copyright (c) 2009 PJRC.COM, LLC + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + #pragma once #include From 3da07b4156f63724327aad92e2abc4fc54c8a5e5 Mon Sep 17 00:00:00 2001 From: fauxpark Date: Sat, 23 Jan 2021 01:39:47 +1100 Subject: [PATCH 8/8] More docs tweaks --- docs/uart_driver.md | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/docs/uart_driver.md b/docs/uart_driver.md index 14fc939e0798..4d1716975f5c 100644 --- a/docs/uart_driver.md +++ b/docs/uart_driver.md @@ -20,22 +20,32 @@ No special setup is required - just connect the `RX` and `TX` pins of your UART You'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. -To enable UART, modify your board's `halconf.h` to enable the serial driver - `HAL_USE_SERIAL` should be `TRUE`. -Then, modify your board's `mcuconf.h` to enable the peripheral you've chosen -- in the case of using USART2, modify `STM32_SERIAL_USE_USART2` to be `TRUE`. +To enable UART, modify your board's `halconf.h` to enable the serial driver: + +```c +#define HAL_USE_SERIAL TRUE +``` + +Then, modify your board's `mcuconf.h` to enable the peripheral you've chosen, for example: + +```c +#undef STM32_SERIAL_USE_USART2 +#define STM32_SERIAL_USE_USART2 TRUE +``` Configuration-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. |`config.h` override |Description |Default Value| |--------------------------|---------------------------------------------------------------|-------------| -|`#define SERIAL_DRIVER` |USART peripheral to use - USART1 => `SD1`, USART2 => `SD2` etc.|`SD1` | -|`#define SD1_TX_PIN` |The pin to use for the TX line |`A9` | -|`#define SD1_TX_PAL_MODE` |The alternate function mode for the TX pin |`7` | -|`#define SD1_RX_PIN` |The pin to use for the RX line |`A10` | -|`#define SD1_RX_PAL_MODE` |The alternate function mode for the RX pin |`7` | -|`#define SD1_CTS_PIN` |The pin to use for the CTS line |`A11` | -|`#define SD1_CTS_PAL_MODE`|The alternate function mode for the CTS pin |`7` | -|`#define SD1_RTS_PIN` |The pin to use for the RTS line |`A12` | -|`#define SD1_RTS_PAL_MODE`|The alternate function mode for the RTS pin |`7` | +|`#define SERIAL_DRIVER` |USART peripheral to use - USART1 -> `SD1`, USART2 -> `SD2` etc.|`SD1` | +|`#define SD1_TX_PIN` |The pin to use for TX |`A9` | +|`#define SD1_TX_PAL_MODE` |The alternate function mode for TX |`7` | +|`#define SD1_RX_PIN` |The pin to use for RX |`A10` | +|`#define SD1_RX_PAL_MODE` |The alternate function mode for RX |`7` | +|`#define SD1_CTS_PIN` |The pin to use for CTS |`A11` | +|`#define SD1_CTS_PAL_MODE`|The alternate function mode for CTS |`7` | +|`#define SD1_RTS_PIN` |The pin to use for RTS |`A12` | +|`#define SD1_RTS_PAL_MODE`|The alternate function mode for RTS |`7` | ## Functions