-
Notifications
You must be signed in to change notification settings - Fork 0
/
labjack_u3.c
330 lines (275 loc) · 9.11 KB
/
labjack_u3.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include <errno.h>
#include <math.h>
#include <string.h>
#include "labjack_u3.h"
int lju3_config_timer_clock(HANDLE dev,
struct lju3_config_timer_clock *config,
struct lju3_config_timer_clock_resp *config_resp
) {
unsigned long n;
const unsigned n_tx = sizeof(struct lju3_config_timer_clock);
const unsigned n_rx = sizeof(struct lju3_config_timer_clock_resp);
const unsigned n_head = sizeof(struct ljud_extended_header);
config->header.command = 0xF8;
config->header.n_data_words = ((n_tx - n_head) / 2);
config->header.extended_command = 0x0A;
config->header.checksum16 = ljud_checksum16(
(uint8_t *)config + 6, n_tx - 6
);
config->header.checksum8 = ljud_checksum8(
(uint8_t *)config + 1, n_head - 1
);
n = LJUSB_Write(dev, (uint8_t *)config, n_tx);
if (n < n_tx) return -ECOMM;
n = LJUSB_Read(dev, (uint8_t *)config_resp, n_rx);
if (n < n_rx) {
// LJ is telling us we have a bad checksum
if (*(uint16_t *)config_resp == 0xB8B8) return -EBADMSG;
return -EREMOTEIO;
}
if (
config_resp->header.checksum16
!= ljud_checksum16((uint8_t *)config_resp + 6, n_rx - 6)
) {
// LJ checksum failed
return -EBADE;
}
if (config_resp->err) return config_resp->err;
return 0;
}
int lju3_config_io(HANDLE dev,
struct lju3_config_io *config, struct lju3_config_io_resp *config_resp
) {
unsigned long n;
const unsigned n_tx = sizeof(struct lju3_config_io);
const unsigned n_rx = sizeof(struct lju3_config_io_resp);
const unsigned n_head = sizeof(struct ljud_extended_header);
config->header.command = 0xF8;
config->header.n_data_words = ((n_tx - n_head) / 2);
config->header.extended_command = 0x0B;
config->header.checksum16 = ljud_checksum16(
(uint8_t *)config + 6, n_tx - 6
);
config->header.checksum8 = ljud_checksum8(
(uint8_t *)config + 1, n_head - 1
);
n = LJUSB_Write(dev, (uint8_t *)config, n_tx);
if (n < n_tx) return -ECOMM;
n = LJUSB_Read(dev, (uint8_t *)config_resp, n_rx);
if (n < n_rx) {
// LJ is telling us we have a bad checksum
if (*(uint16_t *)config_resp == 0xB8B8) return -EBADMSG;
return -EREMOTEIO;
}
if (
config_resp->header.checksum16
!= ljud_checksum16((uint8_t *)config_resp + 6, n_rx - 6)
) {
// LJ checksum failed
return -EBADE;
}
return 0;
}
int lju3_feedback_timer_config(HANDLE dev,
struct lju3_feedback_timer_config *config,
struct lju3_feedback_resp_header *config_resp
) {
unsigned long n;
const unsigned n_tx = sizeof(struct lju3_feedback_timer_config);
const unsigned n_rx = sizeof(struct lju3_feedback_resp_header);
const unsigned n_head = sizeof(struct ljud_extended_header);
// docs say this is "43,45"; idk i'm just gonna use the former
config->io_type = 0x2B;
config->header.echo = 0xAA; // arbitrary
config->header.header.command = 0xF8;
config->header.header.n_data_words = ((n_tx - n_head) / 2);
config->header.header.extended_command = 0x00;
config->header.header.checksum16 = ljud_checksum16(
(uint8_t *)config + 6, n_tx - 6
);
config->header.header.checksum8 = ljud_checksum8(
(uint8_t *)config + 1, n_head - 1
);
n = LJUSB_Write(dev, (uint8_t *)config, n_tx);
if (n < n_tx) return -ECOMM;
n = LJUSB_Read(dev, (uint8_t *)config_resp, n_rx);
if (n < n_rx) {
// LJ is telling us we have a bad checksum
if (*(uint16_t *)config_resp == 0xB8B8) return -EBADMSG;
return -EREMOTEIO;
}
if (
config_resp->header.checksum16
!= ljud_checksum16((uint8_t *)config_resp + 6, n_rx - 6)
) {
// LJ checksum failed
return -EBADE;
}
if (config_resp->echo != config->header.echo) return -EBADE;
if (config_resp->err) return config_resp->err;
return 0;
}
int lju3_read_cal_mem(HANDLE dev, struct lju3_cal_mem *cal_mem)
{
unsigned long n;
struct lju3_readmem tx;
struct lju3_readmem_resp *rx;
const unsigned n_tx = sizeof(struct lju3_config);
const unsigned n_rx = sizeof(struct lju3_config_resp);
const unsigned n_head = sizeof(struct ljud_extended_header);
tx.header.command = 0xF8;
tx.header.n_data_words = 1;
tx.header.extended_command = 0x2D;
for (unsigned i = 0; i <= 4; i++) {
tx.block_num = i;
tx.header.checksum16 = ljud_checksum16(
(uint8_t *)&tx + 6, n_tx - 6
);
tx.header.checksum8 = ljud_checksum8(
(uint8_t *)&tx + 1, n_head - 1
);
n = LJUSB_Write(dev, (void *)&tx, n_tx);
if (n < n_tx) return -ECOMM;
n = LJUSB_Read(dev, (void *)&rx, n_rx);
if (n < n_rx) {
// LJ is telling us we have a bad checksum
if (*(uint16_t *)rx == 0xB8B8) return -EBADMSG;
return -EREMOTEIO;
}
if (
rx->header.checksum16
!= ljud_checksum16((uint8_t *)rx + 6, n_rx - 6)
) {
return -EBADE;
}
memcpy(
(uint8_t *)cal_mem + sizeof(cal_mem->block0),
rx, sizeof(cal_mem->block0)
);
}
return 0;
}
int lju3_read_config(HANDLE dev, struct lju3_config_resp *config_resp)
{
unsigned long n;
uint8_t tx[sizeof(struct lju3_config)] = {0};
uint8_t rx[sizeof(struct lju3_config_resp)];
struct lju3_config *t = (struct lju3_config *)tx;
t->header.command = 0xF8;
t->header.n_data_words = (
(sizeof(tx) - sizeof(struct ljud_extended_header)) / 2
);
static_assert(
sizeof(tx) - sizeof(struct ljud_extended_header) == 2 * 0x0A,
"bad n_data_words"
);
t->header.extended_command = 0x08;
t->header.checksum16 = ljud_checksum16(tx + 6, sizeof(tx) - 6);
t->header.checksum8 = ljud_checksum8(tx + 1, sizeof(tx) - 1);
n = LJUSB_Write(dev, tx, sizeof(tx));
if (n < sizeof(tx)) return -ECOMM;
n = LJUSB_Read(dev, rx, sizeof(rx));
if (n < sizeof(rx)) return -EREMOTEIO;
if (
((struct lju3_config_resp *)rx)->header.checksum16
!= ljud_checksum16(rx + 6, sizeof(rx) - 6)
) {
return -EBADMSG;
}
memcpy(config_resp, rx, sizeof(struct lju3_config_resp));
return 0;
}
int lju3_square(
HANDLE dev, ljud_pin pin, unsigned long hz_req, double *hz_real
) {
int err;
// LJ docs: To configure timers, write to the following:
// NumberTimersEnabled TimerClockBase TimerClockDivisor
// TimerCounterPinOffset TimerValue TimerMode
// this involves communicating the following structs:
// config_timer_clock: base, divisor
// config_io.timer_counter_config: number enabled, pin offset
// feedback_timer_config: value, mode
// let's do the timer clock config first
struct lju3_config_timer_clock config_timer_clock = {0};
struct lju3_config_timer_clock_resp config_timer_clock_resp;
config_timer_clock.clock_config = LJU3_WRITE_CLOCK_CONFIG;
// LJ docs: frequency = TimerClockBase/(TimerClockDivisor*2*TimerValue)
// base in {1,4,12,48} MHz, divisor <= 0xFF, value <= 0xFF
// (0x0 maps to 0x100 for divisor and value)
// pick the fastest base possible (not always optimal but whatevs)
unsigned base;
if (hz_req > (48000000 >> 0x11)) {
base = 48000000;
config_timer_clock.clock_config |= LJU3_CLOCK_48MHZ_DIV;
} else if (hz_req > (12000000 >> 0x11)) {
base = 12000000;
config_timer_clock.clock_config |= LJU3_CLOCK_12MHZ_DIV;
} else if (hz_req > (4000000 >> 0x11)) {
base = 4000000;
config_timer_clock.clock_config |= LJU3_CLOCK_4MHZ_DIV;
} else {
base = 1000000;
config_timer_clock.clock_config |= LJU3_CLOCK_1MHZ_DIV;
}
// we want divisor and value to multiply close to this
double product = (double)base / 2 / hz_req;
// WLOG, let divisor <= value. then divisor is at least product / 0x100
unsigned divisor_min = product / 0x100;
// and at most ceil(sqrt(product))
unsigned divisor_max = sqrt(product) + 1;
// but bounds check those
if (divisor_min < 0x001) divisor_min = 0x001;
if (divisor_max > 0x100) divisor_max = 0x100;
// and then brute force the divisors
// initialize these to max, in case we skip the for loop (min > max)
unsigned divisor_best = 0x100;
unsigned value_best = 0x100;
double err_best = fabs(divisor_best * value_best - product);
for (unsigned d = divisor_min; d <= divisor_max; d++) {
// and binary search for the best value for this divisor
unsigned v_max = 0x100;
unsigned v_min = 0x001;
unsigned v;
while (v_max - v_min > 1) {
v = (v_max + v_min) / 2;
if (d * v <= product) v_min = v;
if (d * v > product) v_max = v;
}
if (fabs(d * v_max - product) < err_best) {
divisor_best = d;
value_best = v_max;
err_best = fabs(d * v_max - product);
}
if (fabs(d * v_min - product) < err_best) {
divisor_best = d;
value_best = v_min;
err_best = fabs(d * v_min - product);
}
}
*hz_real = (double)base / (divisor_best * 2 * value_best);
config_timer_clock.clock_divisor = divisor_best;
err = lju3_config_timer_clock(dev,
&config_timer_clock, &config_timer_clock_resp
);
if (err) return err;
// now, config_io.timer_counter_config
struct lju3_config_io config_io = {0};
struct lju3_config_io_resp config_io_resp;
config_io.write_mask |= 1 << 0; // set timer_counter_config
config_io.timer_counter_config = 1; // enable 1 timer
// pin offset to the requested pin
config_io.timer_counter_config |= pin << 4;
err = lju3_config_io(dev, &config_io, &config_io_resp);
if (err) return err;
// finally, feedback
struct lju3_feedback_timer_config feedback_timer_config;
struct lju3_feedback_resp_header feedback_timer_config_resp;
feedback_timer_config.timer_mode = LJU3_TIMER_OUT_SQUARE;
feedback_timer_config.value = value_best;
err = lju3_feedback_timer_config(
dev, &feedback_timer_config, &feedback_timer_config_resp
);
if (err) return err;
return 0;
}