TMCStepper
TMC2160Stepper.cpp
Go to the documentation of this file.
1 #include "TMCStepper.h"
2 #include "TMC_MACROS.h"
3 
4 TMC2160Stepper::TMC2160Stepper(uint16_t pinCS, float RS, int8_t link) : TMC2130Stepper(pinCS, RS, link)
5  { defaults(); }
6 TMC2160Stepper::TMC2160Stepper(uint16_t pinCS, float RS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link) :
7  TMC2130Stepper(pinCS, RS, pinMOSI, pinMISO, pinSCK, link)
8  { defaults(); }
9 TMC2160Stepper::TMC2160Stepper(uint16_t pinCS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link) :
10  TMC2130Stepper(pinCS, default_RS, pinMOSI, pinMISO, pinSCK, link)
11  { defaults(); }
12 
14  //set pins
15  pinMode(_pinCS, OUTPUT);
16  switchCSpin(HIGH);
17 
18  if (TMC_SW_SPI != nullptr) TMC_SW_SPI->init();
19 
20  GCONF(GCONF_register.sr);
21  CHOPCONF(CHOPCONF_register.sr);
22  COOLCONF(COOLCONF_register.sr);
23  PWMCONF(PWMCONF_register.sr);
24  IHOLD_IRUN(IHOLD_IRUN_register.sr);
25 
26  toff(8); //off_time(8);
27  tbl(1); //blank_time(24);
28 }
29 
31  SHORT_CONF_register.s2vs_level = 6;
32  SHORT_CONF_register.s2g_level = 6;
33  SHORT_CONF_register.shortfilter = 0b01;
34  SHORT_CONF_register.shortdelay = 0;
35  DRV_CONF_register.bbmtime = 0;
36  DRV_CONF_register.bbmclks = 4;
37  DRV_CONF_register.otselect = 0b00;
38  DRV_CONF_register.drvstrength = 0b10;
39  DRV_CONF_register.filt_isense = 0b00;
40  TPOWERDOWN_register.sr = 10;
41  //MSLUT0_register.sr = ???;
42  //MSLUT1_register.sr = ???;
43  //MSLUT2_register.sr = ???;
44  //MSLUT3_register.sr = ???;
45  //MSLUT4_register.sr = ???;
46  //MSLUT5_register.sr = ???;
47  //MSLUT6_register.sr = ???;
48  //MSLUT7_register.sr = ???;
49  //MSLUTSEL_register.sr = ???;
50  //MSLUTSTART_register.start_sin = 0;
51  //MSLUTSTART_register.start_sin90 = 247;
52  CHOPCONF_register.sr = 0x10410150;
53  PWMCONF_register.sr = 0xC40C001E;
54 }
55 
56 /*
57  Requested current = mA = I_rms/1000
58  Equation for current:
59  I_rms = GLOBALSCALER/256 * (CS+1)/32 * V_fs/R_sense * 1/sqrt(2)
60  Solve for GLOBALSCALER ->
61 
62  32 * 256 * sqrt(2) * I_rms * R_sense |
63  GLOBALSCALER = ------------------------------------ |
64  (CS + 1) * V_fs | V_fs = 0.325
65 
66 */
67 void TMC2160Stepper::rms_current(uint16_t mA) {
68  constexpr uint32_t V_fs = 325; // 0.325 * 1000
69  uint8_t CS = 31;
70  uint32_t scaler = 0; // = 256
71 
72  const uint16_t RS_scaled = Rsense * 0xFFFF; // Scale to 16b
73  uint32_t numerator = 11585; // 32 * 256 * sqrt(2)
74  numerator *= RS_scaled;
75  numerator >>= 8;
76  numerator *= mA;
77 
78  do {
79  uint32_t denominator = V_fs * 0xFFFF >> 8;
80  denominator *= CS+1;
81  scaler = numerator / denominator;
82 
83  if (scaler > 255) scaler = 0; // Maximum
84  else if (scaler < 128) CS--; // Try again with smaller CS
85  } while(0 < scaler && scaler < 128);
86 
87 
88  if (CS > 31)
89  CS = 31;
90 
91  GLOBAL_SCALER(scaler);
92  irun(CS);
94 }
95 void TMC2160Stepper::rms_current(uint16_t mA, float mult) {
96  holdMultiplier = mult;
97  rms_current(mA);
98 }
99 uint16_t TMC2160Stepper::cs2rms(uint8_t CS) {
100  uint16_t scaler = GLOBAL_SCALER();
101  if (!scaler) scaler = 256;
102  uint32_t numerator = scaler * (CS+1);
103  numerator *= 325;
104  numerator >>= (8+5); // Divide by 256 and 32
105  numerator *= 1000000;
106  uint32_t denominator = Rsense*1000;
107  denominator *= 1414;
108 
109  return numerator / denominator;
110 }
111 uint16_t TMC2160Stepper::rms_current() { return cs2rms(irun()); }
112 
114  GCONF(GCONF_register.sr);
115  IHOLD_IRUN(IHOLD_IRUN_register.sr);
116  TPOWERDOWN(TPOWERDOWN_register.sr);
117  TPWMTHRS(TPWMTHRS_register.sr);
118  TCOOLTHRS(TCOOLTHRS_register.sr);
119  THIGH(THIGH_register.sr);
120  XDIRECT(XDIRECT_register.sr);
121  VDCMIN(VDCMIN_register.sr);
122  CHOPCONF(CHOPCONF_register.sr);
123  COOLCONF(COOLCONF_register.sr);
124  DCCTRL(DCCTRL_register.sr);
125  PWMCONF(PWMCONF_register.sr);
126  SHORT_CONF(SHORT_CONF_register.sr);
127  DRV_CONF(DRV_CONF_register.sr);
128  GLOBAL_SCALER(GLOBAL_SCALER_register.sr);
129 }
130 
132 // R: IOIN
135 }
136 bool TMC2160Stepper::refl_step() { TMC2160_n::IOIN_t r{0}; r.sr = IOIN(); return r.refl_step; }
137 bool TMC2160Stepper::refr_dir() { TMC2160_n::IOIN_t r{0}; r.sr = IOIN(); return r.refr_dir; }
138 bool TMC2160Stepper::encb_dcen_cfg4() { TMC2160_n::IOIN_t r{0}; r.sr = IOIN(); return r.encb_dcen_cfg4; }
139 bool TMC2160Stepper::enca_dcin_cfg5() { TMC2160_n::IOIN_t r{0}; r.sr = IOIN(); return r.enca_dcin_cfg5; }
140 bool TMC2160Stepper::drv_enn() { TMC2160_n::IOIN_t r{0}; r.sr = IOIN(); return r.drv_enn; }
141 bool TMC2160Stepper::dco_cfg6() { TMC2160_n::IOIN_t r{0}; r.sr = IOIN(); return r.dco_cfg6; }
142 uint8_t TMC2160Stepper::version() { TMC2160_n::IOIN_t r{0}; r.sr = IOIN(); return r.version; }
143 
144 // W: GLOBAL_SCALER
145 uint8_t TMC2160Stepper::GLOBAL_SCALER() { return GLOBAL_SCALER_register.sr; }
146 void TMC2160Stepper::GLOBAL_SCALER(uint8_t input) {
147  GLOBAL_SCALER_register.sr = input;
148  write(GLOBAL_SCALER_register.address, GLOBAL_SCALER_register.sr);
149 }
150 
151 // R: OFFSET_READ
153 
154 // R: PWM_SCALE
157 }
158 uint8_t TMC2160Stepper::pwm_scale_sum() { TMC2160_n::PWM_SCALE_t r{0}; r.sr = PWM_SCALE(); return r.pwm_scale_sum; }
159 uint16_t TMC2160Stepper::pwm_scale_auto() { TMC2160_n::PWM_SCALE_t r{0}; r.sr = PWM_SCALE(); return r.pwm_scale_auto; }
OFFSET_READ_t::address
constexpr static uint8_t address
Definition: TMC5160_bitfields.h:41
TMC2130Stepper
Definition: TMCStepper.h:137
TMC2160Stepper::TMC2160Stepper
TMC2160Stepper(uint16_t pinCS, float RS=default_RS, int8_t link_index=-1)
Definition: TMC2160Stepper.cpp:4
TMC2130Stepper::toff
uint8_t toff()
Definition: CHOPCONF.cpp:32
TMC2130Stepper::THIGH
uint32_t THIGH()
Definition: TMC2130Stepper.cpp:244
TMC2160Stepper::drv_enn
bool drv_enn()
Definition: TMC2160Stepper.cpp:140
TMC2160Stepper::refl_step
bool refl_step()
Definition: TMC2160Stepper.cpp:136
TMC2160Stepper::DRV_CONF
uint32_t DRV_CONF()
Definition: DRV_CONF.cpp:8
TMC2160Stepper::SHORT_CONF
uint32_t SHORT_CONF()
Definition: SHORT_CONF.cpp:7
TMC2160Stepper::GLOBAL_SCALER
uint8_t GLOBAL_SCALER()
Definition: TMC2160Stepper.cpp:145
TMC2160_n::PWM_SCALE_t::sr
uint32_t sr
Definition: TMC2160_bitfields.h:44
TMC2160Stepper::dco_cfg6
bool dco_cfg6()
Definition: TMC2160Stepper.cpp:141
TMC2130Stepper::DCCTRL
uint32_t DCCTRL()
Definition: TMC2130Stepper.cpp:284
TMC2130Stepper::TMC_SW_SPI
SW_SPIClass * TMC_SW_SPI
Definition: TMCStepper.h:353
TMC2160Stepper::push
void push()
Definition: TMC2160Stepper.cpp:113
TMC2160Stepper::pwm_scale_sum
uint8_t pwm_scale_sum()
Definition: TMC2160Stepper.cpp:158
TMC2130Stepper::COOLCONF
uint32_t COOLCONF()
Definition: COOLCONF.cpp:8
TMCStepper.h
TMCStepper::Rsense
const float Rsense
Definition: TMCStepper.h:133
TMC2160_n::IOIN_t::sr
uint32_t sr
Definition: TMC2160_bitfields.h:8
TMC2130Stepper::VDCMIN
uint32_t VDCMIN()
Definition: TMC2130Stepper.cpp:264
TMC2130Stepper::switchCSpin
void switchCSpin(bool state)
Definition: TMC2130Stepper.cpp:61
TMC2160Stepper::encb_dcen_cfg4
bool encb_dcen_cfg4()
Definition: TMC2160Stepper.cpp:138
TMC2160Stepper::refr_dir
bool refr_dir()
Definition: TMC2160Stepper.cpp:137
TMC2160Stepper::PWMCONF
uint32_t PWMCONF()
Definition: PWMCONF.cpp:28
TMC2130Stepper::tbl
uint8_t tbl()
Definition: CHOPCONF.cpp:39
TMC2130Stepper::read
uint32_t read(uint8_t addressByte)
Definition: TMC2130Stepper.cpp:65
TMC2130Stepper::GCONF
uint32_t GCONF()
Definition: GCONF.cpp:7
TMC_MACROS.h
TMC2160_n::PWM_SCALE_t
Definition: TMC2160_bitfields.h:41
TMC2130Stepper::CHOPCONF
uint32_t CHOPCONF()
Definition: CHOPCONF.cpp:7
TMC2160Stepper::begin
void begin()
Definition: TMC2160Stepper.cpp:13
TMC2160Stepper::rms_current
uint16_t rms_current()
Definition: TMC2160Stepper.cpp:111
TMC2160Stepper::pwm_scale_auto
uint16_t pwm_scale_auto()
Definition: TMC2160Stepper.cpp:159
TMC2160Stepper::enca_dcin_cfg5
bool enca_dcin_cfg5()
Definition: TMC2160Stepper.cpp:139
SW_SPIClass::init
void init()
Definition: SW_SPI.cpp:9
TMC2160Stepper::cs2rms
uint16_t cs2rms(uint8_t CS)
Definition: TMC2160Stepper.cpp:99
TMCStepper::irun
uint8_t irun()
Definition: IHOLD_IRUN.cpp:19
TMCStepper::TPOWERDOWN
uint8_t TPOWERDOWN()
Definition: TMCStepper.cpp:123
TMC2130Stepper::TCOOLTHRS
uint32_t TCOOLTHRS()
Definition: TMC2130Stepper.cpp:237
TMC2160_n::PWM_SCALE_t::address
constexpr static uint8_t address
Definition: TMC2160_bitfields.h:42
TMC2160_n::IOIN_t::address
constexpr static uint8_t address
Definition: TMC2160_bitfields.h:6
TMC2130Stepper::_pinCS
const uint16_t _pinCS
Definition: TMCStepper.h:352
TMC2160Stepper::IOIN
uint32_t IOIN()
Definition: TMC2160Stepper.cpp:133
TMC2160Stepper::OFFSET_READ
uint16_t OFFSET_READ()
Definition: TMC2160Stepper.cpp:152
TMC2130Stepper::write
void write(uint8_t addressByte, uint32_t config)
Definition: TMC2130Stepper.cpp:148
TMCStepper::holdMultiplier
float holdMultiplier
Definition: TMCStepper.h:134
TMC2160Stepper::PWM_SCALE
uint32_t PWM_SCALE()
Definition: TMC2160Stepper.cpp:155
TMCStepper::IHOLD_IRUN
uint32_t IHOLD_IRUN()
Definition: IHOLD_IRUN.cpp:8
TMC2160Stepper::version
uint8_t version()
Definition: TMC2160Stepper.cpp:142
TMC2160Stepper::defaults
void defaults()
Definition: TMC2160Stepper.cpp:30
TMCStepper::ihold
uint8_t ihold()
Definition: IHOLD_IRUN.cpp:18
TMCStepper::TPWMTHRS
uint32_t TPWMTHRS()
Definition: TMCStepper.cpp:133
TMC2160_n::IOIN_t
Definition: TMC2160_bitfields.h:5
TMC2130Stepper::XDIRECT
uint32_t XDIRECT()
Definition: TMC2130Stepper.cpp:251