-
Notifications
You must be signed in to change notification settings - Fork 21
/
tmc26x.c
194 lines (152 loc) · 6.27 KB
/
tmc26x.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* tmc26x.c - interface for Trinamic TRM26x stepper drivers
*
* v0.0.1 / 2018-10-27 / (c) Io Engineering / Terje
*/
/*
Copyright (c) 2018, Terje Io
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its contributors may
be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <string.h>
#include "tmc26x.h"
static SPI_driver_t io;
//default values
#define CONSTANT_OFF_TIME 3 // 7
#define FAST_DECAY_TIME 4 // 13
#define SINE_WAVE_OFFSET 1 // 15
#define CHOPPER_MODE 0 // 1
#define BLANK_TIME 2 // 3
#define RANDOM_TOFF 0 // 1
static const TMC26x_t tmc26x_sdon_defaults = {
.cool_step_enabled = false,
.resistor = 110,
.current = 500,
.microsteps = TMC26x_Microsteps_16,
.chopconf.addr = 0x01,
.chopconf.toff = CONSTANT_OFF_TIME,
.chopconf.hdec = (FAST_DECAY_TIME & 0x08) >> 3,
.chopconf.hstrt = FAST_DECAY_TIME & 0x07,
.chopconf.hend = SINE_WAVE_OFFSET,
.chopconf.chm = CHOPPER_MODE,
.chopconf.tbl = BLANK_TIME,
.chopconf.rndtf = RANDOM_TOFF,
.smarten.addr = 0x05,
.sgcsconf.addr = 0x03,
.drvconf.addr = 0x07
};
static const TMC26x_t tmc26x_sdoff_defaults = {
.cool_step_enabled = false,
.resistor = 110,
.current = 500,
.chopconf.addr = 0x01,
.chopconf.toff = CONSTANT_OFF_TIME,
.chopconf.hdec = (FAST_DECAY_TIME & 0x08) >> 3,
.chopconf.hstrt = FAST_DECAY_TIME & 0x07,
.chopconf.hend = SINE_WAVE_OFFSET,
.chopconf.chm = CHOPPER_MODE,
.chopconf.tbl = BLANK_TIME,
.chopconf.rndtf = RANDOM_TOFF,
.smarten.addr = 0x05,
.sgcsconf.addr = 0x03,
.drvconf.addr = 0x07,
.drvconf.sdoff = 1
};
void TMC26X_SetDefaults (TMC26x_t *driver, bool sdoff)
{
memcpy(driver, sdoff ? &tmc26x_sdoff_defaults : &tmc26x_sdon_defaults, sizeof(TMC26x_t));
}
void TMC26X_Init (TMC26x_t *driver)
{
static bool ioint_ok = false;
if(!ioint_ok) {
SPI_DriverInit(&io);
ioint_ok = true;
}
driver->cool_step_enabled = false;
TMC26X_SetCurrent(driver, driver->current);
TMC26X_SetConstantOffTimeChopper(driver, 7, 54, 13, 12, 1);
TMC26X_SetMicrosteps(driver, driver->microsteps);
if(driver->drvconf.sdoff)
io.WriteRegister(driver, (TMC26x_datagram_t)driver->drvctrl_sdoff);
else
io.WriteRegister(driver, (TMC26x_datagram_t)driver->drvctrl_sdon);
io.WriteRegister(driver, (TMC26x_datagram_t)driver->chopconf);
io.WriteRegister(driver, (TMC26x_datagram_t)driver->smarten);
io.WriteRegister(driver, (TMC26x_datagram_t)driver->sgcsconf);
io.WriteRegister(driver, (TMC26x_datagram_t)driver->drvconf);
}
void TMC26X_SetCurrent (TMC26x_t *driver, uint16_t mA)
{
driver->current = mA;
float maxv = ((float)driver->resistor * (float)driver->current * 32.0f) / 1000000.0f;
// Calculate the current scaling from the max current setting (in mA),
// this is derived from I=(cs+1)/32*(Vsense/Rsense)
// leading to cs = CS = 32*R*I/V (with V = 0.31V or 0.165V and I = 1000*current)
// with Rsense=0.15
// For vsense = 0.310V (VSENSE not set)
// or vsense = 0.165V (VSENSE set)
uint8_t current_scaling = (uint8_t)((maxv / 0.31f) - 0.5f);
// If the current scaling is too low
// set the vsense bit to get a use half the sense voltage (to support lower motor currents)
// and recalculate the current setting
if ((driver->drvconf.vsense = (current_scaling < 16)))
current_scaling = (uint8_t)((maxv / 0.165f) - 0.5f);
driver->sgcsconf.cs = current_scaling > 31 ? 31 : current_scaling;
io.WriteRegister(driver, (TMC26x_datagram_t)driver->drvconf);
io.WriteRegister(driver, (TMC26x_datagram_t)driver->sgcsconf);
}
void TMC26X_SetMicrosteps (TMC26x_t *driver, tmc26x_microsteps_t usteps)
{
if(driver->drvconf.sdoff)
return;
uint8_t value = 0;
driver->microsteps = usteps = (usteps == 0 ? TMC26x_Microsteps_1 : usteps);
while((usteps & 0x01) == 0) {
value++;
usteps >>= 1;
}
driver->drvctrl_sdon.mres = 8 - (value > 8 ? 8 : value);
io.WriteRegister(driver, (TMC26x_datagram_t)driver->drvctrl_sdoff);
}
void TMC26X_SetConstantOffTimeChopper (TMC26x_t *driver, uint8_t constant_off_time, uint8_t blank_time, uint8_t fast_decay_time, int8_t sine_wave_offset, bool use_current_comparator)
{
//calculate the value acc to the clock cycles
if (blank_time >= 54)
blank_time = 3;
else if (blank_time >= 36)
blank_time = 2;
else if (blank_time >= 24)
blank_time = 1;
else
blank_time = 0;
if (fast_decay_time > 15)
fast_decay_time = 15;
driver->chopconf.chm = 1;
driver->chopconf.tbl = blank_time;
driver->chopconf.toff = constant_off_time < 2 ? 2 : (constant_off_time > 15 ? 15 : constant_off_time);
driver->chopconf.hdec = (fast_decay_time & 0x8) >> 3;
driver->chopconf.hstrt = fast_decay_time & 0x7;
driver->chopconf.hend = (sine_wave_offset < -3 ? -3 : (sine_wave_offset > 12 ? 12 : sine_wave_offset)) + 3;
driver->chopconf.rndtf = !use_current_comparator;
io.WriteRegister(driver, (TMC26x_datagram_t)driver->chopconf);
}