Subversion Repositories MK-Marlin

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 ron 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
 *
26
 * Without endstop interrupts the endstop pins must be polled continually in
27
 * the temperature-ISR via endstops.update(), most of the time finding no change.
28
 * With this feature endstops.update() is called only when we know that at
29
 * least one endstop has changed state, saving valuable CPU cycles.
30
 *
31
 * This feature only works when all used endstop pins can generate either an
32
 * 'external interrupt' or a 'pin change interrupt'.
33
 *
34
 * Test whether pins issue interrupts on your board by flashing 'pin_interrupt_test.ino'.
35
 * (Located in Marlin/buildroot/share/pin_interrupt_test/pin_interrupt_test.ino)
36
 */
37
 
38
#ifndef _ENDSTOP_INTERRUPTS_H_
39
#define _ENDSTOP_INTERRUPTS_H_
40
 
41
#include "macros.h"
42
 
43
// One ISR for all EXT-Interrupts
44
void endstop_ISR(void) { endstops.update(); }
45
 
46
/**
47
 * Patch for pins_arduino.h (...\Arduino\hardware\arduino\avr\variants\mega\pins_arduino.h)
48
 *
49
 * These macros for the Arduino MEGA do not include the two connected pins on Port J (D13, D14).
50
 * So we extend them here because these are the normal pins for Y_MIN and Y_MAX on RAMPS.
51
 * There are more PCI-enabled processor pins on Port J, but they are not connected to Arduino MEGA.
52
 */
53
#if defined(ARDUINO_AVR_MEGA2560) || defined(ARDUINO_AVR_MEGA)
54
  #undef  digitalPinToPCICR
55
  #define digitalPinToPCICR(p)    ( WITHIN(p, 10, 15) || \
56
                                    WITHIN(p, 50, 53) || \
57
                                    WITHIN(p, 62, 69) ? &PCICR : (uint8_t*)0 )
58
  #undef  digitalPinToPCICRbit
59
  #define digitalPinToPCICRbit(p) ( WITHIN(p, 10, 13) || WITHIN(p, 50, 53) ? 0 : \
60
                                    WITHIN(p, 14, 15) ? 1 : \
61
                                    WITHIN(p, 62, 69) ? 2 : \
62
 
63
  #undef  digitalPinToPCMSK
64
  #define digitalPinToPCMSK(p)    ( WITHIN(p, 10, 13) || WITHIN(p, 50, 53) ? &PCMSK0 : \
65
                                    WITHIN(p, 14, 15) ? &PCMSK1 : \
66
                                    WITHIN(p, 62, 69) ? &PCMSK2 : \
67
                                    (uint8_t *)0 )
68
  #undef  digitalPinToPCMSKbit
69
  #define digitalPinToPCMSKbit(p) ( WITHIN(p, 10, 13) ? ((p) - 6) : \
70
                                    (p) == 14 || (p) == 51 ? 2 : \
71
                                    (p) == 15 || (p) == 52 ? 1 : \
72
                                    (p) == 50 ? 3 : \
73
                                    (p) == 53 ? 0 : \
74
                                    WITHIN(p, 62, 69) ? ((p) - 62) : \
75
 
76
#endif
77
 
78
 
79
// Install Pin change interrupt for a pin. Can be called multiple times.
80
void pciSetup(const int8_t pin) {
81
  SBI(*digitalPinToPCMSK(pin), digitalPinToPCMSKbit(pin));  // enable pin
82
  SBI(PCIFR, digitalPinToPCICRbit(pin)); // clear any outstanding interrupt
83
  SBI(PCICR, digitalPinToPCICRbit(pin)); // enable interrupt for the group
84
}
85
 
86
 
87
// Handlers for pin change interrupts
88
#ifdef PCINT0_vect
89
  ISR(PCINT0_vect) { endstop_ISR(); }
90
#endif
91
 
92
#ifdef PCINT1_vect
93
  ISR(PCINT1_vect) { endstop_ISR(); }
