stepper.cpp 44 KB

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