|
@@ -23,6 +23,44 @@
|
23
|
23
|
#define MarlinSerial_h
|
24
|
24
|
#include "Marlin.h"
|
25
|
25
|
|
|
26
|
+#if !defined(SERIAL_PORT)
|
|
27
|
+#error SERIAL_PORT not set
|
|
28
|
+#endif
|
|
29
|
+
|
|
30
|
+// The presence of the UBRRH register is used to detect a UART.
|
|
31
|
+#define UART_PRESENT(port) ((port == 0 && (defined(UBRRH) || defined(UBRR0H))) || \
|
|
32
|
+ (port == 1 && defined(UBRR1H)) || (port == 2 && defined(UBRR2H)) || \
|
|
33
|
+ (port == 3 && defined(UBRR3H)))
|
|
34
|
+
|
|
35
|
+// These are macros to build serial port register names for the selected SERIAL_PORT (C preprocessor
|
|
36
|
+// requires two levels of indirection to expand macro values properly)
|
|
37
|
+#define SERIAL_REGNAME(registerbase,number,suffix) SERIAL_REGNAME_INTERNAL(registerbase,number,suffix)
|
|
38
|
+#define SERIAL_REGNAME_INTERNAL(registerbase,number,suffix) registerbase##number##suffix
|
|
39
|
+
|
|
40
|
+// Registers used by MarlinSerial class (these are expanded
|
|
41
|
+// depending on selected serial port
|
|
42
|
+#define M_UCSRxA SERIAL_REGNAME(UCSR,SERIAL_PORT,A) // defines M_UCSRxA to be UCSRxA where x is the serial port number
|
|
43
|
+#define M_UCSRxB SERIAL_REGNAME(UCSR,SERIAL_PORT,B)
|
|
44
|
+#define M_RXENx SERIAL_REGNAME(RXEN,SERIAL_PORT,)
|
|
45
|
+#define M_TXENx SERIAL_REGNAME(TXEN,SERIAL_PORT,)
|
|
46
|
+#define M_RXCIEx SERIAL_REGNAME(RXCIE,SERIAL_PORT,)
|
|
47
|
+#define M_UDREx SERIAL_REGNAME(UDRE,SERIAL_PORT,)
|
|
48
|
+#if SERIAL_PORT == 0 && !defined(UDR0)
|
|
49
|
+ #if defined(UDR)
|
|
50
|
+ #define M_UDRx UDR // atmega8, atmega32
|
|
51
|
+ #else
|
|
52
|
+ #error UDR not defined
|
|
53
|
+ #endif
|
|
54
|
+#else
|
|
55
|
+ #define M_UDRx SERIAL_REGNAME(UDR,SERIAL_PORT,)
|
|
56
|
+#endif
|
|
57
|
+#define M_UBRRxH SERIAL_REGNAME(UBRR,SERIAL_PORT,H)
|
|
58
|
+#define M_UBRRxL SERIAL_REGNAME(UBRR,SERIAL_PORT,L)
|
|
59
|
+#define M_RXCx SERIAL_REGNAME(RXC,SERIAL_PORT,)
|
|
60
|
+#define M_USARTx_RX_vect SERIAL_REGNAME(USART,SERIAL_PORT,_RX_vect)
|
|
61
|
+#define M_U2Xx SERIAL_REGNAME(U2X,SERIAL_PORT,)
|
|
62
|
+
|
|
63
|
+
|
26
|
64
|
|
27
|
65
|
#define DEC 10
|
28
|
66
|
#define HEX 16
|
|
@@ -46,7 +84,7 @@ struct ring_buffer
|
46
|
84
|
int tail;
|
47
|
85
|
};
|
48
|
86
|
|
49
|
|
-#if defined(UBRRH) || defined(UBRR0H)
|
|
87
|
+#if UART_PRESENT(SERIAL_PORT)
|
50
|
88
|
extern ring_buffer rx_buffer;
|
51
|
89
|
#endif
|
52
|
90
|
|
|
@@ -68,17 +106,17 @@ class MarlinSerial //: public Stream
|
68
|
106
|
|
69
|
107
|
FORCE_INLINE void write(uint8_t c)
|
70
|
108
|
{
|
71
|
|
- while (!((UCSR0A) & (1 << UDRE0)))
|
|
109
|
+ while (!((M_UCSRxA) & (1 << M_UDREx)))
|
72
|
110
|
;
|
73
|
111
|
|
74
|
|
- UDR0 = c;
|
|
112
|
+ M_UDRx = c;
|
75
|
113
|
}
|
76
|
114
|
|
77
|
115
|
|
78
|
116
|
FORCE_INLINE void checkRx(void)
|
79
|
117
|
{
|
80
|
|
- if((UCSR0A & (1<<RXC0)) != 0) {
|
81
|
|
- unsigned char c = UDR0;
|
|
118
|
+ if((M_UCSRxA & (1<<M_RXCx)) != 0) {
|
|
119
|
+ unsigned char c = M_UDRx;
|
82
|
120
|
int i = (unsigned int)(rx_buffer.head + 1) % RX_BUFFER_SIZE;
|
83
|
121
|
|
84
|
122
|
// if we should be storing the received character into the location
|
|
@@ -147,4 +185,4 @@ class MarlinSerial //: public Stream
|
147
|
185
|
extern MarlinSerial MSerial;
|
148
|
186
|
#endif // ! teensylu
|
149
|
187
|
|
150
|
|
-#endif
|
|
188
|
+#endif
|