X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fhardware%2Ffluke-45%2Fapi.c;h=96c8a1b324081507b8a07d64d80fec4c3f358506;hb=HEAD;hp=029ead74bc661ceccc37e1682b8f42224e3e3183;hpb=e756c595b6ac20716bfcf7584a7df0d679f6b884;p=libsigrok.git diff --git a/src/hardware/fluke-45/api.c b/src/hardware/fluke-45/api.c index 029ead74..96c8a1b3 100644 --- a/src/hardware/fluke-45/api.c +++ b/src/hardware/fluke-45/api.c @@ -1,6 +1,7 @@ /* * This file is part of the libsigrok project. * + * Copyright (C) 2012 Bert Vermeulen * Copyright (C) 2017 John Chajecki * * This program is free software: you can redistribute it and/or modify @@ -18,120 +19,202 @@ */ #include +#include +#include +#include +#include +#include +#include +#include +#include +#include "libsigrok-internal.h" +#include "scpi.h" #include "protocol.h" -static GSList *scan(struct sr_dev_driver *di, GSList *options) -{ - struct drv_context *drvc; - GSList *devices; +static const uint32_t scanopts[] = { + SR_CONF_CONN, + SR_CONF_SERIALCOMM, +}; - (void)options; +static const uint32_t drvopts[] = { + SR_CONF_MULTIMETER, +}; - devices = NULL; - drvc = di->context; - drvc->instances = NULL; +static const uint32_t devopts[] = { + SR_CONF_CONTINUOUS, + SR_CONF_LIMIT_SAMPLES | SR_CONF_SET, + SR_CONF_LIMIT_MSEC | SR_CONF_SET, +}; - /* TODO: scan for devices, either based on a SR_CONF_CONN option - * or on a USB scan. */ +/* Vendor, model, number of channels, poll period */ +static const struct fluke_scpi_dmm_model supported_models[] = { + { "FLUKE", "45", 2, 0 }, +}; - return devices; -} +static struct sr_dev_driver fluke_45_driver_info; -static int dev_open(struct sr_dev_inst *sdi) +static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi) { - (void)sdi; + struct dev_context *devc; + struct sr_dev_inst *sdi; + struct sr_scpi_hw_info *hw_info; + const struct scpi_command *cmdset = fluke_45_cmdset; + unsigned int i; + const struct fluke_scpi_dmm_model *model = NULL; + gchar *channel_name; + + /* Get device IDN. */ + if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) { + sr_scpi_hw_info_free(hw_info); + sr_info("Couldn't get IDN response, retrying."); + sr_scpi_close(scpi); + sr_scpi_open(scpi); + if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) { + sr_scpi_hw_info_free(hw_info); + sr_info("Couldn't get IDN response."); + return NULL; + } + } - /* TODO: get handle from sdi->conn and open it. */ + /* Check IDN. */ + for (i = 0; i < ARRAY_SIZE(supported_models); i++) { + if (!g_ascii_strcasecmp(hw_info->manufacturer, + supported_models[i].vendor) && + !strcmp(hw_info->model, supported_models[i].model)) { + model = &supported_models[i]; + break; + } + } + if (!model) { + sr_scpi_hw_info_free(hw_info); + return NULL; + } - return SR_OK; + /* Set up device parameters. */ + sdi = g_malloc0(sizeof(struct sr_dev_inst)); + sdi->vendor = g_strdup(model->vendor); + sdi->model = g_strdup(model->model); + sdi->version = g_strdup(hw_info->firmware_version); + sdi->serial_num = g_strdup(hw_info->serial_number); + sdi->conn = scpi; + sdi->driver = &fluke_45_driver_info; + sdi->inst_type = SR_INST_SCPI; + sr_scpi_hw_info_free(hw_info); + + devc = g_malloc0(sizeof(struct dev_context)); + devc->num_channels = model->num_channels; + devc->cmdset = cmdset; + sdi->priv = devc; + + /* Create channels. */ + for (i = 0; i < devc->num_channels; i++) { + channel_name = g_strdup_printf("P%d", i + 1); + sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, channel_name); + } + + return sdi; } -static int dev_close(struct sr_dev_inst *sdi) +static GSList *scan(struct sr_dev_driver *di, GSList *options) { - (void)sdi; + return sr_scpi_scan(di->context, options, probe_device); +} - /* TODO: get handle from sdi->conn and close it. */ +static int dev_open(struct sr_dev_inst *sdi) +{ + struct sr_scpi_dev_inst *scpi; + int ret; + + scpi = sdi->conn; + + if ((ret = sr_scpi_open(scpi) < 0)) { + sr_err("Failed to open SCPI device: %s.", sr_strerror(ret)); + return SR_ERR; + } 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 dev_close(struct sr_dev_inst *sdi) { - int ret; + struct sr_scpi_dev_inst *scpi; - (void)sdi; - (void)data; - (void)cg; + scpi = sdi->conn; - ret = SR_OK; - switch (key) { - /* TODO */ - default: - return SR_ERR_NA; - } + if (!scpi) + return SR_ERR_BUG; - return ret; + return sr_scpi_close(scpi); } 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; - (void)sdi; - (void)data; (void)cg; - ret = SR_OK; - switch (key) { - /* TODO */ - default: - ret = SR_ERR_NA; - } + devc = sdi->priv; - return ret; + return sr_sw_limits_config_set(&devc->limits, key, data); } static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { - int ret; + return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); +} - (void)sdi; - (void)data; - (void)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 = sdi->priv; - ret = SR_OK; - switch (key) { - /* TODO */ - default: - return SR_ERR_NA; - } + (void)cg; - return ret; + return sr_sw_limits_config_get(&devc->limits, key, data); } static int dev_acquisition_start(const struct sr_dev_inst *sdi) { - /* TODO: configure hardware, reset acquisition state, set up - * callbacks and send header packet. */ + struct sr_scpi_dev_inst *scpi; + struct dev_context *devc; + int ret; - (void)sdi; + scpi = sdi->conn; + devc = sdi->priv; + + sr_sw_limits_acquisition_start(&devc->limits); + std_session_send_df_header(sdi); + + if ((ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 10, + fl45_scpi_receive_data, (void *)sdi)) != SR_OK) + return ret; return SR_OK; } static int dev_acquisition_stop(struct sr_dev_inst *sdi) { - /* TODO: stop acquisition. */ + struct sr_scpi_dev_inst *scpi; + double d; + + scpi = sdi->conn; - (void)sdi; + /* + * A requested value is certainly on the way. Retrieve it now, + * to avoid leaving the device in a state where it's not expecting + * commands. + */ + sr_scpi_get_double(scpi, NULL, &d); + sr_scpi_source_remove(sdi->session, scpi); + + std_session_send_df_end(sdi); return SR_OK; } -SR_PRIV struct sr_dev_driver fluke_45_driver_info = { +static struct sr_dev_driver fluke_45_driver_info = { .name = "fluke-45", .longname = "Fluke 45", .api_version = 1, @@ -149,5 +232,4 @@ SR_PRIV struct sr_dev_driver fluke_45_driver_info = { .dev_acquisition_stop = dev_acquisition_stop, .context = NULL, }; - SR_REGISTER_DEV_DRIVER(fluke_45_driver_info);