1 |
ron |
1 |
/**
|
|
|
2 |
* Marlin 3D Printer Firmware
|
|
|
3 |
* Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
|
|
4 |
*
|
|
|
5 |
* Based on Sprinter and grbl.
|
|
|
6 |
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
|
|
|
7 |
*
|
|
|
8 |
* This program is free software: you can redistribute it and/or modify
|
|
|
9 |
* it under the terms of the GNU General Public License as published by
|
|
|
10 |
* the Free Software Foundation, either version 3 of the License, or
|
|
|
11 |
* (at your option) any later version.
|
|
|
12 |
*
|
|
|
13 |
* This program is distributed in the hope that it will be useful,
|
|
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
16 |
* GNU General Public License for more details.
|
|
|
17 |
*
|
|
|
18 |
* You should have received a copy of the GNU General Public License
|
|
|
19 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
20 |
*
|
|
|
21 |
*/
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* MarlinSerial.h - Hardware serial library for Wiring
|
|
|
25 |
* Copyright (c) 2006 Nicholas Zambetti. All right reserved.
|
|
|
26 |
*
|
|
|
27 |
* Modified 28 September 2010 by Mark Sproul
|
|
|
28 |
* Modified 14 February 2016 by Andreas Hardtung (added tx buffer)
|
|
|
29 |
* Modified 01 October 2017 by Eduardo José Tagle (added XON/XOFF)
|
|
|
30 |
*/
|
|
|
31 |
|
|
|
32 |
#ifndef _MARLINSERIAL_H_
|
|
|
33 |
#define _MARLINSERIAL_H_
|
|
|
34 |
|
|
|
35 |
#include "MarlinConfig.h"
|
|
|
36 |
|
|
|
37 |
#ifndef SERIAL_PORT
|
|
|
38 |
#define SERIAL_PORT 0
|
|
|
39 |
#endif
|
|
|
40 |
|
|
|
41 |
// The presence of the UBRRH register is used to detect a UART.
|
|
|
42 |
#define UART_PRESENT(port) ((port == 0 && (defined(UBRRH) || defined(UBRR0H))) || \
|
|
|
43 |
(port == 1 && defined(UBRR1H)) || (port == 2 && defined(UBRR2H)) || \
|
|
|
44 |
(port == 3 && defined(UBRR3H)))
|
|
|
45 |
|
|
|
46 |
// These are macros to build serial port register names for the selected SERIAL_PORT (C preprocessor
|
|
|
47 |
// requires two levels of indirection to expand macro values properly)
|
|
|
48 |
#define SERIAL_REGNAME(registerbase,number,suffix) SERIAL_REGNAME_INTERNAL(registerbase,number,suffix)
|
|
|
49 |
#if SERIAL_PORT == 0 && (!defined(UBRR0H) || !defined(UDR0)) // use un-numbered registers if necessary
|
|
|
50 |
#define SERIAL_REGNAME_INTERNAL(registerbase,number,suffix) registerbase##suffix
|
|
|
51 |
#else
|
|
|
52 |
#define SERIAL_REGNAME_INTERNAL(registerbase,number,suffix) registerbase##number##suffix
|
|
|
53 |
#endif
|
|
|
54 |
|
|
|
55 |
// Registers used by MarlinSerial class (expanded depending on selected serial port)
|
|
|
56 |
#define M_UCSRxA SERIAL_REGNAME(UCSR,SERIAL_PORT,A) // defines M_UCSRxA to be UCSRnA where n is the serial port number
|
|
|
57 |
#define M_UCSRxB SERIAL_REGNAME(UCSR,SERIAL_PORT,B)
|
|
|
58 |
#define M_RXENx SERIAL_REGNAME(RXEN,SERIAL_PORT,)
|
|
|
59 |
#define M_TXENx SERIAL_REGNAME(TXEN,SERIAL_PORT,)
|
|
|
60 |
#define M_TXCx SERIAL_REGNAME(TXC,SERIAL_PORT,)
|
|
|
61 |
#define M_RXCIEx SERIAL_REGNAME(RXCIE,SERIAL_PORT,)
|
|
|
62 |
#define M_UDREx SERIAL_REGNAME(UDRE,SERIAL_PORT,)
|
|
|
63 |
#define M_FEx SERIAL_REGNAME(FE,SERIAL_PORT,)
|
|
|
64 |
#define M_DORx SERIAL_REGNAME(DOR,SERIAL_PORT,)
|
|
|
65 |
#define M_UPEx SERIAL_REGNAME(UPE,SERIAL_PORT,)
|
|
|
66 |
#define M_UDRIEx SERIAL_REGNAME(UDRIE,SERIAL_PORT,)
|
|
|
67 |
#define M_UDRx SERIAL_REGNAME(UDR,SERIAL_PORT,)
|
|
|
68 |
#define M_UBRRxH SERIAL_REGNAME(UBRR,SERIAL_PORT,H)
|
|
|
69 |
#define M_UBRRxL SERIAL_REGNAME(UBRR,SERIAL_PORT,L)
|
|
|
70 |
#define M_RXCx SERIAL_REGNAME(RXC,SERIAL_PORT,)
|
|
|
71 |
#define M_USARTx_RX_vect SERIAL_REGNAME(USART,SERIAL_PORT,_RX_vect)
|
|
|
72 |
#define M_U2Xx SERIAL_REGNAME(U2X,SERIAL_PORT,)
|
|
|
73 |
#define M_USARTx_UDRE_vect SERIAL_REGNAME(USART,SERIAL_PORT,_UDRE_vect)
|
|
|
74 |
|
|
|
75 |
#define DEC 10
|
|
|
76 |
#define HEX 16
|
|
|
77 |
#define OCT 8
|
|
|
78 |
#define BIN 2
|
|
|
79 |
#define BYTE 0
|
|
|
80 |
|
|
|
81 |
// Define constants and variables for buffering serial data.
|
|
|
82 |
// Use only 0 or powers of 2 greater than 1
|
|
|
83 |
// : [0, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, ...]
|
|
|
84 |
#ifndef RX_BUFFER_SIZE
|
|
|
85 |
#define RX_BUFFER_SIZE 128
|
|
|
86 |
#endif
|
|
|
87 |
// 256 is the max TX buffer limit due to uint8_t head and tail.
|
|
|
88 |
#ifndef TX_BUFFER_SIZE
|
|
|
89 |
#define TX_BUFFER_SIZE 32
|
|
|
90 |
#endif
|
|
|
91 |
|
|
|
92 |
#if USE_MARLINSERIAL
|
|
|
93 |
|
|
|
94 |
#if RX_BUFFER_SIZE > 256
|
|
|
95 |
typedef uint16_t ring_buffer_pos_t;
|
|
|
96 |
#else
|
|
|
97 |
typedef uint8_t ring_buffer_pos_t;
|
|
|
98 |
#endif
|
|
|
99 |
|
|
|
100 |
#if ENABLED(SERIAL_STATS_DROPPED_RX)
|
|
|
101 |
extern uint8_t rx_dropped_bytes;
|
|
|
102 |
#endif
|
|
|
103 |
|
|
|
104 |
#if ENABLED(SERIAL_STATS_RX_BUFFER_OVERRUNS)
|
|
|
105 |
extern uint8_t rx_buffer_overruns;
|
|
|
106 |
#endif
|
|
|
107 |
|
|
|
108 |
#if ENABLED(SERIAL_STATS_RX_FRAMING_ERRORS)
|
|
|
109 |
extern uint8_t rx_framing_errors;
|
|
|
110 |
#endif
|
|
|
111 |
|
|
|
112 |
#if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
|
|
|
113 |
extern ring_buffer_pos_t rx_max_enqueued;
|
|
|
114 |
#endif
|
|
|
115 |
|
|
|
116 |
class MarlinSerial {
|
|
|
117 |
|
|
|
118 |
public:
|
|
|
119 |
MarlinSerial() {};
|
|
|
120 |
static void begin(const long);
|
|
|
121 |
static void end();
|
|
|
122 |
static int peek(void);
|
|
|
123 |
static int read(void);
|
|
|
124 |
static void flush(void);
|
|
|
125 |
static ring_buffer_pos_t available(void);
|
|
|
126 |
static void write(const uint8_t c);
|
|
|
127 |
static void flushTX(void);
|
|
|
128 |
|
|
|
129 |
#if ENABLED(SERIAL_STATS_DROPPED_RX)
|
|
|
130 |
FORCE_INLINE static uint32_t dropped() { return rx_dropped_bytes; }
|
|
|
131 |
#endif
|
|
|
132 |
|
|
|
133 |
#if ENABLED(SERIAL_STATS_RX_BUFFER_OVERRUNS)
|
|
|
134 |
FORCE_INLINE static uint32_t buffer_overruns() { return rx_buffer_overruns; }
|
|
|
135 |
#endif
|
|
|
136 |
|
|
|
137 |
#if ENABLED(SERIAL_STATS_RX_FRAMING_ERRORS)
|
|
|
138 |
FORCE_INLINE static uint32_t framing_errors() { return rx_framing_errors; }
|
|
|
139 |
#endif
|
|
|
140 |
|
|
|
141 |
#if ENABLED(SERIAL_STATS_MAX_RX_QUEUED)
|
|
|
142 |
FORCE_INLINE static ring_buffer_pos_t rxMaxEnqueued() { return rx_max_enqueued; }
|
|
|
143 |
#endif
|
|
|
144 |
|
|
|
145 |
FORCE_INLINE static void write(const char* str) { while (*str) write(*str++); }
|
|
|
146 |
FORCE_INLINE static void write(const uint8_t* buffer, size_t size) { while (size--) write(*buffer++); }
|
|
|
147 |
FORCE_INLINE static void print(const String& s) { for (int i = 0; i < (int)s.length(); i++) write(s[i]); }
|
|
|
148 |
FORCE_INLINE static void print(const char* str) { write(str); }
|
|
|
149 |
|
|
|
150 |
static void print(char, int = BYTE);
|
|
|
151 |
static void print(unsigned char, int = BYTE);
|
|
|
152 |
static void print(int, int = DEC);
|
|
|
153 |
static void print(unsigned int, int = DEC);
|
|
|
154 |
static void print(long, int = DEC);
|
|
|
155 |
static void print(unsigned long, int = DEC);
|
|
|
156 |
static void print(double, int = 2);
|
|
|
157 |
|
|
|
158 |
static void println(const String& s);
|
|
|
159 |
static void println(const char[]);
|
|
|
160 |
static void println(char, int = BYTE);
|
|
|
161 |
static void println(unsigned char, int = BYTE);
|
|
|
162 |
static void println(int, int = DEC);
|
|
|
163 |
static void println(unsigned int, int = DEC);
|
|
|
164 |
static void println(long, int = DEC);
|
|
|
165 |
static void println(unsigned long, int = DEC);
|
|
|
166 |
static void println(double, int = 2);
|
|
|
167 |
static void println(void);
|
|
|
168 |
operator bool() { return true; }
|
|
|
169 |
|
|
|
170 |
private:
|
|
|
171 |
static void printNumber(unsigned long, const uint8_t);
|
|
|
172 |
static void printFloat(double, uint8_t);
|
|
|
173 |
};
|
|
|
174 |
|
|
|
175 |
extern MarlinSerial customizedSerial;
|
|
|
176 |
|
|
|
177 |
#endif // USE_MARLINSERIAL
|
|
|
178 |
|
|
|
179 |
// Use the UART for Bluetooth in AT90USB configurations
|
|
|
180 |
#if !USE_MARLINSERIAL && ENABLED(BLUETOOTH)
|
|
|
181 |
extern HardwareSerial bluetoothSerial;
|
|
|
182 |
#endif
|
|
|
183 |
|
|
|
184 |
#endif // _MARLINSERIAL_H_
|