MarlinSerial.h

Fri, 17 Nov 2017 10:13:31 +0100

author
mdd
date
Fri, 17 Nov 2017 10:13:31 +0100
changeset 3
770b218a4931
parent 0
2c8ba1964db7
permissions
-rw-r--r--

proper configuration, homing and planner optimization

0
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
1 /*
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
2 HardwareSerial.h - Hardware serial library for Wiring
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
3 Copyright (c) 2006 Nicholas Zambetti. All right reserved.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
4
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
5 This library is free software; you can redistribute it and/or
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
6 modify it under the terms of the GNU Lesser General Public
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
7 License as published by the Free Software Foundation; either
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
8 version 2.1 of the License, or (at your option) any later version.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
9
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
10 This library is distributed in the hope that it will be useful,
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
13 Lesser General Public License for more details.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
14
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
15 You should have received a copy of the GNU Lesser General Public
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
16 License along with this library; if not, write to the Free Software
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
18
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
19 Modified 28 September 2010 by Mark Sproul
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
20 */
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
21
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
22
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
23
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
24 #ifndef MarlinSerial_h
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
25 #define MarlinSerial_h
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
26 #include "Marlin.h"
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
27
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
28 #ifndef REPRAPPRO_MULTIMATERIALS
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
29
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
30 #define DEC 10
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
31 #define HEX 16
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
32 #define OCT 8
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
33 #define BIN 2
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
34 #define BYTE 0
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
35
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
36
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
37 #if MOTHERBOARD != 8 // ! teensylu
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
38 // Define constants and variables for buffering incoming serial data. We're
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
39 // using a ring buffer (I think), in which rx_buffer_head is the index of the
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
40 // location to which to write the next incoming character and rx_buffer_tail
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
41 // is the index of the location from which to read.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
42 #define RX_BUFFER_SIZE 128
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
43
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
44
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
45 struct ring_buffer
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
46 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
47 unsigned char buffer[RX_BUFFER_SIZE];
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
48 int head;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
49 int tail;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
50 };
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
51
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
52 #if defined(UBRRH) || defined(UBRR0H)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
53 extern ring_buffer rx_buffer;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
54 #endif
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
55
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
56 class MarlinSerial //: public Stream
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
57 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
58
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
59 public:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
60 MarlinSerial();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
61 void begin(long);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
62 void end();
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
63 int peek(void);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
64 int read(void);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
65 void flush(void);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
66
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
67 FORCE_INLINE int available(void)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
68 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
69 return (unsigned int)(RX_BUFFER_SIZE + rx_buffer.head - rx_buffer.tail) % RX_BUFFER_SIZE;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
70 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
71
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
72 FORCE_INLINE void write(uint8_t c)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
73 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
74 while (!((UCSR0A) & (1 << UDRE0)))
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
75 ;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
76
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
77 UDR0 = c;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
78 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
79
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
80
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
81 FORCE_INLINE void checkRx(void)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
82 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
83 if((UCSR0A & (1<<RXC0)) != 0) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
84 unsigned char c = UDR0;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
85 int i = (unsigned int)(rx_buffer.head + 1) % RX_BUFFER_SIZE;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
86
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
87 // if we should be storing the received character into the location
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
88 // just before the tail (meaning that the head would advance to the
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
89 // current location of the tail), we're about to overflow the buffer
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
90 // and so we don't write the character or advance the head.
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
91 if (i != rx_buffer.tail) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
92 rx_buffer.buffer[rx_buffer.head] = c;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
93 rx_buffer.head = i;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
94 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
95 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
96 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
97
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
98
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
99 private:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
100 void printNumber(unsigned long, uint8_t);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
101 void printFloat(double, uint8_t);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
102
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
103
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
104 public:
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
105
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
106 FORCE_INLINE void write(const char *str)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
107 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
108 while (*str)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
109 write(*str++);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
110 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
111
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
112
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
113 FORCE_INLINE void write(const uint8_t *buffer, size_t size)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
114 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
115 while (size--)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
116 write(*buffer++);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
117 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
118
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
119 FORCE_INLINE void print(const String &s)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
120 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
121 for (int i = 0; i < (int)s.length(); i++) {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
122 write(s[i]);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
123 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
124 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
125
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
126 FORCE_INLINE void print(const char *str)
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
127 {
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
128 write(str);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
129 }
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
130 void print(char, int = BYTE);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
131 void print(unsigned char, int = BYTE);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
132 void print(int, int = DEC);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
133 void print(unsigned int, int = DEC);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
134 void print(long, int = DEC);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
135 void print(unsigned long, int = DEC);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
136 void print(double, int = 3);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
137
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
138 void println(const String &s);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
139 void println(const char[]);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
140 void println(char, int = BYTE);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
141 void println(unsigned char, int = BYTE);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
142 void println(int, int = DEC);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
143 void println(unsigned int, int = DEC);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
144 void println(long, int = DEC);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
145 void println(unsigned long, int = DEC);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
146 void println(double, int = 2);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
147 void println(void);
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
148 };
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
149
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
150 extern MarlinSerial MSerial;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
151 extern MarlinSerial MSerial1;
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
152 #endif // ! teensylu
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
153
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
154 #endif
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
155
2c8ba1964db7 Initial code from reprappro Marlin repository
mbayer
parents:
diff changeset
156 #endif // REPRAPPRO_MULTIMATERIALS

mercurial