My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

endstop_interrupts.h 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 stepper-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. /**
  39. * Patch for pins_arduino.h (...\Arduino\hardware\arduino\avr\variants\mega\pins_arduino.h)
  40. *
  41. * These macros for the Arduino MEGA do not include the two connected pins on Port J (D13, D14).
  42. * So we extend them here because these are the normal pins for Y_MIN and Y_MAX on RAMPS.
  43. * There are more PCI-enabled processor pins on Port J, but they are not connected to Arduino MEGA.
  44. */
  45. #if defined(ARDUINO_AVR_MEGA2560) || defined(ARDUINO_AVR_MEGA)
  46. #undef digitalPinToPCICR
  47. #define digitalPinToPCICR(p) ( (((p) >= 10) && ((p) <= 15)) || \
  48. (((p) >= 50) && ((p) <= 53)) || \
  49. (((p) >= 62) && ((p) <= 69)) ? (&PCICR) : ((uint8_t *)0) )
  50. #undef digitalPinToPCICRbit
  51. #define digitalPinToPCICRbit(p) ( (((p) >= 10) && ((p) <= 13)) || (((p) >= 50) && ((p) <= 53)) ? 0 : \
  52. ( (((p) >= 14) && ((p) <= 15)) ? 1 : \
  53. ( (((p) >= 62) && ((p) <= 69)) ? 2 : \
  54. 0 ) ) )
  55. #undef digitalPinToPCMSK
  56. #define digitalPinToPCMSK(p) ( (((p) >= 10) && ((p) <= 13)) || (((p) >= 50) && ((p) <= 53)) ? (&PCMSK0) : \
  57. ( (((p) >= 14) && ((p) <= 15)) ? (&PCMSK1) : \
  58. ( (((p) >= 62) && ((p) <= 69)) ? (&PCMSK2) : \
  59. ((uint8_t *)0) ) ) )
  60. #undef digitalPinToPCMSKbit
  61. #define digitalPinToPCMSKbit(p) ( (((p) >= 10) && ((p) <= 13)) ? ((p) - 6) : \
  62. ( ((p) == 14) ? 2 : \
  63. ( ((p) == 15) ? 1 : \
  64. ( ((p) == 50) ? 3 : \
  65. ( ((p) == 51) ? 2 : \
  66. ( ((p) == 52) ? 1 : \
  67. ( ((p) == 53) ? 0 : \
  68. ( (((p) >= 62) && ((p) <= 69)) ? ((p) - 62) : \
  69. 0 ) ) ) ) ) ) ) )
  70. #endif
  71. volatile uint8_t e_hit = 0; // Different from 0 when the endstops shall be tested in detail.
  72. // Must be reset to 0 by the test function when the tests are finished.
  73. // Install Pin change interrupt for a pin. Can be called multiple times.
  74. void pciSetup(byte pin) {
  75. *digitalPinToPCMSK(pin) |= bit (digitalPinToPCMSKbit(pin)); // enable pin
  76. PCIFR |= bit (digitalPinToPCICRbit(pin)); // clear any outstanding interrupt
  77. PCICR |= bit (digitalPinToPCICRbit(pin)); // enable interrupt for the group
  78. }
  79. // This is what is really done inside the interrupts.
  80. FORCE_INLINE void endstop_ISR_worker( void ) {
  81. e_hit = 2; // Because the detection of a e-stop hit has a 1 step debouncer it has to be called at least twice.
  82. }
  83. // Use one Routine to handle each group
  84. // One ISR for all EXT-Interrupts
  85. void endstop_ISR(void) { endstop_ISR_worker(); }
  86. // Handlers for pin change interrupts
  87. #ifdef PCINT0_vect
  88. ISR(PCINT0_vect) { endstop_ISR_worker(); }
  89. #endif
  90. #ifdef PCINT1_vect
  91. ISR(PCINT1_vect) { endstop_ISR_worker(); }
  92. #endif
  93. #ifdef PCINT2_vect
  94. ISR(PCINT2_vect) { endstop_ISR_worker(); }
  95. #endif
  96. #ifdef PCINT3_vect
  97. ISR(PCINT3_vect) { endstop_ISR_worker(); }
  98. #endif
  99. void setup_endstop_interrupts( void ) {
  100. #if HAS_X_MAX
  101. #if (digitalPinToInterrupt(X_MAX_PIN) != NOT_AN_INTERRUPT) // if pin has an external interrupt
  102. attachInterrupt(digitalPinToInterrupt(X_MAX_PIN), endstop_ISR, CHANGE); // assign it
  103. #else
  104. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  105. static_assert(digitalPinToPCICR(X_MAX_PIN) != NULL, "X_MAX_PIN is not interrupt-capable"); // if pin has no pin change interrupt - error
  106. pciSetup(X_MAX_PIN); // assign it
  107. #endif
  108. #endif
  109. #if HAS_X_MIN
  110. #if (digitalPinToInterrupt(X_MIN_PIN) != NOT_AN_INTERRUPT)
  111. attachInterrupt(digitalPinToInterrupt(X_MIN_PIN), endstop_ISR, CHANGE);
  112. #else
  113. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  114. static_assert(digitalPinToPCICR(X_MIN_PIN) != NULL, "X_MIN_PIN is not interrupt-capable");
  115. pciSetup(X_MIN_PIN);
  116. #endif
  117. #endif
  118. #if HAS_Y_MAX
  119. #if (digitalPinToInterrupt(Y_MAX_PIN) != NOT_AN_INTERRUPT)
  120. attachInterrupt(digitalPinToInterrupt(Y_MAX_PIN), endstop_ISR, CHANGE);
  121. #else
  122. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  123. static_assert(digitalPinToPCICR(Y_MAX_PIN) != NULL, "Y_MAX_PIN is not interrupt-capable");
  124. pciSetup(Y_MAX_PIN);
  125. #endif
  126. #endif
  127. #if HAS_Y_MIN
  128. #if (digitalPinToInterrupt(Y_MIN_PIN) != NOT_AN_INTERRUPT)
  129. attachInterrupt(digitalPinToInterrupt(Y_MIN_PIN), endstop_ISR, CHANGE);
  130. #else
  131. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  132. static_assert(digitalPinToPCICR(Y_MIN_PIN) != NULL, "Y_MIN_PIN is not interrupt-capable");
  133. pciSetup(Y_MIN_PIN);
  134. #endif
  135. #endif
  136. #if HAS_Z_MAX
  137. #if (digitalPinToInterrupt(Z_MAX_PIN) != NOT_AN_INTERRUPT)
  138. attachInterrupt(digitalPinToInterrupt(Z_MAX_PIN), endstop_ISR, CHANGE);
  139. #else
  140. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  141. static_assert(digitalPinToPCICR(Z_MAX_PIN) != NULL, "Z_MAX_PIN is not interrupt-capable");
  142. pciSetup(Z_MAX_PIN);
  143. #endif
  144. #endif
  145. #if HAS_Z_MIN
  146. #if (digitalPinToInterrupt(Z_MIN_PIN) != NOT_AN_INTERRUPT)
  147. attachInterrupt(digitalPinToInterrupt(Z_MIN_PIN), endstop_ISR, CHANGE);
  148. #else
  149. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  150. static_assert(digitalPinToPCICR(Z_MIN_PIN) != NULL, "Z_MIN_PIN is not interrupt-capable");
  151. pciSetup(Z_MIN_PIN);
  152. #endif
  153. #endif
  154. #if HAS_Z2_MAX
  155. #if (digitalPinToInterrupt(Z2_MAX_PIN) != NOT_AN_INTERRUPT)
  156. attachInterrupt(digitalPinToInterrupt(Z2_MAX_PIN), endstop_ISR, CHANGE);
  157. #else
  158. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  159. static_assert(digitalPinToPCICR(Z2_MAX_PIN) != NULL, "Z2_MAX_PIN is not interrupt-capable");
  160. pciSetup(Z2_MAX_PIN);
  161. #endif
  162. #endif
  163. #if HAS_Z2_MIN
  164. #if (digitalPinToInterrupt(Z2_MIN_PIN) != NOT_AN_INTERRUPT)
  165. attachInterrupt(digitalPinToInterrupt(Z2_MIN_PIN), endstop_ISR, CHANGE);
  166. #else
  167. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  168. static_assert(digitalPinToPCICR(Z2_MIN_PIN) != NULL, "Z2_MIN_PIN is not interrupt-capable");
  169. pciSetup(Z2_MIN_PIN);
  170. #endif
  171. #endif
  172. #if HAS_Z_MIN_PROBE_PIN
  173. #if (digitalPinToInterrupt(Z_MIN_PROBE_PIN) != NOT_AN_INTERRUPT)
  174. attachInterrupt(digitalPinToInterrupt(Z_MIN_PROBE_PIN), endstop_ISR, CHANGE);
  175. #else
  176. // Not all used endstop/probe -pins can raise interrupts. Please deactivate ENDSTOP_INTERRUPTS or change the pin configuration!
  177. static_assert(digitalPinToPCICR(Z_MIN_PROBE_PIN) != NULL, "Z_MIN_PROBE_PIN is not interrupt-capable");
  178. pciSetup(Z_MIN_PROBE_PIN);
  179. #endif
  180. #endif
  181. // If we arrive here without raising an assertion, each pin has either an EXT-interrupt or a PCI.
  182. }
  183. #endif //_ENDSTOP_INTERRUPTS_H_