stepper.cpp 35 KB

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