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));
84 /* Check for the device and temporarily open it. */
85 ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID, USB_DEVICE_ID,
88 /* Log errors, except for -3 ("device not found"). */
90 sr_err("Failed to open device (%d): %s", ret,
91 ftdi_get_error_string(devc->ftdic));
95 /* Purge RX/TX buffers in the FTDI chip. */
96 if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) {
97 sr_err("Failed to purge FTDI RX/TX buffers (%d): %s.",
98 ret, ftdi_get_error_string(devc->ftdic));
99 goto err_open_close_ftdic;
102 /* Reset the FTDI bitmode. */
103 ret = ftdi_set_bitmode(devc->ftdic, 0xff, BITMODE_RESET);
105 sr_err("Failed to reset the FTDI chip bitmode (%d): %s.",
106 ret, ftdi_get_error_string(devc->ftdic));
107 goto err_open_close_ftdic;
110 /* Set the FTDI latency timer to 16. */
111 ret = ftdi_set_latency_timer(devc->ftdic, 16);
113 sr_err("Failed to set FTDI latency timer (%d): %s.",
114 ret, ftdi_get_error_string(devc->ftdic));
115 goto err_open_close_ftdic;
118 /* Set the FTDI read data chunk size to 64kB. */
119 ret = ftdi_read_data_set_chunksize(devc->ftdic, 64 * 1024);
121 sr_err("Failed to set FTDI read data chunk size (%d): %s.",
122 ret, ftdi_get_error_string(devc->ftdic));
123 goto err_open_close_ftdic;
128 err_open_close_ftdic:
129 ftdi_usb_close(devc->ftdic);
134 SR_PRIV int p_ols_close(struct dev_context *devc)
138 /* Note: Caller checks devc and devc->ftdic. */
140 if ((ret = ftdi_usb_close(devc->ftdic)) < 0) {
141 sr_err("Failed to close FTDI device (%d): %s.",
142 ret, ftdi_get_error_string(devc->ftdic));
149 /* Configures the channel mask based on which channels are enabled. */
150 SR_PRIV void pols_channel_mask(const struct sr_dev_inst *sdi)
152 struct dev_context *devc;
153 struct sr_channel *channel;
158 devc->channel_mask = 0;
159 for (l = sdi->channels; l; l = l->next) {
161 if (channel->enabled)
162 devc->channel_mask |= 1 << channel->index;
166 SR_PRIV int pols_convert_trigger(const struct sr_dev_inst *sdi)
168 struct dev_context *devc;
169 struct sr_trigger *trigger;
170 struct sr_trigger_stage *stage;
171 struct sr_trigger_match *match;
177 devc->num_stages = 0;
178 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
179 devc->trigger_mask[i] = 0;
180 devc->trigger_value[i] = 0;
181 devc->trigger_edge[i] = 0;
184 if (!(trigger = sr_session_trigger_get(sdi->session)))
187 devc->num_stages = g_slist_length(trigger->stages);
188 if (devc->num_stages > NUM_TRIGGER_STAGES) {
189 sr_err("This device only supports %d trigger stages.",
194 for (l = trigger->stages; l; l = l->next) {
196 for (m = stage->matches; m; m = m->next) {
198 if (!match->channel->enabled)
199 /* Ignore disabled channels with a trigger. */
201 devc->trigger_mask[stage->stage] |= 1 << match->channel->index;
202 if (match->match == SR_TRIGGER_ONE || match->match == SR_TRIGGER_RISING)
203 devc->trigger_value[stage->stage] |= 1 << match->channel->index;
204 if (match->match == SR_TRIGGER_RISING || match->match == SR_TRIGGER_FALLING)
205 devc->trigger_edge[stage->stage] |= 1 << match->channel->index;
212 SR_PRIV struct sr_dev_inst *p_ols_get_metadata(uint8_t *buf, int bytes_read, struct dev_context *devc)
214 struct sr_dev_inst *sdi;
215 uint32_t tmp_int, ui;
216 uint8_t key, type, token;
217 GString *tmp_str, *devname, *version;
221 sdi = g_malloc0(sizeof(struct sr_dev_inst));
222 sdi->status = SR_ST_INACTIVE;
225 devname = g_string_new("");
226 version = g_string_new("");
229 while (index < bytes_read) {
232 sr_dbg("Got metadata key 0x00, metadata ends.");
239 /* NULL-terminated string */
240 tmp_str = g_string_new("");
241 while ((index < bytes_read) && ((tmp_c = buf[index++]) != '\0'))
242 g_string_append_c(tmp_str, tmp_c);
243 sr_dbg("Got metadata key 0x%.2x value '%s'.",
248 devname = g_string_append(devname, tmp_str->str);
251 /* FPGA firmware version */
253 g_string_append(version, ", ");
254 g_string_append(version, "FPGA version ");
255 g_string_append(version, tmp_str->str);
258 /* Ancillary version */
260 g_string_append(version, ", ");
261 g_string_append(version, "Ancillary version ");
262 g_string_append(version, tmp_str->str);
265 sr_info("Unknown token 0x%.2x: '%s'",
266 token, tmp_str->str);
269 g_string_free(tmp_str, TRUE);
272 /* 32-bit unsigned integer */
274 for (i = 0; i < 4; i++) {
275 tmp_int = (tmp_int << 8) | buf[index++];
277 sr_dbg("Got metadata key 0x%.2x value 0x%.8x.",
281 /* Number of usable channels */
282 for (ui = 0; ui < tmp_int; ui++)
283 sr_channel_new(sdi, ui, SR_CHANNEL_LOGIC, TRUE,
284 p_ols_channel_names[ui]);
287 /* Amount of sample memory available (bytes) */
288 devc->max_samplebytes = tmp_int;
291 /* Amount of dynamic memory available (bytes) */
292 /* what is this for? */
295 /* Maximum sample rate (Hz) */
296 devc->max_samplerate = tmp_int;
299 /* protocol version */
300 devc->protocol_version = tmp_int;
303 sr_info("Unknown token 0x%.2x: 0x%.8x.",
309 /* 8-bit unsigned integer */
310 tmp_c = buf[index++];
311 sr_dbg("Got metadata key 0x%.2x value 0x%.2x.",
315 /* Number of usable channels */
316 for (ui = 0; ui < tmp_c; ui++)
317 sr_channel_new(sdi, ui, SR_CHANNEL_LOGIC, TRUE,
318 p_ols_channel_names[ui]);
321 /* protocol version */
322 devc->protocol_version = tmp_c;
325 sr_info("Unknown token 0x%.2x: 0x%.2x.",
336 sdi->model = devname->str;
337 sdi->version = version->str;
338 g_string_free(devname, FALSE);
339 g_string_free(version, FALSE);
344 SR_PRIV int p_ols_set_samplerate(const struct sr_dev_inst *sdi,
345 const uint64_t samplerate)
347 struct dev_context *devc;
350 if (devc->max_samplerate && samplerate > devc->max_samplerate)
351 return SR_ERR_SAMPLERATE;
353 if (samplerate > CLOCK_RATE) {
354 sr_info("Enabling demux mode.");
355 devc->flag_reg |= FLAG_DEMUX;
356 devc->flag_reg &= ~FLAG_FILTER;
357 devc->max_channels = NUM_CHANNELS / 2;
358 devc->cur_samplerate_divider = (CLOCK_RATE * 2 / samplerate) - 1;
360 sr_info("Disabling demux mode.");
361 devc->flag_reg &= ~FLAG_DEMUX;
362 devc->flag_reg |= FLAG_FILTER;
363 devc->max_channels = NUM_CHANNELS;
364 devc->cur_samplerate_divider = (CLOCK_RATE / samplerate) - 1;
367 /* Calculate actual samplerate used and complain if it is different
368 * from the requested.
370 devc->cur_samplerate = CLOCK_RATE / (devc->cur_samplerate_divider + 1);
371 if (devc->flag_reg & FLAG_DEMUX)
372 devc->cur_samplerate *= 2;
373 if (devc->cur_samplerate != samplerate)
374 sr_info("Can't match samplerate %" PRIu64 ", using %"
375 PRIu64 ".", samplerate, devc->cur_samplerate);
380 SR_PRIV int p_ols_receive_data(int fd, int revents, void *cb_data)
382 struct dev_context *devc;
383 struct sr_dev_inst *sdi;
384 struct sr_datafeed_packet packet;
385 struct sr_datafeed_logic logic;
387 int num_channels, offset, j;
388 int bytes_read, index;
398 if (devc->num_transfers++ == 0) {
399 devc->raw_sample_buf = g_try_malloc(devc->limit_samples * 4);
400 if (!devc->raw_sample_buf) {
401 sr_err("Sample buffer malloc failed.");
404 /* fill with 1010... for debugging */
405 memset(devc->raw_sample_buf, 0x82, devc->limit_samples * 4);
408 if ((devc->num_samples < devc->limit_samples) && (devc->cnt_samples < devc->max_samples)) {
411 for (i = NUM_CHANNELS; i > 0x02; i /= 2) {
412 if ((devc->flag_reg & i) == 0) {
417 /* Get a block of data. */
418 bytes_read = ftdi_read_data(devc->ftdic, devc->ftdi_buf, FTDI_BUF_SIZE);
419 if (bytes_read < 0) {
420 sr_err("Failed to read FTDI data (%d): %s.",
421 bytes_read, ftdi_get_error_string(devc->ftdic));
422 sr_dev_acquisition_stop(sdi);
425 if (bytes_read == 0) {
426 sr_spew("Received 0 bytes, nothing to do.");
430 sr_dbg("Received %d bytes", bytes_read);
433 while (index < bytes_read) {
434 byte = devc->ftdi_buf[index++];
437 devc->sample[devc->num_bytes++] = byte;
438 sr_spew("Received byte 0x%.2x.", byte);
440 if ((devc->flag_reg & FLAG_DEMUX) && (devc->flag_reg & FLAG_RLE)) {
441 /* RLE in demux mode must be processed differently
442 * since in this case the RLE encoder is operating on pairs of samples.
444 if (devc->num_bytes == num_channels * 2) {
445 devc->cnt_samples += 2;
446 devc->cnt_samples_rle += 2;
448 * Got a sample pair. Convert from the OLS's little-endian
449 * sample to the local format.
451 sample = devc->sample[0] | (devc->sample[1] << 8) \
452 | (devc->sample[2] << 16) | (devc->sample[3] << 24);
453 sr_spew("Received sample pair 0x%.*x.", devc->num_bytes * 2, sample);
456 * In RLE mode the high bit of the sample pair is the
457 * "count" flag, meaning this sample pair is the number
458 * of times the previous sample pair occurred.
460 if (devc->sample[devc->num_bytes - 1] & 0x80) {
461 /* Clear the high bit. */
462 sample &= ~(0x80 << (devc->num_bytes - 1) * 8);
463 devc->rle_count = sample;
464 devc->cnt_samples_rle += devc->rle_count * 2;
465 sr_dbg("RLE count: %u.", devc->rle_count * 2);
469 devc->num_samples += (devc->rle_count + 1) * 2;
470 if (devc->num_samples > devc->limit_samples) {
471 /* Save us from overrunning the buffer. */
472 devc->rle_count -= (devc->num_samples - devc->limit_samples) / 2;
473 devc->num_samples = devc->limit_samples;
478 * Some channel groups may have been turned
479 * off, to speed up transfer between the
480 * hardware and the PC. Expand that here before
481 * submitting it over the session bus --
482 * whatever is listening on the bus will be
483 * expecting a full 32-bit sample, based on
484 * the number of channels.
487 /* expand first sample */
488 memset(devc->tmp_sample, 0, 4);
489 for (i = 0; i < 2; i++) {
490 if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
492 * This channel group was
493 * enabled, copy from received
496 devc->tmp_sample[i] = devc->sample[j++];
499 /* Clear out the most significant bit of the sample */
500 devc->tmp_sample[devc->num_bytes - 1] &= 0x7f;
501 sr_spew("Expanded sample 1: 0x%.2x%.2x%.2x%.2x.",
502 devc->tmp_sample[3], devc->tmp_sample[2],
503 devc->tmp_sample[1], devc->tmp_sample[0]);
505 /* expand second sample */
506 memset(devc->tmp_sample2, 0, 4);
507 for (i = 0; i < 2; i++) {
508 if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
510 * This channel group was
511 * enabled, copy from received
514 devc->tmp_sample2[i] = devc->sample[j++];
517 /* Clear out the most significant bit of the sample */
518 devc->tmp_sample2[devc->num_bytes - 1] &= 0x7f;
519 sr_spew("Expanded sample 2: 0x%.2x%.2x%.2x%.2x.",
520 devc->tmp_sample2[3], devc->tmp_sample2[2],
521 devc->tmp_sample2[1], devc->tmp_sample2[0]);
524 * OLS sends its sample buffer backwards.
525 * store it in reverse order here, so we can dump
526 * this on the session bus later.
528 offset = (devc->limit_samples - devc->num_samples) * 4;
529 for (i = 0; i <= devc->rle_count; i++) {
530 memcpy(devc->raw_sample_buf + offset + (i * 8),
531 devc->tmp_sample2, 4);
532 memcpy(devc->raw_sample_buf + offset + (4 + (i * 8)),
533 devc->tmp_sample, 4);
535 memset(devc->sample, 0, 4);
541 if (devc->num_bytes == num_channels) {
543 devc->cnt_samples_rle++;
545 * Got a full sample. Convert from the OLS's little-endian
546 * sample to the local format.
548 sample = devc->sample[0] | (devc->sample[1] << 8) \
549 | (devc->sample[2] << 16) | (devc->sample[3] << 24);
550 sr_spew("Received sample 0x%.*x.", devc->num_bytes * 2, sample);
551 if (devc->flag_reg & FLAG_RLE) {
553 * In RLE mode the high bit of the sample is the
554 * "count" flag, meaning this sample is the number
555 * of times the previous sample occurred.
557 if (devc->sample[devc->num_bytes - 1] & 0x80) {
558 /* Clear the high bit. */
559 sample &= ~(0x80 << (devc->num_bytes - 1) * 8);
560 devc->rle_count = sample;
561 devc->cnt_samples_rle += devc->rle_count;
562 sr_dbg("RLE count: %u.", devc->rle_count);
567 devc->num_samples += devc->rle_count + 1;
568 if (devc->num_samples > devc->limit_samples) {
569 /* Save us from overrunning the buffer. */
570 devc->rle_count -= devc->num_samples - devc->limit_samples;
571 devc->num_samples = devc->limit_samples;
575 if (num_channels < 4) {
577 * Some channel groups may have been turned
578 * off, to speed up transfer between the
579 * hardware and the PC. Expand that here before
580 * submitting it over the session bus --
581 * whatever is listening on the bus will be
582 * expecting a full 32-bit sample, based on
583 * the number of channels.
586 memset(devc->tmp_sample, 0, 4);
587 for (i = 0; i < 4; i++) {
588 if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
590 * This channel group was
591 * enabled, copy from received
594 devc->tmp_sample[i] = devc->sample[j++];
597 memcpy(devc->sample, devc->tmp_sample, 4);
598 sr_spew("Expanded sample: 0x%.8x.", sample);
602 * Pipistrello OLS sends its sample buffer backwards.
603 * store it in reverse order here, so we can dump
604 * this on the session bus later.
606 offset = (devc->limit_samples - devc->num_samples) * 4;
607 for (i = 0; i <= devc->rle_count; i++) {
608 memcpy(devc->raw_sample_buf + offset + (i * 4),
611 memset(devc->sample, 0, 4);
620 bytes_read = ftdi_read_data(devc->ftdic, devc->ftdi_buf, FTDI_BUF_SIZE);
621 } while (bytes_read > 0);
624 * We've acquired all the samples we asked for -- we're done.
625 * Send the (properly-ordered) buffer to the frontend.
627 sr_dbg("Received %d bytes, %d samples, %d decompressed samples.",
628 devc->cnt_bytes, devc->cnt_samples,
629 devc->cnt_samples_rle);
630 if (devc->trigger_at != -1) {
632 * A trigger was set up, so we need to tell the frontend
635 if (devc->trigger_at > 0) {
636 /* There are pre-trigger samples, send those first. */
637 packet.type = SR_DF_LOGIC;
638 packet.payload = &logic;
639 logic.length = devc->trigger_at * 4;
641 logic.data = devc->raw_sample_buf +
642 (devc->limit_samples - devc->num_samples) * 4;
643 sr_session_send(sdi, &packet);
646 /* Send the trigger. */
647 packet.type = SR_DF_TRIGGER;
648 sr_session_send(sdi, &packet);
650 /* Send post-trigger samples. */
651 packet.type = SR_DF_LOGIC;
652 packet.payload = &logic;
653 logic.length = (devc->num_samples * 4) - (devc->trigger_at * 4);
655 logic.data = devc->raw_sample_buf + devc->trigger_at * 4 +
656 (devc->limit_samples - devc->num_samples) * 4;
657 sr_session_send(sdi, &packet);
659 /* no trigger was used */
660 packet.type = SR_DF_LOGIC;
661 packet.payload = &logic;
662 logic.length = devc->num_samples * 4;
664 logic.data = devc->raw_sample_buf +
665 (devc->limit_samples - devc->num_samples) * 4;
666 sr_session_send(sdi, &packet);
668 g_free(devc->raw_sample_buf);
670 sr_dev_acquisition_stop(sdi);