tmc2130.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #ifndef TMC2130_H
  2. #define TMC2130_H
  3. #include <stdint.h>
  4. //mode
  5. extern uint8_t tmc2130_mode;
  6. extern uint8_t tmc2130_current_h[4];
  7. extern uint8_t tmc2130_current_r[4];
  8. //microstep resolution (0 means 256usteps, 8 means 1ustep
  9. extern uint8_t tmc2130_mres[4];
  10. //flags for axis stall detection
  11. extern uint8_t tmc2130_sg_thr[4];
  12. extern bool tmc2130_sg_stop_on_crash;
  13. extern uint8_t tmc2130_sg_crash; //crash mask
  14. extern uint8_t tmc2130_sg_meassure;
  15. extern uint32_t tmc2130_sg_meassure_cnt;
  16. extern uint32_t tmc2130_sg_meassure_val;
  17. extern uint8_t tmc2130_sg_homing_axes_mask;
  18. extern const char eMotorCurrentScalingEnabled[];
  19. #define TMC2130_MODE_NORMAL 0
  20. #define TMC2130_MODE_SILENT 1
  21. #define TMC2130_WAVE_FAC1000_MIN 30
  22. #define TMC2130_WAVE_FAC1000_MAX 200
  23. #define TMC2130_WAVE_FAC1000_STP 1
  24. #define TMC2130_MINIMUM_PULSE 0 // minimum pulse width in uS
  25. #define TMC2130_SET_DIR_DELAY 20 // minimum delay after setting direction in uS
  26. #define TMC2130_SET_PWR_DELAY 0 // minimum delay after changing pwr mode in uS
  27. #ifdef TMC2130_DEDGE_STEPPING
  28. #define TMC2130_MINIMUM_DELAY //NOP
  29. #elif TMC2130_MINIMUM_PULSE == 0
  30. #define TMC2130_MINIMUM_DELAY asm("nop")
  31. #else
  32. #define TMC2130_MINIMUM_DELAY delayMicroseconds(TMC2130_MINIMUM_PULSE)
  33. #endif
  34. extern uint8_t tmc2130_home_enabled;
  35. extern uint8_t tmc2130_home_origin[2];
  36. extern uint8_t tmc2130_home_bsteps[2];
  37. extern uint8_t tmc2130_home_fsteps[2];
  38. extern uint8_t tmc2130_wave_fac[4];
  39. #pragma pack(push)
  40. #pragma pack(1)
  41. typedef struct
  42. {
  43. uint8_t toff:4;
  44. uint8_t hstr:3;
  45. uint8_t hend:4;
  46. uint8_t tbl:2;
  47. uint8_t res:3;
  48. } tmc2130_chopper_config_t;
  49. #pragma pack(pop)
  50. extern tmc2130_chopper_config_t tmc2130_chopper_config[4];
  51. //initialize tmc2130
  52. struct TMCInitParams {
  53. uint8_t bSuppressFlag : 1; // only relevant on MK3S with PSU_Delta
  54. uint8_t enableECool : 1; // experimental support for E-motor cooler operation
  55. inline TMCInitParams():bSuppressFlag(0), enableECool(0) { }
  56. inline explicit TMCInitParams(bool bSuppressFlag, bool enableECool):bSuppressFlag(bSuppressFlag), enableECool(enableECool) { }
  57. inline explicit TMCInitParams(bool enableECool)
  58. : bSuppressFlag(
  59. #ifdef PSU_delta
  60. 1
  61. #else
  62. 0
  63. #endif
  64. )
  65. , enableECool(enableECool) { }
  66. };
  67. extern void tmc2130_init(TMCInitParams params);
  68. //check diag pins (called from stepper isr)
  69. extern void tmc2130_st_isr();
  70. //update stall guard (called from st_synchronize inside the loop)
  71. extern bool tmc2130_update_sg();
  72. //temperature watching (called from )
  73. extern void tmc2130_check_overtemp();
  74. //enter homing (called from homeaxis before homing starts)
  75. extern void tmc2130_home_enter(uint8_t axes_mask);
  76. //exit homing (called from homeaxis after homing ends)
  77. extern void tmc2130_home_exit();
  78. //start stallguard meassuring for single axis
  79. extern void tmc2130_sg_meassure_start(uint8_t axis);
  80. //stop current stallguard meassuring and report result
  81. extern uint16_t tmc2130_sg_meassure_stop();
  82. extern void tmc2130_setup_chopper(uint8_t axis, uint8_t mres, uint8_t current_h, uint8_t current_r);
  83. //set holding current for any axis (M911)
  84. extern void tmc2130_set_current_h(uint8_t axis, uint8_t current);
  85. //set running current for any axis (M912)
  86. extern void tmc2130_set_current_r(uint8_t axis, uint8_t current);
  87. //print currents (M913)
  88. extern void tmc2130_print_currents();
  89. //set PWM_AMPL for any axis (M917)
  90. extern void tmc2130_set_pwm_ampl(uint8_t axis, uint8_t pwm_ampl);
  91. //set PWM_GRAD for any axis (M918)
  92. extern void tmc2130_set_pwm_grad(uint8_t axis, uint8_t pwm_ampl);
  93. extern uint16_t tmc2130_rd_MSCNT(uint8_t axis);
  94. extern uint32_t tmc2130_rd_MSCURACT(uint8_t axis);
  95. extern uint8_t tmc2130_usteps2mres(uint16_t usteps);
  96. #define tmc2130_mres2usteps(mres) ((uint16_t)256 >> mres)
  97. extern bool tmc2130_wait_standstill_xy(int timeout);
  98. extern uint16_t tmc2130_get_res(uint8_t axis);
  99. extern void tmc2130_set_res(uint8_t axis, uint16_t res);
  100. extern uint8_t tmc2130_get_pwr(uint8_t axis);
  101. extern void tmc2130_set_pwr(uint8_t axis, uint8_t pwr);
  102. extern uint8_t tmc2130_get_inv(uint8_t axis);
  103. extern uint8_t tmc2130_get_dir(uint8_t axis);
  104. extern void tmc2130_set_dir(uint8_t axis, uint8_t dir);
  105. extern void tmc2130_do_step(uint8_t axis);
  106. extern void tmc2130_do_steps(uint8_t axis, uint16_t steps, uint8_t dir, uint16_t delay_us);
  107. extern void tmc2130_goto_step(uint8_t axis, uint8_t step, uint8_t dir, uint16_t delay_us, uint16_t microstep_resolution);
  108. extern void tmc2130_get_wave(uint8_t axis, uint8_t* data, FILE* stream);
  109. extern void tmc2130_set_wave(uint8_t axis, uint8_t amp, uint8_t fac1000);
  110. extern bool tmc2130_home_calibrate(uint8_t axis);
  111. extern uint8_t tmc2130_cur2val(float cur);
  112. extern float tmc2130_val2cur(uint8_t val);
  113. #endif //TMC2130_H