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

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