AVRTools
A Library for the AVR ATmega328 and ATmega2560 Microcontrollers
Classes | Namespaces | Enumerations | Functions
USART0.h File Reference

This file provides functions that offer high-level interfaces to USART0 hardware, which is available on the Arduino Uno (ATmega328) and Arduino Mega (ATmega2560). More...

#include "Writer.h"
#include "Reader.h"
#include <stdint.h>
#include <stddef.h>
Include dependency graph for USART0.h:

Go to the source code of this file.

Classes

class  Serial0
 Provides a high-end interface to serial communications using USART0. More...
 

Namespaces

 USART0
 This namespace bundles a high-level buffered interface to the USART0 hardware. It provides logical cohesion and prevents namespace collisions.
 

Enumerations

enum  UsartSerialConfiguration {
  kSerial_5N1, kSerial_6N1, kSerial_7N1, kSerial_8N1,
  kSerial_5N2, kSerial_6N2, kSerial_7N2, kSerial_8N2,
  kSerial_5E1, kSerial_6E1, kSerial_7E1, kSerial_8E1,
  kSerial_5E2, kSerial_6E2, kSerial_7E2, kSerial_8E2,
  kSerial_5O1, kSerial_6O1, kSerial_7O1, kSerial_8O1,
  kSerial_5O2, kSerial_6O2, kSerial_7O2, kSerial_8O2,
  kSerial_5N1, kSerial_6N1, kSerial_7N1, kSerial_8N1,
  kSerial_5N2, kSerial_6N2, kSerial_7N2, kSerial_8N2,
  kSerial_5E1, kSerial_6E1, kSerial_7E1, kSerial_8E1,
  kSerial_5E2, kSerial_6E2, kSerial_7E2, kSerial_8E2,
  kSerial_5O1, kSerial_6O1, kSerial_7O1, kSerial_8O1,
  kSerial_5O2, kSerial_6O2, kSerial_7O2, kSerial_8O2,
  kSerial_5N1, kSerial_6N1, kSerial_7N1, kSerial_8N1,
  kSerial_5N2, kSerial_6N2, kSerial_7N2, kSerial_8N2,
  kSerial_5E1, kSerial_6E1, kSerial_7E1, kSerial_8E1,
  kSerial_5E2, kSerial_6E2, kSerial_7E2, kSerial_8E2,
  kSerial_5O1, kSerial_6O1, kSerial_7O1, kSerial_8O1,
  kSerial_5O2, kSerial_6O2, kSerial_7O2, kSerial_8O2,
  kSerial_5N1, kSerial_6N1, kSerial_7N1, kSerial_8N1,
  kSerial_5N2, kSerial_6N2, kSerial_7N2, kSerial_8N2,
  kSerial_5E1, kSerial_6E1, kSerial_7E1, kSerial_8E1,
  kSerial_5E2, kSerial_6E2, kSerial_7E2, kSerial_8E2,
  kSerial_5O1, kSerial_6O1, kSerial_7O1, kSerial_8O1,
  kSerial_5O2, kSerial_6O2, kSerial_7O2, kSerial_8O2
}
 This enum lists serial configuration in terms of data bits, parity, and stop bits. More...
 

Functions

void USART0::start (unsigned long baudRate, UsartSerialConfiguration config=kSerial_8N1)
 Initialize USART0 for buffered, asynchronous serial communications using interrupts. More...
 
void USART0::stop ()
 Stops buffered serial communications using interrupts on USART0. More...
 
size_t USART0::write (char c)
 Write a single byte to the transmit buffer. More...
 
size_t USART0::write (const char *c)
 Write a null-terminated string to the transmit buffer. More...
 
size_t USART0::write (const char *c, size_t n)
 Write a character array of given size to the transmit buffer. More...
 
size_t USART0::write (const uint8_t *c, size_t n)
 Write a byte array of given size to the transmit buffer. More...
 
void USART0::flush ()
 Flush transmit buffer. More...
 
int USART0::peek ()
 Examine the next character in the receive buffer without removing it from the buffer. More...
 
int USART0::read ()
 Return the next character in the receive buffer, removing it from the buffer. More...
 
bool USART0::available ()
 Determine if there is data in the receive buffer.. More...
 

Detailed Description

This file provides functions that offer high-level interfaces to USART0 hardware, which is available on the Arduino Uno (ATmega328) and Arduino Mega (ATmega2560).

