HardwareSerial.h
Go to the documentation of this file.
1 /*
2  HardwareSerial.h - Hardware serial library for Wiring
3  Copyright (c) 2006 Nicholas Zambetti. All right reserved.
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 
19  Modified 28 September 2010 by Mark Sproul
20  Modified 14 August 2012 by Alarus
21  Modified 3 December 2013 by Matthijs Kooijman
22 */
23 
24 #ifndef HardwareSerial_h
25 #define HardwareSerial_h
26 
27 #include <inttypes.h>
28 
29 #include "Stream.h"
30 
31 // Define constants and variables for buffering incoming serial data. We're
32 // using a ring buffer (I think), in which head is the index of the location
33 // to which to write the next incoming character and tail is the index of the
34 // location from which to read.
35 // NOTE: a "power of 2" buffer size is reccomended to dramatically
36 // optimize all the modulo operations for ring buffers.
37 // WARNING: When buffer sizes are increased to > 256, the buffer index
38 // variables are automatically increased in size, but the extra
39 // atomicity guards needed for that are not implemented. This will
40 // often work, but occasionally a race condition can occur that makes
41 // Serial behave erratically. See https://github.com/arduino/Arduino/issues/2405
42 #if !defined(SERIAL_TX_BUFFER_SIZE)
43 #if ((RAMEND - RAMSTART) < 1023)
44 #define SERIAL_TX_BUFFER_SIZE 16
45 #else
46 #define SERIAL_TX_BUFFER_SIZE 64
47 #endif
48 #endif
49 #if !defined(SERIAL_RX_BUFFER_SIZE)
50 #if ((RAMEND - RAMSTART) < 1023)
51 #define SERIAL_RX_BUFFER_SIZE 16
52 #else
53 #define SERIAL_RX_BUFFER_SIZE 64
54 #endif
55 #endif
56 #if (SERIAL_TX_BUFFER_SIZE>256)
57 typedef uint16_t tx_buffer_index_t;
58 #else
59 typedef uint8_t tx_buffer_index_t;
60 #endif
61 #if (SERIAL_RX_BUFFER_SIZE>256)
62 typedef uint16_t rx_buffer_index_t;
63 #else
64 typedef uint8_t rx_buffer_index_t;
65 #endif
66 
67 // Define config for Serial.begin(baud, config);
68 #define SERIAL_5N1 0x00
69 #define SERIAL_6N1 0x02
70 #define SERIAL_7N1 0x04
71 #define SERIAL_8N1 0x06
72 #define SERIAL_5N2 0x08
73 #define SERIAL_6N2 0x0A
74 #define SERIAL_7N2 0x0C
75 #define SERIAL_8N2 0x0E
76 #define SERIAL_5E1 0x20
77 #define SERIAL_6E1 0x22
78 #define SERIAL_7E1 0x24
79 #define SERIAL_8E1 0x26
80 #define SERIAL_5E2 0x28
81 #define SERIAL_6E2 0x2A
82 #define SERIAL_7E2 0x2C
83 #define SERIAL_8E2 0x2E
84 #define SERIAL_5O1 0x30
85 #define SERIAL_6O1 0x32
86 #define SERIAL_7O1 0x34
87 #define SERIAL_8O1 0x36
88 #define SERIAL_5O2 0x38
89 #define SERIAL_6O2 0x3A
90 #define SERIAL_7O2 0x3C
91 #define SERIAL_8O2 0x3E
92 
93 class HardwareSerial : public Stream
94 {
95  protected:
96  volatile uint8_t * const _ubrrh;
97  volatile uint8_t * const _ubrrl;
98  volatile uint8_t * const _ucsra;
99  volatile uint8_t * const _ucsrb;
100  volatile uint8_t * const _ucsrc;
101  volatile uint8_t * const _udr;
102  // Has any byte been written to the UART since begin()
103  bool _written;
104 
109 
110  // Don't put any members after these buffers, since only the first
111  // 32 bytes of this struct can be accessed quickly using the ldd
112  // instruction.
115 
116  public:
117  inline HardwareSerial(
118  volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
119  volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
120  volatile uint8_t *ucsrc, volatile uint8_t *udr);
121  void begin(unsigned long baud) { begin(baud, SERIAL_8N1); }
122  void begin(unsigned long, uint8_t);
123  void end();
124  virtual int available(void);
125  virtual int peek(void);
126  virtual int read(void);
127  virtual int availableForWrite(void);
128  virtual void flush(void);
129  virtual size_t write(uint8_t);
130  inline size_t write(unsigned long n) { return write((uint8_t)n); }
131  inline size_t write(long n) { return write((uint8_t)n); }
132  inline size_t write(unsigned int n) { return write((uint8_t)n); }
133  inline size_t write(int n) { return write((uint8_t)n); }
134  using Print::write; // pull in write(str) and write(buf, size) from Print
135  operator bool() { return true; }
136 
137  // Interrupt handlers - Not intended to be called externally
138  inline void _rx_complete_irq(void);
139  void _tx_udr_empty_irq(void);
140 };
141 
142 #if defined(UBRRH) || defined(UBRR0H)
143  extern HardwareSerial Serial;
144  #define HAVE_HWSERIAL0
145 #endif
146 #if defined(UBRR1H)
147  extern HardwareSerial Serial1;
148  #define HAVE_HWSERIAL1
149 #endif
150 #if defined(UBRR2H)
151  extern HardwareSerial Serial2;
152  #define HAVE_HWSERIAL2
153 #endif
154 #if defined(UBRR3H)
155  extern HardwareSerial Serial3;
156  #define HAVE_HWSERIAL3
157 #endif
158 
159 extern void serialEventRun(void) __attribute__((weak));
160 
161 #endif
volatile uint8_t *const _ucsrb
virtual size_t write(uint8_t)
#define SERIAL_8N1
virtual size_t write(uint8_t)=0
volatile rx_buffer_index_t _rx_buffer_head
virtual int read(void)
volatile uint8_t *const _ubrrh
GLsizei n
volatile tx_buffer_index_t _tx_buffer_head
volatile uint8_t *const _ucsrc
uint8_t rx_buffer_index_t
virtual int availableForWrite(void)
virtual void flush(void)
HardwareSerial(volatile uint8_t *ubrrh, volatile uint8_t *ubrrl, volatile uint8_t *ucsra, volatile uint8_t *ucsrb, volatile uint8_t *ucsrc, volatile uint8_t *udr)
#define SERIAL_TX_BUFFER_SIZE
virtual int available(void)
volatile rx_buffer_index_t _rx_buffer_tail
void _rx_complete_irq(void)
rp::arch::net::raw_serial __attribute__
size_t write(unsigned long n)
#define SERIAL_RX_BUFFER_SIZE
size_t write(int n)
void serialEventRun(void) __attribute__((weak))
size_t write(long n)
volatile tx_buffer_index_t _tx_buffer_tail
uint8_t tx_buffer_index_t
unsigned char _rx_buffer[SERIAL_RX_BUFFER_SIZE]
volatile uint8_t *const _udr
volatile uint8_t *const _ubrrl
unsigned char _tx_buffer[SERIAL_TX_BUFFER_SIZE]
size_t write(unsigned int n)
void _tx_udr_empty_irq(void)
volatile uint8_t *const _ucsra
void begin(unsigned long baud)
virtual int peek(void)
Definition: Stream.h:49


arduino_daq
Author(s):
autogenerated on Mon Jun 10 2019 12:46:03