2009-06-01 01:32:11 -07:00
|
|
|
/*
|
|
|
|
HardwareSerial.h - Hardware serial library for Wiring
|
|
|
|
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2010-10-17 10:36:02 -07:00
|
|
|
|
|
|
|
Modified 28 September 2010 by Mark Sproul
|
2012-08-14 06:52:00 -07:00
|
|
|
Modified 14 August 2012 by Alarus
|
2009-06-01 01:32:11 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef HardwareSerial_h
|
|
|
|
#define HardwareSerial_h
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
2010-05-28 15:41:03 -07:00
|
|
|
#include "Stream.h"
|
2009-06-01 01:32:11 -07:00
|
|
|
|
2013-04-18 09:52:48 -07:00
|
|
|
// Define constants and variables for buffering incoming serial data. We're
|
|
|
|
// using a ring buffer (I think), in which head is the index of the location
|
|
|
|
// to which to write the next incoming character and tail is the index of the
|
|
|
|
// location from which to read.
|
|
|
|
#if (RAMEND < 1000)
|
|
|
|
#define SERIAL_BUFFER_SIZE 16
|
|
|
|
#else
|
|
|
|
#define SERIAL_BUFFER_SIZE 64
|
|
|
|
#endif
|
2009-06-01 01:32:11 -07:00
|
|
|
|
2010-05-28 15:41:03 -07:00
|
|
|
class HardwareSerial : public Stream
|
2009-06-01 01:32:11 -07:00
|
|
|
{
|
2013-04-18 09:46:14 -07:00
|
|
|
protected:
|
2009-06-01 01:32:11 -07:00
|
|
|
volatile uint8_t *_ubrrh;
|
|
|
|
volatile uint8_t *_ubrrl;
|
|
|
|
volatile uint8_t *_ucsra;
|
|
|
|
volatile uint8_t *_ucsrb;
|
2012-08-12 07:18:50 -07:00
|
|
|
volatile uint8_t *_ucsrc;
|
2009-06-01 01:32:11 -07:00
|
|
|
volatile uint8_t *_udr;
|
|
|
|
uint8_t _rxen;
|
|
|
|
uint8_t _txen;
|
|
|
|
uint8_t _rxcie;
|
2011-10-27 08:45:13 -07:00
|
|
|
uint8_t _udrie;
|
2009-07-11 18:58:15 -07:00
|
|
|
uint8_t _u2x;
|
2012-08-28 05:02:54 -07:00
|
|
|
bool transmitting;
|
2013-04-18 09:52:48 -07:00
|
|
|
|
2009-06-01 01:32:11 -07:00
|
|
|
public:
|
2013-04-18 09:52:48 -07:00
|
|
|
volatile uint8_t _rx_buffer_head;
|
|
|
|
volatile uint8_t _rx_buffer_tail;
|
|
|
|
volatile uint8_t _tx_buffer_head;
|
|
|
|
volatile uint8_t _tx_buffer_tail;
|
|
|
|
|
|
|
|
// Don't put any members after these buffers, since only the first
|
|
|
|
// 32 bytes of this struct can be accessed quickly using the ldd
|
|
|
|
// instruction.
|
|
|
|
unsigned char _rx_buffer[SERIAL_BUFFER_SIZE];
|
|
|
|
unsigned char _tx_buffer[SERIAL_BUFFER_SIZE];
|
|
|
|
|
|
|
|
HardwareSerial(
|
2009-06-01 01:32:11 -07:00
|
|
|
volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
|
|
|
|
volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
|
2012-08-12 07:18:50 -07:00
|
|
|
volatile uint8_t *ucsrc, volatile uint8_t *udr,
|
2011-10-27 08:45:13 -07:00
|
|
|
uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udrie, uint8_t u2x);
|
2011-02-25 15:36:46 -08:00
|
|
|
void begin(unsigned long);
|
2012-11-02 06:24:51 -07:00
|
|
|
void begin(unsigned long, uint8_t);
|
2009-12-22 16:00:17 -08:00
|
|
|
void end();
|
2010-08-02 15:23:48 -07:00
|
|
|
virtual int available(void);
|
2010-07-04 16:31:55 -07:00
|
|
|
virtual int peek(void);
|
2010-05-28 15:41:03 -07:00
|
|
|
virtual int read(void);
|
|
|
|
virtual void flush(void);
|
2011-10-27 08:45:13 -07:00
|
|
|
virtual size_t write(uint8_t);
|
2012-08-29 13:32:05 -07:00
|
|
|
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); }
|
2009-06-01 01:32:11 -07:00
|
|
|
using Print::write; // pull in write(str) and write(buf, size) from Print
|
2012-04-01 09:54:35 -07:00
|
|
|
operator bool();
|
2009-06-01 01:32:11 -07:00
|
|
|
};
|
|
|
|
|
2012-08-14 06:52:00 -07:00
|
|
|
// Define config for Serial.begin(baud, config);
|
2012-08-30 05:47:35 -07:00
|
|
|
#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
|
2012-08-14 06:52:00 -07:00
|
|
|
|
2010-10-17 10:36:02 -07:00
|
|
|
#if defined(UBRRH) || defined(UBRR0H)
|
|
|
|
extern HardwareSerial Serial;
|
|
|
|
#elif defined(USBCON)
|
2011-10-27 08:45:13 -07:00
|
|
|
#include "USBAPI.h"
|
|
|
|
// extern HardwareSerial Serial_;
|
2010-10-17 10:36:02 -07:00
|
|
|
#endif
|
|
|
|
#if defined(UBRR1H)
|
|
|
|
extern HardwareSerial Serial1;
|
|
|
|
#endif
|
|
|
|
#if defined(UBRR2H)
|
|
|
|
extern HardwareSerial Serial2;
|
|
|
|
#endif
|
|
|
|
#if defined(UBRR3H)
|
|
|
|
extern HardwareSerial Serial3;
|
2009-06-01 01:32:11 -07:00
|
|
|
#endif
|
|
|
|
|
2011-10-27 08:45:13 -07:00
|
|
|
extern void serialEventRun(void) __attribute__((weak));
|
|
|
|
|
2009-06-01 01:32:11 -07:00
|
|
|
#endif
|