|
19 | 19 | Modified 23 November 2006 by David A. Mellis
|
20 | 20 | Modified 28 September 2010 by Mark Sproul
|
21 | 21 | Modified 14 August 2012 by Alarus
|
| 22 | + Modified 2 November 2015 by SlashDev |
| 23 | + Modified 7 November 2019 by Georg Icking-Konert |
22 | 24 | */
|
23 | 25 |
|
24 | 26 | #include "wiring_private.h"
|
@@ -92,32 +94,44 @@ HardwareSerial::HardwareSerial(
|
92 | 94 | _ucsra(ucsra), _ucsrb(ucsrb), _ucsrc(ucsrc),
|
93 | 95 | _udr(udr),
|
94 | 96 | _rx_buffer_head(0), _rx_buffer_tail(0),
|
95 |
| - _tx_buffer_head(0), _tx_buffer_tail(0) |
| 97 | + _tx_buffer_head(0), _tx_buffer_tail(0), |
| 98 | + _isr(0) |
96 | 99 | {
|
97 | 100 | }
|
98 | 101 |
|
99 | 102 | // Actual interrupt handlers //////////////////////////////////////////////////////////////
|
100 | 103 |
|
101 | 104 | void HardwareSerial::_rx_complete_irq(void)
|
102 | 105 | {
|
103 |
| - if (bit_is_clear(*_ucsra, UPE0)) { |
104 |
| - // No Parity error, read byte and store it in the buffer if there is |
105 |
| - // room |
106 |
| - unsigned char c = *_udr; |
107 |
| - rx_buffer_index_t i = (unsigned int)(_rx_buffer_head + 1) % SERIAL_RX_BUFFER_SIZE; |
| 106 | + // user function was attached -> call it with data and status byte |
| 107 | + if (_isr) { |
| 108 | + unsigned char status = *_ucsra; |
| 109 | + unsigned char data = *_udr; |
| 110 | + _isr( data, status ); |
| 111 | + } |
108 | 112 |
|
109 |
| - // if we should be storing the received character into the location |
110 |
| - // just before the tail (meaning that the head would advance to the |
111 |
| - // current location of the tail), we're about to overflow the buffer |
112 |
| - // and so we don't write the character or advance the head. |
113 |
| - if (i != _rx_buffer_tail) { |
114 |
| - _rx_buffer[_rx_buffer_head] = c; |
115 |
| - _rx_buffer_head = i; |
116 |
| - } |
117 |
| - } else { |
118 |
| - // Parity error, read byte but discard it |
119 |
| - *_udr; |
120 |
| - }; |
| 113 | + // default: save data in ring buffer |
| 114 | + else { |
| 115 | + if (bit_is_clear(*_ucsra, UPE0)) { |
| 116 | + unsigned char c = *_udr; |
| 117 | + // No Parity error, read byte and store it in the buffer if there is |
| 118 | + // room |
| 119 | + rx_buffer_index_t i = (unsigned int)(_rx_buffer_head + 1) % SERIAL_RX_BUFFER_SIZE; |
| 120 | + |
| 121 | + // if we should be storing the received character into the location |
| 122 | + // just before the tail (meaning that the head would advance to the |
| 123 | + // current location of the tail), we're about to overflow the buffer |
| 124 | + // and so we don't write the character or advance the head. |
| 125 | + if (i != _rx_buffer_tail) { |
| 126 | + _rx_buffer[_rx_buffer_head] = c; |
| 127 | + _rx_buffer_head = i; |
| 128 | + } |
| 129 | + } |
| 130 | + else { |
| 131 | + // Parity error, read byte but discard it |
| 132 | + *_udr; |
| 133 | + }; |
| 134 | + } |
121 | 135 | }
|
122 | 136 |
|
123 | 137 | #endif // whole file
|
0 commit comments