stepper.cpp 35 KB

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