|
84 | 84 | #error "Not all bit positions for UART3 are the same as for UART0"
|
85 | 85 | #endif
|
86 | 86 |
|
| 87 | +// SerialEvent functions are weak, so when the user doesn't define them, |
| 88 | +// the linker just sets their address to 0 (which is checked below). |
| 89 | +// The Serialx_available is just a wrapper around Serialx.available(), |
| 90 | +// but we can refer to it weakly so we don't pull in the entire |
| 91 | +// HardwareSerial instance if the user doesn't also refer to it. |
87 | 92 | #if defined(HAVE_HWSERIAL0)
|
88 | 93 | void serialEvent() __attribute__((weak));
|
89 |
| - void serialEvent() {} |
90 |
| -#if defined(USART_RX_vect) |
91 |
| - ISR(USART_RX_vect) |
92 |
| -#elif defined(USART0_RX_vect) |
93 |
| - ISR(USART0_RX_vect) |
94 |
| -#elif defined(USART_RXC_vect) |
95 |
| - ISR(USART_RXC_vect) // ATmega8 |
96 |
| -#else |
97 |
| - #error "Don't know what the Data Received vector is called for the first UART" |
98 |
| -#endif |
99 |
| - { |
100 |
| - Serial._rx_complete_irq(); |
101 |
| - } |
| 94 | + bool Serial0_available() __attribute__((weak)); |
102 | 95 | #endif
|
103 | 96 |
|
104 | 97 | #if defined(HAVE_HWSERIAL1)
|
105 | 98 | void serialEvent1() __attribute__((weak));
|
106 |
| - void serialEvent1() {} |
107 |
| - ISR(USART1_RX_vect) |
108 |
| - { |
109 |
| - Serial1._rx_complete_irq(); |
110 |
| - } |
| 99 | + bool Serial1_available() __attribute__((weak)); |
111 | 100 | #endif
|
112 | 101 |
|
113 | 102 | #if defined(HAVE_HWSERIAL2)
|
114 | 103 | void serialEvent2() __attribute__((weak));
|
115 |
| - void serialEvent2() {} |
116 |
| - ISR(USART2_RX_vect) |
117 |
| - { |
118 |
| - Serial2._rx_complete_irq(); |
119 |
| - } |
| 104 | + bool Serial2_available() __attribute__((weak)); |
120 | 105 | #endif
|
121 | 106 |
|
122 | 107 | #if defined(HAVE_HWSERIAL3)
|
123 | 108 | void serialEvent3() __attribute__((weak));
|
124 |
| - void serialEvent3() {} |
125 |
| - ISR(USART3_RX_vect) |
126 |
| - { |
127 |
| - Serial3._rx_complete_irq(); |
128 |
| - } |
| 109 | + bool Serial3_available() __attribute__((weak)); |
129 | 110 | #endif
|
130 | 111 |
|
131 | 112 | void serialEventRun(void)
|
132 | 113 | {
|
133 | 114 | #if defined(HAVE_HWSERIAL0)
|
134 |
| - if (Serial.available()) serialEvent(); |
| 115 | + if (Serial0_available && serialEvent && Serial0_available()) serialEvent(); |
135 | 116 | #endif
|
136 | 117 | #if defined(HAVE_HWSERIAL1)
|
137 |
| - if (Serial1.available()) serialEvent1(); |
| 118 | + if (Serial1_available && serialEvent1 && Serial1_available()) serialEvent1(); |
138 | 119 | #endif
|
139 | 120 | #if defined(HAVE_HWSERIAL2)
|
140 |
| - if (Serial2.available()) serialEvent2(); |
| 121 | + if (Serial2_available && serialEvent2 && Serial2_available()) serialEvent2(); |
141 | 122 | #endif
|
142 | 123 | #if defined(HAVE_HWSERIAL3)
|
143 |
| - if (Serial3.available()) serialEvent3(); |
144 |
| -#endif |
145 |
| -} |
146 |
| - |
147 |
| - |
148 |
| -#if defined(HAVE_HWSERIAL0) |
149 |
| -#if defined(UART0_UDRE_vect) |
150 |
| -ISR(UART0_UDRE_vect) |
151 |
| -#elif defined(UART_UDRE_vect) |
152 |
| -ISR(UART_UDRE_vect) |
153 |
| -#elif defined(USART0_UDRE_vect) |
154 |
| -ISR(USART0_UDRE_vect) |
155 |
| -#elif defined(USART_UDRE_vect) |
156 |
| -ISR(USART_UDRE_vect) |
157 |
| -#else |
158 |
| - #error "Don't know what the Data Register Empty vector is called for the first UART" |
159 |
| -#endif |
160 |
| -{ |
161 |
| - Serial._tx_udr_empty_irq(); |
162 |
| -} |
| 124 | + if (Serial3_available && serialEvent2 && Serial3_available()) serialEvent3(); |
163 | 125 | #endif
|
164 |
| - |
165 |
| -#if defined(HAVE_HWSERIAL1) |
166 |
| -ISR(USART1_UDRE_vect) |
167 |
| -{ |
168 |
| - Serial1._tx_udr_empty_irq(); |
169 | 126 | }
|
170 |
| -#endif |
171 |
| - |
172 |
| -#if defined(HAVE_HWSERIAL2) |
173 |
| -ISR(USART2_UDRE_vect) |
174 |
| -{ |
175 |
| - Serial2._tx_udr_empty_irq(); |
176 |
| -} |
177 |
| -#endif |
178 |
| - |
179 |
| -#if defined(HAVE_HWSERIAL3) |
180 |
| -ISR(USART3_UDRE_vect) |
181 |
| -{ |
182 |
| - Serial3._tx_udr_empty_irq(); |
183 |
| -} |
184 |
| -#endif |
185 |
| - |
186 | 127 |
|
187 | 128 | // Actual interrupt handlers //////////////////////////////////////////////////////////////
|
188 | 129 |
|
@@ -371,25 +312,5 @@ size_t HardwareSerial::write(uint8_t c)
|
371 | 312 | return 1;
|
372 | 313 | }
|
373 | 314 |
|
374 |
| -// Preinstantiate Objects ////////////////////////////////////////////////////// |
375 |
| - |
376 |
| -#if defined(HAVE_HWSERIAL0) |
377 |
| -#if defined(UBRRH) && defined(UBRRL) |
378 |
| - HardwareSerial Serial(&UBRRH, &UBRRL, &UCSRA, &UCSRB, &UCSRC, &UDR); |
379 |
| -#else |
380 |
| - HardwareSerial Serial(&UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UCSR0C, &UDR0); |
381 |
| -#endif |
382 |
| -#endif |
383 |
| - |
384 |
| -#if defined(HAVE_HWSERIAL1) |
385 |
| - HardwareSerial Serial1(&UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UCSR1C, &UDR1); |
386 |
| -#endif |
387 |
| -#if defined(HAVE_HWSERIAL2) |
388 |
| - HardwareSerial Serial2(&UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UCSR2C, &UDR2); |
389 |
| -#endif |
390 |
| -#if defined(HAVE_HWSERIAL3) |
391 |
| - HardwareSerial Serial3(&UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UCSR3C, &UDR3); |
392 |
| -#endif |
393 | 315 |
|
394 | 316 | #endif // whole file
|
395 |
| - |
0 commit comments