stepper.cpp 44 KB

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