stepper.cpp 41 KB

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