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[] = {
30 static const uint32_t drvopts[] = {
34 static const uint32_t devopts_generic[] = {
36 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
37 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
38 SR_CONF_MEASURED_QUANTITY | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
41 static const struct scpi_command cmdset_agilent[] = {
42 { DMM_CMD_SETUP_REMOTE, "\n", },
43 { DMM_CMD_SETUP_FUNC, "CONF:%s", },
44 { DMM_CMD_QUERY_FUNC, "CONF?", },
45 { DMM_CMD_START_ACQ, "MEAS", },
46 { DMM_CMD_STOP_ACQ, "ABORT", },
47 { DMM_CMD_QUERY_VALUE, "READ?", },
48 { DMM_CMD_QUERY_PREC, "CONF?", },
53 * cmdset_hp is used for the 34401A, which was added to this code after the
54 * 34405A and 34465A. It differs in starting the measurement with INIT: using
55 * MEAS without a trailing '?' (as used for the 34405A) is not valid for the
56 * 34401A and gives an error.
57 * I'm surprised the same instruction sequence doesn't work and INIT may
58 * work for both, but I don't have the others to re-test.
60 * cmdset_hp also works well for the 34410A, using cmdset_agilent throws an
61 * error on 'MEAS' without a '?'.
64 * MEAS <optional parameters> ? configures, arms, triggers and waits
66 * CONF <parameters> configures
67 * INIT prepares for triggering (trigger mode is not set, assumed
68 * internal - external might time out)
69 * *OPC waits for completion, and
70 * READ? retrieves the result
72 static const struct scpi_command cmdset_hp[] = {
73 { DMM_CMD_SETUP_REMOTE, "\n", },
74 { DMM_CMD_SETUP_FUNC, "CONF:%s", },
75 { DMM_CMD_QUERY_FUNC, "CONF?", },
76 { DMM_CMD_START_ACQ, "INIT", },
77 { DMM_CMD_STOP_ACQ, "ABORT", },
78 { DMM_CMD_QUERY_VALUE, "READ?", },
79 { DMM_CMD_QUERY_PREC, "CONF?", },
83 static const struct scpi_command cmdset_gwinstek[] = {
84 { DMM_CMD_SETUP_REMOTE, "SYST:REM", },
85 { DMM_CMD_SETUP_FUNC, "CONF:%s", },
86 { DMM_CMD_QUERY_FUNC, "CONF:STAT:FUNC?", },
87 { DMM_CMD_START_ACQ, "*CLS;SYST:REM", },
88 { DMM_CMD_STOP_ACQ, "SYST:LOC", },
89 { DMM_CMD_QUERY_VALUE, "VAL1?", },
90 { DMM_CMD_QUERY_PREC, "SENS:DET:RATE?", },
94 static const struct mqopt_item mqopts_agilent_34405a[] = {
95 { SR_MQ_VOLTAGE, SR_MQFLAG_DC, "VOLT:DC", "VOLT ", NO_DFLT_PREC, },
96 { SR_MQ_VOLTAGE, SR_MQFLAG_AC, "VOLT:AC", "VOLT:AC ", NO_DFLT_PREC, },
97 { SR_MQ_CURRENT, SR_MQFLAG_DC, "CURR:DC", "CURR ", NO_DFLT_PREC, },
98 { SR_MQ_CURRENT, SR_MQFLAG_AC, "CURR:AC", "CURR:AC ", NO_DFLT_PREC, },
99 { SR_MQ_RESISTANCE, 0, "RES", "RES ", NO_DFLT_PREC, },
100 { SR_MQ_CONTINUITY, 0, "CONT", "CONT", -1, },
101 { SR_MQ_CAPACITANCE, 0, "CAP", "CAP ", NO_DFLT_PREC, },
102 { SR_MQ_VOLTAGE, SR_MQFLAG_DC | SR_MQFLAG_DIODE, "DIOD", "DIOD", -4, },
103 { SR_MQ_TEMPERATURE, 0, "TEMP", "TEMP ", NO_DFLT_PREC, },
104 { SR_MQ_FREQUENCY, 0, "FREQ", "FREQ ", NO_DFLT_PREC, },
107 static const struct mqopt_item mqopts_agilent_34401a[] = {
108 { SR_MQ_VOLTAGE, SR_MQFLAG_DC, "VOLT:DC", "VOLT ", NO_DFLT_PREC, },
109 { SR_MQ_VOLTAGE, SR_MQFLAG_AC, "VOLT:AC", "VOLT:AC ", NO_DFLT_PREC, },
110 { SR_MQ_CURRENT, SR_MQFLAG_DC, "CURR:DC", "CURR ", NO_DFLT_PREC, },
111 { SR_MQ_CURRENT, SR_MQFLAG_AC, "CURR:AC", "CURR:AC ", NO_DFLT_PREC, },
112 { SR_MQ_RESISTANCE, 0, "RES", "RES ", NO_DFLT_PREC, },
113 { SR_MQ_RESISTANCE, SR_MQFLAG_FOUR_WIRE, "FRES", "FRES ", NO_DFLT_PREC, },
114 { SR_MQ_CONTINUITY, 0, "CONT", "CONT", -1, },
115 { SR_MQ_VOLTAGE, SR_MQFLAG_DC | SR_MQFLAG_DIODE, "DIOD", "DIOD", -4, },
116 { SR_MQ_FREQUENCY, 0, "FREQ", "FREQ ", NO_DFLT_PREC, },
117 { SR_MQ_TIME, 0, "PER", "PER ", NO_DFLT_PREC, },
120 static const struct mqopt_item mqopts_gwinstek_gdm8200a[] = {
121 { SR_MQ_VOLTAGE, SR_MQFLAG_DC, "VOLT:DC", "01", NO_DFLT_PREC, },
122 { SR_MQ_VOLTAGE, SR_MQFLAG_AC, "VOLT:AC", "02", NO_DFLT_PREC, },
123 { SR_MQ_CURRENT, SR_MQFLAG_DC, "CURR:DC", "03", NO_DFLT_PREC, },
124 { SR_MQ_CURRENT, SR_MQFLAG_AC, "CURR:AC", "04", NO_DFLT_PREC, },
125 { SR_MQ_CURRENT, SR_MQFLAG_DC, "CURR:DC", "05", NO_DFLT_PREC, }, /* mA */
126 { SR_MQ_CURRENT, SR_MQFLAG_AC, "CURR:AC", "06", NO_DFLT_PREC, }, /* mA */
127 { SR_MQ_RESISTANCE, 0, "RES", "07", NO_DFLT_PREC, },
128 { SR_MQ_RESISTANCE, SR_MQFLAG_FOUR_WIRE, "FRES", "16", NO_DFLT_PREC, },
129 { SR_MQ_CONTINUITY, 0, "CONT", "13", -1, },
130 { SR_MQ_VOLTAGE, SR_MQFLAG_DC | SR_MQFLAG_DIODE, "DIOD", "17", -4, },
131 { SR_MQ_TEMPERATURE, 0, "TEMP", "09", NO_DFLT_PREC, }, /* Celsius */
132 { SR_MQ_TEMPERATURE, 0, "TEMP", "15", NO_DFLT_PREC, }, /* Fahrenheit */
133 { SR_MQ_FREQUENCY, 0, "FREQ", "08", NO_DFLT_PREC, },
134 { SR_MQ_TIME, 0, "PER", "14", NO_DFLT_PREC, },
137 SR_PRIV const struct scpi_dmm_model models[] = {
140 1, 5, cmdset_agilent, ARRAY_AND_SIZE(mqopts_agilent_34405a),
141 scpi_dmm_get_meas_agilent,
142 ARRAY_AND_SIZE(devopts_generic),
147 1, 6, cmdset_hp, ARRAY_AND_SIZE(mqopts_agilent_34405a),
148 scpi_dmm_get_meas_agilent,
149 ARRAY_AND_SIZE(devopts_generic),
153 "Keysight", "34465A",
154 1, 5, cmdset_agilent, ARRAY_AND_SIZE(mqopts_agilent_34405a),
155 scpi_dmm_get_meas_agilent,
156 ARRAY_AND_SIZE(devopts_generic),
161 1, 6, cmdset_hp, ARRAY_AND_SIZE(mqopts_agilent_34401a),
162 scpi_dmm_get_meas_agilent,
163 ARRAY_AND_SIZE(devopts_generic),
164 /* 34401A: typ. 1020ms for AC readings (default is 1000ms). */
169 1, 6, cmdset_gwinstek, ARRAY_AND_SIZE(mqopts_gwinstek_gdm8200a),
170 scpi_dmm_get_meas_gwinstek,
171 ARRAY_AND_SIZE(devopts_generic),
176 1, 6, cmdset_gwinstek, ARRAY_AND_SIZE(mqopts_gwinstek_gdm8200a),
177 scpi_dmm_get_meas_gwinstek,
178 ARRAY_AND_SIZE(devopts_generic),
183 static const struct scpi_dmm_model *is_compatible(const char *vendor, const char *model)
186 const struct scpi_dmm_model *entry;
188 for (i = 0; i < ARRAY_SIZE(models); i++) {
190 if (!entry->vendor || !entry->model)
192 if (strcmp(vendor, entry->vendor) != 0)
194 if (strcmp(model, entry->model) != 0)
202 static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
204 struct sr_scpi_hw_info *hw_info;
207 const struct scpi_dmm_model *model;
208 struct sr_dev_inst *sdi;
209 struct dev_context *devc;
213 scpi_dmm_cmd_delay(scpi);
214 ret = sr_scpi_get_hw_id(scpi, &hw_info);
216 sr_info("Could not get IDN response.");
219 vendor = sr_vendor_alias(hw_info->manufacturer);
220 model = is_compatible(vendor, hw_info->model);
222 sr_scpi_hw_info_free(hw_info);
226 sdi = g_malloc0(sizeof(*sdi));
227 sdi->vendor = g_strdup(hw_info->manufacturer);
228 sdi->model = g_strdup(hw_info->model);
229 sdi->version = g_strdup(hw_info->firmware_version);
230 sdi->serial_num = g_strdup(hw_info->serial_number);
232 sdi->driver = &scpi_dmm_driver_info;
233 sdi->inst_type = SR_INST_SCPI;
234 sr_scpi_hw_info_free(hw_info);
235 if (model->read_timeout_us) /* non-default read timeout */
236 scpi->read_timeout_us = model->read_timeout_us;
237 devc = g_malloc0(sizeof(*devc));
239 devc->num_channels = model->num_channels;
240 devc->cmdset = model->cmdset;
242 devc->precision = NULL;
244 for (i = 0; i < devc->num_channels; i++) {
245 channel_name = g_strdup_printf("P%zu", i + 1);
246 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, channel_name);
252 static GSList *scan(struct sr_dev_driver *di, GSList *options)
254 return sr_scpi_scan(di->context, options, probe_device);
257 static int dev_open(struct sr_dev_inst *sdi)
259 struct sr_scpi_dev_inst *scpi;
263 ret = sr_scpi_open(scpi);
265 sr_err("Failed to open SCPI device: %s.", sr_strerror(ret));
272 static int dev_close(struct sr_dev_inst *sdi)
274 struct sr_scpi_dev_inst *scpi;
280 sr_dbg("DIAG: sdi->status %d.", sdi->status - SR_ST_NOT_FOUND);
281 if (sdi->status <= SR_ST_INACTIVE)
284 return sr_scpi_close(scpi);
287 static int config_get(uint32_t key, GVariant **data,
288 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
290 struct dev_context *devc;
292 enum sr_mqflag mqflag;
301 case SR_CONF_LIMIT_SAMPLES:
302 case SR_CONF_LIMIT_MSEC:
303 return sr_sw_limits_config_get(&devc->limits, key, data);
304 case SR_CONF_MEASURED_QUANTITY:
305 ret = scpi_dmm_get_mq(sdi, &mq, &mqflag, NULL, NULL);
308 arr[0] = g_variant_new_uint32(mq);
309 arr[1] = g_variant_new_uint64(mqflag);
310 *data = g_variant_new_tuple(arr, ARRAY_SIZE(arr));
317 static int config_set(uint32_t key, GVariant *data,
318 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
320 struct dev_context *devc;
322 enum sr_mqflag mqflag;
323 GVariant *tuple_child;
330 case SR_CONF_LIMIT_SAMPLES:
331 case SR_CONF_LIMIT_MSEC:
332 return sr_sw_limits_config_set(&devc->limits, key, data);
333 case SR_CONF_MEASURED_QUANTITY:
334 tuple_child = g_variant_get_child_value(data, 0);
335 mq = g_variant_get_uint32(tuple_child);
336 g_variant_unref(tuple_child);
337 tuple_child = g_variant_get_child_value(data, 1);
338 mqflag = g_variant_get_uint64(tuple_child);
339 g_variant_unref(tuple_child);
340 return scpi_dmm_set_mq(sdi, mq, mqflag);
346 static int config_list(uint32_t key, GVariant **data,
347 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
349 struct dev_context *devc;
350 GVariant *gvar, *arr[2];
356 devc = sdi ? sdi->priv : NULL;
359 case SR_CONF_SCAN_OPTIONS:
360 case SR_CONF_DEVICE_OPTIONS:
362 return STD_CONFIG_LIST(key, data, sdi, cg,
363 scanopts, drvopts, devopts_generic);
364 return std_opts_config_list(key, data, sdi, cg,
365 ARRAY_AND_SIZE(scanopts), ARRAY_AND_SIZE(drvopts),
366 devc->model->devopts, devc->model->devopts_size);
367 case SR_CONF_MEASURED_QUANTITY:
368 /* TODO Use std_gvar_measured_quantities() when available. */
371 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
372 for (i = 0; i < devc->model->mqopt_size; i++) {
373 arr[0] = g_variant_new_uint32(devc->model->mqopts[i].mq);
374 arr[1] = g_variant_new_uint64(devc->model->mqopts[i].mqflag);
375 gvar = g_variant_new_tuple(arr, ARRAY_SIZE(arr));
376 g_variant_builder_add_value(&gvb, gvar);
378 *data = g_variant_builder_end(&gvb);
386 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
388 struct sr_scpi_dev_inst *scpi;
389 struct dev_context *devc;
391 const struct mqopt_item *item;
398 ret = scpi_dmm_get_mq(sdi, &devc->start_acq_mq.curr_mq,
399 &devc->start_acq_mq.curr_mqflag, NULL, &item);
404 * Query for current precision if DMM supports the command
406 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_QUERY_PREC);
407 if (command && *command) {
408 scpi_dmm_cmd_delay(scpi);
409 ret = sr_scpi_get_string(scpi, command, &response);
411 g_strstrip(response);
413 g_free(devc->precision);
414 devc->precision=g_strdup(response);
416 sr_dbg("%s: Precision: '%s'", __func__, devc->precision);
418 sr_info("%s: Precision query ('%s') failed: %d",
419 __func__, command, ret);
423 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_START_ACQ);
424 if (command && *command) {
425 scpi_dmm_cmd_delay(scpi);
426 ret = sr_scpi_send(scpi, command);
431 sr_sw_limits_acquisition_start(&devc->limits);
432 ret = std_session_send_df_header(sdi);
436 ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 10,
437 scpi_dmm_receive_data, (void *)sdi);
444 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
446 struct sr_scpi_dev_inst *scpi;
447 struct dev_context *devc;
453 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_STOP_ACQ);
454 if (command && *command) {
455 scpi_dmm_cmd_delay(scpi);
456 (void)sr_scpi_send(scpi, command);
458 sr_scpi_source_remove(sdi->session, scpi);
460 std_session_send_df_end(sdi);
462 if (devc->precision) {
463 g_free(devc->precision);
464 devc->precision = NULL;
470 static struct sr_dev_driver scpi_dmm_driver_info = {
472 .longname = "SCPI DMM",
475 .cleanup = std_cleanup,
477 .dev_list = std_dev_list,
478 .dev_clear = std_dev_clear,
479 .config_get = config_get,
480 .config_set = config_set,
481 .config_list = config_list,
482 .dev_open = dev_open,
483 .dev_close = dev_close,
484 .dev_acquisition_start = dev_acquisition_start,
485 .dev_acquisition_stop = dev_acquisition_stop,
488 SR_REGISTER_DEV_DRIVER(scpi_dmm_driver_info);