stepper.cpp 41 KB

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