stepper.cpp 53 KB

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