AVRTools
A Library for the AVR ATmega328 and ATmega2560 Microcontrollers
USART2.h
Go to the documentation of this file.
1 /*
2  USART2.h - Functions and classes to use USART2 on AVR systems for
3  serial I/O (includes buffering).
4  For AVR ATMega2560 (Arduino Mega).
5  This is part of the AVRTools library.
6  Copyright (c) 2015 Igor Mikolic-Torreira. All right reserved.
7  Functions readlong() and readFloat() adapted from Arduino code that
8  is Copyright (c) 2005-2006 David A. Mellis and licensed under LGPL.
9 
10  This program is free software: you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23 
24 
25 #if !defined(__AVR_ATmega2560__)
26 #error "USART2 doesn't exist on ATMega328p (Arduino Uno); you can only use this on ATMega2560 (Arduino Mega)."
27 #endif
28 
29 
30 
74 #ifndef USART2_h
75 #define USART2_h
76 
77 #include "Writer.h"
78 #include "Reader.h"
79 
80 #include <stdint.h>
81 #include <stddef.h>
82 
83 
84 #ifndef USART_SERIAL_CONFIG
85 #define USART_SERIAL_CONFIG
86 
98 {
99  kSerial_5N1 = 0x00,
100  kSerial_6N1 = 0x02,
101  kSerial_7N1 = 0x04,
102  kSerial_8N1 = 0x06,
103  kSerial_5N2 = 0x08,
104  kSerial_6N2 = 0x0A,
105  kSerial_7N2 = 0x0C,
106  kSerial_8N2 = 0x0E,
107  kSerial_5E1 = 0x20,
108  kSerial_6E1 = 0x22,
109  kSerial_7E1 = 0x24,
110  kSerial_8E1 = 0x26,
111  kSerial_5E2 = 0x28,
112  kSerial_6E2 = 0x2A,
113  kSerial_7E2 = 0x2C,
114  kSerial_8E2 = 0x2E,
115  kSerial_5O1 = 0x30,
116  kSerial_6O1 = 0x32,
117  kSerial_7O1 = 0x34,
118  kSerial_8O1 = 0x36,
119  kSerial_5O2 = 0x38,
120  kSerial_6O2 = 0x3A,
121  kSerial_7O2 = 0x3C,
122  kSerial_8O2 = 0x3E
123 };
124 
125 #endif
126 
127 
128 
129 
135 namespace USART2
136 {
137 
151  void start( unsigned long baudRate, UsartSerialConfiguration config = kSerial_8N1 );
152 
153 
164  void stop();
165 
166 
167 
182  size_t write( char c );
183 
184 
199  size_t write( const char* c );
200 
201 
218  size_t write( const char* c, size_t n );
219 
220 
237  size_t write( const uint8_t* c, size_t n );
238 
239 
248  void flush();
249 
250 
258  int peek();
259 
260 
268  int read();
269 
270 
278  bool available();
279 };
280 
281 
282 
283 
284 
285 
286 
287 
302 class Serial2 : public Writer, public Reader
303 {
304 public:
305 
318  void start( unsigned long baudRate, UsartSerialConfiguration config = kSerial_8N1 )
319  { USART2::start( baudRate, config ); }
320 
321 
331  void stop()
332  { USART2::stop(); }
333 
334 
335 
336 
337 
346  virtual size_t write( char c );
347 
356  virtual size_t write( const char* str );
357 
367  virtual size_t write( const char* buffer, size_t size );
368 
378  virtual size_t write( const uint8_t* buffer, size_t size );
379 
385  virtual void flush();
386 
387 
388 
389  // Virtual functions from Reader
390 
398  virtual int read();
399 
400 
408  virtual int peek();
409 
410 
417  virtual bool available();
418 };
419 
420 
421 
422 #endif
423 
This is an abstract class defining a generic interface to read numbers and strings from a sequential ...
Definition: Reader.h:65
5 data bits, even parity, 1 stop bit
Definition: USART2.h:107
void start(unsigned long baudRate, UsartSerialConfiguration config=kSerial_8N1)
Configure the hardware for two-way serial communications, including turning on associated interrupts...
Definition: USART2.h:318
This file provides a generic interface to incoming data streams of any kind. It is designed around ho...
UsartSerialConfiguration
This enum lists serial configuration in terms of data bits, parity, and stop bits.
Definition: USART0.h:90
This is an abstract class defining a generic interface to write numbers and strings to a sequential s...
Definition: Writer.h:63
8 data bits, odd parity, 2 stop bits
Definition: USART2.h:122
7 data bits, even parity, 2 stop bits
Definition: USART2.h:113
size_t write(char c)
Write a single byte to the transmit buffer.
Definition: USART2.cpp:198
6 data bits, odd parity, 1 stop bit
Definition: USART2.h:116
7 data bits, no parity, 1 stop bit
Definition: USART2.h:101
8 data bits, no parity, 1 stop bit
Definition: USART2.h:102
6 data bits, no parity, 1 stop bit
Definition: USART2.h:100
6 data bits, even parity, 2 stop bits
Definition: USART2.h:112
8 data bits, even parity, 2 stop bits
Definition: USART2.h:114
5 data bits, odd parity, 1 stop bit
Definition: USART2.h:115
void stop()
Stops buffered serial communications using Serial2 on USART2 by deconfiguring the hardware and turnin...
Definition: USART2.h:331
void start(unsigned long baudRate, UsartSerialConfiguration config=kSerial_8N1)
Initialize USART2 for buffered, asynchronous serial communications using interrupts.
Definition: USART2.cpp:114
6 data bits, odd parity, 2 stop bits
Definition: USART2.h:120
7 data bits, even parity, 1 stop bit
Definition: USART2.h:109
int read()
Return the next character in the receive buffer, removing it from the buffer.
Definition: USART2.cpp:191
This file provides a generic interface to outgoing data streams of any kind. It is designed around ho...
5 data bits, odd parity, 2 stop bits
Definition: USART2.h:119
void flush()
Flush transmit buffer.
Definition: USART2.cpp:171
6 data bits, even parity, 1 stop bit
Definition: USART2.h:108
8 data bits, even parity, 1 stop bit
Definition: USART2.h:110
5 data bits, no parity, 2 stop bits
Definition: USART2.h:103
6 data bits, no parity, 2 stop bits
Definition: USART2.h:104
8 data bits, no parity, 2 stop bits
Definition: USART2.h:106
5 data bits, no parity, 1 stop bit
Definition: USART2.h:99
7 data bits, odd parity, 1 stop bit
Definition: USART2.h:117
7 data bits, odd parity, 2 stop bits
Definition: USART2.h:121
int peek()
Examine the next character in the receive buffer without removing it from the buffer.
Definition: USART2.cpp:184
This namespace bundles a high-level buffered interface to the USART2 hardware. It provides logical co...
Definition: USART2.h:135
8 data bits, odd parity, 1 stop bit
Definition: USART2.h:118
5 data bits, even parity, 2 stop bits
Definition: USART2.h:111
Provides a high-end interface to serial communications using USART2.
Definition: USART2.h:302
void stop()
Stops buffered serial communications using interrupts on USART2.
Definition: USART2.cpp:158
bool available()
Determine if there is data in the receive buffer..
Definition: USART2.cpp:280
7 data bits, no parity, 2 stop bits
Definition: USART2.h:105