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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 <https://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 "../../module/endstops.h"
  38. #include <stdint.h>
  39. // One ISR for all EXT-Interrupts
  40. void endstop_ISR() { 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 (D14, D15).
  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. #define digitalPinHasPCICR(p) (WITHIN(p, 10, 15) || WITHIN(p, 50, 53) || WITHIN(p, 62, 69))
  50. #undef digitalPinToPCICR
  51. #define digitalPinToPCICR(p) (digitalPinHasPCICR(p) ? (&PCICR) : nullptr)
  52. #undef digitalPinToPCICRbit
  53. #define digitalPinToPCICRbit(p) (WITHIN(p, 10, 13) || WITHIN(p, 50, 53) ? 0 : \
  54. WITHIN(p, 14, 15) ? 1 : \
  55. WITHIN(p, 62, 69) ? 2 : \
  56. 0)
  57. #undef digitalPinToPCMSK
  58. #define digitalPinToPCMSK(p) (WITHIN(p, 10, 13) || WITHIN(p, 50, 53) ? (&PCMSK0) : \
  59. WITHIN(p, 14, 15) ? (&PCMSK1) : \
  60. WITHIN(p, 62, 69) ? (&PCMSK2) : \
  61. nullptr)
  62. #undef digitalPinToPCMSKbit
  63. #define digitalPinToPCMSKbit(p) (WITHIN(p, 10, 13) ? ((p) - 6) : \
  64. (p) == 14 || (p) == 51 ? 2 : \
  65. (p) == 15 || (p) == 52 ? 1 : \
  66. (p) == 50 ? 3 : \
  67. (p) == 53 ? 0 : \
  68. WITHIN(p, 62, 69) ? ((p) - 62) : \
  69. 0)
  70. #elif defined(__AVR_ATmega164A__) || defined(__AVR_ATmega164P__) || defined(__AVR_ATmega324A__) || \
  71. defined(__AVR_ATmega324P__) || defined(__AVR_ATmega324PA__) || defined(__AVR_ATmega324PB__) || \
  72. defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284__) || \
  73. defined(__AVR_ATmega1284P__)
  74. #define digitalPinHasPCICR(p) WITHIN(p, 0, NUM_DIGITAL_PINS)
  75. #else
  76. #error "Unsupported AVR variant!"
  77. #endif
  78. // Install Pin change interrupt for a pin. Can be called multiple times.
  79. void pciSetup(const int8_t pin) {
  80. if (digitalPinHasPCICR(pin)) {
  81. SBI(*digitalPinToPCMSK(pin), digitalPinToPCMSKbit(pin)); // enable pin
  82. SBI(PCIFR, digitalPinToPCICRbit(pin)); // clear any outstanding interrupt
  83. SBI(PCICR, digitalPinToPCICRbit(pin)); // enable interrupt for the group
  84. }
  85. }
  86. // Handlers for pin change interrupts
  87. #ifdef PCINT0_vect
  88. ISR(PCINT0_vect) { endstop_ISR(); }
  89. #endif
  90. #ifdef PCINT1_vect
  91. ISR(PCINT1_vect, ISR_ALIASOF(PCINT0_vect));
  92. #endif
  93. #ifdef PCINT2_vect
  94. ISR(PCINT2_vect, ISR_ALIASOF(PCINT0_vect));
  95. #endif
  96. #ifdef PCINT3_vect
  97. ISR(PCINT3_vect, ISR_ALIASOF(PCINT0_vect));
  98. #endif
  99. void setup_endstop_interrupts() {
  100. #define _ATTACH(P) attachInterrupt(digitalPinToInterrupt(P), endstop_ISR, CHANGE)
  101. #if HAS_X_MAX
  102. #if (digitalPinToInterrupt(X_MAX_PIN) != NOT_AN_INTERRUPT)
  103. _ATTACH(X_MAX_PIN);
  104. #else
  105. static_assert(digitalPinHasPCICR(X_MAX_PIN), "X_MAX_PIN is not interrupt-capable");
  106. pciSetup(X_MAX_PIN);
  107. #endif
  108. #endif
  109. #if HAS_X_MIN
  110. #if (digitalPinToInterrupt(X_MIN_PIN) != NOT_AN_INTERRUPT)
  111. _ATTACH(X_MIN_PIN);
  112. #else
  113. static_assert(digitalPinHasPCICR(X_MIN_PIN), "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. _ATTACH(Y_MAX_PIN);
  120. #else
  121. static_assert(digitalPinHasPCICR(Y_MAX_PIN), "Y_MAX_PIN is not interrupt-capable");
  122. pciSetup(Y_MAX_PIN);
  123. #endif
  124. #endif
  125. #if HAS_Y_MIN
  126. #if (digitalPinToInterrupt(Y_MIN_PIN) != NOT_AN_INTERRUPT)
  127. _ATTACH(Y_MIN_PIN);
  128. #else
  129. static_assert(digitalPinHasPCICR(Y_MIN_PIN), "Y_MIN_PIN is not interrupt-capable");
  130. pciSetup(Y_MIN_PIN);
  131. #endif
  132. #endif
  133. #if HAS_Z_MAX
  134. #if (digitalPinToInterrupt(Z_MAX_PIN) != NOT_AN_INTERRUPT)
  135. _ATTACH(Z_MAX_PIN);
  136. #else
  137. static_assert(digitalPinHasPCICR(Z_MAX_PIN), "Z_MAX_PIN is not interrupt-capable");
  138. pciSetup(Z_MAX_PIN);
  139. #endif
  140. #endif
  141. #if HAS_Z_MIN
  142. #if (digitalPinToInterrupt(Z_MIN_PIN) != NOT_AN_INTERRUPT)
  143. _ATTACH(Z_MIN_PIN);
  144. #else
  145. static_assert(digitalPinHasPCICR(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. _ATTACH(X2_MAX_PIN);
  152. #else
  153. static_assert(digitalPinHasPCICR(X2_MAX_PIN), "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. _ATTACH(X2_MIN_PIN);
  160. #else
  161. static_assert(digitalPinHasPCICR(X2_MIN_PIN), "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. _ATTACH(Y2_MAX_PIN);
  168. #else
  169. static_assert(digitalPinHasPCICR(Y2_MAX_PIN), "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. _ATTACH(Y2_MIN_PIN);
  176. #else
  177. static_assert(digitalPinHasPCICR(Y2_MIN_PIN), "Y2_MIN_PIN is not interrupt-capable");
  178. pciSetup(Y2_MIN_PIN);
  179. #endif
  180. #endif
  181. #if HAS_Z2_MAX
  182. #if (digitalPinToInterrupt(Z2_MAX_PIN) != NOT_AN_INTERRUPT)
  183. _ATTACH(Z2_MAX_PIN);
  184. #else
  185. static_assert(digitalPinHasPCICR(Z2_MAX_PIN), "Z2_MAX_PIN is not interrupt-capable");
  186. pciSetup(Z2_MAX_PIN);
  187. #endif
  188. #endif
  189. #if HAS_Z2_MIN
  190. #if (digitalPinToInterrupt(Z2_MIN_PIN) != NOT_AN_INTERRUPT)
  191. _ATTACH(Z2_MIN_PIN);
  192. #else
  193. static_assert(digitalPinHasPCICR(Z2_MIN_PIN), "Z2_MIN_PIN is not interrupt-capable");
  194. pciSetup(Z2_MIN_PIN);
  195. #endif
  196. #endif
  197. #if HAS_Z3_MAX
  198. #if (digitalPinToInterrupt(Z3_MAX_PIN) != NOT_AN_INTERRUPT)
  199. _ATTACH(Z3_MAX_PIN);
  200. #else
  201. static_assert(digitalPinHasPCICR(Z3_MAX_PIN), "Z3_MAX_PIN is not interrupt-capable");
  202. pciSetup(Z3_MAX_PIN);
  203. #endif
  204. #endif
  205. #if HAS_Z3_MIN
  206. #if (digitalPinToInterrupt(Z3_MIN_PIN) != NOT_AN_INTERRUPT)
  207. _ATTACH(Z3_MIN_PIN);
  208. #else
  209. static_assert(digitalPinHasPCICR(Z3_MIN_PIN), "Z3_MIN_PIN is not interrupt-capable");
  210. pciSetup(Z3_MIN_PIN);
  211. #endif
  212. #endif
  213. #if HAS_Z4_MAX
  214. #if (digitalPinToInterrupt(Z4_MAX_PIN) != NOT_AN_INTERRUPT)
  215. _ATTACH(Z4_MAX_PIN);
  216. #else
  217. static_assert(digitalPinHasPCICR(Z4_MAX_PIN), "Z4_MAX_PIN is not interrupt-capable");
  218. pciSetup(Z4_MAX_PIN);
  219. #endif
  220. #endif
  221. #if HAS_Z4_MIN
  222. #if (digitalPinToInterrupt(Z4_MIN_PIN) != NOT_AN_INTERRUPT)
  223. _ATTACH(Z4_MIN_PIN);
  224. #else
  225. static_assert(digitalPinHasPCICR(Z4_MIN_PIN), "Z4_MIN_PIN is not interrupt-capable");
  226. pciSetup(Z4_MIN_PIN);
  227. #endif
  228. #endif
  229. #if HAS_Z_MIN_PROBE_PIN
  230. #if (digitalPinToInterrupt(Z_MIN_PROBE_PIN) != NOT_AN_INTERRUPT)
  231. _ATTACH(Z_MIN_PROBE_PIN);
  232. #else
  233. static_assert(digitalPinHasPCICR(Z_MIN_PROBE_PIN), "Z_MIN_PROBE_PIN is not interrupt-capable");
  234. pciSetup(Z_MIN_PROBE_PIN);
  235. #endif
  236. #endif
  237. // If we arrive here without raising an assertion, each pin has either an EXT-interrupt or a PCI.
  238. }