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,
46 uint8_t command, uint8_t *data)
50 sr_dbg("Sending cmd 0x%.2x data 0x%.2x%.2x%.2x%.2x.", command,
51 data[0], 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,
67 uint8_t command, uint32_t value)
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, struct ols_basic_trigger_desc *ols_trigger)
101 struct sr_trigger *trigger;
102 struct sr_trigger_stage *stage;
103 struct sr_trigger_match *match;
107 ols_trigger->num_stages = 0;
108 for (i = 0; i < NUM_BASIC_TRIGGER_STAGES; i++) {
109 ols_trigger->trigger_mask[i] = 0;
110 ols_trigger->trigger_value[i] = 0;
113 if (!(trigger = sr_session_trigger_get(sdi->session)))
116 ols_trigger->num_stages = g_slist_length(trigger->stages);
117 if (ols_trigger->num_stages > NUM_BASIC_TRIGGER_STAGES) {
118 sr_err("This device only supports %d trigger stages.",
119 NUM_BASIC_TRIGGER_STAGES);
123 for (l = trigger->stages; l; l = l->next) {
125 for (m = stage->matches; m; m = m->next) {
127 if (!match->channel->enabled)
128 /* Ignore disabled channels with a trigger. */
130 ols_trigger->trigger_mask[stage->stage] |= 1 << match->channel->index;
131 if (match->match == SR_TRIGGER_ONE)
132 ols_trigger->trigger_value[stage->stage] |= 1 << match->channel->index;
139 SR_PRIV struct dev_context *ols_dev_new(void)
141 struct dev_context *devc;
143 devc = g_malloc0(sizeof(struct dev_context));
144 devc->trigger_at_smpl = OLS_NO_TRIGGER;
149 static void ols_channel_new(struct sr_dev_inst *sdi, int num_chan)
151 struct dev_context *devc = sdi->priv;
154 for (i = 0; i < num_chan; i++)
155 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
156 ols_channel_names[i]);
158 devc->max_channels = num_chan;
161 static void ols_metadata_quirks(struct sr_dev_inst *sdi)
163 struct dev_context *devc;
172 is_shrimp = sdi->model && strcmp(sdi->model, "Shrimp1.0") == 0;
174 if (!devc->max_channels)
175 ols_channel_new(sdi, 4);
176 if (!devc->max_samples)
177 devc->max_samples = 256 * 1024;
178 if (!devc->max_samplerate)
179 devc->max_samplerate = SR_MHZ(20);
182 if (sdi->version && strstr(sdi->version, "FPGA version 3.07"))
183 devc->device_flags |= DEVICE_FLAG_IS_DEMON_CORE;
186 SR_PRIV struct sr_dev_inst *get_metadata(struct sr_serial_dev_inst *serial)
188 struct sr_dev_inst *sdi;
189 struct dev_context *devc;
193 GString *tmp_str, *devname, *version;
196 sdi = g_malloc0(sizeof(struct sr_dev_inst));
197 sdi->status = SR_ST_INACTIVE;
198 devc = ols_dev_new();
201 devname = g_string_new("");
202 version = g_string_new("");
206 delay_ms = serial_timeout(serial, 1);
207 if (serial_read_blocking(serial, &key, 1, delay_ms) != 1)
209 if (key == METADATA_TOKEN_END) {
210 sr_dbg("Got metadata key 0x00, metadata ends.");
216 /* NULL-terminated string */
217 tmp_str = g_string_new("");
218 delay_ms = serial_timeout(serial, 1);
219 while (serial_read_blocking(serial, &tmp_c, 1, delay_ms) == 1 && tmp_c != '\0')
220 g_string_append_c(tmp_str, tmp_c);
221 sr_dbg("Got metadata token 0x%.2x value '%s'.", key, tmp_str->str);
223 case METADATA_TOKEN_DEVICE_NAME:
225 devname = g_string_append(devname, tmp_str->str);
227 case METADATA_TOKEN_FPGA_VERSION:
228 /* FPGA firmware version */
230 g_string_append(version, ", ");
231 g_string_append(version, "FPGA version ");
232 g_string_append(version, tmp_str->str);
234 case METADATA_TOKEN_ANCILLARY_VERSION:
235 /* Ancillary version */
237 g_string_append(version, ", ");
238 g_string_append(version, "Ancillary version ");
239 g_string_append(version, tmp_str->str);
242 sr_info("ols: unknown token 0x%.2x: '%s'", key, tmp_str->str);
245 g_string_free(tmp_str, TRUE);
248 /* 32-bit unsigned integer */
249 delay_ms = serial_timeout(serial, 4);
250 if (serial_read_blocking(serial, &tmp_int, 4, delay_ms) != 4)
252 tmp_int = RB32(&tmp_int);
253 sr_dbg("Got metadata token 0x%.2x value 0x%.8x.", key, tmp_int);
255 case METADATA_TOKEN_NUM_PROBES_LONG:
256 /* Number of usable channels */
257 ols_channel_new(sdi, tmp_int);
259 case METADATA_TOKEN_SAMPLE_MEMORY_BYTES:
260 /* Amount of sample memory available (bytes) */
261 devc->max_samples = tmp_int;
263 case METADATA_TOKEN_DYNAMIC_MEMORY_BYTES:
264 /* Amount of dynamic memory available (bytes) */
265 /* what is this for? */
267 case METADATA_TOKEN_MAX_SAMPLE_RATE_HZ:
268 /* Maximum sample rate (Hz) */
269 devc->max_samplerate = tmp_int;
271 case METADATA_TOKEN_PROTOCOL_VERSION_LONG:
272 /* protocol version */
273 devc->protocol_version = tmp_int;
276 sr_info("Unknown token 0x%.2x: 0x%.8x.", key, tmp_int);
281 /* 8-bit unsigned integer */
282 delay_ms = serial_timeout(serial, 1);
283 if (serial_read_blocking(serial, &tmp_c, 1, delay_ms) != 1)
285 sr_dbg("Got metadata token 0x%.2x value 0x%.2x.", key, tmp_c);
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, tmp_c);
306 sdi->model = devname->str;
307 sdi->version = version->str;
308 g_string_free(devname, FALSE);
309 g_string_free(version, FALSE);
311 /* Optionally amend received metadata, model specific quirks. */
312 ols_metadata_quirks(sdi);
317 SR_PRIV int ols_set_samplerate(const struct sr_dev_inst *sdi,
318 const uint64_t samplerate)
320 struct dev_context *devc;
323 if (devc->max_samplerate && samplerate > devc->max_samplerate)
324 return SR_ERR_SAMPLERATE;
326 if (samplerate > CLOCK_RATE) {
327 sr_info("Enabling demux mode.");
328 devc->capture_flags |= CAPTURE_FLAG_DEMUX;
329 devc->capture_flags &= ~CAPTURE_FLAG_NOISE_FILTER;
330 devc->cur_samplerate_divider = (CLOCK_RATE * 2 / samplerate) - 1;
332 sr_info("Disabling demux mode.");
333 devc->capture_flags &= ~CAPTURE_FLAG_DEMUX;
334 devc->capture_flags |= CAPTURE_FLAG_NOISE_FILTER;
335 devc->cur_samplerate_divider = (CLOCK_RATE / samplerate) - 1;
338 /* Calculate actual samplerate used and complain if it is different
339 * from the requested.
341 devc->cur_samplerate = CLOCK_RATE / (devc->cur_samplerate_divider + 1);
342 if (devc->capture_flags & CAPTURE_FLAG_DEMUX)
343 devc->cur_samplerate *= 2;
344 if (devc->cur_samplerate != samplerate)
345 sr_info("Can't match samplerate %" PRIu64 ", using %"
346 PRIu64 ".", samplerate, devc->cur_samplerate);
351 SR_PRIV void abort_acquisition(const struct sr_dev_inst *sdi)
353 struct sr_serial_dev_inst *serial;
356 ols_send_reset(serial);
358 serial_source_remove(sdi->session, serial);
360 std_session_send_df_end(sdi);
363 SR_PRIV int ols_receive_data(int fd, int revents, void *cb_data)
365 struct dev_context *devc;
366 struct sr_dev_inst *sdi;
367 struct sr_serial_dev_inst *serial;
368 struct sr_datafeed_packet packet;
369 struct sr_datafeed_logic logic;
371 int num_changroups, offset, j;
381 if (devc->num_transfers == 0 && revents == 0) {
382 /* Ignore timeouts as long as we haven't received anything */
386 if (devc->num_transfers++ == 0) {
387 devc->raw_sample_buf = g_try_malloc(devc->limit_samples * 4);
388 if (!devc->raw_sample_buf) {
389 sr_err("Sample buffer malloc failed.");
392 /* fill with 1010... for debugging */
393 memset(devc->raw_sample_buf, 0x82, devc->limit_samples * 4);
397 for (i = 0x20; i > 0x02; i >>= 1) {
398 if ((devc->capture_flags & i) == 0) {
403 if (revents == G_IO_IN && devc->num_samples < devc->limit_samples) {
404 if (serial_read_nonblocking(serial, &byte, 1) != 1)
408 /* Ignore it if we've read enough. */
409 if (devc->num_samples >= devc->limit_samples)
412 devc->sample[devc->num_bytes++] = byte;
413 sr_spew("Received byte 0x%.2x.", byte);
414 if (devc->num_bytes == num_changroups) {
416 devc->cnt_samples_rle++;
418 * Got a full sample. Convert from the OLS's little-endian
419 * sample to the local format.
421 sample = devc->sample[0] | (devc->sample[1] << 8) \
422 | (devc->sample[2] << 16) | (devc->sample[3] << 24);
423 sr_dbg("Received sample 0x%.*x.", devc->num_bytes * 2, sample);
424 if (devc->capture_flags & CAPTURE_FLAG_RLE) {
426 * In RLE mode the high bit of the sample is the
427 * "count" flag, meaning this sample is the number
428 * of times the previous sample occurred.
430 if (devc->sample[devc->num_bytes - 1] & 0x80) {
431 /* Clear the high bit. */
432 sample &= ~(0x80 << (devc->num_bytes - 1) * 8);
433 devc->rle_count = sample;
434 devc->cnt_samples_rle += devc->rle_count;
435 sr_dbg("RLE count: %u.", devc->rle_count);
440 devc->num_samples += devc->rle_count + 1;
441 if (devc->num_samples > devc->limit_samples) {
442 /* Save us from overrunning the buffer. */
443 devc->rle_count -= devc->num_samples - devc->limit_samples;
444 devc->num_samples = devc->limit_samples;
447 if (num_changroups < 4) {
449 * Some channel groups may have been turned
450 * off, to speed up transfer between the
451 * hardware and the PC. Expand that here before
452 * submitting it over the session bus --
453 * whatever is listening on the bus will be
454 * expecting a full 32-bit sample, based on
455 * the number of channels.
458 memset(devc->tmp_sample, 0, 4);
459 for (i = 0; i < 4; i++) {
460 if (((devc->capture_flags >> 2) & (1 << i)) == 0) {
462 * This channel group was
463 * enabled, copy from received
466 devc->tmp_sample[i] = devc->sample[j++];
467 } else if (devc->capture_flags & CAPTURE_FLAG_DEMUX && (i > 2)) {
468 /* group 2 & 3 get added to 0 & 1 */
469 devc->tmp_sample[i - 2] = devc->sample[j++];
472 memcpy(devc->sample, devc->tmp_sample, 4);
473 sr_spew("Expanded sample: 0x%.2hhx%.2hhx%.2hhx%.2hhx ",
474 devc->sample[3], devc->sample[2], devc->sample[1], devc->sample[0]);
478 * the OLS sends its sample buffer backwards.
479 * store it in reverse order here, so we can dump
480 * this on the session bus later.
482 offset = (devc->limit_samples - devc->num_samples) * 4;
483 for (i = 0; i <= devc->rle_count; i++) {
484 memcpy(devc->raw_sample_buf + offset + (i * 4),
487 memset(devc->sample, 0, 4);
493 * This is the main loop telling us a timeout was reached, or
494 * we've acquired all the samples we asked for -- we're done.
495 * Send the (properly-ordered) buffer to the frontend.
497 sr_dbg("Received %d bytes, %d samples, %d decompressed samples.",
498 devc->cnt_bytes, devc->cnt_samples,
499 devc->cnt_samples_rle);
500 if (devc->trigger_at_smpl != OLS_NO_TRIGGER) {
502 * A trigger was set up, so we need to tell the frontend
505 if (devc->trigger_at_smpl > 0) {
506 /* There are pre-trigger samples, send those first. */
507 packet.type = SR_DF_LOGIC;
508 packet.payload = &logic;
509 logic.length = devc->trigger_at_smpl * 4;
511 logic.data = devc->raw_sample_buf +
512 (devc->limit_samples - devc->num_samples) * 4;
513 sr_session_send(sdi, &packet);
516 /* Send the trigger. */
517 std_session_send_df_trigger(sdi);
520 /* Send post-trigger / all captured samples. */
521 int num_pre_trigger_samples = devc->trigger_at_smpl == OLS_NO_TRIGGER
522 ? 0 : devc->trigger_at_smpl;
523 packet.type = SR_DF_LOGIC;
524 packet.payload = &logic;
525 logic.length = (devc->num_samples - num_pre_trigger_samples) * 4;
527 logic.data = devc->raw_sample_buf + (num_pre_trigger_samples +
528 devc->limit_samples - devc->num_samples) * 4;
529 sr_session_send(sdi, &packet);
531 g_free(devc->raw_sample_buf);
533 serial_flush(serial);
534 abort_acquisition(sdi);
540 static int ols_set_basic_trigger_stage(const struct ols_basic_trigger_desc *trigger_desc, struct sr_serial_dev_inst *serial, int stage)
544 cmd = CMD_SET_BASIC_TRIGGER_MASK0 + stage * 4;
545 if (ols_send_longdata(serial, cmd, trigger_desc->trigger_mask[stage]) != SR_OK)
548 cmd = CMD_SET_BASIC_TRIGGER_VALUE0 + stage * 4;
549 if (ols_send_longdata(serial, cmd, trigger_desc->trigger_value[stage]) != SR_OK)
552 cmd = CMD_SET_BASIC_TRIGGER_CONFIG0 + stage * 4;
553 arg[0] = arg[1] = arg[3] = 0x00;
555 if (stage == trigger_desc->num_stages)
556 /* Last stage, fire when this one matches. */
557 arg[3] |= TRIGGER_START;
558 if (send_longcommand(serial, cmd, arg) != SR_OK)
564 SR_PRIV int ols_prepare_acquisition(const struct sr_dev_inst *sdi) {
567 struct dev_context *devc = sdi->priv;
568 struct sr_serial_dev_inst *serial = sdi->conn;
570 int num_changroups = 0;
571 uint8_t changroup_mask = 0;
572 uint32_t channel_mask = ols_channel_mask(sdi);
573 for (unsigned int i = 0; i < 4; i++) {
574 if (channel_mask & (0xff << (i * 8))) {
575 changroup_mask |= (1 << i);
581 * Limit readcount to prevent reading past the end of the hardware
582 * buffer. Rather read too many samples than too few.
584 uint32_t samplecount = MIN(devc->max_samples / num_changroups, devc->limit_samples);
585 uint32_t readcount = (samplecount + 3) / 4;
588 /* Basic triggers. */
589 struct ols_basic_trigger_desc basic_trigger_desc;
590 if (convert_trigger(sdi, &basic_trigger_desc) != SR_OK) {
591 sr_err("Failed to configure channels.");
594 if (basic_trigger_desc.num_stages > 0) {
596 * According to http://mygizmos.org/ols/Logic-Sniffer-FPGA-Spec.pdf
597 * reset command must be send prior each arm command
599 sr_dbg("Send reset command before trigger configure");
600 if (ols_send_reset(serial) != SR_OK)
603 delaycount = readcount * (1 - devc->capture_ratio / 100.0);
604 devc->trigger_at_smpl = (readcount - delaycount) * 4 - basic_trigger_desc.num_stages;
605 for (int i = 0; i <= basic_trigger_desc.num_stages; i++) {
606 sr_dbg("Setting OLS stage %d trigger.", i);
607 if ((ret = ols_set_basic_trigger_stage(&basic_trigger_desc, serial, i)) != SR_OK)
611 /* No triggers configured, force trigger on first stage. */
612 sr_dbg("Forcing trigger at stage 0.");
613 if ((ret = ols_set_basic_trigger_stage(&basic_trigger_desc, serial, 0)) != SR_OK)
615 delaycount = readcount;
619 sr_dbg("Setting samplerate to %" PRIu64 "Hz (divider %u)",
620 devc->cur_samplerate, devc->cur_samplerate_divider);
621 if (ols_send_longdata(serial, CMD_SET_DIVIDER, devc->cur_samplerate_divider & 0x00FFFFFF) != SR_OK)
624 /* Send sample limit and pre/post-trigger capture ratio. */
625 sr_dbg("Setting sample limit %d, trigger point at %d",
626 (readcount - 1) * 4, (delaycount - 1) * 4);
628 if (devc->max_samples > 256 * 1024) {
629 if (ols_send_longdata(serial, CMD_CAPTURE_READCOUNT, readcount-1) != SR_OK)
631 if (ols_send_longdata(serial, CMD_CAPTURE_DELAYCOUNT, delaycount-1) != SR_OK)
635 WL16(&arg[0], readcount-1);
636 WL16(&arg[2], delaycount-1);
637 if (send_longcommand(serial, CMD_CAPTURE_SIZE, arg) != SR_OK)
642 sr_dbg("Setting intpat %s, extpat %s, RLE %s, noise_filter %s, demux %s, %s clock%s",
643 devc->capture_flags & CAPTURE_FLAG_INTERNAL_TEST_MODE ? "on": "off",
644 devc->capture_flags & CAPTURE_FLAG_EXTERNAL_TEST_MODE ? "on": "off",
645 devc->capture_flags & CAPTURE_FLAG_RLE ? "on" : "off",
646 devc->capture_flags & CAPTURE_FLAG_NOISE_FILTER ? "on": "off",
647 devc->capture_flags & CAPTURE_FLAG_DEMUX ? "on" : "off",
648 devc->capture_flags & CAPTURE_FLAG_CLOCK_EXTERNAL ? "external" : "internal",
649 devc->capture_flags & CAPTURE_FLAG_CLOCK_EXTERNAL ? (devc->capture_flags & CAPTURE_FLAG_INVERT_EXT_CLOCK
650 ? " on falling edge" : "on rising edge") : "");
653 * Enable/disable OLS channel groups in the flag register according
654 * to the channel mask. 1 means "disable channel".
656 devc->capture_flags &= ~0x3c;
657 devc->capture_flags |= ~(changroup_mask << 2) & 0x3c;
659 /* RLE mode is always zero, for now. */
661 if (ols_send_longdata(serial, CMD_SET_FLAGS, devc->capture_flags) != SR_OK)