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/>.
22 extern SR_PRIV struct sr_dev_driver p_ols_driver_info;
24 SR_PRIV int write_shortcommand(struct dev_context *devc, uint8_t command)
29 sr_dbg("Sending cmd 0x%.2x.", command);
31 bytes_written = ftdi_write_data(devc->ftdic, buf, 1);
32 if (bytes_written < 0) {
33 sr_err("Failed to write FTDI data (%d): %s.",
34 bytes_written, ftdi_get_error_string(devc->ftdic));
36 } else if (bytes_written != 1) {
37 sr_err("FTDI write error, only %d/%d bytes written: %s.",
38 bytes_written, 1, ftdi_get_error_string(devc->ftdic));
45 SR_PRIV int write_longcommand(struct dev_context *devc, 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 bytes_written = ftdi_write_data(devc->ftdic, buf, 5);
58 if (bytes_written < 0) {
59 sr_err("Failed to write FTDI data (%d): %s.",
60 bytes_written, ftdi_get_error_string(devc->ftdic));
62 } else if (bytes_written != 5) {
63 sr_err("FTDI write error, only %d/%d bytes written: %s.",
64 bytes_written, 1, ftdi_get_error_string(devc->ftdic));
71 SR_PRIV int p_ols_open(struct dev_context *devc)
75 /* Note: Caller checks devc and devc->ftdic. */
77 /* Select interface B, otherwise communication will fail. */
78 ret = ftdi_set_interface(devc->ftdic, INTERFACE_B);
80 sr_err("Failed to set FTDI interface B (%d): %s", ret,
81 ftdi_get_error_string(devc->ftdic));
84 sr_dbg("FTDI chip interface B set successfully.");
86 /* Check for the device and temporarily open it. */
87 ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID, USB_DEVICE_ID,
90 /* Log errors, except for -3 ("device not found"). */
92 sr_err("Failed to open device (%d): %s", ret,
93 ftdi_get_error_string(devc->ftdic));
96 sr_dbg("FTDI device opened successfully.");
98 /* Purge RX/TX buffers in the FTDI chip. */
99 if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) {
100 sr_err("Failed to purge FTDI RX/TX buffers (%d): %s.",
101 ret, ftdi_get_error_string(devc->ftdic));
102 goto err_open_close_ftdic;
104 sr_dbg("FTDI chip buffers purged successfully.");
106 /* Reset the FTDI bitmode. */
107 ret = ftdi_set_bitmode(devc->ftdic, 0xff, BITMODE_RESET);
109 sr_err("Failed to reset the FTDI chip bitmode (%d): %s.",
110 ret, ftdi_get_error_string(devc->ftdic));
111 goto err_open_close_ftdic;
113 sr_dbg("FTDI chip bitmode reset successfully.");
115 /* Set the FTDI latency timer to 16. */
116 ret = ftdi_set_latency_timer(devc->ftdic, 16);
118 sr_err("Failed to set FTDI latency timer (%d): %s.",
119 ret, ftdi_get_error_string(devc->ftdic));
120 goto err_open_close_ftdic;
122 sr_dbg("FTDI chip latency timer set successfully.");
124 /* Set the FTDI read data chunk size to 64kB. */
125 ret = ftdi_read_data_set_chunksize(devc->ftdic, 64 * 1024);
127 sr_err("Failed to set FTDI read data chunk size (%d): %s.",
128 ret, ftdi_get_error_string(devc->ftdic));
129 goto err_open_close_ftdic;
131 sr_dbg("FTDI chip read data chunk size set successfully.");
135 err_open_close_ftdic:
136 ftdi_usb_close(devc->ftdic);
140 SR_PRIV int p_ols_close(struct dev_context *devc)
144 /* Note: Caller checks devc and devc->ftdic. */
146 if ((ret = ftdi_usb_close(devc->ftdic)) < 0) {
147 sr_err("Failed to close FTDI device (%d): %s.",
148 ret, ftdi_get_error_string(devc->ftdic));
155 /* Configures the channel mask based on which channels are enabled. */
156 SR_PRIV void pols_channel_mask(const struct sr_dev_inst *sdi)
158 struct dev_context *devc;
159 struct sr_channel *channel;
164 devc->channel_mask = 0;
165 for (l = sdi->channels; l; l = l->next) {
167 if (channel->enabled)
168 devc->channel_mask |= 1 << channel->index;
172 SR_PRIV int pols_convert_trigger(const struct sr_dev_inst *sdi)
174 struct dev_context *devc;
175 struct sr_trigger *trigger;
176 struct sr_trigger_stage *stage;
177 struct sr_trigger_match *match;
183 devc->num_stages = 0;
184 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
185 devc->trigger_mask[i] = 0;
186 devc->trigger_value[i] = 0;
187 devc->trigger_edge[i] = 0;
190 if (!(trigger = sr_session_trigger_get(sdi->session)))
193 devc->num_stages = g_slist_length(trigger->stages);
194 if (devc->num_stages > NUM_TRIGGER_STAGES) {
195 sr_err("This device only supports %d trigger stages.",
200 for (l = trigger->stages; l; l = l->next) {
202 for (m = stage->matches; m; m = m->next) {
204 if (!match->channel->enabled)
205 /* Ignore disabled channels with a trigger. */
207 devc->trigger_mask[stage->stage] |= 1 << match->channel->index;
208 if (match->match == SR_TRIGGER_ONE || match->match == SR_TRIGGER_RISING)
209 devc->trigger_value[stage->stage] |= 1 << match->channel->index;
210 if (match->match == SR_TRIGGER_RISING || match->match == SR_TRIGGER_FALLING)
211 devc->trigger_edge[stage->stage] |= 1 << match->channel->index;
218 SR_PRIV struct sr_dev_inst *p_ols_get_metadata(uint8_t *buf, int bytes_read, struct dev_context *devc)
220 struct sr_dev_inst *sdi;
221 uint32_t tmp_int, ui;
222 uint8_t key, type, token;
223 GString *tmp_str, *devname, *version;
227 sdi = g_malloc0(sizeof(struct sr_dev_inst));
228 sdi->status = SR_ST_INACTIVE;
229 sdi->driver = &p_ols_driver_info;
232 devname = g_string_new("");
233 version = g_string_new("");
236 while (index < bytes_read) {
239 sr_dbg("Got metadata key 0x00, metadata ends.");
246 /* NULL-terminated string */
247 tmp_str = g_string_new("");
248 while ((index < bytes_read) && ((tmp_c = buf[index++]) != '\0'))
249 g_string_append_c(tmp_str, tmp_c);
250 sr_dbg("Got metadata key 0x%.2x value '%s'.",
255 devname = g_string_append(devname, tmp_str->str);
258 /* FPGA firmware version */
260 g_string_append(version, ", ");
261 g_string_append(version, "FPGA version ");
262 g_string_append(version, tmp_str->str);
265 /* Ancillary version */
267 g_string_append(version, ", ");
268 g_string_append(version, "Ancillary version ");
269 g_string_append(version, tmp_str->str);
272 sr_info("Unknown token 0x%.2x: '%s'",
273 token, tmp_str->str);
276 g_string_free(tmp_str, TRUE);
279 /* 32-bit unsigned integer */
281 for (i = 0; i < 4; i++) {
282 tmp_int = (tmp_int << 8) | buf[index++];
284 sr_dbg("Got metadata key 0x%.2x value 0x%.8x.",
288 /* Number of usable channels */
289 for (ui = 0; ui < tmp_int; ui++)
290 sr_channel_new(sdi, ui, SR_CHANNEL_LOGIC, TRUE,
291 p_ols_channel_names[ui]);
294 /* Amount of sample memory available (bytes) */
295 devc->max_samplebytes = tmp_int;
298 /* Amount of dynamic memory available (bytes) */
299 /* what is this for? */
302 /* Maximum sample rate (hz) */
303 devc->max_samplerate = tmp_int;
306 /* protocol version */
307 devc->protocol_version = tmp_int;
310 sr_info("Unknown token 0x%.2x: 0x%.8x.",
316 /* 8-bit unsigned integer */
317 tmp_c = buf[index++];
318 sr_dbg("Got metadata key 0x%.2x value 0x%.2x.",
322 /* Number of usable channels */
323 for (ui = 0; ui < tmp_c; ui++)
324 sr_channel_new(sdi, ui, SR_CHANNEL_LOGIC, TRUE,
325 p_ols_channel_names[ui]);
328 /* protocol version */
329 devc->protocol_version = tmp_c;
332 sr_info("Unknown token 0x%.2x: 0x%.2x.",
343 sdi->model = devname->str;
344 sdi->version = version->str;
345 g_string_free(devname, FALSE);
346 g_string_free(version, FALSE);
351 SR_PRIV int p_ols_set_samplerate(const struct sr_dev_inst *sdi,
352 const uint64_t samplerate)
354 struct dev_context *devc;
357 if (devc->max_samplerate && samplerate > devc->max_samplerate)
358 return SR_ERR_SAMPLERATE;
360 if (samplerate > CLOCK_RATE) {
361 sr_info("Enabling demux mode.");
362 devc->flag_reg |= FLAG_DEMUX;
363 devc->flag_reg &= ~FLAG_FILTER;
364 devc->max_channels = NUM_CHANNELS / 2;
365 devc->cur_samplerate_divider = (CLOCK_RATE * 2 / samplerate) - 1;
367 sr_info("Disabling demux mode.");
368 devc->flag_reg &= ~FLAG_DEMUX;
369 devc->flag_reg |= FLAG_FILTER;
370 devc->max_channels = NUM_CHANNELS;
371 devc->cur_samplerate_divider = (CLOCK_RATE / samplerate) - 1;
374 /* Calculate actual samplerate used and complain if it is different
375 * from the requested.
377 devc->cur_samplerate = CLOCK_RATE / (devc->cur_samplerate_divider + 1);
378 if (devc->flag_reg & FLAG_DEMUX)
379 devc->cur_samplerate *= 2;
380 if (devc->cur_samplerate != samplerate)
381 sr_info("Can't match samplerate %" PRIu64 ", using %"
382 PRIu64 ".", samplerate, devc->cur_samplerate);
388 SR_PRIV int p_ols_receive_data(int fd, int revents, void *cb_data)
390 struct dev_context *devc;
391 struct sr_dev_inst *sdi;
392 struct sr_datafeed_packet packet;
393 struct sr_datafeed_logic logic;
395 int num_channels, offset, j;
396 int bytes_read, index;
406 if (devc->num_transfers++ == 0) {
407 devc->raw_sample_buf = g_try_malloc(devc->limit_samples * 4);
408 if (!devc->raw_sample_buf) {
409 sr_err("Sample buffer malloc failed.");
412 /* fill with 1010... for debugging */
413 memset(devc->raw_sample_buf, 0x82, devc->limit_samples * 4);
416 if ((devc->num_samples < devc->limit_samples) && (devc->cnt_samples < devc->max_samples)) {
419 for (i = NUM_CHANNELS; i > 0x02; i /= 2) {
420 if ((devc->flag_reg & i) == 0) {
425 /* Get a block of data. */
426 bytes_read = ftdi_read_data(devc->ftdic, devc->ftdi_buf, FTDI_BUF_SIZE);
427 if (bytes_read < 0) {
428 sr_err("Failed to read FTDI data (%d): %s.",
429 bytes_read, ftdi_get_error_string(devc->ftdic));
430 sdi->driver->dev_acquisition_stop(sdi, sdi);
433 if (bytes_read == 0) {
434 sr_spew("Received 0 bytes, nothing to do.");
438 sr_dbg("Received %d bytes", bytes_read);
441 while (index < bytes_read) {
442 byte = devc->ftdi_buf[index++];
445 devc->sample[devc->num_bytes++] = byte;
446 sr_spew("Received byte 0x%.2x.", byte);
448 if ((devc->flag_reg & FLAG_DEMUX) && (devc->flag_reg & FLAG_RLE)) {
449 /* RLE in demux mode must be processed differently
450 * since in this case the RLE encoder is operating on pairs of samples.
452 if (devc->num_bytes == num_channels * 2) {
453 devc->cnt_samples += 2;
454 devc->cnt_samples_rle += 2;
456 * Got a sample pair. Convert from the OLS's little-endian
457 * sample to the local format.
459 sample = devc->sample[0] | (devc->sample[1] << 8) \
460 | (devc->sample[2] << 16) | (devc->sample[3] << 24);
461 sr_spew("Received sample pair 0x%.*x.", devc->num_bytes * 2, sample);
464 * In RLE mode the high bit of the sample pair is the
465 * "count" flag, meaning this sample pair is the number
466 * of times the previous sample pair occurred.
468 if (devc->sample[devc->num_bytes - 1] & 0x80) {
469 /* Clear the high bit. */
470 sample &= ~(0x80 << (devc->num_bytes - 1) * 8);
471 devc->rle_count = sample;
472 devc->cnt_samples_rle += devc->rle_count * 2;
473 sr_dbg("RLE count: %u.", devc->rle_count * 2);
477 devc->num_samples += (devc->rle_count + 1) * 2;
478 if (devc->num_samples > devc->limit_samples) {
479 /* Save us from overrunning the buffer. */
480 devc->rle_count -= (devc->num_samples - devc->limit_samples) / 2;
481 devc->num_samples = devc->limit_samples;
486 * Some channel groups may have been turned
487 * off, to speed up transfer between the
488 * hardware and the PC. Expand that here before
489 * submitting it over the session bus --
490 * whatever is listening on the bus will be
491 * expecting a full 32-bit sample, based on
492 * the number of channels.
495 /* expand first sample */
496 memset(devc->tmp_sample, 0, 4);
497 for (i = 0; i < 2; i++) {
498 if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
500 * This channel group was
501 * enabled, copy from received
504 devc->tmp_sample[i] = devc->sample[j++];
507 /* Clear out the most significant bit of the sample */
508 devc->tmp_sample[devc->num_bytes - 1] &= 0x7f;
509 sr_spew("Expanded sample 1: 0x%.8x.", devc->tmp_sample);
511 /* expand second sample */
512 memset(devc->tmp_sample2, 0, 4);
513 for (i = 0; i < 2; i++) {
514 if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
516 * This channel group was
517 * enabled, copy from received
520 devc->tmp_sample2[i] = devc->sample[j++];
523 /* Clear out the most significant bit of the sample */
524 devc->tmp_sample2[devc->num_bytes - 1] &= 0x7f;
525 sr_spew("Expanded sample 2: 0x%.8x.", devc->tmp_sample2);
528 * OLS sends its sample buffer backwards.
529 * store it in reverse order here, so we can dump
530 * this on the session bus later.
532 offset = (devc->limit_samples - devc->num_samples) * 4;
533 for (i = 0; i <= devc->rle_count; i++) {
534 memcpy(devc->raw_sample_buf + offset + (i * 8),
535 devc->tmp_sample2, 4);
536 memcpy(devc->raw_sample_buf + offset + (4 + (i * 8)),
537 devc->tmp_sample, 4);
539 memset(devc->sample, 0, 4);
545 if (devc->num_bytes == num_channels) {
547 devc->cnt_samples_rle++;
549 * Got a full sample. Convert from the OLS's little-endian
550 * sample to the local format.
552 sample = devc->sample[0] | (devc->sample[1] << 8) \
553 | (devc->sample[2] << 16) | (devc->sample[3] << 24);
554 sr_spew("Received sample 0x%.*x.", devc->num_bytes * 2, sample);
555 if (devc->flag_reg & FLAG_RLE) {
557 * In RLE mode the high bit of the sample is the
558 * "count" flag, meaning this sample is the number
559 * of times the previous sample occurred.
561 if (devc->sample[devc->num_bytes - 1] & 0x80) {
562 /* Clear the high bit. */
563 sample &= ~(0x80 << (devc->num_bytes - 1) * 8);
564 devc->rle_count = sample;
565 devc->cnt_samples_rle += devc->rle_count;
566 sr_dbg("RLE count: %u.", devc->rle_count);
571 devc->num_samples += devc->rle_count + 1;
572 if (devc->num_samples > devc->limit_samples) {
573 /* Save us from overrunning the buffer. */
574 devc->rle_count -= devc->num_samples - devc->limit_samples;
575 devc->num_samples = devc->limit_samples;
579 if (num_channels < 4) {
581 * Some channel groups may have been turned
582 * off, to speed up transfer between the
583 * hardware and the PC. Expand that here before
584 * submitting it over the session bus --
585 * whatever is listening on the bus will be
586 * expecting a full 32-bit sample, based on
587 * the number of channels.
590 memset(devc->tmp_sample, 0, 4);
591 for (i = 0; i < 4; i++) {
592 if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
594 * This channel group was
595 * enabled, copy from received
598 devc->tmp_sample[i] = devc->sample[j++];
601 memcpy(devc->sample, devc->tmp_sample, 4);
602 sr_spew("Expanded sample: 0x%.8x.", sample);
606 * Pipistrello OLS sends its sample buffer backwards.
607 * store it in reverse order here, so we can dump
608 * this on the session bus later.
610 offset = (devc->limit_samples - devc->num_samples) * 4;
611 for (i = 0; i <= devc->rle_count; i++) {
612 memcpy(devc->raw_sample_buf + offset + (i * 4),
615 memset(devc->sample, 0, 4);
623 do bytes_read = ftdi_read_data(devc->ftdic, devc->ftdi_buf, FTDI_BUF_SIZE);
624 while (bytes_read > 0);
627 * We've acquired all the samples we asked for -- we're done.
628 * Send the (properly-ordered) buffer to the frontend.
630 sr_dbg("Received %d bytes, %d samples, %d decompressed samples.",
631 devc->cnt_bytes, devc->cnt_samples,
632 devc->cnt_samples_rle);
633 if (devc->trigger_at != -1) {
635 * A trigger was set up, so we need to tell the frontend
638 if (devc->trigger_at > 0) {
639 /* There are pre-trigger samples, send those first. */
640 packet.type = SR_DF_LOGIC;
641 packet.payload = &logic;
642 logic.length = devc->trigger_at * 4;
644 logic.data = devc->raw_sample_buf +
645 (devc->limit_samples - devc->num_samples) * 4;
646 sr_session_send(cb_data, &packet);
649 /* Send the trigger. */
650 packet.type = SR_DF_TRIGGER;
651 sr_session_send(cb_data, &packet);
653 /* Send post-trigger samples. */
654 packet.type = SR_DF_LOGIC;
655 packet.payload = &logic;
656 logic.length = (devc->num_samples * 4) - (devc->trigger_at * 4);
658 logic.data = devc->raw_sample_buf + devc->trigger_at * 4 +
659 (devc->limit_samples - devc->num_samples) * 4;
660 sr_session_send(cb_data, &packet);
662 /* no trigger was used */
663 packet.type = SR_DF_LOGIC;
664 packet.payload = &logic;
665 logic.length = devc->num_samples * 4;
667 logic.data = devc->raw_sample_buf +
668 (devc->limit_samples - devc->num_samples) * 4;
669 sr_session_send(cb_data, &packet);
671 g_free(devc->raw_sample_buf);
673 sdi->driver->dev_acquisition_stop(sdi, cb_data);