Skip to content

Commit

Permalink
Centrally decide which hardware UARTS are available
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
matthijskooijman authored and cmaglie committed Jan 22, 2014
1 parent 0be4e8c commit 8e43c1a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 43 deletions.
4 changes: 4 additions & 0 deletions hardware/arduino/avr/cores/arduino/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ extern const uint8_t PROGMEM digital_pin_to_timer_PGM[];
#include "WCharacter.h"
#include "WString.h"
#include "HardwareSerial.h"
#include "USBAPI.h"
#if defined(HAVE_HWSERIAL0) && defined(HAVE_CDCSERIAL)
#error "Targets with both UART0 and CDC serial not supported"
#endif

uint16_t makeWord(uint16_t w);
uint16_t makeWord(byte h, byte l);
Expand Down
65 changes: 25 additions & 40 deletions hardware/arduino/avr/cores/arduino/HardwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
#include "Arduino.h"
#include "wiring_private.h"

#include "HardwareSerial.h"

// this next line disables the entire HardwareSerial.cpp,
// this is so I can support Attiny series and any other chip without a uart
#if defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H)

#include "HardwareSerial.h"
#if defined(HAVE_HWSERIAL0) || defined(HAVE_HWSERIAL1) || defined(HAVE_HWSERIAL2) || defined(HAVE_HWSERIAL3)

// Ensure that the various bit positions we use are available with a 0
// postfix, so we can always use the values for UART0 for all UARTs. The
Expand Down Expand Up @@ -84,53 +84,44 @@
#error "Not all bit positions for UART3 are the same as for UART0"
#endif

#if !defined(USART0_RX_vect) && defined(USART1_RX_vect)
// do nothing - on the 32u4 the first USART is USART1
#else
#if !defined(USART_RX_vect) && !defined(USART0_RX_vect) && \
!defined(USART_RXC_vect)
#error "Don't know what the Data Received vector is called for the first UART"
#else
#if defined(HAVE_HWSERIAL0)
void serialEvent() __attribute__((weak));
void serialEvent() {}
#define serialEvent_implemented
#if defined(USART_RX_vect)
ISR(USART_RX_vect)
#elif defined(USART0_RX_vect)
ISR(USART0_RX_vect)
#elif defined(USART_RXC_vect)
ISR(USART_RXC_vect) // ATmega8
#else
#error "Don't know what the Data Received vector is called for the first UART"
#endif
{
Serial._rx_complete_irq();
}
#endif
#endif

#if defined(USART1_RX_vect)
#if defined(HAVE_HWSERIAL1)
void serialEvent1() __attribute__((weak));
void serialEvent1() {}
#define serialEvent1_implemented
ISR(USART1_RX_vect)
{
Serial1._rx_complete_irq();
}
#endif

#if defined(USART2_RX_vect) && defined(UDR2)
#if defined(HAVE_HWSERIAL2)
void serialEvent2() __attribute__((weak));
void serialEvent2() {}
#define serialEvent2_implemented
ISR(USART2_RX_vect)
{
Serial2._rx_complete_irq();
}
#endif

