-
Notifications
You must be signed in to change notification settings - Fork 11
/
Tic.cpp
235 lines (211 loc) · 4.46 KB
/
Tic.cpp
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <Tic.h>
#include <Arduino.h>
static const uint16_t Tic03aCurrentTable[33] =
{
0,
1,
174,
343,
495,
634,
762,
880,
990,
1092,
1189,
1281,
1368,
1452,
1532,
1611,
1687,
1762,
1835,
1909,
1982,
2056,
2131,
2207,
2285,
2366,
2451,
2540,
2634,
2734,
2843,
2962,
3093,
};
void TicBase::setCurrentLimit(uint16_t limit)
{
uint8_t code = 0;
if (product == TicProduct::T500)
{
for (uint8_t i = 0; i < 33; i++)
{
if (Tic03aCurrentTable[i] <= limit)
{
code = i;
}
else
{
break;
}
}
}
else if (product == TicProduct::T249)
{
code = limit / TicT249CurrentUnits;
}
else if (product == TicProduct::Tic36v4)
{
if (limit < 72) { code = 0; }
else if (limit >= 9095) { code = 127; }
else
{
code = ((uint32_t)limit * 768 - 55000 / 2) / 55000;
if (code < 127 && (((uint32_t)55000 * (code + 1) + 384) / 768) <= limit)
{
code++;
}
}
}
else
{
code = limit / TicCurrentUnits;
}
commandW7(TicCommand::SetCurrentLimit, code);
}
uint16_t TicBase::getCurrentLimit()
{
uint8_t code = getVar8(VarOffset::CurrentLimit);
if (product == TicProduct::T500)
{
if (code > 32) { code = 32; }
return Tic03aCurrentTable[code];
}
else if (product == TicProduct::T249)
{
return code * TicT249CurrentUnits;
}
else if (product == TicProduct::Tic36v4)
{
return ((uint32_t)55000 * code + 384) / 768;
}
else
{
return code * TicCurrentUnits;
}
}
/**** TicSerial ****/
void TicSerial::commandW32(TicCommand cmd, uint32_t val)
{
sendCommandHeader(cmd);
// byte with MSbs:
// bit 0 = MSb of first (least significant) data byte
// bit 1 = MSb of second data byte
// bit 2 = MSb of third data byte
// bit 3 = MSb of fourth (most significant) data byte
serialW7(((val >> 7) & 1) |
((val >> 14) & 2) |
((val >> 21) & 4) |
((val >> 28) & 8));
serialW7(val >> 0); // least significant byte with MSb cleared
serialW7(val >> 8);
serialW7(val >> 16);
serialW7(val >> 24); // most significant byte with MSb cleared
_lastError = 0;
}
void TicSerial::commandW7(TicCommand cmd, uint8_t val)
{
sendCommandHeader(cmd);
serialW7(val);
_lastError = 0;
}
void TicSerial::getSegment(TicCommand cmd, uint8_t offset,
uint8_t length, void * buffer)
{
length &= 0x3F;
sendCommandHeader(cmd);
serialW7(offset & 0x7F);
serialW7(length | (offset >> 1 & 0x40));
uint8_t byteCount = _stream->readBytes((uint8_t *)buffer, length);
if (byteCount != length)
{
_lastError = 50;
// Set the buffer bytes to 0 so the program will not use an uninitialized
// variable.
memset(buffer, 0, length);
return;
}
_lastError = 0;
}
void TicSerial::sendCommandHeader(TicCommand cmd)
{
if (_deviceNumber == 255)
{
// Compact protocol
_stream->write((uint8_t)cmd);
}
else
{
// Pololu protocol
_stream->write((uint8_t)0xAA);
serialW7(_deviceNumber);
serialW7((uint8_t)cmd);
}
_lastError = 0;
}
/**** TicI2C ****/
void TicI2C::commandQuick(TicCommand cmd)
{
_bus->beginTransmission(_address);
_bus->write((uint8_t)cmd);
_lastError = _bus->endTransmission();
}
void TicI2C::commandW32(TicCommand cmd, uint32_t val)
{
_bus->beginTransmission(_address);
_bus->write((uint8_t)cmd);
_bus->write((uint8_t)(val >> 0)); // lowest byte
_bus->write((uint8_t)(val >> 8));
_bus->write((uint8_t)(val >> 16));
_bus->write((uint8_t)(val >> 24)); // highest byte
_lastError = _bus->endTransmission();
}
void TicI2C::commandW7(TicCommand cmd, uint8_t val)
{
_bus->beginTransmission(_address);
_bus->write((uint8_t)cmd);
_bus->write((uint8_t)(val & 0x7F));
_lastError = _bus->endTransmission();
}
void TicI2C::getSegment(TicCommand cmd, uint8_t offset,
uint8_t length, void * buffer)
{
_bus->beginTransmission(_address);
_bus->write((uint8_t)cmd);
_bus->write(offset);
_lastError = _bus->endTransmission(false); // no stop (repeated start)
if (_lastError)
{
// Set the buffer bytes to 0 so the program will not use an uninitialized
// variable.
memset(buffer, 0, length);
return;
}
uint8_t byteCount = _bus->requestFrom(_address, (uint8_t)length);
if (byteCount != length)
{
_lastError = 50;
memset(buffer, 0, length);
return;
}
_lastError = 0;
uint8_t * ptr = (uint8_t *)buffer;
for (uint8_t i = 0; i < length; i++)
{
*ptr = _bus->read();
ptr++;
}
}