uart.c (5709B)
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 // Version 1.0: Initial Release 25 // Version 1.1: Add support for Teensy 2.0, minor optimizations 26 27 #include <avr/io.h> 28 #include <avr/interrupt.h> 29 30 #include "uart.h" 31 32 #if defined(__AVR_AT90USB162__) || defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega32U2__) || defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) 33 # define UDRn UDR1 34 # define UBRRnL UBRR1L 35 # define UCSRnA UCSR1A 36 # define UCSRnB UCSR1B 37 # define UCSRnC UCSR1C 38 # define U2Xn U2X1 39 # define RXENn RXEN1 40 # define TXENn TXEN1 41 # define RXCIEn RXCIE1 42 # define UCSZn1 UCSZ11 43 # define UCSZn0 UCSZ10 44 # define UDRIEn UDRIE1 45 # define USARTn_UDRE_vect USART1_UDRE_vect 46 # define USARTn_RX_vect USART1_RX_vect 47 #elif defined(__AVR_ATmega32A__) 48 # define UDRn UDR 49 # define UBRRnL UBRRL 50 # define UCSRnA UCSRA 51 # define UCSRnB UCSRB 52 # define UCSRnC UCSRC 53 # define U2Xn U2X 54 # define RXENn RXEN 55 # define TXENn TXEN 56 # define RXCIEn RXCIE 57 # define UCSZn1 UCSZ1 58 # define UCSZn0 UCSZ0 59 # define UDRIEn UDRIE 60 # define USARTn_UDRE_vect USART_UDRE_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 77 #endif 78 79 // These buffers may be any size from 2 to 256 bytes. 80 #define RX_BUFFER_SIZE 64 81 #define TX_BUFFER_SIZE 256 82 83 static volatile uint8_t tx_buffer[TX_BUFFER_SIZE]; 84 static volatile uint8_t tx_buffer_head; 85 static volatile uint8_t tx_buffer_tail; 86 static volatile uint8_t rx_buffer[RX_BUFFER_SIZE]; 87 static volatile uint8_t rx_buffer_head; 88 static volatile uint8_t rx_buffer_tail; 89 90 // Initialize the UART 91 void uart_init(uint32_t baud) { 92 cli(); 93 UBRRnL = (F_CPU / 4 / baud - 1) / 2; 94 UCSRnA = (1 << U2Xn); 95 UCSRnB = (1 << RXENn) | (1 << TXENn) | (1 << RXCIEn); 96 UCSRnC = (1 << UCSZn1) | (1 << UCSZn0); 97 tx_buffer_head = tx_buffer_tail = 0; 98 rx_buffer_head = rx_buffer_tail = 0; 99 sei(); 100 } 101 102 // Transmit a byte 103 void uart_write(uint8_t data) { 104 uint8_t i; 105 106 i = tx_buffer_head + 1; 107 if (i >= TX_BUFFER_SIZE) i = 0; 108 // return immediately to avoid deadlock when interrupt is disabled(called from ISR) 109 if (tx_buffer_tail == i && (SREG & (1 << SREG_I)) == 0) return; 110 while (tx_buffer_tail == i) 111 ; // wait until space in buffer 112 // cli(); 113 tx_buffer[i] = data; 114 tx_buffer_head = i; 115 UCSRnB = (1 << RXENn) | (1 << TXENn) | (1 << RXCIEn) | (1 << UDRIEn); 116 // sei(); 117 } 118 119 // Receive a byte 120 uint8_t uart_read(void) { 121 uint8_t data, i; 122 123 while (rx_buffer_head == rx_buffer_tail) 124 ; // wait for character 125 i = rx_buffer_tail + 1; 126 if (i >= RX_BUFFER_SIZE) i = 0; 127 data = rx_buffer[i]; 128 rx_buffer_tail = i; 129 return data; 130 } 131 132 void uart_transmit(const uint8_t *data, uint16_t length) { 133 for (uint16_t i = 0; i < length; i++) { 134 uart_write(data[i]); 135 } 136 } 137 138 void uart_receive(uint8_t *data, uint16_t length) { 139 for (uint16_t i = 0; i < length; i++) { 140 data[i] = uart_read(); 141 } 142 } 143 144 // Return whether the number of bytes waiting in the receive buffer is nonzero. 145 // Call this before uart_read() to check if it will need 146 // to wait for a byte to arrive. 147 bool uart_available(void) { 148 uint8_t head, tail; 149 150 head = rx_buffer_head; 151 tail = rx_buffer_tail; 152 if (head >= tail) return (head - tail) > 0; 153 return (RX_BUFFER_SIZE + head - tail) > 0; 154 } 155 156 // Transmit Interrupt 157 ISR(USARTn_UDRE_vect) { 158 uint8_t i; 159 160 if (tx_buffer_head == tx_buffer_tail) { 161 // buffer is empty, disable transmit interrupt 162 UCSRnB = (1 << RXENn) | (1 << TXENn) | (1 << RXCIEn); 163 } else { 164 i = tx_buffer_tail + 1; 165 if (i >= TX_BUFFER_SIZE) i = 0; 166 UDRn = tx_buffer[i]; 167 tx_buffer_tail = i; 168 } 169 } 170 171 // Receive Interrupt 172 ISR(USARTn_RX_vect) { 173 uint8_t c, i; 174 175 c = UDRn; 176 i = rx_buffer_head + 1; 177 if (i >= RX_BUFFER_SIZE) i = 0; 178 if (i != rx_buffer_tail) { 179 rx_buffer[i] = c; 180 rx_buffer_head = i; 181 } 182 }