stepper.cpp 38 KB

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