stepper.cpp 52 KB

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