These interfaces are buffered for both input and output and operate using interrupts associated with USART0. This means the transmit functions return immediately after queuing data in the output buffer for transmission and the transmission happens asynchronously, using dedicated USART0 hardware. Similarly, data is received asynchronously and placed into the input buffer.

The transmit and receive buffers are both ring buffers. If you try to queue more data than the transmit buffer can hold, the write functions will block until there is room in the buffer (as a result of data being transmitted). The receive buffer, however, will overwrite if it gets full. You must clear the receive buffer by reading it regularly when receiving significant amounts of data.

The sizes of the transmit and receive buffers can be set at compile time via macro constants. The default sizes are 32 bytes for the receive buffer and 64 bytes for the transmit buffer. To change these, define the macros USART0_RX_BUFFER_SIZE (for the receive buffer) and USART0_TX_BUFFER_SIZE (for the transmit buffer) to whatever sizes you need. You need to make these define these macros prior to compiling the file USART0.cpp.

Two interfaces are provided. USART0 is a functional interface that makes use of the buffering and asynchronous transmit and receive capabilities of the microcontrollers. However, USART0 is limited to transmitting and receiving byte and character streams.

Serial0 is the most advanced and capable interface to the USART0 hardware. Serial0 provides a object-oriented interface that includes the ability to read and write numbers of various types and in various formats, all asynchronously.

To use these functions, include USART0.h in your source code and link against USART0.cpp.

Note
Linking against USART0.cpp installs interrupt functions for transmit and receive on USART0 (interrupts USART_UDRE and USART_RX on Arduino Uno/ATmega328; interrupts USART0_UDRE and USART0_RX on Arduino Mega/ATmega2560). You cannot use the minimal interface to USART0 (from USARTMinimal.h) if you link against USART0.cpp. In particular, do not call initUSART0() or clearUSART0() if you link against USART0.cpp.

Enumeration Type Documentation

§ UsartSerialConfiguration

This enum lists serial configuration in terms of data bits, parity, and stop bits.

The format is kSerial_XYZ where

  • X = the number of data bits
  • Y = N, E, or O; where N = none, E = even, and O = odd
  • Z = the number of stop bits
Enumerator
kSerial_5N1 

5 data bits, no parity, 1 stop bit

kSerial_6N1 

6 data bits, no parity, 1 stop bit

kSerial_7N1 

7 data bits, no parity, 1 stop bit

kSerial_8N1 

8 data bits, no parity, 1 stop bit

kSerial_5N2 

5 data bits, no parity, 2 stop bits

kSerial_6N2 

6 data bits, no parity, 2 stop bits

kSerial_7N2 

7 data bits, no parity, 2 stop bits

kSerial_8N2 

8 data bits, no parity, 2 stop bits

kSerial_5E1 

5 data bits, even parity, 1 stop bit

kSerial_6E1 

6 data bits, even parity, 1 stop bit

kSerial_7E1 

7 data bits, even parity, 1 stop bit

kSerial_8E1 

8 data bits, even parity, 1 stop bit

kSerial_5E2 

5 data bits, even parity, 2 stop bits

kSerial_6E2 

6 data bits, even parity, 2 stop bits

kSerial_7E2 

7 data bits, even parity, 2 stop bits

kSerial_8E2 

8 data bits, even parity, 2 stop bits

kSerial_5O1 

5 data bits, odd parity, 1 stop bit

kSerial_6O1 

6 data bits, odd parity, 1 stop bit

kSerial_7O1 

7 data bits, odd parity, 1 stop bit

kSerial_8O1 

8 data bits, odd parity, 1 stop bit

kSerial_5O2 

5 data bits, odd parity, 2 stop bits

kSerial_6O2 

6 data bits, odd parity, 2 stop bits

kSerial_7O2 

7 data bits, odd parity, 2 stop bits

kSerial_8O2 

8 data bits, odd parity, 2 stop bits

kSerial_5N1 

5 data bits, no parity, 1 stop bit

kSerial_6N1 

6 data bits, no parity, 1 stop bit

kSerial_7N1 

7 data bits, no parity, 1 stop bit

kSerial_8N1 

8 data bits, no parity, 1 stop bit

kSerial_5N2 

5 data bits, no parity, 2 stop bits

kSerial_6N2 

6 data bits, no parity, 2 stop bits

kSerial_7N2 

7 data bits, no parity, 2 stop bits

kSerial_8N2 

8 data bits, no parity, 2 stop bits

kSerial_5E1 

5 data bits, even parity, 1 stop bit

kSerial_6E1 

