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/>.
23 extern uint64_t sl2_samplerates[NUM_SAMPLERATES];
25 static void stop_acquisition(struct sr_dev_inst *sdi)
27 struct drv_context *drvc = sdi->driver->context;
29 usb_source_remove(sdi->session, drvc->sr_ctx);
31 std_session_send_df_end(sdi);
33 sdi->status = SR_ST_ACTIVE;
36 static void abort_acquisition(struct sr_dev_inst *sdi)
38 struct drv_context *drvc = sdi->driver->context;
40 usb_source_remove(sdi->session, drvc->sr_ctx);
42 std_session_send_df_end(sdi);
47 static void buffer_sample_data(const struct sr_dev_inst *sdi)
49 struct dev_context *devc;
50 unsigned int offset, packet_length;
54 if (devc->channels[devc->channel]->enabled) {
55 offset = devc->sample_packet * PACKET_NUM_SAMPLE_BYTES;
58 * Determine the packet length to ensure that the last packet
59 * will not exceed the buffer size.
61 packet_length = MIN(PACKET_NUM_SAMPLE_BYTES,
62 MAX_DEV_SAMPLE_BYTES - offset);
65 * Skip the first 4 bytes of the source buffer because they
66 * contain channel and packet information only.
68 memcpy(devc->sample_buffer[devc->channel] + offset,
69 devc->xfer_data_in + 4, packet_length);
73 static void process_sample_data(const struct sr_dev_inst *sdi)
75 struct dev_context *devc;
76 struct sr_datafeed_packet packet;
77 struct sr_datafeed_logic logic;
78 uint8_t i, j, tmp, buffer[PACKET_NUM_SAMPLES], *ptr[NUM_CHANNELS];
79 uint16_t offset, n = 0;
83 offset = devc->sample_packet * PACKET_NUM_SAMPLE_BYTES;
86 * Array of pointers to the sample data of all channels up to the last
87 * enabled one for an uniform access to them. Note that the currently
88 * received samples always belong to the last enabled channel.
90 for (i = 0; i < devc->num_enabled_channels - 1; i++)
91 ptr[i] = devc->sample_buffer[devc->channel_map[i]] + offset;
94 * Skip the first 4 bytes of the buffer because they contain channel
95 * and packet information only.
97 ptr[i] = devc->xfer_data_in + 4;
99 for (i = 0; i < PACKET_NUM_SAMPLE_BYTES; i++) {
100 /* Stop processing if all requested samples are processed. */
101 if (devc->samples_processed == devc->limit_samples)
106 if (devc->samples_processed == 0) {
108 * Adjust the position of the first sample to be
109 * processed because possibly more samples than
110 * necessary might have been acquired. This is because
111 * the number of acquired samples is always rounded up
112 * to a multiple of 8.
114 k = k - (devc->pre_trigger_bytes * 8) +
115 devc->pre_trigger_samples;
117 sr_dbg("Start processing at sample: %d.", 7 - k);
120 * Send the trigger before the first sample is
121 * processed if no pre trigger samples were calculated
122 * through the capture ratio.
124 if (devc->trigger_type != TRIGGER_TYPE_NONE &&
125 devc->pre_trigger_samples == 0) {
126 packet.type = SR_DF_TRIGGER;
127 sr_session_send(sdi, &packet);
131 for (; k >= 0; k--) {
133 * Stop processing if all requested samples are
136 if (devc->samples_processed == devc->limit_samples)
142 * Extract the current sample for each enabled channel
143 * and store them in the buffer.
145 for (j = 0; j < devc->num_enabled_channels; j++) {
146 tmp = (ptr[j][i] & (1 << k)) >> k;
147 buffer[n] |= tmp << devc->channel_map[j];
151 devc->samples_processed++;
154 * Send all processed samples and the trigger if the
155 * number of processed samples reaches the calculated
156 * number of pre trigger samples.
158 if (devc->samples_processed == devc->pre_trigger_samples &&
159 devc->trigger_type != TRIGGER_TYPE_NONE) {
160 packet.type = SR_DF_LOGIC;
161 packet.payload = &logic;
165 sr_session_send(sdi, &packet);
167 packet.type = SR_DF_TRIGGER;
168 sr_session_send(sdi, &packet);
176 packet.type = SR_DF_LOGIC;
177 packet.payload = &logic;
181 sr_session_send(sdi, &packet);
185 SR_PRIV int ikalogic_scanalogic2_receive_data(int fd, int revents, void *cb_data)
187 struct sr_dev_inst *sdi;
188 struct sr_dev_driver *di;
189 struct dev_context *devc;
190 struct drv_context *drvc;
192 int64_t current_time, time_elapsed;
198 if (!(sdi = cb_data))
201 if (!(devc = sdi->priv))
206 current_time = g_get_monotonic_time();
208 if (devc->state == STATE_WAIT_DATA_READY &&
209 !devc->wait_data_ready_locked) {
210 time_elapsed = current_time - devc->wait_data_ready_time;
213 * Check here for stopping in addition to the transfer
214 * callback functions to avoid waiting until the
215 * WAIT_DATA_READY_INTERVAL has expired.
217 if (sdi->status == SR_ST_STOPPING) {
218 if (!devc->stopping_in_progress) {
219 devc->next_state = STATE_RESET_AND_IDLE;
220 devc->stopping_in_progress = TRUE;
221 ret = libusb_submit_transfer(devc->xfer_in);
223 } else if (time_elapsed >= WAIT_DATA_READY_INTERVAL) {
224 devc->wait_data_ready_locked = TRUE;
225 ret = libusb_submit_transfer(devc->xfer_in);
230 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
231 abort_acquisition(sdi);
238 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
241 /* Check if an error occurred on a transfer. */
242 if (devc->transfer_error)
243 abort_acquisition(sdi);
248 SR_PRIV void LIBUSB_CALL sl2_receive_transfer_in( struct libusb_transfer *transfer)
250 struct sr_dev_inst *sdi;
251 struct dev_context *devc;
252 uint8_t last_channel;
255 sdi = transfer->user_data;
258 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
259 sr_err("Transfer to device failed: %s.",
260 libusb_error_name(transfer->status));
261 devc->transfer_error = TRUE;
265 if (sdi->status == SR_ST_STOPPING && !devc->stopping_in_progress) {
266 devc->next_state = STATE_RESET_AND_IDLE;
267 devc->stopping_in_progress = TRUE;
269 if (libusb_submit_transfer(devc->xfer_in) != 0) {
270 sr_err("Submit transfer failed: %s.",
271 libusb_error_name(ret));
272 devc->transfer_error = TRUE;
278 if (devc->state != devc->next_state)
279 sr_spew("State changed from %i to %i.",
280 devc->state, devc->next_state);
281 devc->state = devc->next_state;
283 if (devc->state == STATE_WAIT_DATA_READY) {
284 /* Check if the received data are a valid device status. */
285 if (devc->xfer_data_in[0] == 0x05) {
286 if (devc->xfer_data_in[1] == STATUS_WAITING_FOR_TRIGGER)
287 sr_dbg("Waiting for trigger.");
288 else if (devc->xfer_data_in[1] == STATUS_SAMPLING)
289 sr_dbg("Sampling in progress.");
293 * Check if the received data are a valid device status and the
294 * sample data are ready.
296 if (devc->xfer_data_in[0] == 0x05 &&
297 devc->xfer_data_in[1] == STATUS_DATA_READY) {
298 devc->next_state = STATE_RECEIVE_DATA;
299 ret = libusb_submit_transfer(transfer);
301 devc->wait_data_ready_locked = FALSE;
302 devc->wait_data_ready_time = g_get_monotonic_time();
304 } else if (devc->state == STATE_RECEIVE_DATA) {
305 last_channel = devc->channel_map[devc->num_enabled_channels - 1];
307 if (devc->channel < last_channel) {
308 buffer_sample_data(sdi);
309 } else if (devc->channel == last_channel) {
310 process_sample_data(sdi);
313 * Stop acquisition because all samples of enabled
314 * channels are processed.
316 devc->next_state = STATE_RESET_AND_IDLE;
319 devc->sample_packet++;
320 devc->sample_packet %= devc->num_sample_packets;
322 if (devc->sample_packet == 0)
325 ret = libusb_submit_transfer(transfer);
326 } else if (devc->state == STATE_RESET_AND_IDLE) {
327 /* Check if the received data are a valid device status. */
328 if (devc->xfer_data_in[0] == 0x05) {
329 if (devc->xfer_data_in[1] == STATUS_DEVICE_READY) {
330 devc->next_state = STATE_IDLE;
331 devc->xfer_data_out[0] = CMD_IDLE;
333 devc->next_state = STATE_WAIT_DEVICE_READY;
334 devc->xfer_data_out[0] = CMD_RESET;
337 ret = libusb_submit_transfer(devc->xfer_out);
340 * The received device status is invalid which
341 * indicates that the device is not ready to accept
342 * commands. Request a new device status until a valid
343 * device status is received.
345 ret = libusb_submit_transfer(transfer);
347 } else if (devc->state == STATE_WAIT_DEVICE_READY) {
348 /* Check if the received data are a valid device status. */
349 if (devc->xfer_data_in[0] == 0x05) {
350 if (devc->xfer_data_in[1] == STATUS_DEVICE_READY) {
351 devc->next_state = STATE_IDLE;
352 devc->xfer_data_out[0] = CMD_IDLE;
355 * The received device status is valid but the
356 * device is not ready. Probably the device did
357 * not recognize the last reset. Reset the
360 devc->xfer_data_out[0] = CMD_RESET;
363 ret = libusb_submit_transfer(devc->xfer_out);
366 * The device is not ready and therefore not able to
367 * change to the idle state. Request a new device
368 * status until the device is ready.
370 ret = libusb_submit_transfer(transfer);
375 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
376 devc->transfer_error = TRUE;
380 SR_PRIV void LIBUSB_CALL sl2_receive_transfer_out( struct libusb_transfer *transfer)
382 struct sr_dev_inst *sdi;
383 struct dev_context *devc;
386 sdi = transfer->user_data;
389 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
390 sr_err("Transfer to device failed: %s.",
391 libusb_error_name(transfer->status));
392 devc->transfer_error = TRUE;
396 if (sdi->status == SR_ST_STOPPING && !devc->stopping_in_progress) {
397 devc->next_state = STATE_RESET_AND_IDLE;
398 devc->stopping_in_progress = TRUE;
400 if (libusb_submit_transfer(devc->xfer_in) != 0) {
401 sr_err("Submit transfer failed: %s.",
402 libusb_error_name(ret));
404 devc->transfer_error = TRUE;
410 if (devc->state != devc->next_state)
411 sr_spew("State changed from %i to %i.",
412 devc->state, devc->next_state);
413 devc->state = devc->next_state;
415 if (devc->state == STATE_IDLE) {
416 stop_acquisition(sdi);
417 } else if (devc->state == STATE_SAMPLE) {
418 devc->next_state = STATE_WAIT_DATA_READY;
419 ret = libusb_submit_transfer(devc->xfer_in);
420 } else if (devc->state == STATE_WAIT_DEVICE_READY) {
421 ret = libusb_submit_transfer(devc->xfer_in);
425 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
426 devc->transfer_error = TRUE;
430 SR_PRIV int sl2_set_samplerate(const struct sr_dev_inst *sdi,
433 struct dev_context *devc;
438 for (i = 0; i < NUM_SAMPLERATES; i++) {
439 if (sl2_samplerates[i] == samplerate) {
440 devc->samplerate = samplerate;
441 devc->samplerate_id = NUM_SAMPLERATES - i - 1;
449 SR_PRIV int sl2_set_limit_samples(const struct sr_dev_inst *sdi,
450 uint64_t limit_samples)
452 struct dev_context *devc;
456 if (limit_samples == 0) {
457 sr_err("Invalid number of limit samples: %" PRIu64 ".",
462 if (limit_samples > MAX_SAMPLES)
463 limit_samples = MAX_SAMPLES;
465 sr_dbg("Limit samples set to %" PRIu64 ".", limit_samples);
467 devc->limit_samples = limit_samples;
472 SR_PRIV int sl2_convert_trigger(const struct sr_dev_inst *sdi)
474 struct dev_context *devc;
475 struct sr_trigger *trigger;
476 struct sr_trigger_stage *stage;
477 struct sr_trigger_match *match;
479 int num_triggers_anyedge;
483 /* Disable the trigger by default. */
484 devc->trigger_channel = TRIGGER_CHANNEL_0;
485 devc->trigger_type = TRIGGER_TYPE_NONE;
487 if (!(trigger = sr_session_trigger_get(sdi->session)))
490 if (g_slist_length(trigger->stages) > 1) {
491 sr_err("This device only supports 1 trigger stage.");
495 num_triggers_anyedge = 0;
496 for (l = trigger->stages; l; l = l->next) {
498 for (m = stage->matches; m; m = m->next) {
500 if (!match->channel->enabled)
501 /* Ignore disabled channels with a trigger. */
503 devc->trigger_channel = match->channel->index + 1;
504 switch (match->match) {
505 case SR_TRIGGER_RISING:
506 devc->trigger_type = TRIGGER_TYPE_POSEDGE;
508 case SR_TRIGGER_FALLING:
509 devc->trigger_type = TRIGGER_TYPE_NEGEDGE;
511 case SR_TRIGGER_EDGE:
512 devc->trigger_type = TRIGGER_TYPE_ANYEDGE;
513 num_triggers_anyedge++;
520 * Set trigger to any edge on all channels if the trigger for each
521 * channel is set to any edge.
523 if (num_triggers_anyedge == NUM_CHANNELS) {
524 devc->trigger_channel = TRIGGER_CHANNEL_ALL;
525 devc->trigger_type = TRIGGER_TYPE_ANYEDGE;
528 sr_dbg("Trigger set to channel 0x%02x and type 0x%02x.",
529 devc->trigger_channel, devc->trigger_type);
534 SR_PRIV int sl2_set_capture_ratio(const struct sr_dev_inst *sdi,
535 uint64_t capture_ratio)
537 struct dev_context *devc;
541 if (capture_ratio > 100) {
542 sr_err("Invalid capture ratio: %" PRIu64 " %%.", capture_ratio);
546 sr_info("Capture ratio set to %" PRIu64 " %%.", capture_ratio);
548 devc->capture_ratio = capture_ratio;
553 SR_PRIV int sl2_set_after_trigger_delay(const struct sr_dev_inst *sdi,
554 uint64_t after_trigger_delay)
556 struct dev_context *devc;
560 if (after_trigger_delay > MAX_AFTER_TRIGGER_DELAY) {
561 sr_err("Invalid after trigger delay: %" PRIu64 " ms.",
562 after_trigger_delay);
566 sr_info("After trigger delay set to %" PRIu64 " ms.",
567 after_trigger_delay);
569 devc->after_trigger_delay = after_trigger_delay;
574 SR_PRIV void sl2_calculate_trigger_samples(const struct sr_dev_inst *sdi)
576 struct dev_context *devc;
577 uint64_t pre_trigger_samples, post_trigger_samples;
578 uint16_t pre_trigger_bytes, post_trigger_bytes;
582 cr = devc->capture_ratio;
584 /* Ignore the capture ratio if no trigger is enabled. */
585 if (devc->trigger_type == TRIGGER_TYPE_NONE)
588 pre_trigger_samples = (devc->limit_samples * cr) / 100;
589 post_trigger_samples = (devc->limit_samples * (100 - cr)) / 100;
592 * Increase the number of post trigger samples by one to compensate the
593 * possible loss of a sample through integer rounding.
595 if (pre_trigger_samples + post_trigger_samples != devc->limit_samples)
596 post_trigger_samples++;
599 * The device requires the number of samples in multiples of 8 which
600 * will also be called sample bytes in the following.
602 pre_trigger_bytes = pre_trigger_samples / 8;
603 post_trigger_bytes = post_trigger_samples / 8;
606 * Round up the number of sample bytes to ensure that at least the
607 * requested number of samples will be acquired. Note that due to this
608 * rounding the buffer to store these sample bytes needs to be at least
609 * one sample byte larger than the minimal number of sample bytes
610 * needed to store the requested samples.
612 if (pre_trigger_samples % 8 != 0)
615 if (post_trigger_samples % 8 != 0)
616 post_trigger_bytes++;
618 sr_info("Pre trigger samples: %" PRIu64 ".", pre_trigger_samples);
619 sr_info("Post trigger samples: %" PRIu64 ".", post_trigger_samples);
620 sr_dbg("Pre trigger sample bytes: %" PRIu16 ".", pre_trigger_bytes);
621 sr_dbg("Post trigger sample bytes: %" PRIu16 ".", post_trigger_bytes);
623 devc->pre_trigger_samples = pre_trigger_samples;
624 devc->pre_trigger_bytes = pre_trigger_bytes;
625 devc->post_trigger_bytes = post_trigger_bytes;
628 SR_PRIV int sl2_get_device_info(struct sr_dev_driver *di,
629 struct sr_usb_dev_inst usb, struct device_info *dev_info)
631 struct drv_context *drvc;
632 uint8_t buffer[PACKET_LENGTH];
640 if (sr_usb_open(drvc->sr_ctx->libusb_ctx, &usb) != SR_OK)
644 * Determine if a kernel driver is active on this interface and, if so,
647 if (libusb_kernel_driver_active(usb.devhdl, USB_INTERFACE) == 1) {
648 ret = libusb_detach_kernel_driver(usb.devhdl,
652 sr_err("Failed to detach kernel driver: %s.",
653 libusb_error_name(ret));
654 libusb_close(usb.devhdl);
659 ret = libusb_claim_interface(usb.devhdl, USB_INTERFACE);
662 sr_err("Failed to claim interface: %s.",
663 libusb_error_name(ret));
664 libusb_close(usb.devhdl);
668 memset(buffer, 0, sizeof(buffer));
671 * Reset the device to ensure it is in a proper state to request the
672 * device information.
674 buffer[0] = CMD_RESET;
675 if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) {
676 sr_err("Resetting of device failed: %s.",
677 libusb_error_name(ret));
678 libusb_release_interface(usb.devhdl, USB_INTERFACE);
679 libusb_close(usb.devhdl);
683 buffer[0] = CMD_INFO;
684 if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) {
685 sr_err("Requesting of device information failed: %s.",
686 libusb_error_name(ret));
687 libusb_release_interface(usb.devhdl, USB_INTERFACE);
688 libusb_close(usb.devhdl);
692 if ((ret = sl2_transfer_in(usb.devhdl, buffer)) != PACKET_LENGTH) {
693 sr_err("Receiving of device information failed: %s.",
694 libusb_error_name(ret));
695 libusb_release_interface(usb.devhdl, USB_INTERFACE);
696 libusb_close(usb.devhdl);
700 memcpy(&(dev_info->serial), buffer + 1, sizeof(uint32_t));
701 dev_info->serial = GUINT32_FROM_LE(dev_info->serial);
703 dev_info->fw_ver_major = buffer[5];
704 dev_info->fw_ver_minor = buffer[6];
706 buffer[0] = CMD_RESET;
707 if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) {
708 sr_err("Device reset failed: %s.", libusb_error_name(ret));
709 libusb_release_interface(usb.devhdl, USB_INTERFACE);
710 libusb_close(usb.devhdl);
715 * Set the device to idle state. If the device is not in idle state it
716 * possibly will reset itself after a few seconds without being used
717 * and thereby close the connection.
719 buffer[0] = CMD_IDLE;
720 if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) {
721 sr_err("Failed to set device in idle state: %s.",
722 libusb_error_name(ret));
723 libusb_release_interface(usb.devhdl, USB_INTERFACE);
724 libusb_close(usb.devhdl);
728 ret = libusb_release_interface(usb.devhdl, USB_INTERFACE);
731 sr_err("Failed to release interface: %s.",
732 libusb_error_name(ret));
733 libusb_close(usb.devhdl);
737 libusb_close(usb.devhdl);
742 SR_PRIV int sl2_transfer_in(libusb_device_handle *dev_handle, uint8_t *data)
744 return libusb_control_transfer(dev_handle, USB_REQUEST_TYPE_IN,
745 USB_HID_GET_REPORT, USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
746 (unsigned char *)data, PACKET_LENGTH, USB_TIMEOUT_MS);
749 SR_PRIV int sl2_transfer_out(libusb_device_handle *dev_handle, uint8_t *data)
751 return libusb_control_transfer(dev_handle, USB_REQUEST_TYPE_OUT,
752 USB_HID_SET_REPORT, USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
753 (unsigned char *)data, PACKET_LENGTH, USB_TIMEOUT_MS);