1 // Fixed for Arduino Duemilanove ATmega168p by Jun Wako
2 /* UART Example for Teensy USB Development Board
3 * http://www.pjrc.com/teensy/
4 * Copyright (c) 2009 PJRC.COM, LLC
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 // Version 1.0: Initial Release
26 // Version 1.1: Add support for Teensy 2.0, minor optimizations
30 #include <avr/interrupt.h>
34 #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__)
37 # define UCSRnA UCSR0A
38 # define UCSRnB UCSR0B
39 # define UCSRnC UCSR0C
43 # define RXCIEn RXCIE0
44 # define UCSZn1 UCSZ01
45 # define UCSZn0 UCSZ00
46 # define UDRIEn UDRIE0
47 # define UDRE_vect USART_UDRE_vect
48 # define RX_vect USART_RX_vect
49 #elif defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega32U2__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__)
52 # define UCSRnA UCSR1A
53 # define UCSRnB UCSR1B
54 # define UCSRnC UCSR1C
58 # define RXCIEn RXCIE1
59 # define UCSZn1 UCSZ11
60 # define UCSZn0 UCSZ10
61 # define UDRIEn UDRIE1
62 # define UDRE_vect USART1_UDRE_vect
63 # define RX_vect USART1_RX_vect
67 // These buffers may be any size from 2 to 256 bytes.
68 #define RX_BUFFER_SIZE 64
69 #define TX_BUFFER_SIZE 256
71 static volatile uint8_t tx_buffer[TX_BUFFER_SIZE];
72 static volatile uint8_t tx_buffer_head;
73 static volatile uint8_t tx_buffer_tail;
74 static volatile uint8_t rx_buffer[RX_BUFFER_SIZE];
75 static volatile uint8_t rx_buffer_head;
76 static volatile uint8_t rx_buffer_tail;
78 // Initialize the UART
79 void uart_init(uint32_t baud)
82 UBRRn = (F_CPU / 4 / baud - 1) / 2;
84 UCSRnB = (1<<RXENn) | (1<<TXENn) | (1<<RXCIEn);
85 UCSRnC = (1<<UCSZn1) | (1<<UCSZn0);
86 tx_buffer_head = tx_buffer_tail = 0;
87 rx_buffer_head = rx_buffer_tail = 0;
92 void uart_putchar(uint8_t c)
96 i = tx_buffer_head + 1;
97 if (i >= TX_BUFFER_SIZE) i = 0;
98 // return immediately to avoid deadlock when interrupt is disabled(called from ISR)
99 if (tx_buffer_tail == i && (SREG & (1<<SREG_I)) == 0) return;
100 while (tx_buffer_tail == i) ; // wait until space in buffer
104 UCSRnB = (1<<RXENn) | (1<<TXENn) | (1<<RXCIEn) | (1<<UDRIEn);
109 uint8_t uart_getchar(void)
113 while (rx_buffer_head == rx_buffer_tail) ; // wait for character
114 i = rx_buffer_tail + 1;
115 if (i >= RX_BUFFER_SIZE) i = 0;
121 // Return the number of bytes waiting in the receive buffer.
122 // Call this before uart_getchar() to check if it will need
123 // to wait for a byte to arrive.
124 uint8_t uart_available(void)
128 head = rx_buffer_head;
129 tail = rx_buffer_tail;
130 if (head >= tail) return head - tail;
131 return RX_BUFFER_SIZE + head - tail;
134 // Transmit Interrupt
139 if (tx_buffer_head == tx_buffer_tail) {
140 // buffer is empty, disable transmit interrupt
141 UCSRnB = (1<<RXENn) | (1<<TXENn) | (1<<RXCIEn);
143 i = tx_buffer_tail + 1;
144 if (i >= TX_BUFFER_SIZE) i = 0;
156 i = rx_buffer_head + 1;
157 if (i >= RX_BUFFER_SIZE) i = 0;
158 if (i != rx_buffer_tail) {