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