English

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

#ifndef HardwareSerial_h

#define HardwareSerial_h

#include “Arduino.h”

#include <inttypes.h>

#include “Stream.h”

// Define constants and variables for buffering incoming serial


data. We’re

// using a ring buffer (I think), in which head is the index of


the location

// to which to write the next incoming character and tail is the


index of the

// location from which to read.

// NOTE: a “power of 2” buffer size is recommended to


dramatically

// optimize all the modulo operations for ring buffers.

// WARNING: When buffer sizes are increased to > 256, the buffer
index

// variables are automatically increased in size, but the extra

// atomicity guards needed for that are not implemented. This


will
// often work, but occasionally a race condition can occur that
makes

// Serial behave erratically. See


https://github.com/arduino/Arduino/issues/2405

#if !defined(SERIAL_TX_BUFFER_SIZE)

#if ((RAMEND – RAMSTART) < 1023)

#define SERIAL_TX_BUFFER_SIZE 16

#else

#define SERIAL_TX_BUFFER_SIZE 64

#endif

#endif

#if !defined(SERIAL_RX_BUFFER_SIZE)

#if ((RAMEND – RAMSTART) < 1023)

#define SERIAL_RX_BUFFER_SIZE 16

#else

#define SERIAL_RX_BUFFER_SIZE 64

#endif

#endif

#if (SERIAL_TX_BUFFER_SIZE>256)

Typedef uint16_t tx_buffer_index_t;

#else
Typedef uint8_t tx_buffer_index_t;

#endif

#if (SERIAL_RX_BUFFER_SIZE>256)

Typedef uint16_t rx_buffer_index_t;

#else

Typedef uint8_t rx_buffer_index_t;

#endif

// Define config for Serial.begin(baud, config);

#define SERIAL_5N1 0x00

#define SERIAL_6N1 0x02

#define SERIAL_7N1 0x04

#define SERIAL_8N1 0x06

#define SERIAL_5N2 0x08

#define SERIAL_6N2 0x0A

#define SERIAL_7N2 0x0C

#define SERIAL_8N2 0x0E

#define SERIAL_5E1 0x20

#define SERIAL_6E1 0x22

#define SERIAL_7E1 0x24


#define SERIAL_8E1 0x26

#define SERIAL_5E2 0x28

#define SERIAL_6E2 0x2A

#define SERIAL_7E2 0x2C

#define SERIAL_8E2 0x2E

#define SERIAL_5O1 0x30

#define SERIAL_6O1 0x32

#define SERIAL_7O1 0x34

#define SERIAL_8O1 0x36

#define SERIAL_5O2 0x38

#define SERIAL_6O2 0x3A

#define SERIAL_7O2 0x3C

#define SERIAL_8O2 0x3E

Class HardwareSerial : public Stream

Protected:

Volatile uint8_t * const _ubrrh;

Volatile uint8_t * const _ubrrl;

Volatile uint8_t * const _ucsra;


Volatile uint8_t * const _ucsrb;

Volatile uint8_t * const _ucsrc;

Volatile uint8_t * const _udr;

// Has any byte been written to the UART since begin()

Bool _written;

Volatile rx_buffer_index_t _rx_buffer_head;

Volatile rx_buffer_index_t _rx_buffer_tail;

Volatile tx_buffer_index_t _tx_buffer_head;

Volatile tx_buffer_index_t _tx_buffer_tail;

// Don’t put any members after these buffers, since only the
first

// 32 bytes of this struct can be accessed quickly using the


ldd

// instruction.

Unsigned char _rx_buffer[SERIAL_RX_BUFFER_SIZE];

Unsigned char _tx_buffer[SERIAL_TX_BUFFER_SIZE];

Public:

Inline 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);

Void begin(unsigned long baud) { begin(baud, SERIAL_8N1); }

Void begin(unsigned long, uint8_t);

Void end();

Virtual int available(void);

Virtual int peek(void);

Virtual int read(void);

Virtual int availableForWrite(void);

Virtual void flush(void);

Virtual size_t write(uint8_t);

Inline size_t write(unsigned long n) { return


write((uint8_t)n); }

Inline size_t write(long n) { return write((uint8_t)n); }

Inline size_t write(unsigned int n) { return


write((uint8_t)n); }

Inline size_t write(int n) { return write((uint8_t)n); }

Using Print::write; // pull in write(str) and write(buf,


size) from Print

Operator bool() { return true; }


// Interrupt handlers – Not intended to be called externally

Inline void _rx_complete_irq(void);

Void _tx_udr_empty_irq(void);

};

#if defined(UBRRH) || defined(UBRR0H)

Extern HardwareSerial Serial;

#define HAVE_HWSERIAL0

#endif

#if defined(UBRR1H)

Extern HardwareSerial Serial1;

#define HAVE_HWSERIAL1

#endif

#if defined(UBRR2H)

Extern HardwareSerial Serial2;

#define HAVE_HWSERIAL2

#endif

#if defined(UBRR3H)

Extern HardwareSerial Serial3;


#define HAVE_HWSERIAL3

#endif

Extern void serialEventRun(void) __attribute__((weak));

#endif

You might also like