stepper.cpp 46 KB

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