18
18
19
19
Modified 23 November 2006 by David A. Mellis
20
20
*/
21
+ // *******************************************************************************************
22
+ // * Mar 14, 2010 <MLS> Mark Sproul fixed bug in __AVR_ATmega644__ interrupt vector
23
+ // * Mar 25, 2010 <MLS> Fixed bug in __AVR_ATmega644P__ interrupt vector
24
+ // * Apr 2, 2010 <MLS> Changed RX_BUFFER_SIZE to 32 for smaller ram processors
25
+ // * Jul 30, 2010 <MLS> Chainging #ifdefs to register and signals instead of CPU type
26
+ // * Aug 3, 2010 <MLS> Tested on 644P 645 1280, 2560 328 and others
27
+ // * Aug 16, 2010 <MLS> Added support for Atmega32
28
+ // * Aug 31, 2010 <MLS> The ATmega32U4 has uart1 but NOT uart0
29
+ // * Sep 5, 2010 <MLS> V0019 was released, migrated changes into 0019
30
+ // * Sep 28, 2010 <MLS> V0020 was released, migrated changes into 0020
31
+ // *******************************************************************************************
21
32
22
33
#include < stdlib.h>
23
34
#include < stdio.h>
26
37
#include " wiring.h"
27
38
#include " wiring_private.h"
28
39
40
+ // *******************************************************************************************
41
+ // * this next line disables the entire HardwareSerial.cpp,
42
+ // * this is so I can support Attiny series and any other chip without a uart
43
+ #if defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H)
44
+
29
45
#include " HardwareSerial.h"
30
46
31
47
// Define constants and variables for buffering incoming serial data. We're
32
48
// using a ring buffer (I think), in which rx_buffer_head is the index of the
33
49
// location to which to write the next incoming character and rx_buffer_tail
34
50
// is the index of the location from which to read.
35
- #define RX_BUFFER_SIZE 128
51
+ #if (RAMEND < 1500)
52
+ #define RX_BUFFER_SIZE 32
53
+ #else
54
+ #define RX_BUFFER_SIZE 128
55
+ #endif
36
56
37
- struct ring_buffer {
38
- unsigned char buffer[RX_BUFFER_SIZE];
39
- int head;
40
- int tail;
57
+ struct ring_buffer
58
+ {
59
+ unsigned char buffer[RX_BUFFER_SIZE];
60
+ int head;
61
+ int tail;
41
62
};
42
63
43
- ring_buffer rx_buffer = { { 0 }, 0 , 0 };
64
+ #if defined(UBRRH) || defined(UBRR0H)
65
+ ring_buffer rx_buffer = { { 0 }, 0 , 0 };
66
+ #endif
44
67
45
- #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
46
- ring_buffer rx_buffer1 = { { 0 }, 0 , 0 };
47
- ring_buffer rx_buffer2 = { { 0 }, 0 , 0 };
48
- ring_buffer rx_buffer3 = { { 0 }, 0 , 0 };
68
+ // #if defined(__AVR_ATmega1280__)
69
+ #if defined(UBRR1H)
70
+ ring_buffer rx_buffer1 = { { 0 }, 0 , 0 };
71
+ #endif
72
+ #if defined(UBRR2H)
73
+ ring_buffer rx_buffer2 = { { 0 }, 0 , 0 };
74
+ #endif
75
+ #if defined(UBRR3H)
76
+ ring_buffer rx_buffer3 = { { 0 }, 0 , 0 };
49
77
#endif
50
78
79
+ // *******************************************************************************************
51
80
inline void store_char (unsigned char c, ring_buffer *rx_buffer)
52
81
{
53
82
int i = (rx_buffer->head + 1 ) % RX_BUFFER_SIZE;
@@ -62,52 +91,112 @@ inline void store_char(unsigned char c, ring_buffer *rx_buffer)
62
91
}
63
92
}
64
93
65
- #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__ )
66
-
67
- SIGNAL (SIG_USART0_RECV )
94
+ #if defined(USART_RX_vect )
95
+ // *******************************************************************************************
96
+ SIGNAL (USART_RX_vect )
68
97
{
69
- unsigned char c = UDR0;
70
- store_char (c, &rx_buffer);
98
+ #if defined(UDR0)
99
+ unsigned char c = UDR0;
100
+ #elif defined(UDR)
101
+ unsigned char c = UDR; // atmega8535
102
+ #else
103
+ #error UDR not defined
104
+ #endif
105
+ store_char (c, &rx_buffer);
71
106
}
72
-
73
- SIGNAL (SIG_USART1_RECV)
107
+ #elif defined(SIG_USART0_RECV) && defined(UDR0)
108
+ // *******************************************************************************************
109
+ SIGNAL (SIG_USART0_RECV)
74
110
{
75
- unsigned char c = UDR1 ;
76
- store_char (c, &rx_buffer1 );
111
+ unsigned char c = UDR0 ;
112
+ store_char (c, &rx_buffer );
77
113
}
78
-
79
- SIGNAL (SIG_USART2_RECV)
114
+ #elif defined(SIG_UART0_RECV) && defined(UDR0)
115
+ // *******************************************************************************************
116
+ SIGNAL (SIG_UART0_RECV)
80
117
{
81
- unsigned char c = UDR2 ;
82
- store_char (c, &rx_buffer2 );
118
+ unsigned char c = UDR0 ;
119
+ store_char (c, &rx_buffer );
83
120
}
84
-
85
- SIGNAL (SIG_USART3_RECV)
121
+ // #elif defined(SIG_USART_RECV)
122
+ #elif defined(USART0_RX_vect)
123
+ // *******************************************************************************************
124
+ // * fixed by Mark Sproul March 25, 2010
125
+ // * this is on the 644/644p
126
+ // SIGNAL(SIG_USART_RECV)
127
+ SIGNAL (USART0_RX_vect)
86
128
{
87
- unsigned char c = UDR3;
88
- store_char (c, &rx_buffer3) ;
89
- }
90
-
129
+ # if defined(UDR0)
130
+ unsigned char c = UDR0 ;
131
+ # elif defined(UDR)
132
+ unsigned char c = UDR; // atmega8, atmega32
91
133
#else
92
-
93
- #if defined(__AVR_ATmega8__)
134
+ #error UDR not defined
135
+ #endif
136
+ store_char (c, &rx_buffer);
137
+ }
138
+ #elif defined(SIG_UART_RECV)
139
+ // *******************************************************************************************
140
+ // * this is for atmega8
94
141
SIGNAL (SIG_UART_RECV)
142
+ {
143
+ #if defined(UDR0)
144
+ unsigned char c = UDR0; // atmega645
145
+ #elif defined(UDR)
146
+ unsigned char c = UDR; // atmega8
147
+ #endif
148
+
149
+ store_char (c, &rx_buffer);
150
+ }
151
+ #elif defined(USBCON)
152
+ #warning No interrupt handler for usart 0
153
+ #warning Serial(0) is on USB interface
95
154
#else
96
- SIGNAL (USART_RX_vect)
155
+ # error No interrupt handler for usart 0
97
156
#endif
157
+
158
+ // #if defined(SIG_USART1_RECV)
159
+ #if defined(USART1_RX_vect)
160
+ // *******************************************************************************************
161
+ // SIGNAL(SIG_USART1_RECV)
162
+ SIGNAL (USART1_RX_vect)
98
163
{
99
- #if defined(__AVR_ATmega8__)
100
- unsigned char c = UDR;
101
- #else
102
- unsigned char c = UDR0;
164
+ unsigned char c = UDR1;
165
+ store_char (c, &rx_buffer1);
166
+ }
167
+ #elif defined(SIG_USART1_RECV)
168
+ #error SIG_USART1_RECV
103
169
#endif
104
- store_char (c, &rx_buffer);
170
+
171
+
172
+ // #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
173
+ #if defined(USART2_RX_vect)
174
+ // *******************************************************************************************
175
+ SIGNAL (USART2_RX_vect)
176
+ {
177
+ unsigned char c = UDR2;
178
+ store_char (c, &rx_buffer2);
105
179
}
180
+ #elif defined(SIG_USART2_RECV)
181
+ #error SIG_USART2_RECV
182
+ #endif
106
183
184
+ #if defined(USART3_RX_vect)
185
+ // *******************************************************************************************
186
+ SIGNAL (USART3_RX_vect)
187
+ {
188
+ unsigned char c = UDR3;
189
+ store_char (c, &rx_buffer3);
190
+ }
191
+ #elif defined(SIG_USART3_RECV)
192
+ #error SIG_USART3_RECV
107
193
#endif
108
194
195
+
196
+
109
197
// Constructors ////////////////////////////////////////////////////////////////
110
198
199
+ // *******************************************************************************************
111
200
HardwareSerial::HardwareSerial (ring_buffer *rx_buffer,
112
201
volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
113
202
volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
@@ -129,6 +218,7 @@ HardwareSerial::HardwareSerial(ring_buffer *rx_buffer,
129
218
130
219
// Public Methods //////////////////////////////////////////////////////////////
131
220
221
+ // *******************************************************************************************
132
222
void HardwareSerial::begin (long baud)
133
223
{
134
224
uint16_t baud_setting;
@@ -166,18 +256,21 @@ void HardwareSerial::begin(long baud)
166
256
sbi (*_ucsrb, _rxcie);
167
257
}
168
258
259
+ // *******************************************************************************************
169
260
void HardwareSerial::end ()
170
261
{
171
262
cbi (*_ucsrb, _rxen);
172
263
cbi (*_ucsrb, _txen);
173
264
cbi (*_ucsrb, _rxcie);
174
265
}
175
266
267
+ // *******************************************************************************************
176
268
int HardwareSerial::available (void )
177
269
{
178
270
return (RX_BUFFER_SIZE + _rx_buffer->head - _rx_buffer->tail ) % RX_BUFFER_SIZE;
179
271
}
180
272
273
+ // *******************************************************************************************
181
274
int HardwareSerial::peek (void )
182
275
{
183
276
if (_rx_buffer->head == _rx_buffer->tail ) {
@@ -187,6 +280,7 @@ int HardwareSerial::peek(void)
187
280
}
188
281
}
189
282
283
+ // *******************************************************************************************
190
284
int HardwareSerial::read (void )
191
285
{
192
286
// if the head isn't ahead of the tail, we don't have any characters
@@ -199,6 +293,7 @@ int HardwareSerial::read(void)
199
293
}
200
294
}
201
295
296
+ // *******************************************************************************************
202
297
void HardwareSerial::flush ()
203
298
{
204
299
// don't reverse this or there may be problems if the RX interrupt
@@ -213,6 +308,7 @@ void HardwareSerial::flush()
213
308
_rx_buffer->head = _rx_buffer->tail ;
214
309
}
215
310
311
+ // *******************************************************************************************
216
312
void HardwareSerial::write (uint8_t c)
217
313
{
218
314
while (!((*_ucsra) & (1 << _udre)))
@@ -223,14 +319,27 @@ void HardwareSerial::write(uint8_t c)
223
319
224
320
// Preinstantiate Objects //////////////////////////////////////////////////////
225
321
226
- #if defined(__AVR_ATmega8__)
227
- HardwareSerial Serial (&rx_buffer, &UBRRH, &UBRRL, &UCSRA, &UCSRB, &UDR, RXEN, TXEN, RXCIE, UDRE, U2X);
322
+ // #if defined(__AVR_ATmega8__)
323
+ #if defined(UBRRH) && defined(UBRRL)
324
+ HardwareSerial Serial (&rx_buffer, &UBRRH, &UBRRL, &UCSRA, &UCSRB, &UDR, RXEN, TXEN, RXCIE, UDRE, U2X);
325
+ #elif defined(UBRR0H) && defined(UBRR0L)
326
+ HardwareSerial Serial (&rx_buffer, &UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UDR0, RXEN0, TXEN0, RXCIE0, UDRE0, U2X0);
327
+ #elif defined(USBCON)
328
+ #warning no serial port defined (port 0)
228
329
#else
229
- HardwareSerial Serial (&rx_buffer, &UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UDR0, RXEN0, TXEN0, RXCIE0, UDRE0, U2X0);
330
+ # error no serial port defined (port 0)
230
331
#endif
231
332
232
- #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
233
- HardwareSerial Serial1 (&rx_buffer1, &UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UDR1, RXEN1, TXEN1, RXCIE1, UDRE1, U2X1);
234
- HardwareSerial Serial2 (&rx_buffer2, &UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UDR2, RXEN2, TXEN2, RXCIE2, UDRE2, U2X2);
235
- HardwareSerial Serial3 (&rx_buffer3, &UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UDR3, RXEN3, TXEN3, RXCIE3, UDRE3, U2X3);
333
+ // #if defined(__AVR_ATmega1280__)
334
+ #if defined(UBRR1H)
335
+ HardwareSerial Serial1 (&rx_buffer1, &UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UDR1, RXEN1, TXEN1, RXCIE1, UDRE1, U2X1);
336
+ #endif
337
+ #if defined(UBRR2H)
338
+ HardwareSerial Serial2 (&rx_buffer2, &UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UDR2, RXEN2, TXEN2, RXCIE2, UDRE2, U2X2);
339
+ #endif
340
+ #if defined(UBRR3H)
341
+ HardwareSerial Serial3 (&rx_buffer3, &UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UDR3, RXEN3, TXEN3, RXCIE3, UDRE3, U2X3);
236
342
#endif
343
+
344
+
345
+ #endif // defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H)
0 commit comments