stepper.cpp 44 KB

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