2016-11-05 16:38:48 -05:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
|
|
|
* Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
|
|
|
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
|
|
|
|
*
|
|
|
|
* This program 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.
|
|
|
|
*
|
|
|
|
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2016-11-18 21:53:45 -06:00
|
|
|
* Endstop Interrupts
|
2016-11-05 16:38:48 -05:00
|
|
|
*
|
2016-11-18 21:53:45 -06:00
|
|
|
* Without endstop interrupts the endstop pins must be polled continually in
|
2018-05-16 02:08:43 -05:00
|
|
|
* the temperature-ISR via endstops.update(), most of the time finding no change.
|
2016-11-18 21:53:45 -06:00
|
|
|
* With this feature endstops.update() is called only when we know that at
|
|
|
|
* least one endstop has changed state, saving valuable CPU cycles.
|
|
|
|
*
|
|
|
|
* This feature only works when all used endstop pins can generate either an
|
|
|
|
* 'external interrupt' or a 'pin change interrupt'.
|
|
|
|
*
|
|
|
|
* Test whether pins issue interrupts on your board by flashing 'pin_interrupt_test.ino'.
|
|
|
|
* (Located in Marlin/buildroot/share/pin_interrupt_test/pin_interrupt_test.ino)
|
2016-11-05 16:38:48 -05:00
|
|
|
*/
|
|
|
|
|
2017-03-31 09:00:49 -05:00
|
|
|
#ifndef _ENDSTOP_INTERRUPTS_H_
|
|
|
|
#define _ENDSTOP_INTERRUPTS_H_
|
|
|
|
|
2018-10-02 22:09:41 -05:00
|
|
|
#include "../../core/macros.h"
|
2018-10-02 04:25:46 -05:00
|
|
|
#include <stdint.h>
|
2018-10-02 22:09:41 -05:00
|
|
|
#include "../../module/endstops.h"
|
2018-04-12 20:25:08 -05:00
|
|
|
|
|
|
|
// One ISR for all EXT-Interrupts
|
2018-06-21 20:14:16 -05:00
|
|
|
void endstop_ISR(void) { endstops.update(); }
|
2018-04-12 20:25:08 -05:00
|
|
|
|
2016-11-18 21:53:45 -06:00
|
|
|
/**
|
|
|
|
* Patch for pins_arduino.h (...\Arduino\hardware\arduino\avr\variants\mega\pins_arduino.h)
|
|
|
|
*
|
|
|
|
* These macros for the Arduino MEGA do not include the two connected pins on Port J (D13, D14).
|
|
|
|
* So we extend them here because these are the normal pins for Y_MIN and Y_MAX on RAMPS.
|
|
|
|
* There are more PCI-enabled processor pins on Port J, but they are not connected to Arduino MEGA.
|
|
|
|
*/
|
|
|
|
#if defined(ARDUINO_AVR_MEGA2560) || defined(ARDUINO_AVR_MEGA)
|
|
|
|
#undef digitalPinToPCICR
|
2017-03-31 09:00:49 -05:00
|
|
|
#define digitalPinToPCICR(p) ( WITHIN(p, 10, 15) || \
|
|
|
|
WITHIN(p, 50, 53) || \
|
|
|
|
WITHIN(p, 62, 69) ? &PCICR : (uint8_t*)0 )
|
2016-11-18 21:53:45 -06:00
|
|
|
#undef digitalPinToPCICRbit
|
2017-03-31 09:00:49 -05:00
|
|
|
#define digitalPinToPCICRbit(p) ( WITHIN(p, 10, 13) || WITHIN(p, 50, 53) ? 0 : \
|
|
|
|
WITHIN(p, 14, 15) ? 1 : \
|
|
|
|
WITHIN(p, 62, 69) ? 2 : \
|
|
|
|
0 )
|
2016-11-18 21:53:45 -06:00
|
|
|
#undef digitalPinToPCMSK
|
2017-03-31 09:00:49 -05:00
|
|
|
#define digitalPinToPCMSK(p) ( WITHIN(p, 10, 13) || WITHIN(p, 50, 53) ? &PCMSK0 : \
|
|
|
|
WITHIN(p, 14, 15) ? &PCMSK1 : \
|
|
|
|
WITHIN(p, 62, 69) ? &PCMSK2 : \
|
|
|
|
(uint8_t *)0 )
|
2016-11-18 21:53:45 -06:00
|
|
|
#undef digitalPinToPCMSKbit
|
2017-03-31 09:00:49 -05:00
|
|
|
#define digitalPinToPCMSKbit(p) ( WITHIN(p, 10, 13) ? ((p) - 6) : \
|
|
|
|
(p) == 14 || (p) == 51 ? 2 : \
|
|
|
|
(p) == 15 || (p) == 52 ? 1 : \
|
|
|
|
(p) == 50 ? 3 : \
|
|
|
|
(p) == 53 ? 0 : \
|
|
|
|
WITHIN(p, 62, 69) ? ((p) - 62) : \
|
|
|
|
0 )
|
2016-11-18 21:53:45 -06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
// Install Pin change interrupt for a pin. Can be called multiple times.
|
2018-05-19 16:54:37 -05:00
|
|
|
void pciSetup(const int8_t pin) {
|
2017-03-31 09:00:49 -05:00
|
|
|
SBI(*digitalPinToPCMSK(pin), digitalPinToPCMSKbit(pin)); // enable pin
|
|
|
|
SBI(PCIFR, digitalPinToPCICRbit(pin)); // clear any outstanding interrupt
|
|
|
|
SBI(PCICR, digitalPinToPCICRbit(pin)); // enable interrupt for the group
|
2016-11-18 21:53:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Handlers for pin change interrupts
|
|
|
|
#ifdef PCINT0_vect
|
2018-05-16 02:08:43 -05:00
|
|
|
ISR(PCINT0_vect) { endstop_ISR(); }
|
2016-11-18 21:53:45 -06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef PCINT1_vect
|
2018-05-16 02:08:43 -05:00
|
|
|
ISR(PCINT1_vect) { endstop_ISR(); }
|
2016-11-18 21:53:45 -06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef PCINT2_vect
|
2018-05-16 02:08:43 -05:00
|
|
|
ISR(PCINT2_vect) { endstop_ISR(); }
|
2016-11-18 21:53:45 -06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef PCINT3_vect
|
2018-05-16 02:08:43 -05:00
|
|
|
ISR(PCINT3_vect) { endstop_ISR(); }
|
2016-11-18 21:53:45 -06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
void setup_endstop_interrupts( void ) {
|
|
|
|
|
|
|
|
#if HAS_X_MAX
|
|
|
|
#if (digitalPinToInterrupt(X_MAX_PIN) != NOT_AN_INTERRUPT) // if pin has an external interrupt
|
|
|
|
attachInterrupt(digitalPinToInterrupt(X_MAX_PIN), endstop_ISR, CHANGE); // assign it
|
|
|
|
#else
|
|
|
|
// Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
|
2016-11-18 22:00:00 -06:00
|
|
|
static_assert(digitalPinToPCICR(X_MAX_PIN) != NULL, "X_MAX_PIN is not interrupt-capable"); // if pin has no pin change interrupt - error
|
2016-11-18 21:53:45 -06:00
|
|
|
pciSetup(X_MAX_PIN); // assign it
|
2016-11-05 16:38:48 -05:00
|
|
|
#endif
|
2016-11-18 21:53:45 -06:00
|
|
|
#endif
|
2016-11-05 16:38:48 -05:00
|
|
|
|
2016-11-18 21:53:45 -06:00
|
|
|
#if HAS_X_MIN
|
|
|
|
#if (digitalPinToInterrupt(X_MIN_PIN) != NOT_AN_INTERRUPT)
|
|
|
|
attachInterrupt(digitalPinToInterrupt(X_MIN_PIN), endstop_ISR, CHANGE);
|
|
|
|
#else
|
|
|
|
// Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
|
2016-11-18 22:00:00 -06:00
|
|
|
static_assert(digitalPinToPCICR(X_MIN_PIN) != NULL, "X_MIN_PIN is not interrupt-capable");
|
2016-11-18 21:53:45 -06:00
|
|
|
pciSetup(X_MIN_PIN);
|
2016-11-05 16:38:48 -05:00
|
|
|
#endif
|
2016-11-18 21:53:45 -06:00
|
|
|
#endif
|
2016-11-05 16:38:48 -05:00
|
|
|
|
2016-11-18 21:53:45 -06:00
|
|
|
#if HAS_Y_MAX
|
|
|
|
#if (digitalPinToInterrupt(Y_MAX_PIN) != NOT_AN_INTERRUPT)
|
|
|
|
attachInterrupt(digitalPinToInterrupt(Y_MAX_PIN), endstop_ISR, CHANGE);
|
|
|
|
#else
|
|
|
|
// Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
|
2016-11-18 22:00:00 -06:00
|
|
|
static_assert(digitalPinToPCICR(Y_MAX_PIN) != NULL, "Y_MAX_PIN is not interrupt-capable");
|
2016-11-18 21:53:45 -06:00
|
|
|
pciSetup(Y_MAX_PIN);
|
2016-11-05 16:38:48 -05:00
|
|
|
#endif
|
2016-11-18 21:53:45 -06:00
|
|
|
#endif
|
2016-11-05 16:38:48 -05:00
|
|
|
|
2016-11-18 21:53:45 -06:00
|
|
|
#if HAS_Y_MIN
|
|
|
|
#if (digitalPinToInterrupt(Y_MIN_PIN) != NOT_AN_INTERRUPT)
|
|
|
|
attachInterrupt(digitalPinToInterrupt(Y_MIN_PIN), endstop_ISR, CHANGE);
|
|
|
|
#else
|
|
|
|
// Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
|
2016-11-18 22:00:00 -06:00
|
|
|
static_assert(digitalPinToPCICR(Y_MIN_PIN) != NULL, "Y_MIN_PIN is not interrupt-capable");
|
2016-11-18 21:53:45 -06:00
|
|
|
pciSetup(Y_MIN_PIN);
|
2016-11-05 16:38:48 -05:00
|
|
|
#endif
|
2016-11-18 21:53:45 -06:00
|
|
|
#endif
|
2016-11-05 16:38:48 -05:00
|
|
|
|
2016-11-18 21:53:45 -06:00
|
|
|
#if HAS_Z_MAX
|
|
|
|
#if (digitalPinToInterrupt(Z_MAX_PIN) != NOT_AN_INTERRUPT)
|
|
|
|
attachInterrupt(digitalPinToInterrupt(Z_MAX_PIN), endstop_ISR, CHANGE);
|
|
|
|
#else
|
|
|
|
// Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
|
2016-11-18 22:00:00 -06:00
|
|
|
static_assert(digitalPinToPCICR(Z_MAX_PIN) != NULL, "Z_MAX_PIN is not interrupt-capable");
|
2016-11-18 21:53:45 -06:00
|
|
|
pciSetup(Z_MAX_PIN);
|
2016-11-05 16:38:48 -05:00
|
|
|
#endif
|
2016-11-18 21:53:45 -06:00
|
|
|
#endif
|
2016-11-05 16:38:48 -05:00
|
|
|
|
2016-11-18 21:53:45 -06:00
|
|
|
#if HAS_Z_MIN
|
|
|
|
#if (digitalPinToInterrupt(Z_MIN_PIN) != NOT_AN_INTERRUPT)
|
|
|
|
attachInterrupt(digitalPinToInterrupt(Z_MIN_PIN), endstop_ISR, CHANGE);
|
|
|
|
#else
|
|
|
|
// Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
|
2016-11-18 22:00:00 -06:00
|
|
|
static_assert(digitalPinToPCICR(Z_MIN_PIN) != NULL, "Z_MIN_PIN is not interrupt-capable");
|
2016-11-18 21:53:45 -06:00
|
|
|
pciSetup(Z_MIN_PIN);
|
2016-11-05 16:38:48 -05:00
|
|
|
#endif
|
2016-11-18 21:53:45 -06:00
|
|
|
#endif
|
2016-11-05 16:38:48 -05:00
|
|
|
|
2017-10-29 03:43:44 -05:00
|
|
|
#if HAS_X2_MAX
|
|
|
|
#if (digitalPinToInterrupt(X2_MAX_PIN) != NOT_AN_INTERRUPT)
|
|
|
|
attachInterrupt(digitalPinToInterrupt(X2_MAX_PIN), endstop_ISR, CHANGE);
|
|
|
|
#else
|
|
|
|
// Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
|
|
|
|
static_assert(digitalPinToPCICR(X2_MAX_PIN) != NULL, "X2_MAX_PIN is not interrupt-capable");
|
|
|
|
pciSetup(X2_MAX_PIN);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HAS_X2_MIN
|
|
|
|
#if (digitalPinToInterrupt(X2_MIN_PIN) != NOT_AN_INTERRUPT)
|
|
|
|
attachInterrupt(digitalPinToInterrupt(X2_MIN_PIN), endstop_ISR, CHANGE);
|
|
|
|
#else
|
|
|
|
// Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
|
|
|
|
static_assert(digitalPinToPCICR(X2_MIN_PIN) != NULL, "X2_MIN_PIN is not interrupt-capable");
|
|
|
|
pciSetup(X2_MIN_PIN);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HAS_Y2_MAX
|
|
|
|
#if (digitalPinToInterrupt(Y2_MAX_PIN) != NOT_AN_INTERRUPT)
|
|
|
|
attachInterrupt(digitalPinToInterrupt(Y2_MAX_PIN), endstop_ISR, CHANGE);
|
|
|
|
#else
|
|
|
|
// Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
|
|
|
|
static_assert(digitalPinToPCICR(Y2_MAX_PIN) != NULL, "Y2_MAX_PIN is not interrupt-capable");
|
|
|
|
pciSetup(Y2_MAX_PIN);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HAS_Y2_MIN
|
|
|
|
#if (digitalPinToInterrupt(Y2_MIN_PIN) != NOT_AN_INTERRUPT)
|
|
|
|
attachInterrupt(digitalPinToInterrupt(Y2_MIN_PIN), endstop_ISR, CHANGE);
|
|
|
|
#else
|
|
|
|
// Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
|
|
|
|
static_assert(digitalPinToPCICR(Y2_MIN_PIN) != NULL, "Y2_MIN_PIN is not interrupt-capable");
|
|
|
|
pciSetup(Y2_MIN_PIN);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2016-11-18 21:53:45 -06:00
|
|
|
#if HAS_Z2_MAX
|
|
|
|
#if (digitalPinToInterrupt(Z2_MAX_PIN) != NOT_AN_INTERRUPT)
|
|
|
|
attachInterrupt(digitalPinToInterrupt(Z2_MAX_PIN), endstop_ISR, CHANGE);
|
|
|
|
#else
|
|
|
|
// Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
|
2016-11-18 22:00:00 -06:00
|
|
|
static_assert(digitalPinToPCICR(Z2_MAX_PIN) != NULL, "Z2_MAX_PIN is not interrupt-capable");
|
2016-11-18 21:53:45 -06:00
|
|
|
pciSetup(Z2_MAX_PIN);
|
2016-11-05 16:38:48 -05:00
|
|
|
#endif
|
2016-11-18 21:53:45 -06:00
|
|
|
#endif
|
2016-11-05 16:38:48 -05:00
|
|
|
|
2016-11-18 21:53:45 -06:00
|
|
|
#if HAS_Z2_MIN
|
|
|
|
#if (digitalPinToInterrupt(Z2_MIN_PIN) != NOT_AN_INTERRUPT)
|
|
|
|
attachInterrupt(digitalPinToInterrupt(Z2_MIN_PIN), endstop_ISR, CHANGE);
|
|
|
|
#else
|
|
|
|
// Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
|
2016-11-18 22:00:00 -06:00
|
|
|
static_assert(digitalPinToPCICR(Z2_MIN_PIN) != NULL, "Z2_MIN_PIN is not interrupt-capable");
|
2016-11-18 21:53:45 -06:00
|
|
|
pciSetup(Z2_MIN_PIN);
|
2016-11-05 16:38:48 -05:00
|
|
|
#endif
|
2016-11-18 21:53:45 -06:00
|
|
|
#endif
|
2016-11-05 16:38:48 -05:00
|
|
|
|
2018-06-19 11:55:49 -05:00
|
|
|
#if HAS_Z3_MAX
|
|
|
|
#if (digitalPinToInterrupt(Z3_MAX_PIN) != NOT_AN_INTERRUPT)
|
|
|
|
attachInterrupt(digitalPinToInterrupt(Z3_MAX_PIN), endstop_ISR, CHANGE);
|
|
|
|
#else
|
|
|
|
// Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
|
|
|
|
static_assert(digitalPinToPCICR(Z3_MAX_PIN) != NULL, "Z3_MAX_PIN is not interrupt-capable");
|
|
|
|
pciSetup(Z3_MAX_PIN);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HAS_Z3_MIN
|
|
|
|
#if (digitalPinToInterrupt(Z3_MIN_PIN) != NOT_AN_INTERRUPT)
|
|
|
|
attachInterrupt(digitalPinToInterrupt(Z3_MIN_PIN), endstop_ISR, CHANGE);
|
|
|
|
#else
|
|
|
|
// Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
|
|
|
|
static_assert(digitalPinToPCICR(Z3_MIN_PIN) != NULL, "Z3_MIN_PIN is not interrupt-capable");
|
|
|
|
pciSetup(Z3_MIN_PIN);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2016-11-18 21:53:45 -06:00
|
|
|
#if HAS_Z_MIN_PROBE_PIN
|
|
|
|
#if (digitalPinToInterrupt(Z_MIN_PROBE_PIN) != NOT_AN_INTERRUPT)
|
|
|
|
attachInterrupt(digitalPinToInterrupt(Z_MIN_PROBE_PIN), endstop_ISR, CHANGE);
|
|
|
|
#else
|
|
|
|
// Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
|
2016-11-18 22:00:00 -06:00
|
|
|
static_assert(digitalPinToPCICR(Z_MIN_PROBE_PIN) != NULL, "Z_MIN_PROBE_PIN is not interrupt-capable");
|
2016-11-18 21:53:45 -06:00
|
|
|
pciSetup(Z_MIN_PROBE_PIN);
|
2016-11-05 16:38:48 -05:00
|
|
|
#endif
|
2016-11-18 21:53:45 -06:00
|
|
|
#endif
|
2016-11-05 16:38:48 -05:00
|
|
|
|
2016-11-18 21:53:45 -06:00
|
|
|
// If we arrive here without raising an assertion, each pin has either an EXT-interrupt or a PCI.
|
|
|
|
}
|
2016-11-05 16:38:48 -05:00
|
|
|
|
2017-05-09 12:35:43 -05:00
|
|
|
#endif // _ENDSTOP_INTERRUPTS_H_
|