41 uint8_t Tmc2208::calcCRC(uint8_t datagram[], uint8_t len) {
43 for (uint8_t i = 0; i < len; i++) {
44 uint8_t currentByte = datagram[i];
45 for (uint8_t j = 0; j < 8; j++) {
46 if ((crc >> 7) ^ (currentByte & 0x01)) {
47 crc = (crc << 1) ^ 0x07;
52 currentByte = currentByte >> 1;
58 void Tmc2208::writeRegister(uint8_t address, int32_t value)
64 writeData[2] = address | TMC2208_WRITE_BIT;
65 writeData[3] = value >> 24;
66 writeData[4] = value >> 16;
67 writeData[5] = value >> 8;
68 writeData[6] = value & 0xFF;
69 writeData[7] = calcCRC(writeData, 7);
71 for(uint32_t i = 0; i <
ARRAY_SIZE(writeData); i++)
73 this->uartSendByte(writeData[i]);
78 void Tmc2208::readRegister(uint8_t address, int32_t *value)
80 uint8_t readData[8], dataRequest[4];
84 address &= ~TMC2208_WRITE_BIT;
86 dataRequest[0] = 0x05;
87 dataRequest[1] = 0x00;
88 dataRequest[2] = address;
89 dataRequest[7] = calcCRC(dataRequest, 3);
91 for(uint32_t i = 0; i <
ARRAY_SIZE(dataRequest); i++)
93 this->uartSendByte(dataRequest[i]);
102 if(readData[7] != calcCRC(readData, 7) || readData[0] != 0x05 || readData[1] != 0xFF || readData[2] != address)
105 *value = (uint32_t)readData[3] << 24 | (uint32_t)readData[4] << 16 | (uint32_t)readData[5] << 8 | (uint32_t)readData[6];
116 int32_t registerSetting;
124 registerSetting = R00;
125 registerSetting |= TMC2208_PDN_DISABLE_MASK | TMC2208_INDEX_STEP_MASK ;
126 this->writeRegister(TMC2208_GCONF, registerSetting);
127 registerSetting = 5000;
128 this->writeRegister(TMC2208_TPWMTHRS, registerSetting);
136 int32_t registerSetting;
137 registerSetting = R00;
138 if(normal == NORMALDIRECTION)
140 registerSetting |= TMC2208_PDN_DISABLE_MASK | TMC2208_INDEX_STEP_MASK;
144 registerSetting |= TMC2208_PDN_DISABLE_MASK | TMC2208_INDEX_STEP_MASK | TMC2208_SHAFT_MASK;
146 this->writeRegister(TMC2208_GCONF, registerSetting);
160 void Tmc2208::uartInit(
void)
162 UARTRXDDR &= ~(1 << UARTRXPIN);
163 UARTTXDDR |= (1 << UARTTXPIN);
165 UARTRXPORT |= (1 << UARTRXPIN);
166 UARTTXPORT |= (1 << UARTTXPIN);
169 void Tmc2208::uartSendByte(uint8_t value)
175 UARTTXPORT &= ~(1 << UARTTXPIN); UARTCLKDELAY();
179 UARTTXPORT |= (1 << UARTTXPIN);
181 UARTTXPORT &= ~(1 << UARTTXPIN);
187 UARTTXPORT |= (1 << UARTTXPIN); UARTCLKDELAY();
191 bool Tmc2208::uartReceivePacket(uint8_t *packet __attribute__((unused)), uint8_t size __attribute__((unused)))
204 int32_t registerSetting = 0;
206 uint8_t temp = (uint8_t)((
float)runPercent * 0.31f) ;
209 registerSetting |= (((int32_t)(this->
holdCurrent & 0x1F)) << TMC2208_IHOLD_SHIFT );
210 registerSetting |= (((int32_t)(this->
runCurrent & 0x1F)) << TMC2208_IRUN_SHIFT );
212 this->writeRegister(TMC2208_IHOLD_IRUN, registerSetting);
217 int32_t registerSetting = 0;
219 uint8_t temp = (uint8_t)((
float)holdPercent * 0.31f) ;
222 registerSetting |= (((int32_t)(this->
holdCurrent & 0x1F)) << TMC2208_IHOLD_SHIFT );
223 registerSetting |= (((int32_t)(this->
runCurrent & 0x1F)) << TMC2208_IRUN_SHIFT );
225 this->writeRegister(TMC2208_IHOLD_IRUN, registerSetting);
233 dummy *= 55.925333333;
235 RPM = (int32_t)(dummy + 0.5);
237 this->writeRegister(TMC2208_VACTUAL, RPM);
240 float Tmc2208::getRunCurrent(
void)
245 float Tmc2208::getHoldCurrent(
void)