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 SR_PRIV struct dev_context *ols_dev_new(void)
144 struct dev_context *devc;
146 devc = g_malloc0(sizeof(struct dev_context));
147 devc->trigger_at_smpl = OLS_NO_TRIGGER;
152 static void ols_channel_new(struct sr_dev_inst *sdi, int num_chan)
154 struct dev_context *devc = sdi->priv;
157 for (i = 0; i < num_chan; i++)
158 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
159 ols_channel_names[i]);
161 devc->max_channels = num_chan;
164 static void ols_metadata_quirks(struct sr_dev_inst *sdi)
166 struct dev_context *devc;
175 is_shrimp = sdi->model && strcmp(sdi->model, "Shrimp1.0") == 0;
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);
185 if (sdi->version && strstr(sdi->version, "FPGA version 3.07"))
186 devc->device_flags |= DEVICE_FLAG_IS_DEMON_CORE;
189 SR_PRIV struct sr_dev_inst *get_metadata(struct sr_serial_dev_inst *serial)
191 struct sr_dev_inst *sdi;
192 struct dev_context *devc;
196 GString *tmp_str, *devname, *version;
199 sdi = g_malloc0(sizeof(struct sr_dev_inst));
200 sdi->status = SR_ST_INACTIVE;
201 devc = ols_dev_new();
204 devname = g_string_new("");
205 version = g_string_new("");
209 delay_ms = serial_timeout(serial, 1);
210 if (serial_read_blocking(serial, &key, 1, delay_ms) != 1)
212 if (key == METADATA_TOKEN_END) {
213 sr_dbg("Got metadata key 0x00, metadata ends.");
219 /* NULL-terminated string */
220 tmp_str = g_string_new("");
221 delay_ms = serial_timeout(serial, 1);
222 while (serial_read_blocking(serial, &tmp_c, 1,
225 g_string_append_c(tmp_str, tmp_c);
226 sr_dbg("Got metadata token 0x%.2x value '%s'.", key,
229 case METADATA_TOKEN_DEVICE_NAME:
232 g_string_append(devname, tmp_str->str);
234 case METADATA_TOKEN_FPGA_VERSION:
235 /* FPGA firmware version */
237 g_string_append(version, ", ");
238 g_string_append(version, "FPGA version ");
239 g_string_append(version, tmp_str->str);
241 case METADATA_TOKEN_ANCILLARY_VERSION:
242 /* Ancillary version */
244 g_string_append(version, ", ");
245 g_string_append(version, "Ancillary version ");
246 g_string_append(version, tmp_str->str);
249 sr_info("ols: unknown token 0x%.2x: '%s'", key,
253 g_string_free(tmp_str, TRUE);
256 /* 32-bit unsigned integer */
257 delay_ms = serial_timeout(serial, 4);
258 if (serial_read_blocking(serial, &tmp_int, 4,
261 tmp_int = RB32(&tmp_int);
262 sr_dbg("Got metadata token 0x%.2x value 0x%.8x.", key,
265 case METADATA_TOKEN_NUM_PROBES_LONG:
266 /* Number of usable channels */
267 ols_channel_new(sdi, tmp_int);
269 case METADATA_TOKEN_SAMPLE_MEMORY_BYTES:
270 /* Amount of sample memory available (bytes) */
271 devc->max_samples = tmp_int;
273 case METADATA_TOKEN_DYNAMIC_MEMORY_BYTES:
274 /* Amount of dynamic memory available (bytes) */
275 /* what is this for? */
277 case METADATA_TOKEN_MAX_SAMPLE_RATE_HZ:
278 /* Maximum sample rate (Hz) */
279 devc->max_samplerate = tmp_int;
281 case METADATA_TOKEN_PROTOCOL_VERSION_LONG:
282 /* protocol version */
283 devc->protocol_version = tmp_int;
286 sr_info("Unknown token 0x%.2x: 0x%.8x.", key,
292 /* 8-bit unsigned integer */
293 delay_ms = serial_timeout(serial, 1);
294 if (serial_read_blocking(serial, &tmp_c, 1, delay_ms) != 1)
296 sr_dbg("Got metadata token 0x%.2x value 0x%.2x.", key,
299 case METADATA_TOKEN_NUM_PROBES_SHORT:
300 /* Number of usable channels */
301 ols_channel_new(sdi, tmp_c);
303 case METADATA_TOKEN_PROTOCOL_VERSION_SHORT:
304 /* protocol version */
305 devc->protocol_version = tmp_c;
308 sr_info("Unknown token 0x%.2x: 0x%.2x.", key,
319 sdi->model = devname->str;
320 sdi->version = version->str;
321 g_string_free(devname, FALSE);
322 g_string_free(version, FALSE);
324 /* Optionally amend received metadata, model specific quirks. */
325 ols_metadata_quirks(sdi);
330 SR_PRIV int ols_set_samplerate(const struct sr_dev_inst *sdi,
331 const uint64_t samplerate)
333 struct dev_context *devc;
336 if (devc->max_samplerate && samplerate > devc->max_samplerate)
337 return SR_ERR_SAMPLERATE;
339 if (samplerate > CLOCK_RATE) {
340 sr_info("Enabling demux mode.");
341 devc->capture_flags |= CAPTURE_FLAG_DEMUX;
342 devc->capture_flags &= ~CAPTURE_FLAG_NOISE_FILTER;
343 devc->cur_samplerate_divider =
344 (CLOCK_RATE * 2 / samplerate) - 1;
346 sr_info("Disabling demux mode.");
347 devc->capture_flags &= ~CAPTURE_FLAG_DEMUX;
348 devc->capture_flags |= CAPTURE_FLAG_NOISE_FILTER;
349 devc->cur_samplerate_divider = (CLOCK_RATE / samplerate) - 1;
352 /* Calculate actual samplerate used and complain if it is different
353 * from the requested.
355 devc->cur_samplerate = CLOCK_RATE / (devc->cur_samplerate_divider + 1);
356 if (devc->capture_flags & CAPTURE_FLAG_DEMUX)
357 devc->cur_samplerate *= 2;
358 if (devc->cur_samplerate != samplerate)
359 sr_info("Can't match samplerate %" PRIu64 ", using %" PRIu64
361 samplerate, devc->cur_samplerate);
366 SR_PRIV void abort_acquisition(const struct sr_dev_inst *sdi)
368 struct sr_serial_dev_inst *serial;
371 ols_send_reset(serial);
373 serial_source_remove(sdi->session, serial);
375 std_session_send_df_end(sdi);
378 SR_PRIV int ols_receive_data(int fd, int revents, void *cb_data)
380 struct dev_context *devc;
381 struct sr_dev_inst *sdi;
382 struct sr_serial_dev_inst *serial;
383 struct sr_datafeed_packet packet;
384 struct sr_datafeed_logic logic;
386 int num_changroups, offset, j;
396 if (devc->num_transfers == 0 && revents == 0) {
397 /* Ignore timeouts as long as we haven't received anything */
401 if (devc->num_transfers++ == 0) {
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.");
407 /* fill with 1010... for debugging */
408 memset(devc->raw_sample_buf, 0x82, devc->limit_samples * 4);
412 for (i = 0x20; i > 0x02; i >>= 1) {
413 if ((devc->capture_flags & i) == 0) {
418 if (revents == G_IO_IN && devc->num_samples < devc->limit_samples) {
419 if (serial_read_nonblocking(serial, &byte, 1) != 1)
423 /* Ignore it if we've read enough. */
424 if (devc->num_samples >= devc->limit_samples)
427 devc->sample[devc->num_bytes++] = byte;
428 sr_spew("Received byte 0x%.2x.", byte);
429 if (devc->num_bytes == num_changroups) {
431 devc->cnt_samples_rle++;
433 * Got a full sample. Convert from the OLS's little-endian
434 * sample to the local format.
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,
441 if (devc->capture_flags & CAPTURE_FLAG_RLE) {
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.
447 if (devc->sample[devc->num_bytes - 1] & 0x80) {
448 /* Clear the high bit. */
449 sample &= ~(0x80 << (devc->num_bytes -
451 devc->rle_count = sample;
452 devc->cnt_samples_rle +=
454 sr_dbg("RLE count: %u.",
460 devc->num_samples += devc->rle_count + 1;
461 if (devc->num_samples > devc->limit_samples) {
462 /* Save us from overrunning the buffer. */
464 devc->num_samples - devc->limit_samples;
465 devc->num_samples = devc->limit_samples;
468 if (num_changroups < 4) {
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
476 * the number of channels.
479 uint8_t tmp_sample[4] = { 0, 0, 0, 0 };
480 for (i = 0; i < 4; i++) {
481 if (((devc->capture_flags >> 2) &
484 * This channel group was
485 * enabled, copy from received
490 } else if (devc->capture_flags &
491 CAPTURE_FLAG_DEMUX &&
493 /* group 2 & 3 get added to 0 & 1 */
498 memcpy(devc->sample, tmp_sample, 4);
499 sr_spew("Expanded sample: 0x%.2hhx%.2hhx%.2hhx%.2hhx ",
500 devc->sample[3], devc->sample[2],
501 devc->sample[1], devc->sample[0]);
505 * the OLS sends its sample buffer backwards.
506 * store it in reverse order here, so we can dump
507 * this on the session bus later.
509 offset = (devc->limit_samples - devc->num_samples) * 4;
510 for (i = 0; i <= devc->rle_count; i++) {
511 memcpy(devc->raw_sample_buf + offset + (i * 4),
514 memset(devc->sample, 0, 4);
520 * This is the main loop telling us a timeout was reached, or
521 * we've acquired all the samples we asked for -- we're done.
522 * Send the (properly-ordered) buffer to the frontend.
524 sr_dbg("Received %d bytes, %d samples, %d decompressed samples.",
525 devc->cnt_bytes, devc->cnt_samples,
526 devc->cnt_samples_rle);
527 if (devc->trigger_at_smpl != OLS_NO_TRIGGER) {
529 * A trigger was set up, so we need to tell the frontend
532 if (devc->trigger_at_smpl > 0) {
533 /* There are pre-trigger samples, send those first. */
534 packet.type = SR_DF_LOGIC;
535 packet.payload = &logic;
536 logic.length = devc->trigger_at_smpl * 4;
538 logic.data = devc->raw_sample_buf +
539 (devc->limit_samples -
542 sr_session_send(sdi, &packet);
545 /* Send the trigger. */
546 std_session_send_df_trigger(sdi);
549 /* Send post-trigger / all captured samples. */
550 int num_pre_trigger_samples = devc->trigger_at_smpl ==
553 devc->trigger_at_smpl;
554 packet.type = SR_DF_LOGIC;
555 packet.payload = &logic;
557 (devc->num_samples - num_pre_trigger_samples) * 4;
559 logic.data = devc->raw_sample_buf +
560 (num_pre_trigger_samples + devc->limit_samples -
563 sr_session_send(sdi, &packet);
565 g_free(devc->raw_sample_buf);
567 serial_flush(serial);
568 abort_acquisition(sdi);
575 ols_set_basic_trigger_stage(const struct ols_basic_trigger_desc *trigger_desc,
576 struct sr_serial_dev_inst *serial, int stage)
580 cmd = CMD_SET_BASIC_TRIGGER_MASK0 + stage * 4;
581 if (ols_send_longdata(serial, cmd, trigger_desc->trigger_mask[stage]) != SR_OK)
584 cmd = CMD_SET_BASIC_TRIGGER_VALUE0 + stage * 4;
585 if (ols_send_longdata(serial, cmd, trigger_desc->trigger_value[stage]) != SR_OK)
588 cmd = CMD_SET_BASIC_TRIGGER_CONFIG0 + stage * 4;
589 arg[0] = arg[1] = arg[3] = 0x00;
591 if (stage == trigger_desc->num_stages)
592 /* Last stage, fire when this one matches. */
593 arg[3] |= TRIGGER_START;
594 if (send_longcommand(serial, cmd, arg) != SR_OK)
600 SR_PRIV int ols_prepare_acquisition(const struct sr_dev_inst *sdi)
604 struct dev_context *devc = sdi->priv;
605 struct sr_serial_dev_inst *serial = sdi->conn;
607 int num_changroups = 0;
608 uint8_t changroup_mask = 0;
609 uint32_t channel_mask = ols_channel_mask(sdi);
610 for (unsigned int i = 0; i < 4; i++) {
611 if (channel_mask & (0xff << (i * 8))) {
612 changroup_mask |= (1 << i);
618 * Limit readcount to prevent reading past the end of the hardware
619 * buffer. Rather read too many samples than too few.
621 uint32_t samplecount =
622 MIN(devc->max_samples / num_changroups, devc->limit_samples);
623 uint32_t readcount = (samplecount + 3) / 4;
626 /* Basic triggers. */
627 struct ols_basic_trigger_desc basic_trigger_desc;
628 if (convert_trigger(sdi, &basic_trigger_desc) != SR_OK) {
629 sr_err("Failed to configure channels.");
632 if (basic_trigger_desc.num_stages > 0) {
634 * According to http://mygizmos.org/ols/Logic-Sniffer-FPGA-Spec.pdf
635 * reset command must be send prior each arm command
637 sr_dbg("Send reset command before trigger configure");
638 if (ols_send_reset(serial) != SR_OK)
641 delaycount = readcount * (1 - devc->capture_ratio / 100.0);
642 devc->trigger_at_smpl = (readcount - delaycount) * 4 -
643 basic_trigger_desc.num_stages;
644 for (int i = 0; i <= basic_trigger_desc.num_stages; i++) {
645 sr_dbg("Setting OLS stage %d trigger.", i);
646 if ((ret = ols_set_basic_trigger_stage(
647 &basic_trigger_desc, serial, i)) != SR_OK)
651 /* No triggers configured, force trigger on first stage. */
652 sr_dbg("Forcing trigger at stage 0.");
653 if ((ret = ols_set_basic_trigger_stage(&basic_trigger_desc,
654 serial, 0)) != SR_OK)
656 delaycount = readcount;
660 sr_dbg("Setting samplerate to %" PRIu64 "Hz (divider %u)",
661 devc->cur_samplerate, devc->cur_samplerate_divider);
662 if (ols_send_longdata(serial, CMD_SET_DIVIDER,
663 devc->cur_samplerate_divider & 0x00FFFFFF) != SR_OK)
666 /* Send sample limit and pre/post-trigger capture ratio. */
667 sr_dbg("Setting sample limit %d, trigger point at %d",
668 (readcount - 1) * 4, (delaycount - 1) * 4);
670 if (devc->max_samples > 256 * 1024) {
671 if (ols_send_longdata(serial, CMD_CAPTURE_READCOUNT,
672 readcount - 1) != SR_OK)
674 if (ols_send_longdata(serial, CMD_CAPTURE_DELAYCOUNT,
675 delaycount - 1) != SR_OK)
679 WL16(&arg[0], readcount - 1);
680 WL16(&arg[2], delaycount - 1);
681 if (send_longcommand(serial, CMD_CAPTURE_SIZE, arg) != SR_OK)
686 sr_dbg("Setting intpat %s, extpat %s, RLE %s, noise_filter %s, demux %s, "
688 devc->capture_flags & CAPTURE_FLAG_INTERNAL_TEST_MODE ? "on" :
690 devc->capture_flags & CAPTURE_FLAG_EXTERNAL_TEST_MODE ? "on" :
692 devc->capture_flags & CAPTURE_FLAG_RLE ? "on" : "off",
693 devc->capture_flags & CAPTURE_FLAG_NOISE_FILTER ? "on" : "off",
694 devc->capture_flags & CAPTURE_FLAG_DEMUX ? "on" : "off",
695 devc->capture_flags & CAPTURE_FLAG_CLOCK_EXTERNAL ? "external" :
697 devc->capture_flags & CAPTURE_FLAG_CLOCK_EXTERNAL ?
698 (devc->capture_flags & CAPTURE_FLAG_INVERT_EXT_CLOCK ?
704 * Enable/disable OLS channel groups in the flag register according
705 * to the channel mask. 1 means "disable channel".
707 devc->capture_flags &= ~0x3c;
708 devc->capture_flags |= ~(changroup_mask << 2) & 0x3c;
710 /* RLE mode is always zero, for now. */
712 if (ols_send_longdata(serial, CMD_SET_FLAGS, devc->capture_flags) != SR_OK)