tmc2130.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #ifndef TMC2130_H
  2. #define TMC2130_H
  3. //mode
  4. extern uint8_t tmc2130_mode;
  5. //holding and running currents
  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. #define TMC2130_MODE_NORMAL 0
  18. #define TMC2130_MODE_SILENT 1
  19. #define TMC2130_WAVE_FAC200_MIN 180
  20. #define TMC2130_WAVE_FAC200_MAX 250
  21. #define TMC2130_WAVE_FAC200_STP 1
  22. extern uint8_t tmc2130_home_enabled;
  23. extern uint8_t tmc2130_home_origin[2];
  24. extern uint8_t tmc2130_home_bsteps[2];
  25. extern uint8_t tmc2130_home_fsteps[2];
  26. extern uint8_t tmc2130_wave_fac[4];
  27. //initialize tmc2130
  28. extern void tmc2130_init();
  29. //check diag pins (called from stepper isr)
  30. extern void tmc2130_st_isr(uint8_t last_step_mask);
  31. //update stall guard (called from st_synchronize inside the loop)
  32. extern bool tmc2130_update_sg();
  33. //temperature watching (called from )
  34. extern void tmc2130_check_overtemp();
  35. //enter homing (called from homeaxis before homing starts)
  36. extern void tmc2130_home_enter(uint8_t axes_mask);
  37. //exit homing (called from homeaxis after homing ends)
  38. extern void tmc2130_home_exit();
  39. //start stallguard meassuring for single axis
  40. extern void tmc2130_sg_meassure_start(uint8_t axis);
  41. //stop current stallguard meassuring and report result
  42. extern uint16_t tmc2130_sg_meassure_stop();
  43. //set holding current for any axis (M911)
  44. extern void tmc2130_set_current_h(uint8_t axis, uint8_t current);
  45. //set running current for any axis (M912)
  46. extern void tmc2130_set_current_r(uint8_t axis, uint8_t current);
  47. //print currents (M913)
  48. extern void tmc2130_print_currents();
  49. //set PWM_AMPL for any axis (M917)
  50. extern void tmc2130_set_pwm_ampl(uint8_t axis, uint8_t pwm_ampl);
  51. //set PWM_GRAD for any axis (M918)
  52. extern void tmc2130_set_pwm_grad(uint8_t axis, uint8_t pwm_ampl);
  53. extern uint16_t tmc2130_rd_MSCNT(uint8_t axis);
  54. extern uint32_t tmc2130_rd_MSCURACT(uint8_t axis);
  55. extern uint8_t tmc2130_usteps2mres(uint16_t usteps);
  56. #define tmc2130_mres2usteps(mres) ((uint16_t)256 >> mres)
  57. extern bool tmc2130_wait_standstill_xy(int timeout);
  58. extern void tmc2130_eeprom_load_config();
  59. extern void tmc2130_eeprom_save_config();
  60. #pragma pack(push)
  61. #pragma pack(1)
  62. struct
  63. {
  64. uint8_t mres:5; // mres - byte 0, bit 0..4 microstep resolution
  65. uint8_t reserved_0_0:2; // reserved - byte 0, bit 5..6
  66. uint8_t intpol:1; // intpol - byte 0, bit 7 linear interpolation to 255 usteps
  67. uint8_t pwm_ampl:8; // pwm_ampl - byte 1, bit 0..7 pwm amplitude for silent mode
  68. uint8_t pwm_grad:4; // pwm_grad - byte 2, bit 0..3 pwm gradient for silent mode
  69. uint8_t pwm_freq:2; // pwm_freq - byte 2, bit 4..5 pwm frequency for silent mode
  70. uint8_t reserved_2_0:2; // reserved - byte 2, bit 6..7
  71. uint16_t tcoolthrs:16; // tcoolthrs - byte 3..4 coolstep threshold / middle sensitivity
  72. int8_t sg_thrs:8; // sg_thrs - byte 5, bit 0..7 stallguard sensitivity in high power / middle sensitivity
  73. int8_t current_h:6; // current_h - byte 6, bit 0..5 holding current for high power mode
  74. uint8_t reserved_6_0:2; // reserved - byte 6, bit 6..7
  75. int8_t current_r:6; // current_r - byte 7, bit 0..5 running current for high power mode
  76. uint8_t reserved_7_0:2; // reserved - byte 7, bit 6..7
  77. int8_t home_sg_thrs:8; // sg_thrs - byte 8, bit 0..7 stallguard sensitivity for homing
  78. int8_t home_current:6; // current_r - byte 9, bit 0..5 running current for homing
  79. uint8_t reserved_9_0:2; // reserved - byte 9, bit 6..7
  80. int8_t home_dtcoolthrs:8; // dtcoolthrs - byte 10, bit 0..7 delta tcoolthrs for homing
  81. int8_t dtcoolthrs_low:8; // dtcoolthrs - byte 11, bit 0..7 delta tcoolthrs for low sensitivity (based on value for middle sensitivity)
  82. int8_t dtcoolthrs_high:8; // dtcoolthrs - byte 12, bit 0..7 delta tcoolthrs for high sensitivity (based on value for middle sensitivity)
  83. int8_t sg_thrs_low:8; // sg_thrs - byte 13, bit 0..7 stallguard sensitivity in high power / low sensitivity
  84. int8_t sg_thrs_high:8; // sg_thrs - byte 14, bit 0..7 stallguard sensitivity in high power / high sensitivity
  85. } tmc2130_axis_config;
  86. #pragma pack(pop)
  87. extern uint16_t tmc2130_get_res(uint8_t axis);
  88. extern void tmc2130_set_res(uint8_t axis, uint16_t res);
  89. extern uint8_t tmc2130_get_pwr(uint8_t axis);
  90. extern void tmc2130_set_pwr(uint8_t axis, uint8_t pwr);
  91. extern uint8_t tmc2130_get_inv(uint8_t axis);
  92. extern uint8_t tmc2130_get_dir(uint8_t axis);
  93. extern void tmc2130_set_dir(uint8_t axis, uint8_t dir);
  94. extern void tmc2130_do_step(uint8_t axis);
  95. extern void tmc2130_do_steps(uint8_t axis, uint16_t steps, uint8_t dir, uint16_t delay_us);
  96. extern void tmc2130_goto_step(uint8_t axis, uint8_t step, uint8_t dir, uint16_t delay_us, uint16_t microstep_resolution);
  97. extern void tmc2130_get_wave(uint8_t axis, uint8_t* data, FILE* stream);
  98. extern void tmc2130_set_wave(uint8_t axis, uint8_t amp, uint8_t fac200);
  99. extern bool tmc2130_home_calibrate(uint8_t axis);
  100. #endif //TMC2130_H