static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) { (void)cb_data; fx2lafw_abort_acquisition(sdi->priv); return SR_OK; }
SR_PRIV int fx2lafw_start_acquisition(const struct sr_dev_inst *sdi) { struct sr_dev_driver *di; struct drv_context *drvc; struct dev_context *devc; int timeout, ret; size_t size; di = sdi->driver; drvc = di->context; devc = sdi->priv; devc->ctx = drvc->sr_ctx; devc->sent_samples = 0; devc->empty_transfer_count = 0; devc->acq_aborted = FALSE; if (configure_channels(sdi) != SR_OK) { sr_err("Failed to configure channels."); return SR_ERR; } timeout = get_timeout(devc); usb_source_add(sdi->session, devc->ctx, timeout, receive_data, drvc); size = get_buffer_size(devc); /* Prepare for analog sampling. */ if (g_slist_length(devc->enabled_analog_channels) > 0) { /* We need a buffer half the size of a transfer. */ devc->logic_buffer = g_try_malloc(size / 2); devc->analog_buffer = g_try_malloc( sizeof(float) * size / 2); } start_transfers(sdi); if ((ret = command_start_acquisition(sdi)) != SR_OK) { fx2lafw_abort_acquisition(devc); return ret; } return SR_OK; }
SR_PRIV void mso_send_data_proc(struct sr_dev_inst *sdi, uint8_t *data, size_t length, size_t sample_width) { size_t i; struct dev_context *devc; struct sr_datafeed_analog analog; struct sr_analog_encoding encoding; struct sr_analog_meaning meaning; struct sr_analog_spec spec; (void)sample_width; devc = sdi->priv; length /= 2; /* Send the logic */ for (i = 0; i < length; i++) { devc->logic_buffer[i] = data[i * 2]; /* Rescale to -10V - +10V from 0-255. */ devc->analog_buffer[i] = (data[i * 2 + 1] - 128.0f) / 12.8f; }; const struct sr_datafeed_logic logic = { .length = length, .unitsize = 1, .data = devc->logic_buffer }; const struct sr_datafeed_packet logic_packet = { .type = SR_DF_LOGIC, .payload = &logic }; sr_session_send(sdi, &logic_packet); sr_analog_init(&analog, &encoding, &meaning, &spec, 2); analog.meaning->channels = devc->enabled_analog_channels; analog.meaning->mq = SR_MQ_VOLTAGE; analog.meaning->unit = SR_UNIT_VOLT; analog.meaning->mqflags = 0 /* SR_MQFLAG_DC */; analog.num_samples = length; analog.data = devc->analog_buffer; const struct sr_datafeed_packet analog_packet = { .type = SR_DF_ANALOG, .payload = &analog }; sr_session_send(sdi, &analog_packet); } SR_PRIV void la_send_data_proc(struct sr_dev_inst *sdi, uint8_t *data, size_t length, size_t sample_width) { const struct sr_datafeed_logic logic = { .length = length, .unitsize = sample_width, .data = data }; const struct sr_datafeed_packet packet = { .type = SR_DF_LOGIC, .payload = &logic }; sr_session_send(sdi, &packet); } SR_PRIV void LIBUSB_CALL fx2lafw_receive_transfer(struct libusb_transfer *transfer) { struct sr_dev_inst *sdi; struct dev_context *devc; gboolean packet_has_error = FALSE; struct sr_datafeed_packet packet; unsigned int num_samples; int trigger_offset, cur_sample_count, unitsize; int pre_trigger_samples; sdi = transfer->user_data; devc = sdi->priv; /* * If acquisition has already ended, just free any queued up * transfer that come in. */ if (devc->acq_aborted) { free_transfer(transfer); return; } sr_dbg("receive_transfer(): status %s received %d bytes.", libusb_error_name(transfer->status), transfer->actual_length); /* Save incoming transfer before reusing the transfer struct. */ unitsize = devc->sample_wide ? 2 : 1; cur_sample_count = transfer->actual_length / unitsize; switch (transfer->status) { case LIBUSB_TRANSFER_NO_DEVICE: fx2lafw_abort_acquisition(devc); free_transfer(transfer); return; case LIBUSB_TRANSFER_COMPLETED: case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though. */ break; default: packet_has_error = TRUE; break; } if (transfer->actual_length == 0 || packet_has_error) { devc->empty_transfer_count++; if (devc->empty_transfer_count > MAX_EMPTY_TRANSFERS) { /* * The FX2 gave up. End the acquisition, the frontend * will work out that the samplecount is short. */ fx2lafw_abort_acquisition(devc); free_transfer(transfer); } else { resubmit_transfer(transfer); } return; } else { devc->empty_transfer_count = 0; } if (devc->trigger_fired) { if (!devc->limit_samples || devc->sent_samples < devc->limit_samples) { /* Send the incoming transfer to the session bus. */ if (devc->limit_samples && devc->sent_samples + cur_sample_count > devc->limit_samples) num_samples = devc->limit_samples - devc->sent_samples; else num_samples = cur_sample_count; if (devc->dslogic && devc->trigger_pos > devc->sent_samples && devc->trigger_pos <= devc->sent_samples + num_samples) { /* DSLogic trigger in this block. Send trigger position. */ trigger_offset = devc->trigger_pos - devc->sent_samples; /* Pre-trigger samples. */ devc->send_data_proc(sdi, (uint8_t *)transfer->buffer, trigger_offset * unitsize, unitsize); devc->sent_samples += trigger_offset; /* Trigger position. */ devc->trigger_pos = 0; packet.type = SR_DF_TRIGGER; packet.payload = NULL; sr_session_send(sdi, &packet); /* Post trigger samples. */ num_samples -= trigger_offset; devc->send_data_proc(sdi, (uint8_t *)transfer->buffer + trigger_offset * unitsize, num_samples * unitsize, unitsize); devc->sent_samples += num_samples; } else { devc->send_data_proc(sdi, (uint8_t *)transfer->buffer, num_samples * unitsize, unitsize); devc->sent_samples += num_samples; } } } else { trigger_offset = soft_trigger_logic_check(devc->stl, transfer->buffer, transfer->actual_length, &pre_trigger_samples); if (trigger_offset > -1) { devc->sent_samples += pre_trigger_samples; num_samples = cur_sample_count - trigger_offset; if (devc->limit_samples && num_samples > devc->limit_samples - devc->sent_samples) num_samples = devc->limit_samples - devc->sent_samples; devc->send_data_proc(sdi, (uint8_t *)transfer->buffer + trigger_offset * unitsize, num_samples * unitsize, unitsize); devc->sent_samples += num_samples; devc->trigger_fired = TRUE; } } if (devc->limit_samples && devc->sent_samples >= devc->limit_samples) { fx2lafw_abort_acquisition(devc); free_transfer(transfer); } else resubmit_transfer(transfer); } static unsigned int to_bytes_per_ms(unsigned int samplerate) { return samplerate / 1000; } SR_PRIV size_t fx2lafw_get_buffer_size(struct dev_context *devc) { size_t s; /* * The buffer should be large enough to hold 10ms of data and * a multiple of 512. */ s = 10 * to_bytes_per_ms(devc->cur_samplerate); return (s + 511) & ~511; }
static int start_transfers(const struct sr_dev_inst *sdi) { struct dev_context *devc; struct sr_usb_dev_inst *usb; struct sr_trigger *trigger; struct libusb_transfer *transfer; unsigned int i, num_transfers; int timeout, ret; unsigned char *buf; size_t size; devc = sdi->priv; usb = sdi->conn; devc->sent_samples = 0; devc->acq_aborted = FALSE; devc->empty_transfer_count = 0; if ((trigger = sr_session_trigger_get(sdi->session))) { int pre_trigger_samples = 0; if (devc->limit_samples > 0) pre_trigger_samples = (devc->capture_ratio * devc->limit_samples) / 100; devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples); if (!devc->stl) return SR_ERR_MALLOC; devc->trigger_fired = FALSE; } else devc->trigger_fired = TRUE; num_transfers = get_number_of_transfers(devc); size = get_buffer_size(devc); devc->submitted_transfers = 0; devc->transfers = g_try_malloc0(sizeof(*devc->transfers) * num_transfers); if (!devc->transfers) { sr_err("USB transfers malloc failed."); return SR_ERR_MALLOC; } timeout = get_timeout(devc); devc->num_transfers = num_transfers; for (i = 0; i < num_transfers; i++) { if (!(buf = g_try_malloc(size))) { sr_err("USB transfer buffer malloc failed."); return SR_ERR_MALLOC; } transfer = libusb_alloc_transfer(0); libusb_fill_bulk_transfer(transfer, usb->devhdl, 2 | LIBUSB_ENDPOINT_IN, buf, size, receive_transfer, (void *)sdi, timeout); sr_info("submitting transfer: %d", i); if ((ret = libusb_submit_transfer(transfer)) != 0) { sr_err("Failed to submit transfer: %s.", libusb_error_name(ret)); libusb_free_transfer(transfer); g_free(buf); fx2lafw_abort_acquisition(devc); return SR_ERR; } devc->transfers[i] = transfer; devc->submitted_transfers++; } /* * If this device has analog channels and at least one of them is * enabled, use mso_send_data_proc() to properly handle the analog * data. Otherwise use la_send_data_proc(). */ if (g_slist_length(devc->enabled_analog_channels) > 0) devc->send_data_proc = mso_send_data_proc; else devc->send_data_proc = la_send_data_proc; std_session_send_df_header(sdi); return SR_OK; }
static void mso_send_data_proc(struct sr_dev_inst *sdi, uint8_t *data, size_t length, size_t sample_width) { size_t i; struct dev_context *devc; struct sr_datafeed_analog analog; struct sr_analog_encoding encoding; struct sr_analog_meaning meaning; struct sr_analog_spec spec; (void)sample_width; devc = sdi->priv; length /= 2; /* Send the logic */ for (i = 0; i < length; i++) { devc->logic_buffer[i] = data[i * 2]; /* Rescale to -10V - +10V from 0-255. */ devc->analog_buffer[i] = (data[i * 2 + 1] - 128.0f) / 12.8f; }; const struct sr_datafeed_logic logic = { .length = length, .unitsize = 1, .data = devc->logic_buffer }; const struct sr_datafeed_packet logic_packet = { .type = SR_DF_LOGIC, .payload = &logic }; sr_session_send(sdi, &logic_packet); sr_analog_init(&analog, &encoding, &meaning, &spec, 2); analog.meaning->channels = devc->enabled_analog_channels; analog.meaning->mq = SR_MQ_VOLTAGE; analog.meaning->unit = SR_UNIT_VOLT; analog.meaning->mqflags = 0 /* SR_MQFLAG_DC */; analog.num_samples = length; analog.data = devc->analog_buffer; const struct sr_datafeed_packet analog_packet = { .type = SR_DF_ANALOG, .payload = &analog }; sr_session_send(sdi, &analog_packet); } static void la_send_data_proc(struct sr_dev_inst *sdi, uint8_t *data, size_t length, size_t sample_width) { const struct sr_datafeed_logic logic = { .length = length, .unitsize = sample_width, .data = data }; const struct sr_datafeed_packet packet = { .type = SR_DF_LOGIC, .payload = &logic }; sr_session_send(sdi, &packet); } static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer) { struct sr_dev_inst *sdi; struct dev_context *devc; gboolean packet_has_error = FALSE; unsigned int num_samples; int trigger_offset, cur_sample_count, unitsize; int pre_trigger_samples; sdi = transfer->user_data; devc = sdi->priv; /* * If acquisition has already ended, just free any queued up * transfer that come in. */ if (devc->acq_aborted) { free_transfer(transfer); return; } sr_dbg("receive_transfer(): status %s received %d bytes.", libusb_error_name(transfer->status), transfer->actual_length); /* Save incoming transfer before reusing the transfer struct. */ unitsize = devc->sample_wide ? 2 : 1; cur_sample_count = transfer->actual_length / unitsize; switch (transfer->status) { case LIBUSB_TRANSFER_NO_DEVICE: fx2lafw_abort_acquisition(devc); free_transfer(transfer); return; case LIBUSB_TRANSFER_COMPLETED: case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though. */ break; default: packet_has_error = TRUE; break; } if (transfer->actual_length == 0 || packet_has_error) { devc->empty_transfer_count++; if (devc->empty_transfer_count > MAX_EMPTY_TRANSFERS) { /* * The FX2 gave up. End the acquisition, the frontend * will work out that the samplecount is short. */ fx2lafw_abort_acquisition(devc); free_transfer(transfer); } else { resubmit_transfer(transfer); } return; } else { devc->empty_transfer_count = 0; } if (devc->trigger_fired) { if (!devc->limit_samples || devc->sent_samples < devc->limit_samples) { /* Send the incoming transfer to the session bus. */ if (devc->limit_samples && devc->sent_samples + cur_sample_count > devc->limit_samples) num_samples = devc->limit_samples - devc->sent_samples; else num_samples = cur_sample_count; devc->send_data_proc(sdi, (uint8_t *)transfer->buffer, num_samples * unitsize, unitsize); devc->sent_samples += num_samples; } } else { trigger_offset = soft_trigger_logic_check(devc->stl, transfer->buffer, transfer->actual_length, &pre_trigger_samples); if (trigger_offset > -1) { devc->sent_samples += pre_trigger_samples; num_samples = cur_sample_count - trigger_offset; if (devc->limit_samples && num_samples > devc->limit_samples - devc->sent_samples) num_samples = devc->limit_samples - devc->sent_samples; devc->send_data_proc(sdi, (uint8_t *)transfer->buffer + trigger_offset * unitsize, num_samples * unitsize, unitsize); devc->sent_samples += num_samples; devc->trigger_fired = TRUE; } } if (devc->limit_samples && devc->sent_samples >= devc->limit_samples) { fx2lafw_abort_acquisition(devc); free_transfer(transfer); } else resubmit_transfer(transfer); } static int configure_channels(const struct sr_dev_inst *sdi) { struct dev_context *devc; const GSList *l; int p; struct sr_channel *ch; uint32_t channel_mask = 0, num_analog = 0; devc = sdi->priv; g_slist_free(devc->enabled_analog_channels); devc->enabled_analog_channels = NULL; for (l = sdi->channels, p = 0; l; l = l->next, p++) { ch = l->data; if ((p <= NUM_CHANNELS) && (ch->type == SR_CHANNEL_ANALOG) && (ch->enabled)) { num_analog++; devc->enabled_analog_channels = g_slist_append(devc->enabled_analog_channels, ch); } else { channel_mask |= ch->enabled << p; } } /* * Use wide sampling if either any of the LA channels 8..15 is enabled, * and/or at least one analog channel is enabled. */ devc->sample_wide = channel_mask > 0xff || num_analog > 0; return SR_OK; } static unsigned int to_bytes_per_ms(unsigned int samplerate) { return samplerate / 1000; } static size_t get_buffer_size(struct dev_context *devc) { size_t s; /* * The buffer should be large enough to hold 10ms of data and * a multiple of 512. */ s = 10 * to_bytes_per_ms(devc->cur_samplerate); return (s + 511) & ~511; }
static int dev_acquisition_stop(struct sr_dev_inst *sdi) { fx2lafw_abort_acquisition(sdi->priv); return SR_OK; }
SR_PRIV void fx2lafw_receive_transfer(struct libusb_transfer *transfer) { gboolean packet_has_error = FALSE; struct sr_datafeed_packet packet; struct sr_datafeed_logic logic; struct dev_context *devc; int trigger_offset, i, sample_width, cur_sample_count; int trigger_offset_bytes; uint8_t *cur_buf; uint16_t cur_sample; devc = transfer->user_data; /* * If acquisition has already ended, just free any queued up * transfer that come in. */ if (devc->num_samples == -1) { free_transfer(transfer); return; } sr_info("receive_transfer(): status %d received %d bytes.", transfer->status, transfer->actual_length); /* Save incoming transfer before reusing the transfer struct. */ cur_buf = transfer->buffer; sample_width = devc->sample_wide ? 2 : 1; cur_sample_count = transfer->actual_length / sample_width; switch (transfer->status) { case LIBUSB_TRANSFER_NO_DEVICE: fx2lafw_abort_acquisition(devc); free_transfer(transfer); return; case LIBUSB_TRANSFER_COMPLETED: case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though. */ break; default: packet_has_error = TRUE; break; } if (transfer->actual_length == 0 || packet_has_error) { devc->empty_transfer_count++; if (devc->empty_transfer_count > MAX_EMPTY_TRANSFERS) { /* * The FX2 gave up. End the acquisition, the frontend * will work out that the samplecount is short. */ fx2lafw_abort_acquisition(devc); free_transfer(transfer); } else { resubmit_transfer(transfer); } return; } else { devc->empty_transfer_count = 0; } trigger_offset = 0; if (devc->trigger_stage >= 0) { for (i = 0; i < cur_sample_count; i++) { cur_sample = devc->sample_wide ? *((uint16_t *)cur_buf + i) : *((uint8_t *)cur_buf + i); if ((cur_sample & devc->trigger_mask[devc->trigger_stage]) == devc->trigger_value[devc->trigger_stage]) { /* Match on this trigger stage. */ devc->trigger_buffer[devc->trigger_stage] = cur_sample; devc->trigger_stage++; if (devc->trigger_stage == NUM_TRIGGER_STAGES || devc->trigger_mask[devc->trigger_stage] == 0) { /* Match on all trigger stages, we're done. */ trigger_offset = i + 1; /* * TODO: Send pre-trigger buffer to session bus. * Tell the frontend we hit the trigger here. */ packet.type = SR_DF_TRIGGER; packet.payload = NULL; sr_session_send(devc->cb_data, &packet); /* * Send the samples that triggered it, * since we're skipping past them. */ packet.type = SR_DF_LOGIC; packet.payload = &logic; logic.unitsize = sample_width; logic.length = devc->trigger_stage * logic.unitsize; logic.data = devc->trigger_buffer; sr_session_send(devc->cb_data, &packet); devc->trigger_stage = TRIGGER_FIRED; break; } } else if (devc->trigger_stage > 0) { /* * We had a match before, but not in the next sample. However, we may * have a match on this stage in the next bit -- trigger on 0001 will * fail on seeing 00001, so we need to go back to stage 0 -- but at * the next sample from the one that matched originally, which the * counter increment at the end of the loop takes care of. */ i -= devc->trigger_stage; if (i < -1) i = -1; /* Oops, went back past this buffer. */ /* Reset trigger stage. */ devc->trigger_stage = 0; } } } if (devc->trigger_stage == TRIGGER_FIRED) { /* Send the incoming transfer to the session bus. */ trigger_offset_bytes = trigger_offset * sample_width; packet.type = SR_DF_LOGIC; packet.payload = &logic; logic.length = transfer->actual_length - trigger_offset_bytes; logic.unitsize = sample_width; logic.data = cur_buf + trigger_offset_bytes; sr_session_send(devc->cb_data, &packet); devc->num_samples += cur_sample_count; if (devc->limit_samples && (unsigned int)devc->num_samples > devc->limit_samples) { fx2lafw_abort_acquisition(devc); free_transfer(transfer); return; } } else { /* * TODO: Buffer pre-trigger data in capture * ratio-sized buffer. */ } resubmit_transfer(transfer); }
static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) { struct dev_context *devc; struct drv_context *drvc; struct sr_usb_dev_inst *usb; struct sr_trigger *trigger; struct libusb_transfer *transfer; unsigned int i, timeout, num_transfers; int ret; unsigned char *buf; size_t size; if (sdi->status != SR_ST_ACTIVE) return SR_ERR_DEV_CLOSED; drvc = di->priv; devc = sdi->priv; usb = sdi->conn; devc->cb_data = cb_data; devc->sent_samples = 0; devc->acq_aborted = FALSE; devc->empty_transfer_count = 0; if ((trigger = sr_session_trigger_get())) { devc->stl = soft_trigger_logic_new(sdi, trigger); devc->trigger_fired = FALSE; } else devc->trigger_fired = TRUE; timeout = fx2lafw_get_timeout(devc); num_transfers = fx2lafw_get_number_of_transfers(devc); size = fx2lafw_get_buffer_size(devc); devc->submitted_transfers = 0; devc->transfers = g_try_malloc0(sizeof(*devc->transfers) * num_transfers); if (!devc->transfers) { sr_err("USB transfers malloc failed."); return SR_ERR_MALLOC; } devc->num_transfers = num_transfers; for (i = 0; i < num_transfers; i++) { if (!(buf = g_try_malloc(size))) { sr_err("USB transfer buffer malloc failed."); return SR_ERR_MALLOC; } transfer = libusb_alloc_transfer(0); libusb_fill_bulk_transfer(transfer, usb->devhdl, 2 | LIBUSB_ENDPOINT_IN, buf, size, fx2lafw_receive_transfer, (void *)sdi, timeout); if ((ret = libusb_submit_transfer(transfer)) != 0) { sr_err("Failed to submit transfer: %s.", libusb_error_name(ret)); libusb_free_transfer(transfer); g_free(buf); fx2lafw_abort_acquisition(devc); return SR_ERR; } devc->transfers[i] = transfer; devc->submitted_transfers++; } devc->ctx = drvc->sr_ctx; usb_source_add(devc->ctx, timeout, receive_data, NULL); /* Send header packet to the session bus. */ std_session_send_df_header(cb_data, LOG_PREFIX); if ((ret = fx2lafw_command_start_acquisition(sdi)) != SR_OK) { fx2lafw_abort_acquisition(devc); return ret; } return SR_OK; }