stepper.cpp 38 KB

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