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

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