AVRTools
A Library for the AVR ATmega328 and ATmega2560 Microcontrollers
USART0.h
Go to the documentation of this file.
1 /*
2  USART0.h - Functions and classes to use USART0 on AVR systems for
3  serial I/O (includes buffering).
4  For AVR ATMega328p (Arduino Uno) and ATMega2560 (Arduino Mega).
5  This is part of the AVRTools library.
6  Copyright (c) 2014 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 
67 #ifndef USART0_h
68 #define USART0_h
69 
70 #include "Writer.h"
71 #include "Reader.h"
72 
73 #include <stdint.h>
74 #include <stddef.h>
75 
76 
77 #ifndef USART_SERIAL_CONFIG
78 #define USART_SERIAL_CONFIG
79 
91 {
92  kSerial_5N1 = 0x00,
93  kSerial_6N1 = 0x02,
94  kSerial_7N1 = 0x04,
95  kSerial_8N1 = 0x06,
96  kSerial_5N2 = 0x08,
97  kSerial_6N2 = 0x0A,
98  kSerial_7N2 = 0x0C,
99  kSerial_8N2 = 0x0E,
100  kSerial_5E1 = 0x20,
101  kSerial_6E1 = 0x22,
102  kSerial_7E1 = 0x24,
103  kSerial_8E1 = 0x26,
104  kSerial_5E2 = 0x28,
105  kSerial_6E2 = 0x2A,
106  kSerial_7E2 = 0x2C,
107  kSerial_8E2 = 0x2E,
108  kSerial_5O1 = 0x30,
109  kSerial_6O1 = 0x32,
110  kSerial_7O1 = 0x34,
111  kSerial_8O1 = 0x36,
112  kSerial_5O2 = 0x38,
113  kSerial_6O2 = 0x3A,
114  kSerial_7O2 = 0x3C,
115  kSerial_8O2 = 0x3E
116 };
117 
118 #endif
119 
120 
121 
122 
128 namespace USART0
129 {
130 
144  void start( unsigned long baudRate, UsartSerialConfiguration config = kSerial_8N1 );
145 
146 
157  void stop();
158 
159 
160 
175  size_t write( char c );
176 
177 
192  size_t write( const char* c );
193 
194 
211  size_t write( const char* c, size_t n );
212 
213 
230  size_t write( const uint8_t* c, size_t n );
231 
232 
241  void flush();
242 
243 
251  int peek();
252 
253 
261  int read();
262 
263 
271  bool available();
272 };
273 
274 
275 
276 
277 
278 
279 
280 
295 class Serial0 : public Writer, public Reader
296 {
297 public:
298 
311  void start( unsigned long baudRate, UsartSerialConfiguration config = kSerial_8N1 )
312  { USART0::start( baudRate, config ); }
313 
314 
324  void stop()
325  { USART0::stop(); }
326 
327 
328 
329 
330 
339  virtual size_t write( char c );
340 
349  virtual size_t write( const char* str );
350 
360  virtual size_t write( const char* buffer, size_t size );
361 
371  virtual size_t write( const uint8_t* buffer, size_t size );
372 
378  virtual void flush();
379 
380 
381 
382  // Virtual functions from Reader
383 
391  virtual int read();
392 
393 
401  virtual int peek();
402 
403 
410  virtual bool available();
411 };
412 
413 
414 
415 #endif
416 
This is an abstract class defining a generic interface to read numbers and strings from a sequential ...
Definition: Reader.h:65
6 data bits, odd parity, 2 stop bits
Definition: USART0.h:113
int read()
Return the next character in the receive buffer, removing it from the buffer.
Definition: Reader.cpp:47
void stop()
Stops buffered serial communications using interrupts on USART0.
void stop()
Stops buffered serial communications using Serial0 on USART0 by deconfiguring the hardware and turnin...
Definition: USART0.h:324
void flush()
Flush transmit buffer.
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
6 data bits, odd parity, 1 stop bit
Definition: USART0.h:109
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, no parity, 1 stop bit
Definition: USART0.h:95
8 data bits, even parity, 1 stop bit
Definition: USART0.h:103
5 data bits, no parity, 2 stop bits
Definition: USART0.h:96
8 data bits, even parity, 2 stop bits
Definition: USART0.h:107
6 data bits, even parity, 1 stop bit
Definition: USART0.h:101
void start(unsigned long baudRate, UsartSerialConfiguration config=kSerial_8N1)
Initialize USART0 for buffered, asynchronous serial communications using interrupts.
7 data bits, odd parity, 1 stop bit
Definition: USART0.h:110
7 data bits, odd parity, 2 stop bits
Definition: USART0.h:114
This namespace bundles a high-level buffered interface to the USART0 hardware. It provides logical co...
Definition: USART0.h:128
7 data bits, even parity, 2 stop bits
Definition: USART0.h:106
5 data bits, even parity, 2 stop bits
Definition: USART0.h:104
5 data bits, odd parity, 2 stop bits
Definition: USART0.h:112
8 data bits, odd parity, 1 stop bit
Definition: USART0.h:111
7 data bits, no parity, 2 stop bits
Definition: USART0.h:98
Provides a high-end interface to serial communications using USART0.
Definition: USART0.h:295
This file provides a generic interface to outgoing data streams of any kind. It is designed around ho...
5 data bits, odd parity, 1 stop bit
Definition: USART0.h:108
int peek()
Examine the next character in the receive buffer without removing it from the buffer.
Definition: Reader.cpp:54
5 data bits, no parity, 1 stop bit
Definition: USART0.h:92
bool available()
Determine if there is data in the receive buffer..
Definition: Reader.cpp:60
size_t write(char c)
Write a single byte to the transmit buffer.
void start(unsigned long baudRate, UsartSerialConfiguration config=kSerial_8N1)
Configure the hardware for two-way serial communications, including turning on associated interrupts...
Definition: USART0.h:311
7 data bits, no parity, 1 stop bit
Definition: USART0.h:94
6 data bits, no parity, 1 stop bit
Definition: USART0.h:93
5 data bits, even parity, 1 stop bit
Definition: USART0.h:100
6 data bits, even parity, 2 stop bits
Definition: USART0.h:105
7 data bits, even parity, 1 stop bit
Definition: USART0.h:102
6 data bits, no parity, 2 stop bits
Definition: USART0.h:97
8 data bits, no parity, 2 stop bits
Definition: USART0.h:99
8 data bits, odd parity, 2 stop bits
Definition: USART0.h:115