2016-03-25 06:19:46 +00:00
|
|
|
/**
|
2016-03-24 18:01:20 +00: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 06:19:46 +00:00
|
|
|
/**
|
2011-11-27 20:12:55 +00:00
|
|
|
HardwareSerial.h - Hardware serial library for Wiring
|
|
|
|
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
|
|
|
|
|
|
|
|
Modified 28 September 2010 by Mark Sproul
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MarlinSerial_h
|
|
|
|
#define MarlinSerial_h
|
2011-12-22 13:55:45 +00:00
|
|
|
#include "Marlin.h"
|
2011-11-28 20:51:44 +00:00
|
|
|
|
2016-02-24 16:18:12 +00:00
|
|
|
#ifndef CRITICAL_SECTION_START
|
|
|
|
#define CRITICAL_SECTION_START unsigned char _sreg = SREG; cli();
|
|
|
|
#define CRITICAL_SECTION_END SREG = _sreg;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2015-01-24 05:11:50 +00:00
|
|
|
#ifndef SERIAL_PORT
|
|
|
|
#define SERIAL_PORT 0
|
2012-11-05 10:34:27 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// The presence of the UBRRH register is used to detect a UART.
|
|
|
|
#define UART_PRESENT(port) ((port == 0 && (defined(UBRRH) || defined(UBRR0H))) || \
|
2015-10-03 06:08:58 +00:00
|
|
|
(port == 1 && defined(UBRR1H)) || (port == 2 && defined(UBRR2H)) || \
|
|
|
|
(port == 3 && defined(UBRR3H)))
|
|
|
|
|
2012-11-05 10:34:27 +00:00
|
|
|
// These are macros to build serial port register names for the selected SERIAL_PORT (C preprocessor
|
|
|
|
// requires two levels of indirection to expand macro values properly)
|
|
|
|
#define SERIAL_REGNAME(registerbase,number,suffix) SERIAL_REGNAME_INTERNAL(registerbase,number,suffix)
|
2012-11-06 22:00:21 +00:00
|
|
|
#if SERIAL_PORT == 0 && (!defined(UBRR0H) || !defined(UDR0)) // use un-numbered registers if necessary
|
2015-01-24 05:11:50 +00:00
|
|
|
#define SERIAL_REGNAME_INTERNAL(registerbase,number,suffix) registerbase##suffix
|
2012-11-06 22:00:21 +00:00
|
|
|
#else
|
2015-01-24 05:11:50 +00:00
|
|
|
#define SERIAL_REGNAME_INTERNAL(registerbase,number,suffix) registerbase##number##suffix
|
2012-11-06 22:00:21 +00:00
|
|
|
#endif
|
2012-11-05 10:34:27 +00:00
|
|
|
|
2015-10-03 06:08:58 +00:00
|
|
|
// Registers used by MarlinSerial class (these are expanded
|
2012-11-05 10:34:27 +00:00
|
|
|
// depending on selected serial port
|
2012-11-06 22:00:21 +00:00
|
|
|
#define M_UCSRxA SERIAL_REGNAME(UCSR,SERIAL_PORT,A) // defines M_UCSRxA to be UCSRnA where n is the serial port number
|
2015-10-03 06:08:58 +00:00
|
|
|
#define M_UCSRxB SERIAL_REGNAME(UCSR,SERIAL_PORT,B)
|
|
|
|
#define M_RXENx SERIAL_REGNAME(RXEN,SERIAL_PORT,)
|
|
|
|
#define M_TXENx SERIAL_REGNAME(TXEN,SERIAL_PORT,)
|
|
|
|
#define M_RXCIEx SERIAL_REGNAME(RXCIE,SERIAL_PORT,)
|
|
|
|
#define M_UDREx SERIAL_REGNAME(UDRE,SERIAL_PORT,)
|
|
|
|
#define M_UDRx SERIAL_REGNAME(UDR,SERIAL_PORT,)
|
2012-11-05 10:34:27 +00:00
|
|
|
#define M_UBRRxH SERIAL_REGNAME(UBRR,SERIAL_PORT,H)
|
|
|
|
#define M_UBRRxL SERIAL_REGNAME(UBRR,SERIAL_PORT,L)
|
|
|
|
#define M_RXCx SERIAL_REGNAME(RXC,SERIAL_PORT,)
|
|
|
|
#define M_USARTx_RX_vect SERIAL_REGNAME(USART,SERIAL_PORT,_RX_vect)
|
|
|
|
#define M_U2Xx SERIAL_REGNAME(U2X,SERIAL_PORT,)
|
|
|
|
|
|
|
|
|
2011-11-30 07:32:28 +00:00
|
|
|
#define DEC 10
|
|
|
|
#define HEX 16
|
|
|
|
#define OCT 8
|
|
|
|
#define BIN 2
|
|
|
|
#define BYTE 0
|
|
|
|
|
2011-11-27 20:12:55 +00:00
|
|
|
|
2015-05-24 16:32:34 +00:00
|
|
|
#ifndef USBCON
|
2011-11-28 18:13:40 +00:00
|
|
|
// Define constants and variables for buffering incoming serial data. We're
|
|
|
|
// using a ring buffer (I think), in which rx_buffer_head is the index of the
|
|
|
|
// location to which to write the next incoming character and rx_buffer_tail
|
|
|
|
// is the index of the location from which to read.
|
2016-02-24 16:18:12 +00:00
|
|
|
// 256 is the max limit due to uint8_t head and tail. Use only powers of 2. (...,16,32,64,128,256)
|
|
|
|
#ifndef RX_BUFFER_SIZE
|
|
|
|
#define RX_BUFFER_SIZE 128
|
|
|
|
#endif
|
|
|
|
#if !((RX_BUFFER_SIZE == 256) ||(RX_BUFFER_SIZE == 128) ||(RX_BUFFER_SIZE == 64) ||(RX_BUFFER_SIZE == 32) ||(RX_BUFFER_SIZE == 16) ||(RX_BUFFER_SIZE == 8) ||(RX_BUFFER_SIZE == 4) ||(RX_BUFFER_SIZE == 2))
|
2016-05-13 12:10:23 +00:00
|
|
|
#error "RX_BUFFER_SIZE has to be a power of 2 and >= 2"
|
2016-02-24 16:18:12 +00:00
|
|
|
#endif
|
2011-11-28 18:13:40 +00:00
|
|
|
|
2015-01-24 05:11:50 +00:00
|
|
|
struct ring_buffer {
|
2011-11-28 18:13:40 +00:00
|
|
|
unsigned char buffer[RX_BUFFER_SIZE];
|
2016-02-03 20:20:34 +00:00
|
|
|
volatile uint8_t head;
|
|
|
|
volatile uint8_t tail;
|
2011-11-28 18:13:40 +00:00
|
|
|
};
|
|
|
|
|
2012-11-05 10:34:27 +00:00
|
|
|
#if UART_PRESENT(SERIAL_PORT)
|
2011-11-28 18:13:40 +00:00
|
|
|
extern ring_buffer rx_buffer;
|
|
|
|
#endif
|
|
|
|
|
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 21:23:22 +00:00
|
|
|
#if ENABLED(EMERGENCY_PARSER)
|
|
|
|
#include "language.h"
|
|
|
|
void emergency_parser(unsigned char c);
|
|
|
|
#endif
|
|
|
|
|
2015-01-24 05:11:50 +00:00
|
|
|
class MarlinSerial { //: public Stream
|
2011-11-28 18:28:38 +00:00
|
|
|
|
2011-11-27 20:12:55 +00:00
|
|
|
public:
|
2011-11-28 18:28:38 +00:00
|
|
|
MarlinSerial();
|
2011-11-27 20:12:55 +00:00
|
|
|
void begin(long);
|
|
|
|
void end();
|
2011-11-28 18:34:42 +00:00
|
|
|
int peek(void);
|
|
|
|
int read(void);
|
|
|
|
void flush(void);
|
2015-01-24 05:11:50 +00:00
|
|
|
|
2016-02-03 20:20:34 +00:00
|
|
|
FORCE_INLINE uint8_t available(void) {
|
2016-02-24 16:18:12 +00:00
|
|
|
CRITICAL_SECTION_START;
|
|
|
|
uint8_t h = rx_buffer.head;
|
|
|
|
uint8_t t = rx_buffer.tail;
|
|
|
|
CRITICAL_SECTION_END;
|
|
|
|
return (uint8_t)(RX_BUFFER_SIZE + h - t) & (RX_BUFFER_SIZE - 1);
|
2011-11-28 18:13:40 +00:00
|
|
|
}
|
2015-01-24 05:11:50 +00:00
|
|
|
|
|
|
|
FORCE_INLINE void write(uint8_t c) {
|
2015-03-14 11:28:22 +00:00
|
|
|
while (!TEST(M_UCSRxA, M_UDREx))
|
2011-11-28 18:13:40 +00:00
|
|
|
;
|
2012-11-05 10:34:27 +00:00
|
|
|
M_UDRx = c;
|
2011-11-28 18:13:40 +00:00
|
|
|
}
|
2015-01-24 05:11:50 +00:00
|
|
|
|
|
|
|
FORCE_INLINE void checkRx(void) {
|
2015-03-14 11:28:22 +00:00
|
|
|
if (TEST(M_UCSRxA, M_RXCx)) {
|
2012-11-05 10:34:27 +00:00
|
|
|
unsigned char c = M_UDRx;
|
2016-02-24 16:18:12 +00:00
|
|
|
CRITICAL_SECTION_START;
|
|
|
|
uint8_t h = rx_buffer.head;
|
|
|
|
uint8_t i = (uint8_t)(h + 1) & (RX_BUFFER_SIZE - 1);
|
|
|
|
|
|
|
|
// if we should be storing the received character into the location
|
|
|
|
// just before the tail (meaning that the head would advance to the
|
|
|
|
// current location of the tail), we're about to overflow the buffer
|
|
|
|
// and so we don't write the character or advance the head.
|
|
|
|
if (i != rx_buffer.tail) {
|
|
|
|
rx_buffer.buffer[h] = c;
|
|
|
|
rx_buffer.head = i;
|
|
|
|
}
|
|
|
|
CRITICAL_SECTION_END;
|
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 21:23:22 +00:00
|
|
|
|
|
|
|
#if ENABLED(EMERGENCY_PARSER)
|
|
|
|
emergency_parser(c);
|
|
|
|
#endif
|
2011-11-28 18:13:40 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-24 05:11:50 +00:00
|
|
|
|
|
|
|
private:
|
2011-11-28 18:13:40 +00:00
|
|
|
void printNumber(unsigned long, uint8_t);
|
|
|
|
void printFloat(double, uint8_t);
|
2011-11-28 18:34:42 +00:00
|
|
|
|
2015-01-24 05:11:50 +00:00
|
|
|
public:
|
2015-10-03 06:08:58 +00:00
|
|
|
FORCE_INLINE void write(const char* str) { while (*str) write(*str++); }
|
|
|
|
FORCE_INLINE void write(const uint8_t* buffer, size_t size) { while (size--) write(*buffer++); }
|
|
|
|
FORCE_INLINE void print(const String& s) { for (int i = 0; i < (int)s.length(); i++) write(s[i]); }
|
|
|
|
FORCE_INLINE void print(const char* str) { write(str); }
|
2011-11-28 18:34:42 +00:00
|
|
|
|
2011-11-28 18:13:40 +00:00
|
|
|
void print(char, int = BYTE);
|
|
|
|
void print(unsigned char, int = BYTE);
|
|
|
|
void print(int, int = DEC);
|
|
|
|
void print(unsigned int, int = DEC);
|
|
|
|
void print(long, int = DEC);
|
|
|
|
void print(unsigned long, int = DEC);
|
|
|
|
void print(double, int = 2);
|
|
|
|
|
2015-10-03 06:08:58 +00:00
|
|
|
void println(const String& s);
|
2011-11-28 18:13:40 +00:00
|
|
|
void println(const char[]);
|
|
|
|
void println(char, int = BYTE);
|
|
|
|
void println(unsigned char, int = BYTE);
|
|
|
|
void println(int, int = DEC);
|
|
|
|
void println(unsigned int, int = DEC);
|
|
|
|
void println(long, int = DEC);
|
|
|
|
void println(unsigned long, int = DEC);
|
|
|
|
void println(double, int = 2);
|
|
|
|
void println(void);
|
2011-11-27 20:12:55 +00:00
|
|
|
};
|
|
|
|
|
2015-08-01 03:31:53 +00:00
|
|
|
extern MarlinSerial customizedSerial;
|
2015-05-24 16:32:34 +00:00
|
|
|
#endif // !USBCON
|
2011-11-27 20:12:55 +00:00
|
|
|
|
2015-08-01 03:31:53 +00:00
|
|
|
// Use the UART for Bluetooth in AT90USB configurations
|
2015-07-31 23:55:47 +00:00
|
|
|
#if defined(USBCON) && ENABLED(BLUETOOTH)
|
2015-08-01 03:31:53 +00:00
|
|
|
extern HardwareSerial bluetoothSerial;
|
2013-10-30 10:45:32 +00:00
|
|
|
#endif
|
|
|
|
|
2012-11-06 22:00:21 +00:00
|
|
|
#endif
|