stepper.cpp 37 KB

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