49
49
#endif
50
50
#endif
51
51
52
- // Define constants and variables for buffering incoming serial data. We're
53
- // using a ring buffer (I think), in which head is the index of the location
54
- // to which to write the next incoming character and tail is the index of the
55
- // location from which to read.
56
- #if (RAMEND < 1000)
57
- #define SERIAL_BUFFER_SIZE 16
58
- #else
59
- #define SERIAL_BUFFER_SIZE 64
60
- #endif
61
-
62
- struct ring_buffer
63
- {
64
- unsigned char buffer[SERIAL_BUFFER_SIZE];
65
- volatile uint8_t head;
66
- volatile uint8_t tail;
67
- };
68
-
69
- #if SERIAL_BUFFER_SIZE > 256
70
- #error Serial buffer size too big for head and tail pointers
71
- #endif
72
-
73
- #if defined(USBCON)
74
- ring_buffer rx_buffer = { { 0 }, 0 , 0 };
75
- ring_buffer tx_buffer = { { 0 }, 0 , 0 };
76
- #endif
77
- #if defined(UBRRH) || defined(UBRR0H)
78
- ring_buffer rx_buffer = { { 0 }, 0 , 0 };
79
- ring_buffer tx_buffer = { { 0 }, 0 , 0 };
80
- #endif
81
- #if defined(UBRR1H)
82
- ring_buffer rx_buffer1 = { { 0 }, 0 , 0 };
83
- ring_buffer tx_buffer1 = { { 0 }, 0 , 0 };
84
- #endif
85
- #if defined(UBRR2H)
86
- ring_buffer rx_buffer2 = { { 0 }, 0 , 0 };
87
- ring_buffer tx_buffer2 = { { 0 }, 0 , 0 };
88
- #endif
89
- #if defined(UBRR3H)
90
- ring_buffer rx_buffer3 = { { 0 }, 0 , 0 };
91
- ring_buffer tx_buffer3 = { { 0 }, 0 , 0 };
92
- #endif
93
-
94
- inline void store_char (unsigned char c, ring_buffer *buffer)
52
+ inline void store_char (unsigned char c, HardwareSerial *s)
95
53
{
96
- int i = (unsigned int )(buffer-> head + 1 ) % SERIAL_BUFFER_SIZE;
54
+ int i = (unsigned int )(s-> _rx_buffer_head + 1 ) % SERIAL_BUFFER_SIZE;
97
55
98
56
// if we should be storing the received character into the location
99
57
// just before the tail (meaning that the head would advance to the
100
58
// current location of the tail), we're about to overflow the buffer
101
59
// and so we don't write the character or advance the head.
102
- if (i != buffer-> tail ) {
103
- buffer-> buffer [buffer-> head ] = c;
104
- buffer-> head = i;
60
+ if (i != s-> _rx_buffer_tail ) {
61
+ s-> _rx_buffer [s-> _rx_buffer_head ] = c;
62
+ s-> _rx_buffer_head = i;
105
63
}
106
64
}
107
65
@@ -126,7 +84,7 @@ inline void store_char(unsigned char c, ring_buffer *buffer)
126
84
#if defined(UDR0)
127
85
if (bit_is_clear (UCSR0A, UPE0)) {
128
86
unsigned char c = UDR0;
129
- store_char (c, &rx_buffer );
87
+ store_char (c, &Serial );
130
88
} else {
131
89
unsigned char c = UDR0;
132
90
};
@@ -152,7 +110,7 @@ inline void store_char(unsigned char c, ring_buffer *buffer)
152
110
{
153
111
if (bit_is_clear (UCSR1A, UPE1)) {
154
112
unsigned char c = UDR1;
155
- store_char (c, &rx_buffer1 );
113
+ store_char (c, &Serial1 );
156
114
} else {
157
115
unsigned char c = UDR1;
158
116
};
@@ -167,7 +125,7 @@ inline void store_char(unsigned char c, ring_buffer *buffer)
167
125
{
168
126
if (bit_is_clear (UCSR2A, UPE2)) {
169
127
unsigned char c = UDR2;
170
- store_char (c, &rx_buffer2 );
128
+ store_char (c, &Serial2 );
171
129
} else {
172
130
unsigned char c = UDR2;
173
131
};
@@ -182,7 +140,7 @@ inline void store_char(unsigned char c, ring_buffer *buffer)
182
140
{
183
141
if (bit_is_clear (UCSR3A, UPE3)) {
184
142
unsigned char c = UDR3;
185
- store_char (c, &rx_buffer3 );
143
+ store_char (c, &Serial3 );
186
144
} else {
187
145
unsigned char c = UDR3;
188
146
};
@@ -222,7 +180,7 @@ ISR(USART0_UDRE_vect)
222
180
ISR (USART_UDRE_vect)
223
181
#endif
224
182
{
225
- if (tx_buffer. head == tx_buffer. tail ) {
183
+ if (Serial. _tx_buffer_head == Serial. _tx_buffer_tail ) {
226
184
// Buffer empty, so disable interrupts
227
185
#if defined(UCSR0B)
228
186
cbi (UCSR0B, UDRIE0);
@@ -232,8 +190,8 @@ ISR(USART_UDRE_vect)
232
190
}
233
191
else {
234
192
// There is more data in the output buffer. Send the next byte
235
- unsigned char c = tx_buffer. buffer [tx_buffer. tail ];
236
- tx_buffer. tail = (tx_buffer. tail + 1 ) % SERIAL_BUFFER_SIZE;
193
+ unsigned char c = Serial. _tx_buffer [Serial. _tx_buffer_tail ];
194
+ Serial. _tx_buffer_tail = (Serial. _tx_buffer_tail + 1 ) % SERIAL_BUFFER_SIZE;
237
195
238
196
#if defined(UDR0)
239
197
UDR0 = c;
@@ -250,14 +208,14 @@ ISR(USART_UDRE_vect)
250
208
#ifdef USART1_UDRE_vect
251
209
ISR (USART1_UDRE_vect)
252
210
{
253
- if (tx_buffer1. head == tx_buffer1. tail ) {
211
+ if (Serial1. _tx_buffer_head == Serial1. _tx_buffer_tail ) {
254
212
// Buffer empty, so disable interrupts
255
213
cbi (UCSR1B, UDRIE1);
256
214
}
257
215
else {
258
216
// There is more data in the output buffer. Send the next byte
259
- unsigned char c = tx_buffer1. buffer [tx_buffer1. tail ];
260
- tx_buffer1. tail = (tx_buffer1. tail + 1 ) % SERIAL_BUFFER_SIZE;
217
+ unsigned char c = Serial1. _tx_buffer [Serial1. _tx_buffer_tail ];
218
+ Serial1. _tx_buffer_tail = (Serial1. _tx_buffer_tail + 1 ) % SERIAL_BUFFER_SIZE;
261
219
262
220
UDR1 = c;
263
221
}
@@ -267,14 +225,14 @@ ISR(USART1_UDRE_vect)
267
225
#ifdef USART2_UDRE_vect
268
226
ISR (USART2_UDRE_vect)
269
227
{
270
- if (tx_buffer2. head == tx_buffer2. tail ) {
228
+ if (Serial2. _tx_buffer_head == Serial2. _tx_buffer_tail ) {
271
229
// Buffer empty, so disable interrupts
272
230
cbi (UCSR2B, UDRIE2);
273
231
}
274
232
else {
275
233
// There is more data in the output buffer. Send the next byte
276
- unsigned char c = tx_buffer2. buffer [tx_buffer2. tail ];
277
- tx_buffer2. tail = (tx_buffer2. tail + 1 ) % SERIAL_BUFFER_SIZE;
234
+ unsigned char c = Serial2. _tx_buffer [Serial2. _tx_buffer_tail ];
235
+ Serial2. _tx_buffer_tail = (Serial2. _tx_buffer_tail + 1 ) % SERIAL_BUFFER_SIZE;
278
236
279
237
UDR2 = c;
280
238
}
@@ -284,31 +242,30 @@ ISR(USART2_UDRE_vect)
284
242
#ifdef USART3_UDRE_vect
285
243
ISR (USART3_UDRE_vect)
286
244
{
287
- if (tx_buffer3. head == tx_buffer3. tail ) {
245
+ if (Serial3. _tx_buffer_head == Serial3. _tx_buffer_tail ) {
288
246
// Buffer empty, so disable interrupts
289
247
cbi (UCSR3B, UDRIE3);
290
248
}
291
249
else {
292
250
// There is more data in the output buffer. Send the next byte
293
- unsigned char c = tx_buffer3. buffer [tx_buffer3. tail ];
294
- tx_buffer3. tail = (tx_buffer3. tail + 1 ) % SERIAL_BUFFER_SIZE;
251
+ unsigned char c = Serial3. _tx_buffer [Serial3. _tx_buffer_tail ];
252
+ Serial3. _tx_buffer_tail = (Serial3. _tx_buffer_tail + 1 ) % SERIAL_BUFFER_SIZE;
295
253
296
254
UDR3 = c;
297
255
}
298
256
}
299
257
#endif
300
258
301
-
302
259
// Constructors ////////////////////////////////////////////////////////////////
303
260
304
- HardwareSerial::HardwareSerial (ring_buffer *rx_buffer, ring_buffer *tx_buffer,
261
+ HardwareSerial::HardwareSerial (
305
262
volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
306
263
volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
307
264
volatile uint8_t *ucsrc, volatile uint8_t *udr,
308
265
uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udrie, uint8_t u2x)
309
266
{
310
- _rx_buffer = rx_buffer ;
311
- _tx_buffer = tx_buffer ;
267
+ _tx_buffer_head = _tx_buffer_tail = 0 ;
268
+ _rx_buffer_head = _rx_buffer_tail = 0 ;
312
269
_ubrrh = ubrrh;
313
270
_ubrrl = ubrrl;
314
271
_ucsra = ucsra;
@@ -416,7 +373,7 @@ void HardwareSerial::begin(unsigned long baud, byte config)
416
373
void HardwareSerial::end ()
417
374
{
418
375
// wait for transmission of outgoing data
419
- while (_tx_buffer-> head != _tx_buffer-> tail )
376
+ while (_tx_buffer_head != _tx_buffer_tail )
420
377
;
421
378
422
379
cbi (*_ucsrb, _rxen);
@@ -425,31 +382,31 @@ void HardwareSerial::end()
425
382
cbi (*_ucsrb, _udrie);
426
383
427
384
// clear any received data
428
- _rx_buffer-> head = _rx_buffer-> tail ;
385
+ _rx_buffer_head = _rx_buffer_tail ;
429
386
}
430
387
431
388
int HardwareSerial::available (void )
432
389
{
433
- return (unsigned int )(SERIAL_BUFFER_SIZE + _rx_buffer-> head - _rx_buffer-> tail ) % SERIAL_BUFFER_SIZE;
390
+ return (unsigned int )(SERIAL_BUFFER_SIZE + _rx_buffer_head - _rx_buffer_tail ) % SERIAL_BUFFER_SIZE;
434
391
}
435
392
436
393
int HardwareSerial::peek (void )
437
394
{
438
- if (_rx_buffer-> head == _rx_buffer-> tail ) {
395
+ if (_rx_buffer_head == _rx_buffer_tail ) {
439
396
return -1 ;
440
397
} else {
441
- return _rx_buffer-> buffer [_rx_buffer-> tail ];
398
+ return _rx_buffer[_rx_buffer_tail ];
442
399
}
443
400
}
444
401
445
402
int HardwareSerial::read (void )
446
403
{
447
404
// if the head isn't ahead of the tail, we don't have any characters
448
- if (_rx_buffer-> head == _rx_buffer-> tail ) {
405
+ if (_rx_buffer_head == _rx_buffer_tail ) {
449
406
return -1 ;
450
407
} else {
451
- unsigned char c = _rx_buffer-> buffer [_rx_buffer-> tail ];
452
- _rx_buffer-> tail = (unsigned int )(_rx_buffer-> tail + 1 ) % SERIAL_BUFFER_SIZE;
408
+ unsigned char c = _rx_buffer[_rx_buffer_tail ];
409
+ _rx_buffer_tail = (unsigned int )(_rx_buffer_tail + 1 ) % SERIAL_BUFFER_SIZE;
453
410
return c;
454
411
}
455
412
}
@@ -463,16 +420,16 @@ void HardwareSerial::flush()
463
420
464
421
size_t HardwareSerial::write (uint8_t c)
465
422
{
466
- int i = (_tx_buffer-> head + 1 ) % SERIAL_BUFFER_SIZE;
423
+ int i = (_tx_buffer_head + 1 ) % SERIAL_BUFFER_SIZE;
467
424
468
425
// If the output buffer is full, there's nothing for it other than to
469
426
// wait for the interrupt handler to empty it a bit
470
427
// ???: return 0 here instead?
471
- while (i == _tx_buffer-> tail )
428
+ while (i == _tx_buffer_tail )
472
429
;
473
430
474
- _tx_buffer-> buffer [_tx_buffer-> head ] = c;
475
- _tx_buffer-> head = i;
431
+ _tx_buffer[_tx_buffer_head ] = c;
432
+ _tx_buffer_head = i;
476
433
477
434
sbi (*_ucsrb, _udrie);
478
435
// clear the TXC bit -- "can be cleared by writing a one to its bit location"
@@ -489,23 +446,23 @@ HardwareSerial::operator bool() {
489
446
// Preinstantiate Objects //////////////////////////////////////////////////////
490
447
491
448
#if defined(UBRRH) && defined(UBRRL)
492
- HardwareSerial Serial (&rx_buffer, &tx_buffer, & UBRRH, &UBRRL, &UCSRA, &UCSRB, &UCSRC, &UDR, RXEN, TXEN, RXCIE, UDRIE, U2X);
449
+ HardwareSerial Serial (&UBRRH, &UBRRL, &UCSRA, &UCSRB, &UCSRC, &UDR, RXEN, TXEN, RXCIE, UDRIE, U2X);
493
450
#elif defined(UBRR0H) && defined(UBRR0L)
494
- HardwareSerial Serial (&rx_buffer, &tx_buffer, & UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UCSR0C, &UDR0, RXEN0, TXEN0, RXCIE0, UDRIE0, U2X0);
451
+ HardwareSerial Serial (&UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UCSR0C, &UDR0, RXEN0, TXEN0, RXCIE0, UDRIE0, U2X0);
495
452
#elif defined(USBCON)
496
453
// do nothing - Serial object and buffers are initialized in CDC code
497
454
#else
498
455
#error no serial port defined (port 0)
499
456
#endif
500
457
501
458
#if defined(UBRR1H)
502
- HardwareSerial Serial1 (&rx_buffer1, &tx_buffer1, & UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UCSR1C, &UDR1, RXEN1, TXEN1, RXCIE1, UDRIE1, U2X1);
459
+ HardwareSerial Serial1 (&UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UCSR1C, &UDR1, RXEN1, TXEN1, RXCIE1, UDRIE1, U2X1);
503
460
#endif
504
461
#if defined(UBRR2H)
505
- HardwareSerial Serial2 (&rx_buffer2, &tx_buffer2, & UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UCSR2C, &UDR2, RXEN2, TXEN2, RXCIE2, UDRIE2, U2X2);
462
+ HardwareSerial Serial2 (&UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UCSR2C, &UDR2, RXEN2, TXEN2, RXCIE2, UDRIE2, U2X2);
506
463
#endif
507
464
#if defined(UBRR3H)
508
- HardwareSerial Serial3 (&rx_buffer3, &tx_buffer3, & UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UCSR3C, &UDR3, RXEN3, TXEN3, RXCIE3, UDRIE3, U2X3);
465
+ HardwareSerial Serial3 (&UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UCSR3C, &UDR3, RXEN3, TXEN3, RXCIE3, UDRIE3, U2X3);
509
466
#endif
510
467
511
468
#endif // whole file
0 commit comments