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

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