From 99f7ef7c673e02d559eb55d064f38a105393c8f6 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Sun, 1 Dec 2013 17:21:54 +0100 Subject: 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). --- cores/arduino/Arduino.h | 4 +++ cores/arduino/HardwareSerial.cpp | 65 ++++++++++++++++------------------------ cores/arduino/HardwareSerial.h | 7 +++-- cores/arduino/USBAPI.h | 2 ++ 4 files changed, 35 insertions(+), 43 deletions(-) (limited to 'cores/arduino') diff --git a/cores/arduino/Arduino.h b/cores/arduino/Arduino.h index 8502e44..a189777 100644 --- a/cores/arduino/Arduino.h +++ b/cores/arduino/Arduino.h @@ -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); diff --git a/cores/arduino/HardwareSerial.cpp b/cores/arduino/HardwareSerial.cpp index 9a02f23..4318a1e 100644 --- a/cores/arduino/HardwareSerial.cpp +++ b/cores/arduino/HardwareSerial.cpp @@ -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 @@ -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(); @@ -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) @@ -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(); @@ -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 diff --git a/cores/arduino/HardwareSerial.h b/cores/arduino/HardwareSerial.h index 0ae51f4..da9efbc 100644 --- a/cores/arduino/HardwareSerial.h +++ b/cores/arduino/HardwareSerial.h @@ -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)); diff --git a/cores/arduino/USBAPI.h b/cores/arduino/USBAPI.h index da2e869..5a33002 100644 --- a/cores/arduino/USBAPI.h +++ b/cores/arduino/USBAPI.h @@ -55,6 +55,8 @@ public: }; extern Serial_ Serial; +#define HAVE_CDCSERIAL + //================================================================================ //================================================================================ // Mouse -- cgit v1.2.3-18-g5258