stepper.cpp 38 KB

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