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