Prusa3d Marlin fork
MarlinSerial.h
1 /*
2  HardwareSerial.h - Hardware serial library for Wiring
3  Copyright (c) 2006 Nicholas Zambetti. All right reserved.
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 
19  Modified 28 September 2010 by Mark Sproul
20 */
21 
22 #ifndef MarlinSerial_h
23 #define MarlinSerial_h
24 #include "Marlin.h"
25 
26 #if !defined(SERIAL_PORT)
27 #define SERIAL_PORT 0
28 #endif
29 
30 // The presence of the UBRRH register is used to detect a UART.
31 #if ((SERIAL_PORT == 0 && (defined(UBRRH) || defined(UBRR0H))) || \
32  (SERIAL_PORT == 1 && defined(UBRR1H)) || \
33  (SERIAL_PORT == 2 && defined(UBRR2H)) || \
34  (SERIAL_PORT == 3 && defined(UBRR3H)))
35 #define HAS_UART
36 #endif
37 
38 // These are macros to build serial port register names for the selected SERIAL_PORT (C preprocessor
39 // requires two levels of indirection to expand macro values properly)
40 #if SERIAL_PORT == 0 && (!defined(UBRR0H) || !defined(UDR0)) // use un-numbered registers if necessary
41 #define SERIAL_REGNAME(registerbase,number,suffix) _REGNAME_SHORT(registerbase, suffix)
42 #else
43 #define SERIAL_REGNAME(registerbase,number,suffix) _REGNAME(registerbase, number, suffix)
44 #endif
45 
46 // Registers used by MarlinSerial class (these are expanded
47 // depending on selected serial port
48 #define M_UCSRxA SERIAL_REGNAME(UCSR,SERIAL_PORT,A) // defines M_UCSRxA to be UCSRnA where n is the serial port number
49 #define M_UCSRxB SERIAL_REGNAME(UCSR,SERIAL_PORT,B)
50 #define M_RXENx SERIAL_REGNAME(RXEN,SERIAL_PORT,)
51 #define M_TXENx SERIAL_REGNAME(TXEN,SERIAL_PORT,)
52 #define M_RXCIEx SERIAL_REGNAME(RXCIE,SERIAL_PORT,)
53 #define M_UDREx SERIAL_REGNAME(UDRE,SERIAL_PORT,)
54 #define M_UDRx SERIAL_REGNAME(UDR,SERIAL_PORT,)
55 #define M_UBRRxH SERIAL_REGNAME(UBRR,SERIAL_PORT,H)
56 #define M_UBRRxL SERIAL_REGNAME(UBRR,SERIAL_PORT,L)
57 #define M_RXCx SERIAL_REGNAME(RXC,SERIAL_PORT,)
58 #define M_FEx SERIAL_REGNAME(FE,SERIAL_PORT,)
59 #define M_USARTx_RX_vect SERIAL_REGNAME(USART,SERIAL_PORT,_RX_vect)
60 #define M_U2Xx SERIAL_REGNAME(U2X,SERIAL_PORT,)
61 
62 
63 
64 #define DEC 10
65 #define HEX 16
66 #define OCT 8
67 #define BIN 2
68 #define BYTE 0
69 
70 
71 #ifndef AT90USB
72 // Define constants and variables for buffering incoming serial data. We're
73 // using a ring buffer (I think), in which rx_buffer_head is the index of the
74 // location to which to write the next incoming character and rx_buffer_tail
75 // is the index of the location from which to read.
76 #define RX_BUFFER_SIZE 128
77 
78 extern uint8_t selectedSerialPort;
79 
81 {
82  unsigned char buffer[RX_BUFFER_SIZE];
83  int head;
84  int tail;
85 };
86 
87 #ifdef HAS_UART
88  extern ring_buffer rx_buffer;
89 #endif
90 
91 class MarlinSerial //: public Stream
92 {
93 
94  public:
95  static void begin(long);
96  static void end();
97  static int peek(void);
98  static int read(void);
99  static void flush(void);
100 
101  static /*FORCE_INLINE*/ int available(void)
102  {
103  return (unsigned int)(RX_BUFFER_SIZE + rx_buffer.head - rx_buffer.tail) % RX_BUFFER_SIZE;
104  }
105  /*
106  FORCE_INLINE void write(uint8_t c)
107  {
108  while (!((M_UCSRxA) & (1 << M_UDREx)))
109  ;
110 
111  M_UDRx = c;
112  }
113  */
114  static void write(uint8_t c)
115  {
116  if (selectedSerialPort == 0)
117  {
118  while (!((M_UCSRxA) & (1 << M_UDREx)));
119  M_UDRx = c;
120  }
121  else if (selectedSerialPort == 1)
122  {
123  while (!((UCSR1A) & (1 << UDRE1)));
124  UDR1 = c;
125  }
126  }
127 
128  static void checkRx(void)
129  {
130  if (selectedSerialPort == 0) {
131  if((M_UCSRxA & (1<<M_RXCx)) != 0) {
132  // Test for a framing error.
133  if (M_UCSRxA & (1<<M_FEx)) {
134  // Characters received with the framing errors will be ignored.
135  // The temporary variable "c" was made volatile, so the compiler does not optimize this out.
136  (void)(*(char *)M_UDRx);
137  } else {
138  unsigned char c = M_UDRx;
139  int i = (unsigned int)(rx_buffer.head + 1) % RX_BUFFER_SIZE;
140  // if we should be storing the received character into the location
141  // just before the tail (meaning that the head would advance to the
142  // current location of the tail), we're about to overflow the buffer
143  // and so we don't write the character or advance the head.
144  if (i != rx_buffer.tail) {
145  rx_buffer.buffer[rx_buffer.head] = c;
146  rx_buffer.head = i;
147  }
148  //selectedSerialPort = 0;
149 #ifdef DEBUG_DUMP_TO_2ND_SERIAL
150  UDR1 = c;
151 #endif //DEBUG_DUMP_TO_2ND_SERIAL
152  }
153  }
154  } else { // if(selectedSerialPort == 1) {
155  if((UCSR1A & (1<<RXC1)) != 0) {
156  // Test for a framing error.
157  if (UCSR1A & (1<<FE1)) {
158  // Characters received with the framing errors will be ignored.
159  // The temporary variable "c" was made volatile, so the compiler does not optimize this out.
160  (void)(*(char *)UDR1);
161  } else {
162  unsigned char c = UDR1;
163  int i = (unsigned int)(rx_buffer.head + 1) % RX_BUFFER_SIZE;
164  // if we should be storing the received character into the location
165  // just before the tail (meaning that the head would advance to the
166  // current location of the tail), we're about to overflow the buffer
167  // and so we don't write the character or advance the head.
168  if (i != rx_buffer.tail) {
169  rx_buffer.buffer[rx_buffer.head] = c;
170  rx_buffer.head = i;
171  }
172  //selectedSerialPort = 1;
173 #ifdef DEBUG_DUMP_TO_2ND_SERIAL
174  M_UDRx = c;
175 #endif //DEBUG_DUMP_TO_2ND_SERIAL
176  }
177  }
178  }
179  }
180 
181 
182  private:
183  static void printNumber(unsigned long, uint8_t);
184  static void printFloat(double, uint8_t);
185 
186 
187  public:
188 
189  static /*FORCE_INLINE*/ void write(const char *str)
190  {
191  while (*str)
192  write(*str++);
193  }
194 
195 
196  static /*FORCE_INLINE*/ void write(const uint8_t *buffer, size_t size)
197  {
198  while (size--)
199  write(*buffer++);
200  }
201 
202 /* static FORCE_INLINE void print(const String &s)
203  {
204  for (int i = 0; i < (int)s.length(); i++) {
205  write(s[i]);
206  }
207  }*/
208 
209  static FORCE_INLINE void print(const char *str)
210  {
211  write(str);
212  }
213  static void print(char, int = BYTE);
214  static void print(unsigned char, int = BYTE);
215  static void print(int, int = DEC);
216  static void print(unsigned int, int = DEC);
217  static void print(long, int = DEC);
218  static void print(unsigned long, int = DEC);
219  static void print(double, int = 2);
220 
221 // static void println(const String &s);
222  static void println(const char[]);
223  static void println(char, int = BYTE);
224  static void println(unsigned char, int = BYTE);
225  static void println(int, int = DEC);
226  static void println(unsigned int, int = DEC);
227  static void println(long, int = DEC);
228  static void println(unsigned long, int = DEC);
229  static void println(double, int = 2);
230  static void println(void);
231 };
232 
233 extern MarlinSerial MSerial;
234 #endif // !AT90USB
235 
236 // Use the UART for BT in AT90USB configurations
237 #if defined(AT90USB) && defined (BTENABLED)
238  extern HardwareSerial bt;
239 #endif
240 
241 #endif
Definition: MarlinSerial.h:92
Definition: MarlinSerial.h:81