stepper.cpp 51 KB

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