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 /* Select interface B, otherwise communication will fail. */
77 ret = ftdi_set_interface(devc->ftdic, INTERFACE_B);
79 sr_err("Failed to set FTDI interface B (%d): %s", ret,
80 ftdi_get_error_string(devc->ftdic));
83 sr_dbg("FTDI chip interface B set successfully.");
85 /* Check for the device and temporarily open it. */
86 ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID, USB_DEVICE_ID,
89 /* Log errors, except for -3 ("device not found"). */
91 sr_err("Failed to open device (%d): %s", ret,
92 ftdi_get_error_string(devc->ftdic));
95 sr_dbg("FTDI device opened successfully.");
97 /* Purge RX/TX buffers in the FTDI chip. */
98 if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) {
99 sr_err("Failed to purge FTDI RX/TX buffers (%d): %s.",
100 ret, ftdi_get_error_string(devc->ftdic));
101 goto err_open_close_ftdic;
103 sr_dbg("FTDI chip buffers purged successfully.");
105 /* Reset the FTDI bitmode. */
106 ret = ftdi_set_bitmode(devc->ftdic, 0xff, BITMODE_RESET);
108 sr_err("Failed to reset the FTDI chip bitmode (%d): %s.",
109 ret, ftdi_get_error_string(devc->ftdic));
110 goto err_open_close_ftdic;
112 sr_dbg("FTDI chip bitmode reset successfully.");
114 /* Set the FTDI latency timer to 16. */
115 ret = ftdi_set_latency_timer(devc->ftdic, 16);
117 sr_err("Failed to set FTDI latency timer (%d): %s.",
118 ret, ftdi_get_error_string(devc->ftdic));
119 goto err_open_close_ftdic;
121 sr_dbg("FTDI chip latency timer set successfully.");
123 /* Set the FTDI read data chunk size to 64kB. */
124 ret = ftdi_read_data_set_chunksize(devc->ftdic, 64 * 1024);
126 sr_err("Failed to set FTDI read data chunk size (%d): %s.",
127 ret, ftdi_get_error_string(devc->ftdic));
128 goto err_open_close_ftdic;
130 sr_dbg("FTDI chip read data chunk size set successfully.");
134 err_open_close_ftdic:
135 ftdi_usb_close(devc->ftdic);
139 SR_PRIV int p_ols_close(struct dev_context *devc)
143 /* Note: Caller checks devc and devc->ftdic. */
145 if ((ret = ftdi_usb_close(devc->ftdic)) < 0) {
146 sr_err("Failed to close FTDI device (%d): %s.",
147 ret, ftdi_get_error_string(devc->ftdic));
154 /* Configures the channel mask based on which channels are enabled. */
155 SR_PRIV void pols_channel_mask(const struct sr_dev_inst *sdi)
157 struct dev_context *devc;
158 struct sr_channel *channel;
163 devc->channel_mask = 0;
164 for (l = sdi->channels; l; l = l->next) {
166 if (channel->enabled)
167 devc->channel_mask |= 1 << channel->index;
171 SR_PRIV int pols_convert_trigger(const struct sr_dev_inst *sdi)
173 struct dev_context *devc;
174 struct sr_trigger *trigger;
175 struct sr_trigger_stage *stage;
176 struct sr_trigger_match *match;
182 devc->num_stages = 0;
183 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
184 devc->trigger_mask[i] = 0;
185 devc->trigger_value[i] = 0;
186 devc->trigger_edge[i] = 0;
189 if (!(trigger = sr_session_trigger_get(sdi->session)))
192 devc->num_stages = g_slist_length(trigger->stages);
193 if (devc->num_stages > NUM_TRIGGER_STAGES) {
194 sr_err("This device only supports %d trigger stages.",
199 for (l = trigger->stages; l; l = l->next) {
201 for (m = stage->matches; m; m = m->next) {
203 if (!match->channel->enabled)
204 /* Ignore disabled channels with a trigger. */
206 devc->trigger_mask[stage->stage] |= 1 << match->channel->index;
207 if (match->match == SR_TRIGGER_ONE || match->match == SR_TRIGGER_RISING)
208 devc->trigger_value[stage->stage] |= 1 << match->channel->index;
209 if (match->match == SR_TRIGGER_RISING || match->match == SR_TRIGGER_FALLING)
210 devc->trigger_edge[stage->stage] |= 1 << match->channel->index;
217 SR_PRIV struct sr_dev_inst *p_ols_get_metadata(uint8_t *buf, int bytes_read, struct dev_context *devc)
219 struct sr_dev_inst *sdi;
220 uint32_t tmp_int, ui;
221 uint8_t key, type, token;
222 GString *tmp_str, *devname, *version;
226 sdi = g_malloc0(sizeof(struct sr_dev_inst));
227 sdi->status = SR_ST_INACTIVE;
230 devname = g_string_new("");
231 version = g_string_new("");
234 while (index < bytes_read) {
237 sr_dbg("Got metadata key 0x00, metadata ends.");
244 /* NULL-terminated string */
245 tmp_str = g_string_new("");
246 while ((index < bytes_read) && ((tmp_c = buf[index++]) != '\0'))
247 g_string_append_c(tmp_str, tmp_c);
248 sr_dbg("Got metadata key 0x%.2x value '%s'.",
253 devname = g_string_append(devname, tmp_str->str);
256 /* FPGA firmware version */
258 g_string_append(version, ", ");
259 g_string_append(version, "FPGA version ");
260 g_string_append(version, tmp_str->str);
263 /* Ancillary version */
265 g_string_append(version, ", ");
266 g_string_append(version, "Ancillary version ");
267 g_string_append(version, tmp_str->str);
270 sr_info("Unknown token 0x%.2x: '%s'",
271 token, tmp_str->str);
274 g_string_free(tmp_str, TRUE);
277 /* 32-bit unsigned integer */
279 for (i = 0; i < 4; i++) {
280 tmp_int = (tmp_int << 8) | buf[index++];
282 sr_dbg("Got metadata key 0x%.2x value 0x%.8x.",
286 /* Number of usable channels */
287 for (ui = 0; ui < tmp_int; ui++)
288 sr_channel_new(sdi, ui, SR_CHANNEL_LOGIC, TRUE,
289 p_ols_channel_names[ui]);
292 /* Amount of sample memory available (bytes) */
293 devc->max_samplebytes = tmp_int;
296 /* Amount of dynamic memory available (bytes) */
297 /* what is this for? */
300 /* Maximum sample rate (Hz) */
301 devc->max_samplerate = tmp_int;
304 /* protocol version */
305 devc->protocol_version = tmp_int;
308 sr_info("Unknown token 0x%.2x: 0x%.8x.",
314 /* 8-bit unsigned integer */
315 tmp_c = buf[index++];
316 sr_dbg("Got metadata key 0x%.2x value 0x%.2x.",
320 /* Number of usable channels */
321 for (ui = 0; ui < tmp_c; ui++)
322 sr_channel_new(sdi, ui, SR_CHANNEL_LOGIC, TRUE,
323 p_ols_channel_names[ui]);
326 /* protocol version */
327 devc->protocol_version = tmp_c;
330 sr_info("Unknown token 0x%.2x: 0x%.2x.",
341 sdi->model = devname->str;
342 sdi->version = version->str;
343 g_string_free(devname, FALSE);
344 g_string_free(version, FALSE);
349 SR_PRIV int p_ols_set_samplerate(const struct sr_dev_inst *sdi,
350 const uint64_t samplerate)
352 struct dev_context *devc;
355 if (devc->max_samplerate && samplerate > devc->max_samplerate)
356 return SR_ERR_SAMPLERATE;
358 if (samplerate > CLOCK_RATE) {
359 sr_info("Enabling demux mode.");
360 devc->flag_reg |= FLAG_DEMUX;
361 devc->flag_reg &= ~FLAG_FILTER;
362 devc->max_channels = NUM_CHANNELS / 2;
363 devc->cur_samplerate_divider = (CLOCK_RATE * 2 / samplerate) - 1;
365 sr_info("Disabling demux mode.");
366 devc->flag_reg &= ~FLAG_DEMUX;
367 devc->flag_reg |= FLAG_FILTER;
368 devc->max_channels = NUM_CHANNELS;
369 devc->cur_samplerate_divider = (CLOCK_RATE / samplerate) - 1;
372 /* Calculate actual samplerate used and complain if it is different
373 * from the requested.
375 devc->cur_samplerate = CLOCK_RATE / (devc->cur_samplerate_divider + 1);
376 if (devc->flag_reg & FLAG_DEMUX)
377 devc->cur_samplerate *= 2;
378 if (devc->cur_samplerate != samplerate)
379 sr_info("Can't match samplerate %" PRIu64 ", using %"
380 PRIu64 ".", samplerate, devc->cur_samplerate);
385 SR_PRIV int p_ols_receive_data(int fd, int revents, void *cb_data)
387 struct dev_context *devc;
388 struct sr_dev_inst *sdi;
389 struct sr_datafeed_packet packet;
390 struct sr_datafeed_logic logic;
392 int num_channels, offset, j;
393 int bytes_read, index;
403 if (devc->num_transfers++ == 0) {
404 devc->raw_sample_buf = g_try_malloc(devc->limit_samples * 4);
405 if (!devc->raw_sample_buf) {
406 sr_err("Sample buffer malloc failed.");
409 /* fill with 1010... for debugging */
410 memset(devc->raw_sample_buf, 0x82, devc->limit_samples * 4);
413 if ((devc->num_samples < devc->limit_samples) && (devc->cnt_samples < devc->max_samples)) {
416 for (i = NUM_CHANNELS; i > 0x02; i /= 2) {
417 if ((devc->flag_reg & i) == 0) {
422 /* Get a block of data. */
423 bytes_read = ftdi_read_data(devc->ftdic, devc->ftdi_buf, FTDI_BUF_SIZE);
424 if (bytes_read < 0) {
425 sr_err("Failed to read FTDI data (%d): %s.",
426 bytes_read, ftdi_get_error_string(devc->ftdic));
427 sdi->driver->dev_acquisition_stop(sdi);
430 if (bytes_read == 0) {
431 sr_spew("Received 0 bytes, nothing to do.");
435 sr_dbg("Received %d bytes", bytes_read);
438 while (index < bytes_read) {
439 byte = devc->ftdi_buf[index++];
442 devc->sample[devc->num_bytes++] = byte;
443 sr_spew("Received byte 0x%.2x.", byte);
445 if ((devc->flag_reg & FLAG_DEMUX) && (devc->flag_reg & FLAG_RLE)) {
446 /* RLE in demux mode must be processed differently
447 * since in this case the RLE encoder is operating on pairs of samples.
449 if (devc->num_bytes == num_channels * 2) {
450 devc->cnt_samples += 2;
451 devc->cnt_samples_rle += 2;
453 * Got a sample pair. Convert from the OLS's little-endian
454 * sample to the local format.
456 sample = devc->sample[0] | (devc->sample[1] << 8) \
457 | (devc->sample[2] << 16) | (devc->sample[3] << 24);
458 sr_spew("Received sample pair 0x%.*x.", devc->num_bytes * 2, sample);
461 * In RLE mode the high bit of the sample pair is the
462 * "count" flag, meaning this sample pair is the number
463 * of times the previous sample pair occurred.
465 if (devc->sample[devc->num_bytes - 1] & 0x80) {
466 /* Clear the high bit. */
467 sample &= ~(0x80 << (devc->num_bytes - 1) * 8);
468 devc->rle_count = sample;
469 devc->cnt_samples_rle += devc->rle_count * 2;
470 sr_dbg("RLE count: %u.", devc->rle_count * 2);
474 devc->num_samples += (devc->rle_count + 1) * 2;
475 if (devc->num_samples > devc->limit_samples) {
476 /* Save us from overrunning the buffer. */
477 devc->rle_count -= (devc->num_samples - devc->limit_samples) / 2;
478 devc->num_samples = devc->limit_samples;
483 * Some channel groups may have been turned
484 * off, to speed up transfer between the
485 * hardware and the PC. Expand that here before
486 * submitting it over the session bus --
487 * whatever is listening on the bus will be
488 * expecting a full 32-bit sample, based on
489 * the number of channels.
492 /* expand first sample */
493 memset(devc->tmp_sample, 0, 4);
494 for (i = 0; i < 2; i++) {
495 if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
497 * This channel group was
498 * enabled, copy from received
501 devc->tmp_sample[i] = devc->sample[j++];
504 /* Clear out the most significant bit of the sample */
505 devc->tmp_sample[devc->num_bytes - 1] &= 0x7f;
506 sr_spew("Expanded sample 1: 0x%.2x%.2x%.2x%.2x.",
507 devc->tmp_sample[3], devc->tmp_sample[2],
508 devc->tmp_sample[1], devc->tmp_sample[0]);
510 /* expand second sample */
511 memset(devc->tmp_sample2, 0, 4);
512 for (i = 0; i < 2; i++) {
513 if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
515 * This channel group was
516 * enabled, copy from received
519 devc->tmp_sample2[i] = devc->sample[j++];
522 /* Clear out the most significant bit of the sample */
523 devc->tmp_sample2[devc->num_bytes - 1] &= 0x7f;
524 sr_spew("Expanded sample 2: 0x%.2x%.2x%.2x%.2x.",
525 devc->tmp_sample2[3], devc->tmp_sample2[2],
526 devc->tmp_sample2[1], devc->tmp_sample2[0]);
529 * OLS sends its sample buffer backwards.
530 * store it in reverse order here, so we can dump
531 * this on the session bus later.
533 offset = (devc->limit_samples - devc->num_samples) * 4;
534 for (i = 0; i <= devc->rle_count; i++) {
535 memcpy(devc->raw_sample_buf + offset + (i * 8),
536 devc->tmp_sample2, 4);
537 memcpy(devc->raw_sample_buf + offset + (4 + (i * 8)),
538 devc->tmp_sample, 4);
540 memset(devc->sample, 0, 4);
546 if (devc->num_bytes == num_channels) {
548 devc->cnt_samples_rle++;
550 * Got a full sample. Convert from the OLS's little-endian
551 * sample to the local format.
553 sample = devc->sample[0] | (devc->sample[1] << 8) \
554 | (devc->sample[2] << 16) | (devc->sample[3] << 24);
555 sr_spew("Received sample 0x%.*x.", devc->num_bytes * 2, sample);
556 if (devc->flag_reg & FLAG_RLE) {
558 * In RLE mode the high bit of the sample is the
559 * "count" flag, meaning this sample is the number
560 * of times the previous sample occurred.
562 if (devc->sample[devc->num_bytes - 1] & 0x80) {
563 /* Clear the high bit. */
564 sample &= ~(0x80 << (devc->num_bytes - 1) * 8);
565 devc->rle_count = sample;
566 devc->cnt_samples_rle += devc->rle_count;
567 sr_dbg("RLE count: %u.", devc->rle_count);
572 devc->num_samples += devc->rle_count + 1;
573 if (devc->num_samples > devc->limit_samples) {
574 /* Save us from overrunning the buffer. */
575 devc->rle_count -= devc->num_samples - devc->limit_samples;
576 devc->num_samples = devc->limit_samples;
580 if (num_channels < 4) {
582 * Some channel groups may have been turned
583 * off, to speed up transfer between the
584 * hardware and the PC. Expand that here before
585 * submitting it over the session bus --
586 * whatever is listening on the bus will be
587 * expecting a full 32-bit sample, based on
588 * the number of channels.
591 memset(devc->tmp_sample, 0, 4);
592 for (i = 0; i < 4; i++) {
593 if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
595 * This channel group was
596 * enabled, copy from received
599 devc->tmp_sample[i] = devc->sample[j++];
602 memcpy(devc->sample, devc->tmp_sample, 4);
603 sr_spew("Expanded sample: 0x%.8x.", sample);
607 * Pipistrello OLS sends its sample buffer backwards.
608 * store it in reverse order here, so we can dump
609 * this on the session bus later.
611 offset = (devc->limit_samples - devc->num_samples) * 4;
612 for (i = 0; i <= devc->rle_count; i++) {
613 memcpy(devc->raw_sample_buf + offset + (i * 4),
616 memset(devc->sample, 0, 4);
625 bytes_read = ftdi_read_data(devc->ftdic, devc->ftdi_buf, FTDI_BUF_SIZE);
626 } while (bytes_read > 0);
629 * We've acquired all the samples we asked for -- we're done.
630 * Send the (properly-ordered) buffer to the frontend.
632 sr_dbg("Received %d bytes, %d samples, %d decompressed samples.",
633 devc->cnt_bytes, devc->cnt_samples,
634 devc->cnt_samples_rle);
635 if (devc->trigger_at != -1) {
637 * A trigger was set up, so we need to tell the frontend
640 if (devc->trigger_at > 0) {
641 /* There are pre-trigger samples, send those first. */
642 packet.type = SR_DF_LOGIC;
643 packet.payload = &logic;
644 logic.length = devc->trigger_at * 4;
646 logic.data = devc->raw_sample_buf +
647 (devc->limit_samples - devc->num_samples) * 4;
648 sr_session_send(sdi, &packet);
651 /* Send the trigger. */
652 packet.type = SR_DF_TRIGGER;
653 sr_session_send(sdi, &packet);
655 /* Send post-trigger samples. */
656 packet.type = SR_DF_LOGIC;
657 packet.payload = &logic;
658 logic.length = (devc->num_samples * 4) - (devc->trigger_at * 4);
660 logic.data = devc->raw_sample_buf + devc->trigger_at * 4 +
661 (devc->limit_samples - devc->num_samples) * 4;
662 sr_session_send(sdi, &packet);
664 /* no trigger was used */
665 packet.type = SR_DF_LOGIC;
666 packet.payload = &logic;
667 logic.length = devc->num_samples * 4;
669 logic.data = devc->raw_sample_buf +
670 (devc->limit_samples - devc->num_samples) * 4;
671 sr_session_send(sdi, &packet);
673 g_free(devc->raw_sample_buf);
675 sdi->driver->dev_acquisition_stop(sdi);