|
@@ -0,0 +1,211 @@
|
|
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
|
+ * Endstop interrupts
|
|
25
|
+ * Without endstop interrups the stepper-ISR must always test all endstops when interested in their states (endstops.update()).
|
|
26
|
+ * Most time the test will result in finding out nothing has changed.
|
|
27
|
+ * With endstop interrupts endstops.update() is called only when we know that at least one endstop has changed its state.
|
|
28
|
+ *
|
|
29
|
+ * This can work only if all __used__ endstop pins can provide ether an 'external interrupt' or a 'pin change interrupt'.
|
|
30
|
+ * You can find out about pins issuing interrupts by running 'pin_interrupt_test.ino' (Marlin\buildroot\share\pin_interrupt_test\pin_interrupt_test.ino)
|
|
31
|
+ */
|
|
32
|
+
|
|
33
|
+ #ifndef _ENDSTOP_INTERRUPTS_H_
|
|
34
|
+ #define _ENDSTOP_INTERRUPTS_H_
|
|
35
|
+
|
|
36
|
+ /**
|
|
37
|
+ * Patch for pins_arduino.h (...\Arduino\hardware\arduino\avr\variants\mega\pins_arduino.h)
|
|
38
|
+ *
|
|
39
|
+ * These macros for the Arduino MEGA do not include the two connected pins on Port J (D13, D14).
|
|
40
|
+ * So we extend them here because this are the normal pins for Y_MIN and Y_MAX on RAMPS.
|
|
41
|
+ * There are more PCI enabled processor pins on Port J, but they are not connected to Arduino MEGA.
|
|
42
|
+ */
|
|
43
|
+ #if defined(ARDUINO_AVR_MEGA2560) || defined(ARDUINO_AVR_MEGA)
|
|
44
|
+ #undef digitalPinToPCICR
|
|
45
|
+ #define digitalPinToPCICR(p) ( (((p) >= 10) && ((p) <= 15)) || \
|
|
46
|
+ (((p) >= 50) && ((p) <= 53)) || \
|
|
47
|
+ (((p) >= 62) && ((p) <= 69)) ? (&PCICR) : ((uint8_t *)0) )
|
|
48
|
+ #undef digitalPinToPCICRbit
|
|
49
|
+ #define digitalPinToPCICRbit(p) ( (((p) >= 10) && ((p) <= 13)) || (((p) >= 50) && ((p) <= 53)) ? 0 : \
|
|
50
|
+ ( (((p) >= 14) && ((p) <= 15)) ? 1 : \
|
|
51
|
+ ( (((p) >= 62) && ((p) <= 69)) ? 2 : \
|
|
52
|
+ 0 ) ) )
|
|
53
|
+ #undef digitalPinToPCMSK
|
|
54
|
+ #define digitalPinToPCMSK(p) ( (((p) >= 10) && ((p) <= 13)) || (((p) >= 50) && ((p) <= 53)) ? (&PCMSK0) : \
|
|
55
|
+ ( (((p) >= 14) && ((p) <= 15)) ? (&PCMSK1) : \
|
|
56
|
+ ( (((p) >= 62) && ((p) <= 69)) ? (&PCMSK2) : \
|
|
57
|
+ ((uint8_t *)0) ) ) )
|
|
58
|
+ #undef digitalPinToPCMSKbit
|
|
59
|
+ #define digitalPinToPCMSKbit(p) ( (((p) >= 10) && ((p) <= 13)) ? ((p) - 6) : \
|
|
60
|
+ ( ((p) == 14) ? 2 : \
|
|
61
|
+ ( ((p) == 15) ? 1 : \
|
|
62
|
+ ( ((p) == 50) ? 3 : \
|
|
63
|
+ ( ((p) == 51) ? 2 : \
|
|
64
|
+ ( ((p) == 52) ? 1 : \
|
|
65
|
+ ( ((p) == 53) ? 0 : \
|
|
66
|
+ ( (((p) >= 62) && ((p) <= 69)) ? ((p) - 62) : \
|
|
67
|
+ 0 ) ) ) ) ) ) ) )
|
|
68
|
+ #endif
|
|
69
|
+
|
|
70
|
+ volatile uint8_t e_hit = 0; // Different from 0 when the endstops shall be tested in detail.
|
|
71
|
+ // Must be reset to 0 by the test function when the tests are finished.
|
|
72
|
+
|
|
73
|
+ // Install Pin change interrupt for a pin, can be called multiple times
|
|
74
|
+ void pciSetup(byte pin) {
|
|
75
|
+ *digitalPinToPCMSK(pin) |= bit (digitalPinToPCMSKbit(pin)); // enable pin
|
|
76
|
+ PCIFR |= bit (digitalPinToPCICRbit(pin)); // clear any outstanding interrupt
|
|
77
|
+ PCICR |= bit (digitalPinToPCICRbit(pin)); // enable interrupt for the group
|
|
78
|
+ }
|
|
79
|
+
|
|
80
|
+ // This is what is really done inside the interrupts.
|
|
81
|
+ FORCE_INLINE void endstop_ISR_worker( void ) {
|
|
82
|
+ e_hit = 2; // Because the detection of a e-stop hit has a 1 step debouncer it has to be called at least twice.
|
|
83
|
+ }
|
|
84
|
+
|
|
85
|
+ // Use one Routine to handle each group
|
|
86
|
+ // One ISR for all EXT-Interrupts
|
|
87
|
+ void endstop_ISR(void) {
|
|
88
|
+ endstop_ISR_worker();
|
|
89
|
+ }
|
|
90
|
+
|
|
91
|
+ #ifdef PCINT0_vect
|
|
92
|
+ ISR(PCINT0_vect) { // handle pin change interrupt
|
|
93
|
+ endstop_ISR_worker();
|
|
94
|
+ }
|
|
95
|
+ #endif
|
|
96
|
+
|
|
97
|
+ #ifdef PCINT1_vect
|
|
98
|
+ ISR(PCINT1_vect) { // handle pin change interrupt
|
|
99
|
+ endstop_ISR_worker();
|
|
100
|
+ }
|
|
101
|
+ #endif
|
|
102
|
+
|
|
103
|
+ #ifdef PCINT2_vect
|
|
104
|
+ ISR(PCINT2_vect) { // handle pin change interrupt
|
|
105
|
+ endstop_ISR_worker();
|
|
106
|
+ }
|
|
107
|
+ #endif
|
|
108
|
+
|
|
109
|
+ #ifdef PCINT3_vect
|
|
110
|
+ ISR(PCINT3_vect) { // handle pin change interrupt
|
|
111
|
+ endstop_ISR_worker();
|
|
112
|
+ }
|
|
113
|
+ #endif
|
|
114
|
+
|
|
115
|
+ void setup_enstop_interrupts( void ) {
|
|
116
|
+
|
|
117
|
+ #if HAS_X_MAX
|
|
118
|
+ #if (digitalPinToInterrupt(X_MAX_PIN) != NOT_AN_INTERRUPT) // if pin has an external interrupt
|
|
119
|
+ attachInterrupt(digitalPinToInterrupt(X_MAX_PIN), endstop_ISR, CHANGE); // assign it
|
|
120
|
+ #else
|
|
121
|
+ // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
|
|
122
|
+ static_assert(digitalPinToPCICR(X_MAX_PIN) != NULL, "ENDSTOP_INTERRUPT_ERROR"); // if pin has no pin change interrupt - error
|
|
123
|
+ pciSetup(X_MAX_PIN); // assign it
|
|
124
|
+ #endif
|
|
125
|
+ #endif
|
|
126
|
+
|
|
127
|
+ #if HAS_X_MIN
|
|
128
|
+ #if (digitalPinToInterrupt(X_MIN_PIN) != NOT_AN_INTERRUPT)
|
|
129
|
+ attachInterrupt(digitalPinToInterrupt(X_MIN_PIN), endstop_ISR, CHANGE);
|
|
130
|
+ #else
|
|
131
|
+ // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
|
|
132
|
+ static_assert(digitalPinToPCICR(X_MIN_PIN) != NULL, "ENDSTOP_INTERRUPT_ERROR");
|
|
133
|
+ pciSetup(X_MIN_PIN);
|
|
134
|
+ #endif
|
|
135
|
+ #endif
|
|
136
|
+
|
|
137
|
+ #if HAS_Y_MAX
|
|
138
|
+ #if (digitalPinToInterrupt(Y_MAX_PIN) != NOT_AN_INTERRUPT)
|
|
139
|
+ attachInterrupt(digitalPinToInterrupt(Y_MAX_PIN), endstop_ISR, CHANGE);
|
|
140
|
+ #else
|
|
141
|
+ // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
|
|
142
|
+ static_assert(digitalPinToPCICR(Y_MAX_PIN) != NULL, "ENDSTOP_INTERRUPT_ERROR");
|
|
143
|
+ pciSetup(Y_MAX_PIN);
|
|
144
|
+ #endif
|
|
145
|
+ #endif
|
|
146
|
+
|
|
147
|
+ #if HAS_Y_MIN
|
|
148
|
+ #if (digitalPinToInterrupt(Y_MIN_PIN) != NOT_AN_INTERRUPT)
|
|
149
|
+ attachInterrupt(digitalPinToInterrupt(Y_MIN_PIN), endstop_ISR, CHANGE);
|
|
150
|
+ #else
|
|
151
|
+ // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
|
|
152
|
+ static_assert(digitalPinToPCICR(Y_MIN_PIN) != NULL, "ENDSTOP_INTERRUPT_ERROR");
|
|
153
|
+ pciSetup(Y_MIN_PIN);
|
|
154
|
+ #endif
|
|
155
|
+ #endif
|
|
156
|
+
|
|
157
|
+ #if HAS_Z_MAX
|
|
158
|
+ #if (digitalPinToInterrupt(Z_MAX_PIN) != NOT_AN_INTERRUPT)
|
|
159
|
+ attachInterrupt(digitalPinToInterrupt(Z_MAX_PIN), endstop_ISR, CHANGE);
|
|
160
|
+ #else
|
|
161
|
+ // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
|
|
162
|
+ static_assert(digitalPinToPCICR(Z_MAX_PIN) != NULL, "ENDSTOP_INTERRUPT_ERROR");
|
|
163
|
+ pciSetup(Z_MAX_PIN);
|
|
164
|
+ #endif
|
|
165
|
+ #endif
|
|
166
|
+
|
|
167
|
+ #if HAS_Z_MIN
|
|
168
|
+ #if (digitalPinToInterrupt(Z_MIN_PIN) != NOT_AN_INTERRUPT)
|
|
169
|
+ attachInterrupt(digitalPinToInterrupt(Z_MIN_PIN), endstop_ISR, CHANGE);
|
|
170
|
+ #else
|
|
171
|
+ // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
|
|
172
|
+ static_assert(digitalPinToPCICR(Z_MIN_PIN) != NULL, "ENDSTOP_INTERRUPT_ERROR");
|
|
173
|
+ pciSetup(Z_MIN_PIN);
|
|
174
|
+ #endif
|
|
175
|
+ #endif
|
|
176
|
+
|
|
177
|
+ #if HAS_Z2_MAX
|
|
178
|
+ #if (digitalPinToInterrupt(Z2_MAX_PIN) != NOT_AN_INTERRUPT)
|
|
179
|
+ attachInterrupt(digitalPinToInterrupt(Z2_MAX_PIN), endstop_ISR, CHANGE);
|
|
180
|
+ #else
|
|
181
|
+ // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
|
|
182
|
+ static_assert(digitalPinToPCICR(Z2_MAX_PIN) != NULL, "ENDSTOP_INTERRUPT_ERROR");
|
|
183
|
+ pciSetup(Z2_MAX_PIN);
|
|
184
|
+ #endif
|
|
185
|
+ #endif
|
|
186
|
+
|
|
187
|
+ #if HAS_Z2_MIN
|
|
188
|
+ #if (digitalPinToInterrupt(Z2_MIN_PIN) != NOT_AN_INTERRUPT)
|
|
189
|
+ attachInterrupt(digitalPinToInterrupt(Z2_MIN_PIN), endstop_ISR, CHANGE);
|
|
190
|
+ #else
|
|
191
|
+ // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
|
|
192
|
+ static_assert(digitalPinToPCICR(Z2_MIN_PIN) != NULL, "ENDSTOP_INTERRUPT_ERROR");
|
|
193
|
+ pciSetup(Z2_MIN_PIN);
|
|
194
|
+ #endif
|
|
195
|
+ #endif
|
|
196
|
+
|
|
197
|
+ #if HAS_Z_MIN_PROBE_PIN
|
|
198
|
+ #if (digitalPinToInterrupt(Z_MIN_PROBE_PIN) != NOT_AN_INTERRUPT)
|
|
199
|
+ attachInterrupt(digitalPinToInterrupt(Z_MIN_PROBE_PIN), endstop_ISR, CHANGE);
|
|
200
|
+ #else
|
|
201
|
+ // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
|
|
202
|
+ static_assert(digitalPinToPCICR(Z_MIN_PROBE_PIN) != NULL, "ENDSTOP_INTERRUPT_ERROR");
|
|
203
|
+ pciSetup(Z_MIN_PROBE_PIN);
|
|
204
|
+ #endif
|
|
205
|
+ #endif
|
|
206
|
+
|
|
207
|
+ // When we arive here without error each pin has ether a EXT-interrupt or a PCI.
|
|
208
|
+ }
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+#endif //_ENDSTOP_INTERRUPTS_H_
|