2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
23 struct ols_basic_trigger_desc {
24 uint32_t trigger_mask[NUM_BASIC_TRIGGER_STAGES];
25 uint32_t trigger_value[NUM_BASIC_TRIGGER_STAGES];
29 SR_PRIV int send_shortcommand(struct sr_serial_dev_inst *serial,
34 sr_dbg("Sending cmd 0x%.2x.", command);
36 if (serial_write_blocking(serial, buf, 1, serial_timeout(serial, 1)) != 1)
39 if (serial_drain(serial) != SR_OK)
45 SR_PRIV int send_longcommand(struct sr_serial_dev_inst *serial, uint8_t command,
50 sr_dbg("Sending cmd 0x%.2x data 0x%.2x%.2x%.2x%.2x.", command, data[0],
51 data[1], data[2], data[3]);
57 if (serial_write_blocking(serial, buf, 5, serial_timeout(serial, 1)) != 5)
60 if (serial_drain(serial) != SR_OK)
66 static int ols_send_longdata(struct sr_serial_dev_inst *serial, uint8_t command,
71 return send_longcommand(serial, command, data);
74 SR_PRIV int ols_send_reset(struct sr_serial_dev_inst *serial)
78 for (i = 0; i < 5; i++) {
79 if (send_shortcommand(serial, CMD_RESET) != SR_OK)
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)
89 uint32_t channel_mask = 0;
90 for (const GSList *l = sdi->channels; l; l = l->next) {
91 struct sr_channel *channel = l->data;
93 channel_mask |= 1 << channel->index;
99 static int convert_trigger(const struct sr_dev_inst *sdi,
100 struct ols_basic_trigger_desc *ols_trigger)
102 struct sr_trigger *trigger;
103 struct sr_trigger_stage *stage;
104 struct sr_trigger_match *match;
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;
114 if (!(trigger = sr_session_trigger_get(sdi->session)))
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);
124 for (l = trigger->stages; l; l = l->next) {
126 for (m = stage->matches; m; m = m->next) {
128 if (!match->channel->enabled)
129 /* Ignore disabled channels with a trigger. */
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;
142 static void ols_channel_new(struct sr_dev_inst *sdi, int num_chan)
144 struct dev_context *devc = sdi->priv;
147 for (i = 0; i < num_chan; i++)
148 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
149 ols_channel_names[i]);
151 devc->max_channels = num_chan;
154 static void ols_metadata_quirks(struct sr_dev_inst *sdi)
156 struct dev_context *devc;
165 is_shrimp = sdi->model && strcmp(sdi->model, "Shrimp1.0") == 0;
167 if (!devc->max_channels)
168 ols_channel_new(sdi, 4);
169 if (!devc->max_samples)
170 devc->max_samples = 256 * 1024;
171 if (!devc->max_samplerate)
172 devc->max_samplerate = SR_MHZ(20);
175 if (sdi->version && strstr(sdi->version, "FPGA version 3.07"))
176 devc->device_flags |= DEVICE_FLAG_IS_DEMON_CORE;
179 SR_PRIV int ols_get_metadata(struct sr_dev_inst *sdi)
181 struct sr_serial_dev_inst *serial;
182 struct dev_context *devc;
186 GString *tmp_str, *devname, *version;
192 devname = g_string_new("");
193 version = g_string_new("");
197 delay_ms = serial_timeout(serial, 1);
198 if (serial_read_blocking(serial, &key, 1, delay_ms) != 1)
200 if (key == METADATA_TOKEN_END) {
201 sr_dbg("Got metadata key 0x00, metadata ends.");
207 /* NULL-terminated string */
208 tmp_str = g_string_new("");
209 delay_ms = serial_timeout(serial, 1);
210 while (serial_read_blocking(serial, &tmp_c, 1,
213 g_string_append_c(tmp_str, tmp_c);
214 sr_dbg("Got metadata token 0x%.2x value '%s'.", key,
217 case METADATA_TOKEN_DEVICE_NAME:
220 g_string_append(devname, tmp_str->str);
222 case METADATA_TOKEN_FPGA_VERSION:
223 /* FPGA firmware version */
225 g_string_append(version, ", ");
226 g_string_append(version, "FPGA version ");
227 g_string_append(version, tmp_str->str);
229 case METADATA_TOKEN_ANCILLARY_VERSION:
230 /* Ancillary version */
232 g_string_append(version, ", ");
233 g_string_append(version, "Ancillary version ");
234 g_string_append(version, tmp_str->str);
237 sr_info("ols: unknown token 0x%.2x: '%s'", key,
241 g_string_free(tmp_str, TRUE);
244 /* 32-bit unsigned integer */
245 delay_ms = serial_timeout(serial, 4);
246 if (serial_read_blocking(serial, &tmp_int, 4,
249 tmp_int = RB32(&tmp_int);
250 sr_dbg("Got metadata token 0x%.2x value 0x%.8x.", key,
253 case METADATA_TOKEN_NUM_PROBES_LONG:
254 /* Number of usable channels */
255 ols_channel_new(sdi, tmp_int);
257 case METADATA_TOKEN_SAMPLE_MEMORY_BYTES:
258 /* Amount of sample memory available (bytes) */
259 devc->max_samples = tmp_int;
261 case METADATA_TOKEN_DYNAMIC_MEMORY_BYTES:
262 /* Amount of dynamic memory available (bytes) */
263 /* what is this for? */
265 case METADATA_TOKEN_MAX_SAMPLE_RATE_HZ:
266 /* Maximum sample rate (Hz) */
267 devc->max_samplerate = tmp_int;
269 case METADATA_TOKEN_PROTOCOL_VERSION_LONG:
270 /* protocol version */
271 devc->protocol_version = tmp_int;
274 sr_info("Unknown token 0x%.2x: 0x%.8x.", key,
280 /* 8-bit unsigned integer */
281 delay_ms = serial_timeout(serial, 1);
282 if (serial_read_blocking(serial, &tmp_c, 1, delay_ms) != 1)
284 sr_dbg("Got metadata token 0x%.2x value 0x%.2x.", key,
287 case METADATA_TOKEN_NUM_PROBES_SHORT:
288 /* Number of usable channels */
289 ols_channel_new(sdi, tmp_c);
291 case METADATA_TOKEN_PROTOCOL_VERSION_SHORT:
292 /* protocol version */
293 devc->protocol_version = tmp_c;
296 sr_info("Unknown token 0x%.2x: 0x%.2x.", key,
307 sdi->model = devname->str;
308 sdi->version = version->str;
309 g_string_free(devname, FALSE);
310 g_string_free(version, FALSE);
312 /* Optionally amend received metadata, model specific quirks. */
313 ols_metadata_quirks(sdi);
318 SR_PRIV int ols_set_samplerate(const struct sr_dev_inst *sdi,
319 const uint64_t samplerate)
321 struct dev_context *devc;
324 if (devc->max_samplerate && samplerate > devc->max_samplerate)
325 return SR_ERR_SAMPLERATE;
327 if (samplerate > CLOCK_RATE) {
328 sr_info("Enabling demux mode.");
329 devc->capture_flags |= CAPTURE_FLAG_DEMUX;
330 devc->capture_flags &= ~CAPTURE_FLAG_NOISE_FILTER;
331 devc->cur_samplerate_divider =
332 (CLOCK_RATE * 2 / samplerate) - 1;
334 sr_info("Disabling demux mode.");
335 devc->capture_flags &= ~CAPTURE_FLAG_DEMUX;
336 devc->capture_flags |= CAPTURE_FLAG_NOISE_FILTER;
337 devc->cur_samplerate_divider = (CLOCK_RATE / samplerate) - 1;
340 /* Calculate actual samplerate used and complain if it is different
341 * from the requested.
343 devc->cur_samplerate = CLOCK_RATE / (devc->cur_samplerate_divider + 1);
344 if (devc->capture_flags & CAPTURE_FLAG_DEMUX)
345 devc->cur_samplerate *= 2;
346 if (devc->cur_samplerate != samplerate)
347 sr_info("Can't match samplerate %" PRIu64 ", using %" PRIu64
349 samplerate, devc->cur_samplerate);
354 SR_PRIV void abort_acquisition(const struct sr_dev_inst *sdi)
356 struct sr_serial_dev_inst *serial;
359 ols_send_reset(serial);
361 serial_source_remove(sdi->session, serial);
363 std_session_send_df_end(sdi);
366 SR_PRIV int ols_receive_data(int fd, int revents, void *cb_data)
368 struct dev_context *devc;
369 struct sr_dev_inst *sdi;
370 struct sr_serial_dev_inst *serial;
371 struct sr_datafeed_packet packet;
372 struct sr_datafeed_logic logic;
374 int num_changroups, offset, j;
384 if (devc->num_transfers == 0 && revents == 0) {
385 /* Ignore timeouts as long as we haven't received anything */
389 if (devc->num_transfers++ == 0) {
390 devc->raw_sample_buf = g_try_malloc(devc->limit_samples * 4);
391 if (!devc->raw_sample_buf) {
392 sr_err("Sample buffer malloc failed.");
395 /* fill with 1010... for debugging */
396 memset(devc->raw_sample_buf, 0x82, devc->limit_samples * 4);
400 for (i = 0x20; i > 0x02; i >>= 1) {
401 if ((devc->capture_flags & i) == 0) {
406 if (revents == G_IO_IN && devc->num_samples < devc->limit_samples) {
407 if (serial_read_nonblocking(serial, &byte, 1) != 1)
411 /* Ignore it if we've read enough. */
412 if (devc->num_samples >= devc->limit_samples)
415 devc->sample[devc->num_bytes++] = byte;
416 sr_spew("Received byte 0x%.2x.", byte);
417 if (devc->num_bytes == num_changroups) {
419 devc->cnt_samples_rle++;
421 * Got a full sample. Convert from the OLS's little-endian
422 * sample to the local format.
424 sample = devc->sample[0] | (devc->sample[1] << 8) |
425 (devc->sample[2] << 16) |
426 (devc->sample[3] << 24);
427 sr_dbg("Received sample 0x%.*x.", devc->num_bytes * 2,
429 if (devc->capture_flags & CAPTURE_FLAG_RLE) {
431 * In RLE mode the high bit of the sample is the
432 * "count" flag, meaning this sample is the number
433 * of times the previous sample occurred.
435 if (devc->sample[devc->num_bytes - 1] & 0x80) {
436 /* Clear the high bit. */
437 sample &= ~(0x80 << (devc->num_bytes -
439 devc->rle_count = sample;
440 devc->cnt_samples_rle +=
442 sr_dbg("RLE count: %u.",
448 devc->num_samples += devc->rle_count + 1;
449 if (devc->num_samples > devc->limit_samples) {
450 /* Save us from overrunning the buffer. */
452 devc->num_samples - devc->limit_samples;
453 devc->num_samples = devc->limit_samples;
456 if (num_changroups < 4) {
458 * Some channel groups may have been turned
459 * off, to speed up transfer between the
460 * hardware and the PC. Expand that here before
461 * submitting it over the session bus --
462 * whatever is listening on the bus will be
463 * expecting a full 32-bit sample, based on
464 * the number of channels.
467 uint8_t tmp_sample[4] = { 0, 0, 0, 0 };
468 for (i = 0; i < 4; i++) {
469 if (((devc->capture_flags >> 2) &
472 * This channel group was
473 * enabled, copy from received
480 memcpy(devc->sample, tmp_sample, 4);
481 sr_spew("Expanded sample: 0x%.2hhx%.2hhx%.2hhx%.2hhx ",
482 devc->sample[3], devc->sample[2],
483 devc->sample[1], devc->sample[0]);
487 * the OLS sends its sample buffer backwards.
488 * store it in reverse order here, so we can dump
489 * this on the session bus later.
491 offset = (devc->limit_samples - devc->num_samples) * 4;
492 for (i = 0; i <= devc->rle_count; i++) {
493 memcpy(devc->raw_sample_buf + offset + (i * 4),
496 memset(devc->sample, 0, 4);
502 * This is the main loop telling us a timeout was reached, or
503 * we've acquired all the samples we asked for -- we're done.
504 * Send the (properly-ordered) buffer to the frontend.
506 sr_dbg("Received %d bytes, %d samples, %d decompressed samples.",
507 devc->cnt_bytes, devc->cnt_samples,
508 devc->cnt_samples_rle);
509 if (devc->trigger_at_smpl != OLS_NO_TRIGGER) {
511 * A trigger was set up, so we need to tell the frontend
514 if (devc->trigger_at_smpl > 0) {
515 /* There are pre-trigger samples, send those first. */
516 packet.type = SR_DF_LOGIC;
517 packet.payload = &logic;
518 logic.length = devc->trigger_at_smpl * 4;
520 logic.data = devc->raw_sample_buf +
521 (devc->limit_samples -
524 sr_session_send(sdi, &packet);
527 /* Send the trigger. */
528 std_session_send_df_trigger(sdi);
531 /* Send post-trigger / all captured samples. */
532 int num_pre_trigger_samples = devc->trigger_at_smpl ==
535 devc->trigger_at_smpl;
536 packet.type = SR_DF_LOGIC;
537 packet.payload = &logic;
539 (devc->num_samples - num_pre_trigger_samples) * 4;
541 logic.data = devc->raw_sample_buf +
542 (num_pre_trigger_samples + devc->limit_samples -
545 sr_session_send(sdi, &packet);
547 g_free(devc->raw_sample_buf);
549 serial_flush(serial);
550 abort_acquisition(sdi);
557 ols_set_basic_trigger_stage(const struct ols_basic_trigger_desc *trigger_desc,
558 struct sr_serial_dev_inst *serial, int stage)
562 cmd = CMD_SET_BASIC_TRIGGER_MASK0 + stage * 4;
563 if (ols_send_longdata(serial, cmd, trigger_desc->trigger_mask[stage]) != SR_OK)
566 cmd = CMD_SET_BASIC_TRIGGER_VALUE0 + stage * 4;
567 if (ols_send_longdata(serial, cmd, trigger_desc->trigger_value[stage]) != SR_OK)
570 cmd = CMD_SET_BASIC_TRIGGER_CONFIG0 + stage * 4;
571 arg[0] = arg[1] = arg[3] = 0x00;
573 if (stage == trigger_desc->num_stages - 1)
574 /* Last stage, fire when this one matches. */
575 arg[3] |= TRIGGER_START;
576 if (send_longcommand(serial, cmd, arg) != SR_OK)
582 SR_PRIV int ols_prepare_acquisition(const struct sr_dev_inst *sdi)
586 struct dev_context *devc = sdi->priv;
587 struct sr_serial_dev_inst *serial = sdi->conn;
589 int num_changroups = 0;
590 uint8_t changroup_mask = 0;
591 uint32_t channel_mask = ols_channel_mask(sdi);
592 for (unsigned int i = 0; i < 4; i++) {
593 if (channel_mask & (0xff << (i * 8))) {
594 changroup_mask |= (1 << i);
600 * Limit readcount to prevent reading past the end of the hardware
601 * buffer. Rather read too many samples than too few.
603 uint32_t samplecount =
604 MIN(devc->max_samples / num_changroups, devc->limit_samples);
605 uint32_t readcount = (samplecount + 3) / 4;
608 /* Basic triggers. */
609 struct ols_basic_trigger_desc basic_trigger_desc;
610 if (convert_trigger(sdi, &basic_trigger_desc) != SR_OK) {
611 sr_err("Failed to configure channels.");
614 if (basic_trigger_desc.num_stages > 0) {
616 * According to http://mygizmos.org/ols/Logic-Sniffer-FPGA-Spec.pdf
617 * reset command must be send prior each arm command
619 sr_dbg("Send reset command before trigger configure");
620 if (ols_send_reset(serial) != SR_OK)
623 delaycount = readcount * (1 - devc->capture_ratio / 100.0);
624 devc->trigger_at_smpl = (readcount - delaycount) * 4 -
625 basic_trigger_desc.num_stages;
626 for (int i = 0; i < basic_trigger_desc.num_stages; i++) {
627 sr_dbg("Setting OLS stage %d trigger.", i);
628 if ((ret = ols_set_basic_trigger_stage(
629 &basic_trigger_desc, serial, i)) != SR_OK)
633 /* No triggers configured, force trigger on first stage. */
634 sr_dbg("Forcing trigger at stage 0.");
635 basic_trigger_desc.num_stages = 1;
636 if ((ret = ols_set_basic_trigger_stage(&basic_trigger_desc,
637 serial, 0)) != SR_OK)
639 delaycount = readcount;
643 sr_dbg("Setting samplerate to %" PRIu64 "Hz (divider %u)",
644 devc->cur_samplerate, devc->cur_samplerate_divider);
645 if (ols_send_longdata(serial, CMD_SET_DIVIDER,
646 devc->cur_samplerate_divider & 0x00FFFFFF) != SR_OK)
649 /* Send sample limit and pre/post-trigger capture ratio. */
650 sr_dbg("Setting sample limit %d, trigger point at %d",
651 (readcount - 1) * 4, (delaycount - 1) * 4);
653 if (devc->max_samples > 256 * 1024) {
654 if (ols_send_longdata(serial, CMD_CAPTURE_READCOUNT,
655 readcount - 1) != SR_OK)
657 if (ols_send_longdata(serial, CMD_CAPTURE_DELAYCOUNT,
658 delaycount - 1) != SR_OK)
662 WL16(&arg[0], readcount - 1);
663 WL16(&arg[2], delaycount - 1);
664 if (send_longcommand(serial, CMD_CAPTURE_SIZE, arg) != SR_OK)
669 sr_dbg("Setting intpat %s, extpat %s, RLE %s, noise_filter %s, demux %s, "
671 devc->capture_flags & CAPTURE_FLAG_INTERNAL_TEST_MODE ? "on" :
673 devc->capture_flags & CAPTURE_FLAG_EXTERNAL_TEST_MODE ? "on" :
675 devc->capture_flags & CAPTURE_FLAG_RLE ? "on" : "off",
676 devc->capture_flags & CAPTURE_FLAG_NOISE_FILTER ? "on" : "off",
677 devc->capture_flags & CAPTURE_FLAG_DEMUX ? "on" : "off",
678 devc->capture_flags & CAPTURE_FLAG_CLOCK_EXTERNAL ? "external" :
680 devc->capture_flags & CAPTURE_FLAG_CLOCK_EXTERNAL ?
681 (devc->capture_flags & CAPTURE_FLAG_INVERT_EXT_CLOCK ?
687 * Enable/disable OLS channel groups in the flag register according
688 * to the channel mask. 1 means "disable channel".
690 devc->capture_flags &= ~0x3c;
691 devc->capture_flags |= ~(changroup_mask << 2) & 0x3c;
693 /* RLE mode is always zero, for now. */
695 if (ols_send_longdata(serial, CMD_SET_FLAGS, devc->capture_flags) != SR_OK)