stepper.cpp 50 KB

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