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

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