stepper.cpp 52 KB

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