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);
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;
231 devname = g_string_new("");
232 version = g_string_new("");
235 while (index < bytes_read) {
238 sr_dbg("Got metadata key 0x00, metadata ends.");
245 /* NULL-terminated string */
246 tmp_str = g_string_new("");
247 while ((index < bytes_read) && ((tmp_c = buf[index++]) != '\0'))
248 g_string_append_c(tmp_str, tmp_c);
249 sr_dbg("Got metadata key 0x%.2x value '%s'.",
254 devname = g_string_append(devname, tmp_str->str);
257 /* FPGA firmware version */
259 g_string_append(version, ", ");
260 g_string_append(version, "FPGA version ");
261 g_string_append(version, tmp_str->str);
264 /* Ancillary version */
266 g_string_append(version, ", ");
267 g_string_append(version, "Ancillary version ");
268 g_string_append(version, tmp_str->str);
271 sr_info("Unknown token 0x%.2x: '%s'",
272 token, tmp_str->str);
275 g_string_free(tmp_str, TRUE);
278 /* 32-bit unsigned integer */
280 for (i = 0; i < 4; i++) {
281 tmp_int = (tmp_int << 8) | buf[index++];
283 sr_dbg("Got metadata key 0x%.2x value 0x%.8x.",
287 /* Number of usable channels */
288 for (ui = 0; ui < tmp_int; ui++)
289 sr_channel_new(sdi, ui, SR_CHANNEL_LOGIC, TRUE,
290 p_ols_channel_names[ui]);
293 /* Amount of sample memory available (bytes) */
294 devc->max_samplebytes = tmp_int;
297 /* Amount of dynamic memory available (bytes) */
298 /* what is this for? */
301 /* Maximum sample rate (Hz) */
302 devc->max_samplerate = tmp_int;
305 /* protocol version */
306 devc->protocol_version = tmp_int;
309 sr_info("Unknown token 0x%.2x: 0x%.8x.",
315 /* 8-bit unsigned integer */
316 tmp_c = buf[index++];
317 sr_dbg("Got metadata key 0x%.2x value 0x%.2x.",
321 /* Number of usable channels */
322 for (ui = 0; ui < tmp_c; ui++)
323 sr_channel_new(sdi, ui, SR_CHANNEL_LOGIC, TRUE,
324 p_ols_channel_names[ui]);
327 /* protocol version */
328 devc->protocol_version = tmp_c;
331 sr_info("Unknown token 0x%.2x: 0x%.2x.",
342 sdi->model = devname->str;
343 sdi->version = version->str;
344 g_string_free(devname, FALSE);
345 g_string_free(version, FALSE);
350 SR_PRIV int p_ols_set_samplerate(const struct sr_dev_inst *sdi,
351 const uint64_t samplerate)
353 struct dev_context *devc;
356 if (devc->max_samplerate && samplerate > devc->max_samplerate)
357 return SR_ERR_SAMPLERATE;
359 if (samplerate > CLOCK_RATE) {
360 sr_info("Enabling demux mode.");
361 devc->flag_reg |= FLAG_DEMUX;
362 devc->flag_reg &= ~FLAG_FILTER;
363 devc->max_channels = NUM_CHANNELS / 2;
364 devc->cur_samplerate_divider = (CLOCK_RATE * 2 / samplerate) - 1;
366 sr_info("Disabling demux mode.");
367 devc->flag_reg &= ~FLAG_DEMUX;
368 devc->flag_reg |= FLAG_FILTER;
369 devc->max_channels = NUM_CHANNELS;
370 devc->cur_samplerate_divider = (CLOCK_RATE / samplerate) - 1;
373 /* Calculate actual samplerate used and complain if it is different
374 * from the requested.
376 devc->cur_samplerate = CLOCK_RATE / (devc->cur_samplerate_divider + 1);
377 if (devc->flag_reg & FLAG_DEMUX)
378 devc->cur_samplerate *= 2;
379 if (devc->cur_samplerate != samplerate)
380 sr_info("Can't match samplerate %" PRIu64 ", using %"
381 PRIu64 ".", samplerate, devc->cur_samplerate);
386 SR_PRIV int p_ols_receive_data(int fd, int revents, void *cb_data)
388 struct dev_context *devc;
389 struct sr_dev_inst *sdi;
390 struct sr_datafeed_packet packet;
391 struct sr_datafeed_logic logic;
393 int num_channels, offset, j;
394 int bytes_read, index;
404 if (devc->num_transfers++ == 0) {
405 devc->raw_sample_buf = g_try_malloc(devc->limit_samples * 4);
406 if (!devc->raw_sample_buf) {
407 sr_err("Sample buffer malloc failed.");
410 /* fill with 1010... for debugging */
411 memset(devc->raw_sample_buf, 0x82, devc->limit_samples * 4);
414 if ((devc->num_samples < devc->limit_samples) && (devc->cnt_samples < devc->max_samples)) {
417 for (i = NUM_CHANNELS; i > 0x02; i /= 2) {
418 if ((devc->flag_reg & i) == 0) {
423 /* Get a block of data. */
424 bytes_read = ftdi_read_data(devc->ftdic, devc->ftdi_buf, FTDI_BUF_SIZE);
425 if (bytes_read < 0) {
426 sr_err("Failed to read FTDI data (%d): %s.",
427 bytes_read, ftdi_get_error_string(devc->ftdic));
428 sr_dev_acquisition_stop(sdi);
431 if (bytes_read == 0) {
432 sr_spew("Received 0 bytes, nothing to do.");
436 sr_dbg("Received %d bytes", bytes_read);
439 while (index < bytes_read) {
440 byte = devc->ftdi_buf[index++];
443 devc->sample[devc->num_bytes++] = byte;
444 sr_spew("Received byte 0x%.2x.", byte);
446 if ((devc->flag_reg & FLAG_DEMUX) && (devc->flag_reg & FLAG_RLE)) {
447 /* RLE in demux mode must be processed differently
448 * since in this case the RLE encoder is operating on pairs of samples.
450 if (devc->num_bytes == num_channels * 2) {
451 devc->cnt_samples += 2;
452 devc->cnt_samples_rle += 2;
454 * Got a sample pair. Convert from the OLS's little-endian
455 * sample to the local format.
457 sample = devc->sample[0] | (devc->sample[1] << 8) \
458 | (devc->sample[2] << 16) | (devc->sample[3] << 24);
459 sr_spew("Received sample pair 0x%.*x.", devc->num_bytes * 2, sample);
462 * In RLE mode the high bit of the sample pair is the
463 * "count" flag, meaning this sample pair is the number
464 * of times the previous sample pair occurred.
466 if (devc->sample[devc->num_bytes - 1] & 0x80) {
467 /* Clear the high bit. */
468 sample &= ~(0x80 << (devc->num_bytes - 1) * 8);
469 devc->rle_count = sample;
470 devc->cnt_samples_rle += devc->rle_count * 2;
471 sr_dbg("RLE count: %u.", devc->rle_count * 2);
475 devc->num_samples += (devc->rle_count + 1) * 2;
476 if (devc->num_samples > devc->limit_samples) {
477 /* Save us from overrunning the buffer. */
478 devc->rle_count -= (devc->num_samples - devc->limit_samples) / 2;
479 devc->num_samples = devc->limit_samples;
484 * Some channel groups may have been turned
485 * off, to speed up transfer between the
486 * hardware and the PC. Expand that here before
487 * submitting it over the session bus --
488 * whatever is listening on the bus will be
489 * expecting a full 32-bit sample, based on
490 * the number of channels.
493 /* expand first sample */
494 memset(devc->tmp_sample, 0, 4);
495 for (i = 0; i < 2; i++) {
496 if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
498 * This channel group was
499 * enabled, copy from received
502 devc->tmp_sample[i] = devc->sample[j++];
505 /* Clear out the most significant bit of the sample */
506 devc->tmp_sample[devc->num_bytes - 1] &= 0x7f;
507 sr_spew("Expanded sample 1: 0x%.2x%.2x%.2x%.2x.",
508 devc->tmp_sample[3], devc->tmp_sample[2],
509 devc->tmp_sample[1], devc->tmp_sample[0]);
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%.2x%.2x%.2x%.2x.",
526 devc->tmp_sample2[3], devc->tmp_sample2[2],
527 devc->tmp_sample2[1], devc->tmp_sample2[0]);
530 * OLS sends its sample buffer backwards.
531 * store it in reverse order here, so we can dump
532 * this on the session bus later.
534 offset = (devc->limit_samples - devc->num_samples) * 4;
535 for (i = 0; i <= devc->rle_count; i++) {
536 memcpy(devc->raw_sample_buf + offset + (i * 8),
537 devc->tmp_sample2, 4);
538 memcpy(devc->raw_sample_buf + offset + (4 + (i * 8)),
539 devc->tmp_sample, 4);
541 memset(devc->sample, 0, 4);
547 if (devc->num_bytes == num_channels) {
549 devc->cnt_samples_rle++;
551 * Got a full sample. Convert from the OLS's little-endian
552 * sample to the local format.
554 sample = devc->sample[0] | (devc->sample[1] << 8) \
555 | (devc->sample[2] << 16) | (devc->sample[3] << 24);
556 sr_spew("Received sample 0x%.*x.", devc->num_bytes * 2, sample);
557 if (devc->flag_reg & FLAG_RLE) {
559 * In RLE mode the high bit of the sample is the
560 * "count" flag, meaning this sample is the number
561 * of times the previous sample occurred.
563 if (devc->sample[devc->num_bytes - 1] & 0x80) {
564 /* Clear the high bit. */
565 sample &= ~(0x80 << (devc->num_bytes - 1) * 8);
566 devc->rle_count = sample;
567 devc->cnt_samples_rle += devc->rle_count;
568 sr_dbg("RLE count: %u.", devc->rle_count);
573 devc->num_samples += devc->rle_count + 1;
574 if (devc->num_samples > devc->limit_samples) {
575 /* Save us from overrunning the buffer. */
576 devc->rle_count -= devc->num_samples - devc->limit_samples;
577 devc->num_samples = devc->limit_samples;
581 if (num_channels < 4) {
583 * Some channel groups may have been turned
584 * off, to speed up transfer between the
585 * hardware and the PC. Expand that here before
586 * submitting it over the session bus --
587 * whatever is listening on the bus will be
588 * expecting a full 32-bit sample, based on
589 * the number of channels.
592 memset(devc->tmp_sample, 0, 4);
593 for (i = 0; i < 4; i++) {
594 if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
596 * This channel group was
597 * enabled, copy from received
600 devc->tmp_sample[i] = devc->sample[j++];
603 memcpy(devc->sample, devc->tmp_sample, 4);
604 sr_spew("Expanded sample: 0x%.8x.", sample);
608 * Pipistrello OLS sends its sample buffer backwards.
609 * store it in reverse order here, so we can dump
610 * this on the session bus later.
612 offset = (devc->limit_samples - devc->num_samples) * 4;
613 for (i = 0; i <= devc->rle_count; i++) {
614 memcpy(devc->raw_sample_buf + offset + (i * 4),
617 memset(devc->sample, 0, 4);
626 bytes_read = ftdi_read_data(devc->ftdic, devc->ftdi_buf, FTDI_BUF_SIZE);
627 } while (bytes_read > 0);
630 * We've acquired all the samples we asked for -- we're done.
631 * Send the (properly-ordered) buffer to the frontend.
633 sr_dbg("Received %d bytes, %d samples, %d decompressed samples.",
634 devc->cnt_bytes, devc->cnt_samples,
635 devc->cnt_samples_rle);
636 if (devc->trigger_at != -1) {
638 * A trigger was set up, so we need to tell the frontend
641 if (devc->trigger_at > 0) {
642 /* There are pre-trigger samples, send those first. */
643 packet.type = SR_DF_LOGIC;
644 packet.payload = &logic;
645 logic.length = devc->trigger_at * 4;
647 logic.data = devc->raw_sample_buf +
648 (devc->limit_samples - devc->num_samples) * 4;
649 sr_session_send(sdi, &packet);
652 /* Send the trigger. */
653 packet.type = SR_DF_TRIGGER;
654 sr_session_send(sdi, &packet);
656 /* Send post-trigger samples. */
657 packet.type = SR_DF_LOGIC;
658 packet.payload = &logic;
659 logic.length = (devc->num_samples * 4) - (devc->trigger_at * 4);
661 logic.data = devc->raw_sample_buf + devc->trigger_at * 4 +
662 (devc->limit_samples - devc->num_samples) * 4;
663 sr_session_send(sdi, &packet);
665 /* no trigger was used */
666 packet.type = SR_DF_LOGIC;
667 packet.payload = &logic;
668 logic.length = devc->num_samples * 4;
670 logic.data = devc->raw_sample_buf +
671 (devc->limit_samples - devc->num_samples) * 4;
672 sr_session_send(sdi, &packet);
674 g_free(devc->raw_sample_buf);
676 sr_dev_acquisition_stop(sdi);