stepper.cpp 37 KB

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