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
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
26 // Version 1.0: Initial Release
27 // Version 1.1: Add support for Teensy 2.0, minor optimizations
31 #include <avr/interrupt.h>
35 // These buffers may be any size from 2 to 256 bytes.
36 #define RX_BUFFER_SIZE 64
37 #define TX_BUFFER_SIZE 40
39 static volatile uint8_t tx_buffer[TX_BUFFER_SIZE];
40 static volatile uint8_t tx_buffer_head;
41 static volatile uint8_t tx_buffer_tail;
42 static volatile uint8_t rx_buffer[RX_BUFFER_SIZE];
43 static volatile uint8_t rx_buffer_head;
44 static volatile uint8_t rx_buffer_tail;
46 // Initialize the UART
47 void uart_init(uint32_t baud)
50 UBRR0 = (F_CPU / 4 / baud - 1) / 2;
52 UCSR0B = (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0);
53 UCSR0C = (1<<UCSZ01) | (1<<UCSZ00);
54 tx_buffer_head = tx_buffer_tail = 0;
55 rx_buffer_head = rx_buffer_tail = 0;
60 void uart_putchar(uint8_t c)
64 i = tx_buffer_head + 1;
65 if (i >= TX_BUFFER_SIZE) i = 0;
66 while (tx_buffer_tail == i) ; // wait until space in buffer
70 UCSR0B = (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0) | (1<<UDRIE0);
75 uint8_t uart_getchar(void)
79 while (rx_buffer_head == rx_buffer_tail) ; // wait for character
80 i = rx_buffer_tail + 1;
81 if (i >= RX_BUFFER_SIZE) i = 0;
87 // Return the number of bytes waiting in the receive buffer.
88 // Call this before uart_getchar() to check if it will need
89 // to wait for a byte to arrive.
90 uint8_t uart_available(void)
94 head = rx_buffer_head;
95 tail = rx_buffer_tail;
96 if (head >= tail) return head - tail;
97 return RX_BUFFER_SIZE + head - tail;
100 // Transmit Interrupt
105 if (tx_buffer_head == tx_buffer_tail) {
106 // buffer is empty, disable transmit interrupt
107 UCSR0B = (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0);
109 i = tx_buffer_tail + 1;
110 if (i >= TX_BUFFER_SIZE) i = 0;
122 i = rx_buffer_head + 1;
123 if (i >= RX_BUFFER_SIZE) i = 0;
124 if (i != rx_buffer_tail) {