Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
TMCStepper.h
00001 #pragma once 00002 00003 //#define TMCDEBUG 00004 00005 #include "mbed.h" 00006 00007 #define SW_CAPABLE_PLATFORM true 00008 00009 //#include "TMC2130_bitfields.h" 00010 //#include "TMC2160_bitfields.h" 00011 //#include "TMC5130_bitfields.h" 00012 //#include "TMC5160_bitfields.h" 00013 #include "TMC2208_bitfields.h" 00014 #include "TMC2209_bitfields.h" 00015 //#include "TMC2660_bitfields.h" 00016 00017 #define INIT_REGISTER(REG) REG##_t REG##_register = REG##_t 00018 //#define INIT2130_REGISTER(REG) TMC2130_n::REG##_t REG##_register = TMC2130_n::REG##_t 00019 //#define INIT2160_REGISTER(REG) TMC2160_n::REG##_t REG##_register = TMC2160_n::REG##_t 00020 //#define INIT5130_REGISTER(REG) TMC5130_n::REG##_t REG##_register = TMC5130_n::REG##_t 00021 //#define INIT5160_REGISTER(REG) TMC5160_n::REG##_t REG##_register = TMC5160_n::REG##_t 00022 //#define INIT2660_REGISTER(REG) TMC2660_n::REG##_t REG##_register = TMC2660_n::REG##_t 00023 #define INIT2208_REGISTER(REG) TMC2208_n::REG##_t REG##_register = TMC2208_n::REG##_t 00024 //#define INIT2224_REGISTER(REG) TMC2224_n::REG##_t REG##_register = TMC2224_n::REG##_t 00025 #define SET_ALIAS(TYPE, DRIVER, NEW, ARG, OLD) TYPE (DRIVER::*NEW)(ARG) = &DRIVER::OLD 00026 00027 #define TMCSTEPPER_VERSION 0x000701 // v0.7.1 00028 00029 #if SW_CAPABLE_PLATFORM 00030 #include <BufferedSerial.h> 00031 #include <Stream.h> 00032 #endif 00033 class TMCStepper { 00034 public: 00035 uint16_t cs2rms(uint8_t CS); 00036 void rms_current(uint16_t mA); 00037 void rms_current(uint16_t mA, float mult); 00038 uint16_t rms_current(); 00039 void hold_multiplier(float val) { holdMultiplier = val; } 00040 float hold_multiplier() { return holdMultiplier; } 00041 uint8_t test_connection(); 00042 00043 // Helper functions 00044 void microsteps(uint16_t ms); 00045 uint16_t microsteps(); 00046 void blank_time(uint8_t value); 00047 uint8_t blank_time(); 00048 void hysteresis_end(int8_t value); 00049 int8_t hysteresis_end(); 00050 void hysteresis_start(uint8_t value); 00051 uint8_t hysteresis_start(); 00052 00053 // R+WC: GSTAT 00054 void GSTAT( uint8_t input); 00055 uint8_t GSTAT(); 00056 bool reset(); 00057 bool drv_err(); 00058 bool uv_cp(); 00059 00060 // W: IHOLD_IRUN 00061 void IHOLD_IRUN( uint32_t input); 00062 uint32_t IHOLD_IRUN(); 00063 void ihold( uint8_t B); 00064 void irun( uint8_t B); 00065 void iholddelay( uint8_t B); 00066 uint8_t ihold(); 00067 uint8_t irun(); 00068 uint8_t iholddelay(); 00069 00070 // W: TPOWERDOWN 00071 uint8_t TPOWERDOWN(); 00072 void TPOWERDOWN( uint8_t input); 00073 00074 // R: TSTEP 00075 uint32_t TSTEP(); 00076 00077 // W: TPWMTHRS 00078 uint32_t TPWMTHRS(); 00079 void TPWMTHRS( uint32_t input); 00080 00081 // R: MSCNT 00082 uint16_t MSCNT(); 00083 00084 // R: MSCURACT 00085 uint32_t MSCURACT(); 00086 int16_t cur_a(); 00087 int16_t cur_b(); 00088 00089 protected: 00090 TMCStepper(float RS) : Rsense(RS) {}; 00091 INIT_REGISTER(IHOLD_IRUN){{.sr=0}}; // 32b 00092 INIT_REGISTER(TPOWERDOWN){.sr=0}; // 8b 00093 INIT_REGISTER(TPWMTHRS){.sr=0}; // 32b 00094 00095 static constexpr uint8_t TMC_READ = 0x00, 00096 TMC_WRITE = 0x80; 00097 00098 struct TSTEP_t { constexpr static uint8_t address = 0x12; }; 00099 struct MSCNT_t { constexpr static uint8_t address = 0x6A; }; 00100 00101 virtual void write(uint8_t, uint32_t) = 0; 00102 virtual uint32_t read(uint8_t) = 0; 00103 virtual void vsense(bool) = 0; 00104 virtual bool vsense(void) = 0; 00105 virtual uint32_t DRV_STATUS() = 0; 00106 virtual void hend(uint8_t) = 0; 00107 virtual uint8_t hend() = 0; 00108 virtual void hstrt(uint8_t) = 0; 00109 virtual uint8_t hstrt() = 0; 00110 virtual void mres(uint8_t) = 0; 00111 virtual uint8_t mres() = 0; 00112 virtual void tbl(uint8_t) = 0; 00113 virtual uint8_t tbl() = 0; 00114 00115 const float Rsense; 00116 float holdMultiplier = 0.5; 00117 }; 00118 00119 00120 class TMC2208Stepper : public TMCStepper { 00121 public: 00122 TMC2208Stepper(Stream * SerialPort, float RS, uint8_t addr, uint16_t mul_pin1, uint16_t mul_pin2); 00123 TMC2208Stepper(Stream * SerialPort, float RS) : 00124 TMC2208Stepper(SerialPort, RS, TMC2208_SLAVE_ADDR) 00125 {} 00126 #if SW_CAPABLE_PLATFORM 00127 TMC2208Stepper(PinName SW_RX_pin, PinName SW_TX_pin, float RS) : 00128 TMC2208Stepper(SW_RX_pin, SW_TX_pin, RS, TMC2208_SLAVE_ADDR) 00129 {} 00130 00131 __attribute__((deprecated("Boolean argument has been deprecated and does nothing"))) 00132 TMC2208Stepper(PinName SW_RX_pin, PinName SW_TX_pin, float RS, bool) : 00133 TMC2208Stepper(SW_RX_pin, SW_TX_pin, RS, TMC2208_SLAVE_ADDR) 00134 {}; 00135 #else 00136 TMC2208Stepper(PinName, PinName, float) = delete; // Your platform does not currently support Software Serial 00137 #endif 00138 void defaults(); 00139 void push(); 00140 void begin(); 00141 #if SW_CAPABLE_PLATFORM 00142 void beginSerial(uint32_t baudrate) __attribute__((weak)); 00143 #else 00144 void beginSerial(uint32_t) = delete; // Your platform does not currently support Software Serial 00145 #endif 00146 bool isEnabled(); 00147 00148 // RW: GCONF 00149 void GCONF(uint32_t input); 00150 void I_scale_analog(bool B); 00151 void internal_Rsense(bool B); 00152 void en_spreadCycle(bool B); 00153 void shaft(bool B); 00154 void index_otpw(bool B); 00155 void index_step(bool B); 00156 void pdn_disable(bool B); 00157 void mstep_reg_select(bool B); 00158 void multistep_filt(bool B); 00159 uint32_t GCONF(); 00160 bool I_scale_analog(); 00161 bool internal_Rsense(); 00162 bool en_spreadCycle(); 00163 bool shaft(); 00164 bool index_otpw(); 00165 bool index_step(); 00166 bool pdn_disable(); 00167 bool mstep_reg_select(); 00168 bool multistep_filt(); 00169 00170 // R: IFCNT 00171 uint8_t IFCNT(); 00172 00173 // W: SLAVECONF 00174 void SLAVECONF(uint16_t input); 00175 uint16_t SLAVECONF(); 00176 void senddelay(uint8_t B); 00177 uint8_t senddelay(); 00178 00179 // W: OTP_PROG 00180 void OTP_PROG(uint16_t input); 00181 00182 // R: OTP_READ 00183 uint32_t OTP_READ(); 00184 00185 // R: IOIN 00186 uint32_t IOIN(); 00187 bool enn(); 00188 bool ms1(); 00189 bool ms2(); 00190 bool diag(); 00191 bool pdn_uart(); 00192 bool step(); 00193 bool sel_a(); 00194 bool dir(); 00195 uint8_t version(); 00196 00197 // RW: FACTORY_CONF 00198 void FACTORY_CONF(uint16_t input); 00199 uint16_t FACTORY_CONF(); 00200 void fclktrim(uint8_t B); 00201 void ottrim(uint8_t B); 00202 uint8_t fclktrim(); 00203 uint8_t ottrim(); 00204 00205 // W: VACTUAL 00206 void VACTUAL(uint32_t input); 00207 uint32_t VACTUAL(); 00208 00209 // RW: CHOPCONF 00210 void CHOPCONF(uint32_t input); 00211 void toff(uint8_t B); 00212 void hstrt(uint8_t B); 00213 void hend(uint8_t B); 00214 void tbl(uint8_t B); 00215 void vsense(bool B); 00216 void mres(uint8_t B); 00217 void intpol(bool B); 00218 void dedge(bool B); 00219 void diss2g(bool B); 00220 void diss2vs(bool B); 00221 uint32_t CHOPCONF(); 00222 uint8_t toff(); 00223 uint8_t hstrt(); 00224 uint8_t hend(); 00225 uint8_t tbl(); 00226 bool vsense(); 00227 uint8_t mres(); 00228 bool intpol(); 00229 bool dedge(); 00230 bool diss2g(); 00231 bool diss2vs(); 00232 00233 // R: DRV_STATUS 00234 uint32_t DRV_STATUS(); 00235 bool otpw(); 00236 bool ot(); 00237 bool s2ga(); 00238 bool s2gb(); 00239 bool s2vsa(); 00240 bool s2vsb(); 00241 bool ola(); 00242 bool olb(); 00243 bool t120(); 00244 bool t143(); 00245 bool t150(); 00246 bool t157(); 00247 uint16_t cs_actual(); 00248 bool stealth(); 00249 bool stst(); 00250 00251 // RW: PWMCONF 00252 void PWMCONF(uint32_t input); 00253 void pwm_ofs(uint8_t B); 00254 void pwm_grad(uint8_t B); 00255 void pwm_freq(uint8_t B); 00256 void pwm_autoscale(bool B); 00257 void pwm_autograd(bool B); 00258 void freewheel(uint8_t B); 00259 void pwm_reg(uint8_t B); 00260 void pwm_lim(uint8_t B); 00261 uint32_t PWMCONF(); 00262 uint8_t pwm_ofs(); 00263 uint8_t pwm_grad(); 00264 uint8_t pwm_freq(); 00265 bool pwm_autoscale(); 00266 bool pwm_autograd(); 00267 uint8_t freewheel(); 00268 uint8_t pwm_reg(); 00269 uint8_t pwm_lim(); 00270 00271 // R: PWM_SCALE 00272 uint32_t PWM_SCALE(); 00273 uint8_t pwm_scale_sum(); 00274 int16_t pwm_scale_auto(); 00275 00276 // R: PWM_AUTO (0x72) 00277 uint32_t PWM_AUTO(); 00278 uint8_t pwm_ofs_auto(); 00279 uint8_t pwm_grad_auto(); 00280 00281 uint16_t bytesWritten = 0; 00282 float Rsense = 0.11; 00283 bool CRCerror = false; 00284 protected: 00285 INIT2208_REGISTER(GCONF) {{.sr=0}}; 00286 INIT_REGISTER(SLAVECONF) {{.sr=0}}; 00287 INIT_REGISTER(FACTORY_CONF) {{.sr=0}}; 00288 INIT2208_REGISTER(VACTUAL) {.sr=0}; 00289 INIT2208_REGISTER(CHOPCONF) {{.sr=0}}; 00290 INIT2208_REGISTER(PWMCONF) {{.sr=0}}; 00291 00292 struct IFCNT_t { constexpr static uint8_t address = 0x02; }; 00293 struct OTP_PROG_t { constexpr static uint8_t address = 0x04; }; 00294 struct OTP_READ_t { constexpr static uint8_t address = 0x05; }; 00295 00296 TMC2208Stepper(Stream * SerialPort, float RS, uint8_t addr); 00297 #if SW_CAPABLE_PLATFORM 00298 TMC2208Stepper(PinName SW_RX_pin, PinName SW_TX_pin, float RS, uint8_t addr); 00299 #endif 00300 00301 Stream * HWSerial = nullptr; 00302 #if SW_CAPABLE_PLATFORM 00303 BufferedSerial * SWSerial = nullptr; 00304 const uint16_t RXTX_pin = 0; // Half duplex 00305 #endif 00306 00307 // SSwitch *sswitch = nullptr; 00308 00309 int available(); 00310 void preWriteCommunication(); 00311 void preReadCommunication(); 00312 int16_t serial_read(); 00313 uint8_t serial_write(const uint8_t data); 00314 void postWriteCommunication(); 00315 void postReadCommunication(); 00316 void write(uint8_t, uint32_t); 00317 uint32_t read(uint8_t); 00318 const uint8_t slave_address; 00319 uint8_t calcCRC(uint8_t datagram[], uint8_t len); 00320 static constexpr uint8_t TMC2208_SYNC = 0x05, 00321 TMC2208_SLAVE_ADDR = 0x00; 00322 static constexpr uint8_t replyDelay = 2; //ms 00323 static constexpr uint8_t abort_window = 5; 00324 static constexpr uint8_t max_retries = 2; 00325 00326 uint64_t _sendDatagram(uint8_t [], const uint8_t, uint16_t); 00327 }; 00328 00329 class TMC2209Stepper : public TMC2208Stepper { 00330 public: 00331 TMC2209Stepper(Stream * SerialPort, float RS, uint8_t addr) : 00332 TMC2208Stepper(SerialPort, RS, addr) {} 00333 00334 #if SW_CAPABLE_PLATFORM 00335 TMC2209Stepper(PinName SW_RX_pin, PinName SW_TX_pin, float RS, uint8_t addr) : 00336 TMC2208Stepper(SW_RX_pin, SW_TX_pin, RS, addr) {} 00337 #else 00338 TMC2209Stepper(PinName, PinName, float, uint8_t) = delete; // Your platform does not currently support Software Serial 00339 #endif 00340 void push(); 00341 00342 // R: IOIN 00343 uint32_t IOIN(); 00344 bool enn(); 00345 bool ms1(); 00346 bool ms2(); 00347 bool diag(); 00348 bool pdn_uart(); 00349 bool step(); 00350 bool spread_en(); 00351 bool dir(); 00352 uint8_t version(); 00353 00354 // W: TCOOLTHRS 00355 uint32_t TCOOLTHRS(); 00356 void TCOOLTHRS(uint32_t input); 00357 00358 // W: SGTHRS 00359 void SGTHRS(uint8_t B); 00360 uint8_t SGTHRS(); 00361 00362 // R: SG_RESULT 00363 uint16_t SG_RESULT(); 00364 00365 // W: COOLCONF 00366 void COOLCONF(uint16_t B); 00367 uint16_t COOLCONF(); 00368 void semin(uint8_t B); 00369 void seup(uint8_t B); 00370 void semax(uint8_t B); 00371 void sedn(uint8_t B); 00372 void seimin(bool B); 00373 uint8_t semin(); 00374 uint8_t seup(); 00375 uint8_t semax(); 00376 uint8_t sedn(); 00377 bool seimin(); 00378 00379 protected: 00380 INIT_REGISTER(TCOOLTHRS){.sr=0}; 00381 TMC2209_n::SGTHRS_t SGTHRS_register{.sr=0}; 00382 TMC2209_n::COOLCONF_t COOLCONF_register{{.sr=0}}; 00383 };
Generated on Wed Jul 13 2022 22:52:51 by
1.7.2