diff options
| author | Ryan <fauxpark@gmail.com> | 2021-01-27 17:42:49 +1100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-01-27 17:42:49 +1100 |
| commit | 30b46fad5764b54ab4d47e9c4024f8030e1bf1a7 (patch) | |
| tree | 9a9aa60158edf5adea802e33ee2db2ba4164145a /tmk_core | |
| parent | be897cbc2f9d542dfcc98c8e06aeb04179cc484c (diff) | |
| download | qmk_firmware-30b46fad5764b54ab4d47e9c4024f8030e1bf1a7.tar.gz qmk_firmware-30b46fad5764b54ab4d47e9c4024f8030e1bf1a7.zip | |
UART driver refactor (#11637)
Diffstat (limited to 'tmk_core')
| -rw-r--r-- | tmk_core/common/uart.c | 172 | ||||
| -rw-r--r-- | tmk_core/common/uart.h | 8 |
2 files changed, 0 insertions, 180 deletions
diff --git a/tmk_core/common/uart.c b/tmk_core/common/uart.c deleted file mode 100644 index 150e256c8..000000000 --- a/tmk_core/common/uart.c +++ /dev/null | |||
| @@ -1,172 +0,0 @@ | |||
| 1 | // TODO: Teensy support(ATMega32u4/AT90USB128) | ||
| 2 | // Fixed for Arduino Duemilanove ATmega168p by Jun Wako | ||
| 3 | /* UART Example for Teensy USB Development Board | ||
| 4 | * http://www.pjrc.com/teensy/ | ||
| 5 | * Copyright (c) 2009 PJRC.COM, LLC | ||
| 6 | * | ||
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 8 | * of this software and associated documentation files (the "Software"), to deal | ||
| 9 | * in the Software without restriction, including without limitation the rights | ||
| 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 11 | * copies of the Software, and to permit persons to whom the Software is | ||
| 12 | * furnished to do so, subject to the following conditions: | ||
| 13 | * | ||
| 14 | * The above copyright notice and this permission notice shall be included in | ||
| 15 | * all copies or substantial portions of the Software. | ||
| 16 | * | ||
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| 23 | * THE SOFTWARE. | ||
| 24 | */ | ||
| 25 | |||
| 26 | // Version 1.0: Initial Release | ||
| 27 | // Version 1.1: Add support for Teensy 2.0, minor optimizations | ||
| 28 | |||
| 29 | #include <avr/io.h> | ||
| 30 | #include <avr/interrupt.h> | ||
| 31 | |||
| 32 | #include "uart.h" | ||
| 33 | |||
| 34 | #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328__) | ||
| 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 | ||
| 51 | # define UBRRnL UBRR1L | ||
| 52 | # define UCSRnA UCSR1A | ||
| 53 | # define UCSRnB UCSR1B | ||
| 54 | # define UCSRnC UCSR1C | ||
| 55 | # define U2Xn U2X1 | ||
| 56 | # define RXENn RXEN1 | ||
| 57 | # define TXENn TXEN1 | ||
| 58 | # define RXCIEn RXCIE1 | ||
| 59 | # define UCSZn1 UCSZ11 | ||
| 60 | # define UCSZn0 UCSZ10 | ||
| 61 | # define UDRIEn UDRIE1 | ||
| 62 | # define USARTn_UDRE_vect USART1_UDRE_vect | ||
| 63 | # define USARTn_RX_vect USART1_RX_vect | ||
| 64 | #elif defined(__AVR_ATmega32A__) | ||
| 65 | # define UDRn UDR | ||
| 66 | # define UBRRnL UBRRL | ||
| 67 | # define UCSRnA UCSRA | ||
| 68 | # define UCSRnB UCSRB | ||
| 69 | # define UCSRnC UCSRC | ||
| 70 | # define U2Xn U2X | ||
| 71 | # define RXENn RXEN | ||
| 72 | # define TXENn TXEN | ||
| 73 | # define RXCIEn RXCIE | ||
| 74 | # define UCSZn1 UCSZ1 | ||
| 75 | # define UCSZn0 UCSZ0 | ||
| 76 | # define UDRIEn UDRIE | ||
| 77 | # define USARTn_UDRE_vect USART_UDRE_vect | ||
| 78 | # define USARTn_RX_vect USART_RX_vect | ||
| 79 | #endif | ||
| 80 | |||
| 81 | // These buffers may be any size from 2 to 256 bytes. | ||
| 82 | #define RX_BUFFER_SIZE 64 | ||
| 83 | #define TX_BUFFER_SIZE 256 | ||
| 84 | |||
| 85 | static volatile uint8_t tx_buffer[TX_BUFFER_SIZE]; | ||
| 86 | static volatile uint8_t tx_buffer_head; | ||
| 87 | static volatile uint8_t tx_buffer_tail; | ||
| 88 | static volatile uint8_t rx_buffer[RX_BUFFER_SIZE]; | ||
| 89 | static volatile uint8_t rx_buffer_head; | ||
| 90 | static volatile uint8_t rx_buffer_tail; | ||
| 91 | |||
| 92 | // Initialize the UART | ||
| 93 | void uart_init(uint32_t baud) { | ||
| 94 | cli(); | ||
| 95 | UBRRnL = (F_CPU / 4 / baud - 1) / 2; | ||
| 96 | UCSRnA = (1 << U2Xn); | ||
| 97 | UCSRnB = (1 << RXENn) | (1 << TXENn) | (1 << RXCIEn); | ||
| 98 | UCSRnC = (1 << UCSZn1) | (1 << UCSZn0); | ||
| 99 | tx_buffer_head = tx_buffer_tail = 0; | ||
| 100 | rx_buffer_head = rx_buffer_tail = 0; | ||
| 101 | sei(); | ||
| 102 | } | ||
| 103 | |||
| 104 | // Transmit a byte | ||
| 105 | void uart_putchar(uint8_t c) { | ||
| 106 | uint8_t i; | ||
| 107 | |||
| 108 | i = tx_buffer_head + 1; | ||
| 109 | if (i >= TX_BUFFER_SIZE) i = 0; | ||
| 110 | // return immediately to avoid deadlock when interrupt is disabled(called from ISR) | ||
| 111 | if (tx_buffer_tail == i && (SREG & (1 << SREG_I)) == 0) return; | ||
| 112 | while (tx_buffer_tail == i) | ||
| 113 | ; // wait until space in buffer | ||
| 114 | // cli(); | ||
| 115 | tx_buffer[i] = c; | ||
| 116 | tx_buffer_head = i; | ||
| 117 | UCSRnB = (1 << RXENn) | (1 << TXENn) | (1 << RXCIEn) | (1 << UDRIEn); | ||
| 118 | // sei(); | ||
| 119 | } | ||
| 120 | |||
| 121 | // Receive a byte | ||
| 122 | uint8_t uart_getchar(void) { | ||
| 123 | uint8_t c, i; | ||
| 124 | |||
| 125 | while (rx_buffer_head == rx_buffer_tail) | ||
| 126 | ; // wait for character | ||
| 127 | i = rx_buffer_tail + 1; | ||
| 128 | if (i >= RX_BUFFER_SIZE) i = 0; | ||
| 129 | c = rx_buffer[i]; | ||
| 130 | rx_buffer_tail = i; | ||
| 131 | return c; | ||
| 132 | } | ||
| 133 | |||
| 134 | // Return the number of bytes waiting in the receive buffer. | ||
| 135 | // Call this before uart_getchar() to check if it will need | ||
| 136 | // to wait for a byte to arrive. | ||
| 137 | uint8_t uart_available(void) { | ||
| 138 | uint8_t head, tail; | ||
| 139 | |||
| 140 | head = rx_buffer_head; | ||
| 141 | tail = rx_buffer_tail; | ||
| 142 | if (head >= tail) return head - tail; | ||
| 143 | return RX_BUFFER_SIZE + head - tail; | ||
| 144 | } | ||
| 145 | |||
| 146 | // Transmit Interrupt | ||
| 147 | ISR(USARTn_UDRE_vect) { | ||
| 148 | uint8_t i; | ||
| 149 | |||
| 150 | if (tx_buffer_head == tx_buffer_tail) { | ||
| 151 | // buffer is empty, disable transmit interrupt | ||
| 152 | UCSRnB = (1 << RXENn) | (1 << TXENn) | (1 << RXCIEn); | ||
| 153 | } else { | ||
| 154 | i = tx_buffer_tail + 1; | ||
| 155 | if (i >= TX_BUFFER_SIZE) i = 0; | ||
| 156 | UDRn = tx_buffer[i]; | ||
| 157 | tx_buffer_tail = i; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | // Receive Interrupt | ||
| 162 | ISR(USARTn_RX_vect) { | ||
| 163 | uint8_t c, i; | ||
| 164 | |||
| 165 | c = UDRn; | ||
| 166 | i = rx_buffer_head + 1; | ||
| 167 | if (i >= RX_BUFFER_SIZE) i = 0; | ||
| 168 | if (i != rx_buffer_tail) { | ||
| 169 | rx_buffer[i] = c; | ||
| 170 | rx_buffer_head = i; | ||
| 171 | } | ||
| 172 | } | ||
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 | |||
| 5 | void uart_init(uint32_t baud); | ||
| 6 | void uart_putchar(uint8_t c); | ||
| 7 | uint8_t uart_getchar(void); | ||
| 8 | uint8_t uart_available(void); | ||
