sanguino/cores/arduino/HardwareSerial.cpp

Thu, 07 Jul 2016 12:23:34 +0200

author
mbayer
date
Thu, 07 Jul 2016 12:23:34 +0200
changeset 2
b373b0288715
permissions
-rw-r--r--

added missing sanguino files

2
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1 /*
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
2 HardwareSerial.cpp - Hardware serial library for Wiring
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
3 Copyright (c) 2006 Nicholas Zambetti. All right reserved.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
4
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
5 This library is free software; you can redistribute it and/or
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
6 modify it under the terms of the GNU Lesser General Public
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
7 License as published by the Free Software Foundation; either
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
8 version 2.1 of the License, or (at your option) any later version.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
9
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
10 This library is distributed in the hope that it will be useful,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
13 Lesser General Public License for more details.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
14
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
15 You should have received a copy of the GNU Lesser General Public
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
16 License along with this library; if not, write to the Free Software
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
18
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
19 Modified 23 November 2006 by David A. Mellis
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
20 Modified 28 September 2010 by Mark Sproul
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
21 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
22
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
23 #include <stdlib.h>
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
24 #include <stdio.h>
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
25 #include <string.h>
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
26 #include <inttypes.h>
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
27 #include "Arduino.h"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
28 #include "wiring_private.h"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
29
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
30 // this next line disables the entire HardwareSerial.cpp,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
31 // this is so I can support Attiny series and any other chip without a uart
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
32 #if defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
33
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
34 #include "HardwareSerial.h"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
35
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
36 // Define constants and variables for buffering incoming serial data. We're
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
37 // using a ring buffer (I think), in which head is the index of the location
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
38 // to which to write the next incoming character and tail is the index of the
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
39 // location from which to read.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
40 #if (RAMEND < 1000)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
41 #define SERIAL_BUFFER_SIZE 16
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
42 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
43 #define SERIAL_BUFFER_SIZE 64
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
44 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
45
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
46 struct ring_buffer
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
47 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
48 unsigned char buffer[SERIAL_BUFFER_SIZE];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
49 volatile unsigned int head;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
50 volatile unsigned int tail;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
51 };
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
52
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
53 #if defined(USBCON)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
54 ring_buffer rx_buffer = { { 0 }, 0, 0};
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
55 ring_buffer tx_buffer = { { 0 }, 0, 0};
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
56 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
57 #if defined(UBRRH) || defined(UBRR0H)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
58 ring_buffer rx_buffer = { { 0 }, 0, 0 };
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
59 ring_buffer tx_buffer = { { 0 }, 0, 0 };
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
60 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
61 #if defined(UBRR1H)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
62 ring_buffer rx_buffer1 = { { 0 }, 0, 0 };
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
63 ring_buffer tx_buffer1 = { { 0 }, 0, 0 };
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
64 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
65 #if defined(UBRR2H)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
66 ring_buffer rx_buffer2 = { { 0 }, 0, 0 };
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
67 ring_buffer tx_buffer2 = { { 0 }, 0, 0 };
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
68 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
69 #if defined(UBRR3H)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
70 ring_buffer rx_buffer3 = { { 0 }, 0, 0 };
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
71 ring_buffer tx_buffer3 = { { 0 }, 0, 0 };
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
72 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
73
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
74 inline void store_char(unsigned char c, ring_buffer *buffer)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
75 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
76 int i = (unsigned int)(buffer->head + 1) % SERIAL_BUFFER_SIZE;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
77
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
78 // if we should be storing the received character into the location
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
79 // just before the tail (meaning that the head would advance to the
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
80 // current location of the tail), we're about to overflow the buffer
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
81 // and so we don't write the character or advance the head.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
82 if (i != buffer->tail) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
83 buffer->buffer[buffer->head] = c;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
84 buffer->head = i;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
85 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
86 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
87
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
88 #if !defined(USART0_RX_vect) && defined(USART1_RX_vect)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
89 // do nothing - on the 32u4 the first USART is USART1
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
90 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
91 #if !defined(USART_RX_vect) && !defined(SIG_USART0_RECV) && \
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
92 !defined(SIG_UART0_RECV) && !defined(USART0_RX_vect) && \
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
93 !defined(SIG_UART_RECV)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
94 #error "Don't know what the Data Received vector is called for the first UART"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
95 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
96 void serialEvent() __attribute__((weak));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
97 void serialEvent() {}
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
98 #define serialEvent_implemented
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
99 #if defined(USART_RX_vect)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
100 SIGNAL(USART_RX_vect)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
101 #elif defined(SIG_USART0_RECV)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
102 SIGNAL(SIG_USART0_RECV)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
103 #elif defined(SIG_UART0_RECV)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
104 SIGNAL(SIG_UART0_RECV)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
105 #elif defined(USART0_RX_vect)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
106 SIGNAL(USART0_RX_vect)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
107 #elif defined(SIG_UART_RECV)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
108 SIGNAL(SIG_UART_RECV)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
109 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
110 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
111 #if defined(UDR0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
112 unsigned char c = UDR0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
113 #elif defined(UDR)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
114 unsigned char c = UDR;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
115 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
116 #error UDR not defined
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
117 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
118 store_char(c, &rx_buffer);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
119 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
120 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
121 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
122
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
123 #if defined(USART1_RX_vect)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
124 void serialEvent1() __attribute__((weak));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
125 void serialEvent1() {}
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
126 #define serialEvent1_implemented
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
127 SIGNAL(USART1_RX_vect)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
128 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
129 unsigned char c = UDR1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
130 store_char(c, &rx_buffer1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
131 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
132 #elif defined(SIG_USART1_RECV)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
133 #error SIG_USART1_RECV
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
134 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
135
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
136 #if defined(USART2_RX_vect) && defined(UDR2)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
137 void serialEvent2() __attribute__((weak));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
138 void serialEvent2() {}
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
139 #define serialEvent2_implemented
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
140 SIGNAL(USART2_RX_vect)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
141 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
142 unsigned char c = UDR2;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
143 store_char(c, &rx_buffer2);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
144 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
145 #elif defined(SIG_USART2_RECV)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
146 #error SIG_USART2_RECV
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
147 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
148
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
149 #if defined(USART3_RX_vect) && defined(UDR3)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
150 void serialEvent3() __attribute__((weak));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
151 void serialEvent3() {}
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
152 #define serialEvent3_implemented
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
153 SIGNAL(USART3_RX_vect)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
154 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
155 unsigned char c = UDR3;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
156 store_char(c, &rx_buffer3);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
157 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
158 #elif defined(SIG_USART3_RECV)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
159 #error SIG_USART3_RECV
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
160 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
161
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
162 void serialEventRun(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
163 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
164 #ifdef serialEvent_implemented
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
165 if (Serial.available()) serialEvent();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
166 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
167 #ifdef serialEvent1_implemented
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
168 if (Serial1.available()) serialEvent1();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
169 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
170 #ifdef serialEvent2_implemented
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
171 if (Serial2.available()) serialEvent2();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
172 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
173 #ifdef serialEvent3_implemented
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
174 if (Serial3.available()) serialEvent3();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
175 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
176 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
177
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
178
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
179 #if !defined(USART0_UDRE_vect) && defined(USART1_UDRE_vect)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
180 // do nothing - on the 32u4 the first USART is USART1
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
181 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
182 #if !defined(UART0_UDRE_vect) && !defined(UART_UDRE_vect) && !defined(USART0_UDRE_vect) && !defined(USART_UDRE_vect)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
183 #error "Don't know what the Data Register Empty vector is called for the first UART"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
184 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
185 #if defined(UART0_UDRE_vect)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
186 ISR(UART0_UDRE_vect)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
187 #elif defined(UART_UDRE_vect)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
188 ISR(UART_UDRE_vect)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
189 #elif defined(USART0_UDRE_vect)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
190 ISR(USART0_UDRE_vect)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
191 #elif defined(USART_UDRE_vect)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
192 ISR(USART_UDRE_vect)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
193 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
194 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
195 if (tx_buffer.head == tx_buffer.tail) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
196 // Buffer empty, so disable interrupts
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
197 #if defined(UCSR0B)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
198 cbi(UCSR0B, UDRIE0);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
199 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
200 cbi(UCSRB, UDRIE);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
201 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
202 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
203 else {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
204 // There is more data in the output buffer. Send the next byte
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
205 unsigned char c = tx_buffer.buffer[tx_buffer.tail];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
206 tx_buffer.tail = (tx_buffer.tail + 1) % SERIAL_BUFFER_SIZE;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
207
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
208 #if defined(UDR0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
209 UDR0 = c;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
210 #elif defined(UDR)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
211 UDR = c;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
212 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
213 #error UDR not defined
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
214 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
215 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
216 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
217 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
218 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
219
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
220 #ifdef USART1_UDRE_vect
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
221 ISR(USART1_UDRE_vect)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
222 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
223 if (tx_buffer1.head == tx_buffer1.tail) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
224 // Buffer empty, so disable interrupts
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
225 cbi(UCSR1B, UDRIE1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
226 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
227 else {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
228 // There is more data in the output buffer. Send the next byte
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
229 unsigned char c = tx_buffer1.buffer[tx_buffer1.tail];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
230 tx_buffer1.tail = (tx_buffer1.tail + 1) % SERIAL_BUFFER_SIZE;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
231
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
232 UDR1 = c;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
233 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
234 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
235 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
236
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
237 #ifdef USART2_UDRE_vect
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
238 ISR(USART2_UDRE_vect)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
239 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
240 if (tx_buffer2.head == tx_buffer2.tail) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
241 // Buffer empty, so disable interrupts
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
242 cbi(UCSR2B, UDRIE2);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
243 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
244 else {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
245 // There is more data in the output buffer. Send the next byte
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
246 unsigned char c = tx_buffer2.buffer[tx_buffer2.tail];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
247 tx_buffer2.tail = (tx_buffer2.tail + 1) % SERIAL_BUFFER_SIZE;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
248
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
249 UDR2 = c;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
250 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
251 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
252 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
253
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
254 #ifdef USART3_UDRE_vect
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
255 ISR(USART3_UDRE_vect)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
256 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
257 if (tx_buffer3.head == tx_buffer3.tail) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
258 // Buffer empty, so disable interrupts
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
259 cbi(UCSR3B, UDRIE3);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
260 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
261 else {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
262 // There is more data in the output buffer. Send the next byte
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
263 unsigned char c = tx_buffer3.buffer[tx_buffer3.tail];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
264 tx_buffer3.tail = (tx_buffer3.tail + 1) % SERIAL_BUFFER_SIZE;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
265
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
266 UDR3 = c;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
267 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
268 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
269 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
270
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
271
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
272 // Constructors ////////////////////////////////////////////////////////////////
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
273
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
274 HardwareSerial::HardwareSerial(ring_buffer *rx_buffer, ring_buffer *tx_buffer,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
275 volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
276 volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
277 volatile uint8_t *udr,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
278 uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udrie, uint8_t u2x)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
279 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
280 _rx_buffer = rx_buffer;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
281 _tx_buffer = tx_buffer;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
282 _ubrrh = ubrrh;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
283 _ubrrl = ubrrl;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
284 _ucsra = ucsra;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
285 _ucsrb = ucsrb;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
286 _udr = udr;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
287 _rxen = rxen;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
288 _txen = txen;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
289 _rxcie = rxcie;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
290 _udrie = udrie;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
291 _u2x = u2x;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
292 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
293
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
294 // Public Methods //////////////////////////////////////////////////////////////
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
295
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
296 void HardwareSerial::begin(unsigned long baud)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
297 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
298 uint16_t baud_setting;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
299 bool use_u2x = true;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
300
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
301 #if F_CPU == 16000000UL
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
302 // hardcoded exception for compatibility with the bootloader shipped
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
303 // with the Duemilanove and previous boards and the firmware on the 8U2
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
304 // on the Uno and Mega 2560.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
305 if (baud == 57600) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
306 use_u2x = false;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
307 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
308 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
309
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
310 try_again:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
311
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
312 if (use_u2x) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
313 *_ucsra = 1 << _u2x;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
314 baud_setting = (F_CPU / 4 / baud - 1) / 2;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
315 } else {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
316 *_ucsra = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
317 baud_setting = (F_CPU / 8 / baud - 1) / 2;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
318 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
319
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
320 if ((baud_setting > 4095) && use_u2x)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
321 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
322 use_u2x = false;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
323 goto try_again;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
324 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
325
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
326 // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
327 *_ubrrh = baud_setting >> 8;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
328 *_ubrrl = baud_setting;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
329
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
330 sbi(*_ucsrb, _rxen);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
331 sbi(*_ucsrb, _txen);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
332 sbi(*_ucsrb, _rxcie);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
333 cbi(*_ucsrb, _udrie);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
334 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
335
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
336 void HardwareSerial::end()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
337 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
338 // wait for transmission of outgoing data
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
339 while (_tx_buffer->head != _tx_buffer->tail)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
340 ;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
341
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
342 cbi(*_ucsrb, _rxen);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
343 cbi(*_ucsrb, _txen);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
344 cbi(*_ucsrb, _rxcie);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
345 cbi(*_ucsrb, _udrie);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
346
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
347 // clear any received data
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
348 _rx_buffer->head = _rx_buffer->tail;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
349 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
350
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
351 int HardwareSerial::available(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
352 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
353 return (unsigned int)(SERIAL_BUFFER_SIZE + _rx_buffer->head - _rx_buffer->tail) % SERIAL_BUFFER_SIZE;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
354 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
355
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
356 int HardwareSerial::peek(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
357 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
358 if (_rx_buffer->head == _rx_buffer->tail) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
359 return -1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
360 } else {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
361 return _rx_buffer->buffer[_rx_buffer->tail];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
362 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
363 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
364
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
365 int HardwareSerial::read(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
366 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
367 // if the head isn't ahead of the tail, we don't have any characters
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
368 if (_rx_buffer->head == _rx_buffer->tail) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
369 return -1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
370 } else {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
371 unsigned char c = _rx_buffer->buffer[_rx_buffer->tail];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
372 _rx_buffer->tail = (unsigned int)(_rx_buffer->tail + 1) % SERIAL_BUFFER_SIZE;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
373 return c;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
374 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
375 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
376
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
377 void HardwareSerial::flush()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
378 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
379 while (_tx_buffer->head != _tx_buffer->tail)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
380 ;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
381 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
382
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
383 size_t HardwareSerial::write(uint8_t c)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
384 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
385 int i = (_tx_buffer->head + 1) % SERIAL_BUFFER_SIZE;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
386
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
387 // If the output buffer is full, there's nothing for it other than to
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
388 // wait for the interrupt handler to empty it a bit
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
389 // ???: return 0 here instead?
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
390 while (i == _tx_buffer->tail)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
391 ;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
392
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
393 _tx_buffer->buffer[_tx_buffer->head] = c;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
394 _tx_buffer->head = i;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
395
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
396 sbi(*_ucsrb, _udrie);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
397
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
398 return 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
399 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
400
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
401 HardwareSerial::operator bool() {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
402 return true;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
403 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
404
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
405 // Preinstantiate Objects //////////////////////////////////////////////////////
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
406
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
407 #if defined(UBRRH) && defined(UBRRL)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
408 HardwareSerial Serial(&rx_buffer, &tx_buffer, &UBRRH, &UBRRL, &UCSRA, &UCSRB, &UDR, RXEN, TXEN, RXCIE, UDRIE, U2X);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
409 #elif defined(UBRR0H) && defined(UBRR0L)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
410 HardwareSerial Serial(&rx_buffer, &tx_buffer, &UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UDR0, RXEN0, TXEN0, RXCIE0, UDRIE0, U2X0);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
411 #elif defined(USBCON)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
412 // do nothing - Serial object and buffers are initialized in CDC code
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
413 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
414 #error no serial port defined (port 0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
415 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
416
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
417 #if defined(UBRR1H)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
418 HardwareSerial Serial1(&rx_buffer1, &tx_buffer1, &UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UDR1, RXEN1, TXEN1, RXCIE1, UDRIE1, U2X1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
419 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
420 #if defined(UBRR2H)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
421 HardwareSerial Serial2(&rx_buffer2, &tx_buffer2, &UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UDR2, RXEN2, TXEN2, RXCIE2, UDRIE2, U2X2);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
422 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
423 #if defined(UBRR3H)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
424 HardwareSerial Serial3(&rx_buffer3, &tx_buffer3, &UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UDR3, RXEN3, TXEN3, RXCIE3, UDRIE3, U2X3);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
425 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
426
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
427 #endif // whole file
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
428

mercurial