Skip to content

Commit bdedb50

Browse files
z1996xmthinkyhead
authored andcommitted
TMC2240 UART by z1996xm
1 parent 74e8e68 commit bdedb50

File tree

12 files changed

+1262
-56
lines changed

12 files changed

+1262
-56
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Supported TMC drivers:
66
* [TMC2208][2208]
77
* [TMC2209][2209]
88
* [TMC2224][2224]
9+
* [TMC2240][2240]
910
* [TMC2660][2660]
1011
* [TMC5130][5130]
1112
* [TMC5160][5160]
@@ -16,6 +17,7 @@ Supported TMC drivers:
1617
[2208]: https://teemuatlut.github.io/TMCStepper/class_t_m_c2208_stepper.html
1718
[2209]: https://teemuatlut.github.io/TMCStepper/class_t_m_c2209_stepper.html
1819
[2224]: https://teemuatlut.github.io/TMCStepper/class_t_m_c2224_stepper.html
20+
[2240]: https://teemuatlut.github.io/TMCStepper/class_t_m_c2240_stepper.html
1921
[2660]: https://teemuatlut.github.io/TMCStepper/class_t_m_c2660_stepper.html
2022
[5130]: https://teemuatlut.github.io/TMCStepper/class_t_m_c5130_stepper.html
2123
[5160]: https://teemuatlut.github.io/TMCStepper/class_t_m_c5160_stepper.html

src/TMCStepper.h

Lines changed: 303 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "source/TMC2208_bitfields.h"
5151
#include "source/TMC2209_bitfields.h"
5252
#include "source/TMC2660_bitfields.h"
53+
#include "source/TMC2240_bitfields.h"
5354

5455
#define INIT_REGISTER(REG) REG##_t REG##_register = REG##_t
5556
#define INIT2130_REGISTER(REG) TMC2130_n::REG##_t REG##_register = TMC2130_n::REG##_t
@@ -59,6 +60,7 @@
5960
#define INIT2660_REGISTER(REG) TMC2660_n::REG##_t REG##_register = TMC2660_n::REG##_t
6061
#define INIT2208_REGISTER(REG) TMC2208_n::REG##_t REG##_register = TMC2208_n::REG##_t
6162
#define INIT2224_REGISTER(REG) TMC2224_n::REG##_t REG##_register = TMC2224_n::REG##_t
63+
#define INIT2240_REGISTER(REG) TMC2240_n::REG##_t REG##_register = TMC2240_n::REG##_t
6264
#define SET_ALIAS(TYPE, DRIVER, NEW, ARG, OLD) TYPE (DRIVER::*NEW)(ARG) = &DRIVER::OLD
6365

