2016-03-25 07:19:46 +01:00
|
|
|
/**
|
2016-03-24 19:01:20 +01:00
|
|
|
* Marlin 3D Printer Firmware
|
|
|
|
* Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
|
|
|
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-03-25 07:19:46 +01:00
|
|
|
/**
|
2016-07-26 05:06:00 +02:00
|
|
|
* MarlinSerial.cpp - Hardware serial library for Wiring
|
|
|
|
* Copyright (c) 2006 Nicholas Zambetti. All right reserved.
|
|
|
|
*
|
|
|
|
* 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)
|
2017-10-02 07:26:56 +02:00
|
|
|
* Modified 01 October 2017 by Eduardo José Tagle (added XON/XOFF)
|
2016-07-26 05:06:00 +02:00
|
|
|
*/
|
2016-08-03 04:36:58 +02:00
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
// Disable HardwareSerial.cpp to support chips without a UART (Attiny, etc.)
|
2011-11-27 21:12:55 +01:00
|
|
|
|
2017-10-02 07:26:56 +02:00
|
|
|
#include "MarlinConfig.h"
|
|
|
|
|
2018-05-24 12:47:55 +02:00
|
|
|
#if USE_MARLINSERIAL && (defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H))
|
2017-04-02 07:47:20 +02:00
|
|
|
|
2017-10-02 07:26:56 +02:00
|
|
|
#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
|
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
#if UART_PRESENT(SERIAL_PORT)
|
2017-04-02 11:24:28 +02:00
|
|
|
ring_buffer_r rx_buffer = { { 0 }, 0, 0 };
|
2017-04-02 07:47:20 +02:00
|
|
|
#if TX_BUFFER_SIZE > 0
|
2017-04-02 11:24:28 +02:00
|
|
|
ring_buffer_t tx_buffer = { { 0 }, 0, 0 };
|
2017-04-02 07:47:20 +02:00
|
|
|
static bool _written;
|
|
|
|
#endif
|
2016-07-08 17:25:21 +02:00
|
|
|
#endif
|
2011-11-27 21:12:55 +01:00
|
|
|
|
2017-08-08 07:46:37 +02:00
|
|
|
#if ENABLED(SERIAL_XON_XOFF)
|
2017-10-02 07:26:56 +02:00
|
|
|
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;
|
2017-10-04 19:10:15 +02:00
|
|
|
uint8_t xon_xoff_state = XON_XOFF_CHAR_SENT | XON_CHAR;
|
2017-08-08 07:46:37 +02:00
|
|
|
#endif
|
2017-10-02 07:26:56 +02:00
|
|
|
|
2017-08-08 07:46:37 +02:00
|
|
|
#if ENABLED(SERIAL_STATS_DROPPED_RX)
|
2017-10-02 07:26:56 +02:00
|
|
|
uint8_t rx_dropped_bytes = 0;
|
2017-08-08 07:46:37 +02:00
|
|
|
#endif
|
2017-10-02 07:26:56 +02:00
|
|
|
|
2017-08-08 07:46:37 +02:00
|
|
|
#if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
|
2017-10-02 07:26:56 +02:00
|
|
|
ring_buffer_pos_t rx_max_enqueued = 0;
|
|
|
|
#endif
|
2017-08-08 07:46:37 +02:00
|
|
|
|
2018-06-02 02:00:59 +02:00
|
|
|
// A SW memory barrier, to ensure GCC does not overoptimize loops
|
|
|
|
#define sw_barrier() asm volatile("": : :"memory");
|
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
#if ENABLED(EMERGENCY_PARSER)
|
2018-04-26 05:42:43 +02:00
|
|
|
#include "emergency_parser.h"
|
|
|
|
#endif
|
2016-08-19 23:53:20 +02:00
|
|
|
|
2018-06-02 02:00:59 +02:00
|
|
|
// (called with RX interrupts disabled)
|
2017-08-08 07:46:37 +02:00
|
|
|
FORCE_INLINE void store_rxd_char() {
|
|
|
|
const ring_buffer_pos_t h = rx_buffer.head,
|
2017-10-02 07:26:56 +02:00
|
|
|
i = (ring_buffer_pos_t)(h + 1) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1);
|
2017-04-02 07:47:20 +02:00
|
|
|
|
2017-10-02 07:26:56 +02:00
|
|
|
// 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.
|
2017-10-14 22:54:17 +02:00
|
|
|
const char c = M_UDRx;
|
2017-10-02 07:26:56 +02:00
|
|
|
if (i != rx_buffer.tail) {
|
2017-10-14 22:54:17 +02:00
|
|
|
rx_buffer.buffer[h] = c;
|
2017-10-02 07:26:56 +02:00
|
|
|
rx_buffer.head = i;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
#if ENABLED(SERIAL_STATS_DROPPED_RX)
|
|
|
|
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
|
|
|
|
NOLESS(rx_max_enqueued, rx_count);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#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) == XON_CHAR) {
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
// if we are above 12.5% of RX buffer capacity, send XOFF before
|
|
|
|
// 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) {
|
2018-06-02 02:00:59 +02:00
|
|
|
|
2017-10-02 07:26:56 +02:00
|
|
|
// 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)) {
|
2018-06-02 02:00:59 +02:00
|
|
|
|
2017-10-02 07:26:56 +02:00
|
|
|
// Send an XOFF character
|
|
|
|
M_UDRx = XOFF_CHAR;
|
2018-06-02 02:00:59 +02:00
|
|
|
|
2017-10-02 07:26:56 +02:00
|
|
|
// 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);
|
2018-06-02 02:00:59 +02:00
|
|
|
|
2017-10-02 07:26:56 +02:00
|
|
|
// And remember it was sent
|
|
|
|
xon_xoff_state = XOFF_CHAR | XON_XOFF_CHAR_SENT;
|
|
|
|
}
|
|
|
|
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
|
2018-06-02 02:00:59 +02:00
|
|
|
while (!TEST(M_UCSRxA, M_UDREx)) sw_barrier();
|
2017-10-02 07:26:56 +02:00
|
|
|
M_UDRx = XOFF_CHAR;
|
2018-06-02 02:00:59 +02:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
2017-10-02 07:26:56 +02:00
|
|
|
// And remember we already sent it
|
|
|
|
xon_xoff_state = XOFF_CHAR | XON_XOFF_CHAR_SENT;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
2017-04-02 07:47:20 +02:00
|
|
|
}
|
2017-10-02 07:26:56 +02:00
|
|
|
#endif // SERIAL_XON_XOFF
|
2016-07-08 17:25:21 +02:00
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
#if ENABLED(EMERGENCY_PARSER)
|
2018-04-26 05:42:43 +02:00
|
|
|
emergency_parser.update(c);
|
2017-04-02 07:47:20 +02:00
|
|
|
#endif
|
|
|
|
}
|
2016-07-08 17:25:21 +02:00
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
#if TX_BUFFER_SIZE > 0
|
2016-07-08 17:25:21 +02:00
|
|
|
|
2018-06-02 02:00:59 +02:00
|
|
|
// (called with TX irqs disabled)
|
2017-04-02 07:47:20 +02:00
|
|
|
FORCE_INLINE void _tx_udr_empty_irq(void) {
|
|
|
|
// If interrupts are enabled, there must be more data in the output
|
2017-08-08 07:46:37 +02:00
|
|
|
// buffer.
|
|
|
|
|
2017-10-02 07:26:56 +02:00
|
|
|
#if ENABLED(SERIAL_XON_XOFF)
|
|
|
|
// 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
|
|
|
|
#endif
|
|
|
|
{ // 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;
|
|
|
|
}
|
2017-04-02 07:47:20 +02:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
2017-10-02 07:26:56 +02:00
|
|
|
// Disable interrupts if the buffer is empty
|
|
|
|
if (tx_buffer.head == tx_buffer.tail)
|
2017-04-02 07:47:20 +02:00
|
|
|
CBI(M_UCSRxB, M_UDRIEx);
|
2016-07-08 17:25:21 +02:00
|
|
|
}
|
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
#ifdef M_USARTx_UDRE_vect
|
2017-10-02 07:26:56 +02:00
|
|
|
ISR(M_USARTx_UDRE_vect) { _tx_udr_empty_irq(); }
|
2017-04-02 07:47:20 +02:00
|
|
|
#endif
|
2011-11-27 21:12:55 +01:00
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
#endif // TX_BUFFER_SIZE
|
2011-11-27 21:12:55 +01:00
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
#ifdef M_USARTx_RX_vect
|
2017-10-02 07:26:56 +02:00
|
|
|
ISR(M_USARTx_RX_vect) { store_rxd_char(); }
|
2017-04-02 07:47:20 +02:00
|
|
|
#endif
|
2011-11-27 21:12:55 +01:00
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
// Public Methods
|
2011-11-27 21:12:55 +01:00
|
|
|
|
2017-04-02 11:24:28 +02:00
|
|
|
void MarlinSerial::begin(const long baud) {
|
2017-04-02 07:47:20 +02:00
|
|
|
uint16_t baud_setting;
|
|
|
|
bool useU2X = true;
|
2011-11-27 21:12:55 +01:00
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
#if F_CPU == 16000000UL && SERIAL_PORT == 0
|
2017-10-02 07:26:56 +02:00
|
|
|
// 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.
|
2017-04-02 11:24:28 +02:00
|
|
|
if (baud == 57600) useU2X = false;
|
2017-04-02 07:47:20 +02:00
|
|
|
#endif
|
2011-11-27 21:12:55 +01:00
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
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;
|
2015-01-24 06:11:50 +01:00
|
|
|
}
|
2015-10-03 08:08:58 +02:00
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
// assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
|
|
|
|
M_UBRRxH = baud_setting >> 8;
|
|
|
|
M_UBRRxL = baud_setting;
|
2011-11-27 21:12:55 +01:00
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
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
|
|
|
|
}
|
2011-11-27 21:12:55 +01:00
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
void MarlinSerial::end() {
|
|
|
|
CBI(M_UCSRxB, M_RXENx);
|
|
|
|
CBI(M_UCSRxB, M_TXENx);
|
|
|
|
CBI(M_UCSRxB, M_RXCIEx);
|
2016-07-08 17:25:21 +02:00
|
|
|
CBI(M_UCSRxB, M_UDRIEx);
|
|
|
|
}
|
2017-04-02 07:47:20 +02:00
|
|
|
|
|
|
|
int MarlinSerial::peek(void) {
|
2018-06-02 02:00:59 +02:00
|
|
|
#if RX_BUFFER_SIZE > 256
|
|
|
|
// Disable RX interrupts, but only if non atomic reads
|
|
|
|
const bool isr_enabled = TEST(M_UCSRxB, M_RXCIEx);
|
|
|
|
CBI(M_UCSRxB, M_RXCIEx);
|
|
|
|
#endif
|
2017-04-02 11:24:28 +02:00
|
|
|
const int v = rx_buffer.head == rx_buffer.tail ? -1 : rx_buffer.buffer[rx_buffer.tail];
|
2018-06-02 02:00:59 +02:00
|
|
|
#if RX_BUFFER_SIZE > 256
|
|
|
|
// Reenable RX interrupts if they were enabled
|
|
|
|
if (isr_enabled) SBI(M_UCSRxB, M_RXCIEx);
|
|
|
|
#endif
|
2017-04-02 07:47:20 +02:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
int MarlinSerial::read(void) {
|
|
|
|
int v;
|
2018-06-02 02:00:59 +02:00
|
|
|
|
|
|
|
#if RX_BUFFER_SIZE > 256
|
|
|
|
// Disable RX interrupts to ensure atomic reads
|
|
|
|
const bool isr_enabled = TEST(M_UCSRxB, M_RXCIEx);
|
|
|
|
CBI(M_UCSRxB, M_RXCIEx);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const ring_buffer_pos_t h = rx_buffer.head;
|
|
|
|
|
|
|
|
#if RX_BUFFER_SIZE > 256
|
|
|
|
// End critical section
|
|
|
|
if (isr_enabled) SBI(M_UCSRxB, M_RXCIEx);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ring_buffer_pos_t t = rx_buffer.tail;
|
|
|
|
|
|
|
|
if (h == t)
|
|
|
|
v = -1;
|
|
|
|
else {
|
|
|
|
v = rx_buffer.buffer[t];
|
|
|
|
t = (ring_buffer_pos_t)(t + 1) & (RX_BUFFER_SIZE - 1);
|
|
|
|
|
|
|
|
#if RX_BUFFER_SIZE > 256
|
|
|
|
// Disable RX interrupts to ensure atomic write to tail, so
|
|
|
|
// the RX isr can't read partially updated values
|
|
|
|
const bool isr_enabled = TEST(M_UCSRxB, M_RXCIEx);
|
|
|
|
CBI(M_UCSRxB, M_RXCIEx);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Advance tail
|
|
|
|
rx_buffer.tail = t;
|
|
|
|
|
|
|
|
#if RX_BUFFER_SIZE > 256
|
|
|
|
// End critical section
|
|
|
|
if (isr_enabled) SBI(M_UCSRxB, M_RXCIEx);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if ENABLED(SERIAL_XON_XOFF)
|
|
|
|
if ((xon_xoff_state & XON_XOFF_CHAR_MASK) == XOFF_CHAR) {
|
|
|
|
|
|
|
|
// Get count of bytes in the RX buffer
|
|
|
|
ring_buffer_pos_t rx_count = (ring_buffer_pos_t)(h - t) & (ring_buffer_pos_t)(RX_BUFFER_SIZE - 1);
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
write(XON_CHAR);
|
|
|
|
return v;
|
2017-10-02 07:26:56 +02:00
|
|
|
}
|
2018-06-02 02:00:59 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
return v;
|
2016-07-08 17:25:21 +02:00
|
|
|
}
|
|
|
|
|
2017-08-08 07:46:37 +02:00
|
|
|
ring_buffer_pos_t MarlinSerial::available(void) {
|
2018-06-02 02:00:59 +02:00
|
|
|
#if RX_BUFFER_SIZE > 256
|
|
|
|
const bool isr_enabled = TEST(M_UCSRxB, M_RXCIEx);
|
|
|
|
CBI(M_UCSRxB, M_RXCIEx);
|
|
|
|
#endif
|
|
|
|
|
2017-10-02 07:26:56 +02:00
|
|
|
const ring_buffer_pos_t h = rx_buffer.head, t = rx_buffer.tail;
|
2018-06-02 02:00:59 +02:00
|
|
|
|
|
|
|
#if RX_BUFFER_SIZE > 256
|
|
|
|
if (isr_enabled) SBI(M_UCSRxB, M_RXCIEx);
|
|
|
|
#endif
|
|
|
|
|
2017-08-08 07:46:37 +02:00
|
|
|
return (ring_buffer_pos_t)(RX_BUFFER_SIZE + h - t) & (RX_BUFFER_SIZE - 1);
|
2017-04-02 07:47:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MarlinSerial::flush(void) {
|
2018-06-02 02:00:59 +02:00
|
|
|
#if RX_BUFFER_SIZE > 256
|
|
|
|
const bool isr_enabled = TEST(M_UCSRxB, M_RXCIEx);
|
|
|
|
CBI(M_UCSRxB, M_RXCIEx);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
rx_buffer.tail = rx_buffer.head;
|
|
|
|
|
|
|
|
#if RX_BUFFER_SIZE > 256
|
|
|
|
if (isr_enabled) SBI(M_UCSRxB, M_RXCIEx);
|
|
|
|
#endif
|
2017-10-02 07:26:56 +02:00
|
|
|
|
|
|
|
#if ENABLED(SERIAL_XON_XOFF)
|
|
|
|
if ((xon_xoff_state & XON_XOFF_CHAR_MASK) == XOFF_CHAR) {
|
|
|
|
xon_xoff_state = XON_CHAR | XON_XOFF_CHAR_SENT;
|
2018-06-02 02:00:59 +02:00
|
|
|
write(XON_CHAR);
|
2017-10-02 07:26:56 +02:00
|
|
|
}
|
|
|
|
#endif
|
2017-04-02 07:47:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#if TX_BUFFER_SIZE > 0
|
2017-04-02 11:24:28 +02:00
|
|
|
void MarlinSerial::write(const uint8_t c) {
|
2017-04-02 07:47:20 +02:00
|
|
|
_written = true;
|
2018-06-02 02:00:59 +02:00
|
|
|
|
|
|
|
// If the TX interrupts are disabled 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 (!TEST(M_UCSRxB, M_UDRIEx) && TEST(M_UCSRxA, M_UDREx)) {
|
|
|
|
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);
|
2017-04-02 07:47:20 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-06-02 02:00:59 +02:00
|
|
|
|
2017-04-02 11:24:28 +02:00
|
|
|
const uint8_t i = (tx_buffer.head + 1) & (TX_BUFFER_SIZE - 1);
|
2017-04-02 07:47:20 +02:00
|
|
|
|
|
|
|
// 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) {
|
2018-06-02 02:00:59 +02:00
|
|
|
if (!ISRS_ENABLED()) {
|
2017-04-02 07:47:20 +02:00
|
|
|
// 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();
|
2017-06-10 23:39:48 +02:00
|
|
|
}
|
2018-06-02 02:00:59 +02:00
|
|
|
// (else , the interrupt handler will free up space for us)
|
|
|
|
|
|
|
|
// Make sure compiler rereads tx_buffer.tail
|
|
|
|
sw_barrier();
|
2017-04-02 07:47:20 +02:00
|
|
|
}
|
2016-07-08 17:25:21 +02:00
|
|
|
|
2018-06-02 02:00:59 +02:00
|
|
|
// Store new char. head is always safe to move
|
2017-04-02 07:47:20 +02:00
|
|
|
tx_buffer.buffer[tx_buffer.head] = c;
|
2018-06-02 02:00:59 +02:00
|
|
|
tx_buffer.head = i;
|
|
|
|
|
|
|
|
// Enable TX isr
|
|
|
|
SBI(M_UCSRxB, M_UDRIEx);
|
2016-07-08 17:25:21 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-04-02 07:47:20 +02:00
|
|
|
|
|
|
|
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)) {
|
2018-06-02 02:00:59 +02:00
|
|
|
if (!ISRS_ENABLED()) {
|
2017-04-02 07:47:20 +02:00
|
|
|
// 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();
|
2018-06-02 02:00:59 +02:00
|
|
|
}
|
|
|
|
sw_barrier();
|
2017-04-02 07:47:20 +02:00
|
|
|
}
|
|
|
|
// If we get here, nothing is queued anymore (DRIE is disabled) and
|
2018-06-02 02:00:59 +02:00
|
|
|
// the hardware finished transmission (TXC is set).
|
2017-10-02 07:26:56 +02:00
|
|
|
}
|
2016-07-08 17:25:21 +02:00
|
|
|
|
2017-10-02 07:26:56 +02:00
|
|
|
#else // TX_BUFFER_SIZE == 0
|
|
|
|
|
|
|
|
void MarlinSerial::write(const uint8_t c) {
|
2018-06-02 02:00:59 +02:00
|
|
|
while (!TEST(M_UCSRxA, M_UDREx)) sw_barrier();
|
2017-04-02 07:47:20 +02:00
|
|
|
M_UDRx = c;
|
|
|
|
}
|
2011-11-27 21:12:55 +01:00
|
|
|
|
2017-10-02 07:26:56 +02:00
|
|
|
#endif // TX_BUFFER_SIZE == 0
|
2011-11-27 21:12:55 +01:00
|
|
|
|
2017-10-02 07:26:56 +02:00
|
|
|
/**
|
|
|
|
* Imports from print.h
|
|
|
|
*/
|
2011-11-28 19:13:40 +01:00
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
void MarlinSerial::print(char c, int base) {
|
2017-04-02 11:24:28 +02:00
|
|
|
print((long)c, base);
|
2017-04-02 07:47:20 +02:00
|
|
|
}
|
2011-11-28 19:13:40 +01:00
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
void MarlinSerial::print(unsigned char b, int base) {
|
2017-04-02 11:24:28 +02:00
|
|
|
print((unsigned long)b, base);
|
2017-04-02 07:47:20 +02:00
|
|
|
}
|
2011-11-28 19:13:40 +01:00
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
void MarlinSerial::print(int n, int base) {
|
2017-04-02 11:24:28 +02:00
|
|
|
print((long)n, base);
|
2017-04-02 07:47:20 +02:00
|
|
|
}
|
2011-11-28 19:13:40 +01:00
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
void MarlinSerial::print(unsigned int n, int base) {
|
2017-04-02 11:24:28 +02:00
|
|
|
print((unsigned long)n, base);
|
2015-01-24 06:11:50 +01:00
|
|
|
}
|
2017-04-02 07:47:20 +02:00
|
|
|
|
|
|
|
void MarlinSerial::print(long n, int base) {
|
2017-04-02 11:24:28 +02:00
|
|
|
if (base == 0)
|
2017-04-02 07:47:20 +02:00
|
|
|
write(n);
|
|
|
|
else if (base == 10) {
|
|
|
|
if (n < 0) {
|
|
|
|
print('-');
|
|
|
|
n = -n;
|
|
|
|
}
|
|
|
|
printNumber(n, 10);
|
|
|
|
}
|
2017-04-02 11:24:28 +02:00
|
|
|
else
|
2017-04-02 07:47:20 +02:00
|
|
|
printNumber(n, base);
|
2015-10-03 08:08:58 +02:00
|
|
|
}
|
2017-04-02 07:47:20 +02:00
|
|
|
|
|
|
|
void MarlinSerial::print(unsigned long n, int base) {
|
|
|
|
if (base == 0) write(n);
|
|
|
|
else printNumber(n, base);
|
2011-11-28 19:13:40 +01:00
|
|
|
}
|
2017-04-02 07:47:20 +02:00
|
|
|
|
|
|
|
void MarlinSerial::print(double n, int digits) {
|
|
|
|
printFloat(n, digits);
|
2011-11-28 19:13:40 +01:00
|
|
|
}
|
2017-04-02 07:47:20 +02:00
|
|
|
|
|
|
|
void MarlinSerial::println(void) {
|
|
|
|
print('\r');
|
|
|
|
print('\n');
|
2011-11-28 19:13:40 +01:00
|
|
|
}
|
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
void MarlinSerial::println(const String& s) {
|
|
|
|
print(s);
|
|
|
|
println();
|
2015-10-03 08:08:58 +02:00
|
|
|
}
|
2011-11-27 21:12:55 +01:00
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
void MarlinSerial::println(const char c[]) {
|
|
|
|
print(c);
|
|
|
|
println();
|
|
|
|
}
|
2011-11-27 21:12:55 +01:00
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
void MarlinSerial::println(char c, int base) {
|
|
|
|
print(c, base);
|
|
|
|
println();
|
|
|
|
}
|
2011-11-27 21:12:55 +01:00
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
void MarlinSerial::println(unsigned char b, int base) {
|
|
|
|
print(b, base);
|
|
|
|
println();
|
|
|
|
}
|
2013-10-30 11:45:32 +01:00
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
void MarlinSerial::println(int n, int base) {
|
|
|
|
print(n, base);
|
|
|
|
println();
|
|
|
|
}
|
Add an emergency-command parser to MarlinSerial (supporting M108)
Add an emergency-command parser to MarlinSerial's RX interrupt.
The parser tries to find and execute M108,M112,M410 before the commands disappear in the RX-buffer.
To avoid false positives for M117, comments and commands followed by filenames (M23, M28, M30, M32, M33) are filtered.
This enables Marlin to receive and react on the Emergency command at all times - regardless of whether the buffers are full or not. It remains to convince hosts to send the commands. To inform the hosts about the new feature a new entry in the M115-report was made. "`EMERGENCY_CODES:M112,M108,M410;`".
The parser is fast. It only ever needs two switch decisions and one assignment of the new state for every character.
One problem remains. If the host has sent an incomplete line before sending an emergency command the emergency command could be omitted when the parser is in `state_IGNORE`.
In that case the host should send "\ncommand\n"
Also introduces M108 to break the waiting for the heaters in M109, M190 and M303.
Rename `cancel_heatup` to `wait_for_heatup` to better see the purpose.
2016-07-04 23:23:22 +02:00
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
void MarlinSerial::println(unsigned int n, int base) {
|
|
|
|
print(n, base);
|
|
|
|
println();
|
|
|
|
}
|
Add an emergency-command parser to MarlinSerial (supporting M108)
Add an emergency-command parser to MarlinSerial's RX interrupt.
The parser tries to find and execute M108,M112,M410 before the commands disappear in the RX-buffer.
To avoid false positives for M117, comments and commands followed by filenames (M23, M28, M30, M32, M33) are filtered.
This enables Marlin to receive and react on the Emergency command at all times - regardless of whether the buffers are full or not. It remains to convince hosts to send the commands. To inform the hosts about the new feature a new entry in the M115-report was made. "`EMERGENCY_CODES:M112,M108,M410;`".
The parser is fast. It only ever needs two switch decisions and one assignment of the new state for every character.
One problem remains. If the host has sent an incomplete line before sending an emergency command the emergency command could be omitted when the parser is in `state_IGNORE`.
In that case the host should send "\ncommand\n"
Also introduces M108 to break the waiting for the heaters in M109, M190 and M303.
Rename `cancel_heatup` to `wait_for_heatup` to better see the purpose.
2016-07-04 23:23:22 +02:00
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
void MarlinSerial::println(long n, int base) {
|
|
|
|
print(n, base);
|
|
|
|
println();
|
|
|
|
}
|
Add an emergency-command parser to MarlinSerial (supporting M108)
Add an emergency-command parser to MarlinSerial's RX interrupt.
The parser tries to find and execute M108,M112,M410 before the commands disappear in the RX-buffer.
To avoid false positives for M117, comments and commands followed by filenames (M23, M28, M30, M32, M33) are filtered.
This enables Marlin to receive and react on the Emergency command at all times - regardless of whether the buffers are full or not. It remains to convince hosts to send the commands. To inform the hosts about the new feature a new entry in the M115-report was made. "`EMERGENCY_CODES:M112,M108,M410;`".
The parser is fast. It only ever needs two switch decisions and one assignment of the new state for every character.
One problem remains. If the host has sent an incomplete line before sending an emergency command the emergency command could be omitted when the parser is in `state_IGNORE`.
In that case the host should send "\ncommand\n"
Also introduces M108 to break the waiting for the heaters in M109, M190 and M303.
Rename `cancel_heatup` to `wait_for_heatup` to better see the purpose.
2016-07-04 23:23:22 +02:00
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
void MarlinSerial::println(unsigned long n, int base) {
|
|
|
|
print(n, base);
|
|
|
|
println();
|
|
|
|
}
|
Add an emergency-command parser to MarlinSerial (supporting M108)
Add an emergency-command parser to MarlinSerial's RX interrupt.
The parser tries to find and execute M108,M112,M410 before the commands disappear in the RX-buffer.
To avoid false positives for M117, comments and commands followed by filenames (M23, M28, M30, M32, M33) are filtered.
This enables Marlin to receive and react on the Emergency command at all times - regardless of whether the buffers are full or not. It remains to convince hosts to send the commands. To inform the hosts about the new feature a new entry in the M115-report was made. "`EMERGENCY_CODES:M112,M108,M410;`".
The parser is fast. It only ever needs two switch decisions and one assignment of the new state for every character.
One problem remains. If the host has sent an incomplete line before sending an emergency command the emergency command could be omitted when the parser is in `state_IGNORE`.
In that case the host should send "\ncommand\n"
Also introduces M108 to break the waiting for the heaters in M109, M190 and M303.
Rename `cancel_heatup` to `wait_for_heatup` to better see the purpose.
2016-07-04 23:23:22 +02:00
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
void MarlinSerial::println(double n, int digits) {
|
|
|
|
print(n, digits);
|
|
|
|
println();
|
|
|
|
}
|
Add an emergency-command parser to MarlinSerial (supporting M108)
Add an emergency-command parser to MarlinSerial's RX interrupt.
The parser tries to find and execute M108,M112,M410 before the commands disappear in the RX-buffer.
To avoid false positives for M117, comments and commands followed by filenames (M23, M28, M30, M32, M33) are filtered.
This enables Marlin to receive and react on the Emergency command at all times - regardless of whether the buffers are full or not. It remains to convince hosts to send the commands. To inform the hosts about the new feature a new entry in the M115-report was made. "`EMERGENCY_CODES:M112,M108,M410;`".
The parser is fast. It only ever needs two switch decisions and one assignment of the new state for every character.
One problem remains. If the host has sent an incomplete line before sending an emergency command the emergency command could be omitted when the parser is in `state_IGNORE`.
In that case the host should send "\ncommand\n"
Also introduces M108 to break the waiting for the heaters in M109, M190 and M303.
Rename `cancel_heatup` to `wait_for_heatup` to better see the purpose.
2016-07-04 23:23:22 +02:00
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
// Private Methods
|
Add an emergency-command parser to MarlinSerial (supporting M108)
Add an emergency-command parser to MarlinSerial's RX interrupt.
The parser tries to find and execute M108,M112,M410 before the commands disappear in the RX-buffer.
To avoid false positives for M117, comments and commands followed by filenames (M23, M28, M30, M32, M33) are filtered.
This enables Marlin to receive and react on the Emergency command at all times - regardless of whether the buffers are full or not. It remains to convince hosts to send the commands. To inform the hosts about the new feature a new entry in the M115-report was made. "`EMERGENCY_CODES:M112,M108,M410;`".
The parser is fast. It only ever needs two switch decisions and one assignment of the new state for every character.
One problem remains. If the host has sent an incomplete line before sending an emergency command the emergency command could be omitted when the parser is in `state_IGNORE`.
In that case the host should send "\ncommand\n"
Also introduces M108 to break the waiting for the heaters in M109, M190 and M303.
Rename `cancel_heatup` to `wait_for_heatup` to better see the purpose.
2016-07-04 23:23:22 +02:00
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
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)));
|
Add an emergency-command parser to MarlinSerial (supporting M108)
Add an emergency-command parser to MarlinSerial's RX interrupt.
The parser tries to find and execute M108,M112,M410 before the commands disappear in the RX-buffer.
To avoid false positives for M117, comments and commands followed by filenames (M23, M28, M30, M32, M33) are filtered.
This enables Marlin to receive and react on the Emergency command at all times - regardless of whether the buffers are full or not. It remains to convince hosts to send the commands. To inform the hosts about the new feature a new entry in the M115-report was made. "`EMERGENCY_CODES:M112,M108,M410;`".
The parser is fast. It only ever needs two switch decisions and one assignment of the new state for every character.
One problem remains. If the host has sent an incomplete line before sending an emergency command the emergency command could be omitted when the parser is in `state_IGNORE`.
In that case the host should send "\ncommand\n"
Also introduces M108 to break the waiting for the heaters in M109, M190 and M303.
Rename `cancel_heatup` to `wait_for_heatup` to better see the purpose.
2016-07-04 23:23:22 +02:00
|
|
|
}
|
2017-04-02 07:47:20 +02:00
|
|
|
else
|
|
|
|
print('0');
|
Add an emergency-command parser to MarlinSerial (supporting M108)
Add an emergency-command parser to MarlinSerial's RX interrupt.
The parser tries to find and execute M108,M112,M410 before the commands disappear in the RX-buffer.
To avoid false positives for M117, comments and commands followed by filenames (M23, M28, M30, M32, M33) are filtered.
This enables Marlin to receive and react on the Emergency command at all times - regardless of whether the buffers are full or not. It remains to convince hosts to send the commands. To inform the hosts about the new feature a new entry in the M115-report was made. "`EMERGENCY_CODES:M112,M108,M410;`".
The parser is fast. It only ever needs two switch decisions and one assignment of the new state for every character.
One problem remains. If the host has sent an incomplete line before sending an emergency command the emergency command could be omitted when the parser is in `state_IGNORE`.
In that case the host should send "\ncommand\n"
Also introduces M108 to break the waiting for the heaters in M109, M190 and M303.
Rename `cancel_heatup` to `wait_for_heatup` to better see the purpose.
2016-07-04 23:23:22 +02:00
|
|
|
}
|
2016-08-19 23:53:20 +02:00
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
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;
|
|
|
|
|
2018-05-24 12:47:55 +02:00
|
|
|
#endif // USE_MARLINSERIAL && (UBRRH || UBRR0H || UBRR1H || UBRR2H || UBRR3H)
|
2017-04-02 07:47:20 +02:00
|
|
|
|
|
|
|
// For AT90USB targets use the UART for BT interfacing
|
2018-05-24 12:47:55 +02:00
|
|
|
#if !USE_MARLINSERIAL && ENABLED(BLUETOOTH)
|
2017-04-02 07:47:20 +02:00
|
|
|
HardwareSerial bluetoothSerial;
|
Add an emergency-command parser to MarlinSerial (supporting M108)
Add an emergency-command parser to MarlinSerial's RX interrupt.
The parser tries to find and execute M108,M112,M410 before the commands disappear in the RX-buffer.
To avoid false positives for M117, comments and commands followed by filenames (M23, M28, M30, M32, M33) are filtered.
This enables Marlin to receive and react on the Emergency command at all times - regardless of whether the buffers are full or not. It remains to convince hosts to send the commands. To inform the hosts about the new feature a new entry in the M115-report was made. "`EMERGENCY_CODES:M112,M108,M410;`".
The parser is fast. It only ever needs two switch decisions and one assignment of the new state for every character.
One problem remains. If the host has sent an incomplete line before sending an emergency command the emergency command could be omitted when the parser is in `state_IGNORE`.
In that case the host should send "\ncommand\n"
Also introduces M108 to break the waiting for the heaters in M109, M190 and M303.
Rename `cancel_heatup` to `wait_for_heatup` to better see the purpose.
2016-07-04 23:23:22 +02:00
|
|
|
#endif
|