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
|
|
|
/**
|
2018-03-04 06:13:53 +01:00
|
|
|
* MarlinSerial.h - Hardware serial library for Wiring
|
|
|
|
* Copyright (c) 2006 Nicholas Zambetti. All right reserved.
|
|
|
|
*
|
|
|
|
* Modified 28 September 2010 by Mark Sproul
|
|
|
|
* Modified 14 February 2016 by Andreas Hardtung (added tx buffer)
|
|
|
|
*/
|
2011-11-27 21:12:55 +01:00
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
#ifndef MARLINSERIAL_H
|
|
|
|
#define MARLINSERIAL_H
|
2016-02-24 17:18:12 +01:00
|
|
|
|
2016-08-03 04:36:58 +02:00
|
|
|
#include "MarlinConfig.h"
|
2016-02-24 17:18:12 +01:00
|
|
|
|
2015-01-24 06:11:50 +01:00
|
|
|
#ifndef SERIAL_PORT
|
|
|
|
#define SERIAL_PORT 0
|
2012-11-05 11:34:27 +01: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 08:08:58 +02:00
|
|
|
(port == 1 && defined(UBRR1H)) || (port == 2 && defined(UBRR2H)) || \
|
|
|
|
(port == 3 && defined(UBRR3H)))
|
|
|
|
|
2012-11-05 11:34:27 +01: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 23:00:21 +01:00
|
|
|
#if SERIAL_PORT == 0 && (!defined(UBRR0H) || !defined(UDR0)) // use un-numbered registers if necessary
|
2015-01-24 06:11:50 +01:00
|
|
|
#define SERIAL_REGNAME_INTERNAL(registerbase,number,suffix) registerbase##suffix
|
2012-11-06 23:00:21 +01:00
|
|
|
#else
|
2015-01-24 06:11:50 +01:00
|
|
|
#define SERIAL_REGNAME_INTERNAL(registerbase,number,suffix) registerbase##number##suffix
|
2012-11-06 23:00:21 +01:00
|
|
|
#endif
|
2012-11-05 11:34:27 +01:00
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
// Registers used by MarlinSerial class (expanded depending on selected serial port)
|
|
|
|
#define M_UCSRxA SERIAL_REGNAME(UCSR,SERIAL_PORT,A) // defines M_UCSRxA to be UCSRnA where n is the serial port number
|
|
|
|
#define M_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_TXCx SERIAL_REGNAME(TXC,SERIAL_PORT,)
|
|
|
|
#define M_RXCIEx SERIAL_REGNAME(RXCIE,SERIAL_PORT,)
|
|
|
|
#define M_UDREx SERIAL_REGNAME(UDRE,SERIAL_PORT,)
|
|
|
|
#define M_UDRIEx SERIAL_REGNAME(UDRIE,SERIAL_PORT,)
|
|
|
|
#define M_UDRx SERIAL_REGNAME(UDR,SERIAL_PORT,)
|
|
|
|
#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,)
|
2016-07-08 17:25:21 +02:00
|
|
|
#define M_USARTx_UDRE_vect SERIAL_REGNAME(USART,SERIAL_PORT,_UDRE_vect)
|
2012-11-05 11:34:27 +01:00
|
|
|
|
2011-11-30 08:32:28 +01:00
|
|
|
#define DEC 10
|
|
|
|
#define HEX 16
|
|
|
|
#define OCT 8
|
|
|
|
#define BIN 2
|
|
|
|
#define BYTE 0
|
|
|
|
|
2017-12-04 23:48:19 +01:00
|
|
|
// Define constants and variables for buffering serial data.
|
|
|
|
// Use only 0 or powers of 2 greater than 1
|
|
|
|
// : [0, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, ...]
|
|
|
|
#ifndef RX_BUFFER_SIZE
|
|
|
|
#define RX_BUFFER_SIZE 128
|
|
|
|
#endif
|
|
|
|
// 256 is the max TX buffer limit due to uint8_t head and tail.
|
|
|
|
#ifndef TX_BUFFER_SIZE
|
|
|
|
#define TX_BUFFER_SIZE 32
|
|
|
|
#endif
|
2011-11-28 19:13:40 +01:00
|
|
|
|
2017-12-04 23:48:19 +01:00
|
|
|
#ifndef USBCON
|
2017-04-02 07:47:20 +02:00
|
|
|
|
2017-10-02 07:26:56 +02:00
|
|
|
#if RX_BUFFER_SIZE > 256
|
|
|
|
typedef uint16_t ring_buffer_pos_t;
|
|
|
|
#else
|
|
|
|
typedef uint8_t ring_buffer_pos_t;
|
2017-08-08 07:46:37 +02:00
|
|
|
#endif
|
2017-10-06 21:58:37 +02:00
|
|
|
|
2017-08-08 07:46:37 +02:00
|
|
|
#if ENABLED(SERIAL_STATS_DROPPED_RX)
|
2017-10-02 07:26:56 +02:00
|
|
|
extern uint8_t rx_dropped_bytes;
|
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
|
|
|
extern ring_buffer_pos_t rx_max_enqueued;
|
2017-10-06 21:58:37 +02:00
|
|
|
#endif
|
|
|
|
|
2018-03-04 06:13:53 +01:00
|
|
|
#if ENABLED(EMERGENCY_PARSER)
|
|
|
|
extern bool killed_by_M112;
|
|
|
|
#endif
|
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
class MarlinSerial { //: public Stream
|
|
|
|
|
|
|
|
public:
|
|
|
|
MarlinSerial() {};
|
2017-04-02 11:24:28 +02:00
|
|
|
static void begin(const long);
|
2017-04-02 07:47:20 +02:00
|
|
|
static void end();
|
|
|
|
static int peek(void);
|
|
|
|
static int read(void);
|
|
|
|
static void flush(void);
|
2017-08-08 07:46:37 +02:00
|
|
|
static ring_buffer_pos_t available(void);
|
2017-04-02 07:47:20 +02:00
|
|
|
static void checkRx(void);
|
2017-04-02 11:24:28 +02:00
|
|
|
static void write(const uint8_t c);
|
2017-04-02 07:47:20 +02:00
|
|
|
#if TX_BUFFER_SIZE > 0
|
|
|
|
static uint8_t availableForWrite(void);
|
|
|
|
static void flushTX(void);
|
|
|
|
#endif
|
2017-10-02 07:26:56 +02:00
|
|
|
static void writeNoHandshake(const uint8_t c);
|
2017-08-08 07:46:37 +02:00
|
|
|
|
2017-10-02 07:26:56 +02:00
|
|
|
#if ENABLED(SERIAL_STATS_DROPPED_RX)
|
|
|
|
FORCE_INLINE static uint32_t dropped() { return rx_dropped_bytes; }
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
|
|
|
|
FORCE_INLINE static ring_buffer_pos_t rxMaxEnqueued() { return rx_max_enqueued; }
|
2017-10-06 21:58:37 +02:00
|
|
|
#endif
|
2017-04-02 07:47:20 +02:00
|
|
|
|
|
|
|
private:
|
2017-04-02 11:24:28 +02:00
|
|
|
static void printNumber(unsigned long, const uint8_t);
|
2017-04-02 07:47:20 +02:00
|
|
|
static void printFloat(double, uint8_t);
|
|
|
|
|
|
|
|
public:
|
2017-12-08 08:03:31 +01:00
|
|
|
FORCE_INLINE static void write(const char* str) { while (*str) write(*str++); }
|
|
|
|
FORCE_INLINE static void write(const uint8_t* buffer, size_t size) { while (size--) write(*buffer++); }
|
|
|
|
FORCE_INLINE static void print(const String& s) { for (int i = 0; i < (int)s.length(); i++) write(s[i]); }
|
|
|
|
FORCE_INLINE static void print(const char* str) { write(str); }
|
2017-04-02 07:47:20 +02:00
|
|
|
|
|
|
|
static void print(char, int = BYTE);
|
|
|
|
static void print(unsigned char, int = BYTE);
|
|
|
|
static void print(int, int = DEC);
|
|
|
|
static void print(unsigned int, int = DEC);
|
|
|
|
static void print(long, int = DEC);
|
|
|
|
static void print(unsigned long, int = DEC);
|
|
|
|
static void print(double, int = 2);
|
|
|
|
|
|
|
|
static void println(const String& s);
|
|
|
|
static void println(const char[]);
|
|
|
|
static void println(char, int = BYTE);
|
|
|
|
static void println(unsigned char, int = BYTE);
|
|
|
|
static void println(int, int = DEC);
|
|
|
|
static void println(unsigned int, int = DEC);
|
|
|
|
static void println(long, int = DEC);
|
|
|
|
static void println(unsigned long, int = DEC);
|
|
|
|
static void println(double, int = 2);
|
|
|
|
static void println(void);
|
|
|
|
};
|
|
|
|
|
|
|
|
extern MarlinSerial customizedSerial;
|
2015-01-24 06:11:50 +01:00
|
|
|
|
2015-05-24 18:32:34 +02:00
|
|
|
#endif // !USBCON
|
2011-11-27 21:12:55 +01:00
|
|
|
|
2015-08-01 05:31:53 +02:00
|
|
|
// Use the UART for Bluetooth in AT90USB configurations
|
2015-08-01 01:55:47 +02:00
|
|
|
#if defined(USBCON) && ENABLED(BLUETOOTH)
|
2015-08-01 05:31:53 +02:00
|
|
|
extern HardwareSerial bluetoothSerial;
|
2013-10-30 11:45:32 +01:00
|
|
|
#endif
|
|
|
|
|
2017-04-02 07:47:20 +02:00
|
|
|
#endif // MARLINSERIAL_H
|