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