endstop_interrupts.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 temperature-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. // One ISR for all EXT-Interrupts
  40. void endstop_ISR(void) { 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 (D13, D14).
  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. #undef digitalPinToPCICR
  50. #define digitalPinToPCICR(p) ( WITHIN(p, 10, 15) || \
  51. WITHIN(p, 50, 53) || \
  52. WITHIN(p, 62, 69) ? &PCICR : (uint8_t*)0 )
  53. #undef digitalPinToPCICRbit
  54. #define digitalPinToPCICRbit(p) ( WITHIN(p, 10, 13) || WITHIN(p, 50, 53) ? 0 : \
  55. WITHIN(p, 14, 15) ? 1 : \
  56. WITHIN(p, 62, 69) ? 2 : \
  57. 0 )
  58. #undef digitalPinToPCMSK
  59. #define digitalPinToPCMSK(p) ( WITHIN(p, 10, 13) || WITHIN(p, 50, 53) ? &PCMSK0 : \
  60. WITHIN(p, 14, 15) ? &PCMSK1 : \
  61. WITHIN(p, 62, 69) ? &PCMSK2 : \
  62. (uint8_t *)0 )
  63. #undef digitalPinToPCMSKbit
  64. #define digitalPinToPCMSKbit(p) ( WITHIN(p, 10, 13) ? ((p) - 6) : \
  65. (p) == 14 || (p) == 51 ? 2 : \
  66. (p) == 15 || (p) == 52 ? 1 : \
  67. (p) == 50 ? 3 : \
  68. (p) == 53 ? 0 : \
  69. WITHIN(p, 62, 69) ? ((p) - 62) : \
  70. 0 )
  71. #endif
  72. // Install Pin change interrupt for a pin. Can be called multiple times.
  73. void pciSetup(const int8_t 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. // Handlers for pin change interrupts
  79. #ifdef PCINT0_vect
  80. ISR(PCINT0_vect) { endstop_ISR(); }
  81. #endif
  82. #ifdef PCINT1_vect
  83. ISR(PCINT1_vect) { endstop_ISR(); }
  84. #endif
  85. #ifdef PCINT2_vect
  86. ISR(PCINT2_vect) { endstop_ISR(); }
  87. #endif
  88. #ifdef PCINT3_vect
  89. ISR(PCINT3_vect) { endstop_ISR(); }
  90. #endif
  91. void setup_endstop_interrupts( void ) {
  92. #if HAS_X_MAX
  93. #if digitalPinToInterrupt(X_MAX_PIN) != NOT_AN_INTERRUPT // if pin has an external interrupt
  94. attachInterrupt(digitalPinToInterrupt(X_MAX_PIN), endstop_ISR, CHANGE); // assign it
  95. #else
  96. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  97. static_assert(digitalPinToPCICR(X_MAX_PIN) != NULL, "X_MAX_PIN is not interrupt-capable"); // if pin has no pin change interrupt - error
  98. pciSetup(X_MAX_PIN); // assign it
  99. #endif
  100. #endif
  101. #if HAS_X_MIN
  102. #if digitalPinToInterrupt(X_MIN_PIN) != NOT_AN_INTERRUPT
  103. attachInterrupt(digitalPinToInterrupt(X_MIN_PIN), endstop_ISR, CHANGE);
  104. #else
  105. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  106. static_assert(digitalPinToPCICR(X_MIN_PIN) != NULL, "X_MIN_PIN is not interrupt-capable");
  107. pciSetup(X_MIN_PIN);
  108. #endif
  109. #endif
  110. #if HAS_Y_MAX
  111. #if digitalPinToInterrupt(Y_MAX_PIN) != NOT_AN_INTERRUPT
  112. attachInterrupt(digitalPinToInterrupt(Y_MAX_PIN), endstop_ISR, CHANGE);
  113. #else
  114. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  115. static_assert(digitalPinToPCICR(Y_MAX_PIN) != NULL, "Y_MAX_PIN is not interrupt-capable");
  116. pciSetup(Y_MAX_PIN);
  117. #endif
  118. #endif
  119. #if HAS_Y_MIN
  120. #if digitalPinToInterrupt(Y_MIN_PIN) != NOT_AN_INTERRUPT
  121. attachInterrupt(digitalPinToInterrupt(Y_MIN_PIN), endstop_ISR, CHANGE);
  122. #else
  123. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  124. static_assert(digitalPinToPCICR(Y_MIN_PIN) != NULL, "Y_MIN_PIN is not interrupt-capable");
  125. pciSetup(Y_MIN_PIN);
  126. #endif
  127. #endif
  128. #if HAS_Z_MAX
  129. #if digitalPinToInterrupt(Z_MAX_PIN) != NOT_AN_INTERRUPT
  130. attachInterrupt(digitalPinToInterrupt(Z_MAX_PIN), endstop_ISR, CHANGE);
  131. #else
  132. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  133. static_assert(digitalPinToPCICR(Z_MAX_PIN) != NULL, "Z_MAX_PIN is not interrupt-capable");
  134. pciSetup(Z_MAX_PIN);
  135. #endif
  136. #endif
  137. #if HAS_Z_MIN
  138. #if digitalPinToInterrupt(Z_MIN_PIN) != NOT_AN_INTERRUPT
  139. attachInterrupt(digitalPinToInterrupt(Z_MIN_PIN), endstop_ISR, CHANGE);
  140. #else
  141. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  142. static_assert(digitalPinToPCICR(Z_MIN_PIN) != NULL, "Z_MIN_PIN is not interrupt-capable");
  143. pciSetup(Z_MIN_PIN);
  144. #endif
  145. #endif
  146. #if HAS_X2_MAX
  147. #if (digitalPinToInterrupt(X2_MAX_PIN) != NOT_AN_INTERRUPT)
  148. attachInterrupt(digitalPinToInterrupt(X2_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(X2_MAX_PIN) != NULL, "X2_MAX_PIN is not interrupt-capable");
  152. pciSetup(X2_MAX_PIN);
  153. #endif
  154. #endif
  155. #if HAS_X2_MIN
  156. #if (digitalPinToInterrupt(X2_MIN_PIN) != NOT_AN_INTERRUPT)
  157. attachInterrupt(digitalPinToInterrupt(X2_MIN_PIN), endstop_ISR, CHANGE);
  158. #else
  159. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  160. static_assert(digitalPinToPCICR(X2_MIN_PIN) != NULL, "X2_MIN_PIN is not interrupt-capable");
  161. pciSetup(X2_MIN_PIN);
  162. #endif
  163. #endif
  164. #if HAS_Y2_MAX
  165. #if (digitalPinToInterrupt(Y2_MAX_PIN) != NOT_AN_INTERRUPT)
  166. attachInterrupt(digitalPinToInterrupt(Y2_MAX_PIN), endstop_ISR, CHANGE);
  167. #else
  168. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  169. static_assert(digitalPinToPCICR(Y2_MAX_PIN) != NULL, "Y2_MAX_PIN is not interrupt-capable");
  170. pciSetup(Y2_MAX_PIN);
  171. #endif
  172. #endif
  173. #if HAS_Y2_MIN
  174. #if (digitalPinToInterrupt(Y2_MIN_PIN) != NOT_AN_INTERRUPT)
  175. attachInterrupt(digitalPinToInterrupt(Y2_MIN_PIN), endstop_ISR, CHANGE);
  176. #else
  177. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  178. static_assert(digitalPinToPCICR(Y2_MIN_PIN) != NULL, "Y2_MIN_PIN is not interrupt-capable");
  179. pciSetup(Y2_MIN_PIN);
  180. #endif
  181. #endif
  182. #if HAS_Z2_MAX
  183. #if digitalPinToInterrupt(Z2_MAX_PIN) != NOT_AN_INTERRUPT
  184. attachInterrupt(digitalPinToInterrupt(Z2_MAX_PIN), endstop_ISR, CHANGE);
  185. #else
  186. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  187. static_assert(digitalPinToPCICR(Z2_MAX_PIN) != NULL, "Z2_MAX_PIN is not interrupt-capable");
  188. pciSetup(Z2_MAX_PIN);
  189. #endif
  190. #endif
  191. #if HAS_Z2_MIN
  192. #if digitalPinToInterrupt(Z2_MIN_PIN) != NOT_AN_INTERRUPT
  193. attachInterrupt(digitalPinToInterrupt(Z2_MIN_PIN), endstop_ISR, CHANGE);
  194. #else
  195. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  196. static_assert(digitalPinToPCICR(Z2_MIN_PIN) != NULL, "Z2_MIN_PIN is not interrupt-capable");
  197. pciSetup(Z2_MIN_PIN);
  198. #endif
  199. #endif
  200. #if HAS_Z_MIN_PROBE_PIN
  201. #if digitalPinToInterrupt(Z_MIN_PROBE_PIN) != NOT_AN_INTERRUPT
  202. attachInterrupt(digitalPinToInterrupt(Z_MIN_PROBE_PIN), endstop_ISR, CHANGE);
  203. #else
  204. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  205. static_assert(digitalPinToPCICR(Z_MIN_PROBE_PIN) != NULL, "Z_MIN_PROBE_PIN is not interrupt-capable");
  206. pciSetup(Z_MIN_PROBE_PIN);
  207. #endif
  208. #endif
  209. // If we arrive here without raising an assertion, each pin has either an EXT-interrupt or a PCI.
  210. }
  211. #endif // _ENDSTOP_INTERRUPTS_H_