stepper.cpp 44 KB

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