stepper.cpp 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632
  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 TMC2130
  30. #include "tmc2130.h"
  31. #endif //TMC2130
  32. #ifdef FILAMENT_SENSOR
  33. #include "fsensor.h"
  34. int fsensor_counter = 0; //counter for e-steps
  35. #endif //FILAMENT_SENSOR
  36. #ifdef DEBUG_STACK_MONITOR
  37. uint16_t SP_min = 0x21FF;
  38. #endif //DEBUG_STACK_MONITOR
  39. //===========================================================================
  40. //=============================public variables ============================
  41. //===========================================================================
  42. block_t *current_block; // A pointer to the block currently being traced
  43. bool x_min_endstop = false;
  44. bool x_max_endstop = false;
  45. bool y_min_endstop = false;
  46. bool y_max_endstop = false;
  47. bool z_min_endstop = false;
  48. bool z_max_endstop = false;
  49. //===========================================================================
  50. //=============================private variables ============================
  51. //===========================================================================
  52. //static makes it inpossible to be called from outside of this file by extern.!
  53. // Variables used by The Stepper Driver Interrupt
  54. static unsigned char out_bits; // The next stepping-bits to be output
  55. static dda_isteps_t
  56. counter_x, // Counter variables for the bresenham line tracer
  57. counter_y,
  58. counter_z,
  59. counter_e;
  60. volatile dda_usteps_t step_events_completed; // The number of step events executed in the current block
  61. static int32_t acceleration_time, deceleration_time;
  62. //static unsigned long accelerate_until, decelerate_after, acceleration_rate, initial_rate, final_rate, nominal_rate;
  63. static uint16_t acc_step_rate; // needed for deccelaration start point
  64. static uint8_t step_loops;
  65. static uint16_t OCR1A_nominal;
  66. static uint8_t step_loops_nominal;
  67. volatile long endstops_trigsteps[3]={0,0,0};
  68. volatile long endstops_stepsTotal,endstops_stepsDone;
  69. static volatile bool endstop_x_hit=false;
  70. static volatile bool endstop_y_hit=false;
  71. static volatile bool endstop_z_hit=false;
  72. #ifdef ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED
  73. bool abort_on_endstop_hit = false;
  74. #endif
  75. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  76. int motor_current_setting[3] = DEFAULT_PWM_MOTOR_CURRENT;
  77. int motor_current_setting_silent[3] = DEFAULT_PWM_MOTOR_CURRENT;
  78. int motor_current_setting_loud[3] = DEFAULT_PWM_MOTOR_CURRENT_LOUD;
  79. #endif
  80. static bool old_x_min_endstop=false;
  81. static bool old_x_max_endstop=false;
  82. static bool old_y_min_endstop=false;
  83. static bool old_y_max_endstop=false;
  84. static bool old_z_min_endstop=false;
  85. static bool old_z_max_endstop=false;
  86. static bool check_endstops = true;
  87. static bool check_z_endstop = false;
  88. static bool z_endstop_invert = false;
  89. volatile long count_position[NUM_AXIS] = { 0, 0, 0, 0};
  90. volatile signed char count_direction[NUM_AXIS] = { 1, 1, 1, 1};
  91. #ifdef LIN_ADVANCE
  92. static uint16_t nextMainISR = 0;
  93. static uint16_t eISR_Rate;
  94. // Extrusion steps to be executed by the stepper.
  95. // If set to non zero, the timer ISR routine will tick the Linear Advance extruder ticks first.
  96. // If e_steps is zero, then the timer ISR routine will perform the usual DDA step.
  97. static volatile int16_t e_steps = 0;
  98. // How many extruder steps shall be ticked at a single ISR invocation?
  99. static uint8_t estep_loops;
  100. // The current speed of the extruder, scaled by the linear advance constant, so it has the same measure
  101. // as current_adv_steps.
  102. static int current_estep_rate;
  103. // The current pretension of filament expressed in extruder micro steps.
  104. static int current_adv_steps;
  105. #define _NEXT_ISR(T) nextMainISR = T
  106. #else
  107. #define _NEXT_ISR(T) OCR1A = T
  108. #endif
  109. #ifdef DEBUG_STEPPER_TIMER_MISSED
  110. extern bool stepper_timer_overflow_state;
  111. extern uint16_t stepper_timer_overflow_last;
  112. #endif /* DEBUG_STEPPER_TIMER_MISSED */
  113. //===========================================================================
  114. //=============================functions ============================
  115. //===========================================================================
  116. #ifndef _NO_ASM
  117. // intRes = intIn1 * intIn2 >> 16
  118. // uses:
  119. // r26 to store 0
  120. // r27 to store the byte 1 of the 24 bit result
  121. #define MultiU16X8toH16(intRes, charIn1, intIn2) \
  122. asm volatile ( \
  123. "clr r26 \n\t" \
  124. "mul %A1, %B2 \n\t" \
  125. "movw %A0, r0 \n\t" \
  126. "mul %A1, %A2 \n\t" \
  127. "add %A0, r1 \n\t" \
  128. "adc %B0, r26 \n\t" \
  129. "lsr r0 \n\t" \
  130. "adc %A0, r26 \n\t" \
  131. "adc %B0, r26 \n\t" \
  132. "clr r1 \n\t" \
  133. : \
  134. "=&r" (intRes) \
  135. : \
  136. "d" (charIn1), \
  137. "d" (intIn2) \
  138. : \
  139. "r26" \
  140. )
  141. // intRes = longIn1 * longIn2 >> 24
  142. // uses:
  143. // r26 to store 0
  144. // r27 to store the byte 1 of the 48bit result
  145. #define MultiU24X24toH16(intRes, longIn1, longIn2) \
  146. asm volatile ( \
  147. "clr r26 \n\t" \
  148. "mul %A1, %B2 \n\t" \
  149. "mov r27, r1 \n\t" \
  150. "mul %B1, %C2 \n\t" \
  151. "movw %A0, r0 \n\t" \
  152. "mul %C1, %C2 \n\t" \
  153. "add %B0, r0 \n\t" \
  154. "mul %C1, %B2 \n\t" \
  155. "add %A0, r0 \n\t" \
  156. "adc %B0, r1 \n\t" \
  157. "mul %A1, %C2 \n\t" \
  158. "add r27, r0 \n\t" \
  159. "adc %A0, r1 \n\t" \
  160. "adc %B0, r26 \n\t" \
  161. "mul %B1, %B2 \n\t" \
  162. "add r27, r0 \n\t" \
  163. "adc %A0, r1 \n\t" \
  164. "adc %B0, r26 \n\t" \
  165. "mul %C1, %A2 \n\t" \
  166. "add r27, r0 \n\t" \
  167. "adc %A0, r1 \n\t" \
  168. "adc %B0, r26 \n\t" \
  169. "mul %B1, %A2 \n\t" \
  170. "add r27, r1 \n\t" \
  171. "adc %A0, r26 \n\t" \
  172. "adc %B0, r26 \n\t" \
  173. "lsr r27 \n\t" \
  174. "adc %A0, r26 \n\t" \
  175. "adc %B0, r26 \n\t" \
  176. "clr r1 \n\t" \
  177. : \
  178. "=&r" (intRes) \
  179. : \
  180. "d" (longIn1), \
  181. "d" (longIn2) \
  182. : \
  183. "r26" , "r27" \
  184. )
  185. #else //_NO_ASM
  186. void MultiU16X8toH16(unsigned short& intRes, unsigned char& charIn1, unsigned short& intIn2)
  187. {
  188. }
  189. void MultiU24X24toH16(uint16_t& intRes, int32_t& longIn1, long& longIn2)
  190. {
  191. }
  192. #endif //_NO_ASM
  193. // Some useful constants
  194. void checkHitEndstops()
  195. {
  196. if( endstop_x_hit || endstop_y_hit || endstop_z_hit) {
  197. SERIAL_ECHO_START;
  198. SERIAL_ECHORPGM(_T(MSG_ENDSTOPS_HIT));
  199. if(endstop_x_hit) {
  200. SERIAL_ECHOPAIR(" X:",(float)endstops_trigsteps[X_AXIS]/axis_steps_per_unit[X_AXIS]);
  201. LCD_MESSAGERPGM(CAT2(_T(MSG_ENDSTOPS_HIT), PSTR("X")));
  202. }
  203. if(endstop_y_hit) {
  204. SERIAL_ECHOPAIR(" Y:",(float)endstops_trigsteps[Y_AXIS]/axis_steps_per_unit[Y_AXIS]);
  205. LCD_MESSAGERPGM(CAT2(_T(MSG_ENDSTOPS_HIT), PSTR("Y")));
  206. }
  207. if(endstop_z_hit) {
  208. SERIAL_ECHOPAIR(" Z:",(float)endstops_trigsteps[Z_AXIS]/axis_steps_per_unit[Z_AXIS]);
  209. LCD_MESSAGERPGM(CAT2(_T(MSG_ENDSTOPS_HIT),PSTR("Z")));
  210. }
  211. SERIAL_ECHOLN("");
  212. endstop_x_hit=false;
  213. endstop_y_hit=false;
  214. endstop_z_hit=false;
  215. #if defined(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED) && defined(SDSUPPORT)
  216. if (abort_on_endstop_hit)
  217. {
  218. card.sdprinting = false;
  219. card.closefile();
  220. quickStop();
  221. setTargetHotend0(0);
  222. setTargetHotend1(0);
  223. setTargetHotend2(0);
  224. }
  225. #endif
  226. }
  227. }
  228. bool endstops_hit_on_purpose()
  229. {
  230. bool hit = endstop_x_hit || endstop_y_hit || endstop_z_hit;
  231. endstop_x_hit=false;
  232. endstop_y_hit=false;
  233. endstop_z_hit=false;
  234. return hit;
  235. }
  236. bool endstop_z_hit_on_purpose()
  237. {
  238. bool hit = endstop_z_hit;
  239. endstop_z_hit=false;
  240. return hit;
  241. }
  242. bool enable_endstops(bool check)
  243. {
  244. bool old = check_endstops;
  245. check_endstops = check;
  246. return old;
  247. }
  248. bool enable_z_endstop(bool check)
  249. {
  250. bool old = check_z_endstop;
  251. check_z_endstop = check;
  252. endstop_z_hit = false;
  253. return old;
  254. }
  255. void invert_z_endstop(bool endstop_invert)
  256. {
  257. z_endstop_invert = endstop_invert;
  258. }
  259. // __________________________
  260. // /| |\ _________________ ^
  261. // / | | \ /| |\ |
  262. // / | | \ / | | \ s
  263. // / | | | | | \ p
  264. // / | | | | | \ e
  265. // +-----+------------------------+---+--+---------------+----+ e
  266. // | BLOCK 1 | BLOCK 2 | d
  267. //
  268. // time ----->
  269. //
  270. // The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates
  271. // first block->accelerate_until step_events_completed, then keeps going at constant speed until
  272. // step_events_completed reaches block->decelerate_after after which it decelerates until the trapezoid generator is reset.
  273. // The slope of acceleration is calculated with the leib ramp alghorithm.
  274. FORCE_INLINE unsigned short calc_timer(uint16_t step_rate) {
  275. unsigned short timer;
  276. if(step_rate > MAX_STEP_FREQUENCY) step_rate = MAX_STEP_FREQUENCY;
  277. if(step_rate > 20000) { // If steprate > 20kHz >> step 4 times
  278. step_rate = (step_rate >> 2)&0x3fff;
  279. step_loops = 4;
  280. }
  281. else if(step_rate > 10000) { // If steprate > 10kHz >> step 2 times
  282. step_rate = (step_rate >> 1)&0x7fff;
  283. step_loops = 2;
  284. }
  285. else {
  286. step_loops = 1;
  287. }
  288. // step_loops = 1;
  289. if(step_rate < (F_CPU/500000)) step_rate = (F_CPU/500000);
  290. step_rate -= (F_CPU/500000); // Correct for minimal speed
  291. if(step_rate >= (8*256)){ // higher step rate
  292. unsigned short table_address = (unsigned short)&speed_lookuptable_fast[(unsigned char)(step_rate>>8)][0];
  293. unsigned char tmp_step_rate = (step_rate & 0x00ff);
  294. unsigned short gain = (unsigned short)pgm_read_word_near(table_address+2);
  295. MultiU16X8toH16(timer, tmp_step_rate, gain);
  296. timer = (unsigned short)pgm_read_word_near(table_address) - timer;
  297. }
  298. else { // lower step rates
  299. unsigned short table_address = (unsigned short)&speed_lookuptable_slow[0][0];
  300. table_address += ((step_rate)>>1) & 0xfffc;
  301. timer = (unsigned short)pgm_read_word_near(table_address);
  302. timer -= (((unsigned short)pgm_read_word_near(table_address+2) * (unsigned char)(step_rate & 0x0007))>>3);
  303. }
  304. if(timer < 100) { timer = 100; MYSERIAL.print(_i("Steprate too high: ")); MYSERIAL.println(step_rate); }//(20kHz this should never happen)////MSG_STEPPER_TOO_HIGH c=0 r=0
  305. return timer;
  306. }
  307. // "The Stepper Driver Interrupt" - This timer interrupt is the workhorse.
  308. // It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
  309. ISR(TIMER1_COMPA_vect) {
  310. #ifdef DEBUG_STACK_MONITOR
  311. uint16_t sp = SPL + 256 * SPH;
  312. if (sp < SP_min) SP_min = sp;
  313. #endif //DEBUG_STACK_MONITOR
  314. #ifdef LIN_ADVANCE
  315. // If there are any e_steps planned, tick them.
  316. bool run_main_isr = false;
  317. if (e_steps) {
  318. //WRITE_NC(LOGIC_ANALYZER_CH7, true);
  319. for (uint8_t i = estep_loops; e_steps && i --;) {
  320. WRITE_NC(E0_STEP_PIN, !INVERT_E_STEP_PIN);
  321. -- e_steps;
  322. WRITE_NC(E0_STEP_PIN, INVERT_E_STEP_PIN);
  323. }
  324. if (e_steps) {
  325. // Plan another Linear Advance tick.
  326. OCR1A = eISR_Rate;
  327. nextMainISR -= eISR_Rate;
  328. } else if (! (nextMainISR & 0x8000) || nextMainISR < 16) {
  329. // The timer did not overflow and it is big enough, so it makes sense to plan it.
  330. OCR1A = nextMainISR;
  331. } else {
  332. // The timer has overflown, or it is too small. Run the main ISR just after the Linear Advance routine
  333. // in the current interrupt tick.
  334. run_main_isr = true;
  335. //FIXME pick the serial line.
  336. }
  337. //WRITE_NC(LOGIC_ANALYZER_CH7, false);
  338. } else
  339. run_main_isr = true;
  340. if (run_main_isr)
  341. #endif
  342. isr();
  343. // Don't run the ISR faster than possible
  344. // Is there a 8us time left before the next interrupt triggers?
  345. if (OCR1A < TCNT1 + 16) {
  346. #ifdef DEBUG_STEPPER_TIMER_MISSED
  347. // Verify whether the next planned timer interrupt has not been missed already.
  348. // This debugging test takes < 1.125us
  349. // This skews the profiling slightly as the fastest stepper timer
  350. // interrupt repeats at a 100us rate (10kHz).
  351. if (OCR1A + 40 < TCNT1) {
  352. // The interrupt was delayed by more than 20us (which is 1/5th of the 10kHz ISR repeat rate).
  353. // Give a warning.
  354. stepper_timer_overflow_state = true;
  355. stepper_timer_overflow_last = TCNT1 - OCR1A;
  356. // Beep, the beeper will be cleared at the stepper_timer_overflow() called from the main thread.
  357. WRITE(BEEPER, HIGH);
  358. }
  359. #endif
  360. // Fix the next interrupt to be executed after 8us from now.
  361. OCR1A = TCNT1 + 16;
  362. }
  363. }
  364. uint8_t last_dir_bits = 0;
  365. #ifdef BACKLASH_X
  366. uint8_t st_backlash_x = 0;
  367. #endif //BACKLASH_X
  368. #ifdef BACKLASH_Y
  369. uint8_t st_backlash_y = 0;
  370. #endif //BACKLASH_Y
  371. FORCE_INLINE void stepper_next_block()
  372. {
  373. // Anything in the buffer?
  374. //WRITE_NC(LOGIC_ANALYZER_CH2, true);
  375. current_block = plan_get_current_block();
  376. if (current_block != NULL) {
  377. #ifdef BACKLASH_X
  378. if (current_block->steps_x.wide)
  379. { //X-axis movement
  380. if ((current_block->direction_bits ^ last_dir_bits) & 1)
  381. {
  382. printf_P(PSTR("BL %d\n"), (current_block->direction_bits & 1)?st_backlash_x:-st_backlash_x);
  383. if (current_block->direction_bits & 1)
  384. WRITE_NC(X_DIR_PIN, INVERT_X_DIR);
  385. else
  386. WRITE_NC(X_DIR_PIN, !INVERT_X_DIR);
  387. _delay_us(100);
  388. for (uint8_t i = 0; i < st_backlash_x; i++)
  389. {
  390. WRITE_NC(X_STEP_PIN, !INVERT_X_STEP_PIN);
  391. _delay_us(100);
  392. WRITE_NC(X_STEP_PIN, INVERT_X_STEP_PIN);
  393. _delay_us(900);
  394. }
  395. }
  396. last_dir_bits &= ~1;
  397. last_dir_bits |= current_block->direction_bits & 1;
  398. }
  399. #endif
  400. #ifdef BACKLASH_Y
  401. if (current_block->steps_y.wide)
  402. { //Y-axis movement
  403. if ((current_block->direction_bits ^ last_dir_bits) & 2)
  404. {
  405. printf_P(PSTR("BL %d\n"), (current_block->direction_bits & 2)?st_backlash_y:-st_backlash_y);
  406. if (current_block->direction_bits & 2)
  407. WRITE_NC(Y_DIR_PIN, INVERT_Y_DIR);
  408. else
  409. WRITE_NC(Y_DIR_PIN, !INVERT_Y_DIR);
  410. _delay_us(100);
  411. for (uint8_t i = 0; i < st_backlash_y; i++)
  412. {
  413. WRITE_NC(Y_STEP_PIN, !INVERT_Y_STEP_PIN);
  414. _delay_us(100);
  415. WRITE_NC(Y_STEP_PIN, INVERT_Y_STEP_PIN);
  416. _delay_us(900);
  417. }
  418. }
  419. last_dir_bits &= ~2;
  420. last_dir_bits |= current_block->direction_bits & 2;
  421. }
  422. #endif
  423. #ifdef FILAMENT_SENSOR
  424. fsensor_counter = 0;
  425. fsensor_st_block_begin(current_block);
  426. #endif //FILAMENT_SENSOR
  427. // The busy flag is set by the plan_get_current_block() call.
  428. // current_block->busy = true;
  429. // Initializes the trapezoid generator from the current block. Called whenever a new
  430. // block begins.
  431. deceleration_time = 0;
  432. // Set the nominal step loops to zero to indicate, that the timer value is not known yet.
  433. // That means, delay the initialization of nominal step rate and step loops until the steady
  434. // state is reached.
  435. step_loops_nominal = 0;
  436. acc_step_rate = uint16_t(current_block->initial_rate);
  437. acceleration_time = calc_timer(acc_step_rate);
  438. #ifdef LIN_ADVANCE
  439. current_estep_rate = ((unsigned long)acc_step_rate * current_block->abs_adv_steps_multiplier8) >> 17;
  440. #endif /* LIN_ADVANCE */
  441. if (current_block->flag & BLOCK_FLAG_DDA_LOWRES) {
  442. counter_x.lo = -(current_block->step_event_count.lo >> 1);
  443. counter_y.lo = counter_x.lo;
  444. counter_z.lo = counter_x.lo;
  445. counter_e.lo = counter_x.lo;
  446. } else {
  447. counter_x.wide = -(current_block->step_event_count.wide >> 1);
  448. counter_y.wide = counter_x.wide;
  449. counter_z.wide = counter_x.wide;
  450. counter_e.wide = counter_x.wide;
  451. }
  452. step_events_completed.wide = 0;
  453. // Set directions.
  454. out_bits = current_block->direction_bits;
  455. // Set the direction bits (X_AXIS=A_AXIS and Y_AXIS=B_AXIS for COREXY)
  456. if((out_bits & (1<<X_AXIS))!=0){
  457. WRITE_NC(X_DIR_PIN, INVERT_X_DIR);
  458. count_direction[X_AXIS]=-1;
  459. } else {
  460. WRITE_NC(X_DIR_PIN, !INVERT_X_DIR);
  461. count_direction[X_AXIS]=1;
  462. }
  463. if((out_bits & (1<<Y_AXIS))!=0){
  464. WRITE_NC(Y_DIR_PIN, INVERT_Y_DIR);
  465. count_direction[Y_AXIS]=-1;
  466. } else {
  467. WRITE_NC(Y_DIR_PIN, !INVERT_Y_DIR);
  468. count_direction[Y_AXIS]=1;
  469. }
  470. if ((out_bits & (1<<Z_AXIS)) != 0) { // -direction
  471. WRITE_NC(Z_DIR_PIN,INVERT_Z_DIR);
  472. count_direction[Z_AXIS]=-1;
  473. } else { // +direction
  474. WRITE_NC(Z_DIR_PIN,!INVERT_Z_DIR);
  475. count_direction[Z_AXIS]=1;
  476. }
  477. if ((out_bits & (1 << E_AXIS)) != 0) { // -direction
  478. #ifndef LIN_ADVANCE
  479. WRITE(E0_DIR_PIN,
  480. #ifdef SNMM
  481. (snmm_extruder == 0 || snmm_extruder == 2) ? !INVERT_E0_DIR :
  482. #endif // SNMM
  483. INVERT_E0_DIR);
  484. #endif /* LIN_ADVANCE */
  485. count_direction[E_AXIS] = -1;
  486. } else { // +direction
  487. #ifndef LIN_ADVANCE
  488. WRITE(E0_DIR_PIN,
  489. #ifdef SNMM
  490. (snmm_extruder == 0 || snmm_extruder == 2) ? INVERT_E0_DIR :
  491. #endif // SNMM
  492. !INVERT_E0_DIR);
  493. #endif /* LIN_ADVANCE */
  494. count_direction[E_AXIS] = 1;
  495. }
  496. }
  497. else {
  498. OCR1A = 2000; // 1kHz.
  499. }
  500. //WRITE_NC(LOGIC_ANALYZER_CH2, false);
  501. }
  502. // Check limit switches.
  503. FORCE_INLINE void stepper_check_endstops()
  504. {
  505. if(check_endstops)
  506. {
  507. #ifndef COREXY
  508. if ((out_bits & (1<<X_AXIS)) != 0) // stepping along -X axis
  509. #else
  510. if ((((out_bits & (1<<X_AXIS)) != 0)&&(out_bits & (1<<Y_AXIS)) != 0)) //-X occurs for -A and -B
  511. #endif
  512. {
  513. #if ( (defined(X_MIN_PIN) && (X_MIN_PIN > -1)) || defined(TMC2130_SG_HOMING) ) && !defined(DEBUG_DISABLE_XMINLIMIT)
  514. #ifdef TMC2130_SG_HOMING
  515. // Stall guard homing turned on
  516. x_min_endstop = (READ(X_TMC2130_DIAG) != 0);
  517. #else
  518. // Normal homing
  519. x_min_endstop = (READ(X_MIN_PIN) != X_MIN_ENDSTOP_INVERTING);
  520. #endif
  521. if(x_min_endstop && old_x_min_endstop && (current_block->steps_x.wide > 0)) {
  522. endstops_trigsteps[X_AXIS] = count_position[X_AXIS];
  523. endstop_x_hit=true;
  524. step_events_completed.wide = current_block->step_event_count.wide;
  525. }
  526. old_x_min_endstop = x_min_endstop;
  527. #endif
  528. } else { // +direction
  529. #if ( (defined(X_MAX_PIN) && (X_MAX_PIN > -1)) || defined(TMC2130_SG_HOMING) ) && !defined(DEBUG_DISABLE_XMAXLIMIT)
  530. #ifdef TMC2130_SG_HOMING
  531. // Stall guard homing turned on
  532. x_max_endstop = (READ(X_TMC2130_DIAG) != 0);
  533. #else
  534. // Normal homing
  535. x_max_endstop = (READ(X_MAX_PIN) != X_MAX_ENDSTOP_INVERTING);
  536. #endif
  537. if(x_max_endstop && old_x_max_endstop && (current_block->steps_x.wide > 0)){
  538. endstops_trigsteps[X_AXIS] = count_position[X_AXIS];
  539. endstop_x_hit=true;
  540. step_events_completed.wide = current_block->step_event_count.wide;
  541. }
  542. old_x_max_endstop = x_max_endstop;
  543. #endif
  544. }
  545. #ifndef COREXY
  546. if ((out_bits & (1<<Y_AXIS)) != 0) // -direction
  547. #else
  548. if ((((out_bits & (1<<X_AXIS)) != 0)&&(out_bits & (1<<Y_AXIS)) == 0)) // -Y occurs for -A and +B
  549. #endif
  550. {
  551. #if ( (defined(Y_MIN_PIN) && (Y_MIN_PIN > -1)) || defined(TMC2130_SG_HOMING) ) && !defined(DEBUG_DISABLE_YMINLIMIT)
  552. #ifdef TMC2130_SG_HOMING
  553. // Stall guard homing turned on
  554. y_min_endstop = (READ(Y_TMC2130_DIAG) != 0);
  555. #else
  556. // Normal homing
  557. y_min_endstop = (READ(Y_MIN_PIN) != Y_MIN_ENDSTOP_INVERTING);
  558. #endif
  559. if(y_min_endstop && old_y_min_endstop && (current_block->steps_y.wide > 0)) {
  560. endstops_trigsteps[Y_AXIS] = count_position[Y_AXIS];
  561. endstop_y_hit=true;
  562. step_events_completed.wide = current_block->step_event_count.wide;
  563. }
  564. old_y_min_endstop = y_min_endstop;
  565. #endif
  566. } else { // +direction
  567. #if ( (defined(Y_MAX_PIN) && (Y_MAX_PIN > -1)) || defined(TMC2130_SG_HOMING) ) && !defined(DEBUG_DISABLE_YMAXLIMIT)
  568. #ifdef TMC2130_SG_HOMING
  569. // Stall guard homing turned on
  570. y_max_endstop = (READ(Y_TMC2130_DIAG) != 0);
  571. #else
  572. // Normal homing
  573. y_max_endstop = (READ(Y_MAX_PIN) != Y_MAX_ENDSTOP_INVERTING);
  574. #endif
  575. if(y_max_endstop && old_y_max_endstop && (current_block->steps_y.wide > 0)){
  576. endstops_trigsteps[Y_AXIS] = count_position[Y_AXIS];
  577. endstop_y_hit=true;
  578. step_events_completed.wide = current_block->step_event_count.wide;
  579. }
  580. old_y_max_endstop = y_max_endstop;
  581. #endif
  582. }
  583. if ((out_bits & (1<<Z_AXIS)) != 0) // -direction
  584. {
  585. #if defined(Z_MIN_PIN) && (Z_MIN_PIN > -1) && !defined(DEBUG_DISABLE_ZMINLIMIT)
  586. if (! check_z_endstop) {
  587. #ifdef TMC2130_SG_HOMING
  588. // Stall guard homing turned on
  589. #ifdef TMC2130_STEALTH_Z
  590. if ((tmc2130_mode == TMC2130_MODE_SILENT) && !(tmc2130_sg_homing_axes_mask & 0x04))
  591. z_min_endstop = (READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING);
  592. else
  593. #endif //TMC2130_STEALTH_Z
  594. z_min_endstop = (READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING) || (READ(Z_TMC2130_DIAG) != 0);
  595. #else
  596. z_min_endstop = (READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING);
  597. #endif //TMC2130_SG_HOMING
  598. if(z_min_endstop && old_z_min_endstop && (current_block->steps_z.wide > 0)) {
  599. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  600. endstop_z_hit=true;
  601. step_events_completed.wide = current_block->step_event_count.wide;
  602. }
  603. old_z_min_endstop = z_min_endstop;
  604. }
  605. #endif
  606. } else { // +direction
  607. #if defined(Z_MAX_PIN) && (Z_MAX_PIN > -1) && !defined(DEBUG_DISABLE_ZMAXLIMIT)
  608. #ifdef TMC2130_SG_HOMING
  609. // Stall guard homing turned on
  610. #ifdef TMC2130_STEALTH_Z
  611. if ((tmc2130_mode == TMC2130_MODE_SILENT) && !(tmc2130_sg_homing_axes_mask & 0x04))
  612. z_max_endstop = false;
  613. else
  614. #endif //TMC2130_STEALTH_Z
  615. z_max_endstop = (READ(Z_TMC2130_DIAG) != 0);
  616. #else
  617. z_max_endstop = (READ(Z_MAX_PIN) != Z_MAX_ENDSTOP_INVERTING);
  618. #endif //TMC2130_SG_HOMING
  619. if(z_max_endstop && old_z_max_endstop && (current_block->steps_z.wide > 0)) {
  620. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  621. endstop_z_hit=true;
  622. step_events_completed.wide = current_block->step_event_count.wide;
  623. }
  624. old_z_max_endstop = z_max_endstop;
  625. #endif
  626. }
  627. }
  628. // Supporting stopping on a trigger of the Z-stop induction sensor, not only for the Z-minus movements.
  629. #if defined(Z_MIN_PIN) && (Z_MIN_PIN > -1) && !defined(DEBUG_DISABLE_ZMINLIMIT)
  630. if (check_z_endstop) {
  631. // Check the Z min end-stop no matter what.
  632. // Good for searching for the center of an induction target.
  633. #ifdef TMC2130_SG_HOMING
  634. // Stall guard homing turned on
  635. #ifdef TMC2130_STEALTH_Z
  636. if ((tmc2130_mode == TMC2130_MODE_SILENT) && !(tmc2130_sg_homing_axes_mask & 0x04))
  637. z_min_endstop = (READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING);
  638. else
  639. #endif //TMC2130_STEALTH_Z
  640. z_min_endstop = (READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING) || (READ(Z_TMC2130_DIAG) != 0);
  641. #else
  642. z_min_endstop = (READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING);
  643. #endif //TMC2130_SG_HOMING
  644. if(z_min_endstop && old_z_min_endstop) {
  645. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  646. endstop_z_hit=true;
  647. step_events_completed.wide = current_block->step_event_count.wide;
  648. }
  649. old_z_min_endstop = z_min_endstop;
  650. }
  651. #endif
  652. }
  653. FORCE_INLINE void stepper_tick_lowres()
  654. {
  655. for (uint8_t i=0; i < step_loops; ++ i) { // Take multiple steps per interrupt (For high speed moves)
  656. MSerial.checkRx(); // Check for serial chars.
  657. // Step in X axis
  658. counter_x.lo += current_block->steps_x.lo;
  659. if (counter_x.lo > 0) {
  660. WRITE_NC(X_STEP_PIN, !INVERT_X_STEP_PIN);
  661. #ifdef DEBUG_XSTEP_DUP_PIN
  662. WRITE_NC(DEBUG_XSTEP_DUP_PIN,!INVERT_X_STEP_PIN);
  663. #endif //DEBUG_XSTEP_DUP_PIN
  664. counter_x.lo -= current_block->step_event_count.lo;
  665. count_position[X_AXIS]+=count_direction[X_AXIS];
  666. WRITE_NC(X_STEP_PIN, INVERT_X_STEP_PIN);
  667. #ifdef DEBUG_XSTEP_DUP_PIN
  668. WRITE_NC(DEBUG_XSTEP_DUP_PIN,INVERT_X_STEP_PIN);
  669. #endif //DEBUG_XSTEP_DUP_PIN
  670. }
  671. // Step in Y axis
  672. counter_y.lo += current_block->steps_y.lo;
  673. if (counter_y.lo > 0) {
  674. WRITE_NC(Y_STEP_PIN, !INVERT_Y_STEP_PIN);
  675. #ifdef DEBUG_YSTEP_DUP_PIN
  676. WRITE_NC(DEBUG_YSTEP_DUP_PIN,!INVERT_Y_STEP_PIN);
  677. #endif //DEBUG_YSTEP_DUP_PIN
  678. counter_y.lo -= current_block->step_event_count.lo;
  679. count_position[Y_AXIS]+=count_direction[Y_AXIS];
  680. WRITE_NC(Y_STEP_PIN, INVERT_Y_STEP_PIN);
  681. #ifdef DEBUG_YSTEP_DUP_PIN
  682. WRITE_NC(DEBUG_YSTEP_DUP_PIN,INVERT_Y_STEP_PIN);
  683. #endif //DEBUG_YSTEP_DUP_PIN
  684. }
  685. // Step in Z axis
  686. counter_z.lo += current_block->steps_z.lo;
  687. if (counter_z.lo > 0) {
  688. WRITE_NC(Z_STEP_PIN, !INVERT_Z_STEP_PIN);
  689. counter_z.lo -= current_block->step_event_count.lo;
  690. count_position[Z_AXIS]+=count_direction[Z_AXIS];
  691. WRITE_NC(Z_STEP_PIN, INVERT_Z_STEP_PIN);
  692. }
  693. // Step in E axis
  694. counter_e.lo += current_block->steps_e.lo;
  695. if (counter_e.lo > 0) {
  696. #ifndef LIN_ADVANCE
  697. WRITE(E0_STEP_PIN, !INVERT_E_STEP_PIN);
  698. #endif /* LIN_ADVANCE */
  699. counter_e.lo -= current_block->step_event_count.lo;
  700. count_position[E_AXIS] += count_direction[E_AXIS];
  701. #ifdef LIN_ADVANCE
  702. ++ e_steps;
  703. #else
  704. #ifdef FILAMENT_SENSOR
  705. ++ fsensor_counter;
  706. #endif //FILAMENT_SENSOR
  707. WRITE(E0_STEP_PIN, INVERT_E_STEP_PIN);
  708. #endif
  709. }
  710. if(++ step_events_completed.lo >= current_block->step_event_count.lo)
  711. break;
  712. }
  713. }
  714. FORCE_INLINE void stepper_tick_highres()
  715. {
  716. for (uint8_t i=0; i < step_loops; ++ i) { // Take multiple steps per interrupt (For high speed moves)
  717. MSerial.checkRx(); // Check for serial chars.
  718. // Step in X axis
  719. counter_x.wide += current_block->steps_x.wide;
  720. if (counter_x.wide > 0) {
  721. WRITE_NC(X_STEP_PIN, !INVERT_X_STEP_PIN);
  722. #ifdef DEBUG_XSTEP_DUP_PIN
  723. WRITE_NC(DEBUG_XSTEP_DUP_PIN,!INVERT_X_STEP_PIN);
  724. #endif //DEBUG_XSTEP_DUP_PIN
  725. counter_x.wide -= current_block->step_event_count.wide;
  726. count_position[X_AXIS]+=count_direction[X_AXIS];
  727. WRITE_NC(X_STEP_PIN, INVERT_X_STEP_PIN);
  728. #ifdef DEBUG_XSTEP_DUP_PIN
  729. WRITE_NC(DEBUG_XSTEP_DUP_PIN,INVERT_X_STEP_PIN);
  730. #endif //DEBUG_XSTEP_DUP_PIN
  731. }
  732. // Step in Y axis
  733. counter_y.wide += current_block->steps_y.wide;
  734. if (counter_y.wide > 0) {
  735. WRITE_NC(Y_STEP_PIN, !INVERT_Y_STEP_PIN);
  736. #ifdef DEBUG_YSTEP_DUP_PIN
  737. WRITE_NC(DEBUG_YSTEP_DUP_PIN,!INVERT_Y_STEP_PIN);
  738. #endif //DEBUG_YSTEP_DUP_PIN
  739. counter_y.wide -= current_block->step_event_count.wide;
  740. count_position[Y_AXIS]+=count_direction[Y_AXIS];
  741. WRITE_NC(Y_STEP_PIN, INVERT_Y_STEP_PIN);
  742. #ifdef DEBUG_YSTEP_DUP_PIN
  743. WRITE_NC(DEBUG_YSTEP_DUP_PIN,INVERT_Y_STEP_PIN);
  744. #endif //DEBUG_YSTEP_DUP_PIN
  745. }
  746. // Step in Z axis
  747. counter_z.wide += current_block->steps_z.wide;
  748. if (counter_z.wide > 0) {
  749. WRITE_NC(Z_STEP_PIN, !INVERT_Z_STEP_PIN);
  750. counter_z.wide -= current_block->step_event_count.wide;
  751. count_position[Z_AXIS]+=count_direction[Z_AXIS];
  752. WRITE_NC(Z_STEP_PIN, INVERT_Z_STEP_PIN);
  753. }
  754. // Step in E axis
  755. counter_e.wide += current_block->steps_e.wide;
  756. if (counter_e.wide > 0) {
  757. #ifndef LIN_ADVANCE
  758. WRITE(E0_STEP_PIN, !INVERT_E_STEP_PIN);
  759. #endif /* LIN_ADVANCE */
  760. counter_e.wide -= current_block->step_event_count.wide;
  761. count_position[E_AXIS]+=count_direction[E_AXIS];
  762. #ifdef LIN_ADVANCE
  763. ++ e_steps;
  764. #else
  765. #ifdef FILAMENT_SENSOR
  766. ++ fsensor_counter;
  767. #endif //FILAMENT_SENSOR
  768. WRITE(E0_STEP_PIN, INVERT_E_STEP_PIN);
  769. #endif
  770. }
  771. if(++ step_events_completed.wide >= current_block->step_event_count.wide)
  772. break;
  773. }
  774. }
  775. // 50us delay
  776. #define LIN_ADV_FIRST_TICK_DELAY 100
  777. FORCE_INLINE void isr() {
  778. //WRITE_NC(LOGIC_ANALYZER_CH0, true);
  779. //if (UVLO) uvlo();
  780. // If there is no current block, attempt to pop one from the buffer
  781. if (current_block == NULL)
  782. stepper_next_block();
  783. if (current_block != NULL)
  784. {
  785. stepper_check_endstops();
  786. #ifdef LIN_ADVANCE
  787. e_steps = 0;
  788. #endif /* LIN_ADVANCE */
  789. if (current_block->flag & BLOCK_FLAG_DDA_LOWRES)
  790. stepper_tick_lowres();
  791. else
  792. stepper_tick_highres();
  793. #ifdef LIN_ADVANCE
  794. if (out_bits&(1<<E_AXIS))
  795. // Move in negative direction.
  796. e_steps = - e_steps;
  797. if (current_block->use_advance_lead) {
  798. //int esteps_inc = 0;
  799. //esteps_inc = current_estep_rate - current_adv_steps;
  800. //e_steps += esteps_inc;
  801. e_steps += current_estep_rate - current_adv_steps;
  802. #if 0
  803. if (abs(esteps_inc) > 4) {
  804. LOGIC_ANALYZER_SERIAL_TX_WRITE(esteps_inc);
  805. if (esteps_inc < -511 || esteps_inc > 511)
  806. LOGIC_ANALYZER_SERIAL_TX_WRITE(esteps_inc >> 9);
  807. }
  808. #endif
  809. current_adv_steps = current_estep_rate;
  810. }
  811. // If we have esteps to execute, step some of them now.
  812. if (e_steps) {
  813. //WRITE_NC(LOGIC_ANALYZER_CH7, true);
  814. // Set the step direction.
  815. {
  816. bool neg = e_steps < 0;
  817. bool dir =
  818. #ifdef SNMM
  819. (neg == (snmm_extruder & 1))
  820. #else
  821. neg
  822. #endif
  823. ? INVERT_E0_DIR : !INVERT_E0_DIR; //If we have SNMM, reverse every second extruder.
  824. WRITE_NC(E0_DIR_PIN, dir);
  825. if (neg)
  826. // Flip the e_steps counter to be always positive.
  827. e_steps = - e_steps;
  828. }
  829. // Tick min(step_loops, abs(e_steps)).
  830. estep_loops = (e_steps & 0x0ff00) ? 4 : e_steps;
  831. if (step_loops < estep_loops)
  832. estep_loops = step_loops;
  833. #ifdef FILAMENT_SENSOR
  834. fsensor_counter += estep_loops;
  835. #endif //FILAMENT_SENSOR
  836. do {
  837. WRITE_NC(E0_STEP_PIN, !INVERT_E_STEP_PIN);
  838. -- e_steps;
  839. WRITE_NC(E0_STEP_PIN, INVERT_E_STEP_PIN);
  840. } while (-- estep_loops != 0);
  841. //WRITE_NC(LOGIC_ANALYZER_CH7, false);
  842. MSerial.checkRx(); // Check for serial chars.
  843. }
  844. #endif
  845. // Calculare new timer value
  846. // 13.38-14.63us for steady state,
  847. // 25.12us for acceleration / deceleration.
  848. {
  849. //WRITE_NC(LOGIC_ANALYZER_CH1, true);
  850. if (step_events_completed.wide <= (unsigned long int)current_block->accelerate_until) {
  851. // v = t * a -> acc_step_rate = acceleration_time * current_block->acceleration_rate
  852. MultiU24X24toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate);
  853. acc_step_rate += uint16_t(current_block->initial_rate);
  854. // upper limit
  855. if(acc_step_rate > uint16_t(current_block->nominal_rate))
  856. acc_step_rate = current_block->nominal_rate;
  857. // step_rate to timer interval
  858. uint16_t timer = calc_timer(acc_step_rate);
  859. _NEXT_ISR(timer);
  860. acceleration_time += timer;
  861. #ifdef LIN_ADVANCE
  862. if (current_block->use_advance_lead)
  863. // int32_t = (uint16_t * uint32_t) >> 17
  864. current_estep_rate = ((uint32_t)acc_step_rate * current_block->abs_adv_steps_multiplier8) >> 17;
  865. #endif
  866. }
  867. else if (step_events_completed.wide > (unsigned long int)current_block->decelerate_after) {
  868. uint16_t step_rate;
  869. MultiU24X24toH16(step_rate, deceleration_time, current_block->acceleration_rate);
  870. step_rate = acc_step_rate - step_rate; // Decelerate from aceleration end point.
  871. if ((step_rate & 0x8000) || step_rate < uint16_t(current_block->final_rate)) {
  872. // Result is negative or too small.
  873. step_rate = uint16_t(current_block->final_rate);
  874. }
  875. // Step_rate to timer interval.
  876. uint16_t timer = calc_timer(step_rate);
  877. _NEXT_ISR(timer);
  878. deceleration_time += timer;
  879. #ifdef LIN_ADVANCE
  880. if (current_block->use_advance_lead)
  881. current_estep_rate = ((uint32_t)step_rate * current_block->abs_adv_steps_multiplier8) >> 17;
  882. #endif
  883. }
  884. else {
  885. if (! step_loops_nominal) {
  886. // Calculation of the steady state timer rate has been delayed to the 1st tick of the steady state to lower
  887. // the initial interrupt blocking.
  888. OCR1A_nominal = calc_timer(uint16_t(current_block->nominal_rate));
  889. step_loops_nominal = step_loops;
  890. #ifdef LIN_ADVANCE
  891. if (current_block->use_advance_lead)
  892. current_estep_rate = (current_block->nominal_rate * current_block->abs_adv_steps_multiplier8) >> 17;
  893. #endif
  894. }
  895. _NEXT_ISR(OCR1A_nominal);
  896. }
  897. //WRITE_NC(LOGIC_ANALYZER_CH1, false);
  898. }
  899. #ifdef LIN_ADVANCE
  900. if (e_steps && current_block->use_advance_lead) {
  901. //WRITE_NC(LOGIC_ANALYZER_CH7, true);
  902. MSerial.checkRx(); // Check for serial chars.
  903. // Some of the E steps were not ticked yet. Plan additional interrupts.
  904. uint16_t now = TCNT1;
  905. // Plan the first linear advance interrupt after 50us from now.
  906. uint16_t to_go = nextMainISR - now - LIN_ADV_FIRST_TICK_DELAY;
  907. eISR_Rate = 0;
  908. if ((to_go & 0x8000) == 0) {
  909. // The to_go number is not negative.
  910. // Count the number of 7812,5 ticks, that fit into to_go 2MHz ticks.
  911. uint8_t ticks = to_go >> 8;
  912. if (ticks == 1) {
  913. // Avoid running the following loop for a very short interval.
  914. estep_loops = 255;
  915. eISR_Rate = 1;
  916. } else if ((e_steps & 0x0ff00) == 0) {
  917. // e_steps <= 0x0ff
  918. if (uint8_t(e_steps) <= ticks) {
  919. // Spread the e_steps along the whole go_to interval.
  920. eISR_Rate = to_go / uint8_t(e_steps);
  921. estep_loops = 1;
  922. } else if (ticks != 0) {
  923. // At least one tick fits into the to_go interval. Calculate the e-step grouping.
  924. uint8_t e = uint8_t(e_steps) >> 1;
  925. estep_loops = 2;
  926. while (e > ticks) {
  927. e >>= 1;
  928. estep_loops <<= 1;
  929. }
  930. // Now the estep_loops contains the number of loops of power of 2, that will be sufficient
  931. // to squeeze enough of Linear Advance ticks until nextMainISR.
  932. // Calculate the tick rate.
  933. eISR_Rate = to_go / ticks;
  934. }
  935. } else {
  936. // This is an exterme case with too many e_steps inserted by the linear advance.
  937. // At least one tick fits into the to_go interval. Calculate the e-step grouping.
  938. estep_loops = 2;
  939. uint16_t e = e_steps >> 1;
  940. while (e & 0x0ff00) {
  941. e >>= 1;
  942. estep_loops <<= 1;
  943. }
  944. while (uint8_t(e) > ticks) {
  945. e >>= 1;
  946. estep_loops <<= 1;
  947. }
  948. // Now the estep_loops contains the number of loops of power of 2, that will be sufficient
  949. // to squeeze enough of Linear Advance ticks until nextMainISR.
  950. // Calculate the tick rate.
  951. eISR_Rate = to_go / ticks;
  952. }
  953. }
  954. if (eISR_Rate == 0) {
  955. // There is not enough time to fit even a single additional tick.
  956. // Tick all the extruder ticks now.
  957. #ifdef FILAMENT_SENSOR
  958. fsensor_counter += e_steps;
  959. #endif //FILAMENT_SENSOR
  960. MSerial.checkRx(); // Check for serial chars.
  961. do {
  962. WRITE_NC(E0_STEP_PIN, !INVERT_E_STEP_PIN);
  963. -- e_steps;
  964. WRITE_NC(E0_STEP_PIN, INVERT_E_STEP_PIN);
  965. } while (e_steps);
  966. OCR1A = nextMainISR;
  967. } else {
  968. // Tick the 1st Linear Advance interrupt after 50us from now.
  969. nextMainISR -= LIN_ADV_FIRST_TICK_DELAY;
  970. OCR1A = now + LIN_ADV_FIRST_TICK_DELAY;
  971. }
  972. //WRITE_NC(LOGIC_ANALYZER_CH7, false);
  973. } else
  974. OCR1A = nextMainISR;
  975. #endif
  976. // If current block is finished, reset pointer
  977. if (step_events_completed.wide >= current_block->step_event_count.wide) {
  978. #ifdef FILAMENT_SENSOR
  979. fsensor_st_block_chunk(current_block, fsensor_counter);
  980. fsensor_counter = 0;
  981. #endif //FILAMENT_SENSOR
  982. current_block = NULL;
  983. plan_discard_current_block();
  984. }
  985. #ifdef FILAMENT_SENSOR
  986. else if (fsensor_counter >= fsensor_chunk_len)
  987. {
  988. fsensor_st_block_chunk(current_block, fsensor_counter);
  989. fsensor_counter = 0;
  990. }
  991. #endif //FILAMENT_SENSOR
  992. }
  993. #ifdef TMC2130
  994. tmc2130_st_isr();
  995. #endif //TMC2130
  996. //WRITE_NC(LOGIC_ANALYZER_CH0, false);
  997. }
  998. #ifdef LIN_ADVANCE
  999. void clear_current_adv_vars() {
  1000. e_steps = 0; //Should be already 0 at an filament change event, but just to be sure..
  1001. current_adv_steps = 0;
  1002. }
  1003. #endif // LIN_ADVANCE
  1004. void st_init()
  1005. {
  1006. #ifdef TMC2130
  1007. tmc2130_init();
  1008. #endif //TMC2130
  1009. st_current_init(); //Initialize Digipot Motor Current
  1010. microstep_init(); //Initialize Microstepping Pins
  1011. //Initialize Dir Pins
  1012. #if defined(X_DIR_PIN) && X_DIR_PIN > -1
  1013. SET_OUTPUT(X_DIR_PIN);
  1014. #endif
  1015. #if defined(X2_DIR_PIN) && X2_DIR_PIN > -1
  1016. SET_OUTPUT(X2_DIR_PIN);
  1017. #endif
  1018. #if defined(Y_DIR_PIN) && Y_DIR_PIN > -1
  1019. SET_OUTPUT(Y_DIR_PIN);
  1020. #if defined(Y_DUAL_STEPPER_DRIVERS) && defined(Y2_DIR_PIN) && (Y2_DIR_PIN > -1)
  1021. SET_OUTPUT(Y2_DIR_PIN);
  1022. #endif
  1023. #endif
  1024. #if defined(Z_DIR_PIN) && Z_DIR_PIN > -1
  1025. SET_OUTPUT(Z_DIR_PIN);
  1026. #if defined(Z_DUAL_STEPPER_DRIVERS) && defined(Z2_DIR_PIN) && (Z2_DIR_PIN > -1)
  1027. SET_OUTPUT(Z2_DIR_PIN);
  1028. #endif
  1029. #endif
  1030. #if defined(E0_DIR_PIN) && E0_DIR_PIN > -1
  1031. SET_OUTPUT(E0_DIR_PIN);
  1032. #endif
  1033. #if defined(E1_DIR_PIN) && (E1_DIR_PIN > -1)
  1034. SET_OUTPUT(E1_DIR_PIN);
  1035. #endif
  1036. #if defined(E2_DIR_PIN) && (E2_DIR_PIN > -1)
  1037. SET_OUTPUT(E2_DIR_PIN);
  1038. #endif
  1039. //Initialize Enable Pins - steppers default to disabled.
  1040. #if defined(X_ENABLE_PIN) && X_ENABLE_PIN > -1
  1041. SET_OUTPUT(X_ENABLE_PIN);
  1042. if(!X_ENABLE_ON) WRITE(X_ENABLE_PIN,HIGH);
  1043. #endif
  1044. #if defined(X2_ENABLE_PIN) && X2_ENABLE_PIN > -1
  1045. SET_OUTPUT(X2_ENABLE_PIN);
  1046. if(!X_ENABLE_ON) WRITE(X2_ENABLE_PIN,HIGH);
  1047. #endif
  1048. #if defined(Y_ENABLE_PIN) && Y_ENABLE_PIN > -1
  1049. SET_OUTPUT(Y_ENABLE_PIN);
  1050. if(!Y_ENABLE_ON) WRITE(Y_ENABLE_PIN,HIGH);
  1051. #if defined(Y_DUAL_STEPPER_DRIVERS) && defined(Y2_ENABLE_PIN) && (Y2_ENABLE_PIN > -1)
  1052. SET_OUTPUT(Y2_ENABLE_PIN);
  1053. if(!Y_ENABLE_ON) WRITE(Y2_ENABLE_PIN,HIGH);
  1054. #endif
  1055. #endif
  1056. #if defined(Z_ENABLE_PIN) && Z_ENABLE_PIN > -1
  1057. SET_OUTPUT(Z_ENABLE_PIN);
  1058. if(!Z_ENABLE_ON) WRITE(Z_ENABLE_PIN,HIGH);
  1059. #if defined(Z_DUAL_STEPPER_DRIVERS) && defined(Z2_ENABLE_PIN) && (Z2_ENABLE_PIN > -1)
  1060. SET_OUTPUT(Z2_ENABLE_PIN);
  1061. if(!Z_ENABLE_ON) WRITE(Z2_ENABLE_PIN,HIGH);
  1062. #endif
  1063. #endif
  1064. #if defined(E0_ENABLE_PIN) && (E0_ENABLE_PIN > -1)
  1065. SET_OUTPUT(E0_ENABLE_PIN);
  1066. if(!E_ENABLE_ON) WRITE(E0_ENABLE_PIN,HIGH);
  1067. #endif
  1068. #if defined(E1_ENABLE_PIN) && (E1_ENABLE_PIN > -1)
  1069. SET_OUTPUT(E1_ENABLE_PIN);
  1070. if(!E_ENABLE_ON) WRITE(E1_ENABLE_PIN,HIGH);
  1071. #endif
  1072. #if defined(E2_ENABLE_PIN) && (E2_ENABLE_PIN > -1)
  1073. SET_OUTPUT(E2_ENABLE_PIN);
  1074. if(!E_ENABLE_ON) WRITE(E2_ENABLE_PIN,HIGH);
  1075. #endif
  1076. //endstops and pullups
  1077. #ifdef TMC2130_SG_HOMING
  1078. SET_INPUT(X_TMC2130_DIAG);
  1079. WRITE(X_TMC2130_DIAG,HIGH);
  1080. SET_INPUT(Y_TMC2130_DIAG);
  1081. WRITE(Y_TMC2130_DIAG,HIGH);
  1082. SET_INPUT(Z_TMC2130_DIAG);
  1083. WRITE(Z_TMC2130_DIAG,HIGH);
  1084. SET_INPUT(E0_TMC2130_DIAG);
  1085. WRITE(E0_TMC2130_DIAG,HIGH);
  1086. #endif
  1087. #if defined(X_MIN_PIN) && X_MIN_PIN > -1
  1088. SET_INPUT(X_MIN_PIN);
  1089. #ifdef ENDSTOPPULLUP_XMIN
  1090. WRITE(X_MIN_PIN,HIGH);
  1091. #endif
  1092. #endif
  1093. #if defined(Y_MIN_PIN) && Y_MIN_PIN > -1
  1094. SET_INPUT(Y_MIN_PIN);
  1095. #ifdef ENDSTOPPULLUP_YMIN
  1096. WRITE(Y_MIN_PIN,HIGH);
  1097. #endif
  1098. #endif
  1099. #if defined(Z_MIN_PIN) && Z_MIN_PIN > -1
  1100. SET_INPUT(Z_MIN_PIN);
  1101. #ifdef ENDSTOPPULLUP_ZMIN
  1102. WRITE(Z_MIN_PIN,HIGH);
  1103. #endif
  1104. #endif
  1105. #if defined(X_MAX_PIN) && X_MAX_PIN > -1
  1106. SET_INPUT(X_MAX_PIN);
  1107. #ifdef ENDSTOPPULLUP_XMAX
  1108. WRITE(X_MAX_PIN,HIGH);
  1109. #endif
  1110. #endif
  1111. #if defined(Y_MAX_PIN) && Y_MAX_PIN > -1
  1112. SET_INPUT(Y_MAX_PIN);
  1113. #ifdef ENDSTOPPULLUP_YMAX
  1114. WRITE(Y_MAX_PIN,HIGH);
  1115. #endif
  1116. #endif
  1117. #if defined(Z_MAX_PIN) && Z_MAX_PIN > -1
  1118. SET_INPUT(Z_MAX_PIN);
  1119. #ifdef ENDSTOPPULLUP_ZMAX
  1120. WRITE(Z_MAX_PIN,HIGH);
  1121. #endif
  1122. #endif
  1123. #if (defined(FANCHECK) && defined(TACH_0) && (TACH_0 > -1))
  1124. SET_INPUT(TACH_0);
  1125. #ifdef TACH0PULLUP
  1126. WRITE(TACH_0, HIGH);
  1127. #endif
  1128. #endif
  1129. //Initialize Step Pins
  1130. #if defined(X_STEP_PIN) && (X_STEP_PIN > -1)
  1131. SET_OUTPUT(X_STEP_PIN);
  1132. WRITE(X_STEP_PIN,INVERT_X_STEP_PIN);
  1133. #ifdef DEBUG_XSTEP_DUP_PIN
  1134. SET_OUTPUT(DEBUG_XSTEP_DUP_PIN);
  1135. WRITE(DEBUG_XSTEP_DUP_PIN,INVERT_X_STEP_PIN);
  1136. #endif //DEBUG_XSTEP_DUP_PIN
  1137. disable_x();
  1138. #endif
  1139. #if defined(X2_STEP_PIN) && (X2_STEP_PIN > -1)
  1140. SET_OUTPUT(X2_STEP_PIN);
  1141. WRITE(X2_STEP_PIN,INVERT_X_STEP_PIN);
  1142. disable_x();
  1143. #endif
  1144. #if defined(Y_STEP_PIN) && (Y_STEP_PIN > -1)
  1145. SET_OUTPUT(Y_STEP_PIN);
  1146. WRITE(Y_STEP_PIN,INVERT_Y_STEP_PIN);
  1147. #ifdef DEBUG_YSTEP_DUP_PIN
  1148. SET_OUTPUT(DEBUG_YSTEP_DUP_PIN);
  1149. WRITE(DEBUG_YSTEP_DUP_PIN,INVERT_Y_STEP_PIN);
  1150. #endif //DEBUG_YSTEP_DUP_PIN
  1151. #if defined(Y_DUAL_STEPPER_DRIVERS) && defined(Y2_STEP_PIN) && (Y2_STEP_PIN > -1)
  1152. SET_OUTPUT(Y2_STEP_PIN);
  1153. WRITE(Y2_STEP_PIN,INVERT_Y_STEP_PIN);
  1154. #endif
  1155. disable_y();
  1156. #endif
  1157. #if defined(Z_STEP_PIN) && (Z_STEP_PIN > -1)
  1158. SET_OUTPUT(Z_STEP_PIN);
  1159. WRITE(Z_STEP_PIN,INVERT_Z_STEP_PIN);
  1160. #if defined(Z_DUAL_STEPPER_DRIVERS) && defined(Z2_STEP_PIN) && (Z2_STEP_PIN > -1)
  1161. SET_OUTPUT(Z2_STEP_PIN);
  1162. WRITE(Z2_STEP_PIN,INVERT_Z_STEP_PIN);
  1163. #endif
  1164. disable_z();
  1165. #endif
  1166. #if defined(E0_STEP_PIN) && (E0_STEP_PIN > -1)
  1167. SET_OUTPUT(E0_STEP_PIN);
  1168. WRITE(E0_STEP_PIN,INVERT_E_STEP_PIN);
  1169. disable_e0();
  1170. #endif
  1171. #if defined(E1_STEP_PIN) && (E1_STEP_PIN > -1)
  1172. SET_OUTPUT(E1_STEP_PIN);
  1173. WRITE(E1_STEP_PIN,INVERT_E_STEP_PIN);
  1174. disable_e1();
  1175. #endif
  1176. #if defined(E2_STEP_PIN) && (E2_STEP_PIN > -1)
  1177. SET_OUTPUT(E2_STEP_PIN);
  1178. WRITE(E2_STEP_PIN,INVERT_E_STEP_PIN);
  1179. disable_e2();
  1180. #endif
  1181. // waveform generation = 0100 = CTC
  1182. TCCR1B &= ~(1<<WGM13);
  1183. TCCR1B |= (1<<WGM12);
  1184. TCCR1A &= ~(1<<WGM11);
  1185. TCCR1A &= ~(1<<WGM10);
  1186. // output mode = 00 (disconnected)
  1187. TCCR1A &= ~(3<<COM1A0);
  1188. TCCR1A &= ~(3<<COM1B0);
  1189. // Set the timer pre-scaler
  1190. // Generally we use a divider of 8, resulting in a 2MHz timer
  1191. // frequency on a 16MHz MCU. If you are going to change this, be
  1192. // sure to regenerate speed_lookuptable.h with
  1193. // create_speed_lookuptable.py
  1194. TCCR1B = (TCCR1B & ~(0x07<<CS10)) | (2<<CS10);
  1195. // Plan the first interrupt after 8ms from now.
  1196. OCR1A = 0x4000;
  1197. TCNT1 = 0;
  1198. ENABLE_STEPPER_DRIVER_INTERRUPT();
  1199. #ifdef LIN_ADVANCE
  1200. e_steps = 0;
  1201. current_adv_steps = 0;
  1202. #endif
  1203. enable_endstops(true); // Start with endstops active. After homing they can be disabled
  1204. sei();
  1205. }
  1206. // Block until all buffered steps are executed
  1207. void st_synchronize()
  1208. {
  1209. while(blocks_queued())
  1210. {
  1211. #ifdef TMC2130
  1212. manage_heater();
  1213. // Vojtech: Don't disable motors inside the planner!
  1214. if (!tmc2130_update_sg())
  1215. {
  1216. manage_inactivity(true);
  1217. lcd_update(0);
  1218. }
  1219. #else //TMC2130
  1220. manage_heater();
  1221. // Vojtech: Don't disable motors inside the planner!
  1222. manage_inactivity(true);
  1223. lcd_update(0);
  1224. #endif //TMC2130
  1225. }
  1226. }
  1227. void st_set_position(const long &x, const long &y, const long &z, const long &e)
  1228. {
  1229. CRITICAL_SECTION_START;
  1230. // Copy 4x4B.
  1231. // This block locks the interrupts globally for 4.56 us,
  1232. // which corresponds to a maximum repeat frequency of 219.18 kHz.
  1233. // This blocking is safe in the context of a 10kHz stepper driver interrupt
  1234. // or a 115200 Bd serial line receive interrupt, which will not trigger faster than 12kHz.
  1235. count_position[X_AXIS] = x;
  1236. count_position[Y_AXIS] = y;
  1237. count_position[Z_AXIS] = z;
  1238. count_position[E_AXIS] = e;
  1239. CRITICAL_SECTION_END;
  1240. }
  1241. void st_set_e_position(const long &e)
  1242. {
  1243. CRITICAL_SECTION_START;
  1244. count_position[E_AXIS] = e;
  1245. CRITICAL_SECTION_END;
  1246. }
  1247. long st_get_position(uint8_t axis)
  1248. {
  1249. long count_pos;
  1250. CRITICAL_SECTION_START;
  1251. count_pos = count_position[axis];
  1252. CRITICAL_SECTION_END;
  1253. return count_pos;
  1254. }
  1255. void st_get_position_xy(long &x, long &y)
  1256. {
  1257. CRITICAL_SECTION_START;
  1258. x = count_position[X_AXIS];
  1259. y = count_position[Y_AXIS];
  1260. CRITICAL_SECTION_END;
  1261. }
  1262. float st_get_position_mm(uint8_t axis)
  1263. {
  1264. float steper_position_in_steps = st_get_position(axis);
  1265. return steper_position_in_steps / axis_steps_per_unit[axis];
  1266. }
  1267. void finishAndDisableSteppers()
  1268. {
  1269. st_synchronize();
  1270. disable_x();
  1271. disable_y();
  1272. disable_z();
  1273. disable_e0();
  1274. disable_e1();
  1275. disable_e2();
  1276. }
  1277. void quickStop()
  1278. {
  1279. DISABLE_STEPPER_DRIVER_INTERRUPT();
  1280. while (blocks_queued()) plan_discard_current_block();
  1281. current_block = NULL;
  1282. st_reset_timer();
  1283. ENABLE_STEPPER_DRIVER_INTERRUPT();
  1284. }
  1285. #ifdef BABYSTEPPING
  1286. void babystep(const uint8_t axis,const bool direction)
  1287. {
  1288. //MUST ONLY BE CALLED BY A ISR, it depends on that no other ISR interrupts this
  1289. //store initial pin states
  1290. switch(axis)
  1291. {
  1292. case X_AXIS:
  1293. {
  1294. enable_x();
  1295. uint8_t old_x_dir_pin= READ(X_DIR_PIN); //if dualzstepper, both point to same direction.
  1296. //setup new step
  1297. WRITE(X_DIR_PIN,(INVERT_X_DIR)^direction);
  1298. //perform step
  1299. WRITE(X_STEP_PIN, !INVERT_X_STEP_PIN);
  1300. #ifdef DEBUG_XSTEP_DUP_PIN
  1301. WRITE(DEBUG_XSTEP_DUP_PIN,!INVERT_X_STEP_PIN);
  1302. #endif //DEBUG_XSTEP_DUP_PIN
  1303. delayMicroseconds(1);
  1304. WRITE(X_STEP_PIN, INVERT_X_STEP_PIN);
  1305. #ifdef DEBUG_XSTEP_DUP_PIN
  1306. WRITE(DEBUG_XSTEP_DUP_PIN,INVERT_X_STEP_PIN);
  1307. #endif //DEBUG_XSTEP_DUP_PIN
  1308. //get old pin state back.
  1309. WRITE(X_DIR_PIN,old_x_dir_pin);
  1310. }
  1311. break;
  1312. case Y_AXIS:
  1313. {
  1314. enable_y();
  1315. uint8_t old_y_dir_pin= READ(Y_DIR_PIN); //if dualzstepper, both point to same direction.
  1316. //setup new step
  1317. WRITE(Y_DIR_PIN,(INVERT_Y_DIR)^direction);
  1318. //perform step
  1319. WRITE(Y_STEP_PIN, !INVERT_Y_STEP_PIN);
  1320. #ifdef DEBUG_YSTEP_DUP_PIN
  1321. WRITE(DEBUG_YSTEP_DUP_PIN,!INVERT_Y_STEP_PIN);
  1322. #endif //DEBUG_YSTEP_DUP_PIN
  1323. delayMicroseconds(1);
  1324. WRITE(Y_STEP_PIN, INVERT_Y_STEP_PIN);
  1325. #ifdef DEBUG_YSTEP_DUP_PIN
  1326. WRITE(DEBUG_YSTEP_DUP_PIN,INVERT_Y_STEP_PIN);
  1327. #endif //DEBUG_YSTEP_DUP_PIN
  1328. //get old pin state back.
  1329. WRITE(Y_DIR_PIN,old_y_dir_pin);
  1330. }
  1331. break;
  1332. case Z_AXIS:
  1333. {
  1334. enable_z();
  1335. uint8_t old_z_dir_pin= READ(Z_DIR_PIN); //if dualzstepper, both point to same direction.
  1336. //setup new step
  1337. WRITE(Z_DIR_PIN,(INVERT_Z_DIR)^direction^BABYSTEP_INVERT_Z);
  1338. #ifdef Z_DUAL_STEPPER_DRIVERS
  1339. WRITE(Z2_DIR_PIN,(INVERT_Z_DIR)^direction^BABYSTEP_INVERT_Z);
  1340. #endif
  1341. //perform step
  1342. WRITE(Z_STEP_PIN, !INVERT_Z_STEP_PIN);
  1343. #ifdef Z_DUAL_STEPPER_DRIVERS
  1344. WRITE(Z2_STEP_PIN, !INVERT_Z_STEP_PIN);
  1345. #endif
  1346. delayMicroseconds(1);
  1347. WRITE(Z_STEP_PIN, INVERT_Z_STEP_PIN);
  1348. #ifdef Z_DUAL_STEPPER_DRIVERS
  1349. WRITE(Z2_STEP_PIN, INVERT_Z_STEP_PIN);
  1350. #endif
  1351. //get old pin state back.
  1352. WRITE(Z_DIR_PIN,old_z_dir_pin);
  1353. #ifdef Z_DUAL_STEPPER_DRIVERS
  1354. WRITE(Z2_DIR_PIN,old_z_dir_pin);
  1355. #endif
  1356. }
  1357. break;
  1358. default: break;
  1359. }
  1360. }
  1361. #endif //BABYSTEPPING
  1362. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  1363. void digitalPotWrite(int address, int value) // From Arduino DigitalPotControl example
  1364. {
  1365. digitalWrite(DIGIPOTSS_PIN,LOW); // take the SS pin low to select the chip
  1366. SPI.transfer(address); // send in the address and value via SPI:
  1367. SPI.transfer(value);
  1368. digitalWrite(DIGIPOTSS_PIN,HIGH); // take the SS pin high to de-select the chip:
  1369. //delay(10);
  1370. }
  1371. #endif
  1372. void EEPROM_read_st(int pos, uint8_t* value, uint8_t size)
  1373. {
  1374. do
  1375. {
  1376. *value = eeprom_read_byte((unsigned char*)pos);
  1377. pos++;
  1378. value++;
  1379. }while(--size);
  1380. }
  1381. void st_current_init() //Initialize Digipot Motor Current
  1382. {
  1383. uint8_t SilentMode = eeprom_read_byte((uint8_t*)EEPROM_SILENT);
  1384. SilentModeMenu = SilentMode;
  1385. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  1386. pinMode(MOTOR_CURRENT_PWM_XY_PIN, OUTPUT);
  1387. pinMode(MOTOR_CURRENT_PWM_Z_PIN, OUTPUT);
  1388. pinMode(MOTOR_CURRENT_PWM_E_PIN, OUTPUT);
  1389. if((SilentMode == SILENT_MODE_OFF) || (farm_mode) ){
  1390. motor_current_setting[0] = motor_current_setting_loud[0];
  1391. motor_current_setting[1] = motor_current_setting_loud[1];
  1392. motor_current_setting[2] = motor_current_setting_loud[2];
  1393. }else{
  1394. motor_current_setting[0] = motor_current_setting_silent[0];
  1395. motor_current_setting[1] = motor_current_setting_silent[1];
  1396. motor_current_setting[2] = motor_current_setting_silent[2];
  1397. }
  1398. st_current_set(0, motor_current_setting[0]);
  1399. st_current_set(1, motor_current_setting[1]);
  1400. st_current_set(2, motor_current_setting[2]);
  1401. //Set timer5 to 31khz so the PWM of the motor power is as constant as possible. (removes a buzzing noise)
  1402. TCCR5B = (TCCR5B & ~(_BV(CS50) | _BV(CS51) | _BV(CS52))) | _BV(CS50);
  1403. #endif
  1404. }
  1405. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  1406. void st_current_set(uint8_t driver, int current)
  1407. {
  1408. if (driver == 0) analogWrite(MOTOR_CURRENT_PWM_XY_PIN, (long)current * 255L / (long)MOTOR_CURRENT_PWM_RANGE);
  1409. if (driver == 1) analogWrite(MOTOR_CURRENT_PWM_Z_PIN, (long)current * 255L / (long)MOTOR_CURRENT_PWM_RANGE);
  1410. if (driver == 2) analogWrite(MOTOR_CURRENT_PWM_E_PIN, (long)current * 255L / (long)MOTOR_CURRENT_PWM_RANGE);
  1411. }
  1412. #else //MOTOR_CURRENT_PWM_XY_PIN
  1413. void st_current_set(uint8_t, int ){}
  1414. #endif //MOTOR_CURRENT_PWM_XY_PIN
  1415. void microstep_init()
  1416. {
  1417. #if defined(E1_MS1_PIN) && E1_MS1_PIN > -1
  1418. pinMode(E1_MS1_PIN,OUTPUT);
  1419. pinMode(E1_MS2_PIN,OUTPUT);
  1420. #endif
  1421. #if defined(X_MS1_PIN) && X_MS1_PIN > -1
  1422. const uint8_t microstep_modes[] = MICROSTEP_MODES;
  1423. pinMode(X_MS1_PIN,OUTPUT);
  1424. pinMode(X_MS2_PIN,OUTPUT);
  1425. pinMode(Y_MS1_PIN,OUTPUT);
  1426. pinMode(Y_MS2_PIN,OUTPUT);
  1427. pinMode(Z_MS1_PIN,OUTPUT);
  1428. pinMode(Z_MS2_PIN,OUTPUT);
  1429. pinMode(E0_MS1_PIN,OUTPUT);
  1430. pinMode(E0_MS2_PIN,OUTPUT);
  1431. for(int i=0;i<=4;i++) microstep_mode(i,microstep_modes[i]);
  1432. #endif
  1433. }
  1434. #ifndef TMC2130
  1435. void microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2)
  1436. {
  1437. if(ms1 > -1) switch(driver)
  1438. {
  1439. case 0: digitalWrite( X_MS1_PIN,ms1); break;
  1440. case 1: digitalWrite( Y_MS1_PIN,ms1); break;
  1441. case 2: digitalWrite( Z_MS1_PIN,ms1); break;
  1442. case 3: digitalWrite(E0_MS1_PIN,ms1); break;
  1443. #if defined(E1_MS1_PIN) && E1_MS1_PIN > -1
  1444. case 4: digitalWrite(E1_MS1_PIN,ms1); break;
  1445. #endif
  1446. }
  1447. if(ms2 > -1) switch(driver)
  1448. {
  1449. case 0: digitalWrite( X_MS2_PIN,ms2); break;
  1450. case 1: digitalWrite( Y_MS2_PIN,ms2); break;
  1451. case 2: digitalWrite( Z_MS2_PIN,ms2); break;
  1452. case 3: digitalWrite(E0_MS2_PIN,ms2); break;
  1453. #if defined(E1_MS2_PIN) && E1_MS2_PIN > -1
  1454. case 4: digitalWrite(E1_MS2_PIN,ms2); break;
  1455. #endif
  1456. }
  1457. }
  1458. void microstep_mode(uint8_t driver, uint8_t stepping_mode)
  1459. {
  1460. switch(stepping_mode)
  1461. {
  1462. case 1: microstep_ms(driver,MICROSTEP1); break;
  1463. case 2: microstep_ms(driver,MICROSTEP2); break;
  1464. case 4: microstep_ms(driver,MICROSTEP4); break;
  1465. case 8: microstep_ms(driver,MICROSTEP8); break;
  1466. case 16: microstep_ms(driver,MICROSTEP16); break;
  1467. }
  1468. }
  1469. void microstep_readings()
  1470. {
  1471. SERIAL_PROTOCOLPGM("MS1,MS2 Pins\n");
  1472. SERIAL_PROTOCOLPGM("X: ");
  1473. SERIAL_PROTOCOL( digitalRead(X_MS1_PIN));
  1474. SERIAL_PROTOCOLLN( digitalRead(X_MS2_PIN));
  1475. SERIAL_PROTOCOLPGM("Y: ");
  1476. SERIAL_PROTOCOL( digitalRead(Y_MS1_PIN));
  1477. SERIAL_PROTOCOLLN( digitalRead(Y_MS2_PIN));
  1478. SERIAL_PROTOCOLPGM("Z: ");
  1479. SERIAL_PROTOCOL( digitalRead(Z_MS1_PIN));
  1480. SERIAL_PROTOCOLLN( digitalRead(Z_MS2_PIN));
  1481. SERIAL_PROTOCOLPGM("E0: ");
  1482. SERIAL_PROTOCOL( digitalRead(E0_MS1_PIN));
  1483. SERIAL_PROTOCOLLN( digitalRead(E0_MS2_PIN));
  1484. #if defined(E1_MS1_PIN) && E1_MS1_PIN > -1
  1485. SERIAL_PROTOCOLPGM("E1: ");
  1486. SERIAL_PROTOCOL( digitalRead(E1_MS1_PIN));
  1487. SERIAL_PROTOCOLLN( digitalRead(E1_MS2_PIN));
  1488. #endif
  1489. }
  1490. #endif //TMC2130