2 * This file is part of the libsigrok project.
4 * Copyright (C) 2018 Gerhard Sittig <gerhard.sittig@gmx.net>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 static struct sr_dev_driver scpi_dmm_driver_info;
25 static const uint32_t scanopts[] = {
29 static const uint32_t drvopts[] = {
33 static const uint32_t devopts[] = {
35 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
36 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
37 SR_CONF_MEASURED_QUANTITY | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
40 static const struct scpi_command cmdset_agilent[] = {
41 { DMM_CMD_SETUP_REMOTE, "\n", },
42 { DMM_CMD_SETUP_FUNC, "CONF:%s", },
43 { DMM_CMD_QUERY_FUNC, "CONF?", },
44 { DMM_CMD_START_ACQ, "MEAS", },
45 { DMM_CMD_STOP_ACQ, "ABORT", },
46 { DMM_CMD_QUERY_VALUE, "READ?", },
47 { DMM_CMD_QUERY_PREC, "CONF?", },
51 static const struct mqopt_item mqopts_agilent_5digit[] = {
52 { SR_MQ_VOLTAGE, SR_MQFLAG_DC, "VOLT:DC", "VOLT ", NO_DFLT_PREC, },
53 { SR_MQ_VOLTAGE, SR_MQFLAG_AC, "VOLT:AC", "VOLT:AC ", NO_DFLT_PREC, },
54 { SR_MQ_CURRENT, SR_MQFLAG_DC, "CURR:DC", "CURR ", NO_DFLT_PREC, },
55 { SR_MQ_CURRENT, SR_MQFLAG_AC, "CURR:AC", "CURR:AC ", NO_DFLT_PREC, },
56 { SR_MQ_RESISTANCE, 0, "RES", "RES ", NO_DFLT_PREC, },
57 { SR_MQ_RESISTANCE, SR_MQFLAG_FOUR_WIRE, "FRES", "FRES ", NO_DFLT_PREC, },
58 { SR_MQ_CONTINUITY, 0, "CONT", "CONT", -1, },
59 { SR_MQ_CAPACITANCE, 0, "CAP", "CAP ", NO_DFLT_PREC, },
60 { SR_MQ_VOLTAGE, SR_MQFLAG_DC | SR_MQFLAG_DIODE, "DIOD", "DIOD", -4, },
61 { SR_MQ_TEMPERATURE, 0, "TEMP", "TEMP ", NO_DFLT_PREC, },
62 { SR_MQ_FREQUENCY, 0, "FREQ", "FREQ ", NO_DFLT_PREC, },
65 SR_PRIV const struct scpi_dmm_model models[] = {
68 1, 5, cmdset_agilent, ARRAY_AND_SIZE(mqopts_agilent_5digit),
69 scpi_dmm_get_meas_agilent,
73 static const struct scpi_dmm_model *is_compatible(const char *vendor, const char *model)
76 const struct scpi_dmm_model *entry;
78 for (i = 0; i < ARRAY_SIZE(models); i++) {
80 if (!entry->vendor || !entry->model)
82 if (strcmp(vendor, entry->vendor) != 0)
84 if (strcmp(model, entry->model) != 0)
92 static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
94 struct sr_scpi_hw_info *hw_info;
97 const struct scpi_dmm_model *model;
98 struct sr_dev_inst *sdi;
99 struct dev_context *devc;
103 ret = sr_scpi_get_hw_id(scpi, &hw_info);
104 scpi_dmm_cmd_delay(scpi);
106 sr_info("Could not get IDN response.");
109 vendor = sr_vendor_alias(hw_info->manufacturer);
110 model = is_compatible(vendor, hw_info->model);
112 sr_scpi_hw_info_free(hw_info);
116 sdi = g_malloc0(sizeof(*sdi));
117 sdi->vendor = g_strdup(hw_info->manufacturer);
118 sdi->model = g_strdup(hw_info->model);
119 sdi->version = g_strdup(hw_info->firmware_version);
120 sdi->serial_num = g_strdup(hw_info->serial_number);
122 sdi->driver = &scpi_dmm_driver_info;
123 sdi->inst_type = SR_INST_SCPI;
124 sr_scpi_hw_info_free(hw_info);
126 devc = g_malloc0(sizeof(*devc));
128 devc->num_channels = model->num_channels;
129 devc->cmdset = model->cmdset;
132 for (i = 0; i < devc->num_channels; i++) {
133 channel_name = g_strdup_printf("P%zu", i + 1);
134 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, channel_name);
140 static GSList *scan(struct sr_dev_driver *di, GSList *options)
142 return sr_scpi_scan(di->context, options, probe_device);
145 static int dev_open(struct sr_dev_inst *sdi)
147 struct sr_scpi_dev_inst *scpi;
151 ret = sr_scpi_open(scpi);
153 sr_err("Failed to open SCPI device: %s.", sr_strerror(ret));
160 static int dev_close(struct sr_dev_inst *sdi)
162 struct sr_scpi_dev_inst *scpi;
168 sr_dbg("DIAG: sdi->status %d.", sdi->status - SR_ST_NOT_FOUND);
169 if (sdi->status <= SR_ST_INACTIVE)
172 return sr_scpi_close(scpi);
175 static int config_get(uint32_t key, GVariant **data,
176 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
178 struct dev_context *devc;
180 enum sr_mqflag mqflag;
189 case SR_CONF_LIMIT_SAMPLES:
190 case SR_CONF_LIMIT_MSEC:
191 return sr_sw_limits_config_get(&devc->limits, key, data);
192 case SR_CONF_MEASURED_QUANTITY:
193 ret = scpi_dmm_get_mq(sdi, &mq, &mqflag, NULL);
196 arr[0] = g_variant_new_uint32(mq);
197 arr[1] = g_variant_new_uint64(mqflag);
198 *data = g_variant_new_tuple(arr, ARRAY_SIZE(arr));
205 static int config_set(uint32_t key, GVariant *data,
206 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
208 struct dev_context *devc;
210 enum sr_mqflag mqflag;
211 GVariant *tuple_child;
218 case SR_CONF_LIMIT_SAMPLES:
219 case SR_CONF_LIMIT_MSEC:
220 return sr_sw_limits_config_set(&devc->limits, key, data);
221 case SR_CONF_MEASURED_QUANTITY:
222 tuple_child = g_variant_get_child_value(data, 0);
223 mq = g_variant_get_uint32(tuple_child);
224 tuple_child = g_variant_get_child_value(data, 1);
225 mqflag = g_variant_get_uint64(tuple_child);
226 g_variant_unref(tuple_child);
227 return scpi_dmm_set_mq(sdi, mq, mqflag);
233 static int config_list(uint32_t key, GVariant **data,
234 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
236 struct dev_context *devc;
237 GVariant *gvar, *arr[2];
243 devc = sdi ? sdi->priv : NULL;
246 case SR_CONF_SCAN_OPTIONS:
247 case SR_CONF_DEVICE_OPTIONS:
248 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
249 case SR_CONF_MEASURED_QUANTITY:
250 /* TODO Use std_gvar_measured_quantities() when available. */
253 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
254 for (i = 0; i < devc->model->mqopt_size; i++) {
255 arr[0] = g_variant_new_uint32(devc->model->mqopts[i].mq);
256 arr[1] = g_variant_new_uint64(devc->model->mqopts[i].mqflag);
257 gvar = g_variant_new_tuple(arr, ARRAY_SIZE(arr));
258 g_variant_builder_add_value(&gvb, gvar);
260 *data = g_variant_builder_end(&gvb);
268 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
270 struct sr_scpi_dev_inst *scpi;
271 struct dev_context *devc;
278 ret = scpi_dmm_get_mq(sdi, &devc->start_acq_mq.curr_mq,
279 &devc->start_acq_mq.curr_mqflag, NULL);
283 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_START_ACQ);
284 if (command && *command) {
285 ret = sr_scpi_send(scpi, command);
286 scpi_dmm_cmd_delay(scpi);
291 sr_sw_limits_acquisition_start(&devc->limits);
292 ret = std_session_send_df_header(sdi);
296 ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 10,
297 scpi_dmm_receive_data, (void *)sdi);
304 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
306 struct sr_scpi_dev_inst *scpi;
307 struct dev_context *devc;
313 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_STOP_ACQ);
314 if (command && *command) {
315 (void)sr_scpi_send(scpi, command);
316 scpi_dmm_cmd_delay(scpi);
318 sr_scpi_source_remove(sdi->session, scpi);
320 std_session_send_df_end(sdi);
325 static struct sr_dev_driver scpi_dmm_driver_info = {
327 .longname = "SCPI DMM",
330 .cleanup = std_cleanup,
332 .dev_list = std_dev_list,
333 .dev_clear = std_dev_clear,
334 .config_get = config_get,
335 .config_set = config_set,
336 .config_list = config_list,
337 .dev_open = dev_open,
338 .dev_close = dev_close,
339 .dev_acquisition_start = dev_acquisition_start,
340 .dev_acquisition_stop = dev_acquisition_stop,
343 SR_REGISTER_DEV_DRIVER(scpi_dmm_driver_info);