]> sigrok.org Git - libsigrok.git/blame - src/hardware/openbench-logic-sniffer/protocol.c
ols: Fix off-by-one when setting up trigger stages
[libsigrok.git] / src / 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
6ec6c43b 20#include <config.h>
515ab088 21#include "protocol.h"
0aba65da 22
f8fd8420 23struct ols_basic_trigger_desc {
24 uint32_t trigger_mask[NUM_BASIC_TRIGGER_STAGES];
25 uint32_t trigger_value[NUM_BASIC_TRIGGER_STAGES];
26 int num_stages;
27};
28
0aba65da 29SR_PRIV int send_shortcommand(struct sr_serial_dev_inst *serial,
55eca716 30 uint8_t command)
0aba65da
UH
31{
32 char buf[1];
33
34 sr_dbg("Sending cmd 0x%.2x.", command);
35 buf[0] = command;
f4d3a4fb 36 if (serial_write_blocking(serial, buf, 1, serial_timeout(serial, 1)) != 1)
0aba65da
UH
37 return SR_ERR;
38
dcdc2848 39 if (serial_drain(serial) != SR_OK)
bce75f94
UJ
40 return SR_ERR;
41
0aba65da
UH
42 return SR_OK;
43}
44
55eca716 45SR_PRIV int send_longcommand(struct sr_serial_dev_inst *serial, uint8_t command,
46 uint8_t *data)
0aba65da
UH
47{
48 char buf[5];
49
55eca716 50 sr_dbg("Sending cmd 0x%.2x data 0x%.2x%.2x%.2x%.2x.", command, data[0],
51 data[1], data[2], data[3]);
0aba65da 52 buf[0] = command;
016e72f3
BV
53 buf[1] = data[0];
54 buf[2] = data[1];
55 buf[3] = data[2];
56 buf[4] = data[3];
f4d3a4fb 57 if (serial_write_blocking(serial, buf, 5, serial_timeout(serial, 1)) != 5)
0aba65da
UH
58 return SR_ERR;
59
dcdc2848 60 if (serial_drain(serial) != SR_OK)
bce75f94
UJ
61 return SR_ERR;
62
0aba65da
UH
63 return SR_OK;
64}
65
55eca716 66static int ols_send_longdata(struct sr_serial_dev_inst *serial, uint8_t command,
67 uint32_t value)
3cc9e215 68{
69 uint8_t data[4];
70 WL32(data, value);
71 return send_longcommand(serial, command, data);
72}
73
244995a2
GGM
74SR_PRIV int ols_send_reset(struct sr_serial_dev_inst *serial)
75{
76 unsigned int i;
77
78 for (i = 0; i < 5; i++) {
79 if (send_shortcommand(serial, CMD_RESET) != SR_OK)
80 return SR_ERR;
81 }
82
83 return SR_OK;
84}
85
91fd0f72 86/* Configures the channel mask based on which channels are enabled. */
f8fd8420 87SR_PRIV uint32_t ols_channel_mask(const struct sr_dev_inst *sdi)
0aba65da 88{
f8fd8420 89 uint32_t channel_mask = 0;
90 for (const GSList *l = sdi->channels; l; l = l->next) {
91 struct sr_channel *channel = l->data;
91fd0f72 92 if (channel->enabled)
f8fd8420 93 channel_mask |= 1 << channel->index;
91fd0f72 94 }
f8fd8420 95
96 return channel_mask;
91fd0f72
BV
97}
98
55eca716 99static int convert_trigger(const struct sr_dev_inst *sdi,
100 struct ols_basic_trigger_desc *ols_trigger)
91fd0f72 101{
91fd0f72
BV
102 struct sr_trigger *trigger;
103 struct sr_trigger_stage *stage;
104 struct sr_trigger_match *match;
105 const GSList *l, *m;
106 int i;
107
f8fd8420 108 ols_trigger->num_stages = 0;
109 for (i = 0; i < NUM_BASIC_TRIGGER_STAGES; i++) {
110 ols_trigger->trigger_mask[i] = 0;
111 ols_trigger->trigger_value[i] = 0;
0aba65da
UH
112 }
113
0812c40e 114 if (!(trigger = sr_session_trigger_get(sdi->session)))
91fd0f72 115 return SR_OK;
0aba65da 116
f8fd8420 117 ols_trigger->num_stages = g_slist_length(trigger->stages);
118 if (ols_trigger->num_stages > NUM_BASIC_TRIGGER_STAGES) {
91fd0f72 119 sr_err("This device only supports %d trigger stages.",
55eca716 120 NUM_BASIC_TRIGGER_STAGES);
91fd0f72
BV
121 return SR_ERR;
122 }
b1de0407 123
91fd0f72
BV
124 for (l = trigger->stages; l; l = l->next) {
125 stage = l->data;
126 for (m = stage->matches; m; m = m->next) {
127 match = m->data;
128 if (!match->channel->enabled)
129 /* Ignore disabled channels with a trigger. */
130 continue;
55eca716 131 ols_trigger->trigger_mask[stage->stage] |=
132 1 << match->channel->index;
91fd0f72 133 if (match->match == SR_TRIGGER_ONE)
55eca716 134 ols_trigger->trigger_value[stage->stage] |=
135 1 << match->channel->index;
0aba65da 136 }
0aba65da
UH
137 }
138
139 return SR_OK;
140}
141
0aba65da
UH
142SR_PRIV struct dev_context *ols_dev_new(void)
143{
144 struct dev_context *devc;
145
f57d8ffe 146 devc = g_malloc0(sizeof(struct dev_context));
a2b1a53b 147 devc->trigger_at_smpl = OLS_NO_TRIGGER;
bf256783 148
0aba65da
UH
149 return devc;
150}
151
4a34a74d
WS
152static void ols_channel_new(struct sr_dev_inst *sdi, int num_chan)
153{
ea642977 154 struct dev_context *devc = sdi->priv;
4a34a74d
WS
155 int i;
156
157 for (i = 0; i < num_chan; i++)
158 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
55eca716 159 ols_channel_names[i]);
ea642977
WS
160
161 devc->max_channels = num_chan;
4a34a74d
WS
162}
163
aad6b9de 164static void ols_metadata_quirks(struct sr_dev_inst *sdi)
ad4174c1
GS
165{
166 struct dev_context *devc;
167 gboolean is_shrimp;
168
169 if (!sdi)
170 return;
171 devc = sdi->priv;
172 if (!devc)
173 return;
174
175 is_shrimp = sdi->model && strcmp(sdi->model, "Shrimp1.0") == 0;
176 if (is_shrimp) {
177 if (!devc->max_channels)
178 ols_channel_new(sdi, 4);
179 if (!devc->max_samples)
180 devc->max_samples = 256 * 1024;
181 if (!devc->max_samplerate)
182 devc->max_samplerate = SR_MHZ(20);
183 }
6f9234e6 184
185 if (sdi->version && strstr(sdi->version, "FPGA version 3.07"))
186 devc->device_flags |= DEVICE_FLAG_IS_DEMON_CORE;
ad4174c1
GS
187}
188
0aba65da
UH
189SR_PRIV struct sr_dev_inst *get_metadata(struct sr_serial_dev_inst *serial)
190{
191 struct sr_dev_inst *sdi;
192 struct dev_context *devc;
4a34a74d 193 uint32_t tmp_int;
6f9234e6 194 uint8_t key, type;
f4d3a4fb 195 int delay_ms;
0aba65da
UH
196 GString *tmp_str, *devname, *version;
197 guchar tmp_c;
198
aac29cc1 199 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be 200 sdi->status = SR_ST_INACTIVE;
0aba65da
UH
201 devc = ols_dev_new();
202 sdi->priv = devc;
203
204 devname = g_string_new("");
205 version = g_string_new("");
206
207 key = 0xff;
208 while (key) {
f4d3a4fb
BV
209 delay_ms = serial_timeout(serial, 1);
210 if (serial_read_blocking(serial, &key, 1, delay_ms) != 1)
0aba65da 211 break;
6f9234e6 212 if (key == METADATA_TOKEN_END) {
625763e2
BV
213 sr_dbg("Got metadata key 0x00, metadata ends.");
214 break;
215 }
0aba65da 216 type = key >> 5;
0aba65da
UH
217 switch (type) {
218 case 0:
219 /* NULL-terminated string */
220 tmp_str = g_string_new("");
f4d3a4fb 221 delay_ms = serial_timeout(serial, 1);
55eca716 222 while (serial_read_blocking(serial, &tmp_c, 1,
223 delay_ms) == 1 &&
224 tmp_c != '\0')
0aba65da 225 g_string_append_c(tmp_str, tmp_c);
55eca716 226 sr_dbg("Got metadata token 0x%.2x value '%s'.", key,
227 tmp_str->str);
6f9234e6 228 switch (key) {
229 case METADATA_TOKEN_DEVICE_NAME:
0aba65da 230 /* Device name */
55eca716 231 devname =
232 g_string_append(devname, tmp_str->str);
0aba65da 233 break;
6f9234e6 234 case METADATA_TOKEN_FPGA_VERSION:
0aba65da
UH
235 /* FPGA firmware version */
236 if (version->len)
237 g_string_append(version, ", ");
238 g_string_append(version, "FPGA version ");
239 g_string_append(version, tmp_str->str);
240 break;
6f9234e6 241 case METADATA_TOKEN_ANCILLARY_VERSION:
0aba65da
UH
242 /* Ancillary version */
243 if (version->len)
244 g_string_append(version, ", ");
245 g_string_append(version, "Ancillary version ");
246 g_string_append(version, tmp_str->str);
247 break;
248 default:
55eca716 249 sr_info("ols: unknown token 0x%.2x: '%s'", key,
250 tmp_str->str);
0aba65da
UH
251 break;
252 }
253 g_string_free(tmp_str, TRUE);
254 break;
255 case 1:
256 /* 32-bit unsigned integer */
f4d3a4fb 257 delay_ms = serial_timeout(serial, 4);
55eca716 258 if (serial_read_blocking(serial, &tmp_int, 4,
259 delay_ms) != 4)
0aba65da 260 break;
016e72f3 261 tmp_int = RB32(&tmp_int);
55eca716 262 sr_dbg("Got metadata token 0x%.2x value 0x%.8x.", key,
263 tmp_int);
6f9234e6 264 switch (key) {
265 case METADATA_TOKEN_NUM_PROBES_LONG:
ba7dd8bb 266 /* Number of usable channels */
4a34a74d 267 ols_channel_new(sdi, tmp_int);
0aba65da 268 break;
6f9234e6 269 case METADATA_TOKEN_SAMPLE_MEMORY_BYTES:
0aba65da
UH
270 /* Amount of sample memory available (bytes) */
271 devc->max_samples = tmp_int;
272 break;
6f9234e6 273 case METADATA_TOKEN_DYNAMIC_MEMORY_BYTES:
0aba65da
UH
274 /* Amount of dynamic memory available (bytes) */
275 /* what is this for? */
276 break;
6f9234e6 277 case METADATA_TOKEN_MAX_SAMPLE_RATE_HZ:
f3f19d11 278 /* Maximum sample rate (Hz) */
0aba65da
UH
279 devc->max_samplerate = tmp_int;
280 break;
6f9234e6 281 case METADATA_TOKEN_PROTOCOL_VERSION_LONG:
0aba65da
UH
282 /* protocol version */
283 devc->protocol_version = tmp_int;
284 break;
285 default:
55eca716 286 sr_info("Unknown token 0x%.2x: 0x%.8x.", key,
287 tmp_int);
0aba65da
UH
288 break;
289 }
290 break;
291 case 2:
292 /* 8-bit unsigned integer */
f4d3a4fb
BV
293 delay_ms = serial_timeout(serial, 1);
294 if (serial_read_blocking(serial, &tmp_c, 1, delay_ms) != 1)
0aba65da 295 break;
55eca716 296 sr_dbg("Got metadata token 0x%.2x value 0x%.2x.", key,
297 tmp_c);
6f9234e6 298 switch (key) {
299 case METADATA_TOKEN_NUM_PROBES_SHORT:
ba7dd8bb 300 /* Number of usable channels */
4a34a74d 301 ols_channel_new(sdi, tmp_c);
0aba65da 302 break;
6f9234e6 303 case METADATA_TOKEN_PROTOCOL_VERSION_SHORT:
0aba65da
UH
304 /* protocol version */
305 devc->protocol_version = tmp_c;
306 break;
307 default:
55eca716 308 sr_info("Unknown token 0x%.2x: 0x%.2x.", key,
309 tmp_c);
0aba65da
UH
310 break;
311 }
312 break;
313 default:
314 /* unknown type */
315 break;
316 }
317 }
318
319 sdi->model = devname->str;
320 sdi->version = version->str;
321 g_string_free(devname, FALSE);
322 g_string_free(version, FALSE);
323
ad4174c1 324 /* Optionally amend received metadata, model specific quirks. */
aad6b9de 325 ols_metadata_quirks(sdi);
ad4174c1 326
0aba65da
UH
327 return sdi;
328}
329
330SR_PRIV int ols_set_samplerate(const struct sr_dev_inst *sdi,
55eca716 331 const uint64_t samplerate)
0aba65da
UH
332{
333 struct dev_context *devc;
334
335 devc = sdi->priv;
e46aa4f6 336 if (devc->max_samplerate && samplerate > devc->max_samplerate)
0aba65da
UH
337 return SR_ERR_SAMPLERATE;
338
339 if (samplerate > CLOCK_RATE) {
6ebe0039 340 sr_info("Enabling demux mode.");
a80bed76 341 devc->capture_flags |= CAPTURE_FLAG_DEMUX;
342 devc->capture_flags &= ~CAPTURE_FLAG_NOISE_FILTER;
55eca716 343 devc->cur_samplerate_divider =
344 (CLOCK_RATE * 2 / samplerate) - 1;
0aba65da 345 } else {
6ebe0039 346 sr_info("Disabling demux mode.");
a80bed76 347 devc->capture_flags &= ~CAPTURE_FLAG_DEMUX;
348 devc->capture_flags |= CAPTURE_FLAG_NOISE_FILTER;
0aba65da
UH
349 devc->cur_samplerate_divider = (CLOCK_RATE / samplerate) - 1;
350 }
351
352 /* Calculate actual samplerate used and complain if it is different
353 * from the requested.
354 */
355 devc->cur_samplerate = CLOCK_RATE / (devc->cur_samplerate_divider + 1);
a80bed76 356 if (devc->capture_flags & CAPTURE_FLAG_DEMUX)
0aba65da
UH
357 devc->cur_samplerate *= 2;
358 if (devc->cur_samplerate != samplerate)
55eca716 359 sr_info("Can't match samplerate %" PRIu64 ", using %" PRIu64
360 ".",
361 samplerate, devc->cur_samplerate);
0aba65da
UH
362
363 return SR_OK;
364}
365
366SR_PRIV void abort_acquisition(const struct sr_dev_inst *sdi)
367{
459a0f26 368 struct sr_serial_dev_inst *serial;
0aba65da 369
459a0f26 370 serial = sdi->conn;
7dd1dd9f 371 ols_send_reset(serial);
6d8182b6 372
102f1239 373 serial_source_remove(sdi->session, serial);
0aba65da 374
bee2b016 375 std_session_send_df_end(sdi);
0aba65da
UH
376}
377
378SR_PRIV int ols_receive_data(int fd, int revents, void *cb_data)
379{
459a0f26 380 struct dev_context *devc;
625763e2 381 struct sr_dev_inst *sdi;
459a0f26 382 struct sr_serial_dev_inst *serial;
0aba65da
UH
383 struct sr_datafeed_packet packet;
384 struct sr_datafeed_logic logic;
fe9ac252 385 uint32_t sample;
f8fd8420 386 int num_changroups, offset, j;
b1de0407 387 unsigned int i;
0aba65da 388 unsigned char byte;
625763e2
BV
389
390 (void)fd;
391
392 sdi = cb_data;
393 serial = sdi->conn;
394 devc = sdi->priv;
0aba65da 395
8105e829
DE
396 if (devc->num_transfers == 0 && revents == 0) {
397 /* Ignore timeouts as long as we haven't received anything */
398 return TRUE;
399 }
400
0aba65da 401 if (devc->num_transfers++ == 0) {
0aba65da
UH
402 devc->raw_sample_buf = g_try_malloc(devc->limit_samples * 4);
403 if (!devc->raw_sample_buf) {
404 sr_err("Sample buffer malloc failed.");
405 return FALSE;
406 }
407 /* fill with 1010... for debugging */
408 memset(devc->raw_sample_buf, 0x82, devc->limit_samples * 4);
409 }
410
f8fd8420 411 num_changroups = 0;
be15c51e 412 for (i = 0x20; i > 0x02; i >>= 1) {
a80bed76 413 if ((devc->capture_flags & i) == 0) {
f8fd8420 414 num_changroups++;
f51acd69 415 }
0aba65da
UH
416 }
417
faf72024 418 if (revents == G_IO_IN && devc->num_samples < devc->limit_samples) {
9f5d4c3c 419 if (serial_read_nonblocking(serial, &byte, 1) != 1)
0aba65da 420 return FALSE;
625763e2 421 devc->cnt_bytes++;
0aba65da
UH
422
423 /* Ignore it if we've read enough. */
424 if (devc->num_samples >= devc->limit_samples)
425 return TRUE;
426
427 devc->sample[devc->num_bytes++] = byte;
6d16fdfb 428 sr_spew("Received byte 0x%.2x.", byte);
f8fd8420 429 if (devc->num_bytes == num_changroups) {
625763e2
BV
430 devc->cnt_samples++;
431 devc->cnt_samples_rle++;
432 /*
433 * Got a full sample. Convert from the OLS's little-endian
434 * sample to the local format.
435 */
55eca716 436 sample = devc->sample[0] | (devc->sample[1] << 8) |
437 (devc->sample[2] << 16) |
438 (devc->sample[3] << 24);
439 sr_dbg("Received sample 0x%.*x.", devc->num_bytes * 2,
440 sample);
a80bed76 441 if (devc->capture_flags & CAPTURE_FLAG_RLE) {
0aba65da 442 /*
00d04d3b
BV
443 * In RLE mode the high bit of the sample is the
444 * "count" flag, meaning this sample is the number
445 * of times the previous sample occurred.
0aba65da
UH
446 */
447 if (devc->sample[devc->num_bytes - 1] & 0x80) {
00d04d3b 448 /* Clear the high bit. */
55eca716 449 sample &= ~(0x80 << (devc->num_bytes -
450 1) * 8);
fe9ac252 451 devc->rle_count = sample;
55eca716 452 devc->cnt_samples_rle +=
453 devc->rle_count;
454 sr_dbg("RLE count: %u.",
455 devc->rle_count);
0aba65da
UH
456 devc->num_bytes = 0;
457 return TRUE;
458 }
459 }
460 devc->num_samples += devc->rle_count + 1;
461 if (devc->num_samples > devc->limit_samples) {
462 /* Save us from overrunning the buffer. */
55eca716 463 devc->rle_count -=
464 devc->num_samples - devc->limit_samples;
0aba65da
UH
465 devc->num_samples = devc->limit_samples;
466 }
467
f8fd8420 468 if (num_changroups < 4) {
0aba65da
UH
469 /*
470 * Some channel groups may have been turned
471 * off, to speed up transfer between the
472 * hardware and the PC. Expand that here before
473 * submitting it over the session bus --
474 * whatever is listening on the bus will be
475 * expecting a full 32-bit sample, based on
ba7dd8bb 476 * the number of channels.
0aba65da
UH
477 */
478 j = 0;
cfe55d09 479 uint8_t tmp_sample[4] = { 0, 0, 0, 0 };
0aba65da 480 for (i = 0; i < 4; i++) {
55eca716 481 if (((devc->capture_flags >> 2) &
482 (1 << i)) == 0) {
0aba65da
UH
483 /*
484 * This channel group was
485 * enabled, copy from received
486 * sample.
487 */
cfe55d09 488 tmp_sample[i] =
55eca716 489 devc->sample[j++];
0aba65da
UH
490 }
491 }
cfe55d09 492 memcpy(devc->sample, tmp_sample, 4);
29f15d52 493 sr_spew("Expanded sample: 0x%.2hhx%.2hhx%.2hhx%.2hhx ",
55eca716 494 devc->sample[3], devc->sample[2],
495 devc->sample[1], devc->sample[0]);
0aba65da
UH
496 }
497
625763e2
BV
498 /*
499 * the OLS sends its sample buffer backwards.
0aba65da
UH
500 * store it in reverse order here, so we can dump
501 * this on the session bus later.
502 */
503 offset = (devc->limit_samples - devc->num_samples) * 4;
504 for (i = 0; i <= devc->rle_count; i++) {
505 memcpy(devc->raw_sample_buf + offset + (i * 4),
506 devc->sample, 4);
507 }
508 memset(devc->sample, 0, 4);
509 devc->num_bytes = 0;
510 devc->rle_count = 0;
511 }
512 } else {
513 /*
514 * This is the main loop telling us a timeout was reached, or
515 * we've acquired all the samples we asked for -- we're done.
516 * Send the (properly-ordered) buffer to the frontend.
517 */
625763e2 518 sr_dbg("Received %d bytes, %d samples, %d decompressed samples.",
55eca716 519 devc->cnt_bytes, devc->cnt_samples,
520 devc->cnt_samples_rle);
a2b1a53b 521 if (devc->trigger_at_smpl != OLS_NO_TRIGGER) {
625763e2
BV
522 /*
523 * A trigger was set up, so we need to tell the frontend
0aba65da
UH
524 * about it.
525 */
a2b1a53b 526 if (devc->trigger_at_smpl > 0) {
625763e2 527 /* There are pre-trigger samples, send those first. */
0aba65da
UH
528 packet.type = SR_DF_LOGIC;
529 packet.payload = &logic;
a2b1a53b 530 logic.length = devc->trigger_at_smpl * 4;
0aba65da
UH
531 logic.unitsize = 4;
532 logic.data = devc->raw_sample_buf +
55eca716 533 (devc->limit_samples -
534 devc->num_samples) *
535 4;
695dc859 536 sr_session_send(sdi, &packet);
0aba65da
UH
537 }
538
625763e2 539 /* Send the trigger. */
0fa71943 540 std_session_send_df_trigger(sdi);
0aba65da 541 }
2755ab36 542
543 /* Send post-trigger / all captured samples. */
55eca716 544 int num_pre_trigger_samples = devc->trigger_at_smpl ==
545 OLS_NO_TRIGGER ?
546 0 :
547 devc->trigger_at_smpl;
2755ab36 548 packet.type = SR_DF_LOGIC;
549 packet.payload = &logic;
55eca716 550 logic.length =
551 (devc->num_samples - num_pre_trigger_samples) * 4;
2755ab36 552 logic.unitsize = 4;
55eca716 553 logic.data = devc->raw_sample_buf +
554 (num_pre_trigger_samples + devc->limit_samples -
555 devc->num_samples) *
556 4;
2755ab36 557 sr_session_send(sdi, &packet);
558
0aba65da
UH
559 g_free(devc->raw_sample_buf);
560
459a0f26 561 serial_flush(serial);
0aba65da 562 abort_acquisition(sdi);
0aba65da
UH
563 }
564
565 return TRUE;
566}
f8fd8420 567
55eca716 568static int
569ols_set_basic_trigger_stage(const struct ols_basic_trigger_desc *trigger_desc,
570 struct sr_serial_dev_inst *serial, int stage)
f8fd8420 571{
572 uint8_t cmd, arg[4];
573
574 cmd = CMD_SET_BASIC_TRIGGER_MASK0 + stage * 4;
3cc9e215 575 if (ols_send_longdata(serial, cmd, trigger_desc->trigger_mask[stage]) != SR_OK)
f8fd8420 576 return SR_ERR;
577
578 cmd = CMD_SET_BASIC_TRIGGER_VALUE0 + stage * 4;
3cc9e215 579 if (ols_send_longdata(serial, cmd, trigger_desc->trigger_value[stage]) != SR_OK)
f8fd8420 580 return SR_ERR;
581
582 cmd = CMD_SET_BASIC_TRIGGER_CONFIG0 + stage * 4;
583 arg[0] = arg[1] = arg[3] = 0x00;
584 arg[2] = stage;
b28e391a 585 if (stage == trigger_desc->num_stages - 1)
f8fd8420 586 /* Last stage, fire when this one matches. */
587 arg[3] |= TRIGGER_START;
588 if (send_longcommand(serial, cmd, arg) != SR_OK)
589 return SR_ERR;
590
591 return SR_OK;
592}
593
55eca716 594SR_PRIV int ols_prepare_acquisition(const struct sr_dev_inst *sdi)
595{
f8fd8420 596 int ret;
f8fd8420 597
598 struct dev_context *devc = sdi->priv;
599 struct sr_serial_dev_inst *serial = sdi->conn;
600
601 int num_changroups = 0;
602 uint8_t changroup_mask = 0;
603 uint32_t channel_mask = ols_channel_mask(sdi);
604 for (unsigned int i = 0; i < 4; i++) {
605 if (channel_mask & (0xff << (i * 8))) {
606 changroup_mask |= (1 << i);
607 num_changroups++;
608 }
609 }
610
611 /*
612 * Limit readcount to prevent reading past the end of the hardware
613 * buffer. Rather read too many samples than too few.
614 */
55eca716 615 uint32_t samplecount =
616 MIN(devc->max_samples / num_changroups, devc->limit_samples);
f8fd8420 617 uint32_t readcount = (samplecount + 3) / 4;
618 uint32_t delaycount;
619
620 /* Basic triggers. */
621 struct ols_basic_trigger_desc basic_trigger_desc;
622 if (convert_trigger(sdi, &basic_trigger_desc) != SR_OK) {
623 sr_err("Failed to configure channels.");
624 return SR_ERR;
625 }
626 if (basic_trigger_desc.num_stages > 0) {
627 /*
628 * According to http://mygizmos.org/ols/Logic-Sniffer-FPGA-Spec.pdf
629 * reset command must be send prior each arm command
630 */
631 sr_dbg("Send reset command before trigger configure");
632 if (ols_send_reset(serial) != SR_OK)
633 return SR_ERR;
634
635 delaycount = readcount * (1 - devc->capture_ratio / 100.0);
55eca716 636 devc->trigger_at_smpl = (readcount - delaycount) * 4 -
637 basic_trigger_desc.num_stages;
b28e391a 638 for (int i = 0; i < basic_trigger_desc.num_stages; i++) {
f8fd8420 639 sr_dbg("Setting OLS stage %d trigger.", i);
55eca716 640 if ((ret = ols_set_basic_trigger_stage(
641 &basic_trigger_desc, serial, i)) != SR_OK)
f8fd8420 642 return ret;
643 }
644 } else {
645 /* No triggers configured, force trigger on first stage. */
646 sr_dbg("Forcing trigger at stage 0.");
b28e391a 647 basic_trigger_desc.num_stages = 1;
55eca716 648 if ((ret = ols_set_basic_trigger_stage(&basic_trigger_desc,
649 serial, 0)) != SR_OK)
f8fd8420 650 return ret;
651 delaycount = readcount;
652 }
653
654 /* Samplerate. */
655 sr_dbg("Setting samplerate to %" PRIu64 "Hz (divider %u)",
55eca716 656 devc->cur_samplerate, devc->cur_samplerate_divider);
657 if (ols_send_longdata(serial, CMD_SET_DIVIDER,
658 devc->cur_samplerate_divider & 0x00FFFFFF) != SR_OK)
f8fd8420 659 return SR_ERR;
660
661 /* Send sample limit and pre/post-trigger capture ratio. */
662 sr_dbg("Setting sample limit %d, trigger point at %d",
55eca716 663 (readcount - 1) * 4, (delaycount - 1) * 4);
f8fd8420 664
665 if (devc->max_samples > 256 * 1024) {
55eca716 666 if (ols_send_longdata(serial, CMD_CAPTURE_READCOUNT,
667 readcount - 1) != SR_OK)
f8fd8420 668 return SR_ERR;
55eca716 669 if (ols_send_longdata(serial, CMD_CAPTURE_DELAYCOUNT,
670 delaycount - 1) != SR_OK)
f8fd8420 671 return SR_ERR;
672 } else {
3cc9e215 673 uint8_t arg[4];
55eca716 674 WL16(&arg[0], readcount - 1);
675 WL16(&arg[2], delaycount - 1);
f8fd8420 676 if (send_longcommand(serial, CMD_CAPTURE_SIZE, arg) != SR_OK)
677 return SR_ERR;
678 }
679
680 /* Flag register. */
55eca716 681 sr_dbg("Setting intpat %s, extpat %s, RLE %s, noise_filter %s, demux %s, "
682 "%s clock%s",
683 devc->capture_flags & CAPTURE_FLAG_INTERNAL_TEST_MODE ? "on" :
684 "off",
685 devc->capture_flags & CAPTURE_FLAG_EXTERNAL_TEST_MODE ? "on" :
686 "off",
687 devc->capture_flags & CAPTURE_FLAG_RLE ? "on" : "off",
688 devc->capture_flags & CAPTURE_FLAG_NOISE_FILTER ? "on" : "off",
689 devc->capture_flags & CAPTURE_FLAG_DEMUX ? "on" : "off",
690 devc->capture_flags & CAPTURE_FLAG_CLOCK_EXTERNAL ? "external" :
691 "internal",
692 devc->capture_flags & CAPTURE_FLAG_CLOCK_EXTERNAL ?
693 (devc->capture_flags & CAPTURE_FLAG_INVERT_EXT_CLOCK ?
694 " on falling edge" :
695 "on rising edge") :
696 "");
f8fd8420 697
698 /*
699 * Enable/disable OLS channel groups in the flag register according
700 * to the channel mask. 1 means "disable channel".
701 */
702 devc->capture_flags &= ~0x3c;
703 devc->capture_flags |= ~(changroup_mask << 2) & 0x3c;
704
705 /* RLE mode is always zero, for now. */
706
3cc9e215 707 if (ols_send_longdata(serial, CMD_SET_FLAGS, devc->capture_flags) != SR_OK)
f8fd8420 708 return SR_ERR;
709
710 return SR_OK;
711}