X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=blobdiff_plain;f=src%2Fhardware%2Fhantek-4032l%2Fapi.c;h=9576fabccb068ad9fcc6ac3d5b6951e2769fabe7;hp=b6e47ce1f736ba0525c622844ae75ae88326b309;hb=5089a14345442bb87d0970efc0bc10235a530d60;hpb=6a25fa42388fa3ddf7f519c7d1f6b641b9d6cf6f diff --git a/src/hardware/hantek-4032l/api.c b/src/hardware/hantek-4032l/api.c index b6e47ce1..9576fabc 100644 --- a/src/hardware/hantek-4032l/api.c +++ b/src/hardware/hantek-4032l/api.c @@ -1,7 +1,9 @@ /* * This file is part of the libsigrok project. * - * Copyright (C) 2016 Andreas Zschunke + * Copyright (C) 2016 Andreas Zschunke + * Copyright (C) 2017 Andrej Valek + * Copyright (C) 2017 Uwe Hermann * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,67 +22,267 @@ #include #include "protocol.h" -SR_PRIV struct sr_dev_driver hantek_4032l_driver_info; +#define USB_INTERFACE 0 +#define NUM_CHANNELS 32 -static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx) -{ - return std_init(sr_ctx, di, LOG_PREFIX); -} +static const uint32_t scanopts[] = { + SR_CONF_CONN, +}; + +static const uint32_t drvopts[] = { + SR_CONF_LOGIC_ANALYZER, +}; + +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_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, + SR_CONF_TRIGGER_MATCH | SR_CONF_LIST, + SR_CONF_CONN | SR_CONF_GET, + SR_CONF_VOLTAGE_THRESHOLD | SR_CONF_SET | SR_CONF_LIST, +}; + +static const int32_t trigger_matches[] = { + SR_TRIGGER_ZERO, + SR_TRIGGER_ONE, + SR_TRIGGER_RISING, + SR_TRIGGER_FALLING, + SR_TRIGGER_EDGE, +}; + +static const uint64_t samplerates[] = { + SR_KHZ(1), + SR_KHZ(2), + SR_KHZ(4), + SR_KHZ(8), + SR_KHZ(16), + SR_HZ(31250), + SR_HZ(62500), + SR_KHZ(125), + SR_KHZ(250), + SR_KHZ(500), + SR_KHZ(625), + SR_HZ(781250), + SR_MHZ(1), + SR_KHZ(1250), + SR_HZ(1562500), + SR_MHZ(2), + SR_KHZ(2500), + SR_KHZ(3125), + SR_MHZ(4), + SR_MHZ(5), + SR_KHZ(6250), + SR_MHZ(10), + SR_KHZ(12500), + SR_MHZ(20), + SR_MHZ(25), + SR_MHZ(40), + SR_MHZ(50), + SR_MHZ(80), + SR_MHZ(100), + SR_MHZ(160), + SR_MHZ(200), + SR_MHZ(320), + SR_MHZ(400), +}; + +static const uint64_t samplerates_hw[] = { + SR_MHZ(100), + SR_MHZ(50), + SR_MHZ(25), + SR_KHZ(12500), + SR_KHZ(6250), + SR_KHZ(3125), + SR_HZ(1562500), + SR_HZ(781250), + SR_MHZ(80), + SR_MHZ(40), + SR_MHZ(20), + SR_MHZ(10), + SR_MHZ(5), + SR_KHZ(2500), + SR_KHZ(1250), + SR_KHZ(625), + SR_MHZ(4), + SR_MHZ(2), + SR_MHZ(1), + SR_KHZ(500), + SR_KHZ(250), + SR_KHZ(125), + SR_HZ(62500), + SR_HZ(31250), + SR_KHZ(16), + SR_KHZ(8), + SR_KHZ(4), + SR_KHZ(2), + SR_KHZ(1), + 0, + 0, + 0, + SR_MHZ(200), + SR_MHZ(160), + SR_MHZ(400), + SR_MHZ(320), +}; + +SR_PRIV struct sr_dev_driver hantek_4032l_driver_info; static GSList *scan(struct sr_dev_driver *di, GSList *options) { - struct drv_context *drvc; - GSList *devices; - - (void)options; + struct drv_context *drvc = di->context; + GSList *l, *devices, *conn_devices; + libusb_device **devlist; + struct libusb_device_descriptor des; + const char *conn; + int i; + char connection_id[64]; + struct sr_channel_group *cg; + struct sr_dev_inst *sdi; + struct sr_channel *ch; devices = NULL; - drvc = di->context; + conn_devices = NULL; drvc->instances = NULL; + conn = NULL; + + for (l = options; l; l = l->next) { + struct sr_config *src = l->data; + if (src->key == SR_CONF_CONN) { + conn = g_variant_get_string(src->data, NULL); + break; + } + } - /* TODO: scan for devices, either based on a SR_CONF_CONN option - * or on a USB scan. */ - - return devices; -} + if (conn) + conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn); + else + conn_devices = NULL; + + libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); + for (i = 0; devlist[i]; i++) { + if (conn) { + struct sr_usb_dev_inst *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])) + break; + } + if (!l) + /* This device matched none of the ones that + * matched the conn specification. */ + continue; + } + + libusb_get_device_descriptor(devlist[i], &des); + + if (des.idVendor != H4032L_USB_VENDOR || + des.idProduct != H4032L_USB_PRODUCT) + continue; + + usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); + + sdi = g_malloc0(sizeof(struct sr_dev_inst)); + sdi->driver = &hantek_4032l_driver_info; + sdi->vendor = g_strdup("Hantek"); + sdi->model = g_strdup("4032L"); + sdi->connection_id = g_strdup(connection_id); + + struct sr_channel_group *channel_groups[2]; + for (int j = 0; j < 2; j++) { + cg = g_malloc0(sizeof(struct sr_channel_group)); + cg->name = g_strdup_printf("%c", 'A' + j); + channel_groups[j] = cg; + sdi->channel_groups = g_slist_append(sdi->channel_groups, cg); + } + + /* Assemble channel list and add channel to channel groups. */ + for (int j = 0; j < NUM_CHANNELS; j++) { + char channel_name[4]; + sprintf(channel_name, "%c%d", 'A' + (j & 1), j / 2); + ch = sr_channel_new(sdi, j, SR_CHANNEL_LOGIC, TRUE, channel_name); + cg = channel_groups[j & 1]; + cg->channels = g_slist_append(cg->channels, ch); + } + + struct dev_context *devc = g_malloc0(sizeof(struct dev_context)); + + /* Initialize command packet. */ + devc->cmd_pkt.magic = H4032L_CMD_PKT_MAGIC; + 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; + + devc->capture_ratio = 5; + + devc->usb_transfer = libusb_alloc_transfer(0); + + sdi->priv = devc; + devices = g_slist_append(devices, sdi); + + sdi->status = SR_ST_INACTIVE; + sdi->inst_type = SR_INST_USB; + sdi->conn = sr_usb_dev_inst_new( + libusb_get_bus_number(devlist[i]), + libusb_get_device_address(devlist[i]), NULL); + } -static GSList *dev_list(const struct sr_dev_driver *di) -{ - return ((struct drv_context *)(di->context))->instances; -} + g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free); + libusb_free_device_list(devlist, 1); -static int dev_clear(const struct sr_dev_driver *di) -{ - return std_dev_clear(di, NULL); + return std_scan_complete(di, devices); } static int dev_open(struct sr_dev_inst *sdi) { - (void)sdi; + struct sr_usb_dev_inst *usb = sdi->conn; + int ret; - /* TODO: get handle from sdi->conn and open it. */ + ret = h4032l_dev_open(sdi); + if (ret != SR_OK) { + sr_err("Unable to open device."); + return SR_ERR; + } - sdi->status = SR_ST_ACTIVE; + ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE); + if (ret != 0) { + switch (ret) { + case LIBUSB_ERROR_BUSY: + sr_err("Unable to claim USB interface. Another " + "program or driver has already claimed it."); + break; + case LIBUSB_ERROR_NO_DEVICE: + sr_err("Device has been disconnected."); + break; + default: + sr_err("Unable to claim interface: %s.", + libusb_error_name(ret)); + break; + } + + return SR_ERR; + } return SR_OK; } static int dev_close(struct sr_dev_inst *sdi) { - (void)sdi; + struct sr_usb_dev_inst *usb; - /* TODO: get handle from sdi->conn and close it. */ + usb = sdi->conn; - sdi->status = SR_ST_INACTIVE; + if (!usb->devhdl) + return SR_ERR_BUG; - return SR_OK; -} - -static int cleanup(const struct sr_dev_driver *di) -{ - dev_clear(di); - - /* TODO: free other driver resources, if any. */ + sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.", + usb->bus, usb->address, sdi->connection_id, USB_INTERFACE); + libusb_release_interface(usb->devhdl, USB_INTERFACE); + libusb_close(usb->devhdl); + usb->devhdl = NULL; return SR_OK; } @@ -88,82 +290,217 @@ static int cleanup(const struct sr_dev_driver *di) static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { - int ret; + struct dev_context *devc = sdi->priv; + struct sr_usb_dev_inst *usb; - (void)sdi; - (void)data; (void)cg; - ret = SR_OK; switch (key) { - /* TODO */ + case SR_CONF_SAMPLERATE: + *data = g_variant_new_uint64(samplerates_hw[devc->cmd_pkt.sample_rate]); + break; + case SR_CONF_CAPTURE_RATIO: + *data = g_variant_new_uint64(devc->capture_ratio); + break; + case SR_CONF_LIMIT_SAMPLES: + *data = g_variant_new_uint64(devc->cmd_pkt.sample_size); + break; + case SR_CONF_CONN: + if (!sdi || !(usb = sdi->conn)) + return SR_ERR_ARG; + *data = g_variant_new_printf("%d.%d", usb->bus, usb->address); + break; default: return SR_ERR_NA; } - return ret; + return SR_OK; } static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { - int ret; + struct dev_context *devc = sdi->priv; + struct h4032l_cmd_pkt *cmd_pkt = &devc->cmd_pkt; - (void)data; (void)cg; - if (sdi->status != SR_ST_ACTIVE) - return SR_ERR_DEV_CLOSED; - - ret = SR_OK; switch (key) { - /* TODO */ - default: - ret = SR_ERR_NA; + case SR_CONF_SAMPLERATE: { + uint64_t sample_rate = g_variant_get_uint64(data); + uint8_t i = 0; + while (i < ARRAY_SIZE(samplerates_hw) && samplerates_hw[i] != sample_rate) + i++; + + if (i == ARRAY_SIZE(samplerates_hw) || sample_rate == 0) { + sr_err("invalid sample rate"); + return SR_ERR_SAMPLERATE; + } + cmd_pkt->sample_rate = i; + + return SR_OK; + } + case SR_CONF_CAPTURE_RATIO: + devc->capture_ratio = g_variant_get_uint64(data); + return SR_OK; + case SR_CONF_LIMIT_SAMPLES: { + uint64_t number_samples = g_variant_get_uint64(data); + number_samples += 511; + number_samples &= 0xfffffe00; + if (number_samples < 2048 + || number_samples > 64 * 1024 * 1024) { + sr_err("invalid sample range 2k...64M: %ld", + number_samples); + return SR_ERR; + } + cmd_pkt->sample_size = number_samples; + return SR_OK; + } + case SR_CONF_VOLTAGE_THRESHOLD: { + double d1, d2; + g_variant_get(data, "(dd)", &d1, &d2); + devc->cmd_pkt.pwm_a = h4032l_voltage2pwm(d1); + devc->cmd_pkt.pwm_b = h4032l_voltage2pwm(d2); + return SR_OK; + } } - return ret; + return SR_ERR_NA; } static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { - int ret; - - (void)sdi; - (void)data; - (void)cg; - - ret = SR_OK; switch (key) { - /* TODO */ + 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)); + break; + case SR_CONF_TRIGGER_MATCH: + *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches)); + break; + case SR_CONF_VOLTAGE_THRESHOLD: + *data = std_gvar_tuple_double(2.5, 2.5); + break; default: return SR_ERR_NA; } - return ret; + return SR_OK; } -static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) +static int dev_acquisition_start(const struct sr_dev_inst *sdi) { - (void)sdi; - (void)cb_data; - - if (sdi->status != SR_ST_ACTIVE) - return SR_ERR_DEV_CLOSED; + struct sr_dev_driver *di = sdi->driver; + struct drv_context *drvc = di->context; + struct dev_context *devc = sdi->priv; + struct sr_trigger *trigger = sr_session_trigger_get(sdi->session); + struct h4032l_cmd_pkt *cmd_pkt = &devc->cmd_pkt; + + /* Calculate packet ratio. */ + cmd_pkt->pre_trigger_size = (cmd_pkt->sample_size * devc->capture_ratio) / 100; + + cmd_pkt->trig_flags.enable_trigger1 = 0; + cmd_pkt->trig_flags.enable_trigger2 = 0; + cmd_pkt->trig_flags.trigger_and_logic = 0; + + if (trigger && trigger->stages) { + GSList *stages = trigger->stages; + struct sr_trigger_stage *stage1 = stages->data; + if (stages->next) { + sr_err("only one trigger stage supported for now"); + return SR_ERR; + } + cmd_pkt->trig_flags.enable_trigger1 = 1; + cmd_pkt->trigger[0].flags.edge_type = H4032L_TRIGGER_EDGE_TYPE_DISABLED; + cmd_pkt->trigger[0].flags.data_range_enabled = 0; + cmd_pkt->trigger[0].flags.time_range_enabled = 0; + cmd_pkt->trigger[0].flags.combined_enabled = 0; + cmd_pkt->trigger[0].flags.data_range_type = H4032L_TRIGGER_DATA_RANGE_TYPE_MAX; + cmd_pkt->trigger[0].data_range_mask = 0; + cmd_pkt->trigger[0].data_range_max = 0; + + /* Initialize range mask values. */ + uint32_t range_mask = 0; + uint32_t range_value = 0; + + GSList *channel = stage1->matches; + while (channel) { + struct sr_trigger_match *match = channel->data; + + switch (match->match) { + case SR_TRIGGER_ZERO: + range_mask |= (1 << match->channel->index); + break; + case SR_TRIGGER_ONE: + range_mask |= (1 << match->channel->index); + range_value |= (1 << match->channel->index); + break; + case SR_TRIGGER_RISING: + if (cmd_pkt->trigger[0].flags.edge_type != H4032L_TRIGGER_EDGE_TYPE_DISABLED) { + sr_err("only one trigger signal with fall/rising/edge allowed"); + return SR_ERR; + } + cmd_pkt->trigger[0].flags.edge_type = H4032L_TRIGGER_EDGE_TYPE_RISE; + cmd_pkt->trigger[0].flags.edge_signal = match->channel->index; + break; + case SR_TRIGGER_FALLING: + if (cmd_pkt->trigger[0].flags.edge_type != H4032L_TRIGGER_EDGE_TYPE_DISABLED) { + sr_err("only one trigger signal with fall/rising/edge allowed"); + return SR_ERR; + } + cmd_pkt->trigger[0].flags.edge_type = H4032L_TRIGGER_EDGE_TYPE_FALL; + cmd_pkt->trigger[0].flags.edge_signal = match->channel->index; + break; + case SR_TRIGGER_EDGE: + if (cmd_pkt->trigger[0].flags.edge_type != H4032L_TRIGGER_EDGE_TYPE_DISABLED) { + sr_err("only one trigger signal with fall/rising/edge allowed"); + return SR_ERR; + } + cmd_pkt->trigger[0].flags.edge_type = H4032L_TRIGGER_EDGE_TYPE_TOGGLE; + cmd_pkt->trigger[0].flags.edge_signal = match->channel->index; + break; + default: + sr_err("unknown trigger value"); + return SR_ERR; + } + + channel = channel->next; + } + + /* Compress range mask value and apply range settings. */ + if (range_mask) { + cmd_pkt->trigger[0].flags.data_range_enabled = 1; + cmd_pkt->trigger[0].data_range_mask |= (range_mask); + + uint32_t new_range_value = 0; + uint32_t bit_mask = 1; + while (range_mask) { + if ((range_mask & 1) != 0) { + new_range_value <<= 1; + if ((range_value & 1) != 0) + new_range_value |= bit_mask; + bit_mask <<= 1; + } + range_mask >>= 1; + range_value >>= 1; + } + cmd_pkt->trigger[0].data_range_max |= range_value; + } + } - /* TODO: configure hardware, reset acquisition state, set up - * callbacks and send header packet. */ + usb_source_add(sdi->session, drvc->sr_ctx, 10000, + h4032l_receive_data, sdi->driver->context); - return SR_OK; + /* Start capturing. */ + return h4032l_start(sdi); } -static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) +static int dev_acquisition_stop(struct sr_dev_inst *sdi) { - (void)cb_data; - - if (sdi->status != SR_ST_ACTIVE) - return SR_ERR_DEV_CLOSED; + (void)sdi; /* TODO: stop acquisition. */ @@ -172,13 +509,13 @@ static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) SR_PRIV struct sr_dev_driver hantek_4032l_driver_info = { .name = "hantek-4032l", - .longname = "Hantek 4032l", + .longname = "Hantek 4032L", .api_version = 1, - .init = init, - .cleanup = cleanup, + .init = std_init, + .cleanup = std_cleanup, .scan = scan, - .dev_list = dev_list, - .dev_clear = dev_clear, + .dev_list = std_dev_list, + .dev_clear = std_dev_clear, .config_get = config_get, .config_set = config_set, .config_list = config_list, @@ -188,3 +525,4 @@ SR_PRIV struct sr_dev_driver hantek_4032l_driver_info = { .dev_acquisition_stop = dev_acquisition_stop, .context = NULL, }; +SR_REGISTER_DEV_DRIVER(hantek_4032l_driver_info);