2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Marc Schink <sigrok-dev@marcschink.de>
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 struct sr_dev_driver ikalogic_scanalogic2_driver_info;
23 static struct sr_dev_driver *di = &ikalogic_scanalogic2_driver_info;
25 extern uint64_t ikalogic_scanalogic2_samplerates[NUM_SAMPLERATES];
27 static void stop_acquisition(struct sr_dev_inst *sdi)
29 struct dev_context *devc;
30 struct sr_datafeed_packet packet;
35 /* Remove USB file descriptors from polling. */
36 for (i = 0; i < devc->num_usbfd; i++)
37 sr_source_remove(devc->usbfd[i]);
41 packet.type = SR_DF_END;
42 sr_session_send(devc->cb_data, &packet);
44 sdi->status = SR_ST_ACTIVE;
47 static void abort_acquisition(struct sr_dev_inst *sdi)
49 struct dev_context *devc;
50 struct sr_datafeed_packet packet;
55 /* Remove USB file descriptors from polling. */
56 for (i = 0; i < devc->num_usbfd; i++)
57 sr_source_remove(devc->usbfd[i]);
61 packet.type = SR_DF_END;
62 sr_session_send(devc->cb_data, &packet);
64 sdi->driver->dev_close(sdi);
67 static void buffer_sample_data(const struct sr_dev_inst *sdi)
69 struct dev_context *devc;
70 unsigned int offset, packet_length;
74 if (devc->probes[devc->channel]->enabled) {
75 offset = devc->sample_packet * PACKET_NUM_SAMPLE_BYTES;
78 * Determine the packet length to ensure that the last packet
79 * will not exceed the buffer size.
81 packet_length = MIN(PACKET_NUM_SAMPLE_BYTES,
82 MAX_DEV_SAMPLE_BYTES - offset);
85 * Skip the first 4 bytes of the source buffer because they
86 * contain channel and packet information only.
88 memcpy(devc->sample_buffer[devc->channel] + offset,
89 devc->xfer_data_in + 4, packet_length);
93 static void process_sample_data(const struct sr_dev_inst *sdi)
95 struct dev_context *devc;
96 struct sr_datafeed_packet packet;
97 struct sr_datafeed_logic logic;
98 uint8_t i, j, tmp, buffer[PACKET_NUM_SAMPLES], *ptr[NUM_PROBES];
99 uint16_t offset, n = 0;
103 offset = devc->sample_packet * PACKET_NUM_SAMPLE_BYTES;
106 * Array of pointers to the sample data of all channels up to the last
107 * enabled one for an uniform access to them. Note that the currently
108 * received samples always belong to the last enabled channel.
110 for (i = 0; i < devc->num_enabled_probes - 1; i++)
111 ptr[i] = devc->sample_buffer[devc->probe_map[i]] + offset;
114 * Skip the first 4 bytes of the buffer because they contain channel and
115 * packet information only.
117 ptr[i] = devc->xfer_data_in + 4;
119 for (i = 0; i < PACKET_NUM_SAMPLE_BYTES; i++) {
120 /* Stop processing if all requested samples are processed. */
121 if (devc->samples_processed == devc->limit_samples)
126 if (devc->samples_processed == 0) {
128 * Adjust the position of the first sample to be
129 * processed because possibly more samples than
130 * necessary might have been aquired. This is because
131 * the number of aquired samples is always rounded up to
134 k = k - (devc->pre_trigger_bytes * 8) +
135 devc->pre_trigger_samples;
137 sr_dbg("Start processing at sample: %" PRIu8 ".",
141 * Send the trigger before the first sample is processed
142 * if no pre trigger samples were calculated through the
145 if (devc->trigger_type != TRIGGER_TYPE_NONE &&
146 devc->pre_trigger_samples == 0) {
147 packet.type = SR_DF_TRIGGER;
148 sr_session_send(devc->cb_data, &packet);
152 for (; k >= 0; k--) {
154 * Stop processing if all requested samples are
157 if (devc->samples_processed == devc->limit_samples)
163 * Extract the current sample for each enabled channel
164 * and store them in the buffer.
166 for (j = 0; j < devc->num_enabled_probes; j++) {
167 tmp = (ptr[j][i] & (1 << k)) >> k;
168 buffer[n] |= tmp << devc->probe_map[j];
172 devc->samples_processed++;
175 * Send all processed samples and the trigger if the
176 * number of processed samples reaches the calculated
177 * number of pre trigger samples.
179 if (devc->samples_processed == devc->pre_trigger_samples &&
180 devc->trigger_type != TRIGGER_TYPE_NONE) {
181 packet.type = SR_DF_LOGIC;
182 packet.payload = &logic;
186 sr_session_send(devc->cb_data, &packet);
188 packet.type = SR_DF_TRIGGER;
189 sr_session_send(devc->cb_data, &packet);
197 packet.type = SR_DF_LOGIC;
198 packet.payload = &logic;
202 sr_session_send(devc->cb_data, &packet);
206 SR_PRIV int ikalogic_scanalogic2_receive_data(int fd, int revents,
209 struct sr_dev_inst *sdi;
210 struct dev_context *devc;
211 struct drv_context *drvc;
213 int64_t current_time, time_elapsed;
219 if (!(sdi = cb_data))
222 if (!(devc = sdi->priv))
226 current_time = g_get_monotonic_time();
228 if (devc->state == STATE_WAIT_DATA_READY &&
229 !devc->wait_data_ready_locked) {
230 time_elapsed = current_time - devc->wait_data_ready_time;
233 * Check here for stopping in addition to the transfer callback
234 * functions to avoid waiting until the WAIT_DATA_READY_INTERVAL
237 if (sdi->status == SR_ST_STOPPING) {
238 if (!devc->stopping_in_progress) {
239 devc->next_state = STATE_RESET_AND_IDLE;
240 devc->stopping_in_progress = TRUE;
241 ret = libusb_submit_transfer(devc->xfer_in);
243 } else if (time_elapsed >= WAIT_DATA_READY_INTERVAL) {
244 devc->wait_data_ready_locked = TRUE;
245 ret = libusb_submit_transfer(devc->xfer_in);
250 sr_err("Submit transfer failed: %s", libusb_error_name(ret));
251 abort_acquisition(sdi);
258 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
261 /* Check if an error occurred on a transfer. */
262 if (devc->transfer_error) {
263 abort_acquisition(sdi);
269 SR_PRIV void ikalogic_scanalogic2_receive_transfer_in(
270 struct libusb_transfer *transfer)
272 struct sr_dev_inst *sdi;
273 struct dev_context *devc;
274 uint8_t last_channel;
277 sdi = transfer->user_data;
280 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
281 sr_err("Transfer to device failed: %i", transfer->status);
282 devc->transfer_error = TRUE;
286 if (sdi->status == SR_ST_STOPPING && !devc->stopping_in_progress) {
287 devc->next_state = STATE_RESET_AND_IDLE;
288 devc->stopping_in_progress = TRUE;
290 if (libusb_submit_transfer(devc->xfer_in) != 0) {
291 sr_err("Submit transfer failed: %s",
292 libusb_error_name(ret));
293 devc->transfer_error = TRUE;
299 sr_dbg("State changed from %i to %i.", devc->state, devc->next_state);
300 devc->state = devc->next_state;
302 if (devc->state == STATE_WAIT_DATA_READY) {
303 /* Check if the received data are a valid device status. */
304 if (devc->xfer_data_in[0] == 0x05) {
305 if (devc->xfer_data_in[1] == STATUS_WAITING_FOR_TRIGGER)
306 sr_dbg("Waiting for trigger.");
307 else if (devc->xfer_data_in[1] == STATUS_SAMPLING)
308 sr_dbg("Sampling in progress.");
312 * Check if the received data are a valid device status and the
313 * sample data are ready.
315 if (devc->xfer_data_in[0] == 0x05 &&
316 devc->xfer_data_in[1] == STATUS_DATA_READY) {
317 devc->next_state = STATE_RECEIVE_DATA;
318 ret = libusb_submit_transfer(transfer);
320 devc->wait_data_ready_locked = FALSE;
321 devc->wait_data_ready_time = g_get_monotonic_time();
323 } else if (devc->state == STATE_RECEIVE_DATA) {
324 last_channel = devc->probe_map[devc->num_enabled_probes - 1];
326 if (devc->channel < last_channel) {
327 buffer_sample_data(sdi);
328 } else if (devc->channel == last_channel) {
329 process_sample_data(sdi);
332 * Stop acquisition because all samples of enabled
333 * probes are processed.
335 devc->next_state = STATE_RESET_AND_IDLE;
338 devc->sample_packet++;
339 devc->sample_packet %= devc->num_sample_packets;
341 if (devc->sample_packet == 0)
344 ret = libusb_submit_transfer(transfer);
345 } else if (devc->state == STATE_RESET_AND_IDLE) {
346 /* Check if the received data are a valid device status. */
347 if (devc->xfer_data_in[0] == 0x05) {
348 if (devc->xfer_data_in[1] == STATUS_DEVICE_READY) {
349 devc->next_state = STATE_IDLE;
350 devc->xfer_data_out[0] = CMD_IDLE;
352 devc->next_state = STATE_WAIT_DEVICE_READY;
353 devc->xfer_data_out[0] = CMD_RESET;
356 ret = libusb_submit_transfer(devc->xfer_out);
359 * The received device status is invalid which indicates
360 * that the device is not ready to accept commands.
361 * Request a new device status until a valid device
362 * status is received.
364 ret = libusb_submit_transfer(transfer);
366 } else if (devc->state == STATE_WAIT_DEVICE_READY) {
367 /* Check if the received data are a valid device status. */
368 if (devc->xfer_data_in[0] == 0x05) {
369 if (devc->xfer_data_in[1] == STATUS_DEVICE_READY) {
370 devc->next_state = STATE_IDLE;
371 devc->xfer_data_out[0] = CMD_IDLE;
374 * The received device status is valid but the
375 * device is not ready. Probably the device did
376 * not recognize the last reset. Reset the
379 devc->xfer_data_out[0] = CMD_RESET;
382 ret = libusb_submit_transfer(devc->xfer_out);
385 * The device is not ready and therefore not able to
386 * change to the idle state. Request a new device status
387 * until the device is ready.
389 ret = libusb_submit_transfer(transfer);
394 sr_err("Submit transfer failed: %s", libusb_error_name(ret));
395 devc->transfer_error = TRUE;
399 SR_PRIV void ikalogic_scanalogic2_receive_transfer_out(
400 struct libusb_transfer *transfer)
402 struct sr_dev_inst *sdi;
403 struct dev_context *devc;
406 sdi = transfer->user_data;
409 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
410 sr_err("Transfer to device failed: %i", transfer->status);
411 devc->transfer_error = TRUE;
415 if (sdi->status == SR_ST_STOPPING && !devc->stopping_in_progress) {
416 devc->next_state = STATE_RESET_AND_IDLE;
417 devc->stopping_in_progress = TRUE;
419 if (libusb_submit_transfer(devc->xfer_in) != 0) {
420 sr_err("Submit transfer failed: %s",
421 libusb_error_name(ret));
423 devc->transfer_error = TRUE;
429 sr_dbg("State changed from %i to %i.", devc->state, devc->next_state);
430 devc->state = devc->next_state;
432 if (devc->state == STATE_IDLE) {
433 stop_acquisition(sdi);
434 } else if (devc->state == STATE_SAMPLE) {
435 devc->next_state = STATE_WAIT_DATA_READY;
436 ret = libusb_submit_transfer(devc->xfer_in);
437 } else if (devc->state == STATE_WAIT_DEVICE_READY) {
438 ret = libusb_submit_transfer(devc->xfer_in);
442 sr_err("Submit transfer failed: %s", libusb_error_name(ret));
443 devc->transfer_error = TRUE;
447 SR_PRIV int ikalogic_scanalogic2_set_samplerate(const struct sr_dev_inst *sdi,
450 struct dev_context *devc;
455 for (i = 0; i < NUM_SAMPLERATES; i++) {
456 if (ikalogic_scanalogic2_samplerates[i] == samplerate) {
457 devc->samplerate = samplerate;
458 devc->samplerate_id = NUM_SAMPLERATES - i - 1;
466 SR_PRIV int ikalogic_scanalogic2_set_limit_samples(
467 const struct sr_dev_inst *sdi, uint64_t limit_samples)
469 struct dev_context *devc;
473 if (limit_samples == 0) {
474 sr_err("Invalid number of limit samples: %" PRIu64 ".",
479 if (limit_samples > MAX_SAMPLES)
480 limit_samples = MAX_SAMPLES;
482 sr_info("Limit samples set to %" PRIu64 ".", limit_samples);
484 devc->limit_samples = limit_samples;
489 SR_PRIV void ikalogic_scanalogic2_configure_trigger(
490 const struct sr_dev_inst *sdi)
492 struct dev_context *devc;
493 struct sr_probe *probe;
494 uint8_t trigger_type;
495 int probe_index, num_triggers_anyedge;
501 /* Disable the trigger by default. */
502 devc->trigger_channel = TRIGGER_CHANNEL_0;
503 devc->trigger_type = TRIGGER_TYPE_NONE;
505 num_triggers_anyedge = 0;
507 for (l = sdi->probes, probe_index = 0; l; l = l->next, probe_index++) {
509 trigger = probe->trigger;
511 if (!trigger || !probe->enabled)
516 trigger_type = TRIGGER_TYPE_POSEDGE;
519 trigger_type = TRIGGER_TYPE_NEGEDGE;
522 trigger_type = TRIGGER_TYPE_ANYEDGE;
523 num_triggers_anyedge++;
529 devc->trigger_channel = probe_index + 1;
530 devc->trigger_type = trigger_type;
534 * Set trigger to any edge on all channels if the trigger for each
535 * channel is set to any edge.
537 if (num_triggers_anyedge == NUM_PROBES) {
538 devc->trigger_channel = TRIGGER_CHANNEL_ALL;
539 devc->trigger_type = TRIGGER_TYPE_ANYEDGE;
542 sr_dbg("Trigger set to channel %" PRIu8 " and type %" PRIu8 ".",
543 devc->trigger_channel, devc->trigger_type);
546 SR_PRIV int ikalogic_scanalogic2_set_capture_ratio(
547 const struct sr_dev_inst *sdi, uint64_t capture_ratio)
549 struct dev_context *devc;
553 if (capture_ratio > 100) {
554 sr_err("Invalid capture ratio: %" PRIu64 " %%.", capture_ratio);
558 sr_info("Capture ratio set to %" PRIu64 " %%.", capture_ratio);
560 devc->capture_ratio = capture_ratio;
565 SR_PRIV int ikaloigc_scanalogic2_set_after_trigger_delay(
566 const struct sr_dev_inst *sdi, uint64_t after_trigger_delay)
568 struct dev_context *devc;
572 if (after_trigger_delay > MAX_AFTER_TRIGGER_DELAY) {
573 sr_err("Invalid after trigger delay: %" PRIu64 " ms.",
574 after_trigger_delay);
578 sr_info("After trigger delay set to %" PRIu64 " ms.",
579 after_trigger_delay);
581 devc->after_trigger_delay = after_trigger_delay;
586 SR_PRIV void ikalogic_scanalogic2_calculate_trigger_samples(
587 const struct sr_dev_inst *sdi)
589 struct dev_context *devc;
590 uint64_t pre_trigger_samples, post_trigger_samples;
591 uint16_t pre_trigger_bytes, post_trigger_bytes;
595 cr = devc->capture_ratio;
597 /* Ignore the capture ratio if no trigger is enabled. */
598 if (devc->trigger_type == TRIGGER_TYPE_NONE)
601 pre_trigger_samples = (devc->limit_samples * cr) / 100;
602 post_trigger_samples = (devc->limit_samples * (100 - cr)) / 100;
605 * Increase the number of post trigger samples by one to compensate the
606 * possible loss of a sample through integer rounding.
608 if (pre_trigger_samples + post_trigger_samples != devc->limit_samples)
609 post_trigger_samples++;
612 * The device requires the number of samples in multiples of 8 which
613 * will also be called sample bytes in the following.
615 pre_trigger_bytes = pre_trigger_samples / 8;
616 post_trigger_bytes = post_trigger_samples / 8;
619 * Round up the number of sample bytes to ensure that at least the
620 * requested number of samples will be acquired. Note that due to this
621 * rounding the buffer to store these sample bytes needs to be at least
622 * one sample byte larger than the minimal number of sample bytes needed
623 * to store the requested samples.
625 if (pre_trigger_samples % 8 != 0)
628 if (post_trigger_samples % 8 != 0)
629 post_trigger_bytes++;
631 sr_info("Pre trigger samples: %" PRIu64 ".", pre_trigger_samples);
632 sr_info("Post trigger samples: %" PRIu64 ".", post_trigger_samples);
633 sr_dbg("Pre trigger sample bytes: %" PRIu16 ".", pre_trigger_bytes);
634 sr_dbg("Post trigger sample bytes: %" PRIu16 ".", post_trigger_bytes);
636 devc->pre_trigger_samples = pre_trigger_samples;
637 devc->pre_trigger_bytes = pre_trigger_bytes;
638 devc->post_trigger_bytes = post_trigger_bytes;
641 SR_PRIV int ikalogic_scanalogic2_get_device_info(struct sr_usb_dev_inst usb,
642 struct device_info *dev_info)
644 struct drv_context *drvc;
645 uint8_t buffer[PACKET_LENGTH];
653 if (sr_usb_open(drvc->sr_ctx->libusb_ctx, &usb) != SR_OK)
657 * Determine if a kernel driver is active on this interface and, if so,
660 if (libusb_kernel_driver_active(usb.devhdl, USB_INTERFACE) == 1) {
661 ret = libusb_detach_kernel_driver(usb.devhdl,
665 sr_err("Failed to detach kernel driver: %i.",
666 libusb_error_name(ret));
667 libusb_close(usb.devhdl);
672 ret = libusb_claim_interface(usb.devhdl, USB_INTERFACE);
675 sr_err("Failed to claim interface: %s.",
676 libusb_error_name(ret));
677 libusb_close(usb.devhdl);
681 memset(buffer, 0, sizeof(buffer));
684 * Reset the device to ensure it is in a proper state to request the
685 * device information.
687 buffer[0] = CMD_RESET;
688 ret = ikalogic_scanalogic2_transfer_out(usb.devhdl, buffer);
690 if (ret != PACKET_LENGTH) {
691 sr_err("Resetting of device failed: %s.",
692 libusb_error_name(ret));
693 libusb_release_interface(usb.devhdl, USB_INTERFACE);
694 libusb_close(usb.devhdl);
698 buffer[0] = CMD_INFO;
699 ret = ikalogic_scanalogic2_transfer_out(usb.devhdl, buffer);
701 if (ret != PACKET_LENGTH) {
702 sr_err("Requesting of device information failed: %s.",
703 libusb_error_name(ret));
704 libusb_release_interface(usb.devhdl, USB_INTERFACE);
705 libusb_close(usb.devhdl);
709 ret = ikalogic_scanalogic2_transfer_in(usb.devhdl, buffer);
711 if (ret != PACKET_LENGTH) {
712 sr_err("Receiving of device information failed: %s.",
713 libusb_error_name(ret));
714 libusb_release_interface(usb.devhdl, USB_INTERFACE);
715 libusb_close(usb.devhdl);
719 memcpy(&(dev_info->serial), buffer + 1, sizeof(uint32_t));
720 dev_info->serial = GUINT32_FROM_LE(dev_info->serial);
722 dev_info->fw_ver_major = buffer[5];
723 dev_info->fw_ver_minor = buffer[6];
725 buffer[0] = CMD_RESET;
726 ret = ikalogic_scanalogic2_transfer_out(usb.devhdl, buffer);
728 if (ret != PACKET_LENGTH) {
729 sr_err("Device reset failed: %s.", libusb_error_name(ret));
730 libusb_release_interface(usb.devhdl, USB_INTERFACE);
731 libusb_close(usb.devhdl);
736 * Set the device to idle state. If the device is not in idle state it
737 * possibly will reset itself after a few seconds without being used and
738 * thereby close the connection.
740 buffer[0] = CMD_IDLE;
741 ret = ikalogic_scanalogic2_transfer_out(usb.devhdl, buffer);
743 if (ret != PACKET_LENGTH) {
744 sr_err("Failed to set device in idle state: %s.",
745 libusb_error_name(ret));
746 libusb_release_interface(usb.devhdl, USB_INTERFACE);
747 libusb_close(usb.devhdl);
751 ret = libusb_release_interface(usb.devhdl, USB_INTERFACE);
754 sr_err("Failed to release interface: %i.",
755 libusb_error_name(ret));
756 libusb_close(usb.devhdl);
760 libusb_close(usb.devhdl);
765 SR_PRIV int ikalogic_scanalogic2_transfer_in(libusb_device_handle *dev_handle,
768 return libusb_control_transfer(dev_handle, USB_REQUEST_TYPE_IN,
769 USB_HID_SET_REPORT, USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
770 data, PACKET_LENGTH, USB_TIMEOUT);
773 SR_PRIV int ikalogic_scanalogic2_transfer_out(libusb_device_handle *dev_handle,
776 return libusb_control_transfer(dev_handle, USB_REQUEST_TYPE_OUT,
777 USB_HID_SET_REPORT, USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
778 data, PACKET_LENGTH, USB_TIMEOUT);