]>
Commit | Line | Data |
---|---|---|
0aba65da | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
0aba65da | 3 | * |
13d8e03c | 4 | * Copyright (C) 2013 Bert Vermeulen <bert@biot.com> |
0aba65da UH |
5 | * |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include "protocol.h" | |
21 | ||
2239728c | 22 | extern SR_PRIV struct sr_dev_driver ols_driver_info; |
0aba65da UH |
23 | static struct sr_dev_driver *di = &ols_driver_info; |
24 | ||
25 | SR_PRIV int send_shortcommand(struct sr_serial_dev_inst *serial, | |
26 | uint8_t command) | |
27 | { | |
28 | char buf[1]; | |
29 | ||
30 | sr_dbg("Sending cmd 0x%.2x.", command); | |
31 | buf[0] = command; | |
32 | if (serial_write(serial, buf, 1) != 1) | |
33 | return SR_ERR; | |
34 | ||
35 | return SR_OK; | |
36 | } | |
37 | ||
38 | SR_PRIV int send_longcommand(struct sr_serial_dev_inst *serial, | |
39 | uint8_t command, uint32_t data) | |
40 | { | |
41 | char buf[5]; | |
42 | ||
43 | sr_dbg("Sending cmd 0x%.2x data 0x%.8x.", command, data); | |
44 | buf[0] = command; | |
45 | buf[1] = (data & 0xff000000) >> 24; | |
46 | buf[2] = (data & 0xff0000) >> 16; | |
47 | buf[3] = (data & 0xff00) >> 8; | |
48 | buf[4] = data & 0xff; | |
49 | if (serial_write(serial, buf, 5) != 5) | |
50 | return SR_ERR; | |
51 | ||
52 | return SR_OK; | |
53 | } | |
54 | ||
55 | SR_PRIV int ols_configure_probes(const struct sr_dev_inst *sdi) | |
56 | { | |
57 | struct dev_context *devc; | |
58 | const struct sr_probe *probe; | |
59 | const GSList *l; | |
60 | int probe_bit, stage, i; | |
61 | char *tc; | |
62 | ||
63 | devc = sdi->priv; | |
64 | ||
65 | devc->probe_mask = 0; | |
66 | for (i = 0; i < NUM_TRIGGER_STAGES; i++) { | |
67 | devc->trigger_mask[i] = 0; | |
68 | devc->trigger_value[i] = 0; | |
69 | } | |
70 | ||
71 | devc->num_stages = 0; | |
72 | for (l = sdi->probes; l; l = l->next) { | |
73 | probe = (const struct sr_probe *)l->data; | |
74 | if (!probe->enabled) | |
75 | continue; | |
76 | ||
77 | /* | |
78 | * Set up the probe mask for later configuration into the | |
79 | * flag register. | |
80 | */ | |
81 | probe_bit = 1 << (probe->index); | |
82 | devc->probe_mask |= probe_bit; | |
83 | ||
84 | if (!probe->trigger) | |
85 | continue; | |
86 | ||
87 | /* Configure trigger mask and value. */ | |
88 | stage = 0; | |
89 | for (tc = probe->trigger; tc && *tc; tc++) { | |
90 | devc->trigger_mask[stage] |= probe_bit; | |
91 | if (*tc == '1') | |
92 | devc->trigger_value[stage] |= probe_bit; | |
93 | stage++; | |
94 | if (stage > 3) | |
95 | /* | |
96 | * TODO: Only supporting parallel mode, with | |
97 | * up to 4 stages. | |
98 | */ | |
99 | return SR_ERR; | |
100 | } | |
101 | if (stage > devc->num_stages) | |
503133bb | 102 | devc->num_stages = stage - 1; |
0aba65da UH |
103 | } |
104 | ||
105 | return SR_OK; | |
106 | } | |
107 | ||
108 | SR_PRIV uint32_t reverse16(uint32_t in) | |
109 | { | |
110 | uint32_t out; | |
111 | ||
112 | out = (in & 0xff) << 8; | |
113 | out |= (in & 0xff00) >> 8; | |
114 | out |= (in & 0xff0000) << 8; | |
115 | out |= (in & 0xff000000) >> 8; | |
116 | ||
117 | return out; | |
118 | } | |
119 | ||
120 | SR_PRIV uint32_t reverse32(uint32_t in) | |
121 | { | |
122 | uint32_t out; | |
123 | ||
124 | out = (in & 0xff) << 24; | |
125 | out |= (in & 0xff00) << 8; | |
126 | out |= (in & 0xff0000) >> 8; | |
127 | out |= (in & 0xff000000) >> 24; | |
128 | ||
129 | return out; | |
130 | } | |
131 | ||
132 | SR_PRIV struct dev_context *ols_dev_new(void) | |
133 | { | |
134 | struct dev_context *devc; | |
135 | ||
bf256783 | 136 | if (!(devc = g_try_malloc(sizeof(struct dev_context)))) { |
0aba65da UH |
137 | sr_err("Device context malloc failed."); |
138 | return NULL; | |
139 | } | |
140 | ||
bf256783 BV |
141 | /* Device-specific settings */ |
142 | devc->max_samples = devc->max_samplerate = devc->protocol_version = 0; | |
143 | ||
144 | /* Acquisition settings */ | |
145 | devc->limit_samples = devc->capture_ratio = 0; | |
0aba65da UH |
146 | devc->trigger_at = -1; |
147 | devc->probe_mask = 0xffffffff; | |
bf256783 BV |
148 | devc->flag_reg = 0; |
149 | ||
0aba65da UH |
150 | return devc; |
151 | } | |
152 | ||
153 | SR_PRIV struct sr_dev_inst *get_metadata(struct sr_serial_dev_inst *serial) | |
154 | { | |
155 | struct sr_dev_inst *sdi; | |
156 | struct dev_context *devc; | |
157 | struct sr_probe *probe; | |
158 | uint32_t tmp_int, ui; | |
159 | uint8_t key, type, token; | |
160 | GString *tmp_str, *devname, *version; | |
161 | guchar tmp_c; | |
162 | ||
163 | sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, NULL, NULL, NULL); | |
164 | sdi->driver = di; | |
165 | devc = ols_dev_new(); | |
166 | sdi->priv = devc; | |
167 | ||
168 | devname = g_string_new(""); | |
169 | version = g_string_new(""); | |
170 | ||
171 | key = 0xff; | |
172 | while (key) { | |
173 | if (serial_read(serial, &key, 1) != 1 || key == 0x00) | |
174 | break; | |
175 | type = key >> 5; | |
176 | token = key & 0x1f; | |
177 | switch (type) { | |
178 | case 0: | |
179 | /* NULL-terminated string */ | |
180 | tmp_str = g_string_new(""); | |
181 | while (serial_read(serial, &tmp_c, 1) == 1 && tmp_c != '\0') | |
182 | g_string_append_c(tmp_str, tmp_c); | |
183 | sr_dbg("Got metadata key 0x%.2x value '%s'.", | |
184 | key, tmp_str->str); | |
185 | switch (token) { | |
186 | case 0x01: | |
187 | /* Device name */ | |
188 | devname = g_string_append(devname, tmp_str->str); | |
189 | break; | |
190 | case 0x02: | |
191 | /* FPGA firmware version */ | |
192 | if (version->len) | |
193 | g_string_append(version, ", "); | |
194 | g_string_append(version, "FPGA version "); | |
195 | g_string_append(version, tmp_str->str); | |
196 | break; | |
197 | case 0x03: | |
198 | /* Ancillary version */ | |
199 | if (version->len) | |
200 | g_string_append(version, ", "); | |
201 | g_string_append(version, "Ancillary version "); | |
202 | g_string_append(version, tmp_str->str); | |
203 | break; | |
204 | default: | |
205 | sr_info("ols: unknown token 0x%.2x: '%s'", | |
206 | token, tmp_str->str); | |
207 | break; | |
208 | } | |
209 | g_string_free(tmp_str, TRUE); | |
210 | break; | |
211 | case 1: | |
212 | /* 32-bit unsigned integer */ | |
213 | if (serial_read(serial, &tmp_int, 4) != 4) | |
214 | break; | |
215 | tmp_int = reverse32(tmp_int); | |
216 | sr_dbg("Got metadata key 0x%.2x value 0x%.8x.", | |
217 | key, tmp_int); | |
218 | switch (token) { | |
219 | case 0x00: | |
220 | /* Number of usable probes */ | |
221 | for (ui = 0; ui < tmp_int; ui++) { | |
222 | if (!(probe = sr_probe_new(ui, SR_PROBE_LOGIC, TRUE, | |
223 | ols_probe_names[ui]))) | |
224 | return 0; | |
225 | sdi->probes = g_slist_append(sdi->probes, probe); | |
226 | } | |
227 | break; | |
228 | case 0x01: | |
229 | /* Amount of sample memory available (bytes) */ | |
230 | devc->max_samples = tmp_int; | |
231 | break; | |
232 | case 0x02: | |
233 | /* Amount of dynamic memory available (bytes) */ | |
234 | /* what is this for? */ | |
235 | break; | |
236 | case 0x03: | |
237 | /* Maximum sample rate (hz) */ | |
238 | devc->max_samplerate = tmp_int; | |
239 | break; | |
240 | case 0x04: | |
241 | /* protocol version */ | |
242 | devc->protocol_version = tmp_int; | |
243 | break; | |
244 | default: | |
245 | sr_info("Unknown token 0x%.2x: 0x%.8x.", | |
246 | token, tmp_int); | |
247 | break; | |
248 | } | |
249 | break; | |
250 | case 2: | |
251 | /* 8-bit unsigned integer */ | |
252 | if (serial_read(serial, &tmp_c, 1) != 1) | |
253 | break; | |
254 | sr_dbg("Got metadata key 0x%.2x value 0x%.2x.", | |
255 | key, tmp_c); | |
256 | switch (token) { | |
257 | case 0x00: | |
258 | /* Number of usable probes */ | |
259 | for (ui = 0; ui < tmp_c; ui++) { | |
260 | if (!(probe = sr_probe_new(ui, SR_PROBE_LOGIC, TRUE, | |
261 | ols_probe_names[ui]))) | |
262 | return 0; | |
263 | sdi->probes = g_slist_append(sdi->probes, probe); | |
264 | } | |
265 | break; | |
266 | case 0x01: | |
267 | /* protocol version */ | |
268 | devc->protocol_version = tmp_c; | |
269 | break; | |
270 | default: | |
271 | sr_info("Unknown token 0x%.2x: 0x%.2x.", | |
272 | token, tmp_c); | |
273 | break; | |
274 | } | |
275 | break; | |
276 | default: | |
277 | /* unknown type */ | |
278 | break; | |
279 | } | |
280 | } | |
281 | ||
282 | sdi->model = devname->str; | |
283 | sdi->version = version->str; | |
284 | g_string_free(devname, FALSE); | |
285 | g_string_free(version, FALSE); | |
286 | ||
287 | return sdi; | |
288 | } | |
289 | ||
290 | SR_PRIV int ols_set_samplerate(const struct sr_dev_inst *sdi, | |
e46aa4f6 | 291 | const uint64_t samplerate) |
0aba65da UH |
292 | { |
293 | struct dev_context *devc; | |
294 | ||
295 | devc = sdi->priv; | |
e46aa4f6 | 296 | if (devc->max_samplerate && samplerate > devc->max_samplerate) |
0aba65da UH |
297 | return SR_ERR_SAMPLERATE; |
298 | ||
299 | if (samplerate > CLOCK_RATE) { | |
300 | devc->flag_reg |= FLAG_DEMUX; | |
301 | devc->cur_samplerate_divider = (CLOCK_RATE * 2 / samplerate) - 1; | |
302 | } else { | |
303 | devc->flag_reg &= ~FLAG_DEMUX; | |
304 | devc->cur_samplerate_divider = (CLOCK_RATE / samplerate) - 1; | |
305 | } | |
306 | ||
307 | /* Calculate actual samplerate used and complain if it is different | |
308 | * from the requested. | |
309 | */ | |
310 | devc->cur_samplerate = CLOCK_RATE / (devc->cur_samplerate_divider + 1); | |
311 | if (devc->flag_reg & FLAG_DEMUX) | |
312 | devc->cur_samplerate *= 2; | |
313 | if (devc->cur_samplerate != samplerate) | |
e46aa4f6 | 314 | sr_info("Can't match samplerate %" PRIu64 ", using %" |
0aba65da UH |
315 | PRIu64 ".", samplerate, devc->cur_samplerate); |
316 | ||
317 | return SR_OK; | |
318 | } | |
319 | ||
320 | SR_PRIV void abort_acquisition(const struct sr_dev_inst *sdi) | |
321 | { | |
322 | struct sr_datafeed_packet packet; | |
459a0f26 | 323 | struct sr_serial_dev_inst *serial; |
0aba65da | 324 | |
459a0f26 BV |
325 | serial = sdi->conn; |
326 | sr_source_remove(serial->fd); | |
0aba65da UH |
327 | |
328 | /* Terminate session */ | |
329 | packet.type = SR_DF_END; | |
330 | sr_session_send(sdi, &packet); | |
331 | } | |
332 | ||
333 | SR_PRIV int ols_receive_data(int fd, int revents, void *cb_data) | |
334 | { | |
459a0f26 BV |
335 | struct drv_context *drvc; |
336 | struct dev_context *devc; | |
337 | struct sr_serial_dev_inst *serial; | |
0aba65da UH |
338 | struct sr_datafeed_packet packet; |
339 | struct sr_datafeed_logic logic; | |
340 | struct sr_dev_inst *sdi; | |
0aba65da | 341 | GSList *l; |
fe9ac252 | 342 | uint32_t sample; |
00d04d3b | 343 | int num_channels, offset, j; |
f51acd69 | 344 | unsigned int i, max_channels; |
0aba65da UH |
345 | unsigned char byte; |
346 | ||
347 | drvc = di->priv; | |
348 | ||
349 | /* Find this device's devc struct by its fd. */ | |
350 | devc = NULL; | |
351 | for (l = drvc->instances; l; l = l->next) { | |
352 | sdi = l->data; | |
353 | devc = sdi->priv; | |
459a0f26 BV |
354 | serial = sdi->conn; |
355 | if (serial->fd == fd) | |
0aba65da UH |
356 | break; |
357 | devc = NULL; | |
358 | } | |
359 | if (!devc) | |
360 | /* Shouldn't happen. */ | |
361 | return TRUE; | |
362 | ||
363 | if (devc->num_transfers++ == 0) { | |
364 | /* | |
365 | * First time round, means the device started sending data, | |
366 | * and will not stop until done. If it stops sending for | |
367 | * longer than it takes to send a byte, that means it's | |
368 | * finished. We'll double that to 30ms to be sure... | |
369 | */ | |
370 | sr_source_remove(fd); | |
371 | sr_source_add(fd, G_IO_IN, 30, ols_receive_data, cb_data); | |
372 | devc->raw_sample_buf = g_try_malloc(devc->limit_samples * 4); | |
373 | if (!devc->raw_sample_buf) { | |
374 | sr_err("Sample buffer malloc failed."); | |
375 | return FALSE; | |
376 | } | |
377 | /* fill with 1010... for debugging */ | |
378 | memset(devc->raw_sample_buf, 0x82, devc->limit_samples * 4); | |
379 | } | |
380 | ||
381 | num_channels = 0; | |
f51acd69 MR |
382 | max_channels = NUM_PROBES / (devc->flag_reg & FLAG_DEMUX ? 2 : 1); |
383 | ||
384 | for (i = NUM_PROBES; i > 0x02; i /= 2) { | |
385 | if ((devc->flag_reg & i) == 0) { | |
386 | if (i >= max_channels) { | |
387 | sr_err("Channels over the limit of %d\n", max_channels); | |
388 | return FALSE; | |
389 | } | |
0aba65da | 390 | num_channels++; |
f51acd69 | 391 | } |
0aba65da UH |
392 | } |
393 | ||
faf72024 | 394 | if (revents == G_IO_IN && devc->num_samples < devc->limit_samples) { |
459a0f26 | 395 | if (serial_read(serial, &byte, 1) != 1) |
0aba65da UH |
396 | return FALSE; |
397 | ||
398 | /* Ignore it if we've read enough. */ | |
399 | if (devc->num_samples >= devc->limit_samples) | |
400 | return TRUE; | |
401 | ||
402 | devc->sample[devc->num_bytes++] = byte; | |
403 | sr_dbg("Received byte 0x%.2x.", byte); | |
404 | if (devc->num_bytes == num_channels) { | |
00d04d3b BV |
405 | /* Got a full sample. Convert from the OLS's little-endian |
406 | * sample to the local format. */ | |
fe9ac252 BV |
407 | sample = devc->sample[0] | (devc->sample[1] << 8) \ |
408 | | (devc->sample[2] << 16) | (devc->sample[3] << 24); | |
409 | sr_dbg("Received sample 0x%.*x.", devc->num_bytes * 2, sample); | |
0aba65da UH |
410 | if (devc->flag_reg & FLAG_RLE) { |
411 | /* | |
00d04d3b BV |
412 | * In RLE mode the high bit of the sample is the |
413 | * "count" flag, meaning this sample is the number | |
414 | * of times the previous sample occurred. | |
0aba65da UH |
415 | */ |
416 | if (devc->sample[devc->num_bytes - 1] & 0x80) { | |
00d04d3b BV |
417 | /* Clear the high bit. */ |
418 | sample &= ~(0x80 << (devc->num_bytes - 1) * 8); | |
fe9ac252 | 419 | devc->rle_count = sample; |
00d04d3b | 420 | sr_dbg("RLE count: %u.", devc->rle_count); |
0aba65da UH |
421 | devc->num_bytes = 0; |
422 | return TRUE; | |
423 | } | |
424 | } | |
425 | devc->num_samples += devc->rle_count + 1; | |
426 | if (devc->num_samples > devc->limit_samples) { | |
427 | /* Save us from overrunning the buffer. */ | |
428 | devc->rle_count -= devc->num_samples - devc->limit_samples; | |
429 | devc->num_samples = devc->limit_samples; | |
430 | } | |
431 | ||
432 | if (num_channels < 4) { | |
433 | /* | |
434 | * Some channel groups may have been turned | |
435 | * off, to speed up transfer between the | |
436 | * hardware and the PC. Expand that here before | |
437 | * submitting it over the session bus -- | |
438 | * whatever is listening on the bus will be | |
439 | * expecting a full 32-bit sample, based on | |
440 | * the number of probes. | |
441 | */ | |
442 | j = 0; | |
443 | memset(devc->tmp_sample, 0, 4); | |
444 | for (i = 0; i < 4; i++) { | |
445 | if (((devc->flag_reg >> 2) & (1 << i)) == 0) { | |
446 | /* | |
447 | * This channel group was | |
448 | * enabled, copy from received | |
449 | * sample. | |
450 | */ | |
451 | devc->tmp_sample[i] = devc->sample[j++]; | |
f51acd69 MR |
452 | } else if (devc->flag_reg & FLAG_DEMUX && (i > 2)) { |
453 | /* group 3 & 4 get added to 1 & 2 */ | |
454 | devc->tmp_sample[i - 2] = devc->sample[j++]; | |
0aba65da UH |
455 | } |
456 | } | |
457 | memcpy(devc->sample, devc->tmp_sample, 4); | |
fe9ac252 | 458 | sr_dbg("Full sample: 0x%.8x.", sample); |
0aba65da UH |
459 | } |
460 | ||
461 | /* the OLS sends its sample buffer backwards. | |
462 | * store it in reverse order here, so we can dump | |
463 | * this on the session bus later. | |
464 | */ | |
465 | offset = (devc->limit_samples - devc->num_samples) * 4; | |
466 | for (i = 0; i <= devc->rle_count; i++) { | |
467 | memcpy(devc->raw_sample_buf + offset + (i * 4), | |
468 | devc->sample, 4); | |
469 | } | |
470 | memset(devc->sample, 0, 4); | |
471 | devc->num_bytes = 0; | |
472 | devc->rle_count = 0; | |
473 | } | |
474 | } else { | |
475 | /* | |
476 | * This is the main loop telling us a timeout was reached, or | |
477 | * we've acquired all the samples we asked for -- we're done. | |
478 | * Send the (properly-ordered) buffer to the frontend. | |
479 | */ | |
480 | if (devc->trigger_at != -1) { | |
481 | /* a trigger was set up, so we need to tell the frontend | |
482 | * about it. | |
483 | */ | |
484 | if (devc->trigger_at > 0) { | |
485 | /* there are pre-trigger samples, send those first */ | |
486 | packet.type = SR_DF_LOGIC; | |
487 | packet.payload = &logic; | |
488 | logic.length = devc->trigger_at * 4; | |
489 | logic.unitsize = 4; | |
490 | logic.data = devc->raw_sample_buf + | |
491 | (devc->limit_samples - devc->num_samples) * 4; | |
492 | sr_session_send(cb_data, &packet); | |
493 | } | |
494 | ||
495 | /* send the trigger */ | |
496 | packet.type = SR_DF_TRIGGER; | |
497 | sr_session_send(cb_data, &packet); | |
498 | ||
499 | /* send post-trigger samples */ | |
500 | packet.type = SR_DF_LOGIC; | |
501 | packet.payload = &logic; | |
502 | logic.length = (devc->num_samples * 4) - (devc->trigger_at * 4); | |
503 | logic.unitsize = 4; | |
504 | logic.data = devc->raw_sample_buf + devc->trigger_at * 4 + | |
505 | (devc->limit_samples - devc->num_samples) * 4; | |
506 | sr_session_send(cb_data, &packet); | |
507 | } else { | |
508 | /* no trigger was used */ | |
509 | packet.type = SR_DF_LOGIC; | |
510 | packet.payload = &logic; | |
511 | logic.length = devc->num_samples * 4; | |
512 | logic.unitsize = 4; | |
513 | logic.data = devc->raw_sample_buf + | |
514 | (devc->limit_samples - devc->num_samples) * 4; | |
515 | sr_session_send(cb_data, &packet); | |
516 | } | |
517 | g_free(devc->raw_sample_buf); | |
518 | ||
459a0f26 | 519 | serial_flush(serial); |
0aba65da | 520 | abort_acquisition(sdi); |
0aba65da UH |
521 | } |
522 | ||
523 | return TRUE; | |
524 | } |