-
Notifications
You must be signed in to change notification settings - Fork 0
/
Endstops.cpp
316 lines (295 loc) · 9.24 KB
/
Endstops.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
This file is part of Repetier-Firmware.
Repetier-Firmware is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Repetier-Firmware 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with Repetier-Firmware. If not, see <http://www.gnu.org/licenses/>.
This firmware is a nearly complete rewrite of the sprinter firmware
by kliment (https://github.com/kliment/Sprinter)
which based on Tonokip RepRap firmware rewrite based off of Hydra-mmm firmware.
Functions in this file are used to communicate using ascii or repetier protocol.
*/
#include "Repetier.h"
flag8_t Endstops::lastState = 0;
flag8_t Endstops::lastRead = 0;
flag8_t Endstops::accumulator = 0;
#ifdef EXTENDED_ENDSTOPS
flag8_t Endstops::lastState2 = 0;
flag8_t Endstops::lastRead2 = 0;
flag8_t Endstops::accumulator2 = 0;
#endif
void Endstops::update() {
flag8_t newRead = 0;
#ifdef EXTENDED_ENDSTOPS
flag8_t newRead2 = 0;
#endif
#if (Y_MIN_PIN > -1) && MIN_HARDWARE_ENDSTOP_Y
if(READ(Y_MIN_PIN) != ENDSTOP_Y_MIN_INVERTING)
newRead |= ENDSTOP_Y_MIN_ID;
#endif
#if (Y_MAX_PIN > -1) && MAX_HARDWARE_ENDSTOP_Y
if(READ(Y_MAX_PIN) != ENDSTOP_Y_MAX_INVERTING)
newRead |= ENDSTOP_Y_MAX_ID;
#endif
#if (X_MIN_PIN > -1) && MIN_HARDWARE_ENDSTOP_X
if(READ(X_MIN_PIN) != ENDSTOP_X_MIN_INVERTING) {
newRead |= ENDSTOP_X_MIN_ID;
}
#endif
#if (X_MAX_PIN > -1) && MAX_HARDWARE_ENDSTOP_X
if(READ(X_MAX_PIN) != ENDSTOP_X_MAX_INVERTING)
newRead |= ENDSTOP_X_MAX_ID;
#endif
#if (Z_MIN_PIN > -1) && MIN_HARDWARE_ENDSTOP_Z
if(READ(Z_MIN_PIN) != ENDSTOP_Z_MIN_INVERTING)
newRead |= ENDSTOP_Z_MIN_ID;
#endif
#if (Z_MAX_PIN > -1) && MAX_HARDWARE_ENDSTOP_Z
if(READ(Z_MAX_PIN) != ENDSTOP_Z_MAX_INVERTING)
newRead |= ENDSTOP_Z_MAX_ID;
#endif
#if (Z2_MINMAX_PIN > -1) && MINMAX_HARDWARE_ENDSTOP_Z2
if(READ(Z2_MINMAX_PIN) != ENDSTOP_Z2_MINMAX_INVERTING)
newRead |= ENDSTOP_Z2_MINMAX_ID;
#endif
#if FEATURE_Z_PROBE
#if Z_PROBE_PIN == Z_MIN_PIN && MIN_HARDWARE_ENDSTOP_Z
if(newRead & ENDSTOP_Z_MIN_ID) // prevent different results causing confusion
newRead |= ENDSTOP_Z_PROBE_ID;
if(!Printer::isHoming())
newRead &= ~ENDSTOP_Z_MIN_ID; // could cause wrong signals depending on probe position
#else
if(Z_PROBE_ON_HIGH ? READ(Z_PROBE_PIN) : !READ(Z_PROBE_PIN))
newRead |= ENDSTOP_Z_PROBE_ID;
#endif
#endif
#ifdef EXTENDED_ENDSTOPS
#if HAS_PIN(Y2_MIN) && MIN_HARDWARE_ENDSTOP_Y2
if(READ(Y2_MIN_PIN) != ENDSTOP_Y2_MIN_INVERTING)
newRead2 |= ENDSTOP_Y2_MIN_ID;
#endif
#if HAS_PIN(Y2_MAX) && MAX_HARDWARE_ENDSTOP_Y2
if(READ(Y2_MAX_PIN) != ENDSTOP_Y2_MAX_INVERTING)
newRead2 |= ENDSTOP_Y2_MAX_ID;
#endif
#if HAS_PIN(X2_MIN) && MIN_HARDWARE_ENDSTOP_X2
if(READ(X2_MIN_PIN) != ENDSTOP_X2_MIN_INVERTING) {
newRead2 |= ENDSTOP_X2_MIN_ID;
}
#endif
#if HAS_PIN(X2_MAX) && MAX_HARDWARE_ENDSTOP_X2
if(READ(X2_MAX_PIN) != ENDSTOP_X2_MAX_INVERTING)
newRead2 |= ENDSTOP_X2_MAX_ID;
#endif
#if HAS_PIN(Z2_MAX) && MAX_HARDWARE_ENDSTOP_Z2
if(READ(Z2_MAX_PIN) != ENDSTOP_Z2_MAX_INVERTING)
newRead2 |= ENDSTOP_Z2_MAX_ID;
#endif
#if HAS_PIN(Z3_MAX) && MAX_HARDWARE_ENDSTOP_Z3
if(READ(Z3_MAX_PIN) != ENDSTOP_Z3_MAX_INVERTING)
newRead2 |= ENDSTOP_Z3_MAX_ID;
#endif
#if HAS_PIN(Z3_MIN) && MIN_HARDWARE_ENDSTOP_Z3
if(READ(Z3_MIN_PIN) != ENDSTOP_Z3_MIN_INVERTING)
newRead2 |= ENDSTOP_Z3_MIN_ID;
#endif
#endif
InterruptProtectedBlock noInts; // bad idea to run this from different interrupts at once!
lastRead &= newRead;
#ifdef EXTENDED_ENDSTOPS
lastRead2 &= newRead2;
#endif // EXTENDED_ENDSTOPS
if(lastRead != lastState
#ifdef EXTENDED_ENDSTOPS
|| (lastState2 != lastRead2)
#endif
) { // Report endstop hit changes
lastState = lastRead;
accumulator |= lastState;
#ifdef EXTENDED_ENDSTOPS
lastState2 = lastRead2;
accumulator2 |= lastState2;
#endif
if (Printer::debugEndStop()) Endstops::report();
} else {
lastState = lastRead;
#ifdef EXTENDED_ENDSTOPS
lastState2 = lastRead2;
#endif
}
lastRead = newRead;
#ifdef EXTENDED_ENDSTOPS
lastRead2 = newRead2;
#endif
}
void Endstops::report() {
Com::printF(PSTR("endstops hit: "));
#if (X_MIN_PIN > -1) && MIN_HARDWARE_ENDSTOP_X
Com::printF(Com::tXMinColon);
Com::printF(xMin() ? Com::tHSpace : Com::tLSpace);
#endif
#if HAS_PIN(X2_MIN) && IS_MAC_TRUE(MIN_HARDWARE_ENDSTOP_X2)
Com::printF(PSTR("x2_min:"));
Com::printF(x2Min() ? Com::tHSpace : Com::tLSpace);
#endif
#if (X_MAX_PIN > -1) && MAX_HARDWARE_ENDSTOP_X
Com::printF(Com::tXMaxColon);
Com::printF(xMax() ? Com::tHSpace : Com::tLSpace);
#endif
#if HAS_PIN(X2_MAX) && IS_MAC_TRUE(MAX_HARDWARE_ENDSTOP_X2)
Com::printF(PSTR("x2_max:"));
Com::printF(x2Max() ? Com::tHSpace : Com::tLSpace);
#endif
#if (Y_MIN_PIN > -1) && MIN_HARDWARE_ENDSTOP_Y
Com::printF(Com::tYMinColon);
Com::printF(yMin() ? Com::tHSpace : Com::tLSpace);
#endif
#if HAS_PIN(Y2_MIN) && IS_MAC_TRUE(MIN_HARDWARE_ENDSTOP_Y2)
Com::printF(PSTR("y2_min:"));
Com::printF(y2Min() ? Com::tHSpace : Com::tLSpace);
#endif
#if (Y_MAX_PIN > -1) && MAX_HARDWARE_ENDSTOP_Y
Com::printF(Com::tYMaxColon);
Com::printF(yMax() ? Com::tHSpace : Com::tLSpace);
#endif
#if HAS_PIN(Y2_MAX) && IS_MAC_TRUE(MAX_HARDWARE_ENDSTOP_Y2)
Com::printF(PSTR("y2_max:"));
Com::printF(y2Max() ? Com::tHSpace : Com::tLSpace);
#endif
#if (Z_MIN_PIN > -1) && MIN_HARDWARE_ENDSTOP_Z
Com::printF(Com::tZMinColon);
Com::printF(zMin() ? Com::tHSpace : Com::tLSpace);
#endif
#if (Z_MAX_PIN > -1) && MAX_HARDWARE_ENDSTOP_Z
Com::printF(Com::tZMaxColon);
Com::printF(zMax() ? Com::tHSpace : Com::tLSpace);
#endif
#if (Z2_MINMAX_PIN > -1) && MINMAX_HARDWARE_ENDSTOP_Z2
Com::printF(PSTR("z2_minmax:"));
Com::printF(z2MinMax() ? Com::tHSpace : Com::tLSpace);
#endif
#if FEATURE_Z_PROBE
Com::printF(Com::tZProbeState);
Com::printF(zProbe() ? Com::tHSpace : Com::tLSpace);
#endif
Com::println();
}
void Endstops::setup() {
// Set end stops to input and enable pullup if required
#if MIN_HARDWARE_ENDSTOP_X
#if X_MIN_PIN > -1
SET_INPUT(X_MIN_PIN);
#if ENDSTOP_PULLUP_X_MIN
PULLUP(X_MIN_PIN, HIGH);
#endif
#else
#error You have defined hardware x min endstop without pin assignment. Set pin number for X_MIN_PIN
#endif
#endif
#if MIN_HARDWARE_ENDSTOP_X2
#if X2_MIN_PIN > -1
SET_INPUT(X2_MIN_PIN);
#if ENDSTOP_PULLUP_X2_MIN
PULLUP(X2_MIN_PIN, HIGH);
#endif
#else
#error You have defined hardware 2 min endstop without pin assignment. Set pin number for X2_MIN_PIN
#endif
#endif
#if MIN_HARDWARE_ENDSTOP_Y
#if Y_MIN_PIN > -1
SET_INPUT(Y_MIN_PIN);
#if ENDSTOP_PULLUP_Y_MIN
PULLUP(Y_MIN_PIN, HIGH);
#endif
#else
#error You have defined hardware y min endstop without pin assignment. Set pin number for Y_MIN_PIN
#endif
#endif
#if MIN_HARDWARE_ENDSTOP_Y2
#if Y2_MIN_PIN > -1
SET_INPUT(Y2_MIN_PIN);
#if ENDSTOP_PULLUP_Y2_MIN
PULLUP(Y2_MIN_PIN, HIGH);
#endif
#else
#error You have defined hardware y2 min endstop without pin assignment. Set pin number for Y2_MIN_PIN
#endif
#endif
#if MIN_HARDWARE_ENDSTOP_Z
#if Z_MIN_PIN > -1
SET_INPUT(Z_MIN_PIN);
#if ENDSTOP_PULLUP_Z_MIN
PULLUP(Z_MIN_PIN, HIGH);
#endif
#else
#error You have defined hardware z min endstop without pin assignment. Set pin number for Z_MIN_PIN
#endif
#endif
#if MINMAX_HARDWARE_ENDSTOP_Z2
#if Z2_MINMAX_PIN > -1
SET_INPUT(Z2_MINMAX_PIN);
#if ENDSTOP_PULLUP_Z2_MINMAX
PULLUP(Z2_MINMAX_PIN, HIGH);
#endif
#else
#error You have defined hardware z2 minmax endstop without pin assignment. Set pin number for Z2_MINMAX_PIN
#endif
#endif
#if MAX_HARDWARE_ENDSTOP_X
#if X_MAX_PIN > -1
SET_INPUT(X_MAX_PIN);
#if ENDSTOP_PULLUP_X_MAX
PULLUP(X_MAX_PIN, HIGH);
#endif
#else
#error You have defined hardware x max endstop without pin assignment. Set pin number for X_MAX_PIN
#endif
#endif
#if MAX_HARDWARE_ENDSTOP_X2
#if X2_MAX_PIN > -1
SET_INPUT(X2_MAX_PIN);
#if ENDSTOP_PULLUP_X2_MAX
PULLUP(X2_MAX_PIN, HIGH);
#endif
#else
#error You have defined hardware x2 max endstop without pin assignment. Set pin number for X2_MAX_PIN
#endif
#endif
#if MAX_HARDWARE_ENDSTOP_Y
#if Y_MAX_PIN > -1
SET_INPUT(Y_MAX_PIN);
#if ENDSTOP_PULLUP_Y_MAX
PULLUP(Y_MAX_PIN, HIGH);
#endif
#else
#error You have defined hardware y max endstop without pin assignment. Set pin number for Y_MAX_PIN
#endif
#endif
#if MAX_HARDWARE_ENDSTOP_Y2
#if Y2_MAX_PIN > -1
SET_INPUT(Y2_MAX_PIN);
#if ENDSTOP_PULLUP_Y2_MAX
PULLUP(Y2_MAX_PIN, HIGH);
#endif
#else
#error You have defined hardware y2 max endstop without pin assignment. Set pin number for Y2_MAX_PIN
#endif
#endif
#if MAX_HARDWARE_ENDSTOP_Z
#if Z_MAX_PIN>-1
SET_INPUT(Z_MAX_PIN);
#if ENDSTOP_PULLUP_Z_MAX
PULLUP(Z_MAX_PIN, HIGH);
#endif
#else
#error You have defined hardware z max endstop without pin assignment. Set pin number for Z_MAX_PIN
#endif
#endif
}