uStepper S-lite
TMC2208.cpp
Go to the documentation of this file.
1 /********************************************************************************************
2 * File: TMC2208.cpp *
3 * Version: 1.1.0 *
4 * Date: June 14, 2020 *
5 * Author: Thomas Hørring Olsen *
6 * *
7 *********************************************************************************************
8 * TMC2208 class *
9 * *
10 * This file contains the implementation of the class methods, incorporated in the *
11 * TMC2208 Arduino library. *
12 * *
13 *********************************************************************************************
14 * (C) 2020 *
15 * *
16 * uStepper ApS *
17 * www.ustepper.com *
18 * administration@ustepper.com *
19 * *
20 * The code contained in this file is released under the following open source license: *
21 * *
22 * Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International *
23 * *
24 * The code in this file is provided without warranty of any kind - use at own risk! *
25 * neither uStepper ApS nor the author, can be held responsible for any damage *
26 * caused by the use of the code contained in this file ! *
27 * *
28 ********************************************************************************************/
39 #include "TMC2208.h"
40 
41 uint8_t Tmc2208::calcCRC(uint8_t datagram[], uint8_t len) {
42  uint8_t crc = 0;
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;
48  } else {
49  crc = (crc << 1);
50  }
51  crc &= 0xff;
52  currentByte = currentByte >> 1;
53  }
54  }
55  return crc;
56 }
57 
58 void Tmc2208::writeRegister(uint8_t address, int32_t value)
59 {
60  uint8_t writeData[8];
61  //cli();
62  writeData[0] = 0x05; // Sync byte
63  writeData[1] = 0x00; // Slave address
64  writeData[2] = address | TMC2208_WRITE_BIT; // Register address with write bit set
65  writeData[3] = value >> 24; // Register Data
66  writeData[4] = value >> 16; // Register Data
67  writeData[5] = value >> 8; // Register Data
68  writeData[6] = value & 0xFF; // Register Data
69  writeData[7] = calcCRC(writeData, 7); // Cyclic redundancy check
70 
71  for(uint32_t i = 0; i < ARRAY_SIZE(writeData); i++)
72  {
73  this->uartSendByte(writeData[i]);
74  }
75  //sei();
76 }
77 
78 void Tmc2208::readRegister(uint8_t address, int32_t *value)
79 {
80  uint8_t readData[8], dataRequest[4];
81 
82 cli();
83  // Clear write bit
84  address &= ~TMC2208_WRITE_BIT;
85 
86  dataRequest[0] = 0x05; // Sync byte
87  dataRequest[1] = 0x00; // Slave address
88  dataRequest[2] = address; // Register address
89  dataRequest[7] = calcCRC(dataRequest, 3); // Cyclic redundancy check
90 
91  for(uint32_t i = 0; i < ARRAY_SIZE(dataRequest); i++)
92  {
93  this->uartSendByte(dataRequest[i]);
94  }
95 
96  //this->uartReceivePacket(readData, 8);
97  _delay_ms(1);
98  return;
99 
100  // Check if the received data is correct (CRC, Sync, Slave address, Register address)
101  // todo CHECK 2: Only keep CRC check? Should be sufficient for wrong transmissions (LH) #1
102  if(readData[7] != calcCRC(readData, 7) || readData[0] != 0x05 || readData[1] != 0xFF || readData[2] != address)
103  return;
104 
105  *value = (uint32_t)readData[3] << 24 | (uint32_t)readData[4] << 16 | (uint32_t)readData[5] << 8 | (uint32_t)readData[6];
106  return;
107 }
108 
110 {
111 
112 }
113 
114 void Tmc2208::setup(void)
115 {
116  int32_t registerSetting;
117 
118  DDRD |= (1 << 4); //Set Enable as output
119  DDRD |= (1 << 7); //Set Step pin as output
120  DDRB |= (1 << 2); //Set Dir pin as Output
121 
122  this->disableDriver();
123  this->uartInit();
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);
129  this->setCurrent(60,30);
130  this->setVelocity(0);
131 }
132 
133 void Tmc2208::invertDirection(bool normal)
134 {
135  cli();
136  int32_t registerSetting;
137  registerSetting = R00;
138  if(normal == NORMALDIRECTION)
139  {
140  registerSetting |= TMC2208_PDN_DISABLE_MASK | TMC2208_INDEX_STEP_MASK;
141  }
142  else
143  {
144  registerSetting |= TMC2208_PDN_DISABLE_MASK | TMC2208_INDEX_STEP_MASK | TMC2208_SHAFT_MASK;
145  }
146  this->writeRegister(TMC2208_GCONF, registerSetting);
147  sei();
148 }
149 
151 {
152  PORTD &= ~(1 << 4); //Enable motor driver
153 }
154 
156 {
157  PORTD |= (1 << 4); //Disable motor driver
158 }
159 
160 void Tmc2208::uartInit(void)
161 {
162  UARTRXDDR &= ~(1 << UARTRXPIN); //Set RX pin as input
163  UARTTXDDR |= (1 << UARTTXPIN); //Set TX pin as Output
164 
165  UARTRXPORT |= (1 << UARTRXPIN); //Set RX pin pullup enable
166  UARTTXPORT |= (1 << UARTTXPIN); //Set TX pin high
167 }
168 
169 void Tmc2208::uartSendByte(uint8_t value)
170 {
171  uint8_t mask = 1;
172 
173 
174  //Start bit
175  UARTTXPORT &= ~(1 << UARTTXPIN); UARTCLKDELAY();
176  while(mask)
177  {
178  if (mask & value)
179  UARTTXPORT |= (1 << UARTTXPIN);
180  else
181  UARTTXPORT &= ~(1 << UARTTXPIN);
182  UARTCLKDELAY();
183  mask <<= 1;
184  }
185 
186  // Stop bit
187  UARTTXPORT |= (1 << UARTTXPIN); UARTCLKDELAY();
188 
189 }
190 
191 bool Tmc2208::uartReceivePacket(uint8_t *packet __attribute__((unused)), uint8_t size __attribute__((unused)))
192 {
193  return 0;
194 }
195 
196 void Tmc2208::setCurrent(uint8_t runPercent, uint8_t holdPercent)
197 {
198  this->setRunCurrent(runPercent);
199  this->setHoldCurrent(holdPercent);
200 }
201 
202 void Tmc2208::setRunCurrent(uint8_t runPercent)
203 {
204  int32_t registerSetting = 0;
205 
206  uint8_t temp = (uint8_t)((float)runPercent * 0.31f) ;
207  this->runCurrent = temp > 31 ? 31 : temp ;
208 
209  registerSetting |= (((int32_t)(this->holdCurrent & 0x1F)) << TMC2208_IHOLD_SHIFT );
210  registerSetting |= (((int32_t)(this->runCurrent & 0x1F)) << TMC2208_IRUN_SHIFT );
211 
212  this->writeRegister(TMC2208_IHOLD_IRUN, registerSetting);
213 }
214 
215 void Tmc2208::setHoldCurrent(uint8_t holdPercent)
216 {
217  int32_t registerSetting = 0;
218 
219  uint8_t temp = (uint8_t)((float)holdPercent * 0.31f) ;
220  this->holdCurrent = temp > 31 ? 31 : temp ;
221 
222  registerSetting |= (((int32_t)(this->holdCurrent & 0x1F)) << TMC2208_IHOLD_SHIFT );
223  registerSetting |= (((int32_t)(this->runCurrent & 0x1F)) << TMC2208_IRUN_SHIFT );
224 
225  this->writeRegister(TMC2208_IHOLD_IRUN, registerSetting);
226 }
227 
228 void Tmc2208::setVelocity(float RPM)
229 {
230  float dummy;
231 
232  dummy = (float)RPM;
233  dummy *= 55.925333333; //0.016666666666*3200*1.0486 = 55.925333333
234 
235  RPM = (int32_t)(dummy + 0.5);
236 
237  this->writeRegister(TMC2208_VACTUAL, RPM);
238 }
239 
240 float Tmc2208::getRunCurrent(void)
241 {
242  return ((float)this->runCurrent)/0.31;
243 }
244 
245 float Tmc2208::getHoldCurrent(void)
246 {
247  return ((float)this->holdCurrent)/0.31;
248 }
Tmc2208::invertDirection
void invertDirection(bool normal=INVERSEDIRECTION)
Invert motor direction.
Definition: TMC2208.cpp:133
Tmc2208::setCurrent
void setCurrent(uint8_t runPercent, uint8_t holdPercent)
Change run and hold current settings of the stepper motor driver - TMC2208.
Definition: TMC2208.cpp:196
Tmc2208::setVelocity
void setVelocity(float RPM)
Set motor velocity in RPM.
Definition: TMC2208.cpp:228
Tmc2208::setup
void setup(void)
Initializes the different parts of the TMC2208 object.
Definition: TMC2208.cpp:114
TMC2208.h
Function prototypes and definitions for the uStepper TMC2208 driver library.
Tmc2208::setHoldCurrent
void setHoldCurrent(uint8_t holdPercent)
Change hold current setting of the stepper motor driver - TMC2208.
Definition: TMC2208.cpp:215
ARRAY_SIZE
#define ARRAY_SIZE(x)
Definition: TMC2208.h:249
Tmc2208::runCurrent
uint8_t runCurrent
Definition: TMC2208.h:372
Tmc2208::disableDriver
void disableDriver(void)
Disable the stepper motor driver - TMC2208.
Definition: TMC2208.cpp:155
Tmc2208::enableDriver
void enableDriver(void)
Enable the stepper motor driver - TMC2208.
Definition: TMC2208.cpp:150
Tmc2208::setRunCurrent
void setRunCurrent(uint8_t runPercent)
Change run current setting of the stepper motor driver - TMC2208.
Definition: TMC2208.cpp:202
Tmc2208::holdCurrent
uint8_t holdCurrent
Definition: TMC2208.h:376
Tmc2208::Tmc2208
Tmc2208(void)
Constructor.
Definition: TMC2208.cpp:109