From: Andrej Valek Date: Mon, 30 Apr 2018 18:00:11 +0000 (+0200) Subject: hantek-4032l: Implement trigger handling. X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=3dc976fe9fcc21a1b82087fe6b969cf997346bdc;p=libsigrok.git hantek-4032l: Implement trigger handling. Send trigger position to upper layer. Signed-off-by: Andrej Valek --- diff --git a/src/hardware/hantek-4032l/api.c b/src/hardware/hantek-4032l/api.c index 97e9979b..33c55c69 100644 --- a/src/hardware/hantek-4032l/api.c +++ b/src/hardware/hantek-4032l/api.c @@ -35,7 +35,7 @@ static const uint32_t drvopts[] = { static const uint32_t devopts[] = { SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, - SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET, + SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, SR_CONF_TRIGGER_MATCH | SR_CONF_LIST, SR_CONF_CONN | SR_CONF_GET, @@ -213,7 +213,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) devc->cmd_pkt.pwm_a = h4032l_voltage2pwm(2.5); devc->cmd_pkt.pwm_b = h4032l_voltage2pwm(2.5); devc->cmd_pkt.sample_size = 16384; - devc->cmd_pkt.pre_trigger_size = 1024; devc->status = H4032L_STATUS_IDLE; @@ -342,9 +341,15 @@ static int config_set(uint32_t key, GVariant *data, cmd_pkt->sample_rate = i; break; } - case SR_CONF_CAPTURE_RATIO: - devc->capture_ratio = g_variant_get_uint64(data); - break; + case SR_CONF_CAPTURE_RATIO: { + uint64_t capture_ratio = g_variant_get_uint64(data); + if (capture_ratio > 99) { + sr_err("Invalid capture ratio."); + return SR_ERR; + } + devc->capture_ratio = capture_ratio; + break; + } case SR_CONF_LIMIT_SAMPLES: { uint64_t number_samples = g_variant_get_uint64(data); number_samples += 511; @@ -409,9 +414,11 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi) /* Initialize variables. */ devc->acq_aborted = FALSE; devc->submitted_transfers = 0; + devc->sent_samples = 0; /* Calculate packet ratio. */ cmd_pkt->pre_trigger_size = (cmd_pkt->sample_size * devc->capture_ratio) / 100; + devc->trigger_pos = cmd_pkt->pre_trigger_size; cmd_pkt->trig_flags.enable_trigger1 = 0; cmd_pkt->trig_flags.enable_trigger2 = 0; diff --git a/src/hardware/hantek-4032l/protocol.c b/src/hardware/hantek-4032l/protocol.c index 5aa35de4..a166c0a1 100644 --- a/src/hardware/hantek-4032l/protocol.c +++ b/src/hardware/hantek-4032l/protocol.c @@ -101,6 +101,48 @@ static void resubmit_transfer(struct libusb_transfer *transfer) free_transfer(transfer); } +static void send_data(struct sr_dev_inst *sdi, + uint32_t *data, size_t sample_count) +{ + struct dev_context *devc = sdi->priv; + struct sr_datafeed_logic logic = { + .length = sample_count * sizeof(uint32_t), + .unitsize = sizeof(uint32_t), + .data = data + }; + const struct sr_datafeed_packet packet = { + .type = SR_DF_LOGIC, + .payload = &logic + }; + const struct sr_datafeed_packet trig = { + .type = SR_DF_TRIGGER, + .payload = NULL + }; + size_t trigger_offset; + + if (devc->trigger_pos >= devc->sent_samples && + devc->trigger_pos < (devc->sent_samples + sample_count)) { + /* Get trigger position. */ + trigger_offset = devc->trigger_pos - devc->sent_samples; + logic.length = trigger_offset * sizeof(uint32_t); + if (logic.length) + sr_session_send(sdi, &packet); + + /* Send trigger position. */ + sr_session_send(sdi, &trig); + + /* Send rest of data. */ + logic.length = (sample_count-trigger_offset) * sizeof(uint32_t); + logic.data = data + trigger_offset; + if (logic.length) + sr_session_send(sdi, &packet); + } else { + sr_session_send(sdi, &packet); + } + + devc->sent_samples += sample_count; +} + SR_PRIV int h4032l_receive_data(int fd, int revents, void *cb_data) { struct timeval tv; @@ -119,11 +161,9 @@ SR_PRIV int h4032l_receive_data(int fd, int revents, void *cb_data) void LIBUSB_CALL h4032l_data_transfer_callback(struct libusb_transfer *transfer) { - const struct sr_dev_inst *sdi = transfer->user_data; - struct dev_context *devc = sdi->priv; + struct sr_dev_inst *const sdi = transfer->user_data; + struct dev_context *const devc = sdi->priv; uint32_t max_samples = transfer->actual_length / sizeof(uint32_t); - struct sr_datafeed_packet packet; - struct sr_datafeed_logic logic; uint32_t *buffer; uint32_t number_samples; @@ -150,12 +190,7 @@ void LIBUSB_CALL h4032l_data_transfer_callback(struct libusb_transfer *transfer) number_samples = (devc->remaining_samples < max_samples) ? devc->remaining_samples : max_samples; devc->remaining_samples -= number_samples; - packet.type = SR_DF_LOGIC; - packet.payload = &logic; - logic.length = number_samples * sizeof(uint32_t); - logic.unitsize = sizeof(uint32_t); - logic.data = buffer; - sr_session_send(sdi, &packet); + send_data(sdi, buffer, number_samples); sr_dbg("Remaining: %d %08X %08X.", devc->remaining_samples, buffer[0], buffer[1]); @@ -177,15 +212,13 @@ void LIBUSB_CALL h4032l_data_transfer_callback(struct libusb_transfer *transfer) void LIBUSB_CALL h4032l_usb_callback(struct libusb_transfer *transfer) { - const struct sr_dev_inst *sdi = transfer->user_data; - struct dev_context *devc = sdi->priv; + struct sr_dev_inst *const sdi = transfer->user_data; + struct dev_context *const devc = sdi->priv; struct sr_usb_dev_inst *usb = sdi->conn; gboolean cmd = FALSE; uint32_t max_samples = transfer->actual_length / sizeof(uint32_t); uint32_t *buffer; struct h4032l_status_packet *status; - struct sr_datafeed_packet packet; - struct sr_datafeed_logic logic; uint32_t number_samples; int ret; @@ -259,12 +292,7 @@ void LIBUSB_CALL h4032l_usb_callback(struct libusb_transfer *transfer) number_samples = (devc->remaining_samples < max_samples) ? devc->remaining_samples : max_samples; devc->remaining_samples -= number_samples; - packet.type = SR_DF_LOGIC; - packet.payload = &logic; - logic.length = number_samples * sizeof(uint32_t); - logic.unitsize = sizeof(uint32_t); - logic.data = buffer; - sr_session_send(sdi, &packet); + send_data(sdi, buffer, number_samples); sr_dbg("Remaining: %d %08X %08X.", devc->remaining_samples, buffer[0], buffer[1]); break; diff --git a/src/hardware/hantek-4032l/protocol.h b/src/hardware/hantek-4032l/protocol.h index 0a2eeece..8665c8f0 100644 --- a/src/hardware/hantek-4032l/protocol.h +++ b/src/hardware/hantek-4032l/protocol.h @@ -126,6 +126,7 @@ struct h4032l_cmd_pkt { struct dev_context { enum h4032l_status status; + unsigned int sent_samples; int submitted_transfers; uint32_t remaining_samples; gboolean acq_aborted; @@ -134,6 +135,7 @@ struct dev_context { struct libusb_transfer **transfers; uint8_t buffer[512]; uint64_t capture_ratio; + uint32_t trigger_pos; uint32_t fpga_version; };