My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

endstop_interrupts.h 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2019 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. #pragma once
  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. #include "../../module/endstops.h"
  38. #include <stdint.h>
  39. // One ISR for all EXT-Interrupts
  40. void endstop_ISR() { endstops.update(); }
  41. /**
  42. * Patch for pins_arduino.h (...\Arduino\hardware\arduino\avr\variants\mega\pins_arduino.h)
  43. *
  44. * These macros for the Arduino MEGA do not include the two connected pins on Port J (D14, D15).
  45. * So we extend them here because these are the normal pins for Y_MIN and Y_MAX on RAMPS.
  46. * There are more PCI-enabled processor pins on Port J, but they are not connected to Arduino MEGA.
  47. */
  48. #if defined(ARDUINO_AVR_MEGA2560) || defined(ARDUINO_AVR_MEGA)
  49. #define digitalPinHasPCICR(p) (WITHIN(p, 10, 15) || WITHIN(p, 50, 53) || WITHIN(p, 62, 69))
  50. #define moreDigitalPinToPCICR(p) digitalPinToPCICR(WITHIN(p, 14, 15) ? 10 : p)
  51. #define moreDigitalPinToPCICRbit(p) (WITHIN(p, 14, 15) ? 1 : digitalPinToPCICRbit(p))
  52. #define moreDigitalPinToPCMSK(p) (WITHIN(p, 14, 15) ? (&PCMSK1) : digitalPinToPCMSK(p))
  53. #define moreDigitalPinToPCMSKbit(p) digitalPinToPCMSKbit(WITHIN(p, 14, 15) ? (p)+36 : p)
  54. #endif
  55. // Install Pin change interrupt for a pin. Can be called multiple times.
  56. void pciSetup(const int8_t pin) {
  57. if (moreDigitalPinToPCMSK(pin) != nullptr) {
  58. SBI(*moreDigitalPinToPCMSK(pin), moreDigitalPinToPCMSKbit(pin)); // enable pin
  59. SBI(PCIFR, moreDigitalPinToPCICRbit(pin)); // clear any outstanding interrupt
  60. SBI(PCICR, moreDigitalPinToPCICRbit(pin)); // enable interrupt for the group
  61. }
  62. }
  63. // Handlers for pin change interrupts
  64. #ifdef PCINT0_vect
  65. ISR(PCINT0_vect) { endstop_ISR(); }
  66. #endif
  67. #ifdef PCINT1_vect
  68. ISR(PCINT1_vect, ISR_ALIASOF(PCINT0_vect));
  69. #endif
  70. #ifdef PCINT2_vect
  71. ISR(PCINT2_vect, ISR_ALIASOF(PCINT0_vect));
  72. #endif
  73. #ifdef PCINT3_vect
  74. ISR(PCINT3_vect, ISR_ALIASOF(PCINT0_vect));
  75. #endif
  76. void setup_endstop_interrupts() {
  77. #define _ATTACH(P) attachInterrupt(digitalPinToInterrupt(P), endstop_ISR, CHANGE)
  78. #if HAS_X_MAX
  79. #if (digitalPinToInterrupt(X_MAX_PIN) != NOT_AN_INTERRUPT)
  80. _ATTACH(X_MAX_PIN);
  81. #else
  82. static_assert(digitalPinHasPCICR(X_MAX_PIN), "X_MAX_PIN is not interrupt-capable");
  83. pciSetup(X_MAX_PIN);
  84. #endif
  85. #endif
  86. #if HAS_X_MIN
  87. #if (digitalPinToInterrupt(X_MIN_PIN) != NOT_AN_INTERRUPT)
  88. _ATTACH(X_MIN_PIN);
  89. #else
  90. static_assert(digitalPinHasPCICR(X_MIN_PIN), "X_MIN_PIN is not interrupt-capable");
  91. pciSetup(X_MIN_PIN);
  92. #endif
  93. #endif
  94. #if HAS_Y_MAX
  95. #if (digitalPinToInterrupt(Y_MAX_PIN) != NOT_AN_INTERRUPT)
  96. _ATTACH(Y_MAX_PIN);
  97. #else
  98. static_assert(digitalPinHasPCICR(Y_MAX_PIN), "Y_MAX_PIN is not interrupt-capable");
  99. pciSetup(Y_MAX_PIN);
  100. #endif
  101. #endif
  102. #if HAS_Y_MIN
  103. #if (digitalPinToInterrupt(Y_MIN_PIN) != NOT_AN_INTERRUPT)
  104. _ATTACH(Y_MIN_PIN);
  105. #else
  106. static_assert(digitalPinHasPCICR(Y_MIN_PIN), "Y_MIN_PIN is not interrupt-capable");
  107. pciSetup(Y_MIN_PIN);
  108. #endif
  109. #endif
  110. #if HAS_Z_MAX
  111. #if (digitalPinToInterrupt(Z_MAX_PIN) != NOT_AN_INTERRUPT)
  112. _ATTACH(Z_MAX_PIN);
  113. #else
  114. static_assert(digitalPinHasPCICR(Z_MAX_PIN), "Z_MAX_PIN is not interrupt-capable");
  115. pciSetup(Z_MAX_PIN);
  116. #endif
  117. #endif
  118. #if HAS_Z_MIN
  119. #if (digitalPinToInterrupt(Z_MIN_PIN) != NOT_AN_INTERRUPT)
  120. _ATTACH(Z_MIN_PIN);
  121. #else
  122. static_assert(digitalPinHasPCICR(Z_MIN_PIN), "Z_MIN_PIN is not interrupt-capable");
  123. pciSetup(Z_MIN_PIN);
  124. #endif
  125. #endif
  126. #if HAS_X2_MAX
  127. #if (digitalPinToInterrupt(X2_MAX_PIN) != NOT_AN_INTERRUPT)
  128. _ATTACH(X2_MAX_PIN);
  129. #else
  130. static_assert(digitalPinHasPCICR(X2_MAX_PIN), "X2_MAX_PIN is not interrupt-capable");
  131. pciSetup(X2_MAX_PIN);
  132. #endif
  133. #endif
  134. #if HAS_X2_MIN
  135. #if (digitalPinToInterrupt(X2_MIN_PIN) != NOT_AN_INTERRUPT)
  136. _ATTACH(X2_MIN_PIN);
  137. #else
  138. static_assert(digitalPinHasPCICR(X2_MIN_PIN), "X2_MIN_PIN is not interrupt-capable");
  139. pciSetup(X2_MIN_PIN);
  140. #endif
  141. #endif
  142. #if HAS_Y2_MAX
  143. #if (digitalPinToInterrupt(Y2_MAX_PIN) != NOT_AN_INTERRUPT)
  144. _ATTACH(Y2_MAX_PIN);
  145. #else
  146. static_assert(digitalPinHasPCICR(Y2_MAX_PIN), "Y2_MAX_PIN is not interrupt-capable");
  147. pciSetup(Y2_MAX_PIN);
  148. #endif
  149. #endif
  150. #if HAS_Y2_MIN
  151. #if (digitalPinToInterrupt(Y2_MIN_PIN) != NOT_AN_INTERRUPT)
  152. _ATTACH(Y2_MIN_PIN);
  153. #else
  154. static_assert(digitalPinHasPCICR(Y2_MIN_PIN), "Y2_MIN_PIN is not interrupt-capable");
  155. pciSetup(Y2_MIN_PIN);
  156. #endif
  157. #endif
  158. #if HAS_Z2_MAX
  159. #if (digitalPinToInterrupt(Z2_MAX_PIN) != NOT_AN_INTERRUPT)
  160. _ATTACH(Z2_MAX_PIN);
  161. #else
  162. static_assert(digitalPinHasPCICR(Z2_MAX_PIN), "Z2_MAX_PIN is not interrupt-capable");
  163. pciSetup(Z2_MAX_PIN);
  164. #endif
  165. #endif
  166. #if HAS_Z2_MIN
  167. #if (digitalPinToInterrupt(Z2_MIN_PIN) != NOT_AN_INTERRUPT)
  168. _ATTACH(Z2_MIN_PIN);
  169. #else
  170. static_assert(digitalPinHasPCICR(Z2_MIN_PIN), "Z2_MIN_PIN is not interrupt-capable");
  171. pciSetup(Z2_MIN_PIN);
  172. #endif
  173. #endif
  174. #if HAS_Z3_MAX
  175. #if (digitalPinToInterrupt(Z3_MAX_PIN) != NOT_AN_INTERRUPT)
  176. _ATTACH(Z3_MAX_PIN);
  177. #else
  178. static_assert(digitalPinHasPCICR(Z3_MAX_PIN), "Z3_MAX_PIN is not interrupt-capable");
  179. pciSetup(Z3_MAX_PIN);
  180. #endif
  181. #endif
  182. #if HAS_Z3_MIN
  183. #if (digitalPinToInterrupt(Z3_MIN_PIN) != NOT_AN_INTERRUPT)
  184. _ATTACH(Z3_MIN_PIN);
  185. #else
  186. static_assert(digitalPinHasPCICR(Z3_MIN_PIN), "Z3_MIN_PIN is not interrupt-capable");
  187. pciSetup(Z3_MIN_PIN);
  188. #endif
  189. #endif
  190. #if HAS_Z_MIN_PROBE_PIN
  191. #if (digitalPinToInterrupt(Z_MIN_PROBE_PIN) != NOT_AN_INTERRUPT)
  192. _ATTACH(Z_MIN_PROBE_PIN);
  193. #else
  194. static_assert(digitalPinHasPCICR(Z_MIN_PROBE_PIN), "Z_MIN_PROBE_PIN is not interrupt-capable");
  195. pciSetup(Z_MIN_PROBE_PIN);
  196. #endif
  197. #endif
  198. // If we arrive here without raising an assertion, each pin has either an EXT-interrupt or a PCI.
  199. }