stepper.cpp 48 KB

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