stepper.cpp 51 KB

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