stepper.cpp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526
  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 <SPI.h>
  31. #endif
  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
  73. static bool check_endstops = false;
  74. #else
  75. static bool check_endstops = true;
  76. #endif
  77. static bool check_z_endstop = false;
  78. static uint8_t sg_homing_axis = 0xFF;
  79. static uint8_t sg_axis_stalled[2] = {0, 0};
  80. static uint8_t sg_lastHomingStalled = false;
  81. int8_t SilentMode;
  82. volatile long count_position[NUM_AXIS] = { 0, 0, 0, 0};
  83. volatile signed char count_direction[NUM_AXIS] = { 1, 1, 1, 1};
  84. //===========================================================================
  85. //=============================functions ============================
  86. //===========================================================================
  87. #define CHECK_ENDSTOPS if(check_endstops)
  88. // intRes = intIn1 * intIn2 >> 16
  89. // uses:
  90. // r26 to store 0
  91. // r27 to store the byte 1 of the 24 bit result
  92. #define MultiU16X8toH16(intRes, charIn1, intIn2) \
  93. asm volatile ( \
  94. "clr r26 \n\t" \
  95. "mul %A1, %B2 \n\t" \
  96. "movw %A0, r0 \n\t" \
  97. "mul %A1, %A2 \n\t" \
  98. "add %A0, r1 \n\t" \
  99. "adc %B0, r26 \n\t" \
  100. "lsr r0 \n\t" \
  101. "adc %A0, r26 \n\t" \
  102. "adc %B0, r26 \n\t" \
  103. "clr r1 \n\t" \
  104. : \
  105. "=&r" (intRes) \
  106. : \
  107. "d" (charIn1), \
  108. "d" (intIn2) \
  109. : \
  110. "r26" \
  111. )
  112. // intRes = longIn1 * longIn2 >> 24
  113. // uses:
  114. // r26 to store 0
  115. // r27 to store the byte 1 of the 48bit result
  116. #define MultiU24X24toH16(intRes, longIn1, longIn2) \
  117. asm volatile ( \
  118. "clr r26 \n\t" \
  119. "mul %A1, %B2 \n\t" \
  120. "mov r27, r1 \n\t" \
  121. "mul %B1, %C2 \n\t" \
  122. "movw %A0, r0 \n\t" \
  123. "mul %C1, %C2 \n\t" \
  124. "add %B0, r0 \n\t" \
  125. "mul %C1, %B2 \n\t" \
  126. "add %A0, r0 \n\t" \
  127. "adc %B0, r1 \n\t" \
  128. "mul %A1, %C2 \n\t" \
  129. "add r27, r0 \n\t" \
  130. "adc %A0, r1 \n\t" \
  131. "adc %B0, r26 \n\t" \
  132. "mul %B1, %B2 \n\t" \
  133. "add r27, r0 \n\t" \
  134. "adc %A0, r1 \n\t" \
  135. "adc %B0, r26 \n\t" \
  136. "mul %C1, %A2 \n\t" \
  137. "add r27, r0 \n\t" \
  138. "adc %A0, r1 \n\t" \
  139. "adc %B0, r26 \n\t" \
  140. "mul %B1, %A2 \n\t" \
  141. "add r27, r1 \n\t" \
  142. "adc %A0, r26 \n\t" \
  143. "adc %B0, r26 \n\t" \
  144. "lsr r27 \n\t" \
  145. "adc %A0, r26 \n\t" \
  146. "adc %B0, r26 \n\t" \
  147. "clr r1 \n\t" \
  148. : \
  149. "=&r" (intRes) \
  150. : \
  151. "d" (longIn1), \
  152. "d" (longIn2) \
  153. : \
  154. "r26" , "r27" \
  155. )
  156. // Some useful constants
  157. #define ENABLE_STEPPER_DRIVER_INTERRUPT() TIMSK1 |= (1<<OCIE1A)
  158. #define DISABLE_STEPPER_DRIVER_INTERRUPT() TIMSK1 &= ~(1<<OCIE1A)
  159. void checkHitEndstops()
  160. {
  161. if( endstop_x_hit || endstop_y_hit || endstop_z_hit) {
  162. SERIAL_ECHO_START;
  163. SERIAL_ECHORPGM(MSG_ENDSTOPS_HIT);
  164. if(endstop_x_hit) {
  165. SERIAL_ECHOPAIR(" X:",(float)endstops_trigsteps[X_AXIS]/axis_steps_per_unit[X_AXIS]);
  166. LCD_MESSAGERPGM(CAT2(MSG_ENDSTOPS_HIT, PSTR("X")));
  167. }
  168. if(endstop_y_hit) {
  169. SERIAL_ECHOPAIR(" Y:",(float)endstops_trigsteps[Y_AXIS]/axis_steps_per_unit[Y_AXIS]);
  170. LCD_MESSAGERPGM(CAT2(MSG_ENDSTOPS_HIT, PSTR("Y")));
  171. }
  172. if(endstop_z_hit) {
  173. SERIAL_ECHOPAIR(" Z:",(float)endstops_trigsteps[Z_AXIS]/axis_steps_per_unit[Z_AXIS]);
  174. LCD_MESSAGERPGM(CAT2(MSG_ENDSTOPS_HIT,PSTR("Z")));
  175. }
  176. SERIAL_ECHOLN("");
  177. endstop_x_hit=false;
  178. endstop_y_hit=false;
  179. endstop_z_hit=false;
  180. #if defined(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED) && defined(SDSUPPORT)
  181. if (abort_on_endstop_hit)
  182. {
  183. card.sdprinting = false;
  184. card.closefile();
  185. quickStop();
  186. setTargetHotend0(0);
  187. setTargetHotend1(0);
  188. setTargetHotend2(0);
  189. }
  190. #endif
  191. }
  192. }
  193. bool endstops_hit_on_purpose()
  194. {
  195. bool hit = endstop_x_hit || endstop_y_hit || endstop_z_hit;
  196. endstop_x_hit=false;
  197. endstop_y_hit=false;
  198. endstop_z_hit=false;
  199. return hit;
  200. }
  201. bool endstop_z_hit_on_purpose()
  202. {
  203. bool hit = endstop_z_hit;
  204. endstop_z_hit=false;
  205. return hit;
  206. }
  207. bool enable_endstops(bool check)
  208. {
  209. bool old = check_endstops;
  210. check_endstops = check;
  211. return old;
  212. }
  213. bool enable_z_endstop(bool check)
  214. {
  215. bool old = check_z_endstop;
  216. check_z_endstop = check;
  217. endstop_z_hit=false;
  218. return old;
  219. }
  220. // __________________________
  221. // /| |\ _________________ ^
  222. // / | | \ /| |\ |
  223. // / | | \ / | | \ s
  224. // / | | | | | \ p
  225. // / | | | | | \ e
  226. // +-----+------------------------+---+--+---------------+----+ e
  227. // | BLOCK 1 | BLOCK 2 | d
  228. //
  229. // time ----->
  230. //
  231. // The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates
  232. // first block->accelerate_until step_events_completed, then keeps going at constant speed until
  233. // step_events_completed reaches block->decelerate_after after which it decelerates until the trapezoid generator is reset.
  234. // The slope of acceleration is calculated with the leib ramp alghorithm.
  235. void st_wake_up() {
  236. // TCNT1 = 0;
  237. ENABLE_STEPPER_DRIVER_INTERRUPT();
  238. }
  239. void step_wait(){
  240. for(int8_t i=0; i < 6; i++){
  241. }
  242. }
  243. FORCE_INLINE unsigned short calc_timer(unsigned short step_rate) {
  244. unsigned short timer;
  245. if(step_rate > MAX_STEP_FREQUENCY) step_rate = MAX_STEP_FREQUENCY;
  246. if(step_rate > 20000) { // If steprate > 20kHz >> step 4 times
  247. step_rate = (step_rate >> 2)&0x3fff;
  248. step_loops = 4;
  249. }
  250. else if(step_rate > 10000) { // If steprate > 10kHz >> step 2 times
  251. step_rate = (step_rate >> 1)&0x7fff;
  252. step_loops = 2;
  253. }
  254. else {
  255. step_loops = 1;
  256. }
  257. if(step_rate < (F_CPU/500000)) step_rate = (F_CPU/500000);
  258. step_rate -= (F_CPU/500000); // Correct for minimal speed
  259. if(step_rate >= (8*256)){ // higher step rate
  260. unsigned short table_address = (unsigned short)&speed_lookuptable_fast[(unsigned char)(step_rate>>8)][0];
  261. unsigned char tmp_step_rate = (step_rate & 0x00ff);
  262. unsigned short gain = (unsigned short)pgm_read_word_near(table_address+2);
  263. MultiU16X8toH16(timer, tmp_step_rate, gain);
  264. timer = (unsigned short)pgm_read_word_near(table_address) - timer;
  265. }
  266. else { // lower step rates
  267. unsigned short table_address = (unsigned short)&speed_lookuptable_slow[0][0];
  268. table_address += ((step_rate)>>1) & 0xfffc;
  269. timer = (unsigned short)pgm_read_word_near(table_address);
  270. timer -= (((unsigned short)pgm_read_word_near(table_address+2) * (unsigned char)(step_rate & 0x0007))>>3);
  271. }
  272. if(timer < 100) { timer = 100; MYSERIAL.print(MSG_STEPPER_TOO_HIGH); MYSERIAL.println(step_rate); }//(20kHz this should never happen)
  273. return timer;
  274. }
  275. // Initializes the trapezoid generator from the current block. Called whenever a new
  276. // block begins.
  277. FORCE_INLINE void trapezoid_generator_reset() {
  278. deceleration_time = 0;
  279. // step_rate to timer interval
  280. OCR1A_nominal = calc_timer(current_block->nominal_rate);
  281. // make a note of the number of step loops required at nominal speed
  282. step_loops_nominal = step_loops;
  283. acc_step_rate = current_block->initial_rate;
  284. acceleration_time = calc_timer(acc_step_rate);
  285. OCR1A = acceleration_time;
  286. // SERIAL_ECHO_START;
  287. // SERIAL_ECHOPGM("advance :");
  288. // SERIAL_ECHO(current_block->advance/256.0);
  289. // SERIAL_ECHOPGM("advance rate :");
  290. // SERIAL_ECHO(current_block->advance_rate/256.0);
  291. // SERIAL_ECHOPGM("initial advance :");
  292. // SERIAL_ECHO(current_block->initial_advance/256.0);
  293. // SERIAL_ECHOPGM("final advance :");
  294. // SERIAL_ECHOLN(current_block->final_advance/256.0);
  295. }
  296. // "The Stepper Driver Interrupt" - This timer interrupt is the workhorse.
  297. // It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
  298. ISR(TIMER1_COMPA_vect)
  299. {
  300. // If there is no current block, attempt to pop one from the buffer
  301. if (current_block == NULL) {
  302. // Anything in the buffer?
  303. current_block = plan_get_current_block();
  304. if (current_block != NULL) {
  305. // The busy flag is set by the plan_get_current_block() call.
  306. // current_block->busy = true;
  307. trapezoid_generator_reset();
  308. counter_x = -(current_block->step_event_count >> 1);
  309. counter_y = counter_x;
  310. counter_z = counter_x;
  311. counter_e = counter_x;
  312. step_events_completed = 0;
  313. #ifdef Z_LATE_ENABLE
  314. if(current_block->steps_z > 0) {
  315. enable_z();
  316. OCR1A = 2000; //1ms wait
  317. return;
  318. }
  319. #endif
  320. }
  321. else {
  322. OCR1A=2000; // 1kHz.
  323. }
  324. }
  325. if (current_block != NULL) {
  326. // Set directions TO DO This should be done once during init of trapezoid. Endstops -> interrupt
  327. out_bits = current_block->direction_bits;
  328. // Set the direction bits (X_AXIS=A_AXIS and Y_AXIS=B_AXIS for COREXY)
  329. if((out_bits & (1<<X_AXIS))!=0){
  330. WRITE(X_DIR_PIN, INVERT_X_DIR);
  331. count_direction[X_AXIS]=-1;
  332. }
  333. else{
  334. WRITE(X_DIR_PIN, !INVERT_X_DIR);
  335. count_direction[X_AXIS]=1;
  336. }
  337. if((out_bits & (1<<Y_AXIS))!=0){
  338. WRITE(Y_DIR_PIN, INVERT_Y_DIR);
  339. #ifdef Y_DUAL_STEPPER_DRIVERS
  340. WRITE(Y2_DIR_PIN, !(INVERT_Y_DIR == INVERT_Y2_VS_Y_DIR));
  341. #endif
  342. count_direction[Y_AXIS]=-1;
  343. }
  344. else{
  345. WRITE(Y_DIR_PIN, !INVERT_Y_DIR);
  346. #ifdef Y_DUAL_STEPPER_DRIVERS
  347. WRITE(Y2_DIR_PIN, (INVERT_Y_DIR == INVERT_Y2_VS_Y_DIR));
  348. #endif
  349. count_direction[Y_AXIS]=1;
  350. }
  351. // Set direction en check limit switches
  352. #ifndef COREXY
  353. if ((out_bits & (1<<X_AXIS)) != 0) { // stepping along -X axis
  354. #else
  355. if ((((out_bits & (1<<X_AXIS)) != 0)&&(out_bits & (1<<Y_AXIS)) != 0)) { //-X occurs for -A and -B
  356. #endif
  357. CHECK_ENDSTOPS
  358. {
  359. {
  360. #if defined(X_MIN_PIN) && X_MIN_PIN > -1
  361. bool x_min_endstop=(READ(X_MIN_PIN) != X_MIN_ENDSTOP_INVERTING);
  362. #ifdef SG_HOMING
  363. x_min_endstop=false;
  364. #endif
  365. if(sg_homing_axis == X_AXIS && !x_min_endstop)
  366. x_min_endstop = sg_axis_stalled[X_AXIS];
  367. if(x_min_endstop && old_x_min_endstop && (current_block->steps_x > 0)) {
  368. endstops_trigsteps[X_AXIS] = count_position[X_AXIS];
  369. endstop_x_hit=true;
  370. step_events_completed = current_block->step_event_count;
  371. }
  372. old_x_min_endstop = x_min_endstop;
  373. #endif
  374. }
  375. }
  376. }
  377. else { // +direction
  378. CHECK_ENDSTOPS
  379. {
  380. {
  381. #if defined(X_MAX_PIN) && X_MAX_PIN > -1
  382. bool x_max_endstop=(READ(X_MAX_PIN) != X_MAX_ENDSTOP_INVERTING);
  383. if(sg_homing_axis == X_AXIS && !x_max_endstop)
  384. x_max_endstop = sg_axis_stalled[X_AXIS];
  385. if(x_max_endstop && old_x_max_endstop && (current_block->steps_x > 0)){
  386. endstops_trigsteps[X_AXIS] = count_position[X_AXIS];
  387. endstop_x_hit=true;
  388. step_events_completed = current_block->step_event_count;
  389. }
  390. old_x_max_endstop = x_max_endstop;
  391. #endif
  392. }
  393. }
  394. }
  395. #ifndef COREXY
  396. if ((out_bits & (1<<Y_AXIS)) != 0) { // -direction
  397. #else
  398. if ((((out_bits & (1<<X_AXIS)) != 0)&&(out_bits & (1<<Y_AXIS)) == 0)) { // -Y occurs for -A and +B
  399. #endif
  400. CHECK_ENDSTOPS
  401. {
  402. #if defined(Y_MIN_PIN) && Y_MIN_PIN > -1
  403. bool y_min_endstop=(READ(Y_MIN_PIN) != Y_MIN_ENDSTOP_INVERTING);
  404. #ifdef SG_HOMING
  405. y_min_endstop=false;
  406. #endif
  407. if(sg_homing_axis == Y_AXIS && !y_min_endstop)
  408. y_min_endstop = sg_axis_stalled[Y_AXIS];
  409. if(y_min_endstop && old_y_min_endstop && (current_block->steps_y > 0)) {
  410. endstops_trigsteps[Y_AXIS] = count_position[Y_AXIS];
  411. endstop_y_hit=true;
  412. step_events_completed = current_block->step_event_count;
  413. }
  414. old_y_min_endstop = y_min_endstop;
  415. #endif
  416. }
  417. }
  418. else { // +direction
  419. CHECK_ENDSTOPS
  420. {
  421. #if defined(Y_MAX_PIN) && Y_MAX_PIN > -1
  422. bool y_max_endstop=(READ(Y_MAX_PIN) != Y_MAX_ENDSTOP_INVERTING);
  423. if(sg_homing_axis == Y_AXIS && !y_max_endstop)
  424. y_max_endstop = sg_axis_stalled[Y_AXIS];
  425. if(y_max_endstop && old_y_max_endstop && (current_block->steps_y > 0)){
  426. endstops_trigsteps[Y_AXIS] = count_position[Y_AXIS];
  427. endstop_y_hit=true;
  428. step_events_completed = current_block->step_event_count;
  429. }
  430. old_y_max_endstop = y_max_endstop;
  431. #endif
  432. }
  433. }
  434. if ((out_bits & (1<<Z_AXIS)) != 0) { // -direction
  435. WRITE(Z_DIR_PIN,INVERT_Z_DIR);
  436. #ifdef Z_DUAL_STEPPER_DRIVERS
  437. WRITE(Z2_DIR_PIN,INVERT_Z_DIR);
  438. #endif
  439. count_direction[Z_AXIS]=-1;
  440. if(check_endstops && ! check_z_endstop)
  441. {
  442. #if defined(Z_MIN_PIN) && Z_MIN_PIN > -1
  443. bool z_min_endstop=(READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING);
  444. if(z_min_endstop && old_z_min_endstop && (current_block->steps_z > 0)) {
  445. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  446. endstop_z_hit=true;
  447. step_events_completed = current_block->step_event_count;
  448. }
  449. old_z_min_endstop = z_min_endstop;
  450. #endif
  451. }
  452. }
  453. else { // +direction
  454. WRITE(Z_DIR_PIN,!INVERT_Z_DIR);
  455. #ifdef Z_DUAL_STEPPER_DRIVERS
  456. WRITE(Z2_DIR_PIN,!INVERT_Z_DIR);
  457. #endif
  458. count_direction[Z_AXIS]=1;
  459. CHECK_ENDSTOPS
  460. {
  461. #if defined(Z_MAX_PIN) && Z_MAX_PIN > -1
  462. bool z_max_endstop=(READ(Z_MAX_PIN) != Z_MAX_ENDSTOP_INVERTING);
  463. if(z_max_endstop && old_z_max_endstop && (current_block->steps_z > 0)) {
  464. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  465. endstop_z_hit=true;
  466. step_events_completed = current_block->step_event_count;
  467. }
  468. old_z_max_endstop = z_max_endstop;
  469. #endif
  470. }
  471. }
  472. // Supporting stopping on a trigger of the Z-stop induction sensor, not only for the Z-minus movements.
  473. #if defined(Z_MIN_PIN) && Z_MIN_PIN > -1
  474. if(check_z_endstop) {
  475. // Check the Z min end-stop no matter what.
  476. // Good for searching for the center of an induction target.
  477. bool z_min_endstop=(READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING);
  478. if(z_min_endstop && old_z_min_endstop) {
  479. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  480. endstop_z_hit=true;
  481. step_events_completed = current_block->step_event_count;
  482. }
  483. old_z_min_endstop = z_min_endstop;
  484. }
  485. #endif
  486. if ((out_bits & (1<<E_AXIS)) != 0) { // -direction
  487. REV_E_DIR();
  488. count_direction[E_AXIS]=-1;
  489. }
  490. else { // +direction
  491. NORM_E_DIR();
  492. count_direction[E_AXIS]=1;
  493. }
  494. for(uint8_t i=0; i < step_loops; i++) { // Take multiple steps per interrupt (For high speed moves)
  495. #ifndef AT90USB
  496. MSerial.checkRx(); // Check for serial chars.
  497. #endif
  498. counter_x += current_block->steps_x;
  499. if (counter_x > 0) {
  500. WRITE(X_STEP_PIN, !INVERT_X_STEP_PIN);
  501. counter_x -= current_block->step_event_count;
  502. count_position[X_AXIS]+=count_direction[X_AXIS];
  503. WRITE(X_STEP_PIN, INVERT_X_STEP_PIN);
  504. }
  505. counter_y += current_block->steps_y;
  506. if (counter_y > 0) {
  507. WRITE(Y_STEP_PIN, !INVERT_Y_STEP_PIN);
  508. #ifdef Y_DUAL_STEPPER_DRIVERS
  509. WRITE(Y2_STEP_PIN, !INVERT_Y_STEP_PIN);
  510. #endif
  511. counter_y -= current_block->step_event_count;
  512. count_position[Y_AXIS]+=count_direction[Y_AXIS];
  513. WRITE(Y_STEP_PIN, INVERT_Y_STEP_PIN);
  514. #ifdef Y_DUAL_STEPPER_DRIVERS
  515. WRITE(Y2_STEP_PIN, INVERT_Y_STEP_PIN);
  516. #endif
  517. }
  518. counter_z += current_block->steps_z;
  519. if (counter_z > 0) {
  520. WRITE(Z_STEP_PIN, !INVERT_Z_STEP_PIN);
  521. #ifdef Z_DUAL_STEPPER_DRIVERS
  522. WRITE(Z2_STEP_PIN, !INVERT_Z_STEP_PIN);
  523. #endif
  524. counter_z -= current_block->step_event_count;
  525. count_position[Z_AXIS]+=count_direction[Z_AXIS];
  526. WRITE(Z_STEP_PIN, INVERT_Z_STEP_PIN);
  527. #ifdef Z_DUAL_STEPPER_DRIVERS
  528. WRITE(Z2_STEP_PIN, INVERT_Z_STEP_PIN);
  529. #endif
  530. }
  531. counter_e += current_block->steps_e;
  532. if (counter_e > 0) {
  533. WRITE_E_STEP(!INVERT_E_STEP_PIN);
  534. counter_e -= current_block->step_event_count;
  535. count_position[E_AXIS]+=count_direction[E_AXIS];
  536. WRITE_E_STEP(INVERT_E_STEP_PIN);
  537. }
  538. step_events_completed += 1;
  539. if(step_events_completed >= current_block->step_event_count) break;
  540. }
  541. // Calculare new timer value
  542. unsigned short timer;
  543. unsigned short step_rate;
  544. if (step_events_completed <= (unsigned long int)current_block->accelerate_until) {
  545. // v = t * a -> acc_step_rate = acceleration_time * current_block->acceleration_rate
  546. MultiU24X24toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate);
  547. acc_step_rate += current_block->initial_rate;
  548. // upper limit
  549. if(acc_step_rate > current_block->nominal_rate)
  550. acc_step_rate = current_block->nominal_rate;
  551. // step_rate to timer interval
  552. timer = calc_timer(acc_step_rate);
  553. OCR1A = timer;
  554. acceleration_time += timer;
  555. }
  556. else if (step_events_completed > (unsigned long int)current_block->decelerate_after) {
  557. MultiU24X24toH16(step_rate, deceleration_time, current_block->acceleration_rate);
  558. if(step_rate > acc_step_rate) { // Check step_rate stays positive
  559. step_rate = current_block->final_rate;
  560. }
  561. else {
  562. step_rate = acc_step_rate - step_rate; // Decelerate from aceleration end point.
  563. }
  564. // lower limit
  565. if(step_rate < current_block->final_rate)
  566. step_rate = current_block->final_rate;
  567. // step_rate to timer interval
  568. timer = calc_timer(step_rate);
  569. OCR1A = timer;
  570. deceleration_time += timer;
  571. }
  572. else {
  573. OCR1A = OCR1A_nominal;
  574. // ensure we're running at the correct step rate, even if we just came off an acceleration
  575. step_loops = step_loops_nominal;
  576. }
  577. // If current block is finished, reset pointer
  578. if (step_events_completed >= current_block->step_event_count) {
  579. current_block = NULL;
  580. plan_discard_current_block();
  581. }
  582. }
  583. check_fans();
  584. }
  585. #ifdef HAVE_TMC2130_DRIVERS
  586. uint32_t tmc2130_read(uint8_t chipselect, uint8_t address)
  587. {
  588. uint32_t val32;
  589. uint8_t val0;
  590. uint8_t val1;
  591. uint8_t val2;
  592. uint8_t val3;
  593. uint8_t val4;
  594. //datagram1 - read request (address + dummy write)
  595. SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE3));
  596. digitalWrite(chipselect,LOW);
  597. SPI.transfer(address);
  598. SPI.transfer(0);
  599. SPI.transfer(0);
  600. SPI.transfer(0);
  601. SPI.transfer(0);
  602. digitalWrite(chipselect, HIGH);
  603. SPI.endTransaction();
  604. //datagram2 - response
  605. SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE3));
  606. digitalWrite(chipselect,LOW);
  607. val0 = SPI.transfer(0);
  608. val1 = SPI.transfer(0);
  609. val2 = SPI.transfer(0);
  610. val3 = SPI.transfer(0);
  611. val4 = SPI.transfer(0);
  612. digitalWrite(chipselect, HIGH);
  613. SPI.endTransaction();
  614. #ifdef TMC_DBG_READS
  615. MYSERIAL.print("SPIRead 0x");
  616. MYSERIAL.print(address,HEX);
  617. MYSERIAL.print(" Status:");
  618. MYSERIAL.print(val0 & 0b00000111,BIN);
  619. MYSERIAL.print(" ");
  620. MYSERIAL.print(val1,BIN);
  621. MYSERIAL.print(" ");
  622. MYSERIAL.print(val2,BIN);
  623. MYSERIAL.print(" ");
  624. MYSERIAL.print(val3,BIN);
  625. MYSERIAL.print(" ");
  626. MYSERIAL.print(val4,BIN);
  627. #endif
  628. val32 = (uint32_t)val1<<24 | (uint32_t)val2<<16 | (uint32_t)val3<<8 | (uint32_t)val4;
  629. #ifdef TMC_DBG_READS
  630. MYSERIAL.print(" 0x");
  631. MYSERIAL.println(val32,HEX);
  632. #endif
  633. return val32;
  634. }
  635. void tmc2130_write(uint8_t chipselect, uint8_t address,uint8_t wval1,uint8_t wval2,uint8_t wval3,uint8_t wval4)
  636. {
  637. uint32_t val32;
  638. uint8_t val0;
  639. uint8_t val1;
  640. uint8_t val2;
  641. uint8_t val3;
  642. uint8_t val4;
  643. //datagram1 - write
  644. SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
  645. digitalWrite(chipselect,LOW);
  646. SPI.transfer(address+0x80);
  647. SPI.transfer(wval1);
  648. SPI.transfer(wval2);
  649. SPI.transfer(wval3);
  650. SPI.transfer(wval4);
  651. digitalWrite(chipselect, HIGH);
  652. SPI.endTransaction();
  653. //datagram2 - response
  654. SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
  655. digitalWrite(chipselect,LOW);
  656. val0 = SPI.transfer(0);
  657. val1 = SPI.transfer(0);
  658. val2 = SPI.transfer(0);
  659. val3 = SPI.transfer(0);
  660. val4 = SPI.transfer(0);
  661. digitalWrite(chipselect, HIGH);
  662. SPI.endTransaction();
  663. MYSERIAL.print("WriteRead 0x");
  664. MYSERIAL.print(address,HEX);
  665. MYSERIAL.print(" Status:");
  666. MYSERIAL.print(val0 & 0b00000111,BIN);
  667. MYSERIAL.print(" ");
  668. MYSERIAL.print(val1,BIN);
  669. MYSERIAL.print(" ");
  670. MYSERIAL.print(val2,BIN);
  671. MYSERIAL.print(" ");
  672. MYSERIAL.print(val3,BIN);
  673. MYSERIAL.print(" ");
  674. MYSERIAL.print(val4,BIN);
  675. val32 = (uint32_t)val1<<24 | (uint32_t)val2<<16 | (uint32_t)val3<<8 | (uint32_t)val4;
  676. MYSERIAL.print(" 0x");
  677. MYSERIAL.println(val32,HEX);
  678. }
  679. uint8_t tmc2130_read8(uint8_t chipselect, uint8_t address){
  680. //datagram1 - write
  681. SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
  682. digitalWrite(chipselect,LOW);
  683. SPI.transfer(address);
  684. SPI.transfer(0x00);
  685. SPI.transfer(0x00);
  686. SPI.transfer(0x00);
  687. SPI.transfer(0x00);
  688. digitalWrite(chipselect, HIGH);
  689. SPI.endTransaction();
  690. uint8_t val0;
  691. //datagram2 - response
  692. SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
  693. digitalWrite(chipselect,LOW);
  694. val0 = SPI.transfer(0);
  695. digitalWrite(chipselect, HIGH);
  696. SPI.endTransaction();
  697. return val0;
  698. }
  699. uint32_t tmc2130_readRegister(uint8_t chipselect, uint8_t address){
  700. //datagram1 - write
  701. SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
  702. digitalWrite(chipselect,LOW);
  703. SPI.transfer(address);
  704. SPI.transfer(0x00);
  705. SPI.transfer(0x00);
  706. SPI.transfer(0x00);
  707. SPI.transfer(0x00);
  708. digitalWrite(chipselect, HIGH);
  709. SPI.endTransaction();
  710. uint32_t val0;
  711. //datagram2 - response
  712. SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
  713. digitalWrite(chipselect,LOW);
  714. SPI.transfer(0); // ignore status bits
  715. val0 = SPI.transfer(0); // MSB
  716. val0 = (val0 << 8) | SPI.transfer(0);
  717. val0 = (val0 << 8) | SPI.transfer(0);
  718. val0 = (val0 << 8) | SPI.transfer(0); //LSB
  719. digitalWrite(chipselect, HIGH);
  720. SPI.endTransaction();
  721. return val0;
  722. }
  723. uint16_t tmc2130_readSG(uint8_t chipselect){
  724. uint8_t address = 0x6F;
  725. uint32_t registerValue = tmc2130_readRegister(chipselect, address);
  726. uint16_t val0 = registerValue & 0x3ff;
  727. return val0;
  728. }
  729. uint16_t tmc2130_readTStep(uint8_t chipselect){
  730. uint8_t address = 0x12;
  731. uint32_t registerValue = tmc2130_readRegister(chipselect, address);
  732. uint16_t val0 = 0;
  733. if(registerValue & 0x000f0000)
  734. val0 = 0xffff;
  735. else
  736. val0 = registerValue & 0xffff;
  737. return val0;
  738. }
  739. void tmc2130_chopconf(uint8_t cs, bool extrapolate256 = 0, uint16_t microstep_resolution = 16)
  740. {
  741. uint8_t mres=0b0100;
  742. if(microstep_resolution == 256) mres = 0b0000;
  743. if(microstep_resolution == 128) mres = 0b0001;
  744. if(microstep_resolution == 64) mres = 0b0010;
  745. if(microstep_resolution == 32) mres = 0b0011;
  746. if(microstep_resolution == 16) mres = 0b0100;
  747. if(microstep_resolution == 8) mres = 0b0101;
  748. if(microstep_resolution == 4) mres = 0b0110;
  749. if(microstep_resolution == 2) mres = 0b0111;
  750. if(microstep_resolution == 1) mres = 0b1000;
  751. mres |= extrapolate256 << 4; //bit28 intpol
  752. //tmc2130_write(cs,0x6C,mres,0x01,0x00,0xD3);
  753. tmc2130_write(cs,0x6C,mres,0x01,0x00,0xC3);
  754. }
  755. void tmc2130_PWMconf(uint8_t cs, uint8_t PWMautoScale = PWM_AUTOSCALE, uint8_t PWMfreq = PWM_FREQ, uint8_t PWMgrad = PWM_GRAD, uint8_t PWMampl = PWM_AMPL)
  756. {
  757. tmc2130_write(cs,0x70,0x00,(PWMautoScale+PWMfreq),PWMgrad,PWMampl); // TMC LJ -> For better readability changed to 0x00 and added PWMautoScale and PWMfreq
  758. }
  759. void tmc2130_PWMthreshold(uint8_t cs)
  760. {
  761. tmc2130_write(cs,0x13,0x00,0x00,0x00,0x00); // TMC LJ -> Adds possibility to swtich from stealthChop to spreadCycle automatically
  762. }
  763. void st_setSGHoming(uint8_t axis){
  764. sg_homing_axis = axis;
  765. }
  766. void st_resetSGflags(){
  767. sg_axis_stalled[X_AXIS] = false;
  768. sg_axis_stalled[Y_AXIS] = false;
  769. }
  770. uint8_t st_didLastHomingStall(){
  771. uint8_t returnValue = sg_lastHomingStalled;
  772. sg_lastHomingStalled = false;
  773. return returnValue;
  774. }
  775. void tmc2130_disable_motor(uint8_t driver)
  776. {
  777. uint8_t cs[4] = { X_TMC2130_CS, Y_TMC2130_CS, Z_TMC2130_CS, E0_TMC2130_CS };
  778. tmc2130_write(cs[driver],0x6C,0,01,0,0);
  779. }
  780. void tmc2130_check_overtemp()
  781. {
  782. const static char TMC_OVERTEMP_MSG[] PROGMEM = "TMC DRIVER OVERTEMP ";
  783. uint8_t cs[4] = { X_TMC2130_CS, Y_TMC2130_CS, Z_TMC2130_CS, E0_TMC2130_CS };
  784. static uint32_t checktime = 0;
  785. //drivers_disabled[0] = 1; //TEST
  786. if( millis() - checktime > 1000 ) {
  787. for(int i=0;i<4;i++) {
  788. uint32_t drv_status = tmc2130_read(cs[i], 0x6F); //0x6F DRV_STATUS
  789. if(drv_status & ((uint32_t)1<<26)) { // BIT 26 - over temp prewarning ~120C (+-20C)
  790. SERIAL_ERRORRPGM(TMC_OVERTEMP_MSG);
  791. SERIAL_ECHOLN(i);
  792. for(int x=0; x<4;x++) tmc2130_disable_motor(x);
  793. kill(TMC_OVERTEMP_MSG);
  794. }
  795. }
  796. checktime = millis();
  797. }
  798. }
  799. #endif //HAVE_TMC2130_DRIVERS
  800. void tmc2130_init()
  801. {
  802. #ifdef HAVE_TMC2130_DRIVERS
  803. uint8_t cs[4] = { X_TMC2130_CS, Y_TMC2130_CS, Z_TMC2130_CS, E0_TMC2130_CS };
  804. uint8_t current[4] = { 31, 31, 31, 31 };
  805. WRITE(X_TMC2130_CS, HIGH);
  806. WRITE(Y_TMC2130_CS, HIGH);
  807. WRITE(Z_TMC2130_CS, HIGH);
  808. WRITE(E0_TMC2130_CS, HIGH);
  809. SET_OUTPUT(X_TMC2130_CS);
  810. SET_OUTPUT(Y_TMC2130_CS);
  811. SET_OUTPUT(Z_TMC2130_CS);
  812. SET_OUTPUT(E0_TMC2130_CS);
  813. SPI.begin();
  814. for(int i=0;i<4;i++)
  815. {
  816. //tmc2130_write(cs[i],0x6C,0b10100,01,00,0xC5);
  817. tmc2130_chopconf(cs[i],1,16);
  818. tmc2130_write(cs[i],0x10,0,15,current[i],current[i]); //0x10 IHOLD_IRUN
  819. //tmc2130_write(cs[i],0x0,0,0,0,0x05); //address=0x0 GCONF EXT VREF
  820. tmc2130_write(cs[i],0x0,0,0,0,0x05); //address=0x0 GCONF EXT VREF - activate stealthChop
  821. //tmc2130_write(cs[i],0x11,0,0,0,0xA);
  822. // Uncomment lines below to use a different configuration (pwm_autoscale = 0) for XY axes
  823. // if(i==0 || i==1)
  824. // tmc2130_PWMconf(cs[i],PWM_AUTOSCALE_XY,PWM_FREQ_XY,PWM_GRAD_XY,PWM_AMPL_XY); //address=0x70 PWM_CONF //reset default=0x00050480
  825. // else
  826. tmc2130_PWMconf(cs[i]); //address=0x70 PWM_CONF //reset default=0x00050480
  827. tmc2130_PWMthreshold(cs[i]);
  828. }
  829. tmc2130_chopconf(cs[3],0,256);
  830. #endif
  831. }
  832. void st_init()
  833. {
  834. tmc2130_init(); //Initialize TMC2130 drivers
  835. digipot_init(); //Initialize Digipot Motor Current
  836. microstep_init(); //Initialize Microstepping Pins
  837. //Initialize Dir Pins
  838. #if defined(X_DIR_PIN) && X_DIR_PIN > -1
  839. SET_OUTPUT(X_DIR_PIN);
  840. #endif
  841. #if defined(X2_DIR_PIN) && X2_DIR_PIN > -1
  842. SET_OUTPUT(X2_DIR_PIN);
  843. #endif
  844. #if defined(Y_DIR_PIN) && Y_DIR_PIN > -1
  845. SET_OUTPUT(Y_DIR_PIN);
  846. #if defined(Y_DUAL_STEPPER_DRIVERS) && defined(Y2_DIR_PIN) && (Y2_DIR_PIN > -1)
  847. SET_OUTPUT(Y2_DIR_PIN);
  848. #endif
  849. #endif
  850. #if defined(Z_DIR_PIN) && Z_DIR_PIN > -1
  851. SET_OUTPUT(Z_DIR_PIN);
  852. #if defined(Z_DUAL_STEPPER_DRIVERS) && defined(Z2_DIR_PIN) && (Z2_DIR_PIN > -1)
  853. SET_OUTPUT(Z2_DIR_PIN);
  854. #endif
  855. #endif
  856. #if defined(E0_DIR_PIN) && E0_DIR_PIN > -1
  857. SET_OUTPUT(E0_DIR_PIN);
  858. #endif
  859. #if defined(E1_DIR_PIN) && (E1_DIR_PIN > -1)
  860. SET_OUTPUT(E1_DIR_PIN);
  861. #endif
  862. #if defined(E2_DIR_PIN) && (E2_DIR_PIN > -1)
  863. SET_OUTPUT(E2_DIR_PIN);
  864. #endif
  865. //Initialize Enable Pins - steppers default to disabled.
  866. #if defined(X_ENABLE_PIN) && X_ENABLE_PIN > -1
  867. SET_OUTPUT(X_ENABLE_PIN);
  868. if(!X_ENABLE_ON) WRITE(X_ENABLE_PIN,HIGH);
  869. #endif
  870. #if defined(X2_ENABLE_PIN) && X2_ENABLE_PIN > -1
  871. SET_OUTPUT(X2_ENABLE_PIN);
  872. if(!X_ENABLE_ON) WRITE(X2_ENABLE_PIN,HIGH);
  873. #endif
  874. #if defined(Y_ENABLE_PIN) && Y_ENABLE_PIN > -1
  875. SET_OUTPUT(Y_ENABLE_PIN);
  876. if(!Y_ENABLE_ON) WRITE(Y_ENABLE_PIN,HIGH);
  877. #if defined(Y_DUAL_STEPPER_DRIVERS) && defined(Y2_ENABLE_PIN) && (Y2_ENABLE_PIN > -1)
  878. SET_OUTPUT(Y2_ENABLE_PIN);
  879. if(!Y_ENABLE_ON) WRITE(Y2_ENABLE_PIN,HIGH);
  880. #endif
  881. #endif
  882. #if defined(Z_ENABLE_PIN) && Z_ENABLE_PIN > -1
  883. SET_OUTPUT(Z_ENABLE_PIN);
  884. if(!Z_ENABLE_ON) WRITE(Z_ENABLE_PIN,HIGH);
  885. #if defined(Z_DUAL_STEPPER_DRIVERS) && defined(Z2_ENABLE_PIN) && (Z2_ENABLE_PIN > -1)
  886. SET_OUTPUT(Z2_ENABLE_PIN);
  887. if(!Z_ENABLE_ON) WRITE(Z2_ENABLE_PIN,HIGH);
  888. #endif
  889. #endif
  890. #if defined(E0_ENABLE_PIN) && (E0_ENABLE_PIN > -1)
  891. SET_OUTPUT(E0_ENABLE_PIN);
  892. if(!E_ENABLE_ON) WRITE(E0_ENABLE_PIN,HIGH);
  893. #endif
  894. #if defined(E1_ENABLE_PIN) && (E1_ENABLE_PIN > -1)
  895. SET_OUTPUT(E1_ENABLE_PIN);
  896. if(!E_ENABLE_ON) WRITE(E1_ENABLE_PIN,HIGH);
  897. #endif
  898. #if defined(E2_ENABLE_PIN) && (E2_ENABLE_PIN > -1)
  899. SET_OUTPUT(E2_ENABLE_PIN);
  900. if(!E_ENABLE_ON) WRITE(E2_ENABLE_PIN,HIGH);
  901. #endif
  902. //endstops and pullups
  903. #if defined(X_MIN_PIN) && X_MIN_PIN > -1
  904. SET_INPUT(X_MIN_PIN);
  905. #ifdef ENDSTOPPULLUP_XMIN
  906. WRITE(X_MIN_PIN,HIGH);
  907. #endif
  908. #endif
  909. #if defined(Y_MIN_PIN) && Y_MIN_PIN > -1
  910. SET_INPUT(Y_MIN_PIN);
  911. #ifdef ENDSTOPPULLUP_YMIN
  912. WRITE(Y_MIN_PIN,HIGH);
  913. #endif
  914. #endif
  915. #if defined(Z_MIN_PIN) && Z_MIN_PIN > -1
  916. SET_INPUT(Z_MIN_PIN);
  917. #ifdef ENDSTOPPULLUP_ZMIN
  918. WRITE(Z_MIN_PIN,HIGH);
  919. #endif
  920. #endif
  921. #if defined(X_MAX_PIN) && X_MAX_PIN > -1
  922. SET_INPUT(X_MAX_PIN);
  923. #ifdef ENDSTOPPULLUP_XMAX
  924. WRITE(X_MAX_PIN,HIGH);
  925. #endif
  926. #endif
  927. #if defined(Y_MAX_PIN) && Y_MAX_PIN > -1
  928. SET_INPUT(Y_MAX_PIN);
  929. #ifdef ENDSTOPPULLUP_YMAX
  930. WRITE(Y_MAX_PIN,HIGH);
  931. #endif
  932. #endif
  933. #if defined(Z_MAX_PIN) && Z_MAX_PIN > -1
  934. SET_INPUT(Z_MAX_PIN);
  935. #ifdef ENDSTOPPULLUP_ZMAX
  936. WRITE(Z_MAX_PIN,HIGH);
  937. #endif
  938. #endif
  939. //Initialize Step Pins
  940. #if defined(X_STEP_PIN) && (X_STEP_PIN > -1)
  941. SET_OUTPUT(X_STEP_PIN);
  942. WRITE(X_STEP_PIN,INVERT_X_STEP_PIN);
  943. disable_x();
  944. #endif
  945. #if defined(X2_STEP_PIN) && (X2_STEP_PIN > -1)
  946. SET_OUTPUT(X2_STEP_PIN);
  947. WRITE(X2_STEP_PIN,INVERT_X_STEP_PIN);
  948. disable_x();
  949. #endif
  950. #if defined(Y_STEP_PIN) && (Y_STEP_PIN > -1)
  951. SET_OUTPUT(Y_STEP_PIN);
  952. WRITE(Y_STEP_PIN,INVERT_Y_STEP_PIN);
  953. #if defined(Y_DUAL_STEPPER_DRIVERS) && defined(Y2_STEP_PIN) && (Y2_STEP_PIN > -1)
  954. SET_OUTPUT(Y2_STEP_PIN);
  955. WRITE(Y2_STEP_PIN,INVERT_Y_STEP_PIN);
  956. #endif
  957. disable_y();
  958. #endif
  959. #if defined(Z_STEP_PIN) && (Z_STEP_PIN > -1)
  960. SET_OUTPUT(Z_STEP_PIN);
  961. WRITE(Z_STEP_PIN,INVERT_Z_STEP_PIN);
  962. #if defined(Z_DUAL_STEPPER_DRIVERS) && defined(Z2_STEP_PIN) && (Z2_STEP_PIN > -1)
  963. SET_OUTPUT(Z2_STEP_PIN);
  964. WRITE(Z2_STEP_PIN,INVERT_Z_STEP_PIN);
  965. #endif
  966. disable_z();
  967. #endif
  968. #if defined(E0_STEP_PIN) && (E0_STEP_PIN > -1)
  969. SET_OUTPUT(E0_STEP_PIN);
  970. WRITE(E0_STEP_PIN,INVERT_E_STEP_PIN);
  971. disable_e0();
  972. #endif
  973. #if defined(E1_STEP_PIN) && (E1_STEP_PIN > -1)
  974. SET_OUTPUT(E1_STEP_PIN);
  975. WRITE(E1_STEP_PIN,INVERT_E_STEP_PIN);
  976. disable_e1();
  977. #endif
  978. #if defined(E2_STEP_PIN) && (E2_STEP_PIN > -1)
  979. SET_OUTPUT(E2_STEP_PIN);
  980. WRITE(E2_STEP_PIN,INVERT_E_STEP_PIN);
  981. disable_e2();
  982. #endif
  983. // waveform generation = 0100 = CTC
  984. TCCR1B &= ~(1<<WGM13);
  985. TCCR1B |= (1<<WGM12);
  986. TCCR1A &= ~(1<<WGM11);
  987. TCCR1A &= ~(1<<WGM10);
  988. // output mode = 00 (disconnected)
  989. TCCR1A &= ~(3<<COM1A0);
  990. TCCR1A &= ~(3<<COM1B0);
  991. // Set the timer pre-scaler
  992. // Generally we use a divider of 8, resulting in a 2MHz timer
  993. // frequency on a 16MHz MCU. If you are going to change this, be
  994. // sure to regenerate speed_lookuptable.h with
  995. // create_speed_lookuptable.py
  996. TCCR1B = (TCCR1B & ~(0x07<<CS10)) | (2<<CS10);
  997. OCR1A = 0x4000;
  998. TCNT1 = 0;
  999. ENABLE_STEPPER_DRIVER_INTERRUPT();
  1000. enable_endstops(true); // Start with endstops active. After homing they can be disabled
  1001. sei();
  1002. }
  1003. // Block until all buffered steps are executed
  1004. void st_synchronize()
  1005. {
  1006. uint8_t delay = 0;
  1007. while( blocks_queued()) {
  1008. manage_heater();
  1009. // Vojtech: Don't disable motors inside the planner!
  1010. manage_inactivity(true);
  1011. lcd_update();
  1012. if(sg_homing_axis == X_AXIS || sg_homing_axis == Y_AXIS)
  1013. {
  1014. uint8_t axis;
  1015. if(sg_homing_axis == X_AXIS)
  1016. axis = X_TMC2130_CS;
  1017. else
  1018. axis = Y_TMC2130_CS;
  1019. uint16_t tstep = tmc2130_readTStep(axis);
  1020. // SERIAL_PROTOCOLLN(tstep);
  1021. if(tstep < TCOOLTHRS)
  1022. {
  1023. if(delay < 255) // wait for a few tens microsteps until stallGuard is used //todo: read out microsteps directly, instead of delay counter
  1024. delay++;
  1025. else
  1026. {
  1027. uint16_t sg = tmc2130_readSG(axis);
  1028. if(sg==0)
  1029. {
  1030. sg_axis_stalled[sg_homing_axis] = true;
  1031. sg_lastHomingStalled = true;
  1032. }
  1033. else
  1034. sg_axis_stalled[sg_homing_axis] = false;
  1035. // SERIAL_PROTOCOLLN(sg);
  1036. }
  1037. }
  1038. else
  1039. {
  1040. sg_axis_stalled[sg_homing_axis] = false;
  1041. delay = 0;
  1042. }
  1043. }
  1044. else
  1045. {
  1046. sg_axis_stalled[X_AXIS] = false;
  1047. sg_axis_stalled[Y_AXIS] = false;
  1048. }
  1049. }
  1050. }
  1051. void st_set_position(const long &x, const long &y, const long &z, const long &e)
  1052. {
  1053. CRITICAL_SECTION_START;
  1054. count_position[X_AXIS] = x;
  1055. count_position[Y_AXIS] = y;
  1056. count_position[Z_AXIS] = z;
  1057. count_position[E_AXIS] = e;
  1058. CRITICAL_SECTION_END;
  1059. }
  1060. void st_set_e_position(const long &e)
  1061. {
  1062. CRITICAL_SECTION_START;
  1063. count_position[E_AXIS] = e;
  1064. CRITICAL_SECTION_END;
  1065. }
  1066. long st_get_position(uint8_t axis)
  1067. {
  1068. long count_pos;
  1069. CRITICAL_SECTION_START;
  1070. count_pos = count_position[axis];
  1071. CRITICAL_SECTION_END;
  1072. return count_pos;
  1073. }
  1074. float st_get_position_mm(uint8_t axis)
  1075. {
  1076. float steper_position_in_steps = st_get_position(axis);
  1077. return steper_position_in_steps / axis_steps_per_unit[axis];
  1078. }
  1079. void finishAndDisableSteppers()
  1080. {
  1081. st_synchronize();
  1082. disable_x();
  1083. disable_y();
  1084. disable_z();
  1085. disable_e0();
  1086. disable_e1();
  1087. disable_e2();
  1088. }
  1089. void quickStop()
  1090. {
  1091. DISABLE_STEPPER_DRIVER_INTERRUPT();
  1092. while (blocks_queued()) plan_discard_current_block();
  1093. current_block = NULL;
  1094. ENABLE_STEPPER_DRIVER_INTERRUPT();
  1095. }
  1096. #ifdef BABYSTEPPING
  1097. void babystep(const uint8_t axis,const bool direction)
  1098. {
  1099. //MUST ONLY BE CALLED BY A ISR, it depends on that no other ISR interrupts this
  1100. //store initial pin states
  1101. switch(axis)
  1102. {
  1103. case X_AXIS:
  1104. {
  1105. enable_x();
  1106. uint8_t old_x_dir_pin= READ(X_DIR_PIN); //if dualzstepper, both point to same direction.
  1107. //setup new step
  1108. WRITE(X_DIR_PIN,(INVERT_X_DIR)^direction);
  1109. //perform step
  1110. WRITE(X_STEP_PIN, !INVERT_X_STEP_PIN);
  1111. {
  1112. volatile float x=1./float(axis+1)/float(axis+2); //wait a tiny bit
  1113. }
  1114. WRITE(X_STEP_PIN, INVERT_X_STEP_PIN);
  1115. //get old pin state back.
  1116. WRITE(X_DIR_PIN,old_x_dir_pin);
  1117. }
  1118. break;
  1119. case Y_AXIS:
  1120. {
  1121. enable_y();
  1122. uint8_t old_y_dir_pin= READ(Y_DIR_PIN); //if dualzstepper, both point to same direction.
  1123. //setup new step
  1124. WRITE(Y_DIR_PIN,(INVERT_Y_DIR)^direction);
  1125. //perform step
  1126. WRITE(Y_STEP_PIN, !INVERT_Y_STEP_PIN);
  1127. {
  1128. volatile float x=1./float(axis+1)/float(axis+2); //wait a tiny bit
  1129. }
  1130. WRITE(Y_STEP_PIN, INVERT_Y_STEP_PIN);
  1131. //get old pin state back.
  1132. WRITE(Y_DIR_PIN,old_y_dir_pin);
  1133. }
  1134. break;
  1135. case Z_AXIS:
  1136. {
  1137. enable_z();
  1138. uint8_t old_z_dir_pin= READ(Z_DIR_PIN); //if dualzstepper, both point to same direction.
  1139. //setup new step
  1140. WRITE(Z_DIR_PIN,(INVERT_Z_DIR)^direction^BABYSTEP_INVERT_Z);
  1141. #ifdef Z_DUAL_STEPPER_DRIVERS
  1142. WRITE(Z2_DIR_PIN,(INVERT_Z_DIR)^direction^BABYSTEP_INVERT_Z);
  1143. #endif
  1144. //perform step
  1145. WRITE(Z_STEP_PIN, !INVERT_Z_STEP_PIN);
  1146. #ifdef Z_DUAL_STEPPER_DRIVERS
  1147. WRITE(Z2_STEP_PIN, !INVERT_Z_STEP_PIN);
  1148. #endif
  1149. //wait a tiny bit
  1150. {
  1151. volatile float x=1./float(axis+1); //absolutely useless
  1152. }
  1153. WRITE(Z_STEP_PIN, INVERT_Z_STEP_PIN);
  1154. #ifdef Z_DUAL_STEPPER_DRIVERS
  1155. WRITE(Z2_STEP_PIN, INVERT_Z_STEP_PIN);
  1156. #endif
  1157. //get old pin state back.
  1158. WRITE(Z_DIR_PIN,old_z_dir_pin);
  1159. #ifdef Z_DUAL_STEPPER_DRIVERS
  1160. WRITE(Z2_DIR_PIN,old_z_dir_pin);
  1161. #endif
  1162. }
  1163. break;
  1164. default: break;
  1165. }
  1166. }
  1167. #endif //BABYSTEPPING
  1168. void digitalPotWrite(int address, int value) // From Arduino DigitalPotControl example
  1169. {
  1170. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  1171. digitalWrite(DIGIPOTSS_PIN,LOW); // take the SS pin low to select the chip
  1172. SPI.transfer(address); // send in the address and value via SPI:
  1173. SPI.transfer(value);
  1174. digitalWrite(DIGIPOTSS_PIN,HIGH); // take the SS pin high to de-select the chip:
  1175. //delay(10);
  1176. #endif
  1177. }
  1178. void EEPROM_read_st(int pos, uint8_t* value, uint8_t size)
  1179. {
  1180. do
  1181. {
  1182. *value = eeprom_read_byte((unsigned char*)pos);
  1183. pos++;
  1184. value++;
  1185. }while(--size);
  1186. }
  1187. void digipot_init() //Initialize Digipot Motor Current
  1188. {
  1189. EEPROM_read_st(EEPROM_SILENT,(uint8_t*)&SilentMode,sizeof(SilentMode));
  1190. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  1191. if(SilentMode == 0){
  1192. const uint8_t digipot_motor_current[] = DIGIPOT_MOTOR_CURRENT_LOUD;
  1193. }else{
  1194. const uint8_t digipot_motor_current[] = DIGIPOT_MOTOR_CURRENT;
  1195. }
  1196. SPI.begin();
  1197. pinMode(DIGIPOTSS_PIN, OUTPUT);
  1198. for(int i=0;i<=4;i++)
  1199. //digitalPotWrite(digipot_ch[i], digipot_motor_current[i]);
  1200. digipot_current(i,digipot_motor_current[i]);
  1201. #endif
  1202. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  1203. pinMode(MOTOR_CURRENT_PWM_XY_PIN, OUTPUT);
  1204. pinMode(MOTOR_CURRENT_PWM_Z_PIN, OUTPUT);
  1205. pinMode(MOTOR_CURRENT_PWM_E_PIN, OUTPUT);
  1206. if((SilentMode == 0) || (farm_mode) ){
  1207. motor_current_setting[0] = motor_current_setting_loud[0];
  1208. motor_current_setting[1] = motor_current_setting_loud[1];
  1209. motor_current_setting[2] = motor_current_setting_loud[2];
  1210. }else{
  1211. motor_current_setting[0] = motor_current_setting_silent[0];
  1212. motor_current_setting[1] = motor_current_setting_silent[1];
  1213. motor_current_setting[2] = motor_current_setting_silent[2];
  1214. }
  1215. digipot_current(0, motor_current_setting[0]);
  1216. digipot_current(1, motor_current_setting[1]);
  1217. digipot_current(2, motor_current_setting[2]);
  1218. //Set timer5 to 31khz so the PWM of the motor power is as constant as possible. (removes a buzzing noise)
  1219. TCCR5B = (TCCR5B & ~(_BV(CS50) | _BV(CS51) | _BV(CS52))) | _BV(CS50);
  1220. #endif
  1221. }
  1222. void digipot_current(uint8_t driver, int current)
  1223. {
  1224. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  1225. const uint8_t digipot_ch[] = DIGIPOT_CHANNELS;
  1226. digitalPotWrite(digipot_ch[driver], current);
  1227. #endif
  1228. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  1229. if (driver == 0) analogWrite(MOTOR_CURRENT_PWM_XY_PIN, (long)current * 255L / (long)MOTOR_CURRENT_PWM_RANGE);
  1230. if (driver == 1) analogWrite(MOTOR_CURRENT_PWM_Z_PIN, (long)current * 255L / (long)MOTOR_CURRENT_PWM_RANGE);
  1231. if (driver == 2) analogWrite(MOTOR_CURRENT_PWM_E_PIN, (long)current * 255L / (long)MOTOR_CURRENT_PWM_RANGE);
  1232. #endif
  1233. }
  1234. void microstep_init()
  1235. {
  1236. const uint8_t microstep_modes[] = MICROSTEP_MODES;
  1237. #if defined(E1_MS1_PIN) && E1_MS1_PIN > -1
  1238. pinMode(E1_MS1_PIN,OUTPUT);
  1239. pinMode(E1_MS2_PIN,OUTPUT);
  1240. #endif
  1241. #if defined(X_MS1_PIN) && X_MS1_PIN > -1
  1242. pinMode(X_MS1_PIN,OUTPUT);
  1243. pinMode(X_MS2_PIN,OUTPUT);
  1244. pinMode(Y_MS1_PIN,OUTPUT);
  1245. pinMode(Y_MS2_PIN,OUTPUT);
  1246. pinMode(Z_MS1_PIN,OUTPUT);
  1247. pinMode(Z_MS2_PIN,OUTPUT);
  1248. pinMode(E0_MS1_PIN,OUTPUT);
  1249. pinMode(E0_MS2_PIN,OUTPUT);
  1250. for(int i=0;i<=4;i++) microstep_mode(i,microstep_modes[i]);
  1251. #endif
  1252. }
  1253. void microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2)
  1254. {
  1255. if(ms1 > -1) switch(driver)
  1256. {
  1257. case 0: digitalWrite( X_MS1_PIN,ms1); break;
  1258. case 1: digitalWrite( Y_MS1_PIN,ms1); break;
  1259. case 2: digitalWrite( Z_MS1_PIN,ms1); break;
  1260. case 3: digitalWrite(E0_MS1_PIN,ms1); break;
  1261. #if defined(E1_MS1_PIN) && E1_MS1_PIN > -1
  1262. case 4: digitalWrite(E1_MS1_PIN,ms1); break;
  1263. #endif
  1264. }
  1265. if(ms2 > -1) switch(driver)
  1266. {
  1267. case 0: digitalWrite( X_MS2_PIN,ms2); break;
  1268. case 1: digitalWrite( Y_MS2_PIN,ms2); break;
  1269. case 2: digitalWrite( Z_MS2_PIN,ms2); break;
  1270. case 3: digitalWrite(E0_MS2_PIN,ms2); break;
  1271. #if defined(E1_MS2_PIN) && E1_MS2_PIN > -1
  1272. case 4: digitalWrite(E1_MS2_PIN,ms2); break;
  1273. #endif
  1274. }
  1275. }
  1276. void microstep_mode(uint8_t driver, uint8_t stepping_mode)
  1277. {
  1278. switch(stepping_mode)
  1279. {
  1280. case 1: microstep_ms(driver,MICROSTEP1); break;
  1281. case 2: microstep_ms(driver,MICROSTEP2); break;
  1282. case 4: microstep_ms(driver,MICROSTEP4); break;
  1283. case 8: microstep_ms(driver,MICROSTEP8); break;
  1284. case 16: microstep_ms(driver,MICROSTEP16); break;
  1285. }
  1286. }
  1287. void microstep_readings()
  1288. {
  1289. SERIAL_PROTOCOLPGM("MS1,MS2 Pins\n");
  1290. SERIAL_PROTOCOLPGM("X: ");
  1291. SERIAL_PROTOCOL( digitalRead(X_MS1_PIN));
  1292. SERIAL_PROTOCOLLN( digitalRead(X_MS2_PIN));
  1293. SERIAL_PROTOCOLPGM("Y: ");
  1294. SERIAL_PROTOCOL( digitalRead(Y_MS1_PIN));
  1295. SERIAL_PROTOCOLLN( digitalRead(Y_MS2_PIN));
  1296. SERIAL_PROTOCOLPGM("Z: ");
  1297. SERIAL_PROTOCOL( digitalRead(Z_MS1_PIN));
  1298. SERIAL_PROTOCOLLN( digitalRead(Z_MS2_PIN));
  1299. SERIAL_PROTOCOLPGM("E0: ");
  1300. SERIAL_PROTOCOL( digitalRead(E0_MS1_PIN));
  1301. SERIAL_PROTOCOLLN( digitalRead(E0_MS2_PIN));
  1302. #if defined(E1_MS1_PIN) && E1_MS1_PIN > -1
  1303. SERIAL_PROTOCOLPGM("E1: ");
  1304. SERIAL_PROTOCOL( digitalRead(E1_MS1_PIN));
  1305. SERIAL_PROTOCOLLN( digitalRead(E1_MS2_PIN));
  1306. #endif
  1307. }
  1308. static void check_fans() {
  1309. if (READ(TACH_0) != fan_state[0]) {
  1310. fan_edge_counter[0] ++;
  1311. fan_state[0] = READ(TACH_0);
  1312. }
  1313. if (READ(TACH_1) != fan_state[1]){
  1314. fan_edge_counter[1] ++;
  1315. fan_state[1] = READ(TACH_1);
  1316. }
  1317. }