stepper.cpp 50 KB

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