Indent MarlinSerial code

This commit is contained in:
Scott Lahteine 2017-04-02 00:47:20 -05:00
parent 7a7a80e6c5
commit eaa66f3c46
2 changed files with 530 additions and 542 deletions

View file

@ -33,416 +33,26 @@
#include "stepper.h" #include "stepper.h"
#include "Marlin.h" #include "Marlin.h"
#ifndef USBCON // Disable HardwareSerial.cpp to support chips without a UART (Attiny, etc.)
// this next line disables the entire HardwareSerial.cpp,
// this is so I can support Attiny series and any other chip without a UART
#if defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H)
#if UART_PRESENT(SERIAL_PORT) #if !defined(USBCON) && (defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H))
#if UART_PRESENT(SERIAL_PORT)
ring_buffer_r rx_buffer = { { 0 }, 0, 0 }; ring_buffer_r rx_buffer = { { 0 }, 0, 0 };
#if TX_BUFFER_SIZE > 0 #if TX_BUFFER_SIZE > 0
ring_buffer_t tx_buffer = { { 0 }, 0, 0 }; ring_buffer_t tx_buffer = { { 0 }, 0, 0 };
static bool _written; static bool _written;
#endif #endif
#endif #endif
FORCE_INLINE void store_char(unsigned char c) {
CRITICAL_SECTION_START;
uint8_t h = rx_buffer.head;
uint8_t i = (uint8_t)(h + 1) & (RX_BUFFER_SIZE - 1);
// if we should be storing the received character into the location
// just before the tail (meaning that the head would advance to the
// current location of the tail), we're about to overflow the buffer
// and so we don't write the character or advance the head.
if (i != rx_buffer.tail) {
rx_buffer.buffer[h] = c;
rx_buffer.head = i;
}
CRITICAL_SECTION_END;
#if ENABLED(EMERGENCY_PARSER) #if ENABLED(EMERGENCY_PARSER)
emergency_parser(c);
#endif
}
#if TX_BUFFER_SIZE > 0 #include "language.h"
FORCE_INLINE void _tx_udr_empty_irq(void) {
// If interrupts are enabled, there must be more data in the output
// buffer. Send the next byte
uint8_t t = tx_buffer.tail;
uint8_t c = tx_buffer.buffer[t];
tx_buffer.tail = (t + 1) & (TX_BUFFER_SIZE - 1);
M_UDRx = c;
// clear the TXC bit -- "can be cleared by writing a one to its bit
// location". This makes sure flush() won't return until the bytes
// actually got written
SBI(M_UCSRxA, M_TXCx);
if (tx_buffer.head == tx_buffer.tail) {
// Buffer empty, so disable interrupts
CBI(M_UCSRxB, M_UDRIEx);
}
}
#ifdef M_USARTx_UDRE_vect
ISR(M_USARTx_UDRE_vect) {
_tx_udr_empty_irq();
}
#endif
#endif // TX_BUFFER_SIZE
#ifdef M_USARTx_RX_vect
ISR(M_USARTx_RX_vect) {
unsigned char c = M_UDRx;
store_char(c);
}
#endif
// Constructors ////////////////////////////////////////////////////////////////
MarlinSerial::MarlinSerial() { }
// Public Methods //////////////////////////////////////////////////////////////
void MarlinSerial::begin(long baud) {
uint16_t baud_setting;
bool useU2X = true;
#if F_CPU == 16000000UL && SERIAL_PORT == 0
// hard-coded exception for compatibility with the bootloader shipped
// with the Duemilanove and previous boards and the firmware on the 8U2
// on the Uno and Mega 2560.
if (baud == 57600) {
useU2X = false;
}
#endif
if (useU2X) {
M_UCSRxA = _BV(M_U2Xx);
baud_setting = (F_CPU / 4 / baud - 1) / 2;
}
else {
M_UCSRxA = 0;
baud_setting = (F_CPU / 8 / baud - 1) / 2;
}
// assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
M_UBRRxH = baud_setting >> 8;
M_UBRRxL = baud_setting;
SBI(M_UCSRxB, M_RXENx);
SBI(M_UCSRxB, M_TXENx);
SBI(M_UCSRxB, M_RXCIEx);
#if TX_BUFFER_SIZE > 0
CBI(M_UCSRxB, M_UDRIEx);
_written = false;
#endif
}
void MarlinSerial::end() {
CBI(M_UCSRxB, M_RXENx);
CBI(M_UCSRxB, M_TXENx);
CBI(M_UCSRxB, M_RXCIEx);
CBI(M_UCSRxB, M_UDRIEx);
}
void MarlinSerial::checkRx(void) {
if (TEST(M_UCSRxA, M_RXCx)) {
uint8_t c = M_UDRx;
store_char(c);
}
}
int MarlinSerial::peek(void) {
CRITICAL_SECTION_START;
int v = rx_buffer.head == rx_buffer.tail ? -1 : rx_buffer.buffer[rx_buffer.tail];
CRITICAL_SECTION_END;
return v;
}
int MarlinSerial::read(void) {
int v;
CRITICAL_SECTION_START;
uint8_t t = rx_buffer.tail;
if (rx_buffer.head == t) {
v = -1;
}
else {
v = rx_buffer.buffer[t];
rx_buffer.tail = (uint8_t)(t + 1) & (RX_BUFFER_SIZE - 1);
}
CRITICAL_SECTION_END;
return v;
}
uint8_t MarlinSerial::available(void) {
CRITICAL_SECTION_START;
uint8_t h = rx_buffer.head,
t = rx_buffer.tail;
CRITICAL_SECTION_END;
return (uint8_t)(RX_BUFFER_SIZE + h - t) & (RX_BUFFER_SIZE - 1);
}
void MarlinSerial::flush(void) {
// RX
// don't reverse this or there may be problems if the RX interrupt
// occurs after reading the value of rx_buffer_head but before writing
// the value to rx_buffer_tail; the previous value of rx_buffer_head
// may be written to rx_buffer_tail, making it appear as if the buffer
// were full, not empty.
CRITICAL_SECTION_START;
rx_buffer.head = rx_buffer.tail;
CRITICAL_SECTION_END;
}
#if TX_BUFFER_SIZE > 0
uint8_t MarlinSerial::availableForWrite(void) {
CRITICAL_SECTION_START;
uint8_t h = tx_buffer.head;
uint8_t t = tx_buffer.tail;
CRITICAL_SECTION_END;
return (uint8_t)(TX_BUFFER_SIZE + h - t) & (TX_BUFFER_SIZE - 1);
}
void MarlinSerial::write(uint8_t c) {
_written = true;
CRITICAL_SECTION_START;
bool emty = (tx_buffer.head == tx_buffer.tail);
CRITICAL_SECTION_END;
// If the buffer and the data register is empty, just write the byte
// to the data register and be done. This shortcut helps
// significantly improve the effective datarate at high (>
// 500kbit/s) bitrates, where interrupt overhead becomes a slowdown.
if (emty && TEST(M_UCSRxA, M_UDREx)) {
CRITICAL_SECTION_START;
M_UDRx = c;
SBI(M_UCSRxA, M_TXCx);
CRITICAL_SECTION_END;
return;
}
uint8_t i = (tx_buffer.head + 1) & (TX_BUFFER_SIZE - 1);
// If the output buffer is full, there's nothing for it other than to
// wait for the interrupt handler to empty it a bit
while (i == tx_buffer.tail) {
if (!TEST(SREG, SREG_I)) {
// Interrupts are disabled, so we'll have to poll the data
// register empty flag ourselves. If it is set, pretend an
// interrupt has happened and call the handler to free up
// space for us.
if (TEST(M_UCSRxA, M_UDREx))
_tx_udr_empty_irq();
} else {
// nop, the interrupt handler will free up space for us
}
}
tx_buffer.buffer[tx_buffer.head] = c;
{ CRITICAL_SECTION_START;
tx_buffer.head = i;
SBI(M_UCSRxB, M_UDRIEx);
CRITICAL_SECTION_END;
}
return;
}
void MarlinSerial::flushTX(void) {
// TX
// If we have never written a byte, no need to flush. This special
// case is needed since there is no way to force the TXC (transmit
// complete) bit to 1 during initialization
if (!_written)
return;
while (TEST(M_UCSRxB, M_UDRIEx) || !TEST(M_UCSRxA, M_TXCx)) {
if (!TEST(SREG, SREG_I) && TEST(M_UCSRxB, M_UDRIEx))
// Interrupts are globally disabled, but the DR empty
// interrupt should be enabled, so poll the DR empty flag to
// prevent deadlock
if (TEST(M_UCSRxA, M_UDREx))
_tx_udr_empty_irq();
}
// If we get here, nothing is queued anymore (DRIE is disabled) and
// the hardware finished tranmission (TXC is set).
}
#else
void MarlinSerial::write(uint8_t c) {
while (!TEST(M_UCSRxA, M_UDREx))
;
M_UDRx = c;
}
#endif
// end NEW
/// imports from print.h
void MarlinSerial::print(char c, int base) {
print((long) c, base);
}
void MarlinSerial::print(unsigned char b, int base) {
print((unsigned long) b, base);
}
void MarlinSerial::print(int n, int base) {
print((long) n, base);
}
void MarlinSerial::print(unsigned int n, int base) {
print((unsigned long) n, base);
}
void MarlinSerial::print(long n, int base) {
if (base == 0) {
write(n);
}
else if (base == 10) {
if (n < 0) {
print('-');
n = -n;
}
printNumber(n, 10);
}
else {
printNumber(n, base);
}
}
void MarlinSerial::print(unsigned long n, int base) {
if (base == 0) write(n);
else printNumber(n, base);
}
void MarlinSerial::print(double n, int digits) {
printFloat(n, digits);
}
void MarlinSerial::println(void) {
print('\r');
print('\n');
}
void MarlinSerial::println(const String& s) {
print(s);
println();
}
void MarlinSerial::println(const char c[]) {
print(c);
println();
}
void MarlinSerial::println(char c, int base) {
print(c, base);
println();
}
void MarlinSerial::println(unsigned char b, int base) {
print(b, base);
println();
}
void MarlinSerial::println(int n, int base) {
print(n, base);
println();
}
void MarlinSerial::println(unsigned int n, int base) {
print(n, base);
println();
}
void MarlinSerial::println(long n, int base) {
print(n, base);
println();
}
void MarlinSerial::println(unsigned long n, int base) {
print(n, base);
println();
}
void MarlinSerial::println(double n, int digits) {
print(n, digits);
println();
}
// Private Methods /////////////////////////////////////////////////////////////
void MarlinSerial::printNumber(unsigned long n, uint8_t base) {
if (n) {
unsigned char buf[8 * sizeof(long)]; // Enough space for base 2
int8_t i = 0;
while (n) {
buf[i++] = n % base;
n /= base;
}
while (i--)
print((char)(buf[i] + (buf[i] < 10 ? '0' : 'A' - 10)));
}
else
print('0');
}
void MarlinSerial::printFloat(double number, uint8_t digits) {
// Handle negative numbers
if (number < 0.0) {
print('-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i = 0; i < digits; ++i)
rounding *= 0.1;
number += rounding;
// Extract the integer part of the number and print it
unsigned long int_part = (unsigned long)number;
double remainder = number - (double)int_part;
print(int_part);
// Print the decimal point, but only if there are digits beyond
if (digits) {
print('.');
// Extract digits from the remainder one at a time
while (digits--) {
remainder *= 10.0;
int toPrint = int(remainder);
print(toPrint);
remainder -= toPrint;
}
}
}
// Preinstantiate Objects //////////////////////////////////////////////////////
MarlinSerial customizedSerial;
#endif // whole file
#endif // !USBCON
// For AT90USB targets use the UART for BT interfacing
#if defined(USBCON) && ENABLED(BLUETOOTH)
HardwareSerial bluetoothSerial;
#endif
#if ENABLED(EMERGENCY_PARSER)
// Currently looking for: M108, M112, M410 // Currently looking for: M108, M112, M410
// If you alter the parser please don't forget to update the capabilities in Conditionals_post.h // If you alter the parser please don't forget to update the capabilities in Conditionals_post.h
FORCE_INLINE void emergency_parser(unsigned char c) { FORCE_INLINE void emergency_parser(const unsigned char c) {
static e_parser_state state = state_RESET; static e_parser_state state = state_RESET;
@ -524,4 +134,389 @@ MarlinSerial customizedSerial;
} }
} }
#endif
FORCE_INLINE void store_char(unsigned char c) {
CRITICAL_SECTION_START;
uint8_t h = rx_buffer.head;
uint8_t i = (uint8_t)(h + 1) & (RX_BUFFER_SIZE - 1);
// if we should be storing the received character into the location
// just before the tail (meaning that the head would advance to the
// current location of the tail), we're about to overflow the buffer
// and so we don't write the character or advance the head.
if (i != rx_buffer.tail) {
rx_buffer.buffer[h] = c;
rx_buffer.head = i;
}
CRITICAL_SECTION_END;
#if ENABLED(EMERGENCY_PARSER)
emergency_parser(c);
#endif
}
#if TX_BUFFER_SIZE > 0
FORCE_INLINE void _tx_udr_empty_irq(void) {
// If interrupts are enabled, there must be more data in the output
// buffer. Send the next byte
uint8_t t = tx_buffer.tail;
uint8_t c = tx_buffer.buffer[t];
tx_buffer.tail = (t + 1) & (TX_BUFFER_SIZE - 1);
M_UDRx = c;
// clear the TXC bit -- "can be cleared by writing a one to its bit
// location". This makes sure flush() won't return until the bytes
// actually got written
SBI(M_UCSRxA, M_TXCx);
if (tx_buffer.head == tx_buffer.tail) {
// Buffer empty, so disable interrupts
CBI(M_UCSRxB, M_UDRIEx);
}
}
#ifdef M_USARTx_UDRE_vect
ISR(M_USARTx_UDRE_vect) {
_tx_udr_empty_irq();
}
#endif
#endif // TX_BUFFER_SIZE
#ifdef M_USARTx_RX_vect
ISR(M_USARTx_RX_vect) {
unsigned char c = M_UDRx;
store_char(c);
}
#endif
// Public Methods
void MarlinSerial::begin(long baud) {
uint16_t baud_setting;
bool useU2X = true;
#if F_CPU == 16000000UL && SERIAL_PORT == 0
// hard-coded exception for compatibility with the bootloader shipped
// with the Duemilanove and previous boards and the firmware on the 8U2
// on the Uno and Mega 2560.
if (baud == 57600) {
useU2X = false;
}
#endif
if (useU2X) {
M_UCSRxA = _BV(M_U2Xx);
baud_setting = (F_CPU / 4 / baud - 1) / 2;
}
else {
M_UCSRxA = 0;
baud_setting = (F_CPU / 8 / baud - 1) / 2;
}
// assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
M_UBRRxH = baud_setting >> 8;
M_UBRRxL = baud_setting;
SBI(M_UCSRxB, M_RXENx);
SBI(M_UCSRxB, M_TXENx);
SBI(M_UCSRxB, M_RXCIEx);
#if TX_BUFFER_SIZE > 0
CBI(M_UCSRxB, M_UDRIEx);
_written = false;
#endif
}
void MarlinSerial::end() {
CBI(M_UCSRxB, M_RXENx);
CBI(M_UCSRxB, M_TXENx);
CBI(M_UCSRxB, M_RXCIEx);
CBI(M_UCSRxB, M_UDRIEx);
}
void MarlinSerial::checkRx(void) {
if (TEST(M_UCSRxA, M_RXCx)) {
uint8_t c = M_UDRx;
store_char(c);
}
}
int MarlinSerial::peek(void) {
CRITICAL_SECTION_START;
int v = rx_buffer.head == rx_buffer.tail ? -1 : rx_buffer.buffer[rx_buffer.tail];
CRITICAL_SECTION_END;
return v;
}
int MarlinSerial::read(void) {
int v;
CRITICAL_SECTION_START;
uint8_t t = rx_buffer.tail;
if (rx_buffer.head == t) {
v = -1;
}
else {
v = rx_buffer.buffer[t];
rx_buffer.tail = (uint8_t)(t + 1) & (RX_BUFFER_SIZE - 1);
}
CRITICAL_SECTION_END;
return v;
}
uint8_t MarlinSerial::available(void) {
CRITICAL_SECTION_START;
uint8_t h = rx_buffer.head,
t = rx_buffer.tail;
CRITICAL_SECTION_END;
return (uint8_t)(RX_BUFFER_SIZE + h - t) & (RX_BUFFER_SIZE - 1);
}
void MarlinSerial::flush(void) {
// RX
// don't reverse this or there may be problems if the RX interrupt
// occurs after reading the value of rx_buffer_head but before writing
// the value to rx_buffer_tail; the previous value of rx_buffer_head
// may be written to rx_buffer_tail, making it appear as if the buffer
// were full, not empty.
CRITICAL_SECTION_START;
rx_buffer.head = rx_buffer.tail;
CRITICAL_SECTION_END;
}
#if TX_BUFFER_SIZE > 0
uint8_t MarlinSerial::availableForWrite(void) {
CRITICAL_SECTION_START;
uint8_t h = tx_buffer.head;
uint8_t t = tx_buffer.tail;
CRITICAL_SECTION_END;
return (uint8_t)(TX_BUFFER_SIZE + h - t) & (TX_BUFFER_SIZE - 1);
}
void MarlinSerial::write(uint8_t c) {
_written = true;
CRITICAL_SECTION_START;
bool emty = (tx_buffer.head == tx_buffer.tail);
CRITICAL_SECTION_END;
// If the buffer and the data register is empty, just write the byte
// to the data register and be done. This shortcut helps
// significantly improve the effective datarate at high (>
// 500kbit/s) bitrates, where interrupt overhead becomes a slowdown.
if (emty && TEST(M_UCSRxA, M_UDREx)) {
CRITICAL_SECTION_START;
M_UDRx = c;
SBI(M_UCSRxA, M_TXCx);
CRITICAL_SECTION_END;
return;
}
uint8_t i = (tx_buffer.head + 1) & (TX_BUFFER_SIZE - 1);
// If the output buffer is full, there's nothing for it other than to
// wait for the interrupt handler to empty it a bit
while (i == tx_buffer.tail) {
if (!TEST(SREG, SREG_I)) {
// Interrupts are disabled, so we'll have to poll the data
// register empty flag ourselves. If it is set, pretend an
// interrupt has happened and call the handler to free up
// space for us.
if (TEST(M_UCSRxA, M_UDREx))
_tx_udr_empty_irq();
} else {
// nop, the interrupt handler will free up space for us
}
}
tx_buffer.buffer[tx_buffer.head] = c;
{ CRITICAL_SECTION_START;
tx_buffer.head = i;
SBI(M_UCSRxB, M_UDRIEx);
CRITICAL_SECTION_END;
}
return;
}
void MarlinSerial::flushTX(void) {
// TX
// If we have never written a byte, no need to flush. This special
// case is needed since there is no way to force the TXC (transmit
// complete) bit to 1 during initialization
if (!_written)
return;
while (TEST(M_UCSRxB, M_UDRIEx) || !TEST(M_UCSRxA, M_TXCx)) {
if (!TEST(SREG, SREG_I) && TEST(M_UCSRxB, M_UDRIEx))
// Interrupts are globally disabled, but the DR empty
// interrupt should be enabled, so poll the DR empty flag to
// prevent deadlock
if (TEST(M_UCSRxA, M_UDREx))
_tx_udr_empty_irq();
}
// If we get here, nothing is queued anymore (DRIE is disabled) and
// the hardware finished tranmission (TXC is set).
}
#else
void MarlinSerial::write(uint8_t c) {
while (!TEST(M_UCSRxA, M_UDREx))
;
M_UDRx = c;
}
#endif
// end NEW
/// imports from print.h
void MarlinSerial::print(char c, int base) {
print((long) c, base);
}
void MarlinSerial::print(unsigned char b, int base) {
print((unsigned long) b, base);
}
void MarlinSerial::print(int n, int base) {
print((long) n, base);
}
void MarlinSerial::print(unsigned int n, int base) {
print((unsigned long) n, base);
}
void MarlinSerial::print(long n, int base) {
if (base == 0) {
write(n);
}
else if (base == 10) {
if (n < 0) {
print('-');
n = -n;
}
printNumber(n, 10);
}
else {
printNumber(n, base);
}
}
void MarlinSerial::print(unsigned long n, int base) {
if (base == 0) write(n);
else printNumber(n, base);
}
void MarlinSerial::print(double n, int digits) {
printFloat(n, digits);
}
void MarlinSerial::println(void) {
print('\r');
print('\n');
}
void MarlinSerial::println(const String& s) {
print(s);
println();
}
void MarlinSerial::println(const char c[]) {
print(c);
println();
}
void MarlinSerial::println(char c, int base) {
print(c, base);
println();
}
void MarlinSerial::println(unsigned char b, int base) {
print(b, base);
println();
}
void MarlinSerial::println(int n, int base) {
print(n, base);
println();
}
void MarlinSerial::println(unsigned int n, int base) {
print(n, base);
println();
}
void MarlinSerial::println(long n, int base) {
print(n, base);
println();
}
void MarlinSerial::println(unsigned long n, int base) {
print(n, base);
println();
}
void MarlinSerial::println(double n, int digits) {
print(n, digits);
println();
}
// Private Methods
void MarlinSerial::printNumber(unsigned long n, uint8_t base) {
if (n) {
unsigned char buf[8 * sizeof(long)]; // Enough space for base 2
int8_t i = 0;
while (n) {
buf[i++] = n % base;
n /= base;
}
while (i--)
print((char)(buf[i] + (buf[i] < 10 ? '0' : 'A' - 10)));
}
else
print('0');
}
void MarlinSerial::printFloat(double number, uint8_t digits) {
// Handle negative numbers
if (number < 0.0) {
print('-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i = 0; i < digits; ++i)
rounding *= 0.1;
number += rounding;
// Extract the integer part of the number and print it
unsigned long int_part = (unsigned long)number;
double remainder = number - (double)int_part;
print(int_part);
// Print the decimal point, but only if there are digits beyond
if (digits) {
print('.');
// Extract digits from the remainder one at a time
while (digits--) {
remainder *= 10.0;
int toPrint = int(remainder);
print(toPrint);
remainder -= toPrint;
}
}
}
// Preinstantiate
MarlinSerial customizedSerial;
#endif // !USBCON && (UBRRH || UBRR0H || UBRR1H || UBRR2H || UBRR3H)
// For AT90USB targets use the UART for BT interfacing
#if defined(USBCON) && ENABLED(BLUETOOTH)
HardwareSerial bluetoothSerial;
#endif #endif

View file

@ -29,8 +29,8 @@
*/ */
#ifndef MarlinSerial_h #ifndef MARLINSERIAL_H
#define MarlinSerial_h #define MARLINSERIAL_H
#include "MarlinConfig.h" #include "MarlinConfig.h"
@ -52,8 +52,7 @@
#define SERIAL_REGNAME_INTERNAL(registerbase,number,suffix) registerbase##number##suffix #define SERIAL_REGNAME_INTERNAL(registerbase,number,suffix) registerbase##number##suffix
#endif #endif
// Registers used by MarlinSerial class (these are expanded // Registers used by MarlinSerial class (expanded depending on selected serial port)
// depending on selected serial port
#define M_UCSRxA SERIAL_REGNAME(UCSR,SERIAL_PORT,A) // defines M_UCSRxA to be UCSRnA where n is the serial port number #define M_UCSRxA SERIAL_REGNAME(UCSR,SERIAL_PORT,A) // defines M_UCSRxA to be UCSRnA where n is the serial port number
#define M_UCSRxB SERIAL_REGNAME(UCSR,SERIAL_PORT,B) #define M_UCSRxB SERIAL_REGNAME(UCSR,SERIAL_PORT,B)
#define M_RXENx SERIAL_REGNAME(RXEN,SERIAL_PORT,) #define M_RXENx SERIAL_REGNAME(RXEN,SERIAL_PORT,)
@ -70,63 +69,56 @@
#define M_U2Xx SERIAL_REGNAME(U2X,SERIAL_PORT,) #define M_U2Xx SERIAL_REGNAME(U2X,SERIAL_PORT,)
#define M_USARTx_UDRE_vect SERIAL_REGNAME(USART,SERIAL_PORT,_UDRE_vect) #define M_USARTx_UDRE_vect SERIAL_REGNAME(USART,SERIAL_PORT,_UDRE_vect)
#define DEC 10 #define DEC 10
#define HEX 16 #define HEX 16
#define OCT 8 #define OCT 8
#define BIN 2 #define BIN 2
#define BYTE 0 #define BYTE 0
#ifndef USBCON #ifndef USBCON
// Define constants and variables for buffering incoming serial data. We're // Define constants and variables for buffering incoming serial data. We're
// using a ring buffer (I think), in which rx_buffer_head is the index of the // using a ring buffer (I think), in which rx_buffer_head is the index of the
// location to which to write the next incoming character and rx_buffer_tail // location to which to write the next incoming character and rx_buffer_tail
// is the index of the location from which to read. // is the index of the location from which to read.
// 256 is the max limit due to uint8_t head and tail. Use only powers of 2. (...,16,32,64,128,256) // 256 is the max limit due to uint8_t head and tail. Use only powers of 2. (...,16,32,64,128,256)
#ifndef RX_BUFFER_SIZE #ifndef RX_BUFFER_SIZE
#define RX_BUFFER_SIZE 128 #define RX_BUFFER_SIZE 128
#endif #endif
#ifndef TX_BUFFER_SIZE #ifndef TX_BUFFER_SIZE
#define TX_BUFFER_SIZE 32 #define TX_BUFFER_SIZE 32
#endif #endif
#if !((RX_BUFFER_SIZE == 256) ||(RX_BUFFER_SIZE == 128) ||(RX_BUFFER_SIZE == 64) ||(RX_BUFFER_SIZE == 32) ||(RX_BUFFER_SIZE == 16) ||(RX_BUFFER_SIZE == 8) ||(RX_BUFFER_SIZE == 4) ||(RX_BUFFER_SIZE == 2)) #if !((RX_BUFFER_SIZE == 256) ||(RX_BUFFER_SIZE == 128) ||(RX_BUFFER_SIZE == 64) ||(RX_BUFFER_SIZE == 32) ||(RX_BUFFER_SIZE == 16) ||(RX_BUFFER_SIZE == 8) ||(RX_BUFFER_SIZE == 4) ||(RX_BUFFER_SIZE == 2))
#error "RX_BUFFER_SIZE has to be a power of 2 and >= 2" #error "RX_BUFFER_SIZE has to be a power of 2 and >= 2"
#endif #endif
#if !((TX_BUFFER_SIZE == 256) ||(TX_BUFFER_SIZE == 128) ||(TX_BUFFER_SIZE == 64) ||(TX_BUFFER_SIZE == 32) ||(TX_BUFFER_SIZE == 16) ||(TX_BUFFER_SIZE == 8) ||(TX_BUFFER_SIZE == 4) ||(TX_BUFFER_SIZE == 2) ||(TX_BUFFER_SIZE == 0)) #if !((TX_BUFFER_SIZE == 256) ||(TX_BUFFER_SIZE == 128) ||(TX_BUFFER_SIZE == 64) ||(TX_BUFFER_SIZE == 32) ||(TX_BUFFER_SIZE == 16) ||(TX_BUFFER_SIZE == 8) ||(TX_BUFFER_SIZE == 4) ||(TX_BUFFER_SIZE == 2) ||(TX_BUFFER_SIZE == 0))
#error TX_BUFFER_SIZE has to be a power of 2 or 0 #error TX_BUFFER_SIZE has to be a power of 2 or 0
#endif #endif
struct ring_buffer_r { struct ring_buffer_r {
unsigned char buffer[RX_BUFFER_SIZE]; unsigned char buffer[RX_BUFFER_SIZE];
volatile uint8_t head; volatile uint8_t head;
volatile uint8_t tail; volatile uint8_t tail;
}; };
#if TX_BUFFER_SIZE > 0 #if TX_BUFFER_SIZE > 0
struct ring_buffer_t { struct ring_buffer_t {
unsigned char buffer[TX_BUFFER_SIZE]; unsigned char buffer[TX_BUFFER_SIZE];
volatile uint8_t head; volatile uint8_t head;
volatile uint8_t tail; volatile uint8_t tail;
}; };
#endif #endif
#if UART_PRESENT(SERIAL_PORT) #if UART_PRESENT(SERIAL_PORT)
extern ring_buffer_r rx_buffer; extern ring_buffer_r rx_buffer;
#if TX_BUFFER_SIZE > 0 #if TX_BUFFER_SIZE > 0
extern ring_buffer_t tx_buffer; extern ring_buffer_t tx_buffer;
#endif #endif
#endif #endif
#if ENABLED(EMERGENCY_PARSER) class MarlinSerial { //: public Stream
#include "language.h"
void emergency_parser(unsigned char c);
#endif
class MarlinSerial { //: public Stream
public: public:
MarlinSerial(); MarlinSerial() {};
static void begin(long); static void begin(long);
static void end(); static void end();
static int peek(void); static int peek(void);
@ -168,9 +160,10 @@ class MarlinSerial { //: public Stream
static void println(unsigned long, int = DEC); static void println(unsigned long, int = DEC);
static void println(double, int = 2); static void println(double, int = 2);
static void println(void); static void println(void);
}; };
extern MarlinSerial customizedSerial;
extern MarlinSerial customizedSerial;
#endif // !USBCON #endif // !USBCON
// Use the UART for Bluetooth in AT90USB configurations // Use the UART for Bluetooth in AT90USB configurations
@ -178,4 +171,4 @@ extern MarlinSerial customizedSerial;
extern HardwareSerial bluetoothSerial; extern HardwareSerial bluetoothSerial;
#endif #endif
#endif #endif // MARLINSERIAL_H