stepper.cpp 42 KB

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