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);
387 SR_PRIV int p_ols_receive_data(int fd, int revents, void *cb_data)
389 struct dev_context *devc;
390 struct sr_dev_inst *sdi;
391 struct sr_datafeed_packet packet;
392 struct sr_datafeed_logic logic;
394 int num_channels, offset, j;
395 int bytes_read, index;
405 if (devc->num_transfers++ == 0) {
406 devc->raw_sample_buf = g_try_malloc(devc->limit_samples * 4);
407 if (!devc->raw_sample_buf) {
408 sr_err("Sample buffer malloc failed.");
411 /* fill with 1010... for debugging */
412 memset(devc->raw_sample_buf, 0x82, devc->limit_samples * 4);
415 if ((devc->num_samples < devc->limit_samples) && (devc->cnt_samples < devc->max_samples)) {
418 for (i = NUM_CHANNELS; i > 0x02; i /= 2) {
419 if ((devc->flag_reg & i) == 0) {
424 /* Get a block of data. */
425 bytes_read = ftdi_read_data(devc->ftdic, devc->ftdi_buf, FTDI_BUF_SIZE);
426 if (bytes_read < 0) {
427 sr_err("Failed to read FTDI data (%d): %s.",
428 bytes_read, ftdi_get_error_string(devc->ftdic));
429 sdi->driver->dev_acquisition_stop(sdi, sdi);
432 if (bytes_read == 0) {
433 sr_spew("Received 0 bytes, nothing to do.");
437 sr_dbg("Received %d bytes", bytes_read);
440 while (index < bytes_read) {
441 byte = devc->ftdi_buf[index++];
444 devc->sample[devc->num_bytes++] = byte;
445 sr_spew("Received byte 0x%.2x.", byte);
447 if ((devc->flag_reg & FLAG_DEMUX) && (devc->flag_reg & FLAG_RLE)) {
448 /* RLE in demux mode must be processed differently
449 * since in this case the RLE encoder is operating on pairs of samples.
451 if (devc->num_bytes == num_channels * 2) {
452 devc->cnt_samples += 2;
453 devc->cnt_samples_rle += 2;
455 * Got a sample pair. Convert from the OLS's little-endian
456 * sample to the local format.
458 sample = devc->sample[0] | (devc->sample[1] << 8) \
459 | (devc->sample[2] << 16) | (devc->sample[3] << 24);
460 sr_spew("Received sample pair 0x%.*x.", devc->num_bytes * 2, sample);
463 * In RLE mode the high bit of the sample pair is the
464 * "count" flag, meaning this sample pair is the number
465 * of times the previous sample pair occurred.
467 if (devc->sample[devc->num_bytes - 1] & 0x80) {
468 /* Clear the high bit. */
469 sample &= ~(0x80 << (devc->num_bytes - 1) * 8);
470 devc->rle_count = sample;
471 devc->cnt_samples_rle += devc->rle_count * 2;
472 sr_dbg("RLE count: %u.", devc->rle_count * 2);
476 devc->num_samples += (devc->rle_count + 1) * 2;
477 if (devc->num_samples > devc->limit_samples) {
478 /* Save us from overrunning the buffer. */
479 devc->rle_count -= (devc->num_samples - devc->limit_samples) / 2;
480 devc->num_samples = devc->limit_samples;
485 * Some channel groups may have been turned
486 * off, to speed up transfer between the
487 * hardware and the PC. Expand that here before
488 * submitting it over the session bus --
489 * whatever is listening on the bus will be
490 * expecting a full 32-bit sample, based on
491 * the number of channels.
494 /* expand first sample */
495 memset(devc->tmp_sample, 0, 4);
496 for (i = 0; i < 2; i++) {
497 if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
499 * This channel group was
500 * enabled, copy from received
503 devc->tmp_sample[i] = devc->sample[j++];
506 /* Clear out the most significant bit of the sample */
507 devc->tmp_sample[devc->num_bytes - 1] &= 0x7f;
508 sr_spew("Expanded sample 1: 0x%.8x.", devc->tmp_sample);
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%.8x.", devc->tmp_sample2);
527 * OLS sends its sample buffer backwards.
528 * store it in reverse order here, so we can dump
529 * this on the session bus later.
531 offset = (devc->limit_samples - devc->num_samples) * 4;
532 for (i = 0; i <= devc->rle_count; i++) {
533 memcpy(devc->raw_sample_buf + offset + (i * 8),
534 devc->tmp_sample2, 4);
535 memcpy(devc->raw_sample_buf + offset + (4 + (i * 8)),
536 devc->tmp_sample, 4);
538 memset(devc->sample, 0, 4);
544 if (devc->num_bytes == num_channels) {
546 devc->cnt_samples_rle++;
548 * Got a full sample. Convert from the OLS's little-endian
549 * sample to the local format.
551 sample = devc->sample[0] | (devc->sample[1] << 8) \
552 | (devc->sample[2] << 16) | (devc->sample[3] << 24);
553 sr_spew("Received sample 0x%.*x.", devc->num_bytes * 2, sample);
554 if (devc->flag_reg & FLAG_RLE) {
556 * In RLE mode the high bit of the sample is the
557 * "count" flag, meaning this sample is the number
558 * of times the previous sample occurred.
560 if (devc->sample[devc->num_bytes - 1] & 0x80) {
561 /* Clear the high bit. */
562 sample &= ~(0x80 << (devc->num_bytes - 1) * 8);
563 devc->rle_count = sample;
564 devc->cnt_samples_rle += devc->rle_count;
565 sr_dbg("RLE count: %u.", devc->rle_count);
570 devc->num_samples += devc->rle_count + 1;
571 if (devc->num_samples > devc->limit_samples) {
572 /* Save us from overrunning the buffer. */
573 devc->rle_count -= devc->num_samples - devc->limit_samples;
574 devc->num_samples = devc->limit_samples;
578 if (num_channels < 4) {
580 * Some channel groups may have been turned
581 * off, to speed up transfer between the
582 * hardware and the PC. Expand that here before
583 * submitting it over the session bus --
584 * whatever is listening on the bus will be
585 * expecting a full 32-bit sample, based on
586 * the number of channels.
589 memset(devc->tmp_sample, 0, 4);
590 for (i = 0; i < 4; i++) {
591 if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
593 * This channel group was
594 * enabled, copy from received
597 devc->tmp_sample[i] = devc->sample[j++];
600 memcpy(devc->sample, devc->tmp_sample, 4);
601 sr_spew("Expanded sample: 0x%.8x.", sample);
605 * Pipistrello OLS sends its sample buffer backwards.
606 * store it in reverse order here, so we can dump
607 * this on the session bus later.
609 offset = (devc->limit_samples - devc->num_samples) * 4;
610 for (i = 0; i <= devc->rle_count; i++) {
611 memcpy(devc->raw_sample_buf + offset + (i * 4),
614 memset(devc->sample, 0, 4);
622 do bytes_read = ftdi_read_data(devc->ftdic, devc->ftdi_buf, FTDI_BUF_SIZE);
623 while (bytes_read > 0);
626 * We've acquired all the samples we asked for -- we're done.
627 * Send the (properly-ordered) buffer to the frontend.
629 sr_dbg("Received %d bytes, %d samples, %d decompressed samples.",
630 devc->cnt_bytes, devc->cnt_samples,
631 devc->cnt_samples_rle);
632 if (devc->trigger_at != -1) {
634 * A trigger was set up, so we need to tell the frontend
637 if (devc->trigger_at > 0) {
638 /* There are pre-trigger samples, send those first. */
639 packet.type = SR_DF_LOGIC;
640 packet.payload = &logic;
641 logic.length = devc->trigger_at * 4;
643 logic.data = devc->raw_sample_buf +
644 (devc->limit_samples - devc->num_samples) * 4;
645 sr_session_send(cb_data, &packet);
648 /* Send the trigger. */
649 packet.type = SR_DF_TRIGGER;
650 sr_session_send(cb_data, &packet);
652 /* Send post-trigger samples. */
653 packet.type = SR_DF_LOGIC;
654 packet.payload = &logic;
655 logic.length = (devc->num_samples * 4) - (devc->trigger_at * 4);
657 logic.data = devc->raw_sample_buf + devc->trigger_at * 4 +
658 (devc->limit_samples - devc->num_samples) * 4;
659 sr_session_send(cb_data, &packet);
661 /* no trigger was used */
662 packet.type = SR_DF_LOGIC;
663 packet.payload = &logic;
664 logic.length = devc->num_samples * 4;
666 logic.data = devc->raw_sample_buf +
667 (devc->limit_samples - devc->num_samples) * 4;
668 sr_session_send(cb_data, &packet);
670 g_free(devc->raw_sample_buf);
672 sdi->driver->dev_acquisition_stop(sdi, cb_data);