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 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. * Endstop Interrupts
  24. *
  25. * Without endstop interrupts the endstop pins must be polled continually in
  26. * the stepper-ISR via endstops.update(), most of the time finding no change.
  27. * With this feature endstops.update() is called only when we know that at
  28. * least one endstop has changed state, saving valuable CPU cycles.
  29. *
  30. * This feature only works when all used endstop pins can generate either an
  31. * 'external interrupt' or a 'pin change interrupt'.
  32. *
  33. * Test whether pins issue interrupts on your board by flashing 'pin_interrupt_test.ino'.
  34. * (Located in Marlin/buildroot/share/pin_interrupt_test/pin_interrupt_test.ino)
  35. */
  36. #ifndef _ENDSTOP_INTERRUPTS_H_
  37. #define _ENDSTOP_INTERRUPTS_H_
  38. #include "macros.h"
  39. /**
  40. * Patch for pins_arduino.h (...\Arduino\hardware\arduino\avr\variants\mega\pins_arduino.h)
  41. *
  42. * These macros for the Arduino MEGA do not include the two connected pins on Port J (D13, D14).
  43. * So we extend them here because these are the normal pins for Y_MIN and Y_MAX on RAMPS.
  44. * There are more PCI-enabled processor pins on Port J, but they are not connected to Arduino MEGA.
  45. */
  46. #if defined(ARDUINO_AVR_MEGA2560) || defined(ARDUINO_AVR_MEGA)
  47. #undef digitalPinToPCICR
  48. #define digitalPinToPCICR(p) ( WITHIN(p, 10, 15) || \
  49. WITHIN(p, 50, 53) || \
  50. WITHIN(p, 62, 69) ? &PCICR : (uint8_t*)0 )
  51. #undef digitalPinToPCICRbit
  52. #define digitalPinToPCICRbit(p) ( WITHIN(p, 10, 13) || WITHIN(p, 50, 53) ? 0 : \
  53. WITHIN(p, 14, 15) ? 1 : \
  54. WITHIN(p, 62, 69) ? 2 : \
  55. 0 )
  56. #undef digitalPinToPCMSK
  57. #define digitalPinToPCMSK(p) ( WITHIN(p, 10, 13) || WITHIN(p, 50, 53) ? &PCMSK0 : \
  58. WITHIN(p, 14, 15) ? &PCMSK1 : \
  59. WITHIN(p, 62, 69) ? &PCMSK2 : \
  60. (uint8_t *)0 )
  61. #undef digitalPinToPCMSKbit
  62. #define digitalPinToPCMSKbit(p) ( WITHIN(p, 10, 13) ? ((p) - 6) : \
  63. (p) == 14 || (p) == 51 ? 2 : \
  64. (p) == 15 || (p) == 52 ? 1 : \
  65. (p) == 50 ? 3 : \
  66. (p) == 53 ? 0 : \
  67. WITHIN(p, 62, 69) ? ((p) - 62) : \
  68. 0 )
  69. #endif
  70. volatile uint8_t e_hit = 0; // Different from 0 when the endstops should be tested in detail.
  71. // Must be reset to 0 by the test function when finished.
  72. // Install Pin change interrupt for a pin. Can be called multiple times.
  73. void pciSetup(byte pin) {
  74. SBI(*digitalPinToPCMSK(pin), digitalPinToPCMSKbit(pin)); // enable pin
  75. SBI(PCIFR, digitalPinToPCICRbit(pin)); // clear any outstanding interrupt
  76. SBI(PCICR, digitalPinToPCICRbit(pin)); // enable interrupt for the group
  77. }
  78. // This is what is really done inside the interrupts.
  79. FORCE_INLINE void endstop_ISR_worker( void ) {
  80. e_hit = 2; // Because the detection of a e-stop hit has a 1 step debouncer it has to be called at least twice.
  81. }
  82. // Use one Routine to handle each group
  83. // One ISR for all EXT-Interrupts
  84. void endstop_ISR(void) { endstop_ISR_worker(); }
  85. // Handlers for pin change interrupts
  86. #ifdef PCINT0_vect
  87. ISR(PCINT0_vect) { endstop_ISR_worker(); }
  88. #endif
  89. #ifdef PCINT1_vect
  90. ISR(PCINT1_vect) { endstop_ISR_worker(); }
  91. #endif
  92. #ifdef PCINT2_vect
  93. ISR(PCINT2_vect) { endstop_ISR_worker(); }
  94. #endif
  95. #ifdef PCINT3_vect
  96. ISR(PCINT3_vect) { endstop_ISR_worker(); }
  97. #endif
  98. void setup_endstop_interrupts( void ) {
  99. #if HAS_X_MAX
  100. #if (digitalPinToInterrupt(X_MAX_PIN) != NOT_AN_INTERRUPT) // if pin has an external interrupt
  101. attachInterrupt(digitalPinToInterrupt(X_MAX_PIN), endstop_ISR, CHANGE); // assign it
  102. #else
  103. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  104. static_assert(digitalPinToPCICR(X_MAX_PIN) != NULL, "X_MAX_PIN is not interrupt-capable"); // if pin has no pin change interrupt - error
  105. pciSetup(X_MAX_PIN); // assign it
  106. #endif
  107. #endif
  108. #if HAS_X_MIN
  109. #if (digitalPinToInterrupt(X_MIN_PIN) != NOT_AN_INTERRUPT)
  110. attachInterrupt(digitalPinToInterrupt(X_MIN_PIN), endstop_ISR, CHANGE);
  111. #else
  112. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  113. static_assert(digitalPinToPCICR(X_MIN_PIN) != NULL, "X_MIN_PIN is not interrupt-capable");
  114. pciSetup(X_MIN_PIN);
  115. #endif
  116. #endif
  117. #if HAS_Y_MAX
  118. #if (digitalPinToInterrupt(Y_MAX_PIN) != NOT_AN_INTERRUPT)
  119. attachInterrupt(digitalPinToInterrupt(Y_MAX_PIN), endstop_ISR, CHANGE);
  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(Y_MAX_PIN) != NULL, "Y_MAX_PIN is not interrupt-capable");
  123. pciSetup(Y_MAX_PIN);
  124. #endif
  125. #endif
  126. #if HAS_Y_MIN
  127. #if (digitalPinToInterrupt(Y_MIN_PIN) != NOT_AN_INTERRUPT)
  128. attachInterrupt(digitalPinToInterrupt(Y_MIN_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_MIN_PIN) != NULL, "Y_MIN_PIN is not interrupt-capable");
  132. pciSetup(Y_MIN_PIN);
  133. #endif
  134. #endif
  135. #if HAS_Z_MAX
  136. #if (digitalPinToInterrupt(Z_MAX_PIN) != NOT_AN_INTERRUPT)
  137. attachInterrupt(digitalPinToInterrupt(Z_MAX_PIN), endstop_ISR, CHANGE);
  138. #else
  139. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  140. static_assert(digitalPinToPCICR(Z_MAX_PIN) != NULL, "Z_MAX_PIN is not interrupt-capable");
  141. pciSetup(Z_MAX_PIN);
  142. #endif
  143. #endif
  144. #if HAS_Z_MIN
  145. #if (digitalPinToInterrupt(Z_MIN_PIN) != NOT_AN_INTERRUPT)
  146. attachInterrupt(digitalPinToInterrupt(Z_MIN_PIN), endstop_ISR, CHANGE);
  147. #else
  148. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  149. static_assert(digitalPinToPCICR(Z_MIN_PIN) != NULL, "Z_MIN_PIN is not interrupt-capable");
  150. pciSetup(Z_MIN_PIN);
  151. #endif
  152. #endif
  153. #if HAS_Z2_MAX
  154. #if (digitalPinToInterrupt(Z2_MAX_PIN) != NOT_AN_INTERRUPT)
  155. attachInterrupt(digitalPinToInterrupt(Z2_MAX_PIN), endstop_ISR, CHANGE);
  156. #else
  157. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  158. static_assert(digitalPinToPCICR(Z2_MAX_PIN) != NULL, "Z2_MAX_PIN is not interrupt-capable");
  159. pciSetup(Z2_MAX_PIN);
  160. #endif
  161. #endif
  162. #if HAS_Z2_MIN
  163. #if (digitalPinToInterrupt(Z2_MIN_PIN) != NOT_AN_INTERRUPT)
  164. attachInterrupt(digitalPinToInterrupt(Z2_MIN_PIN), endstop_ISR, CHANGE);
  165. #else
  166. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  167. static_assert(digitalPinToPCICR(Z2_MIN_PIN) != NULL, "Z2_MIN_PIN is not interrupt-capable");
  168. pciSetup(Z2_MIN_PIN);
  169. #endif
  170. #endif
  171. #if HAS_Z_MIN_PROBE_PIN
  172. #if (digitalPinToInterrupt(Z_MIN_PROBE_PIN) != NOT_AN_INTERRUPT)
  173. attachInterrupt(digitalPinToInterrupt(Z_MIN_PROBE_PIN), endstop_ISR, CHANGE);
  174. #else
  175. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  176. static_assert(digitalPinToPCICR(Z_MIN_PROBE_PIN) != NULL, "Z_MIN_PROBE_PIN is not interrupt-capable");
  177. pciSetup(Z_MIN_PROBE_PIN);
  178. #endif
  179. #endif
  180. // If we arrive here without raising an assertion, each pin has either an EXT-interrupt or a PCI.
  181. }
  182. #endif // _ENDSTOP_INTERRUPTS_H_