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