stepper.cpp 51 KB

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