stepper.cpp 50 KB

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