stepper.cpp 52 KB

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