6 data bits, even parity, 1 stop bit

kSerial_7E1 

7 data bits, even parity, 1 stop bit

kSerial_8E1 

8 data bits, even parity, 1 stop bit

kSerial_5E2 

5 data bits, even parity, 2 stop bits

kSerial_6E2 

6 data bits, even parity, 2 stop bits

kSerial_7E2 

7 data bits, even parity, 2 stop bits

kSerial_8E2 

8 data bits, even parity, 2 stop bits

kSerial_5O1 

5 data bits, odd parity, 1 stop bit

kSerial_6O1 

6 data bits, odd parity, 1 stop bit

kSerial_7O1 

7 data bits, odd parity, 1 stop bit

kSerial_8O1 

8 data bits, odd parity, 1 stop bit

kSerial_5O2 

5 data bits, odd parity, 2 stop bits

kSerial_6O2 

6 data bits, odd parity, 2 stop bits

kSerial_7O2 

7 data bits, odd parity, 2 stop bits

kSerial_8O2 

8 data bits, odd parity, 2 stop bits

kSerial_5N1 

5 data bits, no parity, 1 stop bit

kSerial_6N1 

6 data bits, no parity, 1 stop bit

kSerial_7N1 

7 data bits, no parity, 1 stop bit

kSerial_8N1 

8 data bits, no parity, 1 stop bit

kSerial_5N2 

5 data bits, no parity, 2 stop bits

kSerial_6N2 

6 data bits, no parity, 2 stop bits

kSerial_7N2 

7 data bits, no parity, 2 stop bits

kSerial_8N2 

8 data bits, no parity, 2 stop bits

kSerial_5E1 

5 data bits, even parity, 1 stop bit

kSerial_6E1 

6 data bits, even parity, 1 stop bit

kSerial_7E1 

7 data bits, even parity, 1 stop bit

kSerial_8E1 

8 data bits, even parity, 1 stop bit

kSerial_5E2 

5 data bits, even parity, 2 stop bits

kSerial_6E2 

6 data bits, even parity, 2 stop bits

kSerial_7E2 

7 data bits, even parity, 2 stop bits

kSerial_8E2 

8 data bits, even parity, 2 stop bits

kSerial_5O1 

5 data bits, odd parity, 1 stop bit

kSerial_6O1 

6 data bits, odd parity, 1 stop bit

kSerial_7O1 

7 data bits, odd parity, 1 stop bit

kSerial_8O1 

8 data bits, odd parity, 1 stop bit

kSerial_5O2 

5 data bits, odd parity, 2 stop bits

kSerial_6O2 

6 data bits, odd parity, 2 stop bits

kSerial_7O2 

7 data bits, odd parity, 2 stop bits

kSerial_8O2 

8 data bits, odd parity, 2 stop bits

kSerial_5N1 

5 data bits, no parity, 1 stop bit

kSerial_6N1 

6 data bits, no parity, 1 stop bit

kSerial_7N1 

7 data bits, no parity, 1 stop bit

kSerial_8N1 

8 data bits, no parity, 1 stop bit

kSerial_5N2 

5 data bits, no parity, 2 stop bits

kSerial_6N2 

6 data bits, no parity, 2 stop bits

kSerial_7N2 

7 data bits, no parity, 2 stop bits

kSerial_8N2 

8 data bits, no parity, 2 stop bits

kSerial_5E1 

5 data bits, even parity, 1 stop bit

kSerial_6E1 

6 data bits, even parity, 1 stop bit

kSerial_7E1 

7 data bits, even parity, 1 stop bit

kSerial_8E1 

8 data bits, even parity, 1 stop bit

kSerial_5E2 

5 data bits, even parity, 2 stop bits

kSerial_6E2 

6 data bits, even parity, 2 stop bits

kSerial_7E2 

7 data bits, even parity, 2 stop bits

kSerial_8E2 

8 data bits, even parity, 2 stop bits

kSerial_5O1 

5 data bits, odd parity, 1 stop bit

kSerial_6O1 

6 data bits, odd parity, 1 stop bit

kSerial_7O1 

7 data bits, odd parity, 1 stop bit

kSerial_8O1 

8 data bits, odd parity, 1 stop bit

kSerial_5O2 

5 data bits, odd parity, 2 stop bits

kSerial_6O2 

6 data bits, odd parity, 2 stop bits

kSerial_7O2 

7 data bits, odd parity, 2 stop bits

kSerial_8O2 

8 data bits, odd parity, 2 stop bits