94
#endif
95
 
96
#ifdef PCINT2_vect
97
  ISR(PCINT2_vect) { endstop_ISR(); }
98
#endif
99
 
100
#ifdef PCINT3_vect
101
  ISR(PCINT3_vect) { endstop_ISR(); }
102
#endif
103
 
104
void setup_endstop_interrupts( void ) {
105
 
106
  #if HAS_X_MAX
107
    #if digitalPinToInterrupt(X_MAX_PIN) != NOT_AN_INTERRUPT // if pin has an external interrupt
108
      attachInterrupt(digitalPinToInterrupt(X_MAX_PIN), endstop_ISR, CHANGE); // assign it
109
    #else
110
      // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
111
      static_assert(digitalPinToPCICR(X_MAX_PIN) != NULL, "X_MAX_PIN is not interrupt-capable"); // if pin has no pin change interrupt - error
112
      pciSetup(X_MAX_PIN);                                                            // assign it
113
    #endif
114
  #endif
115
 
116
  #if HAS_X_MIN
117
    #if digitalPinToInterrupt(X_MIN_PIN) != NOT_AN_INTERRUPT
118
      attachInterrupt(digitalPinToInterrupt(X_MIN_PIN), endstop_ISR, CHANGE);
119
    #else
120
      // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
121
      static_assert(digitalPinToPCICR(X_MIN_PIN) != NULL, "X_MIN_PIN is not interrupt-capable");
122
      pciSetup(X_MIN_PIN);
123
    #endif
124
  #endif
125
 
126
  #if HAS_Y_MAX
127
    #if digitalPinToInterrupt(Y_MAX_PIN) != NOT_AN_INTERRUPT
128
      attachInterrupt(digitalPinToInterrupt(Y_MAX_PIN), endstop_ISR, CHANGE);
129
    #else
130
      // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
131
      static_assert(digitalPinToPCICR(Y_MAX_PIN) != NULL, "Y_MAX_PIN is not interrupt-capable");
132
      pciSetup(Y_MAX_PIN);
133
    #endif
134
  #endif
135
 
136
  #if HAS_Y_MIN
137
    #if digitalPinToInterrupt(Y_MIN_PIN) != NOT_AN_INTERRUPT
138
      attachInterrupt(digitalPinToInterrupt(Y_MIN_PIN), endstop_ISR, CHANGE);
139
    #else
140
      // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
141
      static_assert(digitalPinToPCICR(Y_MIN_PIN) != NULL, "Y_MIN_PIN is not interrupt-capable");
142
      pciSetup(Y_MIN_PIN);
143
    #endif
144
  #endif
145
 
146
  #if HAS_Z_MAX
147
    #if digitalPinToInterrupt(Z_MAX_PIN) != NOT_AN_INTERRUPT
148
      attachInterrupt(digitalPinToInterrupt(Z_MAX_PIN), endstop_ISR, CHANGE);
149
    #else
150
      // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
151
      static_assert(digitalPinToPCICR(Z_MAX_PIN) != NULL, "Z_MAX_PIN is not interrupt-capable");
152
      pciSetup(Z_MAX_PIN);
153
    #endif
154
  #endif
155
 
156
  #if HAS_Z_MIN
157
    #if digitalPinToInterrupt(Z_MIN_PIN) != NOT_AN_INTERRUPT
158
      attachInterrupt(digitalPinToInterrupt(Z_MIN_PIN), endstop_ISR, CHANGE);
159
    #else
160
      // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
161
      static_assert(digitalPinToPCICR(Z_MIN_PIN) != NULL, "Z_MIN_PIN is not interrupt-capable");
162
      pciSetup(Z_MIN_PIN);
163
    #endif
164
  #endif
165
 
166
  #if HAS_X2_MAX
167
    #if (digitalPinToInterrupt(X2_MAX_PIN) != NOT_AN_INTERRUPT)
168
      attachInterrupt(digitalPinToInterrupt(X2_MAX_PIN), endstop_ISR, CHANGE);
169
    #else
170
      // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
171
      static_assert(digitalPinToPCICR(X2_MAX_PIN) != NULL, "X2_MAX_PIN is not interrupt-capable");
172
      pciSetup(X2_MAX_PIN);
173
    #endif
174
  #endif
175
 
176
  #if HAS_X2_MIN
177
    #if (digitalPinToInterrupt(X2_MIN_PIN) != NOT_AN_INTERRUPT)
178
      attachInterrupt(digitalPinToInterrupt(X2_MIN_PIN), endstop_ISR, CHANGE);
179
    #else
180
      // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
181
      static_assert(digitalPinToPCICR(X2_MIN_PIN) != NULL, "X2_MIN_PIN is not interrupt-capable");
182
      pciSetup(X2_MIN_PIN);
183
    #endif
184
  #endif
185
 
186
  #if HAS_Y2_MAX
187
    #if (digitalPinToInterrupt(Y2_MAX_PIN) != NOT_AN_INTERRUPT)
188
      attachInterrupt(digitalPinToInterrupt(Y2_MAX_PIN), endstop_ISR, CHANGE);
189
    #else
190
      // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
191
      static_assert(digitalPinToPCICR(Y2_MAX_PIN) != NULL, "Y2_MAX_PIN is not interrupt-capable");
192
      pciSetup(Y2_MAX_PIN);
193
    #endif
194
  #endif
195
 
196
  #if HAS_Y2_MIN
197
    #if (digitalPinToInterrupt(Y2_MIN_PIN) != NOT_AN_INTERRUPT)
198
      attachInterrupt(digitalPinToInterrupt(Y2_MIN_PIN), endstop_ISR, CHANGE);
199
    #else
200
      // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
201
      static_assert(digitalPinToPCICR(Y2_MIN_PIN) != NULL, "Y2_MIN_PIN is not interrupt-capable");
202
      pciSetup(Y2_MIN_PIN);
203
    #endif
204
  #endif
205
 
206
  #if HAS_Z2_MAX
207
    #if digitalPinToInterrupt(Z2_MAX_PIN) != NOT_AN_INTERRUPT
208
      attachInterrupt(digitalPinToInterrupt(Z2_MAX_PIN), endstop_ISR, CHANGE);
209
    #else
210
      // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
211
      static_assert(digitalPinToPCICR(Z2_MAX_PIN) != NULL, "Z2_MAX_PIN is not interrupt-capable");
212
      pciSetup(Z2_MAX_PIN);
213
    #endif
214
  #endif
215
 
216
  #if HAS_Z2_MIN
217
    #if digitalPinToInterrupt(Z2_MIN_PIN) != NOT_AN_INTERRUPT
218
      attachInterrupt(digitalPinToInterrupt(Z2_MIN_PIN), endstop_ISR, CHANGE);
219
    #else
220
      // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
221
      static_assert(digitalPinToPCICR(Z2_MIN_PIN) != NULL, "Z2_MIN_PIN is not interrupt-capable");
222
      pciSetup(Z2_MIN_PIN);
223
    #endif
224
  #endif
225
 
226
  #if HAS_Z_MIN_PROBE_PIN
227
    #if digitalPinToInterrupt(Z_MIN_PROBE_PIN) != NOT_AN_INTERRUPT
228
      attachInterrupt(digitalPinToInterrupt(Z_MIN_PROBE_PIN), endstop_ISR, CHANGE);
229
    #else
230
      // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
231
      static_assert(digitalPinToPCICR(Z_MIN_PROBE_PIN) != NULL, "Z_MIN_PROBE_PIN is not interrupt-capable");
232
      pciSetup(Z_MIN_PROBE_PIN);
233
    #endif
234
  #endif
235
 
236
  // If we arrive here without raising an assertion, each pin has either an EXT-interrupt or a PCI.
237
}
238
 
239
#endif // _ENDSTOP_INTERRUPTS_H_