stepper.cpp 43 KB

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