stepper.cpp 35 KB

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