TMC7300
Loading...
Searching...
No Matches
TMC7300.h
Go to the documentation of this file.
1#ifndef TMC7300_H
2#define TMC7300_H
3#include "TMC7300_Map.h"
4#include "TMCSerial.h"
5#include <Arduino.h>
6
7#define TMC_WRITE_BIT 0x80
8
13class TMC7300IC {
14public:
21 TMC7300IC(uint8_t _pin, uint8_t _chipAddress, uint32_t _baudrate = 100000)
22 : pin(_pin)
23 , chipAddress(_chipAddress)
24 {
25 _baudrate = constrain(_baudrate, 10000, 100000);
26 uartDelay = 1000000 / _baudrate;
27 }
33 void begin(boolean _extcap = true, uint8_t _irun = 31)
34 {
35 extcap = _extcap;
36 pinMode(pin, OUTPUT);
37 digitalWrite(pin, HIGH);
38 delay(10);
40 writeField(TMC7300_EXTCAP, extcap, true); // capacitor on vcp
42 writeField(TMC7300_IRUN, _irun, true);
43 }
50 {
51 int didSomething = 0;
52 // if drv_error is set, disable and re-enable the driver
53 if (readField(TMC7300_PWM_DIRECT) != 1) {
55 didSomething |= 0b100;
56 }
57 if (readField(TMC7300_ENABLEDRV) != 1) {
60 didSomething |= 0b01000;
61 }
62 if (readField(TMC7300_DRV_ERR) == 1) {
65 didSomething |= 0b1;
66 }
67 return didSomething;
68 }
69
70protected:
71 uint8_t pin;
72 uint8_t chipAddress;
73 uint32_t uartDelay;
74 boolean extcap;
75
76 // clang-format off
77 uint32_t registers[10] = {
78// 0 1 2 3 4 5 6 7 8 9
79 0, 0, 0, 0, 0, (uint32_t)0b1111100000001, 0, (uint32_t)0x13008001, 0, (uint32_t)0xC40D1024};
80 // clang-format on
81
82public:
90 void writeField(TMCField field, uint32_t fieldValue, boolean write = true)
91 {
92 uint32_t registerValueToSend = field.setField(fieldValue, registers[field.valueAddress()]);
93 if (write) {
94 writeRegister(field.address(), registerValueToSend);
95 }
96 registers[field.valueAddress()] = registerValueToSend;
97 }
105 uint32_t readField(TMCField field, boolean read = true)
106 {
107 uint32_t registerValueReceived = 0;
108 if (read) {
109 if (readRegister(field.address(), registerValueReceived)) {
110 registers[field.valueAddress()] = registerValueReceived;
111 }
112 }
113 return field.getField(registers[field.valueAddress()]);
114 }
122 void writeRegister(uint8_t address, uint32_t value)
123 {
124 // from https://github.com/bread-wolf/TMCSerial/blob/cb87877ab4f3c39ed7a5b1ec99cf73eb0f4d5a6c/TMCSerial.cpp#L78C4-L94C6
125 uint8_t addressWrite = address | TMC_WRITE_BIT;
126 uint8_t writeRequest[8] = { SYNC_BYTE, chipAddress, addressWrite, 0, 0, 0, 0, 0 };
127
128 /* Fill data to be written */
129 writeRequest[3] = (value >> 24) & 0xFF;
130 writeRequest[4] = (value >> 16) & 0xFF;
131 writeRequest[5] = (value >> 8) & 0xFF;
132 writeRequest[6] = (value) & 0xFF;
133
134 /* Calculate CRC */
135 writeRequest[7] = calcCRC(writeRequest, 7);
136
137 pinMode(pin, OUTPUT);
138 digitalWrite(pin, HIGH);
139 /* Write data */
140 unsigned long m = micros();
141 for (int j = 0; j < 8; j++) {
142 digitalWrite(pin, LOW); // Start bit
143 while (micros() - m < uartDelay) { };
144 m += uartDelay;
145 for (int i = 0; i < 8; i++) {
146 digitalWrite(pin, (writeRequest[j] >> i) & 0x01);
147 while (micros() - m < uartDelay) { };
148 m += uartDelay;
149 }
150 digitalWrite(pin, HIGH); // Stop bit
151 while (micros() - m < uartDelay) { };
152 m += uartDelay;
153 }
154 delay(1); // prevent messages from being too close together
155 }
163 boolean readRegister(uint8_t address, uint32_t& value)
164 {
165 uint8_t addressRead = address & 0b01111111;
166 uint8_t readRequest[4] = { SYNC_BYTE, chipAddress, addressRead, 0 };
167 readRequest[3] = calcCRC(readRequest, 3);
168
169 uint8_t response[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
170
171 pinMode(pin, OUTPUT);
172 digitalWrite(pin, HIGH);
173
174 uint8_t j = 0;
175
176 unsigned long m = micros();
177 /* Write data */
178 for (j = 0; j < 4; j++) {
179 digitalWrite(pin, LOW); // Start bit
180 while (micros() - m < uartDelay) { };
181 m += uartDelay;
182 for (uint8_t i = 0; i < 8; i++) {
183 digitalWrite(pin, (readRequest[j] >> i) & 0x01);
184 while (micros() - m < uartDelay) { };
185 m += uartDelay;
186 }
187 digitalWrite(pin, HIGH); // Stop bit
188 while (micros() - m < uartDelay) { }
189 m += uartDelay;
190 }
191 pinMode(pin, INPUT_PULLUP);
192 while (micros() - m < uartDelay) { };
193 delayMicroseconds(20);
194 for (j = 0; j < 8; j++) {
195 m = micros();
196 while (digitalRead(pin) == HIGH && micros() - m < 3000) { } // wait for start bit to start
197 m = micros();
198 if (micros() - m >= 3000) { // timed out, tmc7300 didn't respond to the read request
199 delay(1);
200 return false;
201 }
202 while (micros() - m < uartDelay) { }
203 m += uartDelay;
204 for (uint8_t i = 0; i < 8; i++) {
205 while (micros() - m < uartDelay / 2) { }
206 m += uartDelay / 2;
207 response[j] |= (digitalRead(pin) << i);
208 if (i < 7) {
209 while (micros() - m < uartDelay / 2) { }
210 m += uartDelay / 2;
211 } else {
212 while (micros() - m < uartDelay && digitalRead(pin) == LOW) { } // wait for 8th bit to finish
213 delayMicroseconds(1); // make sure the pin has time to go high and stay there
214 }
215 }
216 }
217 delay(1); // prevent messages from being too close together
218 pinMode(pin, OUTPUT);
219 digitalWrite(pin, HIGH);
220
221 if (response[1] != 0xFF) { // "master address"
222 return false;
223 }
224 if (response[2] != addressRead) {
225 return false;
226 }
227 if (response[7] != calcCRC(response, 7)) {
228 return false;
229 }
230
231 value = (response[3] << 24) | (response[4] << 16) | (response[5] << 8) | response[6];
232
233 return true;
234 }
235
241 {
242 return chipAddress;
243 }
244};
245#endif // TMC7300_H
TMCField TMC7300_IRUN(_TMC7300_CURRENT_LIMIT, _TMC7300_IRUN_MASK, _TMC7300_IRUN_SHIFT, _TMC7300_CURRENT_LIMIT_VALUE_ADDR)
TMCField TMC7300_DRV_ERR(_TMC7300_GSTAT, _TMC7300_DRV_ERR_MASK, _TMC7300_DRV_ERR_SHIFT, _TMC7300_GSTAT_VALUE_ADDR)
TMCField TMC7300_PWM_DIRECT(_TMC7300_GCONF, _TMC7300_PWM_DIRECT_MASK, _TMC7300_PWM_DIRECT_SHIFT, _TMC7300_GCONF_VALUE_ADDR)
TMCField TMC7300_SLAVECONF(_TMC7300_SLAVECONF, _TMC7300_SLAVECONF_MASK, _TMC7300_SLAVECONF_SHIFT, _TMC7300_SLAVECONF_VALUE_ADDR)
TMCField TMC7300_ENABLEDRV(_TMC7300_CHOPCONF, _TMC7300_ENABLEDRV_MASK, _TMC7300_ENABLEDRV_SHIFT, _TMC7300_CHOPCONF_VALUE_ADDR)
TMCField TMC7300_EXTCAP(_TMC7300_GCONF, _TMC7300_EXTCAP_MASK, _TMC7300_EXTCAP_SHIFT, _TMC7300_GCONF_VALUE_ADDR)
uint8_t calcCRC(uint8_t data[], uint8_t dataLength)
Definition TMCSerial.h:9
Top level class for the TMC7300 library. This class controls a TMC7300 chip. https://www....
Definition TMC7300.h:13
boolean extcap
Definition TMC7300.h:74
void begin(boolean _extcap=true, uint8_t _irun=31)
begin communication with the TMC7300
Definition TMC7300.h:33
boolean readRegister(uint8_t address, uint32_t &value)
read data from a register in the TMC7300
Definition TMC7300.h:163
uint32_t uartDelay
Definition TMC7300.h:73
int checkDriver()
check if the TMC7300's settings match the settings they should have and reset them if they don't
Definition TMC7300.h:49
void writeRegister(uint8_t address, uint32_t value)
write data to a register in the TMC7300
Definition TMC7300.h:122
uint8_t getChipAddress()
get the chipAddress of the TMC7300
Definition TMC7300.h:240
uint8_t chipAddress
Definition TMC7300.h:72
uint32_t registers[10]
Definition TMC7300.h:77
TMC7300IC(uint8_t _pin, uint8_t _chipAddress, uint32_t _baudrate=100000)
constructor for class that controls a TMC7300 chip.
Definition TMC7300.h:21
void writeField(TMCField field, uint32_t fieldValue, boolean write=true)
write data to a field of a register in the TMC7300 (see TMC7300_Map.h and the TMC7300 datasheet for m...
Definition TMC7300.h:90
uint8_t pin
Definition TMC7300.h:71
uint32_t readField(TMCField field, boolean read=true)
read data from a field of a register in the TMC7300 (see TMC7300_Map.h and the TMC7300 datasheet for ...
Definition TMC7300.h:105
Definition TMC_Field.h:7
uint32_t address()
Definition TMC_Field.h:31
uint32_t getField(uint32_t registerValue)
Definition TMC_Field.h:25
uint32_t setField(uint32_t fieldValue, uint32_t registerValue)
Definition TMC_Field.h:19
uint32_t valueAddress()
Definition TMC_Field.h:35