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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 <stdint.h>
  39. #include "../../module/endstops.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 : (uint8_t*)0 )
  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. (uint8_t *)0 )
  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. SBI(*digitalPinToPCMSK(pin), digitalPinToPCMSKbit(pin)); // enable pin
  76. SBI(PCIFR, digitalPinToPCICRbit(pin)); // clear any outstanding interrupt
  77. SBI(PCICR, digitalPinToPCICRbit(pin)); // enable interrupt for the group
  78. }
  79. // Handlers for pin change interrupts
  80. #ifdef PCINT0_vect
  81. ISR(PCINT0_vect) { endstop_ISR(); }
  82. #endif
  83. #ifdef PCINT1_vect
  84. ISR(PCINT1_vect, ISR_ALIASOF(PCINT0_vect));
  85. #endif
  86. #ifdef PCINT2_vect
  87. ISR(PCINT2_vect, ISR_ALIASOF(PCINT0_vect));
  88. #endif
  89. #ifdef PCINT3_vect
  90. ISR(PCINT3_vect, ISR_ALIASOF(PCINT0_vect));
  91. #endif
  92. void setup_endstop_interrupts( void ) {
  93. #if HAS_X_MAX
  94. #if (digitalPinToInterrupt(X_MAX_PIN) != NOT_AN_INTERRUPT) // if pin has an external interrupt
  95. attachInterrupt(digitalPinToInterrupt(X_MAX_PIN), endstop_ISR, CHANGE); // assign it
  96. #else
  97. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  98. static_assert(digitalPinToPCICR(X_MAX_PIN) != NULL, "X_MAX_PIN is not interrupt-capable"); // if pin has no pin change interrupt - error
  99. pciSetup(X_MAX_PIN); // assign it
  100. #endif
  101. #endif
  102. #if HAS_X_MIN
  103. #if (digitalPinToInterrupt(X_MIN_PIN) != NOT_AN_INTERRUPT)
  104. attachInterrupt(digitalPinToInterrupt(X_MIN_PIN), endstop_ISR, CHANGE);
  105. #else
  106. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  107. static_assert(digitalPinToPCICR(X_MIN_PIN) != NULL, "X_MIN_PIN is not interrupt-capable");
  108. pciSetup(X_MIN_PIN);
  109. #endif
  110. #endif
  111. #if HAS_Y_MAX
  112. #if (digitalPinToInterrupt(Y_MAX_PIN) != NOT_AN_INTERRUPT)
  113. attachInterrupt(digitalPinToInterrupt(Y_MAX_PIN), endstop_ISR, CHANGE);
  114. #else
  115. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  116. static_assert(digitalPinToPCICR(Y_MAX_PIN) != NULL, "Y_MAX_PIN is not interrupt-capable");
  117. pciSetup(Y_MAX_PIN);
  118. #endif
  119. #endif
  120. #if HAS_Y_MIN
  121. #if (digitalPinToInterrupt(Y_MIN_PIN) != NOT_AN_INTERRUPT)
  122. attachInterrupt(digitalPinToInterrupt(Y_MIN_PIN), endstop_ISR, CHANGE);
  123. #else
  124. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  125. static_assert(digitalPinToPCICR(Y_MIN_PIN) != NULL, "Y_MIN_PIN is not interrupt-capable");
  126. pciSetup(Y_MIN_PIN);
  127. #endif
  128. #endif
  129. #if HAS_Z_MAX
  130. #if (digitalPinToInterrupt(Z_MAX_PIN) != NOT_AN_INTERRUPT)
  131. attachInterrupt(digitalPinToInterrupt(Z_MAX_PIN), endstop_ISR, CHANGE);
  132. #else
  133. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  134. static_assert(digitalPinToPCICR(Z_MAX_PIN) != NULL, "Z_MAX_PIN is not interrupt-capable");
  135. pciSetup(Z_MAX_PIN);
  136. #endif
  137. #endif
  138. #if HAS_Z_MIN
  139. #if (digitalPinToInterrupt(Z_MIN_PIN) != NOT_AN_INTERRUPT)
  140. attachInterrupt(digitalPinToInterrupt(Z_MIN_PIN), endstop_ISR, CHANGE);
  141. #else
  142. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  143. static_assert(digitalPinToPCICR(Z_MIN_PIN) != NULL, "Z_MIN_PIN is not interrupt-capable");
  144. pciSetup(Z_MIN_PIN);
  145. #endif
  146. #endif
  147. #if HAS_X2_MAX
  148. #if (digitalPinToInterrupt(X2_MAX_PIN) != NOT_AN_INTERRUPT)
  149. attachInterrupt(digitalPinToInterrupt(X2_MAX_PIN), endstop_ISR, CHANGE);
  150. #else
  151. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  152. static_assert(digitalPinToPCICR(X2_MAX_PIN) != NULL, "X2_MAX_PIN is not interrupt-capable");
  153. pciSetup(X2_MAX_PIN);
  154. #endif
  155. #endif
  156. #if HAS_X2_MIN
  157. #if (digitalPinToInterrupt(X2_MIN_PIN) != NOT_AN_INTERRUPT)
  158. attachInterrupt(digitalPinToInterrupt(X2_MIN_PIN), endstop_ISR, CHANGE);
  159. #else
  160. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  161. static_assert(digitalPinToPCICR(X2_MIN_PIN) != NULL, "X2_MIN_PIN is not interrupt-capable");
  162. pciSetup(X2_MIN_PIN);
  163. #endif
  164. #endif
  165. #if HAS_Y2_MAX
  166. #if (digitalPinToInterrupt(Y2_MAX_PIN) != NOT_AN_INTERRUPT)
  167. attachInterrupt(digitalPinToInterrupt(Y2_MAX_PIN), endstop_ISR, CHANGE);
  168. #else
  169. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  170. static_assert(digitalPinToPCICR(Y2_MAX_PIN) != NULL, "Y2_MAX_PIN is not interrupt-capable");
  171. pciSetup(Y2_MAX_PIN);
  172. #endif
  173. #endif
  174. #if HAS_Y2_MIN
  175. #if (digitalPinToInterrupt(Y2_MIN_PIN) != NOT_AN_INTERRUPT)
  176. attachInterrupt(digitalPinToInterrupt(Y2_MIN_PIN), endstop_ISR, CHANGE);
  177. #else
  178. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  179. static_assert(digitalPinToPCICR(Y2_MIN_PIN) != NULL, "Y2_MIN_PIN is not interrupt-capable");
  180. pciSetup(Y2_MIN_PIN);
  181. #endif
  182. #endif
  183. #if HAS_Z2_MAX
  184. #if (digitalPinToInterrupt(Z2_MAX_PIN) != NOT_AN_INTERRUPT)
  185. attachInterrupt(digitalPinToInterrupt(Z2_MAX_PIN), endstop_ISR, CHANGE);
  186. #else
  187. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  188. static_assert(digitalPinToPCICR(Z2_MAX_PIN) != NULL, "Z2_MAX_PIN is not interrupt-capable");
  189. pciSetup(Z2_MAX_PIN);
  190. #endif
  191. #endif
  192. #if HAS_Z2_MIN
  193. #if (digitalPinToInterrupt(Z2_MIN_PIN) != NOT_AN_INTERRUPT)
  194. attachInterrupt(digitalPinToInterrupt(Z2_MIN_PIN), endstop_ISR, CHANGE);
  195. #else
  196. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  197. static_assert(digitalPinToPCICR(Z2_MIN_PIN) != NULL, "Z2_MIN_PIN is not interrupt-capable");
  198. pciSetup(Z2_MIN_PIN);
  199. #endif
  200. #endif
  201. #if HAS_Z3_MAX
  202. #if (digitalPinToInterrupt(Z3_MAX_PIN) != NOT_AN_INTERRUPT)
  203. attachInterrupt(digitalPinToInterrupt(Z3_MAX_PIN), endstop_ISR, CHANGE);
  204. #else
  205. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  206. static_assert(digitalPinToPCICR(Z3_MAX_PIN) != NULL, "Z3_MAX_PIN is not interrupt-capable");
  207. pciSetup(Z3_MAX_PIN);
  208. #endif
  209. #endif
  210. #if HAS_Z3_MIN
  211. #if (digitalPinToInterrupt(Z3_MIN_PIN) != NOT_AN_INTERRUPT)
  212. attachInterrupt(digitalPinToInterrupt(Z3_MIN_PIN), endstop_ISR, CHANGE);
  213. #else
  214. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  215. static_assert(digitalPinToPCICR(Z3_MIN_PIN) != NULL, "Z3_MIN_PIN is not interrupt-capable");
  216. pciSetup(Z3_MIN_PIN);
  217. #endif
  218. #endif
  219. #if HAS_Z_MIN_PROBE_PIN
  220. #if (digitalPinToInterrupt(Z_MIN_PROBE_PIN) != NOT_AN_INTERRUPT)
  221. attachInterrupt(digitalPinToInterrupt(Z_MIN_PROBE_PIN), endstop_ISR, CHANGE);
  222. #else
  223. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  224. static_assert(digitalPinToPCICR(Z_MIN_PROBE_PIN) != NULL, "Z_MIN_PROBE_PIN is not interrupt-capable");
  225. pciSetup(Z_MIN_PROBE_PIN);
  226. #endif
  227. #endif
  228. // If we arrive here without raising an assertion, each pin has either an EXT-interrupt or a PCI.
  229. }