6466
#define TMCSTEPPER_VERSION 0x000703 // v0.7.3
@@ -815,7 +817,7 @@ class TMC5160Stepper : public TMC5130Stepper {
815817

816818
class TMC5161Stepper : public TMC5160Stepper {
817819
public:
818-
TMC5161Stepper(uint16_t pinCS, float RS = default_RS, int8_t link_index = -1) : TMC5160Stepper(pinCS, RS, link_index) {}
820+
TMC5161Stepper(uint16_t pinCS, float RS = default_RS, int8_t link_index = -1) : TMC5160Stepper(pinCS, RS, link_index) {}
819821
TMC5161Stepper(uint16_t pinCS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link_index = -1) :
820822
TMC5160Stepper(pinCS, pinMOSI, pinMISO, pinSCK, link_index) {}
821823
TMC5161Stepper(uint16_t pinCS, float RS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link_index = -1) :
@@ -824,7 +826,7 @@ class TMC5161Stepper : public TMC5160Stepper {
824826

825827
class TMC2208Stepper : public TMCStepper {
826828
public:
827-
TMC2208Stepper(Stream * SerialPort, float RS, uint8_t addr, uint16_t mul_pin1, uint16_t mul_pin2);
829+
TMC2208Stepper(Stream * SerialPort, float RS, uint8_t addr, uint16_t mul_pin1, uint16_t mul_pin2);
828830
TMC2208Stepper(Stream * SerialPort, float RS) :
829831
TMC2208Stepper(SerialPort, RS, TMC2208_SLAVE_ADDR)
830832
{}
@@ -1101,6 +1103,305 @@ class TMC2224Stepper : public TMC2208Stepper {
11011103
uint8_t version();
11021104
};
11031105

1106+
class TMC2240Stepper {
1107+
public:
1108+
TMC2240Stepper(Stream * SerialPort):TMC2240Stepper(SerialPort, TMC2240_SLAVE_ADDR){};
1109+
TMC2240Stepper(Stream * SerialPort, uint8_t addr, uint16_t mul_pin1, uint16_t mul_pin2);
1110+
1111+
#if SW_CAPABLE_PLATFORM
1112+
TMC2240Stepper(uint16_t SW_RX_pin, uint16_t SW_TX_pin) : TMC2240Stepper(SW_RX_pin, SW_TX_pin,TMC2240_SLAVE_ADDR){};
1113+
1114+
// __attribute__((deprecated("Boolean argument has been deprecated and does nothing")))
1115+
// TMC2240Stepper(uint16_t SW_RX_pin, uint16_t SW_TX_pin, uint8_t) :
1116+
// TMC2240Stepper(SW_RX_pin, SW_TX_pin, TMC2240_SLAVE_ADDR)
1117+
// {};
1118+
#else
1119+
TMC2240Stepper(uint16_t, uint16_t, float) = delete; // Your platform does not currently support Software Serial
1120+
#endif
1121+
1122+
void defaults();
1123+
void push();
1124+
void begin();
1125+
#if SW_CAPABLE_PLATFORM
1126+
void beginSerial(uint32_t baudrate) __attribute__((weak));
1127+
#else
1128+
void beginSerial(uint32_t) = delete; // Your platform does not currently support Software Serial
1129+
#endif
1130+
1131+
bool isEnabled();
1132+
1133+
// RW: GCONF
1134+
void GCONF(uint32_t input);
1135+
void fast_standstill(bool B);
1136+
void en_pwm_mode(bool B);
1137+
void multistep_filt(bool B);
1138+
void shaft(bool B);
1139+
void diag0_error(bool B);
1140+
void diag0_otpw(bool B);
1141+
void diag0_stall(bool B);
1142+
void diag1_stall(bool B);
1143+
void diag1_index(bool B);
1144+
void diag1_onstate(bool B);
1145+
void diag0_pushpull(bool B);
1146+
void diag1_pushpull(bool B);
1147+
void small_hysteresis(bool B);
1148+
void stop_enable(bool B);
1149+
void direct_mode(bool B);
1150+
uint32_t GCONF();
1151+
bool fast_standstill();
1152+
bool en_pwm_mode();
1153+
bool multistep_filt();
1154+
bool shaft();
1155+
bool diag0_error();
1156+
bool diag0_otpw();
1157+
bool diag0_stall();
1158+
bool diag1_stall();
1159+
bool diag1_index();
1160+
bool diag1_onstate();
1161+
bool diag0_pushpull();
1162+
bool diag1_pushpull();
1163+
bool small_hysteresis();
1164+
bool stop_enable();
1165+
bool direct_mode();
1166+
1167+
// R+WC: GSTAT
1168+
void GSTAT(uint8_t input);
1169+
uint8_t GSTAT();
1170+
bool reset();
1171+
bool drv_err();
1172+
bool uv_cp();
1173+
bool register_reset();
1174+
bool vm_uvlo();
1175+
// R: IFCNT
1176+
1177+
uint8_t IFCNT();
1178+
1179+
// W: SLAVECONF
1180+
void SLAVECONF(uint16_t input);
1181+
uint16_t SLAVECONF();
1182+
void senddelay(uint8_t B);
1183+
uint8_t senddelay();
1184+
1185+
// R: IOIN
1186+
uint32_t IOIN();
1187+
bool step();
1188+
bool dir();
1189+
bool encb();
1190+
bool enca();
1191+
bool drv_enn();
1192+
bool encn();
1193+
bool uart_en();
1194+
bool comp_a();
1195+
bool comp_b();
1196+
bool comp_a1_a2();
1197+
bool comp_b1_b2();
1198+
bool output();
1199+
bool ext_res_det();
1200+
bool ext_clk();
1201+
bool adc_err();
1202+
uint8_t silicon_rv();
1203+
uint8_t version();
1204+
1205+
1206+
// W: DRV_CONF
1207+
void DRV_CONF(uint32_t input);
1208+
void current_range(uint8_t);
1209+
void slope_control(uint8_t);
1210+
uint32_t DRV_CONF();
1211+
1212+
1213+
// W: IHOLD_IRUN
1214+
void IHOLD_IRUN(uint32_t input);
1215+
uint32_t IHOLD_IRUN();
1216+
void ihold(uint8_t B);
1217+
void irun(uint8_t B);
1218+
void iholddelay(uint8_t B);
1219+
void irundelay(uint8_t B);
1220+
uint8_t ihold();
1221+
uint8_t irun();
1222+
uint8_t iholddelay();
1223+
uint8_t irundelay();
1224+
1225+
1226+
1227+
// RW: CHOPCONF
1228+
void CHOPCONF(uint32_t input);
1229+
void toff(uint8_t B);
1230+
void hstrt(uint8_t B);
1231+
void hend(uint8_t B);
1232+
void fd3(bool B);
1233+
void disfdcc(bool B);
1234+
void chm(bool B);
1235+
void TBL(uint8_t B);
1236+
void vhighfs(bool B);
1237+
void vhighchm(bool B);
1238+
void tpfd(uint8_t B);
1239+
void mres(uint8_t B);
1240+
void intpol(bool B);
1241+
void dedge(bool B);
1242+
void diss2g(bool B);
1243+
void diss2vs(bool B);
1244+
uint32_t CHOPCONF();
1245+
uint8_t toff();
1246+
uint8_t hstrt();
1247+
uint8_t hend();
1248+
bool fd3();
1249+
bool disfdcc();
1250+
bool chm();
1251+
uint8_t TBL();
1252+
bool vhighfs();
1253+
bool vhighchm();
1254+
uint8_t tpfd();
1255+
uint8_t mres();
1256+
bool intpol();
1257+
bool dedge();
1258+
bool diss2g();
1259+
bool diss2vs();
1260+
1261+
// RW: COOLCONF
1262+
void COOLCONF(uint32_t B);
1263+
uint32_t COOLCONF();
1264+
void semin(uint8_t B);
1265+
void seup(uint8_t B);
1266+
void semax(uint8_t B);
1267+
void sedn(uint8_t B);
1268+
void seimin(bool B);
1269+
void sgt(uint16_t B);
1270+
void sfilt(bool B);
1271+
uint8_t semin();
1272+
uint8_t seup();
1273+
uint8_t semax();
1274+
uint8_t sedn();
1275+
bool seimin();
1276+
uint16_t sgt();
1277+
bool sfilt();
1278+
1279+
// RW: PWMCONF
1280+
void PWMCONF(uint32_t input);
1281+
void pwm_ofs(uint8_t B);
1282+
void pwm_grad(uint8_t B);
1283+
void pwm_freq(uint8_t B);
1284+
void pwm_autoscale(bool B);
1285+
void pwm_autograd(bool B);
1286+
void freewheel(uint8_t B);
1287+
void pwm_meas_sd_enable(bool B);
1288+
void pwm_dis_reg_stst(bool B);
1289+
void pwm_reg(uint8_t B);
1290+
void pwm_lim(uint8_t B);
1291+
uint32_t PWMCONF();
1292+
uint8_t pwm_ofs();
1293+
uint8_t pwm_grad();
1294+
uint8_t pwm_freq();
1295+
bool pwm_autoscale();
1296+
bool pwm_autograd();
1297+
uint8_t freewheel();
1298+
bool pwm_meas_sd_enable();
1299+
bool pwm_dis_reg_stst();
1300+
uint8_t pwm_reg();
1301+
uint8_t pwm_lim();
1302+
1303+
// R: PWM_SCALE
1304+
uint32_t PWM_SCALE();
1305+
uint8_t pwm_scale_sum();
1306+
int16_t pwm_scale_auto();
1307+
1308+
// R: PWM_AUTO
1309+
uint32_t PWM_AUTO();
1310+
uint8_t pwm_ofs_auto();
1311+
uint8_t pwm_grad_auto();
1312+
1313+
uint32_t DRV_STATUS();
1314+
uint32_t SG_RESULT();
1315+
bool s2vsa();
1316+
bool s2vsb();
1317+
bool stealth();
1318+
bool fsactive();
1319+
uint16_t CS_ACTUAL();
1320+
bool stallguard();
1321+
bool ot();
1322+
bool otpw();
1323+
bool s2ga();
1324+
bool s2gb();
1325+
bool ola();
1326+
bool olb();
1327+
bool stst();
1328+
1329+
uint16_t bytesWritten = 0;
1330+
bool CRCerror = false;
1331+
1332+
void microsteps(uint16_t ms);
1333+
uint16_t microsteps();
1334+
1335+
uint8_t test_connection();
1336+
uint16_t MSCNT();
1337+
1338+
uint16_t cs2rms(uint8_t CS);
1339+
void rms_current(uint16_t mA);
1340+
void rms_current(uint16_t mA, float mult);
1341+
uint16_t rms_current();
1342+
1343+
void hysteresis_end(int8_t value);
1344+
int8_t hysteresis_end();
1345+
void hysteresis_start(uint8_t value);
1346+
uint8_t hysteresis_start();
1347+
1348+
uint8_t TPOWERDOWN();
1349+
void TPOWERDOWN(uint8_t input);
1350+
1351+
static constexpr uint8_t TMC_READ = 0x00,
1352+
1353+
TMC_WRITE = 0x80;
1354+
static constexpr uint8_t TMC2240_SLAVE_ADDR = 0x00;
1355+
protected:
1356+
INIT2240_REGISTER(GCONF) {{.sr=0}};
1357+
INIT2240_REGISTER(DRV_CONF) {{.sr=0}};
1358+
INIT2240_REGISTER(SLAVECONF) {{.sr=0}};
1359+
INIT2240_REGISTER(IHOLD_IRUN) {{.sr=0}}; // 32b
1360+
INIT2240_REGISTER(CHOPCONF) {{.sr=0}};
1361+
INIT2240_REGISTER(PWMCONF) {{.sr=0}};
1362+
INIT2240_REGISTER(TPOWERDOWN) {{.sr=0}}; // 8b
1363+
1364+
struct IFCNT_t { constexpr static uint8_t address = 0x02; };
1365+
struct TSTEP_t { constexpr static uint8_t address = 0x12; };
1366+
struct MSCNT_t { constexpr static uint8_t address = 0x6A; };
1367+
1368+
float calc_IFS_current_RMS(int8_t range, uint32_t Rref);
1369+
uint32_t set_globalscaler(float current, float IFS_current_RMS);
1370+
1371+
TMC2240Stepper(Stream * SerialPort, uint8_t addr);
1372+
#if SW_CAPABLE_PLATFORM
1373+
TMC2240Stepper(uint16_t SW_RX_pin, uint16_t SW_TX_pin, uint8_t addr);
1374+
#endif
1375+
1376+
Stream * HWSerial = nullptr;
1377+
#if SW_CAPABLE_PLATFORM
1378+
SoftwareSerial * SWSerial = nullptr;
1379+
const uint16_t RXTX_pin = 0; // Half duplex
1380+
#endif
1381+
1382+
1383+
SSwitch *sswitch = nullptr;
1384+
1385+
int available();
1386+
void preWriteCommunication();
1387+
void preReadCommunication();
1388+
int16_t serial_read();
1389+
uint8_t serial_write(const uint8_t data);
1390+
void postWriteCommunication();
1391+
void postReadCommunication();
1392+
void write(uint8_t, uint32_t);
1393+
uint32_t read(uint8_t);
1394+
const uint8_t slave_address;
1395+
uint8_t calcCRC(uint8_t datagram[], uint8_t len);
1396+
static constexpr uint8_t TMC2240_SYNC = 0x05;
1397+
static constexpr uint8_t replyDelay = 2;
1398+
static constexpr uint8_t abort_window = 5;
1399+
static constexpr uint8_t max_retries = 2;
1400+
1401+
uint64_t _sendDatagram(uint8_t [], const uint8_t, uint16_t);
1402+
float holdMultiplier = 0.5;
1403+
};
1404+
11041405
class TMC2660Stepper {
11051406
public:
11061407
TMC2660Stepper(uint16_t pinCS, float RS = default_RS);

0 commit comments

Comments
 (0)