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.

stepper.cpp 42KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  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. * stepper.cpp - stepper motor driver: executes motion plans using stepper motors
  24. * Marlin Firmware
  25. *
  26. * Derived from Grbl
  27. * Copyright (c) 2009-2011 Simen Svale Skogsrud
  28. *
  29. * Grbl is free software: you can redistribute it and/or modify
  30. * it under the terms of the GNU General Public License as published by
  31. * the Free Software Foundation, either version 3 of the License, or
  32. * (at your option) any later version.
  33. *
  34. * Grbl is distributed in the hope that it will be useful,
  35. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. * GNU General Public License for more details.
  38. *
  39. * You should have received a copy of the GNU General Public License
  40. * along with Grbl. If not, see <http://www.gnu.org/licenses/>.
  41. */
  42. /* The timer calculations of this module informed by the 'RepRap cartesian firmware' by Zack Smith
  43. and Philipp Tiefenbacher. */
  44. #include "Marlin.h"
  45. #include "stepper.h"
  46. #include "planner.h"
  47. #include "temperature.h"
  48. #include "ultralcd.h"
  49. #include "language.h"
  50. #include "cardreader.h"
  51. #include "speed_lookuptable.h"
  52. #if HAS_DIGIPOTSS
  53. #include <SPI.h>
  54. #endif
  55. //===========================================================================
  56. //============================= public variables ============================
  57. //===========================================================================
  58. block_t* current_block; // A pointer to the block currently being traced
  59. #if ENABLED(HAS_Z_MIN_PROBE)
  60. volatile bool z_probe_is_active = false;
  61. #endif
  62. //===========================================================================
  63. //============================= private variables ===========================
  64. //===========================================================================
  65. //static makes it impossible to be called from outside of this file by extern.!
  66. // Variables used by The Stepper Driver Interrupt
  67. static unsigned char out_bits = 0; // The next stepping-bits to be output
  68. static unsigned int cleaning_buffer_counter;
  69. #if ENABLED(Z_DUAL_ENDSTOPS)
  70. static bool performing_homing = false,
  71. locked_z_motor = false,
  72. locked_z2_motor = false;
  73. #endif
  74. // Counter variables for the Bresenham line tracer
  75. static long counter_x, counter_y, counter_z, counter_e;
  76. volatile static unsigned long step_events_completed; // The number of step events executed in the current block
  77. #if ENABLED(ADVANCE)
  78. static long advance_rate, advance, final_advance = 0;
  79. static long old_advance = 0;
  80. static long e_steps[4];
  81. #endif
  82. static long acceleration_time, deceleration_time;
  83. //static unsigned long accelerate_until, decelerate_after, acceleration_rate, initial_rate, final_rate, nominal_rate;
  84. static unsigned short acc_step_rate; // needed for deceleration start point
  85. static uint8_t step_loops;
  86. static uint8_t step_loops_nominal;
  87. static unsigned short OCR1A_nominal;
  88. volatile long endstops_trigsteps[3] = { 0 };
  89. volatile long endstops_stepsTotal, endstops_stepsDone;
  90. static volatile char endstop_hit_bits = 0; // use X_MIN, Y_MIN, Z_MIN and Z_MIN_PROBE as BIT value
  91. #if DISABLED(Z_DUAL_ENDSTOPS)
  92. static byte
  93. #else
  94. static uint16_t
  95. #endif
  96. old_endstop_bits = 0; // use X_MIN, X_MAX... Z_MAX, Z_MIN_PROBE, Z2_MIN, Z2_MAX
  97. #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
  98. bool abort_on_endstop_hit = false;
  99. #endif
  100. #if PIN_EXISTS(MOTOR_CURRENT_PWM_XY)
  101. #ifndef PWM_MOTOR_CURRENT
  102. #define PWM_MOTOR_CURRENT DEFAULT_PWM_MOTOR_CURRENT
  103. #endif
  104. const int motor_current_setting[3] = PWM_MOTOR_CURRENT;
  105. #endif
  106. static bool check_endstops = true;
  107. static bool check_endstops_global =
  108. #if ENABLED(ENDSTOPS_ONLY_FOR_HOMING)
  109. false
  110. #else
  111. true
  112. #endif
  113. ;
  114. volatile long count_position[NUM_AXIS] = { 0 }; // Positions of stepper motors, in step units
  115. volatile signed char count_direction[NUM_AXIS] = { 1 };
  116. //===========================================================================
  117. //================================ functions ================================
  118. //===========================================================================
  119. #if ENABLED(DUAL_X_CARRIAGE)
  120. #define X_APPLY_DIR(v,ALWAYS) \
  121. if (extruder_duplication_enabled || ALWAYS) { \
  122. X_DIR_WRITE(v); \
  123. X2_DIR_WRITE(v); \
  124. } \
  125. else { \
  126. if (current_block->active_extruder) X2_DIR_WRITE(v); else X_DIR_WRITE(v); \
  127. }
  128. #define X_APPLY_STEP(v,ALWAYS) \
  129. if (extruder_duplication_enabled || ALWAYS) { \
  130. X_STEP_WRITE(v); \
  131. X2_STEP_WRITE(v); \
  132. } \
  133. else { \
  134. if (current_block->active_extruder != 0) X2_STEP_WRITE(v); else X_STEP_WRITE(v); \
  135. }
  136. #else
  137. #define X_APPLY_DIR(v,Q) X_DIR_WRITE(v)
  138. #define X_APPLY_STEP(v,Q) X_STEP_WRITE(v)
  139. #endif
  140. #if ENABLED(Y_DUAL_STEPPER_DRIVERS)
  141. #define Y_APPLY_DIR(v,Q) { Y_DIR_WRITE(v); Y2_DIR_WRITE((v) != INVERT_Y2_VS_Y_DIR); }
  142. #define Y_APPLY_STEP(v,Q) { Y_STEP_WRITE(v); Y2_STEP_WRITE(v); }
  143. #else
  144. #define Y_APPLY_DIR(v,Q) Y_DIR_WRITE(v)
  145. #define Y_APPLY_STEP(v,Q) Y_STEP_WRITE(v)
  146. #endif
  147. #if ENABLED(Z_DUAL_STEPPER_DRIVERS)
  148. #define Z_APPLY_DIR(v,Q) { Z_DIR_WRITE(v); Z2_DIR_WRITE(v); }
  149. #if ENABLED(Z_DUAL_ENDSTOPS)
  150. #define Z_APPLY_STEP(v,Q) \
  151. if (performing_homing) { \
  152. if (Z_HOME_DIR > 0) {\
  153. if (!(TEST(old_endstop_bits, Z_MAX) && (count_direction[Z_AXIS] > 0)) && !locked_z_motor) Z_STEP_WRITE(v); \
  154. if (!(TEST(old_endstop_bits, Z2_MAX) && (count_direction[Z_AXIS] > 0)) && !locked_z2_motor) Z2_STEP_WRITE(v); \
  155. } \
  156. else { \
  157. if (!(TEST(old_endstop_bits, Z_MIN) && (count_direction[Z_AXIS] < 0)) && !locked_z_motor) Z_STEP_WRITE(v); \
  158. if (!(TEST(old_endstop_bits, Z2_MIN) && (count_direction[Z_AXIS] < 0)) && !locked_z2_motor) Z2_STEP_WRITE(v); \
  159. } \
  160. } \
  161. else { \
  162. Z_STEP_WRITE(v); \
  163. Z2_STEP_WRITE(v); \
  164. }
  165. #else
  166. #define Z_APPLY_STEP(v,Q) { Z_STEP_WRITE(v); Z2_STEP_WRITE(v); }
  167. #endif
  168. #else
  169. #define Z_APPLY_DIR(v,Q) Z_DIR_WRITE(v)
  170. #define Z_APPLY_STEP(v,Q) Z_STEP_WRITE(v)
  171. #endif
  172. #define E_APPLY_STEP(v,Q) E_STEP_WRITE(v)
  173. // intRes = intIn1 * intIn2 >> 16
  174. // uses:
  175. // r26 to store 0
  176. // r27 to store the byte 1 of the 24 bit result
  177. #define MultiU16X8toH16(intRes, charIn1, intIn2) \
  178. asm volatile ( \
  179. "clr r26 \n\t" \
  180. "mul %A1, %B2 \n\t" \
  181. "movw %A0, r0 \n\t" \
  182. "mul %A1, %A2 \n\t" \
  183. "add %A0, r1 \n\t" \
  184. "adc %B0, r26 \n\t" \
  185. "lsr r0 \n\t" \
  186. "adc %A0, r26 \n\t" \
  187. "adc %B0, r26 \n\t" \
  188. "clr r1 \n\t" \
  189. : \
  190. "=&r" (intRes) \
  191. : \
  192. "d" (charIn1), \
  193. "d" (intIn2) \
  194. : \
  195. "r26" \
  196. )
  197. // intRes = longIn1 * longIn2 >> 24
  198. // uses:
  199. // r26 to store 0
  200. // r27 to store bits 16-23 of the 48bit result. The top bit is used to round the two byte result.
  201. // note that the lower two bytes and the upper byte of the 48bit result are not calculated.
  202. // this can cause the result to be out by one as the lower bytes may cause carries into the upper ones.
  203. // B0 A0 are bits 24-39 and are the returned value
  204. // C1 B1 A1 is longIn1
  205. // D2 C2 B2 A2 is longIn2
  206. //
  207. #define MultiU24X32toH16(intRes, longIn1, longIn2) \
  208. asm volatile ( \
  209. "clr r26 \n\t" \
  210. "mul %A1, %B2 \n\t" \
  211. "mov r27, r1 \n\t" \
  212. "mul %B1, %C2 \n\t" \
  213. "movw %A0, r0 \n\t" \
  214. "mul %C1, %C2 \n\t" \
  215. "add %B0, r0 \n\t" \
  216. "mul %C1, %B2 \n\t" \
  217. "add %A0, r0 \n\t" \
  218. "adc %B0, r1 \n\t" \
  219. "mul %A1, %C2 \n\t" \
  220. "add r27, r0 \n\t" \
  221. "adc %A0, r1 \n\t" \
  222. "adc %B0, r26 \n\t" \
  223. "mul %B1, %B2 \n\t" \
  224. "add r27, r0 \n\t" \
  225. "adc %A0, r1 \n\t" \
  226. "adc %B0, r26 \n\t" \
  227. "mul %C1, %A2 \n\t" \
  228. "add r27, r0 \n\t" \
  229. "adc %A0, r1 \n\t" \
  230. "adc %B0, r26 \n\t" \
  231. "mul %B1, %A2 \n\t" \
  232. "add r27, r1 \n\t" \
  233. "adc %A0, r26 \n\t" \
  234. "adc %B0, r26 \n\t" \
  235. "lsr r27 \n\t" \
  236. "adc %A0, r26 \n\t" \
  237. "adc %B0, r26 \n\t" \
  238. "mul %D2, %A1 \n\t" \
  239. "add %A0, r0 \n\t" \
  240. "adc %B0, r1 \n\t" \
  241. "mul %D2, %B1 \n\t" \
  242. "add %B0, r0 \n\t" \
  243. "clr r1 \n\t" \
  244. : \
  245. "=&r" (intRes) \
  246. : \
  247. "d" (longIn1), \
  248. "d" (longIn2) \
  249. : \
  250. "r26" , "r27" \
  251. )
  252. // Some useful constants
  253. #define ENABLE_STEPPER_DRIVER_INTERRUPT() SBI(TIMSK1, OCIE1A)
  254. #define DISABLE_STEPPER_DRIVER_INTERRUPT() CBI(TIMSK1, OCIE1A)
  255. void enable_endstops(bool check) { check_endstops = check; }
  256. void enable_endstops_globally(bool check) { check_endstops_global = check_endstops = check; }
  257. void endstops_not_homing() { check_endstops = check_endstops_global; }
  258. void endstops_hit_on_purpose() { endstop_hit_bits = 0; }
  259. void checkHitEndstops() {
  260. if (endstop_hit_bits) {
  261. SERIAL_ECHO_START;
  262. SERIAL_ECHOPGM(MSG_ENDSTOPS_HIT);
  263. if (TEST(endstop_hit_bits, X_MIN)) {
  264. SERIAL_ECHOPAIR(" X:", (float)endstops_trigsteps[X_AXIS] / axis_steps_per_unit[X_AXIS]);
  265. LCD_MESSAGEPGM(MSG_ENDSTOPS_HIT "X");
  266. }
  267. if (TEST(endstop_hit_bits, Y_MIN)) {
  268. SERIAL_ECHOPAIR(" Y:", (float)endstops_trigsteps[Y_AXIS] / axis_steps_per_unit[Y_AXIS]);
  269. LCD_MESSAGEPGM(MSG_ENDSTOPS_HIT "Y");
  270. }
  271. if (TEST(endstop_hit_bits, Z_MIN)) {
  272. SERIAL_ECHOPAIR(" Z:", (float)endstops_trigsteps[Z_AXIS] / axis_steps_per_unit[Z_AXIS]);
  273. LCD_MESSAGEPGM(MSG_ENDSTOPS_HIT "Z");
  274. }
  275. #if ENABLED(Z_MIN_PROBE_ENDSTOP)
  276. if (TEST(endstop_hit_bits, Z_MIN_PROBE)) {
  277. SERIAL_ECHOPAIR(" Z_MIN_PROBE:", (float)endstops_trigsteps[Z_AXIS] / axis_steps_per_unit[Z_AXIS]);
  278. LCD_MESSAGEPGM(MSG_ENDSTOPS_HIT "ZP");
  279. }
  280. #endif
  281. SERIAL_EOL;
  282. endstops_hit_on_purpose();
  283. #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED) && ENABLED(SDSUPPORT)
  284. if (abort_on_endstop_hit) {
  285. card.sdprinting = false;
  286. card.closefile();
  287. quickStop();
  288. disable_all_heaters(); // switch off all heaters.
  289. }
  290. #endif
  291. }
  292. }
  293. // Check endstops - Called from ISR!
  294. inline void update_endstops() {
  295. #if ENABLED(Z_DUAL_ENDSTOPS)
  296. uint16_t
  297. #else
  298. byte
  299. #endif
  300. current_endstop_bits = 0;
  301. #define _ENDSTOP_PIN(AXIS, MINMAX) AXIS ##_## MINMAX ##_PIN
  302. #define _ENDSTOP_INVERTING(AXIS, MINMAX) AXIS ##_## MINMAX ##_ENDSTOP_INVERTING
  303. #define _AXIS(AXIS) AXIS ##_AXIS
  304. #define _ENDSTOP_HIT(AXIS) SBI(endstop_hit_bits, _ENDSTOP(AXIS, MIN))
  305. #define _ENDSTOP(AXIS, MINMAX) AXIS ##_## MINMAX
  306. // SET_ENDSTOP_BIT: set the current endstop bits for an endstop to its status
  307. #define SET_ENDSTOP_BIT(AXIS, MINMAX) SET_BIT(current_endstop_bits, _ENDSTOP(AXIS, MINMAX), (READ(_ENDSTOP_PIN(AXIS, MINMAX)) != _ENDSTOP_INVERTING(AXIS, MINMAX)))
  308. // COPY_BIT: copy the value of COPY_BIT to BIT in bits
  309. #define COPY_BIT(bits, COPY_BIT, BIT) SET_BIT(bits, BIT, TEST(bits, COPY_BIT))
  310. // TEST_ENDSTOP: test the old and the current status of an endstop
  311. #define TEST_ENDSTOP(ENDSTOP) (TEST(current_endstop_bits, ENDSTOP) && TEST(old_endstop_bits, ENDSTOP))
  312. #if ENABLED(COREXY) || ENABLED(COREXZ)
  313. #define _SET_TRIGSTEPS(AXIS) do { \
  314. float axis_pos = count_position[_AXIS(AXIS)]; \
  315. if (_AXIS(AXIS) == A_AXIS) \
  316. axis_pos = (axis_pos + count_position[CORE_AXIS_2]) / 2; \
  317. else if (_AXIS(AXIS) == CORE_AXIS_2) \
  318. axis_pos = (count_position[A_AXIS] - axis_pos) / 2; \
  319. endstops_trigsteps[_AXIS(AXIS)] = axis_pos; \
  320. } while(0)
  321. #else
  322. #define _SET_TRIGSTEPS(AXIS) endstops_trigsteps[_AXIS(AXIS)] = count_position[_AXIS(AXIS)]
  323. #endif // COREXY || COREXZ
  324. #define UPDATE_ENDSTOP(AXIS,MINMAX) do { \
  325. SET_ENDSTOP_BIT(AXIS, MINMAX); \
  326. if (TEST_ENDSTOP(_ENDSTOP(AXIS, MINMAX)) && current_block->steps[_AXIS(AXIS)] > 0) { \
  327. _SET_TRIGSTEPS(AXIS); \
  328. _ENDSTOP_HIT(AXIS); \
  329. step_events_completed = current_block->step_event_count; \
  330. } \
  331. } while(0)
  332. #if ENABLED(COREXY) || ENABLED(COREXZ)
  333. // Head direction in -X axis for CoreXY and CoreXZ bots.
  334. // If Delta1 == -Delta2, the movement is only in Y or Z axis
  335. if ((current_block->steps[A_AXIS] != current_block->steps[CORE_AXIS_2]) || (TEST(out_bits, A_AXIS) == TEST(out_bits, CORE_AXIS_2))) {
  336. if (TEST(out_bits, X_HEAD))
  337. #else
  338. if (TEST(out_bits, X_AXIS)) // stepping along -X axis (regular Cartesian bot)
  339. #endif
  340. { // -direction
  341. #if ENABLED(DUAL_X_CARRIAGE)
  342. // with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
  343. if ((current_block->active_extruder == 0 && X_HOME_DIR == -1) || (current_block->active_extruder != 0 && X2_HOME_DIR == -1))
  344. #endif
  345. {
  346. #if HAS_X_MIN
  347. UPDATE_ENDSTOP(X, MIN);
  348. #endif
  349. }
  350. }
  351. else { // +direction
  352. #if ENABLED(DUAL_X_CARRIAGE)
  353. // with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
  354. if ((current_block->active_extruder == 0 && X_HOME_DIR == 1) || (current_block->active_extruder != 0 && X2_HOME_DIR == 1))
  355. #endif
  356. {
  357. #if HAS_X_MAX
  358. UPDATE_ENDSTOP(X, MAX);
  359. #endif
  360. }
  361. }
  362. #if ENABLED(COREXY) || ENABLED(COREXZ)
  363. }
  364. #endif
  365. #if ENABLED(COREXY)
  366. // Head direction in -Y axis for CoreXY bots.
  367. // If DeltaX == DeltaY, the movement is only in X axis
  368. if ((current_block->steps[A_AXIS] != current_block->steps[B_AXIS]) || (TEST(out_bits, A_AXIS) != TEST(out_bits, B_AXIS))) {
  369. if (TEST(out_bits, Y_HEAD))
  370. #else
  371. if (TEST(out_bits, Y_AXIS)) // -direction
  372. #endif
  373. { // -direction
  374. #if HAS_Y_MIN
  375. UPDATE_ENDSTOP(Y, MIN);
  376. #endif
  377. }
  378. else { // +direction
  379. #if HAS_Y_MAX
  380. UPDATE_ENDSTOP(Y, MAX);
  381. #endif
  382. }
  383. #if ENABLED(COREXY)
  384. }
  385. #endif
  386. #if ENABLED(COREXZ)
  387. // Head direction in -Z axis for CoreXZ bots.
  388. // If DeltaX == DeltaZ, the movement is only in X axis
  389. if ((current_block->steps[A_AXIS] != current_block->steps[C_AXIS]) || (TEST(out_bits, A_AXIS) != TEST(out_bits, C_AXIS))) {
  390. if (TEST(out_bits, Z_HEAD))
  391. #else
  392. if (TEST(out_bits, Z_AXIS))
  393. #endif
  394. { // z -direction
  395. #if HAS_Z_MIN
  396. #if ENABLED(Z_DUAL_ENDSTOPS)
  397. SET_ENDSTOP_BIT(Z, MIN);
  398. #if HAS_Z2_MIN
  399. SET_ENDSTOP_BIT(Z2, MIN);
  400. #else
  401. COPY_BIT(current_endstop_bits, Z_MIN, Z2_MIN);
  402. #endif
  403. byte z_test = TEST_ENDSTOP(Z_MIN) | (TEST_ENDSTOP(Z2_MIN) << 1); // bit 0 for Z, bit 1 for Z2
  404. if (z_test && current_block->steps[Z_AXIS] > 0) { // z_test = Z_MIN || Z2_MIN
  405. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  406. SBI(endstop_hit_bits, Z_MIN);
  407. if (!performing_homing || (z_test == 0x3)) //if not performing home or if both endstops were trigged during homing...
  408. step_events_completed = current_block->step_event_count;
  409. }
  410. #else // !Z_DUAL_ENDSTOPS
  411. #if ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) && ENABLED(HAS_Z_MIN_PROBE)
  412. if (z_probe_is_active) UPDATE_ENDSTOP(Z, MIN);
  413. #else
  414. UPDATE_ENDSTOP(Z, MIN);
  415. #endif
  416. #endif // !Z_DUAL_ENDSTOPS
  417. #endif
  418. #if ENABLED(Z_MIN_PROBE_ENDSTOP) && DISABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) && ENABLED(HAS_Z_MIN_PROBE)
  419. if (z_probe_is_active) {
  420. UPDATE_ENDSTOP(Z, MIN_PROBE);
  421. if (TEST_ENDSTOP(Z_MIN_PROBE)) endstop_hit_bits |= _BV(Z_MIN_PROBE);
  422. }
  423. #endif
  424. }
  425. else { // z +direction
  426. #if HAS_Z_MAX
  427. #if ENABLED(Z_DUAL_ENDSTOPS)
  428. SET_ENDSTOP_BIT(Z, MAX);
  429. #if HAS_Z2_MAX
  430. SET_ENDSTOP_BIT(Z2, MAX);
  431. #else
  432. COPY_BIT(current_endstop_bits, Z_MAX, Z2_MAX);
  433. #endif
  434. byte z_test = TEST_ENDSTOP(Z_MAX) | (TEST_ENDSTOP(Z2_MAX) << 1); // bit 0 for Z, bit 1 for Z2
  435. if (z_test && current_block->steps[Z_AXIS] > 0) { // t_test = Z_MAX || Z2_MAX
  436. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  437. SBI(endstop_hit_bits, Z_MIN);
  438. if (!performing_homing || (z_test == 0x3)) //if not performing home or if both endstops were trigged during homing...
  439. step_events_completed = current_block->step_event_count;
  440. }
  441. #else // !Z_DUAL_ENDSTOPS
  442. UPDATE_ENDSTOP(Z, MAX);
  443. #endif // !Z_DUAL_ENDSTOPS
  444. #endif // Z_MAX_PIN
  445. }
  446. #if ENABLED(COREXZ)
  447. }
  448. #endif
  449. old_endstop_bits = current_endstop_bits;
  450. }
  451. // __________________________
  452. // /| |\ _________________ ^
  453. // / | | \ /| |\ |
  454. // / | | \ / | | \ s
  455. // / | | | | | \ p
  456. // / | | | | | \ e
  457. // +-----+------------------------+---+--+---------------+----+ e
  458. // | BLOCK 1 | BLOCK 2 | d
  459. //
  460. // time ----->
  461. //
  462. // The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates
  463. // first block->accelerate_until step_events_completed, then keeps going at constant speed until
  464. // step_events_completed reaches block->decelerate_after after which it decelerates until the trapezoid generator is reset.
  465. // The slope of acceleration is calculated using v = u + at where t is the accumulated timer values of the steps so far.
  466. void st_wake_up() {
  467. // TCNT1 = 0;
  468. ENABLE_STEPPER_DRIVER_INTERRUPT();
  469. }
  470. FORCE_INLINE unsigned short calc_timer(unsigned short step_rate) {
  471. unsigned short timer;
  472. NOMORE(step_rate, MAX_STEP_FREQUENCY);
  473. if (step_rate > 20000) { // If steprate > 20kHz >> step 4 times
  474. step_rate = (step_rate >> 2) & 0x3fff;
  475. step_loops = 4;
  476. }
  477. else if (step_rate > 10000) { // If steprate > 10kHz >> step 2 times
  478. step_rate = (step_rate >> 1) & 0x7fff;
  479. step_loops = 2;
  480. }
  481. else {
  482. step_loops = 1;
  483. }
  484. NOLESS(step_rate, F_CPU / 500000);
  485. step_rate -= F_CPU / 500000; // Correct for minimal speed
  486. if (step_rate >= (8 * 256)) { // higher step rate
  487. unsigned short table_address = (unsigned short)&speed_lookuptable_fast[(unsigned char)(step_rate >> 8)][0];
  488. unsigned char tmp_step_rate = (step_rate & 0x00ff);
  489. unsigned short gain = (unsigned short)pgm_read_word_near(table_address + 2);
  490. MultiU16X8toH16(timer, tmp_step_rate, gain);
  491. timer = (unsigned short)pgm_read_word_near(table_address) - timer;
  492. }
  493. else { // lower step rates
  494. unsigned short table_address = (unsigned short)&speed_lookuptable_slow[0][0];
  495. table_address += ((step_rate) >> 1) & 0xfffc;
  496. timer = (unsigned short)pgm_read_word_near(table_address);
  497. timer -= (((unsigned short)pgm_read_word_near(table_address + 2) * (unsigned char)(step_rate & 0x0007)) >> 3);
  498. }
  499. if (timer < 100) { timer = 100; MYSERIAL.print(MSG_STEPPER_TOO_HIGH); MYSERIAL.println(step_rate); }//(20kHz this should never happen)
  500. return timer;
  501. }
  502. /**
  503. * Set the stepper direction of each axis
  504. *
  505. * X_AXIS=A_AXIS and Y_AXIS=B_AXIS for COREXY
  506. * X_AXIS=A_AXIS and Z_AXIS=C_AXIS for COREXZ
  507. */
  508. void set_stepper_direction() {
  509. #define SET_STEP_DIR(AXIS) \
  510. if (TEST(out_bits, AXIS ##_AXIS)) { \
  511. AXIS ##_APPLY_DIR(INVERT_## AXIS ##_DIR, false); \
  512. count_direction[AXIS ##_AXIS] = -1; \
  513. } \
  514. else { \
  515. AXIS ##_APPLY_DIR(!INVERT_## AXIS ##_DIR, false); \
  516. count_direction[AXIS ##_AXIS] = 1; \
  517. }
  518. SET_STEP_DIR(X); // A
  519. SET_STEP_DIR(Y); // B
  520. SET_STEP_DIR(Z); // C
  521. #if DISABLED(ADVANCE)
  522. if (TEST(out_bits, E_AXIS)) {
  523. REV_E_DIR();
  524. count_direction[E_AXIS] = -1;
  525. }
  526. else {
  527. NORM_E_DIR();
  528. count_direction[E_AXIS] = 1;
  529. }
  530. #endif //!ADVANCE
  531. }
  532. // Initializes the trapezoid generator from the current block. Called whenever a new
  533. // block begins.
  534. FORCE_INLINE void trapezoid_generator_reset() {
  535. static int8_t last_extruder = -1;
  536. if (current_block->direction_bits != out_bits || current_block->active_extruder != last_extruder) {
  537. out_bits = current_block->direction_bits;
  538. last_extruder = current_block->active_extruder;
  539. set_stepper_direction();
  540. }
  541. #if ENABLED(ADVANCE)
  542. advance = current_block->initial_advance;
  543. final_advance = current_block->final_advance;
  544. // Do E steps + advance steps
  545. e_steps[current_block->active_extruder] += ((advance >>8) - old_advance);
  546. old_advance = advance >>8;
  547. #endif
  548. deceleration_time = 0;
  549. // step_rate to timer interval
  550. OCR1A_nominal = calc_timer(current_block->nominal_rate);
  551. // make a note of the number of step loops required at nominal speed
  552. step_loops_nominal = step_loops;
  553. acc_step_rate = current_block->initial_rate;
  554. acceleration_time = calc_timer(acc_step_rate);
  555. OCR1A = acceleration_time;
  556. // SERIAL_ECHO_START;
  557. // SERIAL_ECHOPGM("advance :");
  558. // SERIAL_ECHO(current_block->advance/256.0);
  559. // SERIAL_ECHOPGM("advance rate :");
  560. // SERIAL_ECHO(current_block->advance_rate/256.0);
  561. // SERIAL_ECHOPGM("initial advance :");
  562. // SERIAL_ECHO(current_block->initial_advance/256.0);
  563. // SERIAL_ECHOPGM("final advance :");
  564. // SERIAL_ECHOLN(current_block->final_advance/256.0);
  565. }
  566. // "The Stepper Driver Interrupt" - This timer interrupt is the workhorse.
  567. // It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
  568. ISR(TIMER1_COMPA_vect) {
  569. if (cleaning_buffer_counter) {
  570. current_block = NULL;
  571. plan_discard_current_block();
  572. #ifdef SD_FINISHED_RELEASECOMMAND
  573. if ((cleaning_buffer_counter == 1) && (SD_FINISHED_STEPPERRELEASE)) enqueue_and_echo_commands_P(PSTR(SD_FINISHED_RELEASECOMMAND));
  574. #endif
  575. cleaning_buffer_counter--;
  576. OCR1A = 200;
  577. return;
  578. }
  579. // If there is no current block, attempt to pop one from the buffer
  580. if (!current_block) {
  581. // Anything in the buffer?
  582. current_block = plan_get_current_block();
  583. if (current_block) {
  584. current_block->busy = true;
  585. trapezoid_generator_reset();
  586. counter_x = -(current_block->step_event_count >> 1);
  587. counter_y = counter_z = counter_e = counter_x;
  588. step_events_completed = 0;
  589. #if ENABLED(Z_LATE_ENABLE)
  590. if (current_block->steps[Z_AXIS] > 0) {
  591. enable_z();
  592. OCR1A = 2000; //1ms wait
  593. return;
  594. }
  595. #endif
  596. // #if ENABLED(ADVANCE)
  597. // e_steps[current_block->active_extruder] = 0;
  598. // #endif
  599. }
  600. else {
  601. OCR1A = 2000; // 1kHz.
  602. }
  603. }
  604. if (current_block != NULL) {
  605. // Update endstops state, if enabled
  606. #if ENABLED(HAS_Z_MIN_PROBE)
  607. if (check_endstops || z_probe_is_active) update_endstops();
  608. #else
  609. if (check_endstops) update_endstops();
  610. #endif
  611. // Take multiple steps per interrupt (For high speed moves)
  612. for (int8_t i = 0; i < step_loops; i++) {
  613. #ifndef USBCON
  614. customizedSerial.checkRx(); // Check for serial chars.
  615. #endif
  616. #if ENABLED(ADVANCE)
  617. counter_e += current_block->steps[E_AXIS];
  618. if (counter_e > 0) {
  619. counter_e -= current_block->step_event_count;
  620. e_steps[current_block->active_extruder] += TEST(out_bits, E_AXIS) ? -1 : 1;
  621. }
  622. #endif //ADVANCE
  623. #define _COUNTER(axis) counter_## axis
  624. #define _APPLY_STEP(AXIS) AXIS ##_APPLY_STEP
  625. #define _INVERT_STEP_PIN(AXIS) INVERT_## AXIS ##_STEP_PIN
  626. #define STEP_ADD(axis, AXIS) \
  627. _COUNTER(axis) += current_block->steps[_AXIS(AXIS)]; \
  628. if (_COUNTER(axis) > 0) { _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS),0); }
  629. STEP_ADD(x,X);
  630. STEP_ADD(y,Y);
  631. STEP_ADD(z,Z);
  632. #if DISABLED(ADVANCE)
  633. STEP_ADD(e,E);
  634. #endif
  635. #define STEP_IF_COUNTER(axis, AXIS) \
  636. if (_COUNTER(axis) > 0) { \
  637. _COUNTER(axis) -= current_block->step_event_count; \
  638. count_position[_AXIS(AXIS)] += count_direction[_AXIS(AXIS)]; \
  639. _APPLY_STEP(AXIS)(_INVERT_STEP_PIN(AXIS),0); \
  640. }
  641. STEP_IF_COUNTER(x, X);
  642. STEP_IF_COUNTER(y, Y);
  643. STEP_IF_COUNTER(z, Z);
  644. #if DISABLED(ADVANCE)
  645. STEP_IF_COUNTER(e, E);
  646. #endif
  647. step_events_completed++;
  648. if (step_events_completed >= current_block->step_event_count) break;
  649. }
  650. // Calculate new timer value
  651. unsigned short timer;
  652. unsigned short step_rate;
  653. if (step_events_completed <= (unsigned long)current_block->accelerate_until) {
  654. MultiU24X32toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate);
  655. acc_step_rate += current_block->initial_rate;
  656. // upper limit
  657. NOMORE(acc_step_rate, current_block->nominal_rate);
  658. // step_rate to timer interval
  659. timer = calc_timer(acc_step_rate);
  660. OCR1A = timer;
  661. acceleration_time += timer;
  662. #if ENABLED(ADVANCE)
  663. advance += advance_rate * step_loops;
  664. //NOLESS(advance, current_block->advance);
  665. // Do E steps + advance steps
  666. e_steps[current_block->active_extruder] += ((advance >> 8) - old_advance);
  667. old_advance = advance >> 8;
  668. #endif //ADVANCE
  669. }
  670. else if (step_events_completed > (unsigned long)current_block->decelerate_after) {
  671. MultiU24X32toH16(step_rate, deceleration_time, current_block->acceleration_rate);
  672. if (step_rate <= acc_step_rate) { // Still decelerating?
  673. step_rate = acc_step_rate - step_rate;
  674. NOLESS(step_rate, current_block->final_rate);
  675. }
  676. else
  677. step_rate = current_block->final_rate;
  678. // step_rate to timer interval
  679. timer = calc_timer(step_rate);
  680. OCR1A = timer;
  681. deceleration_time += timer;
  682. #if ENABLED(ADVANCE)
  683. advance -= advance_rate * step_loops;
  684. NOLESS(advance, final_advance);
  685. // Do E steps + advance steps
  686. uint32_t advance_whole = advance >> 8;
  687. e_steps[current_block->active_extruder] += advance_whole - old_advance;
  688. old_advance = advance_whole;
  689. #endif //ADVANCE
  690. }
  691. else {
  692. OCR1A = OCR1A_nominal;
  693. // ensure we're running at the correct step rate, even if we just came off an acceleration
  694. step_loops = step_loops_nominal;
  695. }
  696. OCR1A = (OCR1A < (TCNT1 + 16)) ? (TCNT1 + 16) : OCR1A;
  697. // If current block is finished, reset pointer
  698. if (step_events_completed >= current_block->step_event_count) {
  699. current_block = NULL;
  700. plan_discard_current_block();
  701. }
  702. }
  703. }
  704. #if ENABLED(ADVANCE)
  705. unsigned char old_OCR0A;
  706. // Timer interrupt for E. e_steps is set in the main routine;
  707. // Timer 0 is shared with millies
  708. ISR(TIMER0_COMPA_vect) {
  709. old_OCR0A += 52; // ~10kHz interrupt (250000 / 26 = 9615kHz)
  710. OCR0A = old_OCR0A;
  711. #define STEP_E_ONCE(INDEX) \
  712. if (e_steps[INDEX] != 0) { \
  713. E## INDEX ##_STEP_WRITE(INVERT_E_STEP_PIN); \
  714. if (e_steps[INDEX] < 0) { \
  715. E## INDEX ##_DIR_WRITE(INVERT_E## INDEX ##_DIR); \
  716. e_steps[INDEX]++; \
  717. } \
  718. else if (e_steps[INDEX] > 0) { \
  719. E## INDEX ##_DIR_WRITE(!INVERT_E## INDEX ##_DIR); \
  720. e_steps[INDEX]--; \
  721. } \
  722. E## INDEX ##_STEP_WRITE(!INVERT_E_STEP_PIN); \
  723. }
  724. // Step all E steppers that have steps, up to 4 steps per interrupt
  725. for (unsigned char i = 0; i < 4; i++) {
  726. STEP_E_ONCE(0);
  727. #if EXTRUDERS > 1
  728. STEP_E_ONCE(1);
  729. #if EXTRUDERS > 2
  730. STEP_E_ONCE(2);
  731. #if EXTRUDERS > 3
  732. STEP_E_ONCE(3);
  733. #endif
  734. #endif
  735. #endif
  736. }
  737. }
  738. #endif // ADVANCE
  739. void st_init() {
  740. digipot_init(); //Initialize Digipot Motor Current
  741. microstep_init(); //Initialize Microstepping Pins
  742. // initialise TMC Steppers
  743. #if ENABLED(HAVE_TMCDRIVER)
  744. tmc_init();
  745. #endif
  746. // initialise L6470 Steppers
  747. #if ENABLED(HAVE_L6470DRIVER)
  748. L6470_init();
  749. #endif
  750. // Initialize Dir Pins
  751. #if HAS_X_DIR
  752. X_DIR_INIT;
  753. #endif
  754. #if HAS_X2_DIR
  755. X2_DIR_INIT;
  756. #endif
  757. #if HAS_Y_DIR
  758. Y_DIR_INIT;
  759. #if ENABLED(Y_DUAL_STEPPER_DRIVERS) && HAS_Y2_DIR
  760. Y2_DIR_INIT;
  761. #endif
  762. #endif
  763. #if HAS_Z_DIR
  764. Z_DIR_INIT;
  765. #if ENABLED(Z_DUAL_STEPPER_DRIVERS) && HAS_Z2_DIR
  766. Z2_DIR_INIT;
  767. #endif
  768. #endif
  769. #if HAS_E0_DIR
  770. E0_DIR_INIT;
  771. #endif
  772. #if HAS_E1_DIR
  773. E1_DIR_INIT;
  774. #endif
  775. #if HAS_E2_DIR
  776. E2_DIR_INIT;
  777. #endif
  778. #if HAS_E3_DIR
  779. E3_DIR_INIT;
  780. #endif
  781. //Initialize Enable Pins - steppers default to disabled.
  782. #if HAS_X_ENABLE
  783. X_ENABLE_INIT;
  784. if (!X_ENABLE_ON) X_ENABLE_WRITE(HIGH);
  785. #endif
  786. #if HAS_X2_ENABLE
  787. X2_ENABLE_INIT;
  788. if (!X_ENABLE_ON) X2_ENABLE_WRITE(HIGH);
  789. #endif
  790. #if HAS_Y_ENABLE
  791. Y_ENABLE_INIT;
  792. if (!Y_ENABLE_ON) Y_ENABLE_WRITE(HIGH);
  793. #if ENABLED(Y_DUAL_STEPPER_DRIVERS) && HAS_Y2_ENABLE
  794. Y2_ENABLE_INIT;
  795. if (!Y_ENABLE_ON) Y2_ENABLE_WRITE(HIGH);
  796. #endif
  797. #endif
  798. #if HAS_Z_ENABLE
  799. Z_ENABLE_INIT;
  800. if (!Z_ENABLE_ON) Z_ENABLE_WRITE(HIGH);
  801. #if ENABLED(Z_DUAL_STEPPER_DRIVERS) && HAS_Z2_ENABLE
  802. Z2_ENABLE_INIT;
  803. if (!Z_ENABLE_ON) Z2_ENABLE_WRITE(HIGH);
  804. #endif
  805. #endif
  806. #if HAS_E0_ENABLE
  807. E0_ENABLE_INIT;
  808. if (!E_ENABLE_ON) E0_ENABLE_WRITE(HIGH);
  809. #endif
  810. #if HAS_E1_ENABLE
  811. E1_ENABLE_INIT;
  812. if (!E_ENABLE_ON) E1_ENABLE_WRITE(HIGH);
  813. #endif
  814. #if HAS_E2_ENABLE
  815. E2_ENABLE_INIT;
  816. if (!E_ENABLE_ON) E2_ENABLE_WRITE(HIGH);
  817. #endif
  818. #if HAS_E3_ENABLE
  819. E3_ENABLE_INIT;
  820. if (!E_ENABLE_ON) E3_ENABLE_WRITE(HIGH);
  821. #endif
  822. //endstops and pullups
  823. #if HAS_X_MIN
  824. SET_INPUT(X_MIN_PIN);
  825. #if ENABLED(ENDSTOPPULLUP_XMIN)
  826. WRITE(X_MIN_PIN,HIGH);
  827. #endif
  828. #endif
  829. #if HAS_Y_MIN
  830. SET_INPUT(Y_MIN_PIN);
  831. #if ENABLED(ENDSTOPPULLUP_YMIN)
  832. WRITE(Y_MIN_PIN,HIGH);
  833. #endif
  834. #endif
  835. #if HAS_Z_MIN
  836. SET_INPUT(Z_MIN_PIN);
  837. #if ENABLED(ENDSTOPPULLUP_ZMIN)
  838. WRITE(Z_MIN_PIN,HIGH);
  839. #endif
  840. #endif
  841. #if HAS_Z2_MIN
  842. SET_INPUT(Z2_MIN_PIN);
  843. #if ENABLED(ENDSTOPPULLUP_ZMIN)
  844. WRITE(Z2_MIN_PIN,HIGH);
  845. #endif
  846. #endif
  847. #if HAS_X_MAX
  848. SET_INPUT(X_MAX_PIN);
  849. #if ENABLED(ENDSTOPPULLUP_XMAX)
  850. WRITE(X_MAX_PIN,HIGH);
  851. #endif
  852. #endif
  853. #if HAS_Y_MAX
  854. SET_INPUT(Y_MAX_PIN);
  855. #if ENABLED(ENDSTOPPULLUP_YMAX)
  856. WRITE(Y_MAX_PIN,HIGH);
  857. #endif
  858. #endif
  859. #if HAS_Z_MAX
  860. SET_INPUT(Z_MAX_PIN);
  861. #if ENABLED(ENDSTOPPULLUP_ZMAX)
  862. WRITE(Z_MAX_PIN,HIGH);
  863. #endif
  864. #endif
  865. #if HAS_Z2_MAX
  866. SET_INPUT(Z2_MAX_PIN);
  867. #if ENABLED(ENDSTOPPULLUP_ZMAX)
  868. WRITE(Z2_MAX_PIN,HIGH);
  869. #endif
  870. #endif
  871. #if HAS_Z_PROBE && ENABLED(Z_MIN_PROBE_ENDSTOP) // Check for Z_MIN_PROBE_ENDSTOP so we don't pull a pin high unless it's to be used.
  872. SET_INPUT(Z_MIN_PROBE_PIN);
  873. #if ENABLED(ENDSTOPPULLUP_ZMIN_PROBE)
  874. WRITE(Z_MIN_PROBE_PIN,HIGH);
  875. #endif
  876. #endif
  877. #define _STEP_INIT(AXIS) AXIS ##_STEP_INIT
  878. #define _WRITE_STEP(AXIS, HIGHLOW) AXIS ##_STEP_WRITE(HIGHLOW)
  879. #define _DISABLE(axis) disable_## axis()
  880. #define AXIS_INIT(axis, AXIS, PIN) \
  881. _STEP_INIT(AXIS); \
  882. _WRITE_STEP(AXIS, _INVERT_STEP_PIN(PIN)); \
  883. _DISABLE(axis)
  884. #define E_AXIS_INIT(NUM) AXIS_INIT(e## NUM, E## NUM, E)
  885. // Initialize Step Pins
  886. #if HAS_X_STEP
  887. AXIS_INIT(x, X, X);
  888. #endif
  889. #if HAS_X2_STEP
  890. AXIS_INIT(x, X2, X);
  891. #endif
  892. #if HAS_Y_STEP
  893. #if ENABLED(Y_DUAL_STEPPER_DRIVERS) && HAS_Y2_STEP
  894. Y2_STEP_INIT;
  895. Y2_STEP_WRITE(INVERT_Y_STEP_PIN);
  896. #endif
  897. AXIS_INIT(y, Y, Y);
  898. #endif
  899. #if HAS_Z_STEP
  900. #if ENABLED(Z_DUAL_STEPPER_DRIVERS) && HAS_Z2_STEP
  901. Z2_STEP_INIT;
  902. Z2_STEP_WRITE(INVERT_Z_STEP_PIN);
  903. #endif
  904. AXIS_INIT(z, Z, Z);
  905. #endif
  906. #if HAS_E0_STEP
  907. E_AXIS_INIT(0);
  908. #endif
  909. #if HAS_E1_STEP
  910. E_AXIS_INIT(1);
  911. #endif
  912. #if HAS_E2_STEP
  913. E_AXIS_INIT(2);
  914. #endif
  915. #if HAS_E3_STEP
  916. E_AXIS_INIT(3);
  917. #endif
  918. // waveform generation = 0100 = CTC
  919. CBI(TCCR1B, WGM13);
  920. SBI(TCCR1B, WGM12);
  921. CBI(TCCR1A, WGM11);
  922. CBI(TCCR1A, WGM10);
  923. // output mode = 00 (disconnected)
  924. TCCR1A &= ~(3 << COM1A0);
  925. TCCR1A &= ~(3 << COM1B0);
  926. // Set the timer pre-scaler
  927. // Generally we use a divider of 8, resulting in a 2MHz timer
  928. // frequency on a 16MHz MCU. If you are going to change this, be
  929. // sure to regenerate speed_lookuptable.h with
  930. // create_speed_lookuptable.py
  931. TCCR1B = (TCCR1B & ~(0x07 << CS10)) | (2 << CS10);
  932. OCR1A = 0x4000;
  933. TCNT1 = 0;
  934. ENABLE_STEPPER_DRIVER_INTERRUPT();
  935. #if ENABLED(ADVANCE)
  936. #if defined(TCCR0A) && defined(WGM01)
  937. CBI(TCCR0A, WGM01);
  938. CBI(TCCR0A, WGM00);
  939. #endif
  940. e_steps[0] = e_steps[1] = e_steps[2] = e_steps[3] = 0;
  941. SBI(TIMSK0, OCIE0A);
  942. #endif //ADVANCE
  943. enable_endstops(true); // Start with endstops active. After homing they can be disabled
  944. sei();
  945. set_stepper_direction(); // Init directions to out_bits = 0
  946. }
  947. /**
  948. * Block until all buffered steps are executed
  949. */
  950. void st_synchronize() { while (blocks_queued()) idle(); }
  951. void st_set_position(const long& x, const long& y, const long& z, const long& e) {
  952. CRITICAL_SECTION_START;
  953. count_position[X_AXIS] = x;
  954. count_position[Y_AXIS] = y;
  955. count_position[Z_AXIS] = z;
  956. count_position[E_AXIS] = e;
  957. CRITICAL_SECTION_END;
  958. }
  959. void st_set_e_position(const long& e) {
  960. CRITICAL_SECTION_START;
  961. count_position[E_AXIS] = e;
  962. CRITICAL_SECTION_END;
  963. }
  964. long st_get_position(uint8_t axis) {
  965. CRITICAL_SECTION_START;
  966. long count_pos = count_position[axis];
  967. CRITICAL_SECTION_END;
  968. return count_pos;
  969. }
  970. float st_get_axis_position_mm(AxisEnum axis) {
  971. float axis_pos;
  972. #if ENABLED(COREXY) | ENABLED(COREXZ)
  973. if (axis == X_AXIS || axis == CORE_AXIS_2) {
  974. CRITICAL_SECTION_START;
  975. long pos1 = count_position[A_AXIS],
  976. pos2 = count_position[CORE_AXIS_2];
  977. CRITICAL_SECTION_END;
  978. // ((a1+a2)+(a1-a2))/2 -> (a1+a2+a1-a2)/2 -> (a1+a1)/2 -> a1
  979. // ((a1+a2)-(a1-a2))/2 -> (a1+a2-a1+a2)/2 -> (a2+a2)/2 -> a2
  980. axis_pos = (pos1 + ((axis == X_AXIS) ? pos2 : -pos2)) / 2.0f;
  981. }
  982. else
  983. axis_pos = st_get_position(axis);
  984. #else
  985. axis_pos = st_get_position(axis);
  986. #endif
  987. return axis_pos / axis_steps_per_unit[axis];
  988. }
  989. void finishAndDisableSteppers() {
  990. st_synchronize();
  991. disable_all_steppers();
  992. }
  993. void quickStop() {
  994. cleaning_buffer_counter = 5000;
  995. DISABLE_STEPPER_DRIVER_INTERRUPT();
  996. while (blocks_queued()) plan_discard_current_block();
  997. current_block = NULL;
  998. ENABLE_STEPPER_DRIVER_INTERRUPT();
  999. }
  1000. #if ENABLED(BABYSTEPPING)
  1001. // MUST ONLY BE CALLED BY AN ISR,
  1002. // No other ISR should ever interrupt this!
  1003. void babystep(const uint8_t axis, const bool direction) {
  1004. #define _ENABLE(axis) enable_## axis()
  1005. #define _READ_DIR(AXIS) AXIS ##_DIR_READ
  1006. #define _INVERT_DIR(AXIS) INVERT_## AXIS ##_DIR
  1007. #define _APPLY_DIR(AXIS, INVERT) AXIS ##_APPLY_DIR(INVERT, true)
  1008. #define BABYSTEP_AXIS(axis, AXIS, INVERT) { \
  1009. _ENABLE(axis); \
  1010. uint8_t old_pin = _READ_DIR(AXIS); \
  1011. _APPLY_DIR(AXIS, _INVERT_DIR(AXIS)^direction^INVERT); \
  1012. _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS), true); \
  1013. delayMicroseconds(2); \
  1014. _APPLY_STEP(AXIS)(_INVERT_STEP_PIN(AXIS), true); \
  1015. _APPLY_DIR(AXIS, old_pin); \
  1016. }
  1017. switch (axis) {
  1018. case X_AXIS:
  1019. BABYSTEP_AXIS(x, X, false);
  1020. break;
  1021. case Y_AXIS:
  1022. BABYSTEP_AXIS(y, Y, false);
  1023. break;
  1024. case Z_AXIS: {
  1025. #if DISABLED(DELTA)
  1026. BABYSTEP_AXIS(z, Z, BABYSTEP_INVERT_Z);
  1027. #else // DELTA
  1028. bool z_direction = direction ^ BABYSTEP_INVERT_Z;
  1029. enable_x();
  1030. enable_y();
  1031. enable_z();
  1032. uint8_t old_x_dir_pin = X_DIR_READ,
  1033. old_y_dir_pin = Y_DIR_READ,
  1034. old_z_dir_pin = Z_DIR_READ;
  1035. //setup new step
  1036. X_DIR_WRITE(INVERT_X_DIR ^ z_direction);
  1037. Y_DIR_WRITE(INVERT_Y_DIR ^ z_direction);
  1038. Z_DIR_WRITE(INVERT_Z_DIR ^ z_direction);
  1039. //perform step
  1040. X_STEP_WRITE(!INVERT_X_STEP_PIN);
  1041. Y_STEP_WRITE(!INVERT_Y_STEP_PIN);
  1042. Z_STEP_WRITE(!INVERT_Z_STEP_PIN);
  1043. delayMicroseconds(2);
  1044. X_STEP_WRITE(INVERT_X_STEP_PIN);
  1045. Y_STEP_WRITE(INVERT_Y_STEP_PIN);
  1046. Z_STEP_WRITE(INVERT_Z_STEP_PIN);
  1047. //get old pin state back.
  1048. X_DIR_WRITE(old_x_dir_pin);
  1049. Y_DIR_WRITE(old_y_dir_pin);
  1050. Z_DIR_WRITE(old_z_dir_pin);
  1051. #endif
  1052. } break;
  1053. default: break;
  1054. }
  1055. }
  1056. #endif //BABYSTEPPING
  1057. #if HAS_DIGIPOTSS
  1058. // From Arduino DigitalPotControl example
  1059. void digitalPotWrite(int address, int value) {
  1060. digitalWrite(DIGIPOTSS_PIN, LOW); // take the SS pin low to select the chip
  1061. SPI.transfer(address); // send in the address and value via SPI:
  1062. SPI.transfer(value);
  1063. digitalWrite(DIGIPOTSS_PIN, HIGH); // take the SS pin high to de-select the chip:
  1064. //delay(10);
  1065. }
  1066. #endif //HAS_DIGIPOTSS
  1067. // Initialize Digipot Motor Current
  1068. void digipot_init() {
  1069. #if HAS_DIGIPOTSS
  1070. const uint8_t digipot_motor_current[] = DIGIPOT_MOTOR_CURRENT;
  1071. SPI.begin();
  1072. pinMode(DIGIPOTSS_PIN, OUTPUT);
  1073. for (int i = 0; i < COUNT(digipot_motor_current); i++) {
  1074. //digitalPotWrite(digipot_ch[i], digipot_motor_current[i]);
  1075. digipot_current(i, digipot_motor_current[i]);
  1076. }
  1077. #endif
  1078. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  1079. pinMode(MOTOR_CURRENT_PWM_XY_PIN, OUTPUT);
  1080. pinMode(MOTOR_CURRENT_PWM_Z_PIN, OUTPUT);
  1081. pinMode(MOTOR_CURRENT_PWM_E_PIN, OUTPUT);
  1082. digipot_current(0, motor_current_setting[0]);
  1083. digipot_current(1, motor_current_setting[1]);
  1084. digipot_current(2, motor_current_setting[2]);
  1085. //Set timer5 to 31khz so the PWM of the motor power is as constant as possible. (removes a buzzing noise)
  1086. TCCR5B = (TCCR5B & ~(_BV(CS50) | _BV(CS51) | _BV(CS52))) | _BV(CS50);
  1087. #endif
  1088. }
  1089. void digipot_current(uint8_t driver, int current) {
  1090. #if HAS_DIGIPOTSS
  1091. const uint8_t digipot_ch[] = DIGIPOT_CHANNELS;
  1092. digitalPotWrite(digipot_ch[driver], current);
  1093. #elif defined(MOTOR_CURRENT_PWM_XY_PIN)
  1094. switch (driver) {
  1095. case 0: analogWrite(MOTOR_CURRENT_PWM_XY_PIN, 255L * current / (MOTOR_CURRENT_PWM_RANGE)); break;
  1096. case 1: analogWrite(MOTOR_CURRENT_PWM_Z_PIN, 255L * current / (MOTOR_CURRENT_PWM_RANGE)); break;
  1097. case 2: analogWrite(MOTOR_CURRENT_PWM_E_PIN, 255L * current / (MOTOR_CURRENT_PWM_RANGE)); break;
  1098. }
  1099. #else
  1100. UNUSED(driver);
  1101. UNUSED(current);
  1102. #endif
  1103. }
  1104. void microstep_init() {
  1105. #if HAS_MICROSTEPS_E1
  1106. pinMode(E1_MS1_PIN, OUTPUT);
  1107. pinMode(E1_MS2_PIN, OUTPUT);
  1108. #endif
  1109. #if HAS_MICROSTEPS
  1110. pinMode(X_MS1_PIN, OUTPUT);
  1111. pinMode(X_MS2_PIN, OUTPUT);
  1112. pinMode(Y_MS1_PIN, OUTPUT);
  1113. pinMode(Y_MS2_PIN, OUTPUT);
  1114. pinMode(Z_MS1_PIN, OUTPUT);
  1115. pinMode(Z_MS2_PIN, OUTPUT);
  1116. pinMode(E0_MS1_PIN, OUTPUT);
  1117. pinMode(E0_MS2_PIN, OUTPUT);
  1118. const uint8_t microstep_modes[] = MICROSTEP_MODES;
  1119. for (uint16_t i = 0; i < COUNT(microstep_modes); i++)
  1120. microstep_mode(i, microstep_modes[i]);
  1121. #endif
  1122. }
  1123. void microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2) {
  1124. if (ms1 >= 0) switch (driver) {
  1125. case 0: digitalWrite(X_MS1_PIN, ms1); break;
  1126. case 1: digitalWrite(Y_MS1_PIN, ms1); break;
  1127. case 2: digitalWrite(Z_MS1_PIN, ms1); break;
  1128. case 3: digitalWrite(E0_MS1_PIN, ms1); break;
  1129. #if HAS_MICROSTEPS_E1
  1130. case 4: digitalWrite(E1_MS1_PIN, ms1); break;
  1131. #endif
  1132. }
  1133. if (ms2 >= 0) switch (driver) {
  1134. case 0: digitalWrite(X_MS2_PIN, ms2); break;
  1135. case 1: digitalWrite(Y_MS2_PIN, ms2); break;
  1136. case 2: digitalWrite(Z_MS2_PIN, ms2); break;
  1137. case 3: digitalWrite(E0_MS2_PIN, ms2); break;
  1138. #if PIN_EXISTS(E1_MS2)
  1139. case 4: digitalWrite(E1_MS2_PIN, ms2); break;
  1140. #endif
  1141. }
  1142. }
  1143. void microstep_mode(uint8_t driver, uint8_t stepping_mode) {
  1144. switch (stepping_mode) {
  1145. case 1: microstep_ms(driver, MICROSTEP1); break;
  1146. case 2: microstep_ms(driver, MICROSTEP2); break;
  1147. case 4: microstep_ms(driver, MICROSTEP4); break;
  1148. case 8: microstep_ms(driver, MICROSTEP8); break;
  1149. case 16: microstep_ms(driver, MICROSTEP16); break;
  1150. }
  1151. }
  1152. void microstep_readings() {
  1153. SERIAL_PROTOCOLPGM("MS1,MS2 Pins\n");
  1154. SERIAL_PROTOCOLPGM("X: ");
  1155. SERIAL_PROTOCOL(digitalRead(X_MS1_PIN));
  1156. SERIAL_PROTOCOLLN(digitalRead(X_MS2_PIN));
  1157. SERIAL_PROTOCOLPGM("Y: ");
  1158. SERIAL_PROTOCOL(digitalRead(Y_MS1_PIN));
  1159. SERIAL_PROTOCOLLN(digitalRead(Y_MS2_PIN));
  1160. SERIAL_PROTOCOLPGM("Z: ");
  1161. SERIAL_PROTOCOL(digitalRead(Z_MS1_PIN));
  1162. SERIAL_PROTOCOLLN(digitalRead(Z_MS2_PIN));
  1163. SERIAL_PROTOCOLPGM("E0: ");
  1164. SERIAL_PROTOCOL(digitalRead(E0_MS1_PIN));
  1165. SERIAL_PROTOCOLLN(digitalRead(E0_MS2_PIN));
  1166. #if HAS_MICROSTEPS_E1
  1167. SERIAL_PROTOCOLPGM("E1: ");
  1168. SERIAL_PROTOCOL(digitalRead(E1_MS1_PIN));
  1169. SERIAL_PROTOCOLLN(digitalRead(E1_MS2_PIN));
  1170. #endif
  1171. }
  1172. #if ENABLED(Z_DUAL_ENDSTOPS)
  1173. void In_Homing_Process(bool state) { performing_homing = state; }
  1174. void Lock_z_motor(bool state) { locked_z_motor = state; }
  1175. void Lock_z2_motor(bool state) { locked_z2_motor = state; }
  1176. #endif