|
|
|
@ -27,15 +27,30 @@
|
|
|
|
|
* Modified 23 November 2006 by David A. Mellis
|
|
|
|
|
* Modified 28 September 2010 by Mark Sproul
|
|
|
|
|
* Modified 14 February 2016 by Andreas Hardtung (added tx buffer)
|
|
|
|
|
* Modified 01 October 2017 by Eduardo José Tagle (added XON/XOFF)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "MarlinSerial.h"
|
|
|
|
|
#include "Marlin.h"
|
|
|
|
|
|
|
|
|
|
// Disable HardwareSerial.cpp to support chips without a UART (Attiny, etc.)
|
|
|
|
|
|
|
|
|
|
#include "MarlinConfig.h"
|
|
|
|
|
|
|
|
|
|
#if !defined(USBCON) && (defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H))
|
|
|
|
|
|
|
|
|
|
#include "MarlinSerial.h"
|
|
|
|
|
#include "Marlin.h"
|
|
|
|
|
|
|
|
|
|
struct ring_buffer_r {
|
|
|
|
|
unsigned char buffer[RX_BUFFER_SIZE];
|
|
|
|
|
volatile ring_buffer_pos_t head, tail;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#if TX_BUFFER_SIZE > 0
|
|
|
|
|
struct ring_buffer_t {
|
|
|
|
|
unsigned char buffer[TX_BUFFER_SIZE];
|
|
|
|
|
volatile uint8_t head, tail;
|
|
|
|
|
};
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if UART_PRESENT(SERIAL_PORT)
|
|
|
|
|
ring_buffer_r rx_buffer = { { 0 }, 0, 0 };
|
|
|
|
|
#if TX_BUFFER_SIZE > 0
|
|
|
|
@ -46,11 +61,17 @@
|
|
|
|
|
|
|
|
|
|
#if ENABLED(SERIAL_XON_XOFF)
|
|
|
|
|
uint8_t xon_xoff_state = XON_XOFF_CHAR_SENT | XON_CHAR;
|
|
|
|
|
constexpr uint8_t XON_XOFF_CHAR_SENT = 0x80; // XON / XOFF Character was sent
|
|
|
|
|
constexpr uint8_t XON_XOFF_CHAR_MASK = 0x1F; // XON / XOFF character to send
|
|
|
|
|
// XON / XOFF character definitions
|
|
|
|
|
constexpr uint8_t XON_CHAR = 17;
|
|
|
|
|
constexpr uint8_t XOFF_CHAR = 19;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if ENABLED(SERIAL_STATS_DROPPED_RX)
|
|
|
|
|
uint8_t rx_dropped_bytes = 0;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
|
|
|
|
|
ring_buffer_pos_t rx_max_enqueued = 0;
|
|
|
|
|
#endif
|
|
|
|
@ -151,10 +172,9 @@
|
|
|
|
|
const ring_buffer_pos_t h = rx_buffer.head,
|
|
|
|
|
i = (ring_buffer_pos_t)(h + 1) & (ring_buffer_pos_t)(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 the character is to be stored at the index just before the tail
|
|
|
|
|
// (such that the head would advance to the current tail), the buffer is
|
|
|
|
|
// critical, so don't write the character or advance the head.
|
|
|
|
|
if (i != rx_buffer.tail) {
|
|
|
|
|
rx_buffer.buffer[h] = M_UDRx;
|
|
|
|
|
rx_buffer.head = i;
|
|
|
|
@ -162,19 +182,15 @@
|
|
|
|
|
else {
|
|
|
|
|
(void)M_UDRx;
|
|
|
|
|
#if ENABLED(SERIAL_STATS_DROPPED_RX)
|
|
|
|
|
if (!++rx_dropped_bytes)
|
|
|
|
|
++rx_dropped_bytes;
|
|
|
|
|
if (!++rx_dropped_bytes) ++rx_dropped_bytes;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
|
|
|
|
|
{
|
|
|
|
|
// calculate count of bytes stored into the RX buffer
|
|
|
|
|
ring_buffer_pos_t rx_count = (ring_buffer_pos_t)(rx_buffer.head - rx_buffer.tail) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1);
|
|
|
|
|
|
|
|
|
|
// Keep track of the maximum count of enqueued bytes
|
|
|
|
|
if (rx_max_enqueued < rx_count)
|
|
|
|
|
rx_max_enqueued = rx_count;
|
|
|
|
|
}
|
|
|
|
|
NOLESS(rx_max_enqueued, rx_count);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if ENABLED(SERIAL_XON_XOFF)
|
|
|
|
@ -190,51 +206,40 @@
|
|
|
|
|
// we run out of RX buffer space .. We need 325 bytes @ 250kbits/s to
|
|
|
|
|
// let the host react and stop sending bytes. This translates to 13mS
|
|
|
|
|
// propagation time.
|
|
|
|
|
if (rx_count >= (RX_BUFFER_SIZE/8)) {
|
|
|
|
|
|
|
|
|
|
if (rx_count >= (RX_BUFFER_SIZE) / 8) {
|
|
|
|
|
// If TX interrupts are disabled and 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 (!TEST(M_UCSRxB, M_UDRIEx) && TEST(M_UCSRxA, M_UDREx)) {
|
|
|
|
|
|
|
|
|
|
// Send an XOFF character
|
|
|
|
|
M_UDRx = XOFF_CHAR;
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
|
|
// And remember we already sent it
|
|
|
|
|
// And remember it was sent
|
|
|
|
|
xon_xoff_state = XOFF_CHAR | XON_XOFF_CHAR_SENT;
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// TX interrupts disabled, but buffer still not empty ... or
|
|
|
|
|
// TX interrupts enabled. Reenable TX ints and schedule XOFF
|
|
|
|
|
// character to be sent
|
|
|
|
|
#if TX_BUFFER_SIZE > 0
|
|
|
|
|
|
|
|
|
|
SBI(M_UCSRxB, M_UDRIEx);
|
|
|
|
|
xon_xoff_state = XOFF_CHAR;
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
// We are not using TX interrupts, we will have to send this manually
|
|
|
|
|
while (!TEST(M_UCSRxA, M_UDREx))
|
|
|
|
|
;
|
|
|
|
|
while (!TEST(M_UCSRxA, M_UDREx)) {/* nada */}
|
|
|
|
|
M_UDRx = XOFF_CHAR;
|
|
|
|
|
|
|
|
|
|
// And remember we already sent it
|
|
|
|
|
xon_xoff_state = XOFF_CHAR | XON_XOFF_CHAR_SENT;
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#endif // SERIAL_XON_XOFF
|
|
|
|
|
|
|
|
|
|
#if ENABLED(EMERGENCY_PARSER)
|
|
|
|
|
emergency_parser(c);
|
|
|
|
@ -248,51 +253,38 @@
|
|
|
|
|
// buffer.
|
|
|
|
|
|
|
|
|
|
#if ENABLED(SERIAL_XON_XOFF)
|
|
|
|
|
|
|
|
|
|
// If we must do a priority insertion of an XON/XOFF char,
|
|
|
|
|
// do it now
|
|
|
|
|
uint8_t state = xon_xoff_state;
|
|
|
|
|
// Do a priority insertion of an XON/XOFF char, if needed.
|
|
|
|
|
const uint8_t state = xon_xoff_state;
|
|
|
|
|
if (!(state & XON_XOFF_CHAR_SENT)) {
|
|
|
|
|
M_UDRx = state & XON_XOFF_CHAR_MASK;
|
|
|
|
|
xon_xoff_state = state | XON_XOFF_CHAR_SENT;
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// Send the next byte
|
|
|
|
|
const uint8_t t = tx_buffer.tail,
|
|
|
|
|
c = tx_buffer.buffer[t];
|
|
|
|
|
{ // Send the next byte
|
|
|
|
|
const uint8_t t = tx_buffer.tail, c = tx_buffer.buffer[t];
|
|
|
|
|
tx_buffer.tail = (t + 1) & (TX_BUFFER_SIZE - 1);
|
|
|
|
|
|
|
|
|
|
M_UDRx = c;
|
|
|
|
|
|
|
|
|
|
#if ENABLED(SERIAL_XON_XOFF)
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
// Disable interrupts if the buffer is empty
|
|
|
|
|
if (tx_buffer.head == tx_buffer.tail)
|
|
|
|
|
CBI(M_UCSRxB, M_UDRIEx);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef M_USARTx_UDRE_vect
|
|
|
|
|
ISR(M_USARTx_UDRE_vect) {
|
|
|
|
|
_tx_udr_empty_irq();
|
|
|
|
|
}
|
|
|
|
|
ISR(M_USARTx_UDRE_vect) { _tx_udr_empty_irq(); }
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#endif // TX_BUFFER_SIZE
|
|
|
|
|
|
|
|
|
|
#ifdef M_USARTx_RX_vect
|
|
|
|
|
ISR(M_USARTx_RX_vect) {
|
|
|
|
|
store_rxd_char();
|
|
|
|
|
}
|
|
|
|
|
ISR(M_USARTx_RX_vect) { store_rxd_char(); }
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// Public Methods
|
|
|
|
@ -302,9 +294,9 @@
|
|
|
|
|
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.
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
|
@ -363,33 +355,19 @@
|
|
|
|
|
rx_buffer.tail = (ring_buffer_pos_t)(t + 1) & (RX_BUFFER_SIZE - 1);
|
|
|
|
|
|
|
|
|
|
#if ENABLED(SERIAL_XON_XOFF)
|
|
|
|
|
|
|
|
|
|
// for high speed transfers, we can use XON/XOFF protocol to do
|
|
|
|
|
// software handshake and avoid overruns.
|
|
|
|
|
if ((xon_xoff_state & XON_XOFF_CHAR_MASK) == XOFF_CHAR) {
|
|
|
|
|
|
|
|
|
|
// calculate count of bytes stored into the RX buffer
|
|
|
|
|
// Get count of bytes in the RX buffer
|
|
|
|
|
ring_buffer_pos_t rx_count = (ring_buffer_pos_t)(rx_buffer.head - rx_buffer.tail) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1);
|
|
|
|
|
|
|
|
|
|
// if we are below 10% of RX buffer capacity, send XON before
|
|
|
|
|
// we run out of RX buffer bytes
|
|
|
|
|
if (rx_count < (RX_BUFFER_SIZE/10)) {
|
|
|
|
|
|
|
|
|
|
// Send an XON character
|
|
|
|
|
// When below 10% of RX buffer capacity, send XON before
|
|
|
|
|
// running out of RX buffer bytes
|
|
|
|
|
if (rx_count < (RX_BUFFER_SIZE) / 10) {
|
|
|
|
|
xon_xoff_state = XON_CHAR | XON_XOFF_CHAR_SENT;
|
|
|
|
|
|
|
|
|
|
// End critical section
|
|
|
|
|
CRITICAL_SECTION_END;
|
|
|
|
|
|
|
|
|
|
// Transmit the XON character
|
|
|
|
|
CRITICAL_SECTION_END; // End critical section before returning!
|
|
|
|
|
writeNoHandshake(XON_CHAR);
|
|
|
|
|
|
|
|
|
|
// Done
|
|
|
|
|
return v;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
CRITICAL_SECTION_END;
|
|
|
|
|
return v;
|
|
|
|
@ -397,33 +375,22 @@
|
|
|
|
|
|
|
|
|
|
ring_buffer_pos_t MarlinSerial::available(void) {
|
|
|
|
|
CRITICAL_SECTION_START;
|
|
|
|
|
const ring_buffer_pos_t h = rx_buffer.head,
|
|
|
|
|
t = rx_buffer.tail;
|
|
|
|
|
const ring_buffer_pos_t h = rx_buffer.head, t = rx_buffer.tail;
|
|
|
|
|
CRITICAL_SECTION_END;
|
|
|
|
|
return (ring_buffer_pos_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.
|
|
|
|
|
// Don't change this order of operations. If the RX interrupt occurs between
|
|
|
|
|
// reading rx_buffer_head and updating rx_buffer_tail, the previous rx_buffer_head
|
|
|
|
|
// may be written to rx_buffer_tail, making the buffer appear full rather than empty.
|
|
|
|
|
CRITICAL_SECTION_START;
|
|
|
|
|
rx_buffer.head = rx_buffer.tail;
|
|
|
|
|
CRITICAL_SECTION_END;
|
|
|
|
|
|
|
|
|
|
#if ENABLED(SERIAL_XON_XOFF)
|
|
|
|
|
|
|
|
|
|
// for high speed transfers, we can use XON/XOFF protocol to do
|
|
|
|
|
// software handshake and avoid overruns.
|
|
|
|
|
if ((xon_xoff_state & XON_XOFF_CHAR_MASK) == XOFF_CHAR) {
|
|
|
|
|
|
|
|
|
|
// Send an XON character
|
|
|
|
|
xon_xoff_state = XON_CHAR | XON_XOFF_CHAR_SENT;
|
|
|
|
|
|
|
|
|
|
// Transmit the XON character
|
|
|
|
|
writeNoHandshake(XON_CHAR);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
@ -432,19 +399,16 @@
|
|
|
|
|
#if TX_BUFFER_SIZE > 0
|
|
|
|
|
uint8_t MarlinSerial::availableForWrite(void) {
|
|
|
|
|
CRITICAL_SECTION_START;
|
|
|
|
|
const uint8_t h = tx_buffer.head,
|
|
|
|
|
t = tx_buffer.tail;
|
|
|
|
|
const uint8_t h = tx_buffer.head, t = tx_buffer.tail;
|
|
|
|
|
CRITICAL_SECTION_END;
|
|
|
|
|
return (uint8_t)(TX_BUFFER_SIZE + h - t) & (TX_BUFFER_SIZE - 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MarlinSerial::write(const uint8_t c) {
|
|
|
|
|
|
|
|
|
|
#if ENABLED(SERIAL_XON_XOFF)
|
|
|
|
|
uint8_t state = xon_xoff_state;
|
|
|
|
|
const uint8_t state = xon_xoff_state;
|
|
|
|
|
if (!(state & XON_XOFF_CHAR_SENT)) {
|
|
|
|
|
// 2 characters to send: The XON/XOFF character and the user
|
|
|
|
|
// specified char.
|
|
|
|
|
// Send 2 chars: XON/XOFF, then a user-specified char
|
|
|
|
|
writeNoHandshake(state & XON_XOFF_CHAR_MASK);
|
|
|
|
|
xon_xoff_state = state | XON_XOFF_CHAR_SENT;
|
|
|
|
|
}
|
|
|
|
@ -452,8 +416,7 @@
|
|
|
|
|
writeNoHandshake(c);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MarlinSerial::writeNoHandshake(uint8_t c) {
|
|
|
|
|
|
|
|
|
|
void MarlinSerial::writeNoHandshake(const uint8_t c) {
|
|
|
|
|
_written = true;
|
|
|
|
|
CRITICAL_SECTION_START;
|
|
|
|
|
bool emty = (tx_buffer.head == tx_buffer.tail);
|
|
|
|
@ -497,7 +460,6 @@
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void MarlinSerial::flushTX(void) {
|
|
|
|
|
// TX
|
|
|
|
|
// If we have never written a byte, no need to flush. This special
|
|
|
|
@ -518,33 +480,30 @@
|
|
|
|
|
// the hardware finished tranmission (TXC is set).
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
void MarlinSerial::write(uint8_t c) {
|
|
|
|
|
#else // TX_BUFFER_SIZE == 0
|
|
|
|
|
|
|
|
|
|
void MarlinSerial::write(const uint8_t c) {
|
|
|
|
|
#if ENABLED(SERIAL_XON_XOFF)
|
|
|
|
|
// If we must do a priority insertion of an XON/XOFF char, do it now
|
|
|
|
|
uint8_t state = xon_xoff_state;
|
|
|
|
|
// Do a priority insertion of an XON/XOFF char, if needed.
|
|
|
|
|
const uint8_t state = xon_xoff_state;
|
|
|
|
|
if (!(state & XON_XOFF_CHAR_SENT)) {
|
|
|
|
|
|
|
|
|
|
writeNoHandshake(state & XON_XOFF_CHAR_MASK);
|
|
|
|
|
xon_xoff_state = state | XON_XOFF_CHAR_SENT;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
writeNoHandshake(c);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MarlinSerial::writeNoHandshake(uint8_t c) {
|
|
|
|
|
while (!TEST(M_UCSRxA, M_UDREx))
|
|
|
|
|
;
|
|
|
|
|
while (!TEST(M_UCSRxA, M_UDREx)) {/* nada */}
|
|
|
|
|
M_UDRx = c;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// end NEW
|
|
|
|
|
|
|
|
|
|
/// imports from print.h
|
|
|
|
|
#endif // TX_BUFFER_SIZE == 0
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Imports from print.h
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
void MarlinSerial::print(char c, int base) {
|
|
|
|
|
print((long)c, base);
|
|
|
|
|