avrSerial  1.0
Multi-UART Library for many AVR devices
 All Files Functions Groups
serial_device.h
Go to the documentation of this file.
1 /*
2  * serial_device.h
3  *
4  * Copyright (c) 2012, Thomas Buck <xythobuz@me.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * - Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  *
14  * - Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 #ifndef _serial_device_h
31 #define _serial_device_h
32 
45 #if defined(__AVR_ATmega8__) || defined(__AVR_ATmega16__) || defined(__AVR_ATmega32__) \
46  || defined(__AVR_ATmega8515__) || defined(__AVR_ATmega8535__) \
47 || defined(__AVR_ATmega323__)
48 
49 #define UART_COUNT 1
50 #define UART_REGISTERS 6
51 #define UART_BITS 7
52 volatile uint8_t *serialRegisters[UART_COUNT][UART_REGISTERS] = {{
53  &UDR,
54  &UCSRB,
55  &UCSRC,
56  &UCSRA,
57  &UBRRH,
58  &UBRRL
59 }};
60 #define SERIALBAUDBIT 8
61 uint8_t serialBits[UART_COUNT][UART_BITS] = {{
62  UCSZ0,
63  UCSZ1,
64  RXCIE,
65  RXEN,
66  TXEN,
67  UDRIE,
68  UDRE
69 }};
70 #define SERIALRECIEVEINTERRUPT USART_RXC_vect
71 #define SERIALTRANSMITINTERRUPT USART_UDRE_vect
72 
73 #elif defined(__AVR_ATmega168__)
74 
75 #define UART_COUNT 1
76 #define UART_REGISTERS 5
77 #define UART_BITS 7
78 volatile uint8_t *serialRegisters[UART_COUNT][UART_REGISTERS] = {{
79  &UDR0,
80  &UCSR0B,
81  &UCSR0C,
82  &UCSR0A
83 }};
84 #define SERIALBAUDBIT 16
85 volatile uint16_t *serialBaudRegisters[UART_COUNT] = {
86  &UBRR0
87 };
88 uint8_t serialBits[UART_COUNT][UART_BITS] = {{
89  UCSZ00,
90  UCSZ01,
91  RXCIE0,
92  RXEN0,
93  TXEN0,
94  UDRIE0,
95  UDRE0
96 }};
97 #define SERIALRECIEVEINTERRUPT USART_RX_vect
98 #define SERIALTRANSMITINTERRUPT USART_UDRE_vect
99 
100 #elif defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) || defined(__AVR_ATmega1280__) \
101  || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega640__)
102 
103 #define UART_COUNT 4
104 #define UART_REGISTERS 4
105 #define UART_BITS 7
106 volatile uint8_t *serialRegisters[UART_COUNT][UART_REGISTERS] = {
107  {
108  &UDR0,
109  &UCSR0B,
110  &UCSR0C,
111  &UCSR0A
112  },
113  {
114  &UDR1,
115  &UCSR1B,
116  &UCSR1C,
117  &UCSR1A
118  },
119  {
120  &UDR2,
121  &UCSR2B,
122  &UCSR2C,
123  &UCSR2A
124  },
125  {
126  &UDR3,
127  &UCSR3B,
128  &UCSR3C,
129  &UCSR3A
130  }
131 };
132 #define SERIALBAUDBIT 16
133 volatile uint16_t *serialBaudRegisters[UART_COUNT] = {
134  &UBRR0, &UBRR1, &UBRR2, &UBRR3
135 };
136 uint8_t serialBits[UART_COUNT][UART_BITS] = {
137  {
138  UCSZ00,
139  UCSZ01,
140  RXCIE0,
141  RXEN0,
142  TXEN0,
143  UDRIE0,
144  UDRE0
145  },
146  {
147  UCSZ10,
148  UCSZ11,
149  RXCIE1,
150  RXEN1,
151  TXEN1,
152  UDRIE1,
153  UDRE1
154  },
155  {
156  UCSZ20,
157  UCSZ21,
158  RXCIE2,
159  RXEN2,
160  TXEN2,
161  UDRIE2,
162  UDRE2
163  },
164  {
165  UCSZ30,
166  UCSZ31,
167  RXCIE3,
168  RXEN3,
169  TXEN3,
170  UDRIE3,
171  UDRE3
172  }
173 };
174 #define SERIALRECIEVEINTERRUPT USART0_RX_vect
175 #define SERIALTRANSMITINTERRUPT USART0_UDRE_vect
176 #define SERIALRECIEVEINTERRUPT1 USART1_RX_vect
177 #define SERIALTRANSMITINTERRUPT1 USART1_UDRE_vect
178 #define SERIALRECIEVEINTERRUPT2 USART2_RX_vect
179 #define SERIALTRANSMITINTERRUPT2 USART2_UDRE_vect
180 #define SERIALRECIEVEINTERRUPT3 USART3_RX_vect
181 #define SERIALTRANSMITINTERRUPT3 USART3_UDRE_vect
182 
183 #else
184 #error "AvrSerialLibrary not compatible with your MCU!"
185 #endif
186 
187 #endif // _serial_device_h
188