Skip to content

Commit 8e43c1a

Browse files
matthijskooijmancmaglie
authored andcommitted
Centrally decide which hardware UARTS are available
Before, this decision was made in few different places, based on sometimes different register defines. Now, HardwareSerial.h decides wich UARTS are available, defines USE_HWSERIALn macros and HardwareSerial.cpp simply checks these macros (together with some #ifs to decide which registers to use for UART 0). For consistency, USBAPI.h also defines a HAVE_CDCSERIAL macro when applicable. For supported targets, this should change any behaviour. For unsupported targets, the error messages might subtly change because some checks are moved or changed. Additionally, this moves the USBAPI.h include form HardareSerial.h into Arduino.h and raises an error when both CDC serial and UART0 are available (previously this would silently use UART0 instead of CDC, but there is not currently any Atmel chip available for which this would occur).
1 parent 0be4e8c commit 8e43c1a

File tree

4 files changed

+35
-43
lines changed

4 files changed

+35
-43
lines changed

Diff for: hardware/arduino/avr/cores/arduino/Arduino.h

+4
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ extern const uint8_t PROGMEM digital_pin_to_timer_PGM[];
200200
#include "WCharacter.h"
201201
#include "WString.h"
202202
#include "HardwareSerial.h"
203+
#include "USBAPI.h"
204+
#if defined(HAVE_HWSERIAL0) && defined(HAVE_CDCSERIAL)
205+
#error "Targets with both UART0 and CDC serial not supported"
206+
#endif
203207

204208
uint16_t makeWord(uint16_t w);
205209
uint16_t makeWord(byte h, byte l);

Diff for: hardware/arduino/avr/cores/arduino/HardwareSerial.cpp

+25-40
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
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
@@ -84,53 +84,44 @@
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();
@@ -139,27 +130,22 @@
139130

140131
void 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)
164150
ISR(UART0_UDRE_vect)
165151
#elif defined(UART_UDRE_vect)
@@ -168,28 +154,29 @@ ISR(UART_UDRE_vect)
168154
ISR(USART0_UDRE_vect)
169155
#elif defined(USART_UDRE_vect)
170156
ISR(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)
179166
ISR(USART1_UDRE_vect)
180167
{
181168
Serial1._tx_udr_empty_irq();
182169
}
183170
#endif
184171

185-
#ifdef USART2_UDRE_vect
172+
#if defined(HAVE_HWSERIAL2)
186173
ISR(USART2_UDRE_vect)
187174
{
188175
Serial2._tx_udr_empty_irq();
189176
}
190177
#endif
191178

192-
#ifdef USART3_UDRE_vect
179+
#if defined(HAVE_HWSERIAL3)
193180
ISR(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

Diff for: hardware/arduino/avr/cores/arduino/HardwareSerial.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,19 @@ class HardwareSerial : public Stream
113113

114114
#if defined(UBRRH) || defined(UBRR0H)
115115
extern HardwareSerial Serial;
116-
#elif defined(USBCON)
117-
#include "USBAPI.h"
118-
// extern HardwareSerial Serial_;
116+
#define HAVE_HWSERIAL0
119117
#endif
120118
#if defined(UBRR1H)
121119
extern HardwareSerial Serial1;
120+
#define HAVE_HWSERIAL1
122121
#endif
123122
#if defined(UBRR2H)
124123
extern HardwareSerial Serial2;
124+
#define HAVE_HWSERIAL2
125125
#endif
126126
#if defined(UBRR3H)
127127
extern HardwareSerial Serial3;
128+
#define HAVE_HWSERIAL3
128129
#endif
129130

130131
extern void serialEventRun(void) __attribute__((weak));

Diff for: hardware/arduino/avr/cores/arduino/USBAPI.h

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ class Serial_ : public Stream
5555
};
5656
extern Serial_ Serial;
5757

58+
#define HAVE_CDCSERIAL
59+
5860
//================================================================================
5961
//================================================================================
6062
// Mouse

0 commit comments

Comments
 (0)