stepper.cpp 50 KB

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