#if defined(USART3_RX_vect) && defined(UDR3)
#if defined(HAVE_HWSERIAL3)
void serialEvent3() __attribute__((weak));
void serialEvent3() {}
#define serialEvent3_implemented
ISR(USART3_RX_vect)
{
Serial3._rx_complete_irq();
Expand All @@ -139,27 +130,22 @@

void serialEventRun(void)
{
#ifdef serialEvent_implemented
#if defined(HAVE_HWSERIAL0)
if (Serial.available()) serialEvent();
#endif
#ifdef serialEvent1_implemented
#if defined(HAVE_HWSERIAL1)
if (Serial1.available()) serialEvent1();
#endif
#ifdef serialEvent2_implemented
#if defined(HAVE_HWSERIAL2)
if (Serial2.available()) serialEvent2();
#endif
#ifdef serialEvent3_implemented
#if defined(HAVE_HWSERIAL3)
if (Serial3.available()) serialEvent3();
#endif
}


#if !defined(USART0_UDRE_vect) && defined(USART1_UDRE_vect)
// do nothing - on the 32u4 the first USART is USART1
#else
#if !defined(UART0_UDRE_vect) && !defined(UART_UDRE_vect) && !defined(USART0_UDRE_vect) && !defined(USART_UDRE_vect)
#error "Don't know what the Data Register Empty vector is called for the first UART"
#else
#if defined(HAVE_HWSERIAL0)
#if defined(UART0_UDRE_vect)
ISR(UART0_UDRE_vect)
#elif defined(UART_UDRE_vect)
Expand All @@ -168,28 +154,29 @@ ISR(UART_UDRE_vect)
ISR(USART0_UDRE_vect)
#elif defined(USART_UDRE_vect)
ISR(USART_UDRE_vect)
#else
#error "Don't know what the Data Register Empty vector is called for the first UART"
#endif
{
Serial._tx_udr_empty_irq();
}
#endif
#endif

#ifdef USART1_UDRE_vect
#if defined(HAVE_HWSERIAL1)
ISR(USART1_UDRE_vect)
{
Serial1._tx_udr_empty_irq();
}
#endif

#ifdef USART2_UDRE_vect
#if defined(HAVE_HWSERIAL2)
ISR(USART2_UDRE_vect)
{
Serial2._tx_udr_empty_irq();
}
#endif

#ifdef USART3_UDRE_vect
#if defined(HAVE_HWSERIAL3)
ISR(USART3_UDRE_vect)
{
Serial3._tx_udr_empty_irq();
Expand Down Expand Up @@ -386,23 +373,21 @@ size_t HardwareSerial::write(uint8_t c)

// Preinstantiate Objects //////////////////////////////////////////////////////

#if defined(HAVE_HWSERIAL0)
#if defined(UBRRH) && defined(UBRRL)
HardwareSerial Serial(&UBRRH, &UBRRL, &UCSRA, &UCSRB, &UCSRC, &UDR);
#elif defined(UBRR0H) && defined(UBRR0L)
HardwareSerial Serial(&UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UCSR0C, &UDR0);
#elif defined(USBCON)
// do nothing - Serial object and buffers are initialized in CDC code
#else
#error no serial port defined (port 0)
HardwareSerial Serial(&UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UCSR0C, &UDR0);
#endif
#endif

#if defined(UBRR1H)
#if defined(HAVE_HWSERIAL1)
HardwareSerial Serial1(&UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UCSR1C, &UDR1);
#endif
#if defined(UBRR2H)
#if defined(HAVE_HWSERIAL2)
HardwareSerial Serial2(&UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UCSR2C, &UDR2);
#endif
#if defined(UBRR3H)
#if defined(HAVE_HWSERIAL3)
HardwareSerial Serial3(&UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UCSR3C, &UDR3);
#endif

Expand Down
7 changes: 4 additions & 3 deletions hardware/arduino/avr/cores/arduino/HardwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,19 @@ class HardwareSerial : public Stream

#if defined(UBRRH) || defined(UBRR0H)
extern HardwareSerial Serial;
#elif defined(USBCON)
#include "USBAPI.h"
// extern HardwareSerial Serial_;
#define HAVE_HWSERIAL0
#endif
#if defined(UBRR1H)
extern HardwareSerial Serial1;
#define HAVE_HWSERIAL1
#endif
#if defined(UBRR2H)
extern HardwareSerial Serial2;
#define HAVE_HWSERIAL2
#endif
#if defined(UBRR3H)
extern HardwareSerial Serial3;
#define HAVE_HWSERIAL3
#endif

extern void serialEventRun(void) __attribute__((weak));
Expand Down
2 changes: 2 additions & 0 deletions hardware/arduino/avr/cores/arduino/USBAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class Serial_ : public Stream
};
extern Serial_ Serial;

#define HAVE_CDCSERIAL

//================================================================================
//================================================================================
// Mouse
Expand Down

0 comments on commit 8e43c1a

Please sign in to comment.