2828#include " Arduino.h"
2929#include " wiring_private.h"
3030
31+ #include " HardwareSerial.h"
32+
3133// this next line disables the entire HardwareSerial.cpp,
3234// this is so I can support Attiny series and any other chip without a uart
33- #if defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H)
34-
35- #include " HardwareSerial.h"
35+ #if defined(HAVE_HWSERIAL0) || defined(HAVE_HWSERIAL1) || defined(HAVE_HWSERIAL2) || defined(HAVE_HWSERIAL3)
3636
3737// Ensure that the various bit positions we use are available with a 0
3838// postfix, so we can always use the values for UART0 for all UARTs. The
8484#error "Not all bit positions for UART3 are the same as for UART0"
8585#endif
8686
87- #if !defined(USART0_RX_vect) && defined(USART1_RX_vect)
88- // do nothing - on the 32u4 the first USART is USART1
89- #else
90- #if !defined(USART_RX_vect) && !defined(USART0_RX_vect) && \
91- !defined (USART_RXC_vect)
92- #error "Don't know what the Data Received vector is called for the first UART"
93- #else
87+ #if defined(HAVE_HWSERIAL0)
9488 void serialEvent () __attribute__((weak));
9589 void serialEvent () {}
96- #define serialEvent_implemented
9790#if defined(USART_RX_vect)
9891 ISR (USART_RX_vect)
9992#elif defined(USART0_RX_vect)
10093 ISR (USART0_RX_vect)
10194#elif defined(USART_RXC_vect)
10295 ISR (USART_RXC_vect) // ATmega8
96+ #else
97+ #error "Don't know what the Data Received vector is called for the first UART"
10398#endif
10499 {
105100 Serial._rx_complete_irq ();
106101 }
107102#endif
108- #endif
109103
110- #if defined(USART1_RX_vect )
104+ #if defined(HAVE_HWSERIAL1 )
111105 void serialEvent1 () __attribute__((weak));
112106 void serialEvent1 () {}
113- #define serialEvent1_implemented
114107 ISR (USART1_RX_vect)
115108 {
116109 Serial1._rx_complete_irq ();
117110 }
118111#endif
119112
120- #if defined(USART2_RX_vect) && defined(UDR2 )
113+ #if defined(HAVE_HWSERIAL2 )
121114 void serialEvent2 () __attribute__((weak));
122115 void serialEvent2 () {}
123- #define serialEvent2_implemented
124116 ISR (USART2_RX_vect)
125117 {
126118 Serial2._rx_complete_irq ();
127119 }
128120#endif
129121
130- #if defined(USART3_RX_vect) && defined(UDR3 )
122+ #if defined(HAVE_HWSERIAL3 )
131123 void serialEvent3 () __attribute__((weak));
132124 void serialEvent3 () {}
133- #define serialEvent3_implemented
134125 ISR (USART3_RX_vect)
135126 {
136127 Serial3._rx_complete_irq ();
139130
140131void serialEventRun (void )
141132{
142- #ifdef serialEvent_implemented
133+ #if defined(HAVE_HWSERIAL0)
143134 if (Serial.available ()) serialEvent ();
144135#endif
145- #ifdef serialEvent1_implemented
136+ #if defined(HAVE_HWSERIAL1)
146137 if (Serial1.available ()) serialEvent1 ();
147138#endif
148- #ifdef serialEvent2_implemented
139+ #if defined(HAVE_HWSERIAL2)
149140 if (Serial2.available ()) serialEvent2 ();
150141#endif
151- #ifdef serialEvent3_implemented
142+ #if defined(HAVE_HWSERIAL3)
152143 if (Serial3.available ()) serialEvent3 ();
153144#endif
154145}
155146
156147
157- #if !defined(USART0_UDRE_vect) && defined(USART1_UDRE_vect)
158- // do nothing - on the 32u4 the first USART is USART1
159- #else
160- #if !defined(UART0_UDRE_vect) && !defined(UART_UDRE_vect) && !defined(USART0_UDRE_vect) && !defined(USART_UDRE_vect)
161- #error "Don't know what the Data Register Empty vector is called for the first UART"
162- #else
148+ #if defined(HAVE_HWSERIAL0)
163149#if defined(UART0_UDRE_vect)
164150ISR (UART0_UDRE_vect)
165151#elif defined(UART_UDRE_vect)
@@ -168,28 +154,29 @@ ISR(UART_UDRE_vect)
168154ISR (USART0_UDRE_vect)
169155#elif defined(USART_UDRE_vect)
170156ISR (USART_UDRE_vect)
157+ #else
158+ #error "Don't know what the Data Register Empty vector is called for the first UART"
171159#endif
172160{
173161 Serial._tx_udr_empty_irq ();
174162}
175163#endif
176- #endif
177164
178- #ifdef USART1_UDRE_vect
165+ #if defined(HAVE_HWSERIAL1)
179166ISR (USART1_UDRE_vect)
180167{
181168 Serial1._tx_udr_empty_irq ();
182169}
183170#endif
184171
185- #ifdef USART2_UDRE_vect
172+ #if defined(HAVE_HWSERIAL2)
186173ISR (USART2_UDRE_vect)
187174{
188175 Serial2._tx_udr_empty_irq ();
189176}
190177#endif
191178
192- #ifdef USART3_UDRE_vect
179+ #if defined(HAVE_HWSERIAL3)
193180ISR (USART3_UDRE_vect)
194181{
195182 Serial3._tx_udr_empty_irq ();
@@ -386,23 +373,21 @@ size_t HardwareSerial::write(uint8_t c)
386373
387374// Preinstantiate Objects //////////////////////////////////////////////////////
388375
376+ #if defined(HAVE_HWSERIAL0)
389377#if defined(UBRRH) && defined(UBRRL)
390378 HardwareSerial Serial (&UBRRH, &UBRRL, &UCSRA, &UCSRB, &UCSRC, &UDR);
391- #elif defined(UBRR0H) && defined(UBRR0L)
392- HardwareSerial Serial (&UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UCSR0C, &UDR0);
393- #elif defined(USBCON)
394- // do nothing - Serial object and buffers are initialized in CDC code
395379#else
396- #error no serial port defined (port 0)
380+ HardwareSerial Serial (&UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UCSR0C, &UDR0);
381+ #endif
397382#endif
398383
399- #if defined(UBRR1H )
384+ #if defined(HAVE_HWSERIAL1 )
400385 HardwareSerial Serial1 (&UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UCSR1C, &UDR1);
401386#endif
402- #if defined(UBRR2H )
387+ #if defined(HAVE_HWSERIAL2 )
403388 HardwareSerial Serial2 (&UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UCSR2C, &UDR2);
404389#endif
405- #if defined(UBRR3H )
390+ #if defined(HAVE_HWSERIAL3 )
406391 HardwareSerial Serial3 (&UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UCSR3C, &UDR3);
407392#endif
408393
0 commit comments