sketchbook
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 #include "Print.h" // picorv32: work-a-round; usually included in Stream.h
31 
32 // Define constants and variables for buffering incoming serial data. We're
33 // using a ring buffer (I think), in which head is the index of the location
34 // to which to write the next incoming character and tail is the index of the
35 // location from which to read.
36 // NOTE: a "power of 2" buffer size is reccomended to dramatically
37 // optimize all the modulo operations for ring buffers.
38 // WARNING: When buffer sizes are increased to > 256, the buffer index
39 // variables are automatically increased in size, but the extra
40 // atomicity guards needed for that are not implemented. This will
41 // often work, but occasionally a race condition can occur that makes
42 // Serial behave erratically. See https://github.com/arduino/Arduino/issues/2405
43 /*#if !defined(SERIAL_TX_BUFFER_SIZE)
44 #if ((RAMEND - RAMSTART) < 1023)
45 #define SERIAL_TX_BUFFER_SIZE 16
46 #else
47 #define SERIAL_TX_BUFFER_SIZE 64
48 #endif
49 #endif*/
50 #if !defined(SERIAL_RX_BUFFER_SIZE)
51 #if ((RAMEND - RAMSTART) < 1023)
52 #define SERIAL_RX_BUFFER_SIZE 16
53 #else
54 #define SERIAL_RX_BUFFER_SIZE 64
55 #endif
56 #endif
57 /*#if (SERIAL_TX_BUFFER_SIZE>256)
58 typedef uint16_t tx_buffer_index_t;
59 #else
60 typedef uint8_t tx_buffer_index_t;
61 #endif*/
62 #if (SERIAL_RX_BUFFER_SIZE>256)
63 typedef uint16_t rx_buffer_index_t;
64 #else
65 typedef uint8_t rx_buffer_index_t;
66 #endif
67 
68 /*// Define config for Serial.begin(baud, config);
69 #define SERIAL_5N1 0x00
70 #define SERIAL_6N1 0x02
71 #define SERIAL_7N1 0x04*/
72 #define SERIAL_8N1 0x06
73 /*#define SERIAL_5N2 0x08
74 #define SERIAL_6N2 0x0A
75 #define SERIAL_7N2 0x0C
76 #define SERIAL_8N2 0x0E
77 #define SERIAL_5E1 0x20
78 #define SERIAL_6E1 0x22
79 #define SERIAL_7E1 0x24
80 #define SERIAL_8E1 0x26
81 #define SERIAL_5E2 0x28
82 #define SERIAL_6E2 0x2A
83 #define SERIAL_7E2 0x2C
84 #define SERIAL_8E2 0x2E
85 #define SERIAL_5O1 0x30
86 #define SERIAL_6O1 0x32
87 #define SERIAL_7O1 0x34
88 #define SERIAL_8O1 0x36
89 #define SERIAL_5O2 0x38
90 #define SERIAL_6O2 0x3A
91 #define SERIAL_7O2 0x3C
92 #define SERIAL_8O2 0x3E
93 
94 class HardwareSerial : public Stream*/
95 class HardwareSerial : public Print // work-a-round; as class Stream : public Print
96 {
97  protected:
98  /*volatile uint8_t * const _ubrrh;
99  volatile uint8_t * const _ubrrl;
100  volatile uint8_t * const _ucsra;
101  volatile uint8_t * const _ucsrb;
102  volatile uint8_t * const _ucsrc;
103  volatile uint8_t * const _udr;*/
104  // Has any byte been written to the UART since begin()
105  bool _written;
106 
109 // volatile tx_buffer_index_t _tx_buffer_head;
110 // volatile tx_buffer_index_t _tx_buffer_tail;
111 
112  // Don't put any members after these buffers, since only the first
113  // 32 bytes of this struct can be accessed quickly using the ldd
114  // instruction.
116 // unsigned char _tx_buffer[SERIAL_TX_BUFFER_SIZE];
117 
118  public:
119  inline HardwareSerial(
120  volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
121  volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
122  volatile uint8_t *ucsrc, volatile uint8_t *udr);
123  void begin(unsigned long baud) { begin(baud, SERIAL_8N1); }
124  void begin(unsigned long, uint8_t);
125  void end();
126  virtual int available(void);
127  virtual int peek(void);
128  virtual int read(void);
129  virtual int availableForWrite(void);
130  virtual void flush(void);
131 // virtual size_t write(uint8_t);
132  size_t write(uint8_t);
133  inline size_t write(unsigned long n) { return write((uint8_t)n); }
134  inline size_t write(long n) { return write((uint8_t)n); }
135  inline size_t write(unsigned int n) { return write((uint8_t)n); }
136  inline size_t write(int n) { return write((uint8_t)n); }
137  using Print::write; // pull in write(str) and write(buf, size) from Print
138  operator bool() { return true; }
139 
140  // Interrupt handlers - Not intended to be called externally
141  inline void _rx_complete_irq(void);
142  /*void _tx_udr_empty_irq(void);*/
143 };
144 
145 //#if defined(UBRRH) || defined(UBRR0H)
146  extern HardwareSerial Serial;
147  #define HAVE_HWSERIAL0
148 //#endif
149 /*#if defined(UBRR1H)
150  extern HardwareSerial Serial1;
151  #define HAVE_HWSERIAL1
152 #endif
153 #if defined(UBRR2H)
154  extern HardwareSerial Serial2;
155  #define HAVE_HWSERIAL2
156 #endif
157 #if defined(UBRR3H)
158  extern HardwareSerial Serial3;
159  #define HAVE_HWSERIAL3
160 #endif
161 
162 extern void serialEventRun(void) __attribute__((weak));*/
163 
164 #endif
bool _written
Definition: HardwareSerial.h:105
#define SERIAL_8N1
Definition: HardwareSerial.h:72
virtual int available(void)
Definition: HardwareSerial.cpp:167
size_t write(uint8_t)
Definition: HardwareSerial.cpp:230
volatile rx_buffer_index_t _rx_buffer_head
Definition: HardwareSerial.h:107
void end()
Definition: HardwareSerial.cpp:153
HardwareSerial Serial
Definition: Print.h:38
size_t write(uint8_t buffer)
Definition: Print.h:53
virtual int peek(void)
Definition: HardwareSerial.cpp:172
uint8_t rx_buffer_index_t
Definition: HardwareSerial.h:65
#define bool
Definition: stdbool.h:31
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)
volatile rx_buffer_index_t _rx_buffer_tail
Definition: HardwareSerial.h:108
void _rx_complete_irq(void)
size_t write(unsigned long n)
Definition: HardwareSerial.h:133
virtual int read(void)
Definition: HardwareSerial.cpp:183
#define SERIAL_RX_BUFFER_SIZE
Definition: HardwareSerial.h:52
virtual int availableForWrite(void)
Definition: HardwareSerial.cpp:195
Definition: HardwareSerial.h:95
size_t write(int n)
Definition: HardwareSerial.h:136
size_t write(long n)
Definition: HardwareSerial.h:134
unsigned char _rx_buffer[SERIAL_RX_BUFFER_SIZE]
Definition: HardwareSerial.h:115
float n
Definition: OWGeneric_SensorStation.ino:147
size_t write(unsigned int n)
Definition: HardwareSerial.h:135
void begin(unsigned long baud)
Definition: HardwareSerial.h:123
virtual void flush(void)
Definition: HardwareSerial.cpp:210