diff options
author | Cristian Maglie <c.maglie@bug.st> | 2012-10-18 15:50:09 +0200 |
---|---|---|
committer | Cristian Maglie <c.maglie@bug.st> | 2012-10-18 15:50:09 +0200 |
commit | 6a45ba48ab1f2d0a168373a02ba7fded40a3470e (patch) | |
tree | 163448869b4dfcbce95dc877ffff61a709fba6b6 /cores | |
parent | c313b54c00635f1be14a1b09617dc9b8b562e589 (diff) | |
parent | 6d296e0faba5b9910084c307a3f93cb2653bf7f8 (diff) |
Merged upstream arduino branch
Diffstat (limited to 'cores')
-rwxr-xr-x | cores/arduino/Arduino.h | 2 | ||||
-rw-r--r-- | cores/arduino/HardwareSerial.cpp | 110 | ||||
-rw-r--r-- | cores/arduino/HardwareSerial.h | 52 | ||||
-rw-r--r-- | cores/arduino/USBCore.cpp | 2 | ||||
-rw-r--r-- | cores/arduino/wiring_analog.c | 2 | ||||
-rwxr-xr-x | cores/arduino/wiring_private.h | 2 |
6 files changed, 148 insertions, 22 deletions
diff --git a/cores/arduino/Arduino.h b/cores/arduino/Arduino.h index 830c995..b265825 100755 --- a/cores/arduino/Arduino.h +++ b/cores/arduino/Arduino.h @@ -46,7 +46,7 @@ extern "C"{ #define EXTERNAL 1 #define INTERNAL 2 #else -#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1284P__) +#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644P__) #define INTERNAL1V1 2 #define INTERNAL2V56 3 #else diff --git a/cores/arduino/HardwareSerial.cpp b/cores/arduino/HardwareSerial.cpp index f40ddee..2e64acb 100644 --- a/cores/arduino/HardwareSerial.cpp +++ b/cores/arduino/HardwareSerial.cpp @@ -18,6 +18,7 @@ Modified 23 November 2006 by David A. Mellis Modified 28 September 2010 by Mark Sproul + Modified 14 August 2012 by Alarus */ #include <stdlib.h> @@ -109,13 +110,22 @@ inline void store_char(unsigned char c, ring_buffer *buffer) #endif { #if defined(UDR0) - unsigned char c = UDR0; + if (bit_is_clear(UCSR0A, UPE0)) { + unsigned char c = UDR0; + store_char(c, &rx_buffer); + } else { + unsigned char c = UDR0; + }; #elif defined(UDR) - unsigned char c = UDR; + if (bit_is_clear(UCSRA, PE)) { + unsigned char c = UDR; + store_char(c, &rx_buffer); + } else { + unsigned char c = UDR; + }; #else #error UDR not defined #endif - store_char(c, &rx_buffer); } #endif #endif @@ -126,8 +136,12 @@ inline void store_char(unsigned char c, ring_buffer *buffer) #define serialEvent1_implemented SIGNAL(USART1_RX_vect) { - unsigned char c = UDR1; - store_char(c, &rx_buffer1); + if (bit_is_clear(UCSR1A, UPE1)) { + unsigned char c = UDR1; + store_char(c, &rx_buffer1); + } else { + unsigned char c = UDR1; + }; } #elif defined(SIG_USART1_RECV) #error SIG_USART1_RECV @@ -139,8 +153,12 @@ inline void store_char(unsigned char c, ring_buffer *buffer) #define serialEvent2_implemented SIGNAL(USART2_RX_vect) { - unsigned char c = UDR2; - store_char(c, &rx_buffer2); + if (bit_is_clear(UCSR2A, UPE2)) { + unsigned char c = UDR2; + store_char(c, &rx_buffer2); + } else { + unsigned char c = UDR2; + }; } #elif defined(SIG_USART2_RECV) #error SIG_USART2_RECV @@ -152,8 +170,12 @@ inline void store_char(unsigned char c, ring_buffer *buffer) #define serialEvent3_implemented SIGNAL(USART3_RX_vect) { - unsigned char c = UDR3; - store_char(c, &rx_buffer3); + if (bit_is_clear(UCSR3A, UPE3)) { + unsigned char c = UDR3; + store_char(c, &rx_buffer3); + } else { + unsigned char c = UDR3; + }; } #elif defined(SIG_USART3_RECV) #error SIG_USART3_RECV @@ -274,7 +296,7 @@ ISR(USART3_UDRE_vect) HardwareSerial::HardwareSerial(ring_buffer *rx_buffer, ring_buffer *tx_buffer, volatile uint8_t *ubrrh, volatile uint8_t *ubrrl, volatile uint8_t *ucsra, volatile uint8_t *ucsrb, - volatile uint8_t *udr, + volatile uint8_t *ucsrc, volatile uint8_t *udr, uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udrie, uint8_t u2x) { _rx_buffer = rx_buffer; @@ -283,6 +305,7 @@ HardwareSerial::HardwareSerial(ring_buffer *rx_buffer, ring_buffer *tx_buffer, _ubrrl = ubrrl; _ucsra = ucsra; _ucsrb = ucsrb; + _ucsrc = ucsrc; _udr = udr; _rxen = rxen; _txen = txen; @@ -327,6 +350,55 @@ try_again: *_ubrrh = baud_setting >> 8; *_ubrrl = baud_setting; + transmitting = false; + + sbi(*_ucsrb, _rxen); + sbi(*_ucsrb, _txen); + sbi(*_ucsrb, _rxcie); + cbi(*_ucsrb, _udrie); +} + +void HardwareSerial::begin(unsigned long baud, byte config) +{ + uint16_t baud_setting; + uint8_t current_config; + bool use_u2x = true; + +#if F_CPU == 16000000UL + // hardcoded exception for compatibility with the bootloader shipped + // with the Duemilanove and previous boards and the firmware on the 8U2 + // on the Uno and Mega 2560. + if (baud == 57600) { + use_u2x = false; + } +#endif + +try_again: + + if (use_u2x) { + *_ucsra = 1 << _u2x; + baud_setting = (F_CPU / 4 / baud - 1) / 2; + } else { + *_ucsra = 0; + baud_setting = (F_CPU / 8 / baud - 1) / 2; + } + + if ((baud_setting > 4095) && use_u2x) + { + use_u2x = false; + goto try_again; + } + + // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register) + *_ubrrh = baud_setting >> 8; + *_ubrrl = baud_setting; + + //set the data bits, parity, and stop bits +#if defined(__AVR_ATmega8__) + config |= 0x80; // select UCSRC register (shared with UBRRH) +#endif + *_ucsrc = config; + sbi(*_ucsrb, _rxen); sbi(*_ucsrb, _txen); sbi(*_ucsrb, _rxcie); @@ -376,8 +448,9 @@ int HardwareSerial::read(void) void HardwareSerial::flush() { - while (_tx_buffer->head != _tx_buffer->tail) - ; + // UDR is kept full while the buffer is not empty, so TXC triggers when EMPTY && SENT + while (transmitting && ! (*_ucsra & _BV(TXC0))); + transmitting = false; } size_t HardwareSerial::write(uint8_t c) @@ -394,6 +467,9 @@ size_t HardwareSerial::write(uint8_t c) _tx_buffer->head = i; sbi(*_ucsrb, _udrie); + // clear the TXC bit -- "can be cleared by writing a one to its bit location" + transmitting = true; + sbi(*_ucsra, TXC0); return 1; } @@ -405,9 +481,9 @@ HardwareSerial::operator bool() { // Preinstantiate Objects ////////////////////////////////////////////////////// #if defined(UBRRH) && defined(UBRRL) - HardwareSerial Serial(&rx_buffer, &tx_buffer, &UBRRH, &UBRRL, &UCSRA, &UCSRB, &UDR, RXEN, TXEN, RXCIE, UDRIE, U2X); + HardwareSerial Serial(&rx_buffer, &tx_buffer, &UBRRH, &UBRRL, &UCSRA, &UCSRB, &UCSRC, &UDR, RXEN, TXEN, RXCIE, UDRIE, U2X); #elif defined(UBRR0H) && defined(UBRR0L) - HardwareSerial Serial(&rx_buffer, &tx_buffer, &UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UDR0, RXEN0, TXEN0, RXCIE0, UDRIE0, U2X0); + HardwareSerial Serial(&rx_buffer, &tx_buffer, &UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UCSR0C, &UDR0, RXEN0, TXEN0, RXCIE0, UDRIE0, U2X0); #elif defined(USBCON) // do nothing - Serial object and buffers are initialized in CDC code #else @@ -415,13 +491,13 @@ HardwareSerial::operator bool() { #endif #if defined(UBRR1H) - HardwareSerial Serial1(&rx_buffer1, &tx_buffer1, &UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UDR1, RXEN1, TXEN1, RXCIE1, UDRIE1, U2X1); + HardwareSerial Serial1(&rx_buffer1, &tx_buffer1, &UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UCSR1C, &UDR1, RXEN1, TXEN1, RXCIE1, UDRIE1, U2X1); #endif #if defined(UBRR2H) - HardwareSerial Serial2(&rx_buffer2, &tx_buffer2, &UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UDR2, RXEN2, TXEN2, RXCIE2, UDRIE2, U2X2); + HardwareSerial Serial2(&rx_buffer2, &tx_buffer2, &UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UCSR2C, &UDR2, RXEN2, TXEN2, RXCIE2, UDRIE2, U2X2); #endif #if defined(UBRR3H) - HardwareSerial Serial3(&rx_buffer3, &tx_buffer3, &UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UDR3, RXEN3, TXEN3, RXCIE3, UDRIE3, U2X3); + HardwareSerial Serial3(&rx_buffer3, &tx_buffer3, &UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UCSR3C, &UDR3, RXEN3, TXEN3, RXCIE3, UDRIE3, U2X3); #endif #endif // whole file diff --git a/cores/arduino/HardwareSerial.h b/cores/arduino/HardwareSerial.h index bf4924c..c2d0ce9 100644 --- a/cores/arduino/HardwareSerial.h +++ b/cores/arduino/HardwareSerial.h @@ -17,6 +17,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Modified 28 September 2010 by Mark Sproul + Modified 14 August 2012 by Alarus */ #ifndef HardwareSerial_h @@ -37,29 +38,62 @@ class HardwareSerial : public Stream volatile uint8_t *_ubrrl; volatile uint8_t *_ucsra; volatile uint8_t *_ucsrb; + volatile uint8_t *_ucsrc; volatile uint8_t *_udr; uint8_t _rxen; uint8_t _txen; uint8_t _rxcie; uint8_t _udrie; uint8_t _u2x; + bool transmitting; public: HardwareSerial(ring_buffer *rx_buffer, ring_buffer *tx_buffer, volatile uint8_t *ubrrh, volatile uint8_t *ubrrl, volatile uint8_t *ucsra, volatile uint8_t *ucsrb, - volatile uint8_t *udr, + volatile uint8_t *ucsrc, volatile uint8_t *udr, uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udrie, uint8_t u2x); void begin(unsigned long); + void begin(unsigned long, byte); void end(); virtual int available(void); virtual int peek(void); virtual int read(void); virtual void flush(void); virtual size_t write(uint8_t); + inline size_t write(unsigned long n) { return write((uint8_t)n); } + inline size_t write(long n) { return write((uint8_t)n); } + inline size_t write(unsigned int n) { return write((uint8_t)n); } + inline size_t write(int n) { return write((uint8_t)n); } using Print::write; // pull in write(str) and write(buf, size) from Print operator bool(); }; +// Define config for Serial.begin(baud, config); +#define SERIAL_5N1 0x00 +#define SERIAL_6N1 0x02 +#define SERIAL_7N1 0x04 +#define SERIAL_8N1 0x06 +#define SERIAL_5N2 0x08 +#define SERIAL_6N2 0x0A +#define SERIAL_7N2 0x0C +#define SERIAL_8N2 0x0E +#define SERIAL_5E1 0x20 +#define SERIAL_6E1 0x22 +#define SERIAL_7E1 0x24 +#define SERIAL_8E1 0x26 +#define SERIAL_5E2 0x28 +#define SERIAL_6E2 0x2A +#define SERIAL_7E2 0x2C +#define SERIAL_8E2 0x2E +#define SERIAL_5O1 0x30 +#define SERIAL_6O1 0x32 +#define SERIAL_7O1 0x34 +#define SERIAL_8O1 0x36 +#define SERIAL_5O2 0x38 +#define SERIAL_6O2 0x3A +#define SERIAL_7O2 0x3C +#define SERIAL_8O2 0x3E + #if defined(UBRRH) || defined(UBRR0H) extern HardwareSerial Serial; #elif defined(USBCON) @@ -76,6 +110,22 @@ class HardwareSerial : public Stream extern HardwareSerial Serial3; #endif +/* + * on ATmega8, the uart and its bits are not numbered, so there is no "TXC0" + * definition. It is slightly cleaner to define this here instead of having + * conditional code in the cpp module. + */ +#if !defined(TXC0) +#if defined(TXC) +#define TXC0 TXC +#elif defined(TXC1) +// Some devices have uart1 but no uart0 +#define TXC0 TXC1 +#else +#error TXC0 not definable in HardwareSerial.h +#endif +#endif + extern void serialEventRun(void) __attribute__((weak)); #endif diff --git a/cores/arduino/USBCore.cpp b/cores/arduino/USBCore.cpp index 6766be6..8bcf66c 100644 --- a/cores/arduino/USBCore.cpp +++ b/cores/arduino/USBCore.cpp @@ -603,7 +603,7 @@ ISR(USB_GEN_vect) { #ifdef CDC_ENABLED USB_Flush(CDC_TX); // Send a tx frame if found - while (USB_Available(CDC_RX)) // Handle received bytes (if any) + if (USB_Available(CDC_RX)) // Handle received bytes (if any) Serial.accept(); #endif diff --git a/cores/arduino/wiring_analog.c b/cores/arduino/wiring_analog.c index 0e9881f..23b01c6 100644 --- a/cores/arduino/wiring_analog.c +++ b/cores/arduino/wiring_analog.c @@ -45,7 +45,7 @@ int analogRead(uint8_t pin) if (pin >= 54) pin -= 54; // allow for channel or pin numbers #elif defined(__AVR_ATmega32U4__) if (pin >= 18) pin -= 18; // allow for channel or pin numbers -#elif defined(__AVR_ATmega1284__) +#elif defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644P__) if (pin >= 24) pin -= 24; // allow for channel or pin numbers #else if (pin >= 14) pin -= 14; // allow for channel or pin numbers diff --git a/cores/arduino/wiring_private.h b/cores/arduino/wiring_private.h index 026ce1a..f678265 100755 --- a/cores/arduino/wiring_private.h +++ b/cores/arduino/wiring_private.h @@ -54,7 +54,7 @@ extern "C"{ #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) #define EXTERNAL_NUM_INTERRUPTS 8 -#elif defined(__AVR_ATmega1284P__) +#elif defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644P__) #define EXTERNAL_NUM_INTERRUPTS 3 #elif defined(__AVR_ATmega32U4__) #define EXTERNAL_NUM_INTERRUPTS 4 |