stepper.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  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. void tmc2130_write(uint8_t chipselect, uint8_t address,uint8_t wval1,uint8_t wval2,uint8_t wval3,uint8_t wval4)
  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 - write
  573. SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
  574. digitalWrite(chipselect,LOW);
  575. SPI.transfer(address+0x80);
  576. SPI.transfer(wval1);
  577. SPI.transfer(wval2);
  578. SPI.transfer(wval3);
  579. SPI.transfer(wval4);
  580. digitalWrite(chipselect, HIGH);
  581. SPI.endTransaction();
  582. //datagram2 - response
  583. SPI.beginTransaction(SPISettings(4000000, 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. MYSERIAL.print("WriteRead 0x");
  593. MYSERIAL.print(address,HEX);
  594. MYSERIAL.print(" Status:");
  595. MYSERIAL.print(val0 & 0b00000111,BIN);
  596. MYSERIAL.print(" ");
  597. MYSERIAL.print(val1,BIN);
  598. MYSERIAL.print(" ");
  599. MYSERIAL.print(val2,BIN);
  600. MYSERIAL.print(" ");
  601. MYSERIAL.print(val3,BIN);
  602. MYSERIAL.print(" ");
  603. MYSERIAL.print(val4,BIN);
  604. val32 = (uint32_t)val1<<24 | (uint32_t)val2<<16 | (uint32_t)val3<<8 | (uint32_t)val4;
  605. MYSERIAL.print(" 0x");
  606. MYSERIAL.println(val32,HEX);
  607. }
  608. uint8_t tmc2130_read8(uint8_t chipselect, uint8_t address){
  609. //datagram1 - write
  610. SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
  611. digitalWrite(chipselect,LOW);
  612. SPI.transfer(address);
  613. SPI.transfer(0x00);
  614. SPI.transfer(0x00);
  615. SPI.transfer(0x00);
  616. SPI.transfer(0x00);
  617. digitalWrite(chipselect, HIGH);
  618. SPI.endTransaction();
  619. uint8_t val0;
  620. //datagram2 - response
  621. SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
  622. digitalWrite(chipselect,LOW);
  623. val0 = SPI.transfer(0);
  624. digitalWrite(chipselect, HIGH);
  625. SPI.endTransaction();
  626. return val0;
  627. }
  628. void tmc2130_chopconf(uint8_t cs, bool extrapolate256 = 0, uint16_t microstep_resolution = 16)
  629. {
  630. uint8_t mres=0b0100;
  631. if(microstep_resolution == 256) mres = 0b0000;
  632. if(microstep_resolution == 128) mres = 0b0001;
  633. if(microstep_resolution == 64) mres = 0b0010;
  634. if(microstep_resolution == 32) mres = 0b0011;
  635. if(microstep_resolution == 16) mres = 0b0100;
  636. if(microstep_resolution == 8) mres = 0b0101;
  637. if(microstep_resolution == 4) mres = 0b0110;
  638. if(microstep_resolution == 2) mres = 0b0111;
  639. if(microstep_resolution == 1) mres = 0b1000;
  640. mres |= extrapolate256 << 4; //bit28 intpol
  641. //tmc2130_write(cs,0x6C,mres,0x01,0x00,0xD3);
  642. tmc2130_write(cs,0x6C,mres,0x01,0x00,0xC3);
  643. }
  644. 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)
  645. {
  646. tmc2130_write(cs,0x70,0x00,(PWMautoScale+PWMfreq),PWMgrad,PWMampl); // TMC LJ -> For better readability changed to 0x00 and added PWMautoScale and PWMfreq
  647. }
  648. void tmc2130_PWMthreshold(uint8_t cs)
  649. {
  650. tmc2130_write(cs,0x13,0x00,0x00,0x00,0x00); // TMC LJ -> Adds possibility to swtich from stealthChop to spreadCycle automatically
  651. }
  652. #endif //HAVE_TMC2130_DRIVERS
  653. void tmc2130_init()
  654. {
  655. #ifdef HAVE_TMC2130_DRIVERS
  656. uint8_t cs[4] = { X_TMC2130_CS, Y_TMC2130_CS, Z_TMC2130_CS, E0_TMC2130_CS };
  657. uint8_t current[4] = { 31, 31, 31, 31 };
  658. WRITE(X_TMC2130_CS, HIGH);
  659. WRITE(Y_TMC2130_CS, HIGH);
  660. WRITE(Z_TMC2130_CS, HIGH);
  661. WRITE(E0_TMC2130_CS, HIGH);
  662. SET_OUTPUT(X_TMC2130_CS);
  663. SET_OUTPUT(Y_TMC2130_CS);
  664. SET_OUTPUT(Z_TMC2130_CS);
  665. SET_OUTPUT(E0_TMC2130_CS);
  666. SPI.begin();
  667. for(int i=0;i<4;i++)
  668. {
  669. //tmc2130_write(cs[i],0x6C,0b10100,01,00,0xC5);
  670. tmc2130_chopconf(cs[i],1,16);
  671. tmc2130_write(cs[i],0x10,0,15,current[i],current[i]); //0x10 IHOLD_IRUN
  672. //tmc2130_write(cs[i],0x0,0,0,0,0x05); //address=0x0 GCONF EXT VREF
  673. tmc2130_write(cs[i],0x0,0,0,0,0x05); //address=0x0 GCONF EXT VREF - activate stealthChop
  674. //tmc2130_write(cs[i],0x11,0,0,0,0xA);
  675. // Uncomment lines below to use a different configuration (pwm_autoscale = 0) for XY axes
  676. // if(i==0 || i==1)
  677. // tmc2130_PWMconf(cs[i],PWM_AUTOSCALE_XY,PWM_FREQ_XY,PWM_GRAD_XY,PWM_AMPL_XY); //address=0x70 PWM_CONF //reset default=0x00050480
  678. // else
  679. tmc2130_PWMconf(cs[i]); //address=0x70 PWM_CONF //reset default=0x00050480
  680. tmc2130_PWMthreshold(cs[i]);
  681. }
  682. tmc2130_chopconf(cs[3],0,256);
  683. #endif
  684. }
  685. void st_init()
  686. {
  687. tmc2130_init(); //Initialize TMC2130 drivers
  688. digipot_init(); //Initialize Digipot Motor Current
  689. microstep_init(); //Initialize Microstepping Pins
  690. //Initialize Dir Pins
  691. #if defined(X_DIR_PIN) && X_DIR_PIN > -1
  692. SET_OUTPUT(X_DIR_PIN);
  693. #endif
  694. #if defined(X2_DIR_PIN) && X2_DIR_PIN > -1
  695. SET_OUTPUT(X2_DIR_PIN);
  696. #endif
  697. #if defined(Y_DIR_PIN) && Y_DIR_PIN > -1
  698. SET_OUTPUT(Y_DIR_PIN);
  699. #if defined(Y_DUAL_STEPPER_DRIVERS) && defined(Y2_DIR_PIN) && (Y2_DIR_PIN > -1)
  700. SET_OUTPUT(Y2_DIR_PIN);
  701. #endif
  702. #endif
  703. #if defined(Z_DIR_PIN) && Z_DIR_PIN > -1
  704. SET_OUTPUT(Z_DIR_PIN);
  705. #if defined(Z_DUAL_STEPPER_DRIVERS) && defined(Z2_DIR_PIN) && (Z2_DIR_PIN > -1)
  706. SET_OUTPUT(Z2_DIR_PIN);
  707. #endif
  708. #endif
  709. #if defined(E0_DIR_PIN) && E0_DIR_PIN > -1
  710. SET_OUTPUT(E0_DIR_PIN);
  711. #endif
  712. #if defined(E1_DIR_PIN) && (E1_DIR_PIN > -1)
  713. SET_OUTPUT(E1_DIR_PIN);
  714. #endif
  715. #if defined(E2_DIR_PIN) && (E2_DIR_PIN > -1)
  716. SET_OUTPUT(E2_DIR_PIN);
  717. #endif
  718. //Initialize Enable Pins - steppers default to disabled.
  719. #if defined(X_ENABLE_PIN) && X_ENABLE_PIN > -1
  720. SET_OUTPUT(X_ENABLE_PIN);
  721. if(!X_ENABLE_ON) WRITE(X_ENABLE_PIN,HIGH);
  722. #endif
  723. #if defined(X2_ENABLE_PIN) && X2_ENABLE_PIN > -1
  724. SET_OUTPUT(X2_ENABLE_PIN);
  725. if(!X_ENABLE_ON) WRITE(X2_ENABLE_PIN,HIGH);
  726. #endif
  727. #if defined(Y_ENABLE_PIN) && Y_ENABLE_PIN > -1
  728. SET_OUTPUT(Y_ENABLE_PIN);
  729. if(!Y_ENABLE_ON) WRITE(Y_ENABLE_PIN,HIGH);
  730. #if defined(Y_DUAL_STEPPER_DRIVERS) && defined(Y2_ENABLE_PIN) && (Y2_ENABLE_PIN > -1)
  731. SET_OUTPUT(Y2_ENABLE_PIN);
  732. if(!Y_ENABLE_ON) WRITE(Y2_ENABLE_PIN,HIGH);
  733. #endif
  734. #endif
  735. #if defined(Z_ENABLE_PIN) && Z_ENABLE_PIN > -1
  736. SET_OUTPUT(Z_ENABLE_PIN);
  737. if(!Z_ENABLE_ON) WRITE(Z_ENABLE_PIN,HIGH);
  738. #if defined(Z_DUAL_STEPPER_DRIVERS) && defined(Z2_ENABLE_PIN) && (Z2_ENABLE_PIN > -1)
  739. SET_OUTPUT(Z2_ENABLE_PIN);
  740. if(!Z_ENABLE_ON) WRITE(Z2_ENABLE_PIN,HIGH);
  741. #endif
  742. #endif
  743. #if defined(E0_ENABLE_PIN) && (E0_ENABLE_PIN > -1)
  744. SET_OUTPUT(E0_ENABLE_PIN);
  745. if(!E_ENABLE_ON) WRITE(E0_ENABLE_PIN,HIGH);
  746. #endif
  747. #if defined(E1_ENABLE_PIN) && (E1_ENABLE_PIN > -1)
  748. SET_OUTPUT(E1_ENABLE_PIN);
  749. if(!E_ENABLE_ON) WRITE(E1_ENABLE_PIN,HIGH);
  750. #endif
  751. #if defined(E2_ENABLE_PIN) && (E2_ENABLE_PIN > -1)
  752. SET_OUTPUT(E2_ENABLE_PIN);
  753. if(!E_ENABLE_ON) WRITE(E2_ENABLE_PIN,HIGH);
  754. #endif
  755. //endstops and pullups
  756. #if defined(X_MIN_PIN) && X_MIN_PIN > -1
  757. SET_INPUT(X_MIN_PIN);
  758. #ifdef ENDSTOPPULLUP_XMIN
  759. WRITE(X_MIN_PIN,HIGH);
  760. #endif
  761. #endif
  762. #if defined(Y_MIN_PIN) && Y_MIN_PIN > -1
  763. SET_INPUT(Y_MIN_PIN);
  764. #ifdef ENDSTOPPULLUP_YMIN
  765. WRITE(Y_MIN_PIN,HIGH);
  766. #endif
  767. #endif
  768. #if defined(Z_MIN_PIN) && Z_MIN_PIN > -1
  769. SET_INPUT(Z_MIN_PIN);
  770. #ifdef ENDSTOPPULLUP_ZMIN
  771. WRITE(Z_MIN_PIN,HIGH);
  772. #endif
  773. #endif
  774. #if defined(X_MAX_PIN) && X_MAX_PIN > -1
  775. SET_INPUT(X_MAX_PIN);
  776. #ifdef ENDSTOPPULLUP_XMAX
  777. WRITE(X_MAX_PIN,HIGH);
  778. #endif
  779. #endif
  780. #if defined(Y_MAX_PIN) && Y_MAX_PIN > -1
  781. SET_INPUT(Y_MAX_PIN);
  782. #ifdef ENDSTOPPULLUP_YMAX
  783. WRITE(Y_MAX_PIN,HIGH);
  784. #endif
  785. #endif
  786. #if defined(Z_MAX_PIN) && Z_MAX_PIN > -1
  787. SET_INPUT(Z_MAX_PIN);
  788. #ifdef ENDSTOPPULLUP_ZMAX
  789. WRITE(Z_MAX_PIN,HIGH);
  790. #endif
  791. #endif
  792. //Initialize Step Pins
  793. #if defined(X_STEP_PIN) && (X_STEP_PIN > -1)
  794. SET_OUTPUT(X_STEP_PIN);
  795. WRITE(X_STEP_PIN,INVERT_X_STEP_PIN);
  796. disable_x();
  797. #endif
  798. #if defined(X2_STEP_PIN) && (X2_STEP_PIN > -1)
  799. SET_OUTPUT(X2_STEP_PIN);
  800. WRITE(X2_STEP_PIN,INVERT_X_STEP_PIN);
  801. disable_x();
  802. #endif
  803. #if defined(Y_STEP_PIN) && (Y_STEP_PIN > -1)
  804. SET_OUTPUT(Y_STEP_PIN);
  805. WRITE(Y_STEP_PIN,INVERT_Y_STEP_PIN);
  806. #if defined(Y_DUAL_STEPPER_DRIVERS) && defined(Y2_STEP_PIN) && (Y2_STEP_PIN > -1)
  807. SET_OUTPUT(Y2_STEP_PIN);
  808. WRITE(Y2_STEP_PIN,INVERT_Y_STEP_PIN);
  809. #endif
  810. disable_y();
  811. #endif
  812. #if defined(Z_STEP_PIN) && (Z_STEP_PIN > -1)
  813. SET_OUTPUT(Z_STEP_PIN);
  814. WRITE(Z_STEP_PIN,INVERT_Z_STEP_PIN);
  815. #if defined(Z_DUAL_STEPPER_DRIVERS) && defined(Z2_STEP_PIN) && (Z2_STEP_PIN > -1)
  816. SET_OUTPUT(Z2_STEP_PIN);
  817. WRITE(Z2_STEP_PIN,INVERT_Z_STEP_PIN);
  818. #endif
  819. disable_z();
  820. #endif
  821. #if defined(E0_STEP_PIN) && (E0_STEP_PIN > -1)
  822. SET_OUTPUT(E0_STEP_PIN);
  823. WRITE(E0_STEP_PIN,INVERT_E_STEP_PIN);
  824. disable_e0();
  825. #endif
  826. #if defined(E1_STEP_PIN) && (E1_STEP_PIN > -1)
  827. SET_OUTPUT(E1_STEP_PIN);
  828. WRITE(E1_STEP_PIN,INVERT_E_STEP_PIN);
  829. disable_e1();
  830. #endif
  831. #if defined(E2_STEP_PIN) && (E2_STEP_PIN > -1)
  832. SET_OUTPUT(E2_STEP_PIN);
  833. WRITE(E2_STEP_PIN,INVERT_E_STEP_PIN);
  834. disable_e2();
  835. #endif
  836. // waveform generation = 0100 = CTC
  837. TCCR1B &= ~(1<<WGM13);
  838. TCCR1B |= (1<<WGM12);
  839. TCCR1A &= ~(1<<WGM11);
  840. TCCR1A &= ~(1<<WGM10);
  841. // output mode = 00 (disconnected)
  842. TCCR1A &= ~(3<<COM1A0);
  843. TCCR1A &= ~(3<<COM1B0);
  844. // Set the timer pre-scaler
  845. // Generally we use a divider of 8, resulting in a 2MHz timer
  846. // frequency on a 16MHz MCU. If you are going to change this, be
  847. // sure to regenerate speed_lookuptable.h with
  848. // create_speed_lookuptable.py
  849. TCCR1B = (TCCR1B & ~(0x07<<CS10)) | (2<<CS10);
  850. OCR1A = 0x4000;
  851. TCNT1 = 0;
  852. ENABLE_STEPPER_DRIVER_INTERRUPT();
  853. enable_endstops(true); // Start with endstops active. After homing they can be disabled
  854. sei();
  855. }
  856. // Block until all buffered steps are executed
  857. void st_synchronize()
  858. {
  859. while( blocks_queued()) {
  860. manage_heater();
  861. // Vojtech: Don't disable motors inside the planner!
  862. manage_inactivity(true);
  863. lcd_update();
  864. }
  865. }
  866. void st_set_position(const long &x, const long &y, const long &z, const long &e)
  867. {
  868. CRITICAL_SECTION_START;
  869. count_position[X_AXIS] = x;
  870. count_position[Y_AXIS] = y;
  871. count_position[Z_AXIS] = z;
  872. count_position[E_AXIS] = e;
  873. CRITICAL_SECTION_END;
  874. }
  875. void st_set_e_position(const long &e)
  876. {
  877. CRITICAL_SECTION_START;
  878. count_position[E_AXIS] = e;
  879. CRITICAL_SECTION_END;
  880. }
  881. long st_get_position(uint8_t axis)
  882. {
  883. long count_pos;
  884. CRITICAL_SECTION_START;
  885. count_pos = count_position[axis];
  886. CRITICAL_SECTION_END;
  887. return count_pos;
  888. }
  889. float st_get_position_mm(uint8_t axis)
  890. {
  891. float steper_position_in_steps = st_get_position(axis);
  892. return steper_position_in_steps / axis_steps_per_unit[axis];
  893. }
  894. void finishAndDisableSteppers()
  895. {
  896. st_synchronize();
  897. disable_x();
  898. disable_y();
  899. disable_z();
  900. disable_e0();
  901. disable_e1();
  902. disable_e2();
  903. }
  904. void quickStop()
  905. {
  906. DISABLE_STEPPER_DRIVER_INTERRUPT();
  907. while (blocks_queued()) plan_discard_current_block();
  908. current_block = NULL;
  909. ENABLE_STEPPER_DRIVER_INTERRUPT();
  910. }
  911. #ifdef BABYSTEPPING
  912. void babystep(const uint8_t axis,const bool direction)
  913. {
  914. //MUST ONLY BE CALLED BY A ISR, it depends on that no other ISR interrupts this
  915. //store initial pin states
  916. switch(axis)
  917. {
  918. case X_AXIS:
  919. {
  920. enable_x();
  921. uint8_t old_x_dir_pin= READ(X_DIR_PIN); //if dualzstepper, both point to same direction.
  922. //setup new step
  923. WRITE(X_DIR_PIN,(INVERT_X_DIR)^direction);
  924. //perform step
  925. WRITE(X_STEP_PIN, !INVERT_X_STEP_PIN);
  926. {
  927. volatile float x=1./float(axis+1)/float(axis+2); //wait a tiny bit
  928. }
  929. WRITE(X_STEP_PIN, INVERT_X_STEP_PIN);
  930. //get old pin state back.
  931. WRITE(X_DIR_PIN,old_x_dir_pin);
  932. }
  933. break;
  934. case Y_AXIS:
  935. {
  936. enable_y();
  937. uint8_t old_y_dir_pin= READ(Y_DIR_PIN); //if dualzstepper, both point to same direction.
  938. //setup new step
  939. WRITE(Y_DIR_PIN,(INVERT_Y_DIR)^direction);
  940. //perform step
  941. WRITE(Y_STEP_PIN, !INVERT_Y_STEP_PIN);
  942. {
  943. volatile float x=1./float(axis+1)/float(axis+2); //wait a tiny bit
  944. }
  945. WRITE(Y_STEP_PIN, INVERT_Y_STEP_PIN);
  946. //get old pin state back.
  947. WRITE(Y_DIR_PIN,old_y_dir_pin);
  948. }
  949. break;
  950. case Z_AXIS:
  951. {
  952. enable_z();
  953. uint8_t old_z_dir_pin= READ(Z_DIR_PIN); //if dualzstepper, both point to same direction.
  954. //setup new step
  955. WRITE(Z_DIR_PIN,(INVERT_Z_DIR)^direction^BABYSTEP_INVERT_Z);
  956. #ifdef Z_DUAL_STEPPER_DRIVERS
  957. WRITE(Z2_DIR_PIN,(INVERT_Z_DIR)^direction^BABYSTEP_INVERT_Z);
  958. #endif
  959. //perform step
  960. WRITE(Z_STEP_PIN, !INVERT_Z_STEP_PIN);
  961. #ifdef Z_DUAL_STEPPER_DRIVERS
  962. WRITE(Z2_STEP_PIN, !INVERT_Z_STEP_PIN);
  963. #endif
  964. //wait a tiny bit
  965. {
  966. volatile float x=1./float(axis+1); //absolutely useless
  967. }
  968. WRITE(Z_STEP_PIN, INVERT_Z_STEP_PIN);
  969. #ifdef Z_DUAL_STEPPER_DRIVERS
  970. WRITE(Z2_STEP_PIN, INVERT_Z_STEP_PIN);
  971. #endif
  972. //get old pin state back.
  973. WRITE(Z_DIR_PIN,old_z_dir_pin);
  974. #ifdef Z_DUAL_STEPPER_DRIVERS
  975. WRITE(Z2_DIR_PIN,old_z_dir_pin);
  976. #endif
  977. }
  978. break;
  979. default: break;
  980. }
  981. }
  982. #endif //BABYSTEPPING
  983. void digitalPotWrite(int address, int value) // From Arduino DigitalPotControl example
  984. {
  985. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  986. digitalWrite(DIGIPOTSS_PIN,LOW); // take the SS pin low to select the chip
  987. SPI.transfer(address); // send in the address and value via SPI:
  988. SPI.transfer(value);
  989. digitalWrite(DIGIPOTSS_PIN,HIGH); // take the SS pin high to de-select the chip:
  990. //delay(10);
  991. #endif
  992. }
  993. void EEPROM_read_st(int pos, uint8_t* value, uint8_t size)
  994. {
  995. do
  996. {
  997. *value = eeprom_read_byte((unsigned char*)pos);
  998. pos++;
  999. value++;
  1000. }while(--size);
  1001. }
  1002. void digipot_init() //Initialize Digipot Motor Current
  1003. {
  1004. EEPROM_read_st(EEPROM_SILENT,(uint8_t*)&SilentMode,sizeof(SilentMode));
  1005. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  1006. if(SilentMode == 0){
  1007. const uint8_t digipot_motor_current[] = DIGIPOT_MOTOR_CURRENT_LOUD;
  1008. }else{
  1009. const uint8_t digipot_motor_current[] = DIGIPOT_MOTOR_CURRENT;
  1010. }
  1011. SPI.begin();
  1012. pinMode(DIGIPOTSS_PIN, OUTPUT);
  1013. for(int i=0;i<=4;i++)
  1014. //digitalPotWrite(digipot_ch[i], digipot_motor_current[i]);
  1015. digipot_current(i,digipot_motor_current[i]);
  1016. #endif
  1017. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  1018. pinMode(MOTOR_CURRENT_PWM_XY_PIN, OUTPUT);
  1019. pinMode(MOTOR_CURRENT_PWM_Z_PIN, OUTPUT);
  1020. pinMode(MOTOR_CURRENT_PWM_E_PIN, OUTPUT);
  1021. if((SilentMode == 0) || (farm_mode) ){
  1022. motor_current_setting[0] = motor_current_setting_loud[0];
  1023. motor_current_setting[1] = motor_current_setting_loud[1];
  1024. motor_current_setting[2] = motor_current_setting_loud[2];
  1025. }else{
  1026. motor_current_setting[0] = motor_current_setting_silent[0];
  1027. motor_current_setting[1] = motor_current_setting_silent[1];
  1028. motor_current_setting[2] = motor_current_setting_silent[2];
  1029. }
  1030. digipot_current(0, motor_current_setting[0]);
  1031. digipot_current(1, motor_current_setting[1]);
  1032. digipot_current(2, motor_current_setting[2]);
  1033. //Set timer5 to 31khz so the PWM of the motor power is as constant as possible. (removes a buzzing noise)
  1034. TCCR5B = (TCCR5B & ~(_BV(CS50) | _BV(CS51) | _BV(CS52))) | _BV(CS50);
  1035. #endif
  1036. }
  1037. void digipot_current(uint8_t driver, int current)
  1038. {
  1039. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  1040. const uint8_t digipot_ch[] = DIGIPOT_CHANNELS;
  1041. digitalPotWrite(digipot_ch[driver], current);
  1042. #endif
  1043. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  1044. if (driver == 0) analogWrite(MOTOR_CURRENT_PWM_XY_PIN, (long)current * 255L / (long)MOTOR_CURRENT_PWM_RANGE);
  1045. if (driver == 1) analogWrite(MOTOR_CURRENT_PWM_Z_PIN, (long)current * 255L / (long)MOTOR_CURRENT_PWM_RANGE);
  1046. if (driver == 2) analogWrite(MOTOR_CURRENT_PWM_E_PIN, (long)current * 255L / (long)MOTOR_CURRENT_PWM_RANGE);
  1047. #endif
  1048. }
  1049. void microstep_init()
  1050. {
  1051. const uint8_t microstep_modes[] = MICROSTEP_MODES;
  1052. #if defined(E1_MS1_PIN) && E1_MS1_PIN > -1
  1053. pinMode(E1_MS1_PIN,OUTPUT);
  1054. pinMode(E1_MS2_PIN,OUTPUT);
  1055. #endif
  1056. #if defined(X_MS1_PIN) && X_MS1_PIN > -1
  1057. pinMode(X_MS1_PIN,OUTPUT);
  1058. pinMode(X_MS2_PIN,OUTPUT);
  1059. pinMode(Y_MS1_PIN,OUTPUT);
  1060. pinMode(Y_MS2_PIN,OUTPUT);
  1061. pinMode(Z_MS1_PIN,OUTPUT);
  1062. pinMode(Z_MS2_PIN,OUTPUT);
  1063. pinMode(E0_MS1_PIN,OUTPUT);
  1064. pinMode(E0_MS2_PIN,OUTPUT);
  1065. for(int i=0;i<=4;i++) microstep_mode(i,microstep_modes[i]);
  1066. #endif
  1067. }
  1068. void microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2)
  1069. {
  1070. if(ms1 > -1) switch(driver)
  1071. {
  1072. case 0: digitalWrite( X_MS1_PIN,ms1); break;
  1073. case 1: digitalWrite( Y_MS1_PIN,ms1); break;
  1074. case 2: digitalWrite( Z_MS1_PIN,ms1); break;
  1075. case 3: digitalWrite(E0_MS1_PIN,ms1); break;
  1076. #if defined(E1_MS1_PIN) && E1_MS1_PIN > -1
  1077. case 4: digitalWrite(E1_MS1_PIN,ms1); break;
  1078. #endif
  1079. }
  1080. if(ms2 > -1) switch(driver)
  1081. {
  1082. case 0: digitalWrite( X_MS2_PIN,ms2); break;
  1083. case 1: digitalWrite( Y_MS2_PIN,ms2); break;
  1084. case 2: digitalWrite( Z_MS2_PIN,ms2); break;
  1085. case 3: digitalWrite(E0_MS2_PIN,ms2); break;
  1086. #if defined(E1_MS2_PIN) && E1_MS2_PIN > -1
  1087. case 4: digitalWrite(E1_MS2_PIN,ms2); break;
  1088. #endif
  1089. }
  1090. }
  1091. void microstep_mode(uint8_t driver, uint8_t stepping_mode)
  1092. {
  1093. switch(stepping_mode)
  1094. {
  1095. case 1: microstep_ms(driver,MICROSTEP1); break;
  1096. case 2: microstep_ms(driver,MICROSTEP2); break;
  1097. case 4: microstep_ms(driver,MICROSTEP4); break;
  1098. case 8: microstep_ms(driver,MICROSTEP8); break;
  1099. case 16: microstep_ms(driver,MICROSTEP16); break;
  1100. }
  1101. }
  1102. void microstep_readings()
  1103. {
  1104. SERIAL_PROTOCOLPGM("MS1,MS2 Pins\n");
  1105. SERIAL_PROTOCOLPGM("X: ");
  1106. SERIAL_PROTOCOL( digitalRead(X_MS1_PIN));
  1107. SERIAL_PROTOCOLLN( digitalRead(X_MS2_PIN));
  1108. SERIAL_PROTOCOLPGM("Y: ");
  1109. SERIAL_PROTOCOL( digitalRead(Y_MS1_PIN));
  1110. SERIAL_PROTOCOLLN( digitalRead(Y_MS2_PIN));
  1111. SERIAL_PROTOCOLPGM("Z: ");
  1112. SERIAL_PROTOCOL( digitalRead(Z_MS1_PIN));
  1113. SERIAL_PROTOCOLLN( digitalRead(Z_MS2_PIN));
  1114. SERIAL_PROTOCOLPGM("E0: ");
  1115. SERIAL_PROTOCOL( digitalRead(E0_MS1_PIN));
  1116. SERIAL_PROTOCOLLN( digitalRead(E0_MS2_PIN));
  1117. #if defined(E1_MS1_PIN) && E1_MS1_PIN > -1
  1118. SERIAL_PROTOCOLPGM("E1: ");
  1119. SERIAL_PROTOCOL( digitalRead(E1_MS1_PIN));
  1120. SERIAL_PROTOCOLLN( digitalRead(E1_MS2_PIN));
  1121. #endif
  1122. }