X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fhardware%2Fkingst-la2016%2Fapi.c;h=03edcf14c5e96929dbc6342807b27cac044d7515;hb=a7740b06997b4d455428baaa995b777090c3b76e;hp=47a29b4f20218ef9a5d09003dfbddc58976d1300;hpb=d9a74e97f437d35abc32509a113fe2f5124559ef;p=libsigrok.git diff --git a/src/hardware/kingst-la2016/api.c b/src/hardware/kingst-la2016/api.c index 47a29b4f..03edcf14 100644 --- a/src/hardware/kingst-la2016/api.c +++ b/src/hardware/kingst-la2016/api.c @@ -23,12 +23,14 @@ /* mostly stolen from src/hardware/saleae-logic16/ */ #include + #include +#include #include +#include #include #include -#include -#include + #include "libsigrok-internal.h" #include "protocol.h" @@ -60,11 +62,11 @@ static const int32_t trigger_matches[] = { }; static const char *channel_names[] = { - "0", "1", "2", "3", "4", "5", "6", "7", "8", - "9", "10", "11", "12", "13", "14", "15", + "0", "1", "2", "3", "4", "5", "6", "7", + "8", "9", "10", "11", "12", "13", "14", "15", }; -static const uint64_t samplerates[] = { +static const uint64_t samplerates_la2016[] = { SR_KHZ(20), SR_KHZ(50), SR_KHZ(100), @@ -82,6 +84,23 @@ static const uint64_t samplerates[] = { SR_MHZ(200), }; +static const uint64_t samplerates_la1016[] = { + SR_KHZ(20), + SR_KHZ(50), + SR_KHZ(100), + SR_KHZ(200), + SR_KHZ(500), + SR_MHZ(1), + SR_MHZ(2), + SR_MHZ(4), + SR_MHZ(5), + SR_MHZ(8), + SR_MHZ(10), + SR_MHZ(20), + SR_MHZ(50), + SR_MHZ(100), +}; + static const float logic_threshold_value[] = { 1.58, 2.5, @@ -151,14 +170,15 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) usb = NULL; for (l = conn_devices; l; l = l->next) { usb = l->data; - if (usb->bus == libusb_get_bus_number(devlist[i]) - && usb->address == libusb_get_device_address(devlist[i])) + if (usb->bus == libusb_get_bus_number(devlist[i]) && + usb->address == libusb_get_device_address(devlist[i])) break; } - if (!l) + if (!l) { /* This device matched none of the ones that * matched the conn specification. */ continue; + } } libusb_get_device_descriptor(devlist[i], &des); @@ -340,7 +360,7 @@ static int dev_open(struct sr_dev_inst *sdi) timediff_us = g_get_monotonic_time() - devc->fw_updated; timediff_ms = timediff_us / 1000; - + if ((ret = la2016_dev_open(sdi)) == SR_OK) break; sr_spew("Waited %" PRIi64 "ms.", timediff_ms); @@ -350,8 +370,9 @@ static int dev_open(struct sr_dev_inst *sdi) return SR_ERR; } sr_info("Device came back after %" PRIi64 "ms.", timediff_ms); - } else + } else { ret = la2016_dev_open(sdi); + } if (ret != SR_OK) { sr_err("Unable to open device."); @@ -381,7 +402,8 @@ static int dev_close(struct sr_dev_inst *sdi) return SR_OK; } -static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) +static int config_get(uint32_t key, GVariant **data, + const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { struct dev_context *devc; struct sr_usb_dev_inst *usb; @@ -398,10 +420,11 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s if (!sdi->conn) return SR_ERR_ARG; usb = sdi->conn; - if (usb->address == 255) + if (usb->address == 255) { /* Device still needs to re-enumerate after firmware * upload, so we don't know its (future) address. */ return SR_ERR; + } *data = g_variant_new_printf("%d.%d", usb->bus, usb->address); break; case SR_CONF_SAMPLERATE: @@ -423,7 +446,7 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s case SR_CONF_LOGIC_THRESHOLD_CUSTOM: *data = g_variant_new_double(devc->threshold_voltage); break; - + default: return SR_ERR_NA; } @@ -431,7 +454,8 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s return SR_OK; } -static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) +static int config_set(uint32_t key, GVariant *data, + const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { struct dev_context *devc; double low, high; @@ -477,14 +501,25 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd return SR_OK; } -static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) +static int config_list(uint32_t key, GVariant **data, + const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { + struct dev_context *devc; + switch (key) { case SR_CONF_SCAN_OPTIONS: case SR_CONF_DEVICE_OPTIONS: return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); case SR_CONF_SAMPLERATE: - *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates)); + if (!sdi) + return SR_ERR_ARG; + devc = sdi->priv; + if (devc->max_samplerate == SR_MHZ(200)) { + *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates_la2016)); + } + else { + *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates_la1016)); + } break; case SR_CONF_LIMIT_SAMPLES: *data = std_gvar_tuple_u64(LA2016_NUM_SAMPLES_MIN, LA2016_NUM_SAMPLES_MAX); @@ -507,183 +542,6 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst * return SR_OK; } -static void send_chunk(struct sr_dev_inst *sdi, transfer_packet_t *packets, unsigned int num_tfers) -{ - struct dev_context *devc; - struct sr_datafeed_logic logic; - struct sr_datafeed_packet sr_packet; - transfer_packet_t *packet; - acq_packet_t *p; - unsigned int max_samples, n_samples, total_samples, free_n_samples, ptotal; - unsigned int i, j, k; - int do_signal_trigger; - uint16_t *wp; - - devc = sdi->priv; - - logic.unitsize = 2; - logic.data = devc->convbuffer; - - sr_packet.type = SR_DF_LOGIC; - sr_packet.payload = &logic; - - max_samples = devc->convbuffer_size / 2; - n_samples = 0; - wp = (uint16_t*)devc->convbuffer; - total_samples = 0; - do_signal_trigger = 0; - - if (devc->had_triggers_configured && devc->reading_behind_trigger == 0 && devc->info.n_rep_packets_before_trigger == 0) { - std_session_send_df_trigger(sdi); - devc->reading_behind_trigger = 1; - } - - for (i = 0; i < num_tfers; i++) { - transfer_packet_host(packets[i]); - packet = packets + i; - ptotal = 0; - for (k = 0; k < ARRAY_SIZE(packet->packet); k++) { - free_n_samples = max_samples - n_samples; - if (free_n_samples < 256 || do_signal_trigger) { - logic.length = n_samples * 2; - sr_session_send(sdi, &sr_packet); - n_samples = 0; - wp = (uint16_t*)devc->convbuffer; - if (do_signal_trigger) { - std_session_send_df_trigger(sdi); - do_signal_trigger = 0; - } - } - p = packet->packet + k; - for (j = 0; j < p->repetitions; j++) - *(wp++) = p->state; - n_samples += p->repetitions; - total_samples += p->repetitions; - ptotal += p->repetitions; - devc->total_samples += p->repetitions; - if (!devc->reading_behind_trigger) { - devc->n_reps_until_trigger --; - if (devc->n_reps_until_trigger == 0) { - devc->reading_behind_trigger = 1; - do_signal_trigger = 1; - sr_dbg(" here is trigger position after %" PRIu64 " samples, %.6fms", - devc->total_samples, - (double)devc->total_samples / devc->cur_samplerate * 1e3); - } - } - } - } - if (n_samples) { - logic.length = n_samples * 2; - sr_session_send(sdi, &sr_packet); - if (do_signal_trigger) { - std_session_send_df_trigger(sdi); - } - } - sr_dbg("send_chunk done after %d samples", total_samples); -} - -static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer) -{ - struct sr_dev_inst *sdi; - struct dev_context *devc; - struct sr_usb_dev_inst *usb; - int ret; - - sdi = transfer->user_data; - devc = sdi->priv; - usb = sdi->conn; - - sr_dbg("receive_transfer(): status %s received %d bytes.", - libusb_error_name(transfer->status), transfer->actual_length); - - if (transfer->status == LIBUSB_TRANSFER_TIMED_OUT) { - sr_err("bulk transfer timeout!"); - devc->transfer_finished = 1; - } - send_chunk(sdi, (transfer_packet_t*)transfer->buffer, transfer->actual_length / sizeof(transfer_packet_t)); - - devc->n_bytes_to_read -= transfer->actual_length; - if (devc->n_bytes_to_read) { - uint32_t to_read = devc->n_bytes_to_read; - if (to_read > LA2016_BULK_MAX) - to_read = LA2016_BULK_MAX; - libusb_fill_bulk_transfer( - transfer, usb->devhdl, - 0x86, transfer->buffer, to_read, - receive_transfer, (void*)sdi, DEFAULT_TIMEOUT_MS); - - if ((ret = libusb_submit_transfer(transfer)) == 0) - return; - sr_err("Failed to submit further transfer: %s.", libusb_error_name(ret)); - } - - g_free(transfer->buffer); - libusb_free_transfer(transfer); - devc->transfer_finished = 1; -} - -static int handle_event(int fd, int revents, void *cb_data) -{ - const struct sr_dev_inst *sdi; - struct dev_context *devc; - struct drv_context *drvc; - struct timeval tv; - - (void)fd; - (void)revents; - - sdi = cb_data; - devc = sdi->priv; - drvc = sdi->driver->context; - - if (devc->have_trigger == 0) { - if (la2016_has_triggered(sdi) == 0) { - sr_dbg("not yet ready for download..."); - return TRUE; - } - devc->have_trigger = 1; - devc->transfer_finished = 0; - devc->reading_behind_trigger = 0; - devc->total_samples = 0; - /* we can start retrieving data! */ - if (la2016_start_retrieval(sdi, receive_transfer) != SR_OK) { - sr_err("failed to start retrieval!"); - return FALSE; - } - sr_dbg("retrieval is started..."); - std_session_send_df_frame_begin(sdi); - - return TRUE; - } - - tv.tv_sec = tv.tv_usec = 0; - libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv); - - if (devc->transfer_finished) { - sr_dbg("transfer is finished!"); - std_session_send_df_frame_end(sdi); - - usb_source_remove(sdi->session, drvc->sr_ctx); - std_session_send_df_end(sdi); - - la2016_stop_acquisition(sdi); - - g_free(devc->convbuffer); - devc->convbuffer = NULL; - - sr_dbg("transfer is now finished"); - } - - return TRUE; -} - -static void abort_acquisition(struct dev_context *devc) -{ - if (devc->transfer) - libusb_cancel_transfer(devc->transfer); -} - static int configure_channels(const struct sr_dev_inst *sdi) { struct dev_context *devc; @@ -696,7 +554,7 @@ static int configure_channels(const struct sr_dev_inst *sdi) struct sr_channel *ch = (struct sr_channel*)l->data; if (ch->enabled == FALSE) continue; - devc->cur_channels |= 1 << (ch->index); + devc->cur_channels |= 1 << ch->index; devc->num_channels++; } @@ -734,12 +592,13 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi) devc->ctx = drvc->sr_ctx; if ((ret = la2016_start_acquisition(sdi)) != SR_OK) { - abort_acquisition(devc); + la2016_abort_acquisition(sdi); return ret; } devc->have_trigger = 0; - usb_source_add(sdi->session, drvc->sr_ctx, 50, handle_event, (void*)sdi); + usb_source_add(sdi->session, drvc->sr_ctx, 50, + la2016_receive_data, (void *)sdi); std_session_send_df_header(sdi); @@ -751,7 +610,6 @@ static int dev_acquisition_stop(struct sr_dev_inst *sdi) int ret; ret = la2016_abort_acquisition(sdi); - abort_acquisition(sdi->priv); return ret; }