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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. * Without endstop interrups the stepper-ISR must always test all endstops when interested in their states (endstops.update()).
  25. * Most time the test will result in finding out nothing has changed.
  26. * With endstop interrupts endstops.update() is called only when we know that at least one endstop has changed its state.
  27. *
  28. * This can work only if all __used__ endstop pins can provide ether an 'external interrupt' or a 'pin change interrupt'.
  29. * You can find out about pins issuing interrupts by running 'pin_interrupt_test.ino' (Marlin\buildroot\share\pin_interrupt_test\pin_interrupt_test.ino)
  30. */
  31. #ifndef _ENDSTOP_INTERRUPTS_H_
  32. #define _ENDSTOP_INTERRUPTS_H_
  33. /**
  34. * Patch for pins_arduino.h (...\Arduino\hardware\arduino\avr\variants\mega\pins_arduino.h)
  35. *
  36. * These macros for the Arduino MEGA do not include the two connected pins on Port J (D13, D14).
  37. * So we extend them here because this are the normal pins for Y_MIN and Y_MAX on RAMPS.
  38. * There are more PCI enabled processor pins on Port J, but they are not connected to Arduino MEGA.
  39. */
  40. #if defined(ARDUINO_AVR_MEGA2560) || defined(ARDUINO_AVR_MEGA)
  41. #undef digitalPinToPCICR
  42. #define digitalPinToPCICR(p) ( (((p) >= 10) && ((p) <= 15)) || \
  43. (((p) >= 50) && ((p) <= 53)) || \
  44. (((p) >= 62) && ((p) <= 69)) ? (&PCICR) : ((uint8_t *)0) )
  45. #undef digitalPinToPCICRbit
  46. #define digitalPinToPCICRbit(p) ( (((p) >= 10) && ((p) <= 13)) || (((p) >= 50) && ((p) <= 53)) ? 0 : \
  47. ( (((p) >= 14) && ((p) <= 15)) ? 1 : \
  48. ( (((p) >= 62) && ((p) <= 69)) ? 2 : \
  49. 0 ) ) )
  50. #undef digitalPinToPCMSK
  51. #define digitalPinToPCMSK(p) ( (((p) >= 10) && ((p) <= 13)) || (((p) >= 50) && ((p) <= 53)) ? (&PCMSK0) : \
  52. ( (((p) >= 14) && ((p) <= 15)) ? (&PCMSK1) : \
  53. ( (((p) >= 62) && ((p) <= 69)) ? (&PCMSK2) : \
  54. ((uint8_t *)0) ) ) )
  55. #undef digitalPinToPCMSKbit
  56. #define digitalPinToPCMSKbit(p) ( (((p) >= 10) && ((p) <= 13)) ? ((p) - 6) : \
  57. ( ((p) == 14) ? 2 : \
  58. ( ((p) == 15) ? 1 : \
  59. ( ((p) == 50) ? 3 : \
  60. ( ((p) == 51) ? 2 : \
  61. ( ((p) == 52) ? 1 : \
  62. ( ((p) == 53) ? 0 : \
  63. ( (((p) >= 62) && ((p) <= 69)) ? ((p) - 62) : \
  64. 0 ) ) ) ) ) ) ) )
  65. #endif
  66. volatile uint8_t e_hit = 0; // Different from 0 when the endstops shall be tested in detail.
  67. // Must be reset to 0 by the test function when the tests are finished.
  68. // Install Pin change interrupt for a pin, can be called multiple times
  69. void pciSetup(byte pin) {
  70. *digitalPinToPCMSK(pin) |= bit (digitalPinToPCMSKbit(pin)); // enable pin
  71. PCIFR |= bit (digitalPinToPCICRbit(pin)); // clear any outstanding interrupt
  72. PCICR |= bit (digitalPinToPCICRbit(pin)); // enable interrupt for the group
  73. }
  74. // This is what is really done inside the interrupts.
  75. FORCE_INLINE void endstop_ISR_worker( void ) {
  76. e_hit = 2; // Because the detection of a e-stop hit has a 1 step debouncer it has to be called at least twice.
  77. }
  78. // Use one Routine to handle each group
  79. // One ISR for all EXT-Interrupts
  80. void endstop_ISR(void) {
  81. endstop_ISR_worker();
  82. }
  83. #ifdef PCINT0_vect
  84. ISR(PCINT0_vect) { // handle pin change interrupt
  85. endstop_ISR_worker();
  86. }
  87. #endif
  88. #ifdef PCINT1_vect
  89. ISR(PCINT1_vect) { // handle pin change interrupt
  90. endstop_ISR_worker();
  91. }
  92. #endif
  93. #ifdef PCINT2_vect
  94. ISR(PCINT2_vect) { // handle pin change interrupt
  95. endstop_ISR_worker();
  96. }
  97. #endif
  98. #ifdef PCINT3_vect
  99. ISR(PCINT3_vect) { // handle pin change interrupt
  100. endstop_ISR_worker();
  101. }
  102. #endif
  103. void setup_enstop_interrupts( void ) {
  104. #if HAS_X_MAX
  105. #if (digitalPinToInterrupt(X_MAX_PIN) != NOT_AN_INTERRUPT) // if pin has an external interrupt
  106. attachInterrupt(digitalPinToInterrupt(X_MAX_PIN), endstop_ISR, CHANGE); // assign it
  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_MAX_PIN) != NULL, "ENDSTOP_INTERRUPT_ERROR"); // if pin has no pin change interrupt - error
  110. pciSetup(X_MAX_PIN); // assign it
  111. #endif
  112. #endif
  113. #if HAS_X_MIN
  114. #if (digitalPinToInterrupt(X_MIN_PIN) != NOT_AN_INTERRUPT)
  115. attachInterrupt(digitalPinToInterrupt(X_MIN_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(X_MIN_PIN) != NULL, "ENDSTOP_INTERRUPT_ERROR");
  119. pciSetup(X_MIN_PIN);
  120. #endif
  121. #endif
  122. #if HAS_Y_MAX
  123. #if (digitalPinToInterrupt(Y_MAX_PIN) != NOT_AN_INTERRUPT)
  124. attachInterrupt(digitalPinToInterrupt(Y_MAX_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_MAX_PIN) != NULL, "ENDSTOP_INTERRUPT_ERROR");
  128. pciSetup(Y_MAX_PIN);
  129. #endif
  130. #endif
  131. #if HAS_Y_MIN
  132. #if (digitalPinToInterrupt(Y_MIN_PIN) != NOT_AN_INTERRUPT)
  133. attachInterrupt(digitalPinToInterrupt(Y_MIN_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(Y_MIN_PIN) != NULL, "ENDSTOP_INTERRUPT_ERROR");
  137. pciSetup(Y_MIN_PIN);
  138. #endif
  139. #endif
  140. #if HAS_Z_MAX
  141. #if (digitalPinToInterrupt(Z_MAX_PIN) != NOT_AN_INTERRUPT)
  142. attachInterrupt(digitalPinToInterrupt(Z_MAX_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_MAX_PIN) != NULL, "ENDSTOP_INTERRUPT_ERROR");
  146. pciSetup(Z_MAX_PIN);
  147. #endif
  148. #endif
  149. #if HAS_Z_MIN
  150. #if (digitalPinToInterrupt(Z_MIN_PIN) != NOT_AN_INTERRUPT)
  151. attachInterrupt(digitalPinToInterrupt(Z_MIN_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(Z_MIN_PIN) != NULL, "ENDSTOP_INTERRUPT_ERROR");
  155. pciSetup(Z_MIN_PIN);
  156. #endif
  157. #endif
  158. #if HAS_Z2_MAX
  159. #if (digitalPinToInterrupt(Z2_MAX_PIN) != NOT_AN_INTERRUPT)
  160. attachInterrupt(digitalPinToInterrupt(Z2_MAX_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(Z2_MAX_PIN) != NULL, "ENDSTOP_INTERRUPT_ERROR");
  164. pciSetup(Z2_MAX_PIN);
  165. #endif
  166. #endif
  167. #if HAS_Z2_MIN
  168. #if (digitalPinToInterrupt(Z2_MIN_PIN) != NOT_AN_INTERRUPT)
  169. attachInterrupt(digitalPinToInterrupt(Z2_MIN_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(Z2_MIN_PIN) != NULL, "ENDSTOP_INTERRUPT_ERROR");
  173. pciSetup(Z2_MIN_PIN);
  174. #endif
  175. #endif
  176. #if HAS_Z_MIN_PROBE_PIN
  177. #if (digitalPinToInterrupt(Z_MIN_PROBE_PIN) != NOT_AN_INTERRUPT)
  178. attachInterrupt(digitalPinToInterrupt(Z_MIN_PROBE_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(Z_MIN_PROBE_PIN) != NULL, "ENDSTOP_INTERRUPT_ERROR");
  182. pciSetup(Z_MIN_PROBE_PIN);
  183. #endif
  184. #endif
  185. // When we arive here without error each pin has ether a EXT-interrupt or a PCI.
  186. }
  187. #endif //_ENDSTOP_INTERRUPTS_H_