2016-07-22 13:28:01 +00:00
|
|
|
/*
|
|
|
|
HardwareSerial.h - Hardware serial library for Wiring
|
|
|
|
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library 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
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
|
|
|
Modified 28 September 2010 by Mark Sproul
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MarlinSerial_h
|
|
|
|
#define MarlinSerial_h
|
|
|
|
#include "Marlin.h"
|
|
|
|
|
|
|
|
#if !defined(SERIAL_PORT)
|
|
|
|
#define SERIAL_PORT 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// The presence of the UBRRH register is used to detect a UART.
|
|
|
|
#define UART_PRESENT(port) ((port == 0 && (defined(UBRRH) || defined(UBRR0H))) || \
|
|
|
|
(port == 1 && defined(UBRR1H)) || (port == 2 && defined(UBRR2H)) || \
|
|
|
|
(port == 3 && defined(UBRR3H)))
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
#if SERIAL_PORT == 0 && (!defined(UBRR0H) || !defined(UDR0)) // use un-numbered registers if necessary
|
|
|
|
#define SERIAL_REGNAME_INTERNAL(registerbase,number,suffix) registerbase##suffix
|
|
|
|
#else
|
|
|
|
#define SERIAL_REGNAME_INTERNAL(registerbase,number,suffix) registerbase##number##suffix
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Registers used by MarlinSerial class (these are 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_RXCIEx SERIAL_REGNAME(RXCIE,SERIAL_PORT,)
|
|
|
|
#define M_UDREx SERIAL_REGNAME(UDRE,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_FEx SERIAL_REGNAME(FE,SERIAL_PORT,)
|
|
|
|
#define M_USARTx_RX_vect SERIAL_REGNAME(USART,SERIAL_PORT,_RX_vect)
|
|
|
|
#define M_U2Xx SERIAL_REGNAME(U2X,SERIAL_PORT,)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define DEC 10
|
|
|
|
#define HEX 16
|
|
|
|
#define OCT 8
|
|
|
|
#define BIN 2
|
|
|
|
#define BYTE 0
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef AT90USB
|
|
|
|
// 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.
|
|
|
|
#define RX_BUFFER_SIZE 128
|
|
|
|
|
2017-10-24 21:20:48 +00:00
|
|
|
extern uint8_t selectedSerialPort;
|
2016-07-22 13:28:01 +00:00
|
|
|
|
|
|
|
struct ring_buffer
|
|
|
|
{
|
|
|
|
unsigned char buffer[RX_BUFFER_SIZE];
|
|
|
|
int head;
|
|
|
|
int tail;
|
|
|
|
};
|
|
|
|
|
|
|
|
#if UART_PRESENT(SERIAL_PORT)
|
|
|
|
extern ring_buffer rx_buffer;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
class MarlinSerial //: public Stream
|
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
2018-02-02 19:25:19 +00:00
|
|
|
static void begin(long);
|
|
|
|
static void end();
|
|
|
|
static int peek(void);
|
|
|
|
static int read(void);
|
|
|
|
static void flush(void);
|
2016-07-22 13:28:01 +00:00
|
|
|
|
2018-02-02 19:25:19 +00:00
|
|
|
static FORCE_INLINE int available(void)
|
2016-07-22 13:28:01 +00:00
|
|
|
{
|
|
|
|
return (unsigned int)(RX_BUFFER_SIZE + rx_buffer.head - rx_buffer.tail) % RX_BUFFER_SIZE;
|
|
|
|
}
|
2017-12-07 18:14:17 +00:00
|
|
|
/*
|
2016-07-22 13:28:01 +00:00
|
|
|
FORCE_INLINE void write(uint8_t c)
|
|
|
|
{
|
|
|
|
while (!((M_UCSRxA) & (1 << M_UDREx)))
|
|
|
|
;
|
|
|
|
|
|
|
|
M_UDRx = c;
|
|
|
|
}
|
2017-12-07 18:14:17 +00:00
|
|
|
*/
|
2018-02-02 19:25:19 +00:00
|
|
|
static void write(uint8_t c)
|
2018-01-02 19:55:33 +00:00
|
|
|
{
|
|
|
|
if (selectedSerialPort == 0)
|
2017-12-07 18:14:17 +00:00
|
|
|
{
|
2018-01-02 19:55:33 +00:00
|
|
|
while (!((M_UCSRxA) & (1 << M_UDREx)));
|
|
|
|
M_UDRx = c;
|
2017-12-07 18:14:17 +00:00
|
|
|
}
|
2018-01-02 19:55:33 +00:00
|
|
|
else if (selectedSerialPort == 1)
|
|
|
|
{
|
|
|
|
while (!((UCSR1A) & (1 << UDRE1)));
|
|
|
|
UDR1 = c;
|
2017-12-07 18:14:17 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-22 13:28:01 +00:00
|
|
|
|
2018-02-02 19:25:19 +00:00
|
|
|
static void checkRx(void)
|
2016-07-22 13:28:01 +00:00
|
|
|
{
|
2017-09-13 11:35:22 +00:00
|
|
|
if (selectedSerialPort == 0) {
|
|
|
|
if((M_UCSRxA & (1<<M_RXCx)) != 0) {
|
|
|
|
// Test for a framing error.
|
|
|
|
if (M_UCSRxA & (1<<M_FEx)) {
|
|
|
|
// Characters received with the framing errors will be ignored.
|
|
|
|
// The temporary variable "c" was made volatile, so the compiler does not optimize this out.
|
2018-05-30 11:26:31 +00:00
|
|
|
(void)(*(char *)M_UDRx);
|
2017-09-13 11:35:22 +00:00
|
|
|
} else {
|
|
|
|
unsigned char c = M_UDRx;
|
|
|
|
int i = (unsigned int)(rx_buffer.head + 1) % RX_BUFFER_SIZE;
|
|
|
|
// 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[rx_buffer.head] = c;
|
|
|
|
rx_buffer.head = i;
|
|
|
|
}
|
2018-01-02 19:55:33 +00:00
|
|
|
//selectedSerialPort = 0;
|
|
|
|
#ifdef DEBUG_DUMP_TO_2ND_SERIAL
|
|
|
|
UDR1 = c;
|
|
|
|
#endif //DEBUG_DUMP_TO_2ND_SERIAL
|
2017-09-13 11:35:22 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-02 19:25:19 +00:00
|
|
|
} else { // if(selectedSerialPort == 1) {
|
2017-12-07 18:14:17 +00:00
|
|
|
if((UCSR1A & (1<<RXC1)) != 0) {
|
2017-09-13 11:35:22 +00:00
|
|
|
// Test for a framing error.
|
2017-12-07 18:14:17 +00:00
|
|
|
if (UCSR1A & (1<<FE1)) {
|
2017-09-13 11:35:22 +00:00
|
|
|
// Characters received with the framing errors will be ignored.
|
|
|
|
// The temporary variable "c" was made volatile, so the compiler does not optimize this out.
|
2018-05-30 11:26:31 +00:00
|
|
|
(void)(*(char *)UDR1);
|
2017-09-13 11:35:22 +00:00
|
|
|
} else {
|
2017-12-07 18:14:17 +00:00
|
|
|
unsigned char c = UDR1;
|
2017-09-13 11:35:22 +00:00
|
|
|
int i = (unsigned int)(rx_buffer.head + 1) % RX_BUFFER_SIZE;
|
|
|
|
// 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[rx_buffer.head] = c;
|
|
|
|
rx_buffer.head = i;
|
|
|
|
}
|
2018-01-02 19:55:33 +00:00
|
|
|
//selectedSerialPort = 1;
|
|
|
|
#ifdef DEBUG_DUMP_TO_2ND_SERIAL
|
|
|
|
M_UDRx = c;
|
|
|
|
#endif //DEBUG_DUMP_TO_2ND_SERIAL
|
2016-07-22 13:28:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
2018-02-02 19:25:19 +00:00
|
|
|
static void printNumber(unsigned long, uint8_t);
|
|
|
|
static void printFloat(double, uint8_t);
|
2016-07-22 13:28:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2018-02-02 19:25:19 +00:00
|
|
|
static FORCE_INLINE void write(const char *str)
|
2016-07-22 13:28:01 +00:00
|
|
|
{
|
|
|
|
while (*str)
|
|
|
|
write(*str++);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-02 19:25:19 +00:00
|
|
|
static FORCE_INLINE void write(const uint8_t *buffer, size_t size)
|
2016-07-22 13:28:01 +00:00
|
|
|
{
|
|
|
|
while (size--)
|
|
|
|
write(*buffer++);
|
|
|
|
}
|
|
|
|
|
2018-02-02 19:25:19 +00:00
|
|
|
static FORCE_INLINE void print(const String &s)
|
2016-07-22 13:28:01 +00:00
|
|
|
{
|
|
|
|
for (int i = 0; i < (int)s.length(); i++) {
|
|
|
|
write(s[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-02 19:25:19 +00:00
|
|
|
static FORCE_INLINE void print(const char *str)
|
2016-07-22 13:28:01 +00:00
|
|
|
{
|
|
|
|
write(str);
|
|
|
|
}
|
2018-02-02 19:25:19 +00: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);
|
2016-07-22 13:28:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extern MarlinSerial MSerial;
|
|
|
|
#endif // !AT90USB
|
|
|
|
|
|
|
|
// Use the UART for BT in AT90USB configurations
|
|
|
|
#if defined(AT90USB) && defined (BTENABLED)
|
|
|
|
extern HardwareSerial bt;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|