AVRTools
A Library for the AVR ATmega328 and ATmega2560 Microcontrollers
|
This file provides functions that offer high-level interfaces to USART2 hardware, which is available on Arduino Mega (ATMega2560). More...
Go to the source code of this file.
Classes | |
class | Serial2 |
Provides a high-end interface to serial communications using USART2. More... | |
Namespaces | |
USART2 | |
This namespace bundles a high-level buffered interface to the USART2 hardware. It provides logical cohesion and prevents namespace collisions. | |
Functions | |
void | USART2::start (unsigned long baudRate, UsartSerialConfiguration config=kSerial_8N1) |
Initialize USART2 for buffered, asynchronous serial communications using interrupts. More... | |
void | USART2::stop () |
Stops buffered serial communications using interrupts on USART2. More... | |
size_t | USART2::write (char c) |
Write a single byte to the transmit buffer. More... | |
size_t | USART2::write (const char *c) |
Write a null-terminated string to the transmit buffer. More... | |
size_t | USART2::write (const char *c, size_t n) |
Write a character array of given size to the transmit buffer. More... | |
size_t | USART2::write (const uint8_t *c, size_t n) |
Write a byte array of given size to the transmit buffer. More... | |
void | USART2::flush () |
Flush transmit buffer. More... | |
int | USART2::peek () |
Examine the next character in the receive buffer without removing it from the buffer. More... | |
int | USART2::read () |
Return the next character in the receive buffer, removing it from the buffer. More... | |
bool | USART2::available () |
Determine if there is data in the receive buffer.. More... | |
This file provides functions that offer high-level interfaces to USART2 hardware, which is available on Arduino Mega (ATMega2560).
These interfaces are buffered for both input and output and operate using interrupts associated with USART2. This means the transmit functions return immediately after queuing data in the output buffer for transmission and the transmission happens asynchronously, using dedicated USART2 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 USART2_RX_BUFFER_SIZE
(for the receive buffer) and USART2_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 USART2.cpp.
Two interfaces are provided. USART2 is a functional interface that makes use of the buffering and asynchronous transmit and receive capabilities of the microcontrollers. However, USART2 is limited to transmitting and receiving byte and character streams.
Serial2 is the most advanced and capable interface to the USART2 hardware. Serial2 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 USART2.h in your source code and link against USART2.cpp.
This enum lists serial configuration in terms of data bits, parity, and stop bits.
The format is kSerial_XYZ where