stepper.cpp 49 KB

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