stepper.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  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 defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  27. #include <SPI.h>
  28. #endif
  29. #ifdef TMC2130
  30. #include "tmc2130.h"
  31. #endif //TMC2130
  32. #ifdef PAT9125
  33. #include "fsensor.h"
  34. int fsensor_counter = 0; //counter for e-steps
  35. #endif //PAT9125
  36. #ifdef DEBUG_STACK_MONITOR
  37. uint16_t SP_min = 0x21FF;
  38. #endif //DEBUG_STACK_MONITOR
  39. //===========================================================================
  40. //=============================public variables ============================
  41. //===========================================================================
  42. block_t *current_block; // A pointer to the block currently being traced
  43. bool x_min_endstop = false;
  44. bool x_max_endstop = false;
  45. bool y_min_endstop = false;
  46. bool y_max_endstop = false;
  47. bool z_min_endstop = false;
  48. bool z_max_endstop = false;
  49. //===========================================================================
  50. //=============================private variables ============================
  51. //===========================================================================
  52. //static makes it inpossible to be called from outside of this file by extern.!
  53. // Variables used by The Stepper Driver Interrupt
  54. static unsigned char out_bits; // The next stepping-bits to be output
  55. static int32_t counter_x, // Counter variables for the bresenham line tracer
  56. counter_y,
  57. counter_z,
  58. counter_e;
  59. volatile uint32_t step_events_completed; // The number of step events executed in the current block
  60. static int32_t acceleration_time, deceleration_time;
  61. //static unsigned long accelerate_until, decelerate_after, acceleration_rate, initial_rate, final_rate, nominal_rate;
  62. static uint16_t acc_step_rate; // needed for deccelaration start point
  63. static uint8_t step_loops;
  64. static uint16_t OCR1A_nominal;
  65. static uint8_t step_loops_nominal;
  66. volatile long endstops_trigsteps[3]={0,0,0};
  67. volatile long endstops_stepsTotal,endstops_stepsDone;
  68. static volatile bool endstop_x_hit=false;
  69. static volatile bool endstop_y_hit=false;
  70. static volatile bool endstop_z_hit=false;
  71. #ifdef ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED
  72. bool abort_on_endstop_hit = false;
  73. #endif
  74. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  75. int motor_current_setting[3] = DEFAULT_PWM_MOTOR_CURRENT;
  76. int motor_current_setting_silent[3] = DEFAULT_PWM_MOTOR_CURRENT;
  77. int motor_current_setting_loud[3] = DEFAULT_PWM_MOTOR_CURRENT_LOUD;
  78. #endif
  79. static bool old_x_min_endstop=false;
  80. static bool old_x_max_endstop=false;
  81. static bool old_y_min_endstop=false;
  82. static bool old_y_max_endstop=false;
  83. static bool old_z_min_endstop=false;
  84. static bool old_z_max_endstop=false;
  85. static bool check_endstops = true;
  86. static bool check_z_endstop = false;
  87. static bool z_endstop_invert = false;
  88. int8_t SilentMode = 0;
  89. volatile long count_position[NUM_AXIS] = { 0, 0, 0, 0};
  90. volatile signed char count_direction[NUM_AXIS] = { 1, 1, 1, 1};
  91. uint8_t LastStepMask = 0;
  92. #ifdef LIN_ADVANCE
  93. uint16_t ADV_NEVER = 65535;
  94. static uint16_t nextMainISR = 0;
  95. static uint16_t nextAdvanceISR = ADV_NEVER;
  96. static uint16_t eISR_Rate = ADV_NEVER;
  97. static volatile int e_steps; //Extrusion steps to be executed by the stepper
  98. static int final_estep_rate; //Speed of extruder at cruising speed
  99. static int current_estep_rate; //The current speed of the extruder
  100. static int current_adv_steps; //The current pretension of filament expressed in steps
  101. #define ADV_RATE(T, L) (e_steps ? (T) * (L) / abs(e_steps) : ADV_NEVER)
  102. #define _NEXT_ISR(T) nextMainISR = T
  103. #else
  104. #define _NEXT_ISR(T) OCR1A = T
  105. #endif
  106. #ifdef DEBUG_STEPPER_TIMER_MISSED
  107. extern bool stepper_timer_overflow_state;
  108. #endif /* DEBUG_STEPPER_TIMER_MISSED */
  109. //===========================================================================
  110. //=============================functions ============================
  111. //===========================================================================
  112. #define CHECK_ENDSTOPS if(check_endstops)
  113. #ifndef _NO_ASM
  114. // intRes = intIn1 * intIn2 >> 16
  115. // uses:
  116. // r26 to store 0
  117. // r27 to store the byte 1 of the 24 bit result
  118. #define MultiU16X8toH16(intRes, charIn1, intIn2) \
  119. asm volatile ( \
  120. "clr r26 \n\t" \
  121. "mul %A1, %B2 \n\t" \
  122. "movw %A0, r0 \n\t" \
  123. "mul %A1, %A2 \n\t" \
  124. "add %A0, r1 \n\t" \
  125. "adc %B0, r26 \n\t" \
  126. "lsr r0 \n\t" \
  127. "adc %A0, r26 \n\t" \
  128. "adc %B0, r26 \n\t" \
  129. "clr r1 \n\t" \
  130. : \
  131. "=&r" (intRes) \
  132. : \
  133. "d" (charIn1), \
  134. "d" (intIn2) \
  135. : \
  136. "r26" \
  137. )
  138. // intRes = longIn1 * longIn2 >> 24
  139. // uses:
  140. // r26 to store 0
  141. // r27 to store the byte 1 of the 48bit result
  142. #define MultiU24X24toH16(intRes, longIn1, longIn2) \
  143. asm volatile ( \
  144. "clr r26 \n\t" \
  145. "mul %A1, %B2 \n\t" \
  146. "mov r27, r1 \n\t" \
  147. "mul %B1, %C2 \n\t" \
  148. "movw %A0, r0 \n\t" \
  149. "mul %C1, %C2 \n\t" \
  150. "add %B0, r0 \n\t" \
  151. "mul %C1, %B2 \n\t" \
  152. "add %A0, r0 \n\t" \
  153. "adc %B0, r1 \n\t" \
  154. "mul %A1, %C2 \n\t" \
  155. "add r27, r0 \n\t" \
  156. "adc %A0, r1 \n\t" \
  157. "adc %B0, r26 \n\t" \
  158. "mul %B1, %B2 \n\t" \
  159. "add r27, r0 \n\t" \
  160. "adc %A0, r1 \n\t" \
  161. "adc %B0, r26 \n\t" \
  162. "mul %C1, %A2 \n\t" \
  163. "add r27, r0 \n\t" \
  164. "adc %A0, r1 \n\t" \
  165. "adc %B0, r26 \n\t" \
  166. "mul %B1, %A2 \n\t" \
  167. "add r27, r1 \n\t" \
  168. "adc %A0, r26 \n\t" \
  169. "adc %B0, r26 \n\t" \
  170. "lsr r27 \n\t" \
  171. "adc %A0, r26 \n\t" \
  172. "adc %B0, r26 \n\t" \
  173. "clr r1 \n\t" \
  174. : \
  175. "=&r" (intRes) \
  176. : \
  177. "d" (longIn1), \
  178. "d" (longIn2) \
  179. : \
  180. "r26" , "r27" \
  181. )
  182. #else //_NO_ASM
  183. void MultiU16X8toH16(unsigned short& intRes, unsigned char& charIn1, unsigned short& intIn2)
  184. {
  185. }
  186. void MultiU24X24toH16(uint16_t& intRes, int32_t& longIn1, long& longIn2)
  187. {
  188. }
  189. #endif //_NO_ASM
  190. // Some useful constants
  191. void checkHitEndstops()
  192. {
  193. if( endstop_x_hit || endstop_y_hit || endstop_z_hit) {
  194. SERIAL_ECHO_START;
  195. SERIAL_ECHORPGM(MSG_ENDSTOPS_HIT);
  196. if(endstop_x_hit) {
  197. SERIAL_ECHOPAIR(" X:",(float)endstops_trigsteps[X_AXIS]/axis_steps_per_unit[X_AXIS]);
  198. LCD_MESSAGERPGM(CAT2(MSG_ENDSTOPS_HIT, PSTR("X")));
  199. }
  200. if(endstop_y_hit) {
  201. SERIAL_ECHOPAIR(" Y:",(float)endstops_trigsteps[Y_AXIS]/axis_steps_per_unit[Y_AXIS]);
  202. LCD_MESSAGERPGM(CAT2(MSG_ENDSTOPS_HIT, PSTR("Y")));
  203. }
  204. if(endstop_z_hit) {
  205. SERIAL_ECHOPAIR(" Z:",(float)endstops_trigsteps[Z_AXIS]/axis_steps_per_unit[Z_AXIS]);
  206. LCD_MESSAGERPGM(CAT2(MSG_ENDSTOPS_HIT,PSTR("Z")));
  207. }
  208. SERIAL_ECHOLN("");
  209. endstop_x_hit=false;
  210. endstop_y_hit=false;
  211. endstop_z_hit=false;
  212. #if defined(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED) && defined(SDSUPPORT)
  213. if (abort_on_endstop_hit)
  214. {
  215. card.sdprinting = false;
  216. card.closefile();
  217. quickStop();
  218. setTargetHotend0(0);
  219. setTargetHotend1(0);
  220. setTargetHotend2(0);
  221. }
  222. #endif
  223. }
  224. }
  225. bool endstops_hit_on_purpose()
  226. {
  227. bool hit = endstop_x_hit || endstop_y_hit || endstop_z_hit;
  228. endstop_x_hit=false;
  229. endstop_y_hit=false;
  230. endstop_z_hit=false;
  231. return hit;
  232. }
  233. bool endstop_z_hit_on_purpose()
  234. {
  235. bool hit = endstop_z_hit;
  236. endstop_z_hit=false;
  237. return hit;
  238. }
  239. bool enable_endstops(bool check)
  240. {
  241. bool old = check_endstops;
  242. check_endstops = check;
  243. return old;
  244. }
  245. bool enable_z_endstop(bool check)
  246. {
  247. bool old = check_z_endstop;
  248. check_z_endstop = check;
  249. endstop_z_hit = false;
  250. return old;
  251. }
  252. void invert_z_endstop(bool endstop_invert)
  253. {
  254. z_endstop_invert = endstop_invert;
  255. }
  256. // __________________________
  257. // /| |\ _________________ ^
  258. // / | | \ /| |\ |
  259. // / | | \ / | | \ s
  260. // / | | | | | \ p
  261. // / | | | | | \ e
  262. // +-----+------------------------+---+--+---------------+----+ e
  263. // | BLOCK 1 | BLOCK 2 | d
  264. //
  265. // time ----->
  266. //
  267. // The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates
  268. // first block->accelerate_until step_events_completed, then keeps going at constant speed until
  269. // step_events_completed reaches block->decelerate_after after which it decelerates until the trapezoid generator is reset.
  270. // The slope of acceleration is calculated with the leib ramp alghorithm.
  271. FORCE_INLINE unsigned short calc_timer(unsigned short step_rate) {
  272. unsigned short timer;
  273. if(step_rate > MAX_STEP_FREQUENCY) step_rate = MAX_STEP_FREQUENCY;
  274. if(step_rate > 20000) { // If steprate > 20kHz >> step 4 times
  275. step_rate = (step_rate >> 2)&0x3fff;
  276. step_loops = 4;
  277. }
  278. else if(step_rate > 10000) { // If steprate > 10kHz >> step 2 times
  279. step_rate = (step_rate >> 1)&0x7fff;
  280. step_loops = 2;
  281. }
  282. else {
  283. step_loops = 1;
  284. }
  285. // step_loops = 1;
  286. if(step_rate < (F_CPU/500000)) step_rate = (F_CPU/500000);
  287. step_rate -= (F_CPU/500000); // Correct for minimal speed
  288. if(step_rate >= (8*256)){ // higher step rate
  289. unsigned short table_address = (unsigned short)&speed_lookuptable_fast[(unsigned char)(step_rate>>8)][0];
  290. unsigned char tmp_step_rate = (step_rate & 0x00ff);
  291. unsigned short gain = (unsigned short)pgm_read_word_near(table_address+2);
  292. MultiU16X8toH16(timer, tmp_step_rate, gain);
  293. timer = (unsigned short)pgm_read_word_near(table_address) - timer;
  294. }
  295. else { // lower step rates
  296. unsigned short table_address = (unsigned short)&speed_lookuptable_slow[0][0];
  297. table_address += ((step_rate)>>1) & 0xfffc;
  298. timer = (unsigned short)pgm_read_word_near(table_address);
  299. timer -= (((unsigned short)pgm_read_word_near(table_address+2) * (unsigned char)(step_rate & 0x0007))>>3);
  300. }
  301. if(timer < 100) { timer = 100; MYSERIAL.print(MSG_STEPPER_TOO_HIGH); MYSERIAL.println(step_rate); }//(20kHz this should never happen)
  302. return timer;
  303. }
  304. // Initializes the trapezoid generator from the current block. Called whenever a new
  305. // block begins.
  306. FORCE_INLINE void trapezoid_generator_reset() {
  307. deceleration_time = 0;
  308. // step_rate to timer interval
  309. OCR1A_nominal = calc_timer(current_block->nominal_rate);
  310. // make a note of the number of step loops required at nominal speed
  311. step_loops_nominal = step_loops;
  312. acc_step_rate = current_block->initial_rate;
  313. acceleration_time = calc_timer(acc_step_rate);
  314. _NEXT_ISR(acceleration_time);
  315. #ifdef LIN_ADVANCE
  316. if (current_block->use_advance_lead) {
  317. current_estep_rate = ((unsigned long)acc_step_rate * current_block->abs_adv_steps_multiplier8) >> 17;
  318. final_estep_rate = (current_block->nominal_rate * current_block->abs_adv_steps_multiplier8) >> 17;
  319. }
  320. #endif
  321. }
  322. // "The Stepper Driver Interrupt" - This timer interrupt is the workhorse.
  323. // It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
  324. ISR(TIMER1_COMPA_vect) {
  325. #ifdef DEBUG_STACK_MONITOR
  326. uint16_t sp = SPL + 256 * SPH;
  327. if (sp < SP_min) SP_min = sp;
  328. #endif //DEBUG_STACK_MONITOR
  329. #ifdef LIN_ADVANCE
  330. advance_isr_scheduler();
  331. #else
  332. isr();
  333. #endif
  334. }
  335. void isr() {
  336. //if (UVLO) uvlo();
  337. // If there is no current block, attempt to pop one from the buffer
  338. if (current_block == NULL) {
  339. // Anything in the buffer?
  340. current_block = plan_get_current_block();
  341. if (current_block != NULL) {
  342. #ifdef PAT9125
  343. fsensor_counter = 0;
  344. fsensor_st_block_begin(current_block);
  345. #endif //PAT9125
  346. // The busy flag is set by the plan_get_current_block() call.
  347. // current_block->busy = true;
  348. trapezoid_generator_reset();
  349. counter_x = -(current_block->step_event_count >> 1);
  350. counter_y = counter_x;
  351. counter_z = counter_x;
  352. counter_e = counter_x;
  353. step_events_completed = 0;
  354. #ifdef Z_LATE_ENABLE
  355. if(current_block->steps_z > 0) {
  356. enable_z();
  357. _NEXT_ISR(2000); //1ms wait
  358. return;
  359. }
  360. #endif
  361. }
  362. else {
  363. _NEXT_ISR(2000); // 1kHz.
  364. }
  365. }
  366. LastStepMask = 0;
  367. if (current_block != NULL) {
  368. // Set directions TO DO This should be done once during init of trapezoid. Endstops -> interrupt
  369. out_bits = current_block->direction_bits;
  370. // Set the direction bits (X_AXIS=A_AXIS and Y_AXIS=B_AXIS for COREXY)
  371. if((out_bits & (1<<X_AXIS))!=0){
  372. WRITE_NC(X_DIR_PIN, INVERT_X_DIR);
  373. count_direction[X_AXIS]=-1;
  374. }
  375. else{
  376. WRITE_NC(X_DIR_PIN, !INVERT_X_DIR);
  377. count_direction[X_AXIS]=1;
  378. }
  379. if((out_bits & (1<<Y_AXIS))!=0){
  380. WRITE_NC(Y_DIR_PIN, INVERT_Y_DIR);
  381. #ifdef Y_DUAL_STEPPER_DRIVERS
  382. WRITE_NC(Y2_DIR_PIN, !(INVERT_Y_DIR == INVERT_Y2_VS_Y_DIR));
  383. #endif
  384. count_direction[Y_AXIS]=-1;
  385. }
  386. else{
  387. WRITE_NC(Y_DIR_PIN, !INVERT_Y_DIR);
  388. #ifdef Y_DUAL_STEPPER_DRIVERS
  389. WRITE_NC(Y2_DIR_PIN, (INVERT_Y_DIR == INVERT_Y2_VS_Y_DIR));
  390. #endif
  391. count_direction[Y_AXIS]=1;
  392. }
  393. // Set direction en check limit switches
  394. #ifndef COREXY
  395. if ((out_bits & (1<<X_AXIS)) != 0) { // stepping along -X axis
  396. #else
  397. if ((((out_bits & (1<<X_AXIS)) != 0)&&(out_bits & (1<<Y_AXIS)) != 0)) { //-X occurs for -A and -B
  398. #endif
  399. CHECK_ENDSTOPS
  400. {
  401. {
  402. #if ( (defined(X_MIN_PIN) && (X_MIN_PIN > -1)) || defined(TMC2130_SG_HOMING) ) && !defined(DEBUG_DISABLE_XMINLIMIT)
  403. #ifdef TMC2130_SG_HOMING
  404. // Stall guard homing turned on
  405. x_min_endstop = (READ(X_TMC2130_DIAG) != 0);
  406. #else
  407. // Normal homing
  408. x_min_endstop = (READ(X_MIN_PIN) != X_MIN_ENDSTOP_INVERTING);
  409. #endif
  410. if(x_min_endstop && old_x_min_endstop && (current_block->steps_x > 0)) {
  411. endstops_trigsteps[X_AXIS] = count_position[X_AXIS];
  412. endstop_x_hit=true;
  413. step_events_completed = current_block->step_event_count;
  414. }
  415. old_x_min_endstop = x_min_endstop;
  416. #endif
  417. }
  418. }
  419. }
  420. else { // +direction
  421. CHECK_ENDSTOPS
  422. {
  423. {
  424. #if ( (defined(X_MAX_PIN) && (X_MAX_PIN > -1)) || defined(TMC2130_SG_HOMING) ) && !defined(DEBUG_DISABLE_XMAXLIMIT)
  425. #ifdef TMC2130_SG_HOMING
  426. // Stall guard homing turned on
  427. x_max_endstop = (READ(X_TMC2130_DIAG) != 0);
  428. #else
  429. // Normal homing
  430. x_max_endstop = (READ(X_MAX_PIN) != X_MAX_ENDSTOP_INVERTING);
  431. #endif
  432. if(x_max_endstop && old_x_max_endstop && (current_block->steps_x > 0)){
  433. endstops_trigsteps[X_AXIS] = count_position[X_AXIS];
  434. endstop_x_hit=true;
  435. step_events_completed = current_block->step_event_count;
  436. }
  437. old_x_max_endstop = x_max_endstop;
  438. #endif
  439. }
  440. }
  441. }
  442. #ifndef COREXY
  443. if ((out_bits & (1<<Y_AXIS)) != 0) { // -direction
  444. #else
  445. if ((((out_bits & (1<<X_AXIS)) != 0)&&(out_bits & (1<<Y_AXIS)) == 0)) { // -Y occurs for -A and +B
  446. #endif
  447. CHECK_ENDSTOPS
  448. {
  449. #if ( (defined(Y_MIN_PIN) && (Y_MIN_PIN > -1)) || defined(TMC2130_SG_HOMING) ) && !defined(DEBUG_DISABLE_YMINLIMIT)
  450. #ifdef TMC2130_SG_HOMING
  451. // Stall guard homing turned on
  452. y_min_endstop = (READ(Y_TMC2130_DIAG) != 0);
  453. #else
  454. // Normal homing
  455. y_min_endstop = (READ(Y_MIN_PIN) != Y_MIN_ENDSTOP_INVERTING);
  456. #endif
  457. if(y_min_endstop && old_y_min_endstop && (current_block->steps_y > 0)) {
  458. endstops_trigsteps[Y_AXIS] = count_position[Y_AXIS];
  459. endstop_y_hit=true;
  460. step_events_completed = current_block->step_event_count;
  461. }
  462. old_y_min_endstop = y_min_endstop;
  463. #endif
  464. }
  465. }
  466. else { // +direction
  467. CHECK_ENDSTOPS
  468. {
  469. #if ( (defined(Y_MAX_PIN) && (Y_MAX_PIN > -1)) || defined(TMC2130_SG_HOMING) ) && !defined(DEBUG_DISABLE_YMAXLIMIT)
  470. #ifdef TMC2130_SG_HOMING
  471. // Stall guard homing turned on
  472. y_max_endstop = (READ(Y_TMC2130_DIAG) != 0);
  473. #else
  474. // Normal homing
  475. y_max_endstop = (READ(Y_MAX_PIN) != Y_MAX_ENDSTOP_INVERTING);
  476. #endif
  477. if(y_max_endstop && old_y_max_endstop && (current_block->steps_y > 0)){
  478. endstops_trigsteps[Y_AXIS] = count_position[Y_AXIS];
  479. endstop_y_hit=true;
  480. step_events_completed = current_block->step_event_count;
  481. }
  482. old_y_max_endstop = y_max_endstop;
  483. #endif
  484. }
  485. }
  486. if ((out_bits & (1<<Z_AXIS)) != 0) { // -direction
  487. WRITE_NC(Z_DIR_PIN,INVERT_Z_DIR);
  488. #ifdef Z_DUAL_STEPPER_DRIVERS
  489. WRITE_NC(Z2_DIR_PIN,INVERT_Z_DIR);
  490. #endif
  491. count_direction[Z_AXIS]=-1;
  492. if(check_endstops && ! check_z_endstop)
  493. {
  494. #if defined(Z_MIN_PIN) && (Z_MIN_PIN > -1) && !defined(DEBUG_DISABLE_ZMINLIMIT)
  495. #ifdef TMC2130_SG_HOMING
  496. // Stall guard homing turned on
  497. z_min_endstop = (READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING) || (READ(Z_TMC2130_DIAG) != 0);
  498. #else
  499. z_min_endstop = (READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING);
  500. #endif //TMC2130_SG_HOMING
  501. if(z_min_endstop && old_z_min_endstop && (current_block->steps_z > 0)) {
  502. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  503. endstop_z_hit=true;
  504. step_events_completed = current_block->step_event_count;
  505. }
  506. old_z_min_endstop = z_min_endstop;
  507. #endif
  508. }
  509. }
  510. else { // +direction
  511. WRITE_NC(Z_DIR_PIN,!INVERT_Z_DIR);
  512. #ifdef Z_DUAL_STEPPER_DRIVERS
  513. WRITE_NC(Z2_DIR_PIN,!INVERT_Z_DIR);
  514. #endif
  515. count_direction[Z_AXIS]=1;
  516. CHECK_ENDSTOPS
  517. {
  518. #if defined(Z_MAX_PIN) && (Z_MAX_PIN > -1) && !defined(DEBUG_DISABLE_ZMAXLIMIT)
  519. #ifdef TMC2130_SG_HOMING
  520. // Stall guard homing turned on
  521. z_max_endstop = (READ(Z_TMC2130_DIAG) != 0);
  522. #else
  523. z_max_endstop = (READ(Z_MAX_PIN) != Z_MAX_ENDSTOP_INVERTING);
  524. #endif //TMC2130_SG_HOMING
  525. if(z_max_endstop && old_z_max_endstop && (current_block->steps_z > 0)) {
  526. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  527. endstop_z_hit=true;
  528. step_events_completed = current_block->step_event_count;
  529. }
  530. old_z_max_endstop = z_max_endstop;
  531. #endif
  532. }
  533. }
  534. // Supporting stopping on a trigger of the Z-stop induction sensor, not only for the Z-minus movements.
  535. #if defined(Z_MIN_PIN) && (Z_MIN_PIN > -1) && !defined(DEBUG_DISABLE_ZMINLIMIT)
  536. if(check_z_endstop) {
  537. // Check the Z min end-stop no matter what.
  538. // Good for searching for the center of an induction target.
  539. #ifdef TMC2130_SG_HOMING
  540. // Stall guard homing turned on
  541. z_min_endstop = (READ(Z_MIN_PIN) != z_endstop_invert) || (READ(Z_TMC2130_DIAG) != 0);
  542. #else
  543. z_min_endstop = (READ(Z_MIN_PIN) != z_endstop_invert);
  544. #endif //TMC2130_SG_HOMING
  545. if(z_min_endstop && old_z_min_endstop) {
  546. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  547. endstop_z_hit=true;
  548. step_events_completed = current_block->step_event_count;
  549. }
  550. old_z_min_endstop = z_min_endstop;
  551. }
  552. #endif
  553. if ((out_bits & (1 << E_AXIS)) != 0)
  554. { // -direction
  555. //AKU
  556. #ifdef SNMM
  557. if (snmm_extruder == 0 || snmm_extruder == 2)
  558. {
  559. NORM_E_DIR();
  560. }
  561. else
  562. {
  563. REV_E_DIR();
  564. }
  565. #else
  566. REV_E_DIR();
  567. #endif // SNMM
  568. count_direction[E_AXIS] = -1;
  569. }
  570. else
  571. { // +direction
  572. #ifdef SNMM
  573. if (snmm_extruder == 0 || snmm_extruder == 2)
  574. {
  575. REV_E_DIR();
  576. }
  577. else
  578. {
  579. NORM_E_DIR();
  580. }
  581. #else
  582. NORM_E_DIR();
  583. #endif // SNMM
  584. count_direction[E_AXIS] = 1;
  585. }
  586. for(uint8_t i=0; i < step_loops; i++) { // Take multiple steps per interrupt (For high speed moves)
  587. #ifndef AT90USB
  588. MSerial.checkRx(); // Check for serial chars.
  589. #endif //RP - returned, because missing characters
  590. #ifdef LIN_ADVANCE
  591. counter_e += current_block->steps_e;
  592. if (counter_e > 0) {
  593. counter_e -= current_block->step_event_count;
  594. count_position[E_AXIS] += count_direction[E_AXIS];
  595. ((out_bits&(1<<E_AXIS))!=0) ? --e_steps : ++e_steps;
  596. }
  597. #endif
  598. counter_x += current_block->steps_x;
  599. if (counter_x > 0) {
  600. WRITE_NC(X_STEP_PIN, !INVERT_X_STEP_PIN);
  601. LastStepMask |= X_AXIS_MASK;
  602. #ifdef DEBUG_XSTEP_DUP_PIN
  603. WRITE_NC(DEBUG_XSTEP_DUP_PIN,!INVERT_X_STEP_PIN);
  604. #endif //DEBUG_XSTEP_DUP_PIN
  605. counter_x -= current_block->step_event_count;
  606. count_position[X_AXIS]+=count_direction[X_AXIS];
  607. WRITE_NC(X_STEP_PIN, INVERT_X_STEP_PIN);
  608. #ifdef DEBUG_XSTEP_DUP_PIN
  609. WRITE_NC(DEBUG_XSTEP_DUP_PIN,INVERT_X_STEP_PIN);
  610. #endif //DEBUG_XSTEP_DUP_PIN
  611. }
  612. counter_y += current_block->steps_y;
  613. if (counter_y > 0) {
  614. WRITE_NC(Y_STEP_PIN, !INVERT_Y_STEP_PIN);
  615. LastStepMask |= Y_AXIS_MASK;
  616. #ifdef DEBUG_YSTEP_DUP_PIN
  617. WRITE_NC(DEBUG_YSTEP_DUP_PIN,!INVERT_Y_STEP_PIN);
  618. #endif //DEBUG_YSTEP_DUP_PIN
  619. #ifdef Y_DUAL_STEPPER_DRIVERS
  620. WRITE_NC(Y2_STEP_PIN, !INVERT_Y_STEP_PIN);
  621. #endif
  622. counter_y -= current_block->step_event_count;
  623. count_position[Y_AXIS]+=count_direction[Y_AXIS];
  624. WRITE_NC(Y_STEP_PIN, INVERT_Y_STEP_PIN);
  625. #ifdef DEBUG_YSTEP_DUP_PIN
  626. WRITE_NC(DEBUG_YSTEP_DUP_PIN,INVERT_Y_STEP_PIN);
  627. #endif //DEBUG_YSTEP_DUP_PIN
  628. #ifdef Y_DUAL_STEPPER_DRIVERS
  629. WRITE_NC(Y2_STEP_PIN, INVERT_Y_STEP_PIN);
  630. #endif
  631. }
  632. counter_z += current_block->steps_z;
  633. if (counter_z > 0) {
  634. WRITE_NC(Z_STEP_PIN, !INVERT_Z_STEP_PIN);
  635. LastStepMask |= Z_AXIS_MASK;
  636. #ifdef Z_DUAL_STEPPER_DRIVERS
  637. WRITE_NC(Z2_STEP_PIN, !INVERT_Z_STEP_PIN);
  638. #endif
  639. counter_z -= current_block->step_event_count;
  640. count_position[Z_AXIS]+=count_direction[Z_AXIS];
  641. WRITE_NC(Z_STEP_PIN, INVERT_Z_STEP_PIN);
  642. #ifdef Z_DUAL_STEPPER_DRIVERS
  643. WRITE_NC(Z2_STEP_PIN, INVERT_Z_STEP_PIN);
  644. #endif
  645. }
  646. #ifndef LIN_ADVANCE
  647. counter_e += current_block->steps_e;
  648. if (counter_e > 0) {
  649. WRITE_E_STEP(!INVERT_E_STEP_PIN);
  650. counter_e -= current_block->step_event_count;
  651. count_position[E_AXIS]+=count_direction[E_AXIS];
  652. WRITE_E_STEP(INVERT_E_STEP_PIN);
  653. #ifdef PAT9125
  654. fsensor_counter++;
  655. #endif //PAT9125
  656. }
  657. #endif
  658. step_events_completed += 1;
  659. if(step_events_completed >= current_block->step_event_count) break;
  660. }
  661. #ifdef LIN_ADVANCE
  662. if (current_block->use_advance_lead) {
  663. const int delta_adv_steps = current_estep_rate - current_adv_steps;
  664. current_adv_steps += delta_adv_steps;
  665. e_steps += delta_adv_steps;
  666. }
  667. // If we have esteps to execute, fire the next advance_isr "now"
  668. if (e_steps) nextAdvanceISR = 0;
  669. #endif
  670. // Calculare new timer value
  671. unsigned short timer;
  672. uint16_t step_rate;
  673. if (step_events_completed <= (unsigned long int)current_block->accelerate_until) {
  674. // v = t * a -> acc_step_rate = acceleration_time * current_block->acceleration_rate
  675. MultiU24X24toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate);
  676. acc_step_rate += current_block->initial_rate;
  677. // upper limit
  678. if(acc_step_rate > current_block->nominal_rate)
  679. acc_step_rate = current_block->nominal_rate;
  680. // step_rate to timer interval
  681. timer = calc_timer(acc_step_rate);
  682. _NEXT_ISR(timer);
  683. acceleration_time += timer;
  684. #ifdef LIN_ADVANCE
  685. if (current_block->use_advance_lead) {
  686. current_estep_rate = ((uint32_t)acc_step_rate * current_block->abs_adv_steps_multiplier8) >> 17;
  687. }
  688. eISR_Rate = ADV_RATE(timer, step_loops);
  689. #endif
  690. }
  691. else if (step_events_completed > (unsigned long int)current_block->decelerate_after) {
  692. MultiU24X24toH16(step_rate, deceleration_time, current_block->acceleration_rate);
  693. if(step_rate > acc_step_rate) { // Check step_rate stays positive
  694. step_rate = current_block->final_rate;
  695. }
  696. else {
  697. step_rate = acc_step_rate - step_rate; // Decelerate from aceleration end point.
  698. }
  699. // lower limit
  700. if(step_rate < current_block->final_rate)
  701. step_rate = current_block->final_rate;
  702. // step_rate to timer interval
  703. timer = calc_timer(step_rate);
  704. _NEXT_ISR(timer);
  705. deceleration_time += timer;
  706. #ifdef LIN_ADVANCE
  707. if (current_block->use_advance_lead) {
  708. current_estep_rate = ((uint32_t)step_rate * current_block->abs_adv_steps_multiplier8) >> 17;
  709. }
  710. eISR_Rate = ADV_RATE(timer, step_loops);
  711. #endif
  712. }
  713. else {
  714. #ifdef LIN_ADVANCE
  715. if (current_block->use_advance_lead)
  716. current_estep_rate = final_estep_rate;
  717. eISR_Rate = ADV_RATE(OCR1A_nominal, step_loops_nominal);
  718. #endif
  719. _NEXT_ISR(OCR1A_nominal);
  720. // ensure we're running at the correct step rate, even if we just came off an acceleration
  721. step_loops = step_loops_nominal;
  722. }
  723. // If current block is finished, reset pointer
  724. if (step_events_completed >= current_block->step_event_count) {
  725. #ifdef PAT9125
  726. fsensor_st_block_chunk(current_block, fsensor_counter);
  727. fsensor_counter = 0;
  728. #endif //PAT9125
  729. current_block = NULL;
  730. plan_discard_current_block();
  731. }
  732. #ifdef PAT9125
  733. else if (fsensor_counter >= fsensor_chunk_len)
  734. {
  735. fsensor_st_block_chunk(current_block, fsensor_counter);
  736. fsensor_counter = 0;
  737. }
  738. #endif //PAT9125
  739. }
  740. #ifdef TMC2130
  741. tmc2130_st_isr(LastStepMask);
  742. #endif //TMC2130
  743. #ifdef DEBUG_STEPPER_TIMER_MISSED
  744. // Verify whether the next planned timer interrupt has not been missed already.
  745. // This debugging test takes < 1.125us
  746. // This skews the profiling slightly as the fastest stepper timer
  747. // interrupt repeats at a 100us rate (10kHz).
  748. if (OCR1A < TCNT1) {
  749. stepper_timer_overflow_state = true;
  750. WRITE_NC(BEEPER, HIGH);
  751. SERIAL_PROTOCOLPGM("Stepper timer overflow ");
  752. SERIAL_PROTOCOL(OCR1A);
  753. SERIAL_PROTOCOLPGM("<");
  754. SERIAL_PROTOCOL(TCNT1);
  755. SERIAL_PROTOCOLLN("!");
  756. }
  757. #endif
  758. }
  759. #ifdef LIN_ADVANCE
  760. // Timer interrupt for E. e_steps is set in the main routine.
  761. void advance_isr() {
  762. if (e_steps) {
  763. bool dir =
  764. #ifdef SNMM
  765. ((e_steps < 0) == (snmm_extruder & 1))
  766. #else
  767. (e_steps < 0)
  768. #endif
  769. ? INVERT_E0_DIR : !INVERT_E0_DIR; //If we have SNMM, reverse every second extruder.
  770. WRITE_NC(E0_DIR_PIN, dir);
  771. for (uint8_t i = step_loops; e_steps && i--;) {
  772. WRITE_NC(E0_STEP_PIN, !INVERT_E_STEP_PIN);
  773. e_steps < 0 ? ++e_steps : --e_steps;
  774. WRITE_NC(E0_STEP_PIN, INVERT_E_STEP_PIN);
  775. #ifdef PAT9125
  776. fsensor_counter++;
  777. #endif //PAT9125
  778. }
  779. }
  780. else {
  781. eISR_Rate = ADV_NEVER;
  782. }
  783. nextAdvanceISR = eISR_Rate;
  784. }
  785. void advance_isr_scheduler() {
  786. // Run main stepping ISR if flagged
  787. if (!nextMainISR) isr();
  788. // Run Advance stepping ISR if flagged
  789. if (!nextAdvanceISR) advance_isr();
  790. // Is the next advance ISR scheduled before the next main ISR?
  791. if (nextAdvanceISR <= nextMainISR) {
  792. // Set up the next interrupt
  793. OCR1A = nextAdvanceISR;
  794. // New interval for the next main ISR
  795. if (nextMainISR) nextMainISR -= nextAdvanceISR;
  796. // Will call Stepper::advance_isr on the next interrupt
  797. nextAdvanceISR = 0;
  798. }
  799. else {
  800. // The next main ISR comes first
  801. OCR1A = nextMainISR;
  802. // New interval for the next advance ISR, if any
  803. if (nextAdvanceISR && nextAdvanceISR != ADV_NEVER)
  804. nextAdvanceISR -= nextMainISR;
  805. // Will call Stepper::isr on the next interrupt
  806. nextMainISR = 0;
  807. }
  808. // Don't run the ISR faster than possible
  809. if (OCR1A < TCNT1 + 16) OCR1A = TCNT1 + 16;
  810. }
  811. void clear_current_adv_vars() {
  812. e_steps = 0; //Should be already 0 at an filament change event, but just to be sure..
  813. current_adv_steps = 0;
  814. }
  815. #endif // LIN_ADVANCE
  816. void st_init()
  817. {
  818. #ifdef TMC2130
  819. tmc2130_init();
  820. #endif //TMC2130
  821. digipot_init(); //Initialize Digipot Motor Current
  822. microstep_init(); //Initialize Microstepping Pins
  823. //Initialize Dir Pins
  824. #if defined(X_DIR_PIN) && X_DIR_PIN > -1
  825. SET_OUTPUT(X_DIR_PIN);
  826. #endif
  827. #if defined(X2_DIR_PIN) && X2_DIR_PIN > -1
  828. SET_OUTPUT(X2_DIR_PIN);
  829. #endif
  830. #if defined(Y_DIR_PIN) && Y_DIR_PIN > -1
  831. SET_OUTPUT(Y_DIR_PIN);
  832. #if defined(Y_DUAL_STEPPER_DRIVERS) && defined(Y2_DIR_PIN) && (Y2_DIR_PIN > -1)
  833. SET_OUTPUT(Y2_DIR_PIN);
  834. #endif
  835. #endif
  836. #if defined(Z_DIR_PIN) && Z_DIR_PIN > -1
  837. SET_OUTPUT(Z_DIR_PIN);
  838. #if defined(Z_DUAL_STEPPER_DRIVERS) && defined(Z2_DIR_PIN) && (Z2_DIR_PIN > -1)
  839. SET_OUTPUT(Z2_DIR_PIN);
  840. #endif
  841. #endif
  842. #if defined(E0_DIR_PIN) && E0_DIR_PIN > -1
  843. SET_OUTPUT(E0_DIR_PIN);
  844. #endif
  845. #if defined(E1_DIR_PIN) && (E1_DIR_PIN > -1)
  846. SET_OUTPUT(E1_DIR_PIN);
  847. #endif
  848. #if defined(E2_DIR_PIN) && (E2_DIR_PIN > -1)
  849. SET_OUTPUT(E2_DIR_PIN);
  850. #endif
  851. //Initialize Enable Pins - steppers default to disabled.
  852. #if defined(X_ENABLE_PIN) && X_ENABLE_PIN > -1
  853. SET_OUTPUT(X_ENABLE_PIN);
  854. if(!X_ENABLE_ON) WRITE(X_ENABLE_PIN,HIGH);
  855. #endif
  856. #if defined(X2_ENABLE_PIN) && X2_ENABLE_PIN > -1
  857. SET_OUTPUT(X2_ENABLE_PIN);
  858. if(!X_ENABLE_ON) WRITE(X2_ENABLE_PIN,HIGH);
  859. #endif
  860. #if defined(Y_ENABLE_PIN) && Y_ENABLE_PIN > -1
  861. SET_OUTPUT(Y_ENABLE_PIN);
  862. if(!Y_ENABLE_ON) WRITE(Y_ENABLE_PIN,HIGH);
  863. #if defined(Y_DUAL_STEPPER_DRIVERS) && defined(Y2_ENABLE_PIN) && (Y2_ENABLE_PIN > -1)
  864. SET_OUTPUT(Y2_ENABLE_PIN);
  865. if(!Y_ENABLE_ON) WRITE(Y2_ENABLE_PIN,HIGH);
  866. #endif
  867. #endif
  868. #if defined(Z_ENABLE_PIN) && Z_ENABLE_PIN > -1
  869. SET_OUTPUT(Z_ENABLE_PIN);
  870. if(!Z_ENABLE_ON) WRITE(Z_ENABLE_PIN,HIGH);
  871. #if defined(Z_DUAL_STEPPER_DRIVERS) && defined(Z2_ENABLE_PIN) && (Z2_ENABLE_PIN > -1)
  872. SET_OUTPUT(Z2_ENABLE_PIN);
  873. if(!Z_ENABLE_ON) WRITE(Z2_ENABLE_PIN,HIGH);
  874. #endif
  875. #endif
  876. #if defined(E0_ENABLE_PIN) && (E0_ENABLE_PIN > -1)
  877. SET_OUTPUT(E0_ENABLE_PIN);
  878. if(!E_ENABLE_ON) WRITE(E0_ENABLE_PIN,HIGH);
  879. #endif
  880. #if defined(E1_ENABLE_PIN) && (E1_ENABLE_PIN > -1)
  881. SET_OUTPUT(E1_ENABLE_PIN);
  882. if(!E_ENABLE_ON) WRITE(E1_ENABLE_PIN,HIGH);
  883. #endif
  884. #if defined(E2_ENABLE_PIN) && (E2_ENABLE_PIN > -1)
  885. SET_OUTPUT(E2_ENABLE_PIN);
  886. if(!E_ENABLE_ON) WRITE(E2_ENABLE_PIN,HIGH);
  887. #endif
  888. //endstops and pullups
  889. #ifdef TMC2130_SG_HOMING
  890. SET_INPUT(X_TMC2130_DIAG);
  891. WRITE(X_TMC2130_DIAG,HIGH);
  892. SET_INPUT(Y_TMC2130_DIAG);
  893. WRITE(Y_TMC2130_DIAG,HIGH);
  894. SET_INPUT(Z_TMC2130_DIAG);
  895. WRITE(Z_TMC2130_DIAG,HIGH);
  896. SET_INPUT(E0_TMC2130_DIAG);
  897. WRITE(E0_TMC2130_DIAG,HIGH);
  898. #endif
  899. #if defined(X_MIN_PIN) && X_MIN_PIN > -1
  900. SET_INPUT(X_MIN_PIN);
  901. #ifdef ENDSTOPPULLUP_XMIN
  902. WRITE(X_MIN_PIN,HIGH);
  903. #endif
  904. #endif
  905. #if defined(Y_MIN_PIN) && Y_MIN_PIN > -1
  906. SET_INPUT(Y_MIN_PIN);
  907. #ifdef ENDSTOPPULLUP_YMIN
  908. WRITE(Y_MIN_PIN,HIGH);
  909. #endif
  910. #endif
  911. #if defined(Z_MIN_PIN) && Z_MIN_PIN > -1
  912. SET_INPUT(Z_MIN_PIN);
  913. #ifdef ENDSTOPPULLUP_ZMIN
  914. WRITE(Z_MIN_PIN,HIGH);
  915. #endif
  916. #endif
  917. #if defined(X_MAX_PIN) && X_MAX_PIN > -1
  918. SET_INPUT(X_MAX_PIN);
  919. #ifdef ENDSTOPPULLUP_XMAX
  920. WRITE(X_MAX_PIN,HIGH);
  921. #endif
  922. #endif
  923. #if defined(Y_MAX_PIN) && Y_MAX_PIN > -1
  924. SET_INPUT(Y_MAX_PIN);
  925. #ifdef ENDSTOPPULLUP_YMAX
  926. WRITE(Y_MAX_PIN,HIGH);
  927. #endif
  928. #endif
  929. #if defined(Z_MAX_PIN) && Z_MAX_PIN > -1
  930. SET_INPUT(Z_MAX_PIN);
  931. #ifdef ENDSTOPPULLUP_ZMAX
  932. WRITE(Z_MAX_PIN,HIGH);
  933. #endif
  934. #endif
  935. #if (defined(FANCHECK) && defined(TACH_0) && (TACH_0 > -1))
  936. SET_INPUT(TACH_0);
  937. #ifdef TACH0PULLUP
  938. WRITE(TACH_0, HIGH);
  939. #endif
  940. #endif
  941. //Initialize Step Pins
  942. #if defined(X_STEP_PIN) && (X_STEP_PIN > -1)
  943. SET_OUTPUT(X_STEP_PIN);
  944. WRITE(X_STEP_PIN,INVERT_X_STEP_PIN);
  945. #ifdef DEBUG_XSTEP_DUP_PIN
  946. SET_OUTPUT(DEBUG_XSTEP_DUP_PIN);
  947. WRITE(DEBUG_XSTEP_DUP_PIN,INVERT_X_STEP_PIN);
  948. #endif //DEBUG_XSTEP_DUP_PIN
  949. disable_x();
  950. #endif
  951. #if defined(X2_STEP_PIN) && (X2_STEP_PIN > -1)
  952. SET_OUTPUT(X2_STEP_PIN);
  953. WRITE(X2_STEP_PIN,INVERT_X_STEP_PIN);
  954. disable_x();
  955. #endif
  956. #if defined(Y_STEP_PIN) && (Y_STEP_PIN > -1)
  957. SET_OUTPUT(Y_STEP_PIN);
  958. WRITE(Y_STEP_PIN,INVERT_Y_STEP_PIN);
  959. #ifdef DEBUG_YSTEP_DUP_PIN
  960. SET_OUTPUT(DEBUG_YSTEP_DUP_PIN);
  961. WRITE(DEBUG_YSTEP_DUP_PIN,INVERT_Y_STEP_PIN);
  962. #endif //DEBUG_YSTEP_DUP_PIN
  963. #if defined(Y_DUAL_STEPPER_DRIVERS) && defined(Y2_STEP_PIN) && (Y2_STEP_PIN > -1)
  964. SET_OUTPUT(Y2_STEP_PIN);
  965. WRITE(Y2_STEP_PIN,INVERT_Y_STEP_PIN);
  966. #endif
  967. disable_y();
  968. #endif
  969. #if defined(Z_STEP_PIN) && (Z_STEP_PIN > -1)
  970. SET_OUTPUT(Z_STEP_PIN);
  971. WRITE(Z_STEP_PIN,INVERT_Z_STEP_PIN);
  972. #if defined(Z_DUAL_STEPPER_DRIVERS) && defined(Z2_STEP_PIN) && (Z2_STEP_PIN > -1)
  973. SET_OUTPUT(Z2_STEP_PIN);
  974. WRITE(Z2_STEP_PIN,INVERT_Z_STEP_PIN);
  975. #endif
  976. disable_z();
  977. #endif
  978. #if defined(E0_STEP_PIN) && (E0_STEP_PIN > -1)
  979. SET_OUTPUT(E0_STEP_PIN);
  980. WRITE(E0_STEP_PIN,INVERT_E_STEP_PIN);
  981. disable_e0();
  982. #endif
  983. #if defined(E1_STEP_PIN) && (E1_STEP_PIN > -1)
  984. SET_OUTPUT(E1_STEP_PIN);
  985. WRITE(E1_STEP_PIN,INVERT_E_STEP_PIN);
  986. disable_e1();
  987. #endif
  988. #if defined(E2_STEP_PIN) && (E2_STEP_PIN > -1)
  989. SET_OUTPUT(E2_STEP_PIN);
  990. WRITE(E2_STEP_PIN,INVERT_E_STEP_PIN);
  991. disable_e2();
  992. #endif
  993. // waveform generation = 0100 = CTC
  994. TCCR1B &= ~(1<<WGM13);
  995. TCCR1B |= (1<<WGM12);
  996. TCCR1A &= ~(1<<WGM11);
  997. TCCR1A &= ~(1<<WGM10);
  998. // output mode = 00 (disconnected)
  999. TCCR1A &= ~(3<<COM1A0);
  1000. TCCR1A &= ~(3<<COM1B0);
  1001. // Set the timer pre-scaler
  1002. // Generally we use a divider of 8, resulting in a 2MHz timer
  1003. // frequency on a 16MHz MCU. If you are going to change this, be
  1004. // sure to regenerate speed_lookuptable.h with
  1005. // create_speed_lookuptable.py
  1006. TCCR1B = (TCCR1B & ~(0x07<<CS10)) | (2<<CS10);
  1007. // Plan the first interrupt after 8ms from now.
  1008. OCR1A = 0x4000;
  1009. TCNT1 = 0;
  1010. ENABLE_STEPPER_DRIVER_INTERRUPT();
  1011. #ifdef LIN_ADVANCE
  1012. e_steps = 0;
  1013. current_adv_steps = 0;
  1014. #endif
  1015. enable_endstops(true); // Start with endstops active. After homing they can be disabled
  1016. sei();
  1017. }
  1018. // Block until all buffered steps are executed
  1019. void st_synchronize()
  1020. {
  1021. while(blocks_queued())
  1022. {
  1023. #ifdef TMC2130
  1024. manage_heater();
  1025. // Vojtech: Don't disable motors inside the planner!
  1026. if (!tmc2130_update_sg())
  1027. {
  1028. manage_inactivity(true);
  1029. lcd_update();
  1030. }
  1031. #else //TMC2130
  1032. manage_heater();
  1033. // Vojtech: Don't disable motors inside the planner!
  1034. manage_inactivity(true);
  1035. lcd_update();
  1036. #endif //TMC2130
  1037. }
  1038. }
  1039. void st_set_position(const long &x, const long &y, const long &z, const long &e)
  1040. {
  1041. CRITICAL_SECTION_START;
  1042. // Copy 4x4B.
  1043. // This block locks the interrupts globally for 4.56 us,
  1044. // which corresponds to a maximum repeat frequency of 219.18 kHz.
  1045. // This blocking is safe in the context of a 10kHz stepper driver interrupt
  1046. // or a 115200 Bd serial line receive interrupt, which will not trigger faster than 12kHz.
  1047. count_position[X_AXIS] = x;
  1048. count_position[Y_AXIS] = y;
  1049. count_position[Z_AXIS] = z;
  1050. count_position[E_AXIS] = e;
  1051. CRITICAL_SECTION_END;
  1052. }
  1053. void st_set_e_position(const long &e)
  1054. {
  1055. CRITICAL_SECTION_START;
  1056. count_position[E_AXIS] = e;
  1057. CRITICAL_SECTION_END;
  1058. }
  1059. long st_get_position(uint8_t axis)
  1060. {
  1061. long count_pos;
  1062. CRITICAL_SECTION_START;
  1063. count_pos = count_position[axis];
  1064. CRITICAL_SECTION_END;
  1065. return count_pos;
  1066. }
  1067. void st_get_position_xy(long &x, long &y)
  1068. {
  1069. CRITICAL_SECTION_START;
  1070. x = count_position[X_AXIS];
  1071. y = count_position[Y_AXIS];
  1072. CRITICAL_SECTION_END;
  1073. }
  1074. float st_get_position_mm(uint8_t axis)
  1075. {
  1076. float steper_position_in_steps = st_get_position(axis);
  1077. return steper_position_in_steps / axis_steps_per_unit[axis];
  1078. }
  1079. void finishAndDisableSteppers()
  1080. {
  1081. st_synchronize();
  1082. disable_x();
  1083. disable_y();
  1084. disable_z();
  1085. disable_e0();
  1086. disable_e1();
  1087. disable_e2();
  1088. }
  1089. void quickStop()
  1090. {
  1091. DISABLE_STEPPER_DRIVER_INTERRUPT();
  1092. while (blocks_queued()) plan_discard_current_block();
  1093. current_block = NULL;
  1094. st_reset_timer();
  1095. ENABLE_STEPPER_DRIVER_INTERRUPT();
  1096. }
  1097. #ifdef BABYSTEPPING
  1098. void babystep(const uint8_t axis,const bool direction)
  1099. {
  1100. //MUST ONLY BE CALLED BY A ISR, it depends on that no other ISR interrupts this
  1101. //store initial pin states
  1102. switch(axis)
  1103. {
  1104. case X_AXIS:
  1105. {
  1106. enable_x();
  1107. uint8_t old_x_dir_pin= READ(X_DIR_PIN); //if dualzstepper, both point to same direction.
  1108. //setup new step
  1109. WRITE(X_DIR_PIN,(INVERT_X_DIR)^direction);
  1110. //perform step
  1111. WRITE(X_STEP_PIN, !INVERT_X_STEP_PIN);
  1112. LastStepMask |= X_AXIS_MASK;
  1113. #ifdef DEBUG_XSTEP_DUP_PIN
  1114. WRITE(DEBUG_XSTEP_DUP_PIN,!INVERT_X_STEP_PIN);
  1115. #endif //DEBUG_XSTEP_DUP_PIN
  1116. {
  1117. volatile float x=1./float(axis+1)/float(axis+2); //wait a tiny bit
  1118. }
  1119. WRITE(X_STEP_PIN, INVERT_X_STEP_PIN);
  1120. #ifdef DEBUG_XSTEP_DUP_PIN
  1121. WRITE(DEBUG_XSTEP_DUP_PIN,INVERT_X_STEP_PIN);
  1122. #endif //DEBUG_XSTEP_DUP_PIN
  1123. //get old pin state back.
  1124. WRITE(X_DIR_PIN,old_x_dir_pin);
  1125. }
  1126. break;
  1127. case Y_AXIS:
  1128. {
  1129. enable_y();
  1130. uint8_t old_y_dir_pin= READ(Y_DIR_PIN); //if dualzstepper, both point to same direction.
  1131. //setup new step
  1132. WRITE(Y_DIR_PIN,(INVERT_Y_DIR)^direction);
  1133. //perform step
  1134. WRITE(Y_STEP_PIN, !INVERT_Y_STEP_PIN);
  1135. LastStepMask |= Y_AXIS_MASK;
  1136. #ifdef DEBUG_YSTEP_DUP_PIN
  1137. WRITE(DEBUG_YSTEP_DUP_PIN,!INVERT_Y_STEP_PIN);
  1138. #endif //DEBUG_YSTEP_DUP_PIN
  1139. {
  1140. volatile float x=1./float(axis+1)/float(axis+2); //wait a tiny bit
  1141. }
  1142. WRITE(Y_STEP_PIN, INVERT_Y_STEP_PIN);
  1143. #ifdef DEBUG_YSTEP_DUP_PIN
  1144. WRITE(DEBUG_YSTEP_DUP_PIN,INVERT_Y_STEP_PIN);
  1145. #endif //DEBUG_YSTEP_DUP_PIN
  1146. //get old pin state back.
  1147. WRITE(Y_DIR_PIN,old_y_dir_pin);
  1148. }
  1149. break;
  1150. case Z_AXIS:
  1151. {
  1152. enable_z();
  1153. uint8_t old_z_dir_pin= READ(Z_DIR_PIN); //if dualzstepper, both point to same direction.
  1154. //setup new step
  1155. WRITE(Z_DIR_PIN,(INVERT_Z_DIR)^direction^BABYSTEP_INVERT_Z);
  1156. #ifdef Z_DUAL_STEPPER_DRIVERS
  1157. WRITE(Z2_DIR_PIN,(INVERT_Z_DIR)^direction^BABYSTEP_INVERT_Z);
  1158. #endif
  1159. //perform step
  1160. WRITE(Z_STEP_PIN, !INVERT_Z_STEP_PIN);
  1161. LastStepMask |= Z_AXIS_MASK;
  1162. #ifdef Z_DUAL_STEPPER_DRIVERS
  1163. WRITE(Z2_STEP_PIN, !INVERT_Z_STEP_PIN);
  1164. #endif
  1165. //wait a tiny bit
  1166. {
  1167. volatile float x=1./float(axis+1); //absolutely useless
  1168. }
  1169. WRITE(Z_STEP_PIN, INVERT_Z_STEP_PIN);
  1170. #ifdef Z_DUAL_STEPPER_DRIVERS
  1171. WRITE(Z2_STEP_PIN, INVERT_Z_STEP_PIN);
  1172. #endif
  1173. //get old pin state back.
  1174. WRITE(Z_DIR_PIN,old_z_dir_pin);
  1175. #ifdef Z_DUAL_STEPPER_DRIVERS
  1176. WRITE(Z2_DIR_PIN,old_z_dir_pin);
  1177. #endif
  1178. }
  1179. break;
  1180. default: break;
  1181. }
  1182. }
  1183. #endif //BABYSTEPPING
  1184. void digitalPotWrite(int address, int value) // From Arduino DigitalPotControl example
  1185. {
  1186. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  1187. digitalWrite(DIGIPOTSS_PIN,LOW); // take the SS pin low to select the chip
  1188. SPI.transfer(address); // send in the address and value via SPI:
  1189. SPI.transfer(value);
  1190. digitalWrite(DIGIPOTSS_PIN,HIGH); // take the SS pin high to de-select the chip:
  1191. //delay(10);
  1192. #endif
  1193. }
  1194. void EEPROM_read_st(int pos, uint8_t* value, uint8_t size)
  1195. {
  1196. do
  1197. {
  1198. *value = eeprom_read_byte((unsigned char*)pos);
  1199. pos++;
  1200. value++;
  1201. }while(--size);
  1202. }
  1203. void digipot_init() //Initialize Digipot Motor Current
  1204. {
  1205. EEPROM_read_st(EEPROM_SILENT,(uint8_t*)&SilentMode,sizeof(SilentMode));
  1206. SilentModeMenu = SilentMode;
  1207. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  1208. if(SilentMode == 0){
  1209. const uint8_t digipot_motor_current[] = DIGIPOT_MOTOR_CURRENT_LOUD;
  1210. }else{
  1211. const uint8_t digipot_motor_current[] = DIGIPOT_MOTOR_CURRENT;
  1212. }
  1213. SPI.begin();
  1214. pinMode(DIGIPOTSS_PIN, OUTPUT);
  1215. for(int i=0;i<=4;i++)
  1216. //digitalPotWrite(digipot_ch[i], digipot_motor_current[i]);
  1217. digipot_current(i,digipot_motor_current[i]);
  1218. #endif
  1219. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  1220. pinMode(MOTOR_CURRENT_PWM_XY_PIN, OUTPUT);
  1221. pinMode(MOTOR_CURRENT_PWM_Z_PIN, OUTPUT);
  1222. pinMode(MOTOR_CURRENT_PWM_E_PIN, OUTPUT);
  1223. if((SilentMode == 0) || (farm_mode) ){
  1224. motor_current_setting[0] = motor_current_setting_loud[0];
  1225. motor_current_setting[1] = motor_current_setting_loud[1];
  1226. motor_current_setting[2] = motor_current_setting_loud[2];
  1227. }else{
  1228. motor_current_setting[0] = motor_current_setting_silent[0];
  1229. motor_current_setting[1] = motor_current_setting_silent[1];
  1230. motor_current_setting[2] = motor_current_setting_silent[2];
  1231. }
  1232. digipot_current(0, motor_current_setting[0]);
  1233. digipot_current(1, motor_current_setting[1]);
  1234. digipot_current(2, motor_current_setting[2]);
  1235. //Set timer5 to 31khz so the PWM of the motor power is as constant as possible. (removes a buzzing noise)
  1236. TCCR5B = (TCCR5B & ~(_BV(CS50) | _BV(CS51) | _BV(CS52))) | _BV(CS50);
  1237. #endif
  1238. }
  1239. void digipot_current(uint8_t driver, int current)
  1240. {
  1241. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  1242. const uint8_t digipot_ch[] = DIGIPOT_CHANNELS;
  1243. digitalPotWrite(digipot_ch[driver], current);
  1244. #endif
  1245. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  1246. if (driver == 0) analogWrite(MOTOR_CURRENT_PWM_XY_PIN, (long)current * 255L / (long)MOTOR_CURRENT_PWM_RANGE);
  1247. if (driver == 1) analogWrite(MOTOR_CURRENT_PWM_Z_PIN, (long)current * 255L / (long)MOTOR_CURRENT_PWM_RANGE);
  1248. if (driver == 2) analogWrite(MOTOR_CURRENT_PWM_E_PIN, (long)current * 255L / (long)MOTOR_CURRENT_PWM_RANGE);
  1249. #endif
  1250. }
  1251. void microstep_init()
  1252. {
  1253. const uint8_t microstep_modes[] = MICROSTEP_MODES;
  1254. #if defined(E1_MS1_PIN) && E1_MS1_PIN > -1
  1255. pinMode(E1_MS1_PIN,OUTPUT);
  1256. pinMode(E1_MS2_PIN,OUTPUT);
  1257. #endif
  1258. #if defined(X_MS1_PIN) && X_MS1_PIN > -1
  1259. pinMode(X_MS1_PIN,OUTPUT);
  1260. pinMode(X_MS2_PIN,OUTPUT);
  1261. pinMode(Y_MS1_PIN,OUTPUT);
  1262. pinMode(Y_MS2_PIN,OUTPUT);
  1263. pinMode(Z_MS1_PIN,OUTPUT);
  1264. pinMode(Z_MS2_PIN,OUTPUT);
  1265. pinMode(E0_MS1_PIN,OUTPUT);
  1266. pinMode(E0_MS2_PIN,OUTPUT);
  1267. for(int i=0;i<=4;i++) microstep_mode(i,microstep_modes[i]);
  1268. #endif
  1269. }
  1270. void microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2)
  1271. {
  1272. if(ms1 > -1) switch(driver)
  1273. {
  1274. case 0: digitalWrite( X_MS1_PIN,ms1); break;
  1275. case 1: digitalWrite( Y_MS1_PIN,ms1); break;
  1276. case 2: digitalWrite( Z_MS1_PIN,ms1); break;
  1277. case 3: digitalWrite(E0_MS1_PIN,ms1); break;
  1278. #if defined(E1_MS1_PIN) && E1_MS1_PIN > -1
  1279. case 4: digitalWrite(E1_MS1_PIN,ms1); break;
  1280. #endif
  1281. }
  1282. if(ms2 > -1) switch(driver)
  1283. {
  1284. case 0: digitalWrite( X_MS2_PIN,ms2); break;
  1285. case 1: digitalWrite( Y_MS2_PIN,ms2); break;
  1286. case 2: digitalWrite( Z_MS2_PIN,ms2); break;
  1287. case 3: digitalWrite(E0_MS2_PIN,ms2); break;
  1288. #if defined(E1_MS2_PIN) && E1_MS2_PIN > -1
  1289. case 4: digitalWrite(E1_MS2_PIN,ms2); break;
  1290. #endif
  1291. }
  1292. }
  1293. void microstep_mode(uint8_t driver, uint8_t stepping_mode)
  1294. {
  1295. switch(stepping_mode)
  1296. {
  1297. case 1: microstep_ms(driver,MICROSTEP1); break;
  1298. case 2: microstep_ms(driver,MICROSTEP2); break;
  1299. case 4: microstep_ms(driver,MICROSTEP4); break;
  1300. case 8: microstep_ms(driver,MICROSTEP8); break;
  1301. case 16: microstep_ms(driver,MICROSTEP16); break;
  1302. }
  1303. }
  1304. void microstep_readings()
  1305. {
  1306. SERIAL_PROTOCOLPGM("MS1,MS2 Pins\n");
  1307. SERIAL_PROTOCOLPGM("X: ");
  1308. SERIAL_PROTOCOL( digitalRead(X_MS1_PIN));
  1309. SERIAL_PROTOCOLLN( digitalRead(X_MS2_PIN));
  1310. SERIAL_PROTOCOLPGM("Y: ");
  1311. SERIAL_PROTOCOL( digitalRead(Y_MS1_PIN));
  1312. SERIAL_PROTOCOLLN( digitalRead(Y_MS2_PIN));
  1313. SERIAL_PROTOCOLPGM("Z: ");
  1314. SERIAL_PROTOCOL( digitalRead(Z_MS1_PIN));
  1315. SERIAL_PROTOCOLLN( digitalRead(Z_MS2_PIN));
  1316. SERIAL_PROTOCOLPGM("E0: ");
  1317. SERIAL_PROTOCOL( digitalRead(E0_MS1_PIN));
  1318. SERIAL_PROTOCOLLN( digitalRead(E0_MS2_PIN));
  1319. #if defined(E1_MS1_PIN) && E1_MS1_PIN > -1
  1320. SERIAL_PROTOCOLPGM("E1: ");
  1321. SERIAL_PROTOCOL( digitalRead(E1_MS1_PIN));
  1322. SERIAL_PROTOCOLLN( digitalRead(E1_MS2_PIN));
  1323. #endif
  1324. }