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 SR_PRIV int write_shortcommand(struct dev_context *devc, uint8_t command)
28 sr_dbg("Sending cmd 0x%.2x.", command);
30 bytes_written = ftdi_write_data(devc->ftdic, buf, 1);
31 if (bytes_written < 0) {
32 sr_err("Failed to write FTDI data (%d): %s.",
33 bytes_written, ftdi_get_error_string(devc->ftdic));
35 } else if (bytes_written != 1) {
36 sr_err("FTDI write error, only %d/%d bytes written: %s.",
37 bytes_written, 1, ftdi_get_error_string(devc->ftdic));
44 SR_PRIV int write_longcommand(struct dev_context *devc, uint8_t command, uint8_t *data)
49 sr_dbg("Sending cmd 0x%.2x data 0x%.2x%.2x%.2x%.2x.", command,
50 data[0], data[1], data[2], data[3]);
56 bytes_written = ftdi_write_data(devc->ftdic, buf, 5);
57 if (bytes_written < 0) {
58 sr_err("Failed to write FTDI data (%d): %s.",
59 bytes_written, ftdi_get_error_string(devc->ftdic));
61 } else if (bytes_written != 5) {
62 sr_err("FTDI write error, only %d/%d bytes written: %s.",
63 bytes_written, 1, ftdi_get_error_string(devc->ftdic));
70 SR_PRIV int p_ols_open(struct dev_context *devc)
74 /* Note: Caller checks devc and devc->ftdic. */
76 ret = ftdi_set_interface(devc->ftdic, INTERFACE_B);
78 sr_err("Failed to set FTDI interface B (%d): %s", ret,
79 ftdi_get_error_string(devc->ftdic));
83 ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID, USB_DEVICE_ID,
86 /* Log errors, except for -3 ("device not found"). */
88 sr_err("Failed to open device (%d): %s", ret,
89 ftdi_get_error_string(devc->ftdic));
93 if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) {
94 sr_err("Failed to purge FTDI RX/TX buffers (%d): %s.",
95 ret, ftdi_get_error_string(devc->ftdic));
96 goto err_open_close_ftdic;
99 ret = ftdi_set_bitmode(devc->ftdic, 0xff, BITMODE_RESET);
101 sr_err("Failed to reset the FTDI chip bitmode (%d): %s.",
102 ret, ftdi_get_error_string(devc->ftdic));
103 goto err_open_close_ftdic;
106 ret = ftdi_set_latency_timer(devc->ftdic, 16);
108 sr_err("Failed to set FTDI latency timer (%d): %s.",
109 ret, ftdi_get_error_string(devc->ftdic));
110 goto err_open_close_ftdic;
113 ret = ftdi_read_data_set_chunksize(devc->ftdic, 64 * 1024);
115 sr_err("Failed to set FTDI read data chunk size (%d): %s.",
116 ret, ftdi_get_error_string(devc->ftdic));
117 goto err_open_close_ftdic;
122 err_open_close_ftdic:
123 ftdi_usb_close(devc->ftdic);
128 SR_PRIV int p_ols_close(struct dev_context *devc)
132 /* Note: Caller checks devc and devc->ftdic. */
134 if ((ret = ftdi_usb_close(devc->ftdic)) < 0) {
135 sr_err("Failed to close FTDI device (%d): %s.",
136 ret, ftdi_get_error_string(devc->ftdic));
143 /* Configures the channel mask based on which channels are enabled. */
144 SR_PRIV void pols_channel_mask(const struct sr_dev_inst *sdi)
146 struct dev_context *devc;
147 struct sr_channel *channel;
152 devc->channel_mask = 0;
153 for (l = sdi->channels; l; l = l->next) {
155 if (channel->enabled)
156 devc->channel_mask |= 1 << channel->index;
160 SR_PRIV int pols_convert_trigger(const struct sr_dev_inst *sdi)
162 struct dev_context *devc;
163 struct sr_trigger *trigger;
164 struct sr_trigger_stage *stage;
165 struct sr_trigger_match *match;
171 devc->num_stages = 0;
172 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
173 devc->trigger_mask[i] = 0;
174 devc->trigger_value[i] = 0;
175 devc->trigger_edge[i] = 0;
178 if (!(trigger = sr_session_trigger_get(sdi->session)))
181 devc->num_stages = g_slist_length(trigger->stages);
182 if (devc->num_stages > NUM_TRIGGER_STAGES) {
183 sr_err("This device only supports %d trigger stages.",
188 for (l = trigger->stages; l; l = l->next) {
190 for (m = stage->matches; m; m = m->next) {
192 if (!match->channel->enabled)
193 /* Ignore disabled channels with a trigger. */
195 devc->trigger_mask[stage->stage] |= 1 << match->channel->index;
196 if (match->match == SR_TRIGGER_ONE || match->match == SR_TRIGGER_RISING)
197 devc->trigger_value[stage->stage] |= 1 << match->channel->index;
198 if (match->match == SR_TRIGGER_RISING || match->match == SR_TRIGGER_FALLING)
199 devc->trigger_edge[stage->stage] |= 1 << match->channel->index;
206 SR_PRIV struct sr_dev_inst *p_ols_get_metadata(uint8_t *buf, int bytes_read, struct dev_context *devc)
208 struct sr_dev_inst *sdi;
209 uint32_t tmp_int, ui;
210 uint8_t key, type, token;
211 GString *tmp_str, *devname, *version;
215 sdi = g_malloc0(sizeof(struct sr_dev_inst));
216 sdi->status = SR_ST_INACTIVE;
219 devname = g_string_new("");
220 version = g_string_new("");
223 while (index < bytes_read) {
226 sr_dbg("Got metadata key 0x00, metadata ends.");
233 /* NULL-terminated string */
234 tmp_str = g_string_new("");
235 while ((index < bytes_read) && ((tmp_c = buf[index++]) != '\0'))
236 g_string_append_c(tmp_str, tmp_c);
237 sr_dbg("Got metadata key 0x%.2x value '%s'.",
242 devname = g_string_append(devname, tmp_str->str);
245 /* FPGA firmware version */
247 g_string_append(version, ", ");
248 g_string_append(version, "FPGA version ");
249 g_string_append(version, tmp_str->str);
252 /* Ancillary version */
254 g_string_append(version, ", ");
255 g_string_append(version, "Ancillary version ");
256 g_string_append(version, tmp_str->str);
259 sr_info("Unknown token 0x%.2x: '%s'",
260 token, tmp_str->str);
263 g_string_free(tmp_str, TRUE);
266 /* 32-bit unsigned integer */
267 tmp_int = RB32(&buf[index]);
268 index += sizeof(uint32_t);
269 sr_dbg("Got metadata key 0x%.2x value 0x%.8x.",
273 /* Number of usable channels */
274 for (ui = 0; ui < tmp_int; ui++)
275 sr_channel_new(sdi, ui, SR_CHANNEL_LOGIC, TRUE,
276 p_ols_channel_names[ui]);
279 /* Amount of sample memory available (bytes) */
280 devc->max_samplebytes = tmp_int;
283 /* Amount of dynamic memory available (bytes) */
284 /* what is this for? */
287 /* Maximum sample rate (Hz) */
288 devc->max_samplerate = tmp_int;
291 /* protocol version */
292 devc->protocol_version = tmp_int;
295 sr_info("Unknown token 0x%.2x: 0x%.8x.",
301 /* 8-bit unsigned integer */
302 tmp_c = buf[index++];
303 sr_dbg("Got metadata key 0x%.2x value 0x%.2x.",
307 /* Number of usable channels */
308 for (ui = 0; ui < tmp_c; ui++)
309 sr_channel_new(sdi, ui, SR_CHANNEL_LOGIC, TRUE,
310 p_ols_channel_names[ui]);
313 /* protocol version */
314 devc->protocol_version = tmp_c;
317 sr_info("Unknown token 0x%.2x: 0x%.2x.",
328 sdi->model = devname->str;
329 sdi->version = version->str;
330 g_string_free(devname, FALSE);
331 g_string_free(version, FALSE);
336 SR_PRIV int p_ols_set_samplerate(const struct sr_dev_inst *sdi,
337 const uint64_t samplerate)
339 struct dev_context *devc;
342 if (devc->max_samplerate && samplerate > devc->max_samplerate)
343 return SR_ERR_SAMPLERATE;
345 if (samplerate > CLOCK_RATE) {
346 sr_info("Enabling demux mode.");
347 devc->flag_reg |= FLAG_DEMUX;
348 devc->flag_reg &= ~FLAG_FILTER;
349 devc->max_channels = NUM_CHANNELS / 2;
350 devc->cur_samplerate_divider = (CLOCK_RATE * 2 / samplerate) - 1;
352 sr_info("Disabling demux mode.");
353 devc->flag_reg &= ~FLAG_DEMUX;
354 devc->flag_reg |= FLAG_FILTER;
355 devc->max_channels = NUM_CHANNELS;
356 devc->cur_samplerate_divider = (CLOCK_RATE / samplerate) - 1;
359 /* Calculate actual samplerate used and complain if it is different
360 * from the requested.
362 devc->cur_samplerate = CLOCK_RATE / (devc->cur_samplerate_divider + 1);
363 if (devc->flag_reg & FLAG_DEMUX)
364 devc->cur_samplerate *= 2;
365 if (devc->cur_samplerate != samplerate)
366 sr_info("Can't match samplerate %" PRIu64 ", using %"
367 PRIu64 ".", samplerate, devc->cur_samplerate);
372 SR_PRIV int p_ols_receive_data(int fd, int revents, void *cb_data)
374 struct dev_context *devc;
375 struct sr_dev_inst *sdi;
376 struct sr_datafeed_packet packet;
377 struct sr_datafeed_logic logic;
379 int num_channels, offset, j;
380 int bytes_read, index;
390 if (devc->num_transfers++ == 0) {
391 devc->raw_sample_buf = g_try_malloc(devc->limit_samples * 4);
392 if (!devc->raw_sample_buf) {
393 sr_err("Sample buffer malloc failed.");
396 /* fill with 1010... for debugging */
397 memset(devc->raw_sample_buf, 0x82, devc->limit_samples * 4);
400 if ((devc->num_samples < devc->limit_samples) && (devc->cnt_samples < devc->max_samples)) {
403 for (i = NUM_CHANNELS; i > 0x02; i /= 2) {
404 if ((devc->flag_reg & i) == 0) {
409 /* Get a block of data. */
410 bytes_read = ftdi_read_data(devc->ftdic, devc->ftdi_buf, FTDI_BUF_SIZE);
411 if (bytes_read < 0) {
412 sr_err("Failed to read FTDI data (%d): %s.",
413 bytes_read, ftdi_get_error_string(devc->ftdic));
414 sr_dev_acquisition_stop(sdi);
417 if (bytes_read == 0) {
418 sr_spew("Received 0 bytes, nothing to do.");
422 sr_dbg("Received %d bytes", bytes_read);
425 while (index < bytes_read) {
426 byte = devc->ftdi_buf[index++];
429 devc->sample[devc->num_bytes++] = byte;
430 sr_spew("Received byte 0x%.2x.", byte);
432 if ((devc->flag_reg & FLAG_DEMUX) && (devc->flag_reg & FLAG_RLE)) {
433 /* RLE in demux mode must be processed differently
434 * since in this case the RLE encoder is operating on pairs of samples.
436 if (devc->num_bytes == num_channels * 2) {
437 devc->cnt_samples += 2;
438 devc->cnt_samples_rle += 2;
440 * Got a sample pair. Convert from the OLS's little-endian
441 * sample to the local format.
443 sample = devc->sample[0] | (devc->sample[1] << 8) \
444 | (devc->sample[2] << 16) | (devc->sample[3] << 24);
445 sr_spew("Received sample pair 0x%.*x.", devc->num_bytes * 2, sample);
448 * In RLE mode the high bit of the sample pair is the
449 * "count" flag, meaning this sample pair is the number
450 * of times the previous sample pair occurred.
452 if (devc->sample[devc->num_bytes - 1] & 0x80) {
453 /* Clear the high bit. */
454 sample &= ~(0x80 << (devc->num_bytes - 1) * 8);
455 devc->rle_count = sample;
456 devc->cnt_samples_rle += devc->rle_count * 2;
457 sr_dbg("RLE count: %u.", devc->rle_count * 2);
461 devc->num_samples += (devc->rle_count + 1) * 2;
462 if (devc->num_samples > devc->limit_samples) {
463 /* Save us from overrunning the buffer. */
464 devc->rle_count -= (devc->num_samples - devc->limit_samples) / 2;
465 devc->num_samples = devc->limit_samples;
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 /* expand first sample */
480 memset(devc->tmp_sample, 0, 4);
481 for (i = 0; i < 2; i++) {
482 if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
484 * This channel group was
485 * enabled, copy from received
488 devc->tmp_sample[i] = devc->sample[j++];
491 /* Clear out the most significant bit of the sample */
492 devc->tmp_sample[devc->num_bytes - 1] &= 0x7f;
493 sr_spew("Expanded sample 1: 0x%.2x%.2x%.2x%.2x.",
494 devc->tmp_sample[3], devc->tmp_sample[2],
495 devc->tmp_sample[1], devc->tmp_sample[0]);
497 /* expand second sample */
498 memset(devc->tmp_sample2, 0, 4);
499 for (i = 0; i < 2; i++) {
500 if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
502 * This channel group was
503 * enabled, copy from received
506 devc->tmp_sample2[i] = devc->sample[j++];
509 /* Clear out the most significant bit of the sample */
510 devc->tmp_sample2[devc->num_bytes - 1] &= 0x7f;
511 sr_spew("Expanded sample 2: 0x%.2x%.2x%.2x%.2x.",
512 devc->tmp_sample2[3], devc->tmp_sample2[2],
513 devc->tmp_sample2[1], devc->tmp_sample2[0]);
516 * OLS sends its sample buffer backwards.
517 * store it in reverse order here, so we can dump
518 * this on the session bus later.
520 offset = (devc->limit_samples - devc->num_samples) * 4;
521 for (i = 0; i <= devc->rle_count; i++) {
522 memcpy(devc->raw_sample_buf + offset + (i * 8),
523 devc->tmp_sample2, 4);
524 memcpy(devc->raw_sample_buf + offset + (4 + (i * 8)),
525 devc->tmp_sample, 4);
527 memset(devc->sample, 0, 4);
533 if (devc->num_bytes == num_channels) {
535 devc->cnt_samples_rle++;
537 * Got a full sample. Convert from the OLS's little-endian
538 * sample to the local format.
540 sample = devc->sample[0] | (devc->sample[1] << 8) \
541 | (devc->sample[2] << 16) | (devc->sample[3] << 24);
542 sr_spew("Received sample 0x%.*x.", devc->num_bytes * 2, sample);
543 if (devc->flag_reg & FLAG_RLE) {
545 * In RLE mode the high bit of the sample is the
546 * "count" flag, meaning this sample is the number
547 * of times the previous sample occurred.
549 if (devc->sample[devc->num_bytes - 1] & 0x80) {
550 /* Clear the high bit. */
551 sample &= ~(0x80 << (devc->num_bytes - 1) * 8);
552 devc->rle_count = sample;
553 devc->cnt_samples_rle += devc->rle_count;
554 sr_dbg("RLE count: %u.", devc->rle_count);
559 devc->num_samples += devc->rle_count + 1;
560 if (devc->num_samples > devc->limit_samples) {
561 /* Save us from overrunning the buffer. */
562 devc->rle_count -= devc->num_samples - devc->limit_samples;
563 devc->num_samples = devc->limit_samples;
567 if (num_channels < 4) {
569 * Some channel groups may have been turned
570 * off, to speed up transfer between the
571 * hardware and the PC. Expand that here before
572 * submitting it over the session bus --
573 * whatever is listening on the bus will be
574 * expecting a full 32-bit sample, based on
575 * the number of channels.
578 memset(devc->tmp_sample, 0, 4);
579 for (i = 0; i < 4; i++) {
580 if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
582 * This channel group was
583 * enabled, copy from received
586 devc->tmp_sample[i] = devc->sample[j++];
589 memcpy(devc->sample, devc->tmp_sample, 4);
590 sr_spew("Expanded sample: 0x%.8x.", sample);
594 * Pipistrello OLS sends its sample buffer backwards.
595 * store it in reverse order here, so we can dump
596 * this on the session bus later.
598 offset = (devc->limit_samples - devc->num_samples) * 4;
599 for (i = 0; i <= devc->rle_count; i++) {
600 memcpy(devc->raw_sample_buf + offset + (i * 4),
603 memset(devc->sample, 0, 4);
612 bytes_read = ftdi_read_data(devc->ftdic, devc->ftdi_buf, FTDI_BUF_SIZE);
613 } while (bytes_read > 0);
616 * We've acquired all the samples we asked for -- we're done.
617 * Send the (properly-ordered) buffer to the frontend.
619 sr_dbg("Received %d bytes, %d samples, %d decompressed samples.",
620 devc->cnt_bytes, devc->cnt_samples,
621 devc->cnt_samples_rle);
622 if (devc->trigger_at != -1) {
624 * A trigger was set up, so we need to tell the frontend
627 if (devc->trigger_at > 0) {
628 /* There are pre-trigger samples, send those first. */
629 packet.type = SR_DF_LOGIC;
630 packet.payload = &logic;
631 logic.length = devc->trigger_at * 4;
633 logic.data = devc->raw_sample_buf +
634 (devc->limit_samples - devc->num_samples) * 4;
635 sr_session_send(sdi, &packet);
638 /* Send the trigger. */
639 packet.type = SR_DF_TRIGGER;
640 sr_session_send(sdi, &packet);
642 /* Send post-trigger samples. */
643 packet.type = SR_DF_LOGIC;
644 packet.payload = &logic;
645 logic.length = (devc->num_samples * 4) - (devc->trigger_at * 4);
647 logic.data = devc->raw_sample_buf + devc->trigger_at * 4 +
648 (devc->limit_samples - devc->num_samples) * 4;
649 sr_session_send(sdi, &packet);
651 /* no trigger was used */
652 packet.type = SR_DF_LOGIC;
653 packet.payload = &logic;
654 logic.length = devc->num_samples * 4;
656 logic.data = devc->raw_sample_buf +
657 (devc->limit_samples - devc->num_samples) * 4;
658 sr_session_send(sdi, &packet);
660 g_free(devc->raw_sample_buf);
662 sr_dev_acquisition_stop(sdi);