2016-11-05 21:38:48 +00:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2019-06-28 04:57:50 +00:00
|
|
|
* Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2016-11-05 21:38:48 +00:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
2019-06-28 04:57:50 +00:00
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
2016-11-05 21:38:48 +00:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*
|
|
|
|
*/
|
2018-11-04 08:25:55 +00:00
|
|
|
#pragma once
|
2016-11-05 21:38:48 +00:00
|
|
|
|
|
|
|
/**
|
2016-11-19 03:53:45 +00:00
|
|
|
* Endstop Interrupts
|
2016-11-05 21:38:48 +00:00
|
|
|
*
|
2016-11-19 03:53:45 +00:00
|
|
|
* Without endstop interrupts the endstop pins must be polled continually in
|
2018-05-16 07:08:43 +00:00
|
|
|
* the temperature-ISR via endstops.update(), most of the time finding no change.
|
2016-11-19 03:53:45 +00: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 21:38:48 +00:00
|
|
|
*/
|
|
|
|
|
2018-10-03 03:09:41 +00:00
|
|
|
#include "../../module/endstops.h"
|
2018-04-13 01:25:08 +00:00
|
|
|
|
2019-06-23 08:43:36 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2018-04-13 01:25:08 +00:00
|
|
|
// One ISR for all EXT-Interrupts
|
2019-09-17 01:31:08 +00:00
|
|
|
void endstop_ISR() { endstops.update(); }
|
2018-04-13 01:25:08 +00:00
|
|
|
|
2016-11-19 03:53:45 +00:00
|
|
|
/**
|
|
|
|
* Patch for pins_arduino.h (...\Arduino\hardware\arduino\avr\variants\mega\pins_arduino.h)
|
|
|
|
*
|
2019-11-02 11:59:38 +00:00
|
|
|
* These macros for the Arduino MEGA do not include the two connected pins on Port J (D14, D15).
|
2016-11-19 03:53:45 +00:00
|
|
|
* 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)
|
2019-10-30 20:52:02 +00:00
|
|
|
#define digitalPinHasPCICR(p) (WITHIN(p, 10, 15) || WITHIN(p, 50, 53) || WITHIN(p, 62, 69))
|
2019-11-02 11:59:38 +00:00
|
|
|
#define moreDigitalPinToPCICR(p) digitalPinToPCICR(WITHIN(p, 14, 15) ? 10 : p)
|
|
|
|
#define moreDigitalPinToPCICRbit(p) (WITHIN(p, 14, 15) ? 1 : digitalPinToPCICRbit(p))
|
|
|
|
#define moreDigitalPinToPCMSK(p) (WITHIN(p, 14, 15) ? (&PCMSK1) : digitalPinToPCMSK(p))
|
|
|
|
#define moreDigitalPinToPCMSKbit(p) digitalPinToPCMSKbit(WITHIN(p, 14, 15) ? (p)+36 : p)
|
2016-11-19 03:53:45 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
// Install Pin change interrupt for a pin. Can be called multiple times.
|
2018-05-19 21:54:37 +00:00
|
|
|
void pciSetup(const int8_t pin) {
|
2019-10-30 20:52:02 +00:00
|
|
|
if (moreDigitalPinToPCMSK(pin) != nullptr) {
|
|
|
|
SBI(*moreDigitalPinToPCMSK(pin), moreDigitalPinToPCMSKbit(pin)); // enable pin
|
|
|
|
SBI(PCIFR, moreDigitalPinToPCICRbit(pin)); // clear any outstanding interrupt
|
|
|
|
SBI(PCICR, moreDigitalPinToPCICRbit(pin)); // enable interrupt for the group
|
2019-05-09 16:45:55 +00:00
|
|
|
}
|
2016-11-19 03:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handlers for pin change interrupts
|
|
|
|
#ifdef PCINT0_vect
|
2018-05-16 07:08:43 +00:00
|
|
|
ISR(PCINT0_vect) { endstop_ISR(); }
|
2016-11-19 03:53:45 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef PCINT1_vect
|
2018-10-07 22:05:26 +00:00
|
|
|
ISR(PCINT1_vect, ISR_ALIASOF(PCINT0_vect));
|
2016-11-19 03:53:45 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef PCINT2_vect
|
2018-10-07 22:05:26 +00:00
|
|
|
ISR(PCINT2_vect, ISR_ALIASOF(PCINT0_vect));
|
2016-11-19 03:53:45 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef PCINT3_vect
|
2018-10-07 22:05:26 +00:00
|
|
|
ISR(PCINT3_vect, ISR_ALIASOF(PCINT0_vect));
|
2016-11-19 03:53:45 +00:00
|
|
|
#endif
|
|
|
|
|
2019-09-17 01:31:08 +00:00
|
|
|
void setup_endstop_interrupts() {
|
2019-09-17 01:18:02 +00:00
|
|
|
#define _ATTACH(P) attachInterrupt(digitalPinToInterrupt(P), endstop_ISR, CHANGE)
|
2016-11-19 03:53:45 +00:00
|
|
|
#if HAS_X_MAX
|
2019-09-17 01:18:02 +00:00
|
|
|
#if (digitalPinToInterrupt(X_MAX_PIN) != NOT_AN_INTERRUPT)
|
|
|
|
_ATTACH(X_MAX_PIN);
|
2016-11-19 03:53:45 +00:00
|
|
|
#else
|
2019-10-30 20:52:02 +00:00
|
|
|
static_assert(digitalPinHasPCICR(X_MAX_PIN), "X_MAX_PIN is not interrupt-capable");
|
2019-09-17 01:18:02 +00:00
|
|
|
pciSetup(X_MAX_PIN);
|
2016-11-05 21:38:48 +00:00
|
|
|
#endif
|
2016-11-19 03:53:45 +00:00
|
|
|
#endif
|
|
|
|
#if HAS_X_MIN
|
|
|
|
#if (digitalPinToInterrupt(X_MIN_PIN) != NOT_AN_INTERRUPT)
|
2019-09-17 01:18:02 +00:00
|
|
|
_ATTACH(X_MIN_PIN);
|
2016-11-19 03:53:45 +00:00
|
|
|
#else
|
2019-10-30 20:52:02 +00:00
|
|
|
static_assert(digitalPinHasPCICR(X_MIN_PIN), "X_MIN_PIN is not interrupt-capable");
|
2016-11-19 03:53:45 +00:00
|
|
|
pciSetup(X_MIN_PIN);
|
2016-11-05 21:38:48 +00:00
|
|
|
#endif
|
2016-11-19 03:53:45 +00:00
|
|
|
#endif
|
|
|
|
#if HAS_Y_MAX
|
|
|
|
#if (digitalPinToInterrupt(Y_MAX_PIN) != NOT_AN_INTERRUPT)
|
2019-09-17 01:18:02 +00:00
|
|
|
_ATTACH(Y_MAX_PIN);
|
2016-11-19 03:53:45 +00:00
|
|
|
#else
|
2019-10-30 20:52:02 +00:00
|
|
|
static_assert(digitalPinHasPCICR(Y_MAX_PIN), "Y_MAX_PIN is not interrupt-capable");
|
2016-11-19 03:53:45 +00:00
|
|
|
pciSetup(Y_MAX_PIN);
|
2016-11-05 21:38:48 +00:00
|
|
|
#endif
|
2016-11-19 03:53:45 +00:00
|
|
|
#endif
|
|
|
|
#if HAS_Y_MIN
|
|
|
|
#if (digitalPinToInterrupt(Y_MIN_PIN) != NOT_AN_INTERRUPT)
|
2019-09-17 01:18:02 +00:00
|
|
|
_ATTACH(Y_MIN_PIN);
|
2016-11-19 03:53:45 +00:00
|
|
|
#else
|
2019-10-30 20:52:02 +00:00
|
|
|
static_assert(digitalPinHasPCICR(Y_MIN_PIN), "Y_MIN_PIN is not interrupt-capable");
|
2016-11-19 03:53:45 +00:00
|
|
|
pciSetup(Y_MIN_PIN);
|
2016-11-05 21:38:48 +00:00
|
|
|
#endif
|
2016-11-19 03:53:45 +00:00
|
|
|
#endif
|
|
|
|
#if HAS_Z_MAX
|
|
|
|
#if (digitalPinToInterrupt(Z_MAX_PIN) != NOT_AN_INTERRUPT)
|
2019-09-17 01:18:02 +00:00
|
|
|
_ATTACH(Z_MAX_PIN);
|
2016-11-19 03:53:45 +00:00
|
|
|
#else
|
2019-10-30 20:52:02 +00:00
|
|
|
static_assert(digitalPinHasPCICR(Z_MAX_PIN), "Z_MAX_PIN is not interrupt-capable");
|
2016-11-19 03:53:45 +00:00
|
|
|
pciSetup(Z_MAX_PIN);
|
2016-11-05 21:38:48 +00:00
|
|
|
#endif
|
2016-11-19 03:53:45 +00:00
|
|
|
#endif
|
|
|
|
#if HAS_Z_MIN
|
|
|
|
#if (digitalPinToInterrupt(Z_MIN_PIN) != NOT_AN_INTERRUPT)
|
2019-09-17 01:18:02 +00:00
|
|
|
_ATTACH(Z_MIN_PIN);
|
2016-11-19 03:53:45 +00:00
|
|
|
#else
|
2019-10-30 20:52:02 +00:00
|
|
|
static_assert(digitalPinHasPCICR(Z_MIN_PIN), "Z_MIN_PIN is not interrupt-capable");
|
2016-11-19 03:53:45 +00:00
|
|
|
pciSetup(Z_MIN_PIN);
|
2016-11-05 21:38:48 +00:00
|
|
|
#endif
|
2016-11-19 03:53:45 +00:00
|
|
|
#endif
|
2017-10-29 08:43:44 +00:00
|
|
|
#if HAS_X2_MAX
|
|
|
|
#if (digitalPinToInterrupt(X2_MAX_PIN) != NOT_AN_INTERRUPT)
|
2019-09-17 01:18:02 +00:00
|
|
|
_ATTACH(X2_MAX_PIN);
|
2017-10-29 08:43:44 +00:00
|
|
|
#else
|
2019-10-30 20:52:02 +00:00
|
|
|
static_assert(digitalPinHasPCICR(X2_MAX_PIN), "X2_MAX_PIN is not interrupt-capable");
|
2017-10-29 08:43:44 +00:00
|
|
|
pciSetup(X2_MAX_PIN);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#if HAS_X2_MIN
|
|
|
|
#if (digitalPinToInterrupt(X2_MIN_PIN) != NOT_AN_INTERRUPT)
|
2019-09-17 01:18:02 +00:00
|
|
|
_ATTACH(X2_MIN_PIN);
|
2017-10-29 08:43:44 +00:00
|
|
|
#else
|
2019-10-30 20:52:02 +00:00
|
|
|
static_assert(digitalPinHasPCICR(X2_MIN_PIN), "X2_MIN_PIN is not interrupt-capable");
|
2017-10-29 08:43:44 +00:00
|
|
|
pciSetup(X2_MIN_PIN);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#if HAS_Y2_MAX
|
|
|
|
#if (digitalPinToInterrupt(Y2_MAX_PIN) != NOT_AN_INTERRUPT)
|
2019-09-17 01:18:02 +00:00
|
|
|
_ATTACH(Y2_MAX_PIN);
|
2017-10-29 08:43:44 +00:00
|
|
|
#else
|
2019-10-30 20:52:02 +00:00
|
|
|
static_assert(digitalPinHasPCICR(Y2_MAX_PIN), "Y2_MAX_PIN is not interrupt-capable");
|
2017-10-29 08:43:44 +00:00
|
|
|
pciSetup(Y2_MAX_PIN);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#if HAS_Y2_MIN
|
|
|
|
#if (digitalPinToInterrupt(Y2_MIN_PIN) != NOT_AN_INTERRUPT)
|
2019-09-17 01:18:02 +00:00
|
|
|
_ATTACH(Y2_MIN_PIN);
|
2017-10-29 08:43:44 +00:00
|
|
|
#else
|
2019-10-30 20:52:02 +00:00
|
|
|
static_assert(digitalPinHasPCICR(Y2_MIN_PIN), "Y2_MIN_PIN is not interrupt-capable");
|
2017-10-29 08:43:44 +00:00
|
|
|
pciSetup(Y2_MIN_PIN);
|
|
|
|
#endif
|
|
|
|
#endif
|
2016-11-19 03:53:45 +00:00
|
|
|
#if HAS_Z2_MAX
|
|
|
|
#if (digitalPinToInterrupt(Z2_MAX_PIN) != NOT_AN_INTERRUPT)
|
2019-09-17 01:18:02 +00:00
|
|
|
_ATTACH(Z2_MAX_PIN);
|
2016-11-19 03:53:45 +00:00
|
|
|
#else
|
2019-10-30 20:52:02 +00:00
|
|
|
static_assert(digitalPinHasPCICR(Z2_MAX_PIN), "Z2_MAX_PIN is not interrupt-capable");
|
2016-11-19 03:53:45 +00:00
|
|
|
pciSetup(Z2_MAX_PIN);
|
2016-11-05 21:38:48 +00:00
|
|
|
#endif
|
2016-11-19 03:53:45 +00:00
|
|
|
#endif
|
|
|
|
#if HAS_Z2_MIN
|
|
|
|
#if (digitalPinToInterrupt(Z2_MIN_PIN) != NOT_AN_INTERRUPT)
|
2019-09-17 01:18:02 +00:00
|
|
|
_ATTACH(Z2_MIN_PIN);
|
2016-11-19 03:53:45 +00:00
|
|
|
#else
|
2019-10-30 20:52:02 +00:00
|
|
|
static_assert(digitalPinHasPCICR(Z2_MIN_PIN), "Z2_MIN_PIN is not interrupt-capable");
|
2016-11-19 03:53:45 +00:00
|
|
|
pciSetup(Z2_MIN_PIN);
|
2016-11-05 21:38:48 +00:00
|
|
|
#endif
|
2016-11-19 03:53:45 +00:00
|
|
|
#endif
|
2018-06-19 16:55:49 +00:00
|
|
|
#if HAS_Z3_MAX
|
|
|
|
#if (digitalPinToInterrupt(Z3_MAX_PIN) != NOT_AN_INTERRUPT)
|
2019-09-17 01:18:02 +00:00
|
|
|
_ATTACH(Z3_MAX_PIN);
|
2018-06-19 16:55:49 +00:00
|
|
|
#else
|
2019-10-30 20:52:02 +00:00
|
|
|
static_assert(digitalPinHasPCICR(Z3_MAX_PIN), "Z3_MAX_PIN is not interrupt-capable");
|
2018-06-19 16:55:49 +00:00
|
|
|
pciSetup(Z3_MAX_PIN);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#if HAS_Z3_MIN
|
|
|
|
#if (digitalPinToInterrupt(Z3_MIN_PIN) != NOT_AN_INTERRUPT)
|
2019-09-17 01:18:02 +00:00
|
|
|
_ATTACH(Z3_MIN_PIN);
|
2018-06-19 16:55:49 +00:00
|
|
|
#else
|
2019-10-30 20:52:02 +00:00
|
|
|
static_assert(digitalPinHasPCICR(Z3_MIN_PIN), "Z3_MIN_PIN is not interrupt-capable");
|
2018-06-19 16:55:49 +00:00
|
|
|
pciSetup(Z3_MIN_PIN);
|
|
|
|
#endif
|
|
|
|
#endif
|
2016-11-19 03:53:45 +00:00
|
|
|
#if HAS_Z_MIN_PROBE_PIN
|
|
|
|
#if (digitalPinToInterrupt(Z_MIN_PROBE_PIN) != NOT_AN_INTERRUPT)
|
2019-09-17 01:18:02 +00:00
|
|
|
_ATTACH(Z_MIN_PROBE_PIN);
|
2016-11-19 03:53:45 +00:00
|
|
|
#else
|
2019-10-30 20:52:02 +00:00
|
|
|
static_assert(digitalPinHasPCICR(Z_MIN_PROBE_PIN), "Z_MIN_PROBE_PIN is not interrupt-capable");
|
2016-11-19 03:53:45 +00:00
|
|
|
pciSetup(Z_MIN_PROBE_PIN);
|
2016-11-05 21:38:48 +00:00
|
|
|
#endif
|
2016-11-19 03:53:45 +00:00
|
|
|
#endif
|
2016-11-05 21:38:48 +00:00
|
|
|
|
2016-11-19 03:53:45 +00:00
|
|
|
// If we arrive here without raising an assertion, each pin has either an EXT-interrupt or a PCI.
|
|
|
|
}
|