]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/common/avr/uart.c
Correct the comments of matrix ROW and COL. (#604)
[max/tmk_keyboard.git] / tmk_core / common / avr / uart.c
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
5  * 
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:
12  * 
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  * 
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
22  * THE SOFTWARE.
23  */
24
25 // Version 1.0: Initial Release
26 // Version 1.1: Add support for Teensy 2.0, minor optimizations
27
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__)
35 #   define UDRn         UDR0
36 #   define UBRRn        UBRR0
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 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__)
50 #   define UDRn         UDR1
51 #   define UBRRn        UBRR1
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 UDRE_vect    USART1_UDRE_vect
63 #   define RX_vect      USART1_RX_vect
64 #endif
65
66
67 // These buffers may be any size from 2 to 256 bytes.
68 #define RX_BUFFER_SIZE 64
69 #define TX_BUFFER_SIZE 40
70
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;
77
78 // Initialize the UART
79 void uart_init(uint32_t baud)
80 {
81         cli();
82         UBRRn = (F_CPU / 4 / baud - 1) / 2;
83         UCSRnA = (1<<U2Xn);
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;
88         sei();
89 }
90
91 // Transmit a byte
92 void uart_putchar(uint8_t c)
93 {
94         uint8_t i;
95
96         i = tx_buffer_head + 1;
97         if (i >= TX_BUFFER_SIZE) i = 0;
98         while (tx_buffer_tail == i) ; // wait until space in buffer
99         //cli();
100         tx_buffer[i] = c;
101         tx_buffer_head = i;
102         UCSRnB = (1<<RXENn) | (1<<TXENn) | (1<<RXCIEn) | (1<<UDRIEn);
103         //sei();
104 }
105
106 // Receive a byte
107 uint8_t uart_getchar(void)
108 {
109         uint8_t c, i;
110
111         while (rx_buffer_head == rx_buffer_tail) ; // wait for character
112         i = rx_buffer_tail + 1;
113         if (i >= RX_BUFFER_SIZE) i = 0;
114         c = rx_buffer[i];
115         rx_buffer_tail = i;
116         return c;
117 }
118
119 // Return the number of bytes waiting in the receive buffer.
120 // Call this before uart_getchar() to check if it will need
121 // to wait for a byte to arrive.
122 uint8_t uart_available(void)
123 {
124         uint8_t head, tail;
125
126         head = rx_buffer_head;
127         tail = rx_buffer_tail;
128         if (head >= tail) return head - tail;
129         return RX_BUFFER_SIZE + head - tail;
130 }
131
132 // Transmit Interrupt
133 ISR(UDRE_vect)
134 {
135         uint8_t i;
136
137         if (tx_buffer_head == tx_buffer_tail) {
138                 // buffer is empty, disable transmit interrupt
139                 UCSRnB = (1<<RXENn) | (1<<TXENn) | (1<<RXCIEn);
140         } else {
141                 i = tx_buffer_tail + 1;
142                 if (i >= TX_BUFFER_SIZE) i = 0;
143                 UDRn = tx_buffer[i];
144                 tx_buffer_tail = i;
145         }
146 }
147
148 // Receive Interrupt
149 ISR(RX_vect)
150 {
151         uint8_t c, i;
152
153         c = UDRn;
154         i = rx_buffer_head + 1;
155         if (i >= RX_BUFFER_SIZE) i = 0;
156         if (i != rx_buffer_tail) {
157                 rx_buffer[i] = c;
158                 rx_buffer_head = i;
159         }
160 }
161