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 std_session_send_df_trigger(sdi);
129 for (; k >= 0; k--) {
131 * Stop processing if all requested samples are
134 if (devc->samples_processed == devc->limit_samples)
140 * Extract the current sample for each enabled channel
141 * and store them in the buffer.
143 for (j = 0; j < devc->num_enabled_channels; j++) {
144 tmp = (ptr[j][i] & (1 << k)) >> k;
145 buffer[n] |= tmp << devc->channel_map[j];
149 devc->samples_processed++;
152 * Send all processed samples and the trigger if the
153 * number of processed samples reaches the calculated
154 * number of pre trigger samples.
156 if (devc->samples_processed == devc->pre_trigger_samples &&
157 devc->trigger_type != TRIGGER_TYPE_NONE) {
158 packet.type = SR_DF_LOGIC;
159 packet.payload = &logic;
163 sr_session_send(sdi, &packet);
165 std_session_send_df_trigger(sdi);
173 packet.type = SR_DF_LOGIC;
174 packet.payload = &logic;
178 sr_session_send(sdi, &packet);
182 SR_PRIV int ikalogic_scanalogic2_receive_data(int fd, int revents, void *cb_data)
184 struct sr_dev_inst *sdi;
185 struct sr_dev_driver *di;
186 struct dev_context *devc;
187 struct drv_context *drvc;
189 int64_t current_time, time_elapsed;
195 if (!(sdi = cb_data))
198 if (!(devc = sdi->priv))
203 current_time = g_get_monotonic_time();
205 if (devc->state == STATE_WAIT_DATA_READY &&
206 !devc->wait_data_ready_locked) {
207 time_elapsed = current_time - devc->wait_data_ready_time;
210 * Check here for stopping in addition to the transfer
211 * callback functions to avoid waiting until the
212 * WAIT_DATA_READY_INTERVAL has expired.
214 if (sdi->status == SR_ST_STOPPING) {
215 if (!devc->stopping_in_progress) {
216 devc->next_state = STATE_RESET_AND_IDLE;
217 devc->stopping_in_progress = TRUE;
218 ret = libusb_submit_transfer(devc->xfer_in);
220 } else if (time_elapsed >= WAIT_DATA_READY_INTERVAL) {
221 devc->wait_data_ready_locked = TRUE;
222 ret = libusb_submit_transfer(devc->xfer_in);
227 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
228 abort_acquisition(sdi);
235 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
238 /* Check if an error occurred on a transfer. */
239 if (devc->transfer_error)
240 abort_acquisition(sdi);
245 SR_PRIV void LIBUSB_CALL sl2_receive_transfer_in( struct libusb_transfer *transfer)
247 struct sr_dev_inst *sdi;
248 struct dev_context *devc;
249 uint8_t last_channel;
252 sdi = transfer->user_data;
255 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
256 sr_err("Transfer to device failed: %s.",
257 libusb_error_name(transfer->status));
258 devc->transfer_error = TRUE;
262 if (sdi->status == SR_ST_STOPPING && !devc->stopping_in_progress) {
263 devc->next_state = STATE_RESET_AND_IDLE;
264 devc->stopping_in_progress = TRUE;
266 if (libusb_submit_transfer(devc->xfer_in) != 0) {
267 sr_err("Submit transfer failed: %s.",
268 libusb_error_name(ret));
269 devc->transfer_error = TRUE;
275 if (devc->state != devc->next_state)
276 sr_spew("State changed from %i to %i.",
277 devc->state, devc->next_state);
278 devc->state = devc->next_state;
280 if (devc->state == STATE_WAIT_DATA_READY) {
281 /* Check if the received data are a valid device status. */
282 if (devc->xfer_data_in[0] == 0x05) {
283 if (devc->xfer_data_in[1] == STATUS_WAITING_FOR_TRIGGER)
284 sr_dbg("Waiting for trigger.");
285 else if (devc->xfer_data_in[1] == STATUS_SAMPLING)
286 sr_dbg("Sampling in progress.");
290 * Check if the received data are a valid device status and the
291 * sample data are ready.
293 if (devc->xfer_data_in[0] == 0x05 &&
294 devc->xfer_data_in[1] == STATUS_DATA_READY) {
295 devc->next_state = STATE_RECEIVE_DATA;
296 ret = libusb_submit_transfer(transfer);
298 devc->wait_data_ready_locked = FALSE;
299 devc->wait_data_ready_time = g_get_monotonic_time();
301 } else if (devc->state == STATE_RECEIVE_DATA) {
302 last_channel = devc->channel_map[devc->num_enabled_channels - 1];
304 if (devc->channel < last_channel) {
305 buffer_sample_data(sdi);
306 } else if (devc->channel == last_channel) {
307 process_sample_data(sdi);
310 * Stop acquisition because all samples of enabled
311 * channels are processed.
313 devc->next_state = STATE_RESET_AND_IDLE;
316 devc->sample_packet++;
317 devc->sample_packet %= devc->num_sample_packets;
319 if (devc->sample_packet == 0)
322 ret = libusb_submit_transfer(transfer);
323 } else if (devc->state == STATE_RESET_AND_IDLE) {
324 /* Check if the received data are a valid device status. */
325 if (devc->xfer_data_in[0] == 0x05) {
326 if (devc->xfer_data_in[1] == STATUS_DEVICE_READY) {
327 devc->next_state = STATE_IDLE;
328 devc->xfer_data_out[0] = CMD_IDLE;
330 devc->next_state = STATE_WAIT_DEVICE_READY;
331 devc->xfer_data_out[0] = CMD_RESET;
334 ret = libusb_submit_transfer(devc->xfer_out);
337 * The received device status is invalid which
338 * indicates that the device is not ready to accept
339 * commands. Request a new device status until a valid
340 * device status is received.
342 ret = libusb_submit_transfer(transfer);
344 } else if (devc->state == STATE_WAIT_DEVICE_READY) {
345 /* Check if the received data are a valid device status. */
346 if (devc->xfer_data_in[0] == 0x05) {
347 if (devc->xfer_data_in[1] == STATUS_DEVICE_READY) {
348 devc->next_state = STATE_IDLE;
349 devc->xfer_data_out[0] = CMD_IDLE;
352 * The received device status is valid but the
353 * device is not ready. Probably the device did
354 * not recognize the last reset. Reset the
357 devc->xfer_data_out[0] = CMD_RESET;
360 ret = libusb_submit_transfer(devc->xfer_out);
363 * The device is not ready and therefore not able to
364 * change to the idle state. Request a new device
365 * status until the device is ready.
367 ret = libusb_submit_transfer(transfer);
372 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
373 devc->transfer_error = TRUE;
377 SR_PRIV void LIBUSB_CALL sl2_receive_transfer_out( struct libusb_transfer *transfer)
379 struct sr_dev_inst *sdi;
380 struct dev_context *devc;
383 sdi = transfer->user_data;
386 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
387 sr_err("Transfer to device failed: %s.",
388 libusb_error_name(transfer->status));
389 devc->transfer_error = TRUE;
393 if (sdi->status == SR_ST_STOPPING && !devc->stopping_in_progress) {
394 devc->next_state = STATE_RESET_AND_IDLE;
395 devc->stopping_in_progress = TRUE;
397 if (libusb_submit_transfer(devc->xfer_in) != 0) {
398 sr_err("Submit transfer failed: %s.",
399 libusb_error_name(ret));
401 devc->transfer_error = TRUE;
407 if (devc->state != devc->next_state)
408 sr_spew("State changed from %i to %i.",
409 devc->state, devc->next_state);
410 devc->state = devc->next_state;
412 if (devc->state == STATE_IDLE) {
413 stop_acquisition(sdi);
414 } else if (devc->state == STATE_SAMPLE) {
415 devc->next_state = STATE_WAIT_DATA_READY;
416 ret = libusb_submit_transfer(devc->xfer_in);
417 } else if (devc->state == STATE_WAIT_DEVICE_READY) {
418 ret = libusb_submit_transfer(devc->xfer_in);
422 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
423 devc->transfer_error = TRUE;
427 SR_PRIV int sl2_set_samplerate(const struct sr_dev_inst *sdi,
430 struct dev_context *devc;
435 for (i = 0; i < NUM_SAMPLERATES; i++) {
436 if (sl2_samplerates[i] == samplerate) {
437 devc->samplerate = samplerate;
438 devc->samplerate_id = NUM_SAMPLERATES - i - 1;
446 SR_PRIV int sl2_set_limit_samples(const struct sr_dev_inst *sdi,
447 uint64_t limit_samples)
449 struct dev_context *devc;
453 if (limit_samples == 0) {
454 sr_err("Invalid number of limit samples: %" PRIu64 ".",
459 if (limit_samples > MAX_SAMPLES)
460 limit_samples = MAX_SAMPLES;
462 sr_dbg("Limit samples set to %" PRIu64 ".", limit_samples);
464 devc->limit_samples = limit_samples;
469 SR_PRIV int sl2_convert_trigger(const struct sr_dev_inst *sdi)
471 struct dev_context *devc;
472 struct sr_trigger *trigger;
473 struct sr_trigger_stage *stage;
474 struct sr_trigger_match *match;
476 int num_triggers_anyedge;
480 /* Disable the trigger by default. */
481 devc->trigger_channel = TRIGGER_CHANNEL_0;
482 devc->trigger_type = TRIGGER_TYPE_NONE;
484 if (!(trigger = sr_session_trigger_get(sdi->session)))
487 if (g_slist_length(trigger->stages) > 1) {
488 sr_err("This device only supports 1 trigger stage.");
492 num_triggers_anyedge = 0;
493 for (l = trigger->stages; l; l = l->next) {
495 for (m = stage->matches; m; m = m->next) {
497 if (!match->channel->enabled)
498 /* Ignore disabled channels with a trigger. */
500 devc->trigger_channel = match->channel->index + 1;
501 switch (match->match) {
502 case SR_TRIGGER_RISING:
503 devc->trigger_type = TRIGGER_TYPE_POSEDGE;
505 case SR_TRIGGER_FALLING:
506 devc->trigger_type = TRIGGER_TYPE_NEGEDGE;
508 case SR_TRIGGER_EDGE:
509 devc->trigger_type = TRIGGER_TYPE_ANYEDGE;
510 num_triggers_anyedge++;
517 * Set trigger to any edge on all channels if the trigger for each
518 * channel is set to any edge.
520 if (num_triggers_anyedge == NUM_CHANNELS) {
521 devc->trigger_channel = TRIGGER_CHANNEL_ALL;
522 devc->trigger_type = TRIGGER_TYPE_ANYEDGE;
525 sr_dbg("Trigger set to channel 0x%02x and type 0x%02x.",
526 devc->trigger_channel, devc->trigger_type);
531 SR_PRIV int sl2_set_after_trigger_delay(const struct sr_dev_inst *sdi,
532 uint64_t after_trigger_delay)
534 struct dev_context *devc;
538 if (after_trigger_delay > MAX_AFTER_TRIGGER_DELAY) {
539 sr_err("Invalid after trigger delay: %" PRIu64 " ms.",
540 after_trigger_delay);
544 sr_info("After trigger delay set to %" PRIu64 " ms.",
545 after_trigger_delay);
547 devc->after_trigger_delay = after_trigger_delay;
552 SR_PRIV void sl2_calculate_trigger_samples(const struct sr_dev_inst *sdi)
554 struct dev_context *devc;
555 uint64_t pre_trigger_samples, post_trigger_samples;
556 uint16_t pre_trigger_bytes, post_trigger_bytes;
560 cr = devc->capture_ratio;
562 /* Ignore the capture ratio if no trigger is enabled. */
563 if (devc->trigger_type == TRIGGER_TYPE_NONE)
566 pre_trigger_samples = (devc->limit_samples * cr) / 100;
567 post_trigger_samples = (devc->limit_samples * (100 - cr)) / 100;
570 * Increase the number of post trigger samples by one to compensate the
571 * possible loss of a sample through integer rounding.
573 if (pre_trigger_samples + post_trigger_samples != devc->limit_samples)
574 post_trigger_samples++;
577 * The device requires the number of samples in multiples of 8 which
578 * will also be called sample bytes in the following.
580 pre_trigger_bytes = pre_trigger_samples / 8;
581 post_trigger_bytes = post_trigger_samples / 8;
584 * Round up the number of sample bytes to ensure that at least the
585 * requested number of samples will be acquired. Note that due to this
586 * rounding the buffer to store these sample bytes needs to be at least
587 * one sample byte larger than the minimal number of sample bytes
588 * needed to store the requested samples.
590 if (pre_trigger_samples % 8 != 0)
593 if (post_trigger_samples % 8 != 0)
594 post_trigger_bytes++;
596 sr_info("Pre trigger samples: %" PRIu64 ".", pre_trigger_samples);
597 sr_info("Post trigger samples: %" PRIu64 ".", post_trigger_samples);
598 sr_dbg("Pre trigger sample bytes: %" PRIu16 ".", pre_trigger_bytes);
599 sr_dbg("Post trigger sample bytes: %" PRIu16 ".", post_trigger_bytes);
601 devc->pre_trigger_samples = pre_trigger_samples;
602 devc->pre_trigger_bytes = pre_trigger_bytes;
603 devc->post_trigger_bytes = post_trigger_bytes;
606 SR_PRIV int sl2_get_device_info(struct sr_dev_driver *di,
607 struct sr_usb_dev_inst usb, struct device_info *dev_info)
609 struct drv_context *drvc;
610 uint8_t buffer[PACKET_LENGTH];
618 if (sr_usb_open(drvc->sr_ctx->libusb_ctx, &usb) != SR_OK)
621 if (libusb_kernel_driver_active(usb.devhdl, USB_INTERFACE) == 1) {
622 ret = libusb_detach_kernel_driver(usb.devhdl,
626 sr_err("Failed to detach kernel driver: %s.",
627 libusb_error_name(ret));
628 libusb_close(usb.devhdl);
633 ret = libusb_claim_interface(usb.devhdl, USB_INTERFACE);
636 sr_err("Failed to claim interface: %s.",
637 libusb_error_name(ret));
638 libusb_close(usb.devhdl);
642 memset(buffer, 0, sizeof(buffer));
645 * Reset the device to ensure it is in a proper state to request the
646 * device information.
648 buffer[0] = CMD_RESET;
649 if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) {
650 sr_err("Resetting of device failed: %s.",
651 libusb_error_name(ret));
652 libusb_release_interface(usb.devhdl, USB_INTERFACE);
653 libusb_close(usb.devhdl);
657 buffer[0] = CMD_INFO;
658 if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) {
659 sr_err("Requesting of device information failed: %s.",
660 libusb_error_name(ret));
661 libusb_release_interface(usb.devhdl, USB_INTERFACE);
662 libusb_close(usb.devhdl);
666 if ((ret = sl2_transfer_in(usb.devhdl, buffer)) != PACKET_LENGTH) {
667 sr_err("Receiving of device information failed: %s.",
668 libusb_error_name(ret));
669 libusb_release_interface(usb.devhdl, USB_INTERFACE);
670 libusb_close(usb.devhdl);
674 memcpy(&(dev_info->serial), buffer + 1, sizeof(uint32_t));
675 dev_info->serial = GUINT32_FROM_LE(dev_info->serial);
677 dev_info->fw_ver_major = buffer[5];
678 dev_info->fw_ver_minor = buffer[6];
680 buffer[0] = CMD_RESET;
681 if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) {
682 sr_err("Device reset failed: %s.", libusb_error_name(ret));
683 libusb_release_interface(usb.devhdl, USB_INTERFACE);
684 libusb_close(usb.devhdl);
689 * Set the device to idle state. If the device is not in idle state it
690 * possibly will reset itself after a few seconds without being used
691 * and thereby close the connection.
693 buffer[0] = CMD_IDLE;
694 if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) {
695 sr_err("Failed to set device in idle state: %s.",
696 libusb_error_name(ret));
697 libusb_release_interface(usb.devhdl, USB_INTERFACE);
698 libusb_close(usb.devhdl);
702 ret = libusb_release_interface(usb.devhdl, USB_INTERFACE);
705 sr_err("Failed to release interface: %s.",
706 libusb_error_name(ret));
707 libusb_close(usb.devhdl);
711 libusb_close(usb.devhdl);
716 SR_PRIV int sl2_transfer_in(libusb_device_handle *dev_handle, uint8_t *data)
718 return libusb_control_transfer(dev_handle, USB_REQUEST_TYPE_IN,
719 USB_HID_GET_REPORT, USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
720 (unsigned char *)data, PACKET_LENGTH, USB_TIMEOUT_MS);
723 SR_PRIV int sl2_transfer_out(libusb_device_handle *dev_handle, uint8_t *data)
725 return libusb_control_transfer(dev_handle, USB_REQUEST_TYPE_OUT,
726 USB_HID_SET_REPORT, USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
727 (unsigned char *)data, PACKET_LENGTH, USB_TIMEOUT_MS);