stepper.cpp 48 KB

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