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;
23 static struct sr_dev_driver *di = &p_ols_driver_info;
25 SR_PRIV int write_shortcommand(struct dev_context *devc, uint8_t command)
30 sr_dbg("Sending cmd 0x%.2x.", command);
32 bytes_written = ftdi_write_data(devc->ftdic, buf, 1);
33 if (bytes_written < 0) {
34 sr_err("Failed to write FTDI data (%d): %s.",
35 bytes_written, ftdi_get_error_string(devc->ftdic));
37 } else if (bytes_written != 1) {
38 sr_err("FTDI write error, only %d/%d bytes written: %s.",
39 bytes_written, 1, ftdi_get_error_string(devc->ftdic));
46 SR_PRIV int write_longcommand(struct dev_context *devc, uint8_t command, uint8_t *data)
51 sr_dbg("Sending cmd 0x%.2x data 0x%.2x%.2x%.2x%.2x.", command,
52 data[0], data[1], data[2], data[3]);
58 bytes_written = ftdi_write_data(devc->ftdic, buf, 5);
59 if (bytes_written < 0) {
60 sr_err("Failed to write FTDI data (%d): %s.",
61 bytes_written, ftdi_get_error_string(devc->ftdic));
63 } else if (bytes_written != 5) {
64 sr_err("FTDI write error, only %d/%d bytes written: %s.",
65 bytes_written, 1, ftdi_get_error_string(devc->ftdic));
72 SR_PRIV int p_ols_open(struct dev_context *devc)
76 /* Note: Caller checks devc and devc->ftdic. */
78 /* Select interface B, otherwise communication will fail. */
79 ret = ftdi_set_interface(devc->ftdic, INTERFACE_B);
81 sr_err("Failed to set FTDI interface B (%d): %s", ret,
82 ftdi_get_error_string(devc->ftdic));
85 sr_dbg("FTDI chip interface B set successfully.");
87 /* Check for the device and temporarily open it. */
88 ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID, USB_DEVICE_ID,
91 /* Log errors, except for -3 ("device not found"). */
93 sr_err("Failed to open device (%d): %s", ret,
94 ftdi_get_error_string(devc->ftdic));
97 sr_dbg("FTDI device opened successfully.");
99 /* Purge RX/TX buffers in the FTDI chip. */
100 if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) {
101 sr_err("Failed to purge FTDI RX/TX buffers (%d): %s.",
102 ret, ftdi_get_error_string(devc->ftdic));
103 goto err_open_close_ftdic;
105 sr_dbg("FTDI chip buffers purged successfully.");
107 /* Reset the FTDI bitmode. */
108 ret = ftdi_set_bitmode(devc->ftdic, 0xff, BITMODE_RESET);
110 sr_err("Failed to reset the FTDI chip bitmode (%d): %s.",
111 ret, ftdi_get_error_string(devc->ftdic));
112 goto err_open_close_ftdic;
114 sr_dbg("FTDI chip bitmode reset successfully.");
116 /* Set the FTDI latency timer to 16. */
117 ret = ftdi_set_latency_timer(devc->ftdic, 16);
119 sr_err("Failed to set FTDI latency timer (%d): %s.",
120 ret, ftdi_get_error_string(devc->ftdic));
121 goto err_open_close_ftdic;
123 sr_dbg("FTDI chip latency timer set successfully.");
125 /* Set the FTDI read data chunk size to 64kB. */
126 ret = ftdi_read_data_set_chunksize(devc->ftdic, 64 * 1024);
128 sr_err("Failed to set FTDI read data chunk size (%d): %s.",
129 ret, ftdi_get_error_string(devc->ftdic));
130 goto err_open_close_ftdic;
132 sr_dbg("FTDI chip read data chunk size set successfully.");
136 err_open_close_ftdic:
137 ftdi_usb_close(devc->ftdic);
141 SR_PRIV int p_ols_close(struct dev_context *devc)
145 /* Note: Caller checks devc and devc->ftdic. */
147 if ((ret = ftdi_usb_close(devc->ftdic)) < 0) {
148 sr_err("Failed to close FTDI device (%d): %s.",
149 ret, ftdi_get_error_string(devc->ftdic));
156 /* Configures the channel mask based on which channels are enabled. */
157 SR_PRIV void pols_channel_mask(const struct sr_dev_inst *sdi)
159 struct dev_context *devc;
160 struct sr_channel *channel;
165 devc->channel_mask = 0;
166 for (l = sdi->channels; l; l = l->next) {
168 if (channel->enabled)
169 devc->channel_mask |= 1 << channel->index;
173 SR_PRIV int pols_convert_trigger(const struct sr_dev_inst *sdi)
175 struct dev_context *devc;
176 struct sr_trigger *trigger;
177 struct sr_trigger_stage *stage;
178 struct sr_trigger_match *match;
184 devc->num_stages = 0;
185 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
186 devc->trigger_mask[i] = 0;
187 devc->trigger_value[i] = 0;
188 devc->trigger_edge[i] = 0;
191 if (!(trigger = sr_session_trigger_get(sdi->session)))
194 devc->num_stages = g_slist_length(trigger->stages);
195 if (devc->num_stages > NUM_TRIGGER_STAGES) {
196 sr_err("This device only supports %d trigger stages.",
201 for (l = trigger->stages; l; l = l->next) {
203 for (m = stage->matches; m; m = m->next) {
205 if (!match->channel->enabled)
206 /* Ignore disabled channels with a trigger. */
208 devc->trigger_mask[stage->stage] |= 1 << match->channel->index;
209 if (match->match == SR_TRIGGER_ONE || match->match == SR_TRIGGER_RISING)
210 devc->trigger_value[stage->stage] |= 1 << match->channel->index;
211 if (match->match == SR_TRIGGER_RISING || match->match == SR_TRIGGER_FALLING)
212 devc->trigger_edge[stage->stage] |= 1 << match->channel->index;
219 SR_PRIV struct sr_dev_inst *p_ols_get_metadata(uint8_t *buf, int bytes_read, struct dev_context *devc)
221 struct sr_dev_inst *sdi;
222 struct sr_channel *ch;
223 uint32_t tmp_int, ui;
224 uint8_t key, type, token;
225 GString *tmp_str, *devname, *version;
229 sdi = g_malloc0(sizeof(struct sr_dev_inst));
230 sdi->status = SR_ST_INACTIVE;
234 devname = g_string_new("");
235 version = g_string_new("");
238 while (index < bytes_read) {
241 sr_dbg("Got metadata key 0x00, metadata ends.");
248 /* NULL-terminated string */
249 tmp_str = g_string_new("");
250 while ((index < bytes_read) && ((tmp_c = buf[index++]) != '\0'))
251 g_string_append_c(tmp_str, tmp_c);
252 sr_dbg("Got metadata key 0x%.2x value '%s'.",
257 devname = g_string_append(devname, tmp_str->str);
260 /* FPGA firmware version */
262 g_string_append(version, ", ");
263 g_string_append(version, "FPGA version ");
264 g_string_append(version, tmp_str->str);
267 /* Ancillary version */
269 g_string_append(version, ", ");
270 g_string_append(version, "Ancillary version ");
271 g_string_append(version, tmp_str->str);
274 sr_info("Unknown token 0x%.2x: '%s'",
275 token, tmp_str->str);
278 g_string_free(tmp_str, TRUE);
281 /* 32-bit unsigned integer */
283 for (i = 0; i < 4; i++) {
284 tmp_int = (tmp_int << 8) | buf[index++];
286 sr_dbg("Got metadata key 0x%.2x value 0x%.8x.",
290 /* Number of usable channels */
291 for (ui = 0; ui < tmp_int; ui++) {
292 ch = sr_channel_new(ui, SR_CHANNEL_LOGIC, TRUE,
293 p_ols_channel_names[ui]);
294 sdi->channels = g_slist_append(sdi->channels, ch);
298 /* Amount of sample memory available (bytes) */
299 devc->max_samplebytes = tmp_int;
302 /* Amount of dynamic memory available (bytes) */
303 /* what is this for? */
306 /* Maximum sample rate (hz) */
307 devc->max_samplerate = tmp_int;
310 /* protocol version */
311 devc->protocol_version = tmp_int;
314 sr_info("Unknown token 0x%.2x: 0x%.8x.",
320 /* 8-bit unsigned integer */
321 tmp_c = buf[index++];
322 sr_dbg("Got metadata key 0x%.2x value 0x%.2x.",
326 /* Number of usable channels */
327 for (ui = 0; ui < tmp_c; ui++) {
328 ch = sr_channel_new(ui, SR_CHANNEL_LOGIC, TRUE,
329 p_ols_channel_names[ui]);
330 sdi->channels = g_slist_append(sdi->channels, ch);
334 /* protocol version */
335 devc->protocol_version = tmp_c;
338 sr_info("Unknown token 0x%.2x: 0x%.2x.",
349 sdi->model = devname->str;
350 sdi->version = version->str;
351 g_string_free(devname, FALSE);
352 g_string_free(version, FALSE);
357 SR_PRIV int p_ols_set_samplerate(const struct sr_dev_inst *sdi,
358 const uint64_t samplerate)
360 struct dev_context *devc;
363 if (devc->max_samplerate && samplerate > devc->max_samplerate)
364 return SR_ERR_SAMPLERATE;
366 if (samplerate > CLOCK_RATE) {
367 sr_info("Enabling demux mode.");
368 devc->flag_reg |= FLAG_DEMUX;
369 devc->flag_reg &= ~FLAG_FILTER;
370 devc->max_channels = NUM_CHANNELS / 2;
371 devc->cur_samplerate_divider = (CLOCK_RATE * 2 / samplerate) - 1;
373 sr_info("Disabling demux mode.");
374 devc->flag_reg &= ~FLAG_DEMUX;
375 devc->flag_reg |= FLAG_FILTER;
376 devc->max_channels = NUM_CHANNELS;
377 devc->cur_samplerate_divider = (CLOCK_RATE / samplerate) - 1;
380 /* Calculate actual samplerate used and complain if it is different
381 * from the requested.
383 devc->cur_samplerate = CLOCK_RATE / (devc->cur_samplerate_divider + 1);
384 if (devc->flag_reg & FLAG_DEMUX)
385 devc->cur_samplerate *= 2;
386 if (devc->cur_samplerate != samplerate)
387 sr_info("Can't match samplerate %" PRIu64 ", using %"
388 PRIu64 ".", samplerate, devc->cur_samplerate);
394 SR_PRIV int p_ols_receive_data(int fd, int revents, void *cb_data)
396 struct dev_context *devc;
397 struct sr_dev_inst *sdi;
398 struct sr_datafeed_packet packet;
399 struct sr_datafeed_logic logic;
401 int num_channels, offset, j;
402 int bytes_read, index;
412 if (devc->num_transfers++ == 0) {
413 devc->raw_sample_buf = g_try_malloc(devc->limit_samples * 4);
414 if (!devc->raw_sample_buf) {
415 sr_err("Sample buffer malloc failed.");
418 /* fill with 1010... for debugging */
419 memset(devc->raw_sample_buf, 0x82, devc->limit_samples * 4);
422 if ((devc->num_samples < devc->limit_samples) && (devc->cnt_samples < devc->max_samples)) {
425 for (i = NUM_CHANNELS; i > 0x02; i /= 2) {
426 if ((devc->flag_reg & i) == 0) {
431 /* Get a block of data. */
432 bytes_read = ftdi_read_data(devc->ftdic, devc->ftdi_buf, FTDI_BUF_SIZE);
433 if (bytes_read < 0) {
434 sr_err("Failed to read FTDI data (%d): %s.",
435 bytes_read, ftdi_get_error_string(devc->ftdic));
436 sdi->driver->dev_acquisition_stop(sdi, sdi);
439 if (bytes_read == 0) {
440 sr_spew("Received 0 bytes, nothing to do.");
444 sr_dbg("Received %d bytes", bytes_read);
447 while (index < bytes_read) {
448 byte = devc->ftdi_buf[index++];
451 devc->sample[devc->num_bytes++] = byte;
452 sr_spew("Received byte 0x%.2x.", byte);
454 if ((devc->flag_reg & FLAG_DEMUX) && (devc->flag_reg & FLAG_RLE)) {
455 /* RLE in demux mode must be processed differently
456 * since in this case the RLE encoder is operating on pairs of samples.
458 if (devc->num_bytes == num_channels * 2) {
459 devc->cnt_samples += 2;
460 devc->cnt_samples_rle += 2;
462 * Got a sample pair. Convert from the OLS's little-endian
463 * sample to the local format.
465 sample = devc->sample[0] | (devc->sample[1] << 8) \
466 | (devc->sample[2] << 16) | (devc->sample[3] << 24);
467 sr_spew("Received sample pair 0x%.*x.", devc->num_bytes * 2, sample);
470 * In RLE mode the high bit of the sample pair is the
471 * "count" flag, meaning this sample pair is the number
472 * of times the previous sample pair occurred.
474 if (devc->sample[devc->num_bytes - 1] & 0x80) {
475 /* Clear the high bit. */
476 sample &= ~(0x80 << (devc->num_bytes - 1) * 8);
477 devc->rle_count = sample;
478 devc->cnt_samples_rle += devc->rle_count * 2;
479 sr_dbg("RLE count: %u.", devc->rle_count * 2);
483 devc->num_samples += (devc->rle_count + 1) * 2;
484 if (devc->num_samples > devc->limit_samples) {
485 /* Save us from overrunning the buffer. */
486 devc->rle_count -= (devc->num_samples - devc->limit_samples) / 2;
487 devc->num_samples = devc->limit_samples;
492 * Some channel groups may have been turned
493 * off, to speed up transfer between the
494 * hardware and the PC. Expand that here before
495 * submitting it over the session bus --
496 * whatever is listening on the bus will be
497 * expecting a full 32-bit sample, based on
498 * the number of channels.
501 /* expand first sample */
502 memset(devc->tmp_sample, 0, 4);
503 for (i = 0; i < 2; i++) {
504 if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
506 * This channel group was
507 * enabled, copy from received
510 devc->tmp_sample[i] = devc->sample[j++];
513 /* Clear out the most significant bit of the sample */
514 devc->tmp_sample[devc->num_bytes - 1] &= 0x7f;
515 sr_spew("Expanded sample 1: 0x%.8x.", devc->tmp_sample);
517 /* expand second sample */
518 memset(devc->tmp_sample2, 0, 4);
519 for (i = 0; i < 2; i++) {
520 if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
522 * This channel group was
523 * enabled, copy from received
526 devc->tmp_sample2[i] = devc->sample[j++];
529 /* Clear out the most significant bit of the sample */
530 devc->tmp_sample2[devc->num_bytes - 1] &= 0x7f;
531 sr_spew("Expanded sample 2: 0x%.8x.", devc->tmp_sample2);
534 * OLS sends its sample buffer backwards.
535 * store it in reverse order here, so we can dump
536 * this on the session bus later.
538 offset = (devc->limit_samples - devc->num_samples) * 4;
539 for (i = 0; i <= devc->rle_count; i++) {
540 memcpy(devc->raw_sample_buf + offset + (i * 8),
541 devc->tmp_sample2, 4);
542 memcpy(devc->raw_sample_buf + offset + (4 + (i * 8)),
543 devc->tmp_sample, 4);
545 memset(devc->sample, 0, 4);
551 if (devc->num_bytes == num_channels) {
553 devc->cnt_samples_rle++;
555 * Got a full sample. Convert from the OLS's little-endian
556 * sample to the local format.
558 sample = devc->sample[0] | (devc->sample[1] << 8) \
559 | (devc->sample[2] << 16) | (devc->sample[3] << 24);
560 sr_spew("Received sample 0x%.*x.", devc->num_bytes * 2, sample);
561 if (devc->flag_reg & FLAG_RLE) {
563 * In RLE mode the high bit of the sample is the
564 * "count" flag, meaning this sample is the number
565 * of times the previous sample occurred.
567 if (devc->sample[devc->num_bytes - 1] & 0x80) {
568 /* Clear the high bit. */
569 sample &= ~(0x80 << (devc->num_bytes - 1) * 8);
570 devc->rle_count = sample;
571 devc->cnt_samples_rle += devc->rle_count;
572 sr_dbg("RLE count: %u.", devc->rle_count);
577 devc->num_samples += devc->rle_count + 1;
578 if (devc->num_samples > devc->limit_samples) {
579 /* Save us from overrunning the buffer. */
580 devc->rle_count -= devc->num_samples - devc->limit_samples;
581 devc->num_samples = devc->limit_samples;
585 if (num_channels < 4) {
587 * Some channel groups may have been turned
588 * off, to speed up transfer between the
589 * hardware and the PC. Expand that here before
590 * submitting it over the session bus --
591 * whatever is listening on the bus will be
592 * expecting a full 32-bit sample, based on
593 * the number of channels.
596 memset(devc->tmp_sample, 0, 4);
597 for (i = 0; i < 4; i++) {
598 if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
600 * This channel group was
601 * enabled, copy from received
604 devc->tmp_sample[i] = devc->sample[j++];
607 memcpy(devc->sample, devc->tmp_sample, 4);
608 sr_spew("Expanded sample: 0x%.8x.", sample);
612 * Pipistrello OLS sends its sample buffer backwards.
613 * store it in reverse order here, so we can dump
614 * this on the session bus later.
616 offset = (devc->limit_samples - devc->num_samples) * 4;
617 for (i = 0; i <= devc->rle_count; i++) {
618 memcpy(devc->raw_sample_buf + offset + (i * 4),
621 memset(devc->sample, 0, 4);
629 do bytes_read = ftdi_read_data(devc->ftdic, devc->ftdi_buf, FTDI_BUF_SIZE);
630 while (bytes_read > 0);
633 * We've acquired all the samples we asked for -- we're done.
634 * Send the (properly-ordered) buffer to the frontend.
636 sr_dbg("Received %d bytes, %d samples, %d decompressed samples.",
637 devc->cnt_bytes, devc->cnt_samples,
638 devc->cnt_samples_rle);
639 if (devc->trigger_at != -1) {
641 * A trigger was set up, so we need to tell the frontend
644 if (devc->trigger_at > 0) {
645 /* There are pre-trigger samples, send those first. */
646 packet.type = SR_DF_LOGIC;
647 packet.payload = &logic;
648 logic.length = devc->trigger_at * 4;
650 logic.data = devc->raw_sample_buf +
651 (devc->limit_samples - devc->num_samples) * 4;
652 sr_session_send(cb_data, &packet);
655 /* Send the trigger. */
656 packet.type = SR_DF_TRIGGER;
657 sr_session_send(cb_data, &packet);
659 /* Send post-trigger samples. */
660 packet.type = SR_DF_LOGIC;
661 packet.payload = &logic;
662 logic.length = (devc->num_samples * 4) - (devc->trigger_at * 4);
664 logic.data = devc->raw_sample_buf + devc->trigger_at * 4 +
665 (devc->limit_samples - devc->num_samples) * 4;
666 sr_session_send(cb_data, &packet);
668 /* no trigger was used */
669 packet.type = SR_DF_LOGIC;
670 packet.payload = &logic;
671 logic.length = devc->num_samples * 4;
673 logic.data = devc->raw_sample_buf +
674 (devc->limit_samples - devc->num_samples) * 4;
675 sr_session_send(cb_data, &packet);
677 g_free(devc->raw_sample_buf);
679 sdi->driver->dev_acquisition_stop(sdi, cb_data);