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 09:47:30 +02:00
|
|
|
* Modified 01 October 2017 by Eduardo José Tagle (added XON/XOFF)
|
2018-06-11 03:32:20 +02:00
|
|
|
* Modified 10 June 2018 by Eduardo José Tagle (See #10991)
|
2018-10-03 05:15:30 +02:00
|
|
|
* Templatized 01 October 2018 by Eduardo José Tagle to allow multiple instances
|
2016-07-26 05:06:00 +02:00
|
|
|
*/
|
2018-06-11 03:32:20 +02:00
|
|
|
|
2017-09-24 06:25:28 +02:00
|
|
|
#ifdef __AVR__
|
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 09:47:30 +02:00
|
|
|
#include "../../inc/MarlinConfig.h"
|
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
#if !defined(USBCON) && (defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H))
|
|
|
|
|
2017-10-02 09:47:30 +02:00
|
|
|
#include "MarlinSerial.h"
|
|
|
|
#include "../../Marlin.h"
|
|
|
|
|
2018-10-26 10:53:06 +02:00
|
|
|
template<typename Cfg> typename MarlinSerial<Cfg>::ring_buffer_r MarlinSerial<Cfg>::rx_buffer = { 0, 0, { 0 } };
|
2018-10-03 05:15:30 +02:00
|
|
|
template<typename Cfg> typename MarlinSerial<Cfg>::ring_buffer_t MarlinSerial<Cfg>::tx_buffer = { 0 };
|
|
|
|
template<typename Cfg> bool MarlinSerial<Cfg>::_written = false;
|
|
|
|
template<typename Cfg> uint8_t MarlinSerial<Cfg>::xon_xoff_state = MarlinSerial<Cfg>::XON_XOFF_CHAR_SENT | MarlinSerial<Cfg>::XON_CHAR;
|
|
|
|
template<typename Cfg> uint8_t MarlinSerial<Cfg>::rx_dropped_bytes = 0;
|
|
|
|
template<typename Cfg> uint8_t MarlinSerial<Cfg>::rx_buffer_overruns = 0;
|
|
|
|
template<typename Cfg> uint8_t MarlinSerial<Cfg>::rx_framing_errors = 0;
|
|
|
|
template<typename Cfg> typename MarlinSerial<Cfg>::ring_buffer_pos_t MarlinSerial<Cfg>::rx_max_enqueued = 0;
|
2017-10-02 09:47:30 +02:00
|
|
|
|
2018-06-02 02:02:22 +02:00
|
|
|
// A SW memory barrier, to ensure GCC does not overoptimize loops
|
|
|
|
#define sw_barrier() asm volatile("": : :"memory");
|
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
#include "../../feature/emergency_parser.h"
|
2016-08-19 23:53:20 +02:00
|
|
|
|
2018-06-11 03:32:20 +02:00
|
|
|
// "Atomically" read the RX head index value without disabling interrupts:
|
|
|
|
// This MUST be called with RX interrupts enabled, and CAN'T be called
|
|
|
|
// from the RX ISR itself!
|
2018-10-03 05:15:30 +02:00
|
|
|
template<typename Cfg>
|
|
|
|
FORCE_INLINE typename MarlinSerial<Cfg>::ring_buffer_pos_t MarlinSerial<Cfg>::atomic_read_rx_head() {
|
|
|
|
if (Cfg::RX_SIZE > 256) {
|
2018-06-11 03:32:20 +02:00
|
|
|
// Keep reading until 2 consecutive reads return the same value,
|
|
|
|
// meaning there was no update in-between caused by an interrupt.
|
|
|
|
// This works because serial RX interrupts happen at a slower rate
|
|
|
|
// than successive reads of a variable, so 2 consecutive reads with
|
|
|
|
// the same value means no interrupt updated it.
|
|
|
|
ring_buffer_pos_t vold, vnew = rx_buffer.head;
|
|
|
|
sw_barrier();
|
|
|
|
do {
|
|
|
|
vold = vnew;
|
|
|
|
vnew = rx_buffer.head;
|
|
|
|
sw_barrier();
|
|
|
|
} while (vold != vnew);
|
|
|
|
return vnew;
|
2018-10-03 05:15:30 +02:00
|
|
|
}
|
|
|
|
else {
|
2018-06-11 03:32:20 +02:00
|
|
|
// With an 8bit index, reads are always atomic. No need for special handling
|
|
|
|
return rx_buffer.head;
|
2018-10-03 05:15:30 +02:00
|
|
|
}
|
2018-06-11 03:32:20 +02:00
|
|
|
}
|
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
template<typename Cfg>
|
|
|
|
volatile bool MarlinSerial<Cfg>::rx_tail_value_not_stable = false;
|
|
|
|
template<typename Cfg>
|
|
|
|
volatile uint16_t MarlinSerial<Cfg>::rx_tail_value_backup = 0;
|
2018-06-11 03:32:20 +02:00
|
|
|
|
|
|
|
// Set RX tail index, taking into account the RX ISR could interrupt
|
|
|
|
// the write to this variable in the middle - So a backup strategy
|
|
|
|
// is used to ensure reads of the correct values.
|
|
|
|
// -Must NOT be called from the RX ISR -
|
2018-10-03 05:15:30 +02:00
|
|
|
template<typename Cfg>
|
|
|
|
FORCE_INLINE void MarlinSerial<Cfg>::atomic_set_rx_tail(typename MarlinSerial<Cfg>::ring_buffer_pos_t value) {
|
|
|
|
if (Cfg::RX_SIZE > 256) {
|
2018-06-11 03:32:20 +02:00
|
|
|
// Store the new value in the backup
|
|
|
|
rx_tail_value_backup = value;
|
|
|
|
sw_barrier();
|
|
|
|
// Flag we are about to change the true value
|
|
|
|
rx_tail_value_not_stable = true;
|
|
|
|
sw_barrier();
|
|
|
|
// Store the new value
|
|
|
|
rx_buffer.tail = value;
|
|
|
|
sw_barrier();
|
|
|
|
// Signal the new value is completely stored into the value
|
|
|
|
rx_tail_value_not_stable = false;
|
|
|
|
sw_barrier();
|
2018-10-03 05:15:30 +02:00
|
|
|
}
|
|
|
|
else
|
2018-06-11 03:32:20 +02:00
|
|
|
rx_buffer.tail = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the RX tail index, taking into account the read could be
|
|
|
|
// interrupting in the middle of the update of that index value
|
|
|
|
// -Called from the RX ISR -
|
2018-10-03 05:15:30 +02:00
|
|
|
template<typename Cfg>
|
|
|
|
FORCE_INLINE typename MarlinSerial<Cfg>::ring_buffer_pos_t MarlinSerial<Cfg>::atomic_read_rx_tail() {
|
|
|
|
if (Cfg::RX_SIZE > 256) {
|
2018-06-11 03:32:20 +02:00
|
|
|
// If the true index is being modified, return the backup value
|
|
|
|
if (rx_tail_value_not_stable) return rx_tail_value_backup;
|
2018-10-03 05:15:30 +02:00
|
|
|
}
|
2018-06-11 03:32:20 +02:00
|
|
|
// The true index is stable, return it
|
|
|
|
return rx_buffer.tail;
|
|
|
|
}
|
|
|
|
|
2018-06-02 02:02:22 +02:00
|
|
|
// (called with RX interrupts disabled)
|
2018-10-03 05:15:30 +02:00
|
|
|
template<typename Cfg>
|
|
|
|
FORCE_INLINE void MarlinSerial<Cfg>::store_rxd_char() {
|
2017-10-11 08:28:52 +02:00
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
static EmergencyParser::State emergency_state; // = EP_RESET
|
2018-04-26 03:58:00 +02:00
|
|
|
|
2018-06-11 03:32:20 +02:00
|
|
|
// Get the tail - Nothing can alter its value while this ISR is executing, but there's
|
|
|
|
// a chance that this ISR interrupted the main process while it was updating the index.
|
|
|
|
// The backup mechanism ensures the correct value is always returned.
|
|
|
|
const ring_buffer_pos_t t = atomic_read_rx_tail();
|
2017-10-02 09:47:30 +02:00
|
|
|
|
2018-06-11 03:32:20 +02:00
|
|
|
// Get the head pointer - This ISR is the only one that modifies its value, so it's safe to read here
|
2018-06-06 02:12:08 +02:00
|
|
|
ring_buffer_pos_t h = rx_buffer.head;
|
|
|
|
|
|
|
|
// Get the next element
|
2018-10-03 05:15:30 +02:00
|
|
|
ring_buffer_pos_t i = (ring_buffer_pos_t)(h + 1) & (ring_buffer_pos_t)(Cfg::RX_SIZE - 1);
|
2018-06-06 02:12:08 +02:00
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
// This must read the R_UCSRA register before reading the received byte to detect error causes
|
|
|
|
if (Cfg::DROPPED_RX && B_DOR && !++rx_dropped_bytes) --rx_dropped_bytes;
|
|
|
|
if (Cfg::RX_OVERRUNS && B_DOR && !++rx_buffer_overruns) --rx_buffer_overruns;
|
|
|
|
if (Cfg::RX_FRAMING_ERRORS && B_FE && !++rx_framing_errors) --rx_framing_errors;
|
2018-06-10 02:07:06 +02:00
|
|
|
|
2018-06-06 02:12:08 +02:00
|
|
|
// Read the character from the USART
|
2018-10-03 05:15:30 +02:00
|
|
|
uint8_t c = R_UDR;
|
2018-06-06 02:12:08 +02:00
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
if (Cfg::EMERGENCYPARSER) emergency_parser.update(emergency_state, c);
|
2017-10-11 08:28:52 +02:00
|
|
|
|
2017-10-02 09:47:30 +02:00
|
|
|
// If the character is to be stored at the index just before the tail
|
2018-06-06 02:12:08 +02:00
|
|
|
// (such that the head would advance to the current tail), the RX FIFO is
|
|
|
|
// full, so don't write the character or advance the head.
|
|
|
|
if (i != t) {
|
2017-10-08 23:16:35 +02:00
|
|
|
rx_buffer.buffer[h] = c;
|
2018-06-06 02:12:08 +02:00
|
|
|
h = i;
|
2017-10-02 09:47:30 +02:00
|
|
|
}
|
2018-10-03 05:15:30 +02:00
|
|
|
else if (Cfg::DROPPED_RX && !++rx_dropped_bytes)
|
|
|
|
--rx_dropped_bytes;
|
2017-10-02 09:47:30 +02:00
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
if (Cfg::MAX_RX_QUEUED) {
|
2018-06-06 02:12:08 +02:00
|
|
|
// Calculate count of bytes stored into the RX buffer
|
2018-10-03 05:15:30 +02:00
|
|
|
const ring_buffer_pos_t rx_count = (ring_buffer_pos_t)(h - t) & (ring_buffer_pos_t)(Cfg::RX_SIZE - 1);
|
2018-06-06 02:12:08 +02:00
|
|
|
|
2017-10-02 09:47:30 +02:00
|
|
|
// Keep track of the maximum count of enqueued bytes
|
|
|
|
NOLESS(rx_max_enqueued, rx_count);
|
2018-10-03 05:15:30 +02:00
|
|
|
}
|
2017-10-02 09:47:30 +02:00
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
if (Cfg::XONOFF) {
|
2018-06-06 02:12:08 +02:00
|
|
|
// If the last char that was sent was an XON
|
2017-10-02 09:47:30 +02:00
|
|
|
if ((xon_xoff_state & XON_XOFF_CHAR_MASK) == XON_CHAR) {
|
|
|
|
|
2018-06-06 02:12:08 +02:00
|
|
|
// Bytes stored into the RX buffer
|
2018-10-03 05:15:30 +02:00
|
|
|
const ring_buffer_pos_t rx_count = (ring_buffer_pos_t)(h - t) & (ring_buffer_pos_t)(Cfg::RX_SIZE - 1);
|
2017-10-02 09:47:30 +02:00
|
|
|
|
2018-06-06 02:12:08 +02:00
|
|
|
// If over 12.5% of RX buffer capacity, send XOFF before running out of
|
|
|
|
// RX buffer space .. 325 bytes @ 250kbits/s needed to let the host react
|
|
|
|
// and stop sending bytes. This translates to 13mS propagation time.
|
2018-10-03 05:15:30 +02:00
|
|
|
if (rx_count >= (Cfg::RX_SIZE) / 8) {
|
2018-06-02 02:02:22 +02:00
|
|
|
|
2018-06-11 03:32:20 +02:00
|
|
|
// At this point, definitely no TX interrupt was executing, since the TX ISR can't be preempted.
|
2018-06-06 02:12:08 +02:00
|
|
|
// Don't enable the TX interrupt here as a means to trigger the XOFF char, because if it happens
|
|
|
|
// to be in the middle of trying to disable the RX interrupt in the main program, eventually the
|
|
|
|
// enabling of the TX interrupt could be undone. The ONLY reliable thing this can do to ensure
|
|
|
|
// the sending of the XOFF char is to send it HERE AND NOW.
|
|
|
|
|
|
|
|
// About to send the XOFF char
|
|
|
|
xon_xoff_state = XOFF_CHAR | XON_XOFF_CHAR_SENT;
|
|
|
|
|
|
|
|
// Wait until the TX register becomes empty and send it - Here there could be a problem
|
|
|
|
// - While waiting for the TX register to empty, the RX register could receive a new
|
|
|
|
// character. This must also handle that situation!
|
2018-10-03 05:15:30 +02:00
|
|
|
while (!B_UDRE) {
|
2018-06-06 02:12:08 +02:00
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
if (B_RXC) {
|
2018-06-06 02:12:08 +02:00
|
|
|
// A char arrived while waiting for the TX buffer to be empty - Receive and process it!
|
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
i = (ring_buffer_pos_t)(h + 1) & (ring_buffer_pos_t)(Cfg::RX_SIZE - 1);
|
2018-06-06 02:12:08 +02:00
|
|
|
|
|
|
|
// Read the character from the USART
|
2018-10-03 05:15:30 +02:00
|
|
|
c = R_UDR;
|
2018-06-06 02:12:08 +02:00
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
if (Cfg::EMERGENCYPARSER) emergency_parser.update(emergency_state, c);
|
2018-06-06 02:12:08 +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 FIFO is
|
|
|
|
// full, so don't write the character or advance the head.
|
|
|
|
if (i != t) {
|
|
|
|
rx_buffer.buffer[h] = c;
|
|
|
|
h = i;
|
|
|
|
}
|
2018-10-03 05:15:30 +02:00
|
|
|
else if (Cfg::DROPPED_RX && !++rx_dropped_bytes)
|
|
|
|
--rx_dropped_bytes;
|
2018-06-06 02:12:08 +02:00
|
|
|
}
|
|
|
|
sw_barrier();
|
2017-10-02 09:47:30 +02:00
|
|
|
}
|
2018-06-06 02:12:08 +02:00
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
R_UDR = XOFF_CHAR;
|
2018-06-06 02:12:08 +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
|
2018-10-03 05:15:30 +02:00
|
|
|
B_TXC = 1;
|
2018-06-06 02:12:08 +02:00
|
|
|
|
|
|
|
// At this point there could be a race condition between the write() function
|
|
|
|
// and this sending of the XOFF char. This interrupt could happen between the
|
|
|
|
// wait to be empty TX buffer loop and the actual write of the character. Since
|
|
|
|
// the TX buffer is full because it's sending the XOFF char, the only way to be
|
|
|
|
// sure the write() function will succeed is to wait for the XOFF char to be
|
|
|
|
// completely sent. Since an extra character could be received during the wait
|
|
|
|
// it must also be handled!
|
2018-10-03 05:15:30 +02:00
|
|
|
while (!B_UDRE) {
|
2018-06-06 02:12:08 +02:00
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
if (B_RXC) {
|
2018-06-06 02:12:08 +02:00
|
|
|
// A char arrived while waiting for the TX buffer to be empty - Receive and process it!
|
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
i = (ring_buffer_pos_t)(h + 1) & (ring_buffer_pos_t)(Cfg::RX_SIZE - 1);
|
2018-06-06 02:12:08 +02:00
|
|
|
|
|
|
|
// Read the character from the USART
|
2018-10-03 05:15:30 +02:00
|
|
|
c = R_UDR;
|
2018-06-06 02:12:08 +02:00
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
if (Cfg::EMERGENCYPARSER)
|
2018-06-06 02:12:08 +02:00
|
|
|
emergency_parser.update(emergency_state, c);
|
|
|
|
|
|
|
|
// 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 FIFO is
|
|
|
|
// full, so don't write the character or advance the head.
|
|
|
|
if (i != t) {
|
|
|
|
rx_buffer.buffer[h] = c;
|
|
|
|
h = i;
|
|
|
|
}
|
2018-10-03 05:15:30 +02:00
|
|
|
else if (Cfg::DROPPED_RX && !++rx_dropped_bytes)
|
|
|
|
--rx_dropped_bytes;
|
2018-06-06 02:12:08 +02:00
|
|
|
}
|
|
|
|
sw_barrier();
|
2017-10-02 09:47:30 +02:00
|
|
|
}
|
2018-06-06 02:12:08 +02:00
|
|
|
|
|
|
|
// At this point everything is ready. The write() function won't
|
|
|
|
// have any issues writing to the UART TX register if it needs to!
|
2017-10-02 09:47:30 +02:00
|
|
|
}
|
2017-04-02 07:47:20 +02:00
|
|
|
}
|
2018-10-03 05:15:30 +02:00
|
|
|
}
|
2016-07-08 17:25:21 +02:00
|
|
|
|
2018-06-11 03:32:20 +02:00
|
|
|
// Store the new head value - The main loop will retry until the value is stable
|
2018-06-06 02:12:08 +02:00
|
|
|
rx_buffer.head = h;
|
2017-04-02 07:47:20 +02:00
|
|
|
}
|
2016-07-08 17:25:21 +02:00
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
// (called with TX irqs disabled)
|
|
|
|
template<typename Cfg>
|
|
|
|
FORCE_INLINE void MarlinSerial<Cfg>::_tx_udr_empty_irq(void) {
|
|
|
|
if (Cfg::TX_SIZE > 0) {
|
2018-06-06 02:12:08 +02:00
|
|
|
// Read positions
|
|
|
|
uint8_t t = tx_buffer.tail;
|
|
|
|
const uint8_t h = tx_buffer.head;
|
2017-10-02 09:47:30 +02:00
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
if (Cfg::XONOFF) {
|
2018-06-06 02:12:08 +02:00
|
|
|
// If an XON char is pending to be sent, do it now
|
|
|
|
if (xon_xoff_state == XON_CHAR) {
|
|
|
|
|
|
|
|
// Send the character
|
2018-10-03 05:15:30 +02:00
|
|
|
R_UDR = XON_CHAR;
|
2018-06-06 02:12:08 +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
|
2018-10-03 05:15:30 +02:00
|
|
|
B_TXC = 1;
|
2018-06-06 02:12:08 +02:00
|
|
|
|
|
|
|
// Remember we sent it.
|
|
|
|
xon_xoff_state = XON_CHAR | XON_XOFF_CHAR_SENT;
|
|
|
|
|
|
|
|
// If nothing else to transmit, just disable TX interrupts.
|
2018-10-03 05:15:30 +02:00
|
|
|
if (h == t) B_UDRIE = 0; // (Non-atomic, could be reenabled by the main program, but eventually this will succeed)
|
2018-06-06 02:12:08 +02:00
|
|
|
|
|
|
|
return;
|
2017-10-02 09:47:30 +02:00
|
|
|
}
|
2018-10-03 05:15:30 +02:00
|
|
|
}
|
2018-06-06 02:12:08 +02:00
|
|
|
|
|
|
|
// If nothing to transmit, just disable TX interrupts. This could
|
|
|
|
// happen as the result of the non atomicity of the disabling of RX
|
|
|
|
// interrupts that could end reenabling TX interrupts as a side effect.
|
|
|
|
if (h == t) {
|
2018-10-03 05:15:30 +02:00
|
|
|
B_UDRIE = 0; // (Non-atomic, could be reenabled by the main program, but eventually this will succeed)
|
2018-06-06 02:12:08 +02:00
|
|
|
return;
|
2017-10-02 09:47:30 +02:00
|
|
|
}
|
2017-04-02 07:47:20 +02:00
|
|
|
|
2018-06-06 02:12:08 +02:00
|
|
|
// There is something to TX, Send the next byte
|
|
|
|
const uint8_t c = tx_buffer.buffer[t];
|
2018-10-03 05:15:30 +02:00
|
|
|
t = (t + 1) & (Cfg::TX_SIZE - 1);
|
|
|
|
R_UDR = c;
|
2018-06-06 02:12:08 +02:00
|
|
|
tx_buffer.tail = t;
|
|
|
|
|
|
|
|
// Clear the TXC bit (by writing a one to its bit location).
|
|
|
|
// Ensures flush() won't return until the bytes are actually written/
|
2018-10-03 05:15:30 +02:00
|
|
|
B_TXC = 1;
|
2017-04-02 07:47:20 +02:00
|
|
|
|
2018-06-06 02:12:08 +02:00
|
|
|
// Disable interrupts if there is nothing to transmit following this byte
|
2018-10-03 05:15:30 +02:00
|
|
|
if (h == t) B_UDRIE = 0; // (Non-atomic, could be reenabled by the main program, but eventually this will succeed)
|
2016-07-08 17:25:21 +02:00
|
|
|
}
|
2018-10-03 05:15:30 +02:00
|
|
|
}
|
2011-11-27 21:12:55 +01:00
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
// Public Methods
|
2018-10-03 05:15:30 +02:00
|
|
|
template<typename Cfg>
|
|
|
|
void MarlinSerial<Cfg>::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 09:47:30 +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
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
R_UCSRA = 0;
|
2017-04-02 07:47:20 +02:00
|
|
|
if (useU2X) {
|
2018-10-03 05:15:30 +02:00
|
|
|
B_U2X = 1;
|
2017-04-02 07:47:20 +02:00
|
|
|
baud_setting = (F_CPU / 4 / baud - 1) / 2;
|
|
|
|
}
|
2018-10-03 05:15:30 +02:00
|
|
|
else
|
2017-04-02 07:47:20 +02:00
|
|
|
baud_setting = (F_CPU / 8 / baud - 1) / 2;
|
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)
|
2018-10-03 05:15:30 +02:00
|
|
|
R_UBRRH = baud_setting >> 8;
|
|
|
|
R_UBRRL = baud_setting;
|
|
|
|
|
|
|
|
B_RXEN = 1;
|
|
|
|
B_TXEN = 1;
|
|
|
|
B_RXCIE = 1;
|
|
|
|
if (Cfg::TX_SIZE > 0) B_UDRIE = 0;
|
2018-06-06 02:12:08 +02:00
|
|
|
_written = false;
|
2017-04-02 07:47:20 +02:00
|
|
|
}
|
2011-11-27 21:12:55 +01:00
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
template<typename Cfg>
|
|
|
|
void MarlinSerial<Cfg>::end() {
|
|
|
|
B_RXEN = 0;
|
|
|
|
B_TXEN = 0;
|
|
|
|
B_RXCIE = 0;
|
|
|
|
B_UDRIE = 0;
|
2016-07-08 17:25:21 +02:00
|
|
|
}
|
2017-04-02 07:47:20 +02:00
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
template<typename Cfg>
|
|
|
|
int MarlinSerial<Cfg>::peek(void) {
|
2018-06-11 03:32:20 +02:00
|
|
|
const ring_buffer_pos_t h = atomic_read_rx_head(), t = rx_buffer.tail;
|
|
|
|
return h == t ? -1 : rx_buffer.buffer[t];
|
2017-04-02 07:47:20 +02:00
|
|
|
}
|
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
template<typename Cfg>
|
|
|
|
int MarlinSerial<Cfg>::read(void) {
|
2018-06-11 03:32:20 +02:00
|
|
|
const ring_buffer_pos_t h = atomic_read_rx_head();
|
2018-06-02 02:02:22 +02:00
|
|
|
|
2018-06-11 03:32:20 +02:00
|
|
|
// Read the tail. Main thread owns it, so it is safe to directly read it
|
2018-06-02 02:02:22 +02:00
|
|
|
ring_buffer_pos_t t = rx_buffer.tail;
|
|
|
|
|
2018-06-06 02:12:08 +02:00
|
|
|
// If nothing to read, return now
|
|
|
|
if (h == t) return -1;
|
2018-06-02 02:02:22 +02:00
|
|
|
|
2018-06-06 02:12:08 +02:00
|
|
|
// Get the next char
|
|
|
|
const int v = rx_buffer.buffer[t];
|
2018-10-03 05:15:30 +02:00
|
|
|
t = (ring_buffer_pos_t)(t + 1) & (Cfg::RX_SIZE - 1);
|
2018-06-02 02:02:22 +02:00
|
|
|
|
2018-06-11 03:32:20 +02:00
|
|
|
// Advance tail - Making sure the RX ISR will always get an stable value, even
|
|
|
|
// if it interrupts the writing of the value of that variable in the middle.
|
|
|
|
atomic_set_rx_tail(t);
|
2018-06-02 02:02:22 +02:00
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
if (Cfg::XONOFF) {
|
2018-06-06 02:12:08 +02:00
|
|
|
// If the XOFF char was sent, or about to be sent...
|
|
|
|
if ((xon_xoff_state & XON_XOFF_CHAR_MASK) == XOFF_CHAR) {
|
|
|
|
// Get count of bytes in the RX buffer
|
2018-10-03 05:15:30 +02:00
|
|
|
const ring_buffer_pos_t rx_count = (ring_buffer_pos_t)(h - t) & (ring_buffer_pos_t)(Cfg::RX_SIZE - 1);
|
|
|
|
if (rx_count < (Cfg::RX_SIZE) / 10) {
|
|
|
|
if (Cfg::TX_SIZE > 0) {
|
2018-06-06 02:12:08 +02:00
|
|
|
// Signal we want an XON character to be sent.
|
|
|
|
xon_xoff_state = XON_CHAR;
|
2018-06-11 03:32:20 +02:00
|
|
|
// Enable TX ISR. Non atomic, but it will eventually enable them
|
2018-10-03 05:15:30 +02:00
|
|
|
B_UDRIE = 1;
|
|
|
|
}
|
|
|
|
else {
|
2018-06-06 02:12:08 +02:00
|
|
|
// If not using TX interrupts, we must send the XON char now
|
2018-06-02 02:02:22 +02:00
|
|
|
xon_xoff_state = XON_CHAR | XON_XOFF_CHAR_SENT;
|
2018-10-03 05:15:30 +02:00
|
|
|
while (!B_UDRE) sw_barrier();
|
|
|
|
R_UDR = XON_CHAR;
|
|
|
|
}
|
2018-06-02 02:02:22 +02:00
|
|
|
}
|
2018-06-06 02:12:08 +02:00
|
|
|
}
|
2018-10-03 05:15:30 +02:00
|
|
|
}
|
2018-06-02 02:02:22 +02:00
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
return v;
|
2016-07-08 17:25:21 +02:00
|
|
|
}
|
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
template<typename Cfg>
|
|
|
|
typename MarlinSerial<Cfg>::ring_buffer_pos_t MarlinSerial<Cfg>::available(void) {
|
2018-06-11 03:32:20 +02:00
|
|
|
const ring_buffer_pos_t h = atomic_read_rx_head(), t = rx_buffer.tail;
|
2018-10-03 05:15:30 +02:00
|
|
|
return (ring_buffer_pos_t)(Cfg::RX_SIZE + h - t) & (Cfg::RX_SIZE - 1);
|
2017-04-02 07:47:20 +02:00
|
|
|
}
|
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
template<typename Cfg>
|
|
|
|
void MarlinSerial<Cfg>::flush(void) {
|
2018-06-02 02:02:22 +02:00
|
|
|
|
2018-06-11 03:32:20 +02:00
|
|
|
// Set the tail to the head:
|
|
|
|
// - Read the RX head index in a safe way. (See atomic_read_rx_head.)
|
|
|
|
// - Set the tail, making sure the RX ISR will always get a stable value, even
|
|
|
|
// if it interrupts the writing of the value of that variable in the middle.
|
|
|
|
atomic_set_rx_tail(atomic_read_rx_head());
|
2017-10-02 09:47:30 +02:00
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
if (Cfg::XONOFF) {
|
2018-06-06 02:12:08 +02:00
|
|
|
// If the XOFF char was sent, or about to be sent...
|
2017-10-02 09:47:30 +02:00
|
|
|
if ((xon_xoff_state & XON_XOFF_CHAR_MASK) == XOFF_CHAR) {
|
2018-10-03 05:15:30 +02:00
|
|
|
if (Cfg::TX_SIZE > 0) {
|
2018-06-06 02:12:08 +02:00
|
|
|
// Signal we want an XON character to be sent.
|
|
|
|
xon_xoff_state = XON_CHAR;
|
2018-06-11 03:32:20 +02:00
|
|
|
// Enable TX ISR. Non atomic, but it will eventually enable it.
|
2018-10-03 05:15:30 +02:00
|
|
|
B_UDRIE = 1;
|
|
|
|
}
|
|
|
|
else {
|
2018-06-06 02:12:08 +02:00
|
|
|
// If not using TX interrupts, we must send the XON char now
|
|
|
|
xon_xoff_state = XON_CHAR | XON_XOFF_CHAR_SENT;
|
2018-10-03 05:15:30 +02:00
|
|
|
while (!B_UDRE) sw_barrier();
|
|
|
|
R_UDR = XON_CHAR;
|
|
|
|
}
|
2017-10-02 09:47:30 +02:00
|
|
|
}
|
2018-10-03 05:15:30 +02:00
|
|
|
}
|
2017-04-02 07:47:20 +02:00
|
|
|
}
|
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
template<typename Cfg>
|
|
|
|
void MarlinSerial<Cfg>::write(const uint8_t c) {
|
|
|
|
if (Cfg::TX_SIZE == 0) {
|
|
|
|
|
|
|
|
_written = true;
|
|
|
|
while (!B_UDRE) sw_barrier();
|
|
|
|
R_UDR = c;
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
_written = true;
|
2018-06-02 02:02:22 +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.
|
2018-06-06 02:12:08 +02:00
|
|
|
// Yes, there is a race condition between the sending of the
|
2018-06-11 03:32:20 +02:00
|
|
|
// XOFF char at the RX ISR, but it is properly handled there
|
2018-10-03 05:15:30 +02:00
|
|
|
if (!B_UDRIE && B_UDRE) {
|
|
|
|
R_UDR = c;
|
2018-06-02 02:02:22 +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
|
2018-10-03 05:15:30 +02:00
|
|
|
B_TXC = 1;
|
2017-04-02 07:47:20 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-06-02 02:02:22 +02:00
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
const uint8_t i = (tx_buffer.head + 1) & (Cfg::TX_SIZE - 1);
|
2017-04-02 07:47:20 +02:00
|
|
|
|
2018-06-06 02:12:08 +02:00
|
|
|
// If global interrupts are disabled (as the result of being called from an ISR)...
|
|
|
|
if (!ISRS_ENABLED()) {
|
|
|
|
|
|
|
|
// Make room by polling if it is possible to transmit, and do so!
|
|
|
|
while (i == tx_buffer.tail) {
|
2018-06-02 02:02:22 +02:00
|
|
|
|
2018-06-06 02:12:08 +02:00
|
|
|
// If we can transmit another byte, do it.
|
2018-10-03 05:15:30 +02:00
|
|
|
if (B_UDRE) _tx_udr_empty_irq();
|
2018-06-06 02:12:08 +02:00
|
|
|
|
|
|
|
// Make sure compiler rereads tx_buffer.tail
|
|
|
|
sw_barrier();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Interrupts are enabled, just wait until there is space
|
2018-10-03 05:15:30 +02:00
|
|
|
while (i == 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:02:22 +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:02:22 +02:00
|
|
|
tx_buffer.head = i;
|
|
|
|
|
2018-06-11 03:32:20 +02:00
|
|
|
// Enable TX ISR - Non atomic, but it will eventually enable TX ISR
|
2018-10-03 05:15:30 +02:00
|
|
|
B_UDRIE = 1;
|
2016-07-08 17:25:21 +02:00
|
|
|
}
|
2018-10-03 05:15:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Cfg>
|
|
|
|
void MarlinSerial<Cfg>::flushTX(void) {
|
|
|
|
|
|
|
|
if (Cfg::TX_SIZE == 0) {
|
|
|
|
// No bytes written, no need to flush. This special case is needed since there's
|
|
|
|
// no way to force the TXC (transmit complete) bit to 1 during initialization.
|
|
|
|
if (!_written) return;
|
|
|
|
|
|
|
|
// Wait until everything was transmitted
|
|
|
|
while (!B_TXC) sw_barrier();
|
|
|
|
|
|
|
|
// At this point nothing is queued anymore (DRIE is disabled) and
|
|
|
|
// the hardware finished transmission (TXC is set).
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
2017-04-02 07:47:20 +02:00
|
|
|
|
2018-06-06 02:12:08 +02:00
|
|
|
// No bytes written, no need to flush. This special case is needed since there's
|
|
|
|
// no way to force the TXC (transmit complete) bit to 1 during initialization.
|
|
|
|
if (!_written) return;
|
|
|
|
|
|
|
|
// If global interrupts are disabled (as the result of being called from an ISR)...
|
|
|
|
if (!ISRS_ENABLED()) {
|
2017-04-02 07:47:20 +02:00
|
|
|
|
2018-06-06 02:12:08 +02:00
|
|
|
// Wait until everything was transmitted - We must do polling, as interrupts are disabled
|
2018-10-03 05:15:30 +02:00
|
|
|
while (tx_buffer.head != tx_buffer.tail || !B_TXC) {
|
2018-06-06 02:12:08 +02:00
|
|
|
|
|
|
|
// If there is more space, send an extra character
|
2018-10-03 05:15:30 +02:00
|
|
|
if (B_UDRE) _tx_udr_empty_irq();
|
2018-06-06 02:12:08 +02:00
|
|
|
|
|
|
|
sw_barrier();
|
2018-06-02 02:02:22 +02:00
|
|
|
}
|
2018-06-06 02:12:08 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Wait until everything was transmitted
|
2018-10-03 05:15:30 +02:00
|
|
|
while (tx_buffer.head != tx_buffer.tail || !B_TXC) sw_barrier();
|
2017-04-02 07:47:20 +02:00
|
|
|
}
|
2018-06-06 02:12:08 +02:00
|
|
|
|
|
|
|
// At this point nothing is queued anymore (DRIE is disabled) and
|
2018-06-02 02:02:22 +02:00
|
|
|
// the hardware finished transmission (TXC is set).
|
2017-10-02 09:47:30 +02:00
|
|
|
}
|
2018-10-03 05:15:30 +02:00
|
|
|
}
|
2011-11-27 21:12:55 +01:00
|
|
|
|
2017-10-02 09:47:30 +02:00
|
|
|
/**
|
|
|
|
* Imports from print.h
|
|
|
|
*/
|
2011-11-28 19:13:40 +01:00
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
template<typename Cfg>
|
|
|
|
void MarlinSerial<Cfg>::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
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
template<typename Cfg>
|
|
|
|
void MarlinSerial<Cfg>::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
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
template<typename Cfg>
|
|
|
|
void MarlinSerial<Cfg>::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
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
template<typename Cfg>
|
|
|
|
void MarlinSerial<Cfg>::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
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
template<typename Cfg>
|
|
|
|
void MarlinSerial<Cfg>::print(long n, int base) {
|
2018-06-06 02:12:08 +02:00
|
|
|
if (base == 0) write(n);
|
2017-04-02 07:47:20 +02:00
|
|
|
else if (base == 10) {
|
2018-06-06 02:12:08 +02:00
|
|
|
if (n < 0) { print('-'); n = -n; }
|
2017-04-02 07:47:20 +02:00
|
|
|
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
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
template<typename Cfg>
|
|
|
|
void MarlinSerial<Cfg>::print(unsigned long n, int base) {
|
2017-04-02 07:47:20 +02:00
|
|
|
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
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
template<typename Cfg>
|
|
|
|
void MarlinSerial<Cfg>::print(double n, int digits) {
|
2017-04-02 07:47:20 +02:00
|
|
|
printFloat(n, digits);
|
2011-11-28 19:13:40 +01:00
|
|
|
}
|
2017-04-02 07:47:20 +02:00
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
template<typename Cfg>
|
|
|
|
void MarlinSerial<Cfg>::println(void) {
|
2017-04-02 07:47:20 +02:00
|
|
|
print('\r');
|
|
|
|
print('\n');
|
2011-11-28 19:13:40 +01:00
|
|
|
}
|
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
template<typename Cfg>
|
|
|
|
void MarlinSerial<Cfg>::println(const String& s) {
|
2017-04-02 07:47:20 +02:00
|
|
|
print(s);
|
|
|
|
println();
|
2015-10-03 08:08:58 +02:00
|
|
|
}
|
2011-11-27 21:12:55 +01:00
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
template<typename Cfg>
|
|
|
|
void MarlinSerial<Cfg>::println(const char c[]) {
|
2017-04-02 07:47:20 +02:00
|
|
|
print(c);
|
|
|
|
println();
|
|
|
|
}
|
2011-11-27 21:12:55 +01:00
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
template<typename Cfg>
|
|
|
|
void MarlinSerial<Cfg>::println(char c, int base) {
|
2017-04-02 07:47:20 +02:00
|
|
|
print(c, base);
|
|
|
|
println();
|
|
|
|
}
|
2011-11-27 21:12:55 +01:00
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
template<typename Cfg>
|
|
|
|
void MarlinSerial<Cfg>::println(unsigned char b, int base) {
|
2017-04-02 07:47:20 +02:00
|
|
|
print(b, base);
|
|
|
|
println();
|
|
|
|
}
|
2013-10-30 11:45:32 +01:00
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
template<typename Cfg>
|
|
|
|
void MarlinSerial<Cfg>::println(int n, int base) {
|
2017-04-02 07:47:20 +02:00
|
|
|
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
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
template<typename Cfg>
|
|
|
|
void MarlinSerial<Cfg>::println(unsigned int n, int base) {
|
2017-04-02 07:47:20 +02:00
|
|
|
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
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
template<typename Cfg>
|
|
|
|
void MarlinSerial<Cfg>::println(long n, int base) {
|
2017-04-02 07:47:20 +02:00
|
|
|
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
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
template<typename Cfg>
|
|
|
|
void MarlinSerial<Cfg>::println(unsigned long n, int base) {
|
2017-04-02 07:47:20 +02:00
|
|
|
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
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
template<typename Cfg>
|
|
|
|
void MarlinSerial<Cfg>::println(double n, int digits) {
|
2017-04-02 07:47:20 +02:00
|
|
|
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
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
template<typename Cfg>
|
|
|
|
void MarlinSerial<Cfg>::printNumber(unsigned long n, uint8_t base) {
|
2017-04-02 07:47:20 +02:00
|
|
|
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
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
template<typename Cfg>
|
|
|
|
void MarlinSerial<Cfg>::printFloat(double number, uint8_t digits) {
|
2017-04-02 07:47:20 +02:00
|
|
|
// 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;
|
2018-10-03 05:15:30 +02:00
|
|
|
for (uint8_t i = 0; i < digits; ++i) rounding *= 0.1;
|
2017-04-02 07:47:20 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-03 05:15:30 +02:00
|
|
|
// Hookup ISR handlers
|
|
|
|
ISR(SERIAL_REGNAME(USART,SERIAL_PORT,_RX_vect)) {
|
2018-10-03 18:01:21 +02:00
|
|
|
MarlinSerial<MarlinSerialCfg<SERIAL_PORT>>::store_rxd_char();
|
2018-10-03 05:15:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ISR(SERIAL_REGNAME(USART,SERIAL_PORT,_UDRE_vect)) {
|
2018-10-03 18:01:21 +02:00
|
|
|
MarlinSerial<MarlinSerialCfg<SERIAL_PORT>>::_tx_udr_empty_irq();
|
2018-10-03 05:15:30 +02:00
|
|
|
}
|
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
// Preinstantiate
|
2018-10-03 18:01:21 +02:00
|
|
|
template class MarlinSerial<MarlinSerialCfg<SERIAL_PORT>>;
|
2018-10-03 05:15:30 +02:00
|
|
|
|
|
|
|
// Instantiate
|
2018-10-03 18:01:21 +02:00
|
|
|
MarlinSerial<MarlinSerialCfg<SERIAL_PORT>> customizedSerial1;
|
2018-10-03 07:47:27 +02:00
|
|
|
|
|
|
|
#ifdef SERIAL_PORT_2
|
|
|
|
|
|
|
|
// Hookup ISR handlers
|
|
|
|
ISR(SERIAL_REGNAME(USART,SERIAL_PORT_2,_RX_vect)) {
|
2018-10-03 18:01:21 +02:00
|
|
|
MarlinSerial<MarlinSerialCfg<SERIAL_PORT_2>>::store_rxd_char();
|
2018-10-03 07:47:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ISR(SERIAL_REGNAME(USART,SERIAL_PORT_2,_UDRE_vect)) {
|
2018-10-03 18:01:21 +02:00
|
|
|
MarlinSerial<MarlinSerialCfg<SERIAL_PORT_2>>::_tx_udr_empty_irq();
|
2018-10-03 07:47:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Preinstantiate
|
2018-10-03 18:01:21 +02:00
|
|
|
template class MarlinSerial<MarlinSerialCfg<SERIAL_PORT_2>>;
|
2018-10-03 07:47:27 +02:00
|
|
|
|
|
|
|
// Instantiate
|
2018-10-03 18:01:21 +02:00
|
|
|
MarlinSerial<MarlinSerialCfg<SERIAL_PORT_2>> customizedSerial2;
|
2018-10-03 07:47:27 +02:00
|
|
|
|
|
|
|
#endif
|
2017-04-02 07:47:20 +02:00
|
|
|
|
|
|
|
#endif // !USBCON && (UBRRH || UBRR0H || UBRR1H || UBRR2H || UBRR3H)
|
|
|
|
|
|
|
|
// For AT90USB targets use the UART for BT interfacing
|
|
|
|
#if defined(USBCON) && ENABLED(BLUETOOTH)
|
|
|
|
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
|
2017-06-18 01:36:10 +02:00
|
|
|
|
2017-10-02 09:47:30 +02:00
|
|
|
#endif // __AVR__
|