stepper.cpp 43 KB

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