2011-11-27 14:12:55 -06: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
|
2011-12-22 07:55:45 -06:00
|
|
|
#include "Marlin.h"
|
2011-11-28 14:51:44 -06:00
|
|
|
|
2012-11-05 04:34:27 -06:00
|
|
|
#if !defined(SERIAL_PORT)
|
|
|
|
#error SERIAL_PORT not set
|
|
|
|
#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)
|
2012-11-06 14:04:29 -06:00
|
|
|
#if SERIAL_PORT == 0 && !defined(UBRR0H)
|
2012-11-06 07:43:36 -06:00
|
|
|
#define SERIAL_REGNAME_INTERNAL(registerbase,number,suffix) registerbase##suffix
|
|
|
|
#else
|
2012-11-05 04:34:27 -06:00
|
|
|
#define SERIAL_REGNAME_INTERNAL(registerbase,number,suffix) registerbase##number##suffix
|
2012-11-06 07:43:36 -06:00
|
|
|
#endif
|
2012-11-05 04:34:27 -06:00
|
|
|
|
|
|
|
// 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 UCSRxA where x 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,)
|
|
|
|
#if SERIAL_PORT == 0 && !defined(UDR0)
|
|
|
|
#if defined(UDR)
|
|
|
|
#define M_UDRx UDR // atmega8, atmega32
|
|
|
|
#else
|
|
|
|
#error UDR not defined
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#define M_UDRx SERIAL_REGNAME(UDR,SERIAL_PORT,)
|
|
|
|
#endif
|
|
|
|
#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 01:32:28 -06:00
|
|
|
|
|
|
|
#define DEC 10
|
|
|
|
#define HEX 16
|
|
|
|
#define OCT 8
|
|
|
|
#define BIN 2
|
|
|
|
#define BYTE 0
|
|
|
|
|
2011-11-27 14:12:55 -06:00
|
|
|
|
2012-02-09 12:27:45 -06:00
|
|
|
#if MOTHERBOARD != 8 // ! teensylu
|
2011-11-28 12:13:40 -06: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.
|
|
|
|
#define RX_BUFFER_SIZE 128
|
2011-11-27 14:12:55 -06:00
|
|
|
|
2011-11-28 12:13:40 -06:00
|
|
|
|
|
|
|
struct ring_buffer
|
|
|
|
{
|
|
|
|
unsigned char buffer[RX_BUFFER_SIZE];
|
|
|
|
int head;
|
|
|
|
int tail;
|
|
|
|
};
|
|
|
|
|
2012-11-05 04:34:27 -06:00
|
|
|
#if UART_PRESENT(SERIAL_PORT)
|
2011-11-28 12:13:40 -06:00
|
|
|
extern ring_buffer rx_buffer;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
class MarlinSerial //: public Stream
|
2011-11-27 14:12:55 -06:00
|
|
|
{
|
2011-11-28 12:28:38 -06:00
|
|
|
|
2011-11-27 14:12:55 -06:00
|
|
|
public:
|
2011-11-28 12:28:38 -06:00
|
|
|
MarlinSerial();
|
2011-11-27 14:12:55 -06:00
|
|
|
void begin(long);
|
|
|
|
void end();
|
2011-11-28 12:34:42 -06:00
|
|
|
int peek(void);
|
|
|
|
int read(void);
|
|
|
|
void flush(void);
|
|
|
|
|
2011-11-28 14:51:44 -06:00
|
|
|
FORCE_INLINE int available(void)
|
2011-11-28 12:13:40 -06:00
|
|
|
{
|
|
|
|
return (unsigned int)(RX_BUFFER_SIZE + rx_buffer.head - rx_buffer.tail) % RX_BUFFER_SIZE;
|
|
|
|
}
|
2011-11-28 12:34:42 -06:00
|
|
|
|
2011-11-28 14:51:44 -06:00
|
|
|
FORCE_INLINE void write(uint8_t c)
|
2011-11-28 12:13:40 -06:00
|
|
|
{
|
2012-11-05 04:34:27 -06:00
|
|
|
while (!((M_UCSRxA) & (1 << M_UDREx)))
|
2011-11-28 12:13:40 -06:00
|
|
|
;
|
|
|
|
|
2012-11-05 04:34:27 -06:00
|
|
|
M_UDRx = c;
|
2011-11-28 12:13:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-28 14:51:44 -06:00
|
|
|
FORCE_INLINE void checkRx(void)
|
2011-11-28 12:13:40 -06:00
|
|
|
{
|
2012-11-05 04:34:27 -06:00
|
|
|
if((M_UCSRxA & (1<<M_RXCx)) != 0) {
|
|
|
|
unsigned char c = M_UDRx;
|
2011-11-28 12:13:40 -06: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
void printNumber(unsigned long, uint8_t);
|
|
|
|
void printFloat(double, uint8_t);
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2011-11-28 14:51:44 -06:00
|
|
|
FORCE_INLINE void write(const char *str)
|
2011-11-28 12:34:42 -06:00
|
|
|
{
|
|
|
|
while (*str)
|
|
|
|
write(*str++);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-28 14:51:44 -06:00
|
|
|
FORCE_INLINE void write(const uint8_t *buffer, size_t size)
|
2011-11-28 12:34:42 -06:00
|
|
|
{
|
|
|
|
while (size--)
|
|
|
|
write(*buffer++);
|
|
|
|
}
|
|
|
|
|
2011-11-28 14:51:44 -06:00
|
|
|
FORCE_INLINE void print(const String &s)
|
2011-11-28 12:34:42 -06:00
|
|
|
{
|
2011-12-04 05:40:18 -06:00
|
|
|
for (int i = 0; i < (int)s.length(); i++) {
|
2011-11-28 12:34:42 -06:00
|
|
|
write(s[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-28 14:51:44 -06:00
|
|
|
FORCE_INLINE void print(const char *str)
|
2011-11-28 12:34:42 -06:00
|
|
|
{
|
|
|
|
write(str);
|
|
|
|
}
|
2011-11-28 12:13:40 -06: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);
|
|
|
|
|
|
|
|
void println(const String &s);
|
|
|
|
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 14:12:55 -06:00
|
|
|
};
|
|
|
|
|
2012-02-09 12:27:45 -06:00
|
|
|
extern MarlinSerial MSerial;
|
|
|
|
#endif // ! teensylu
|
2011-11-27 14:12:55 -06:00
|
|
|
|
2012-11-05 04:34:27 -06:00
|
|
|
#endif
|