]> sigrok.org Git - libsigrok.git/blame - src/hardware/scpi-dmm/api.c
contrib: add udev rule for Keysight USBTMC
[libsigrok.git] / src / hardware / scpi-dmm / api.c
CommitLineData
7a396ff5
GS
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2018 Gerhard Sittig <gerhard.sittig@gmx.net>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
3cdad416 20#include <string.h>
7a396ff5
GS
21#include "protocol.h"
22
0617bb5a
GS
23static struct sr_dev_driver scpi_dmm_driver_info;
24
3cdad416
GS
25static const uint32_t scanopts[] = {
26 SR_CONF_CONN,
31e65c62 27 SR_CONF_SERIALCOMM,
3cdad416
GS
28};
29
30static const uint32_t drvopts[] = {
31 SR_CONF_MULTIMETER,
32};
33
1d2f9963 34static const uint32_t devopts_generic[] = {
3cdad416 35 SR_CONF_CONTINUOUS,
7d95afb9 36 SR_CONF_CONN | SR_CONF_GET,
3cdad416
GS
37 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
38 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
39 SR_CONF_MEASURED_QUANTITY | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
40};
41
42static const struct scpi_command cmdset_agilent[] = {
43 { DMM_CMD_SETUP_REMOTE, "\n", },
44 { DMM_CMD_SETUP_FUNC, "CONF:%s", },
45 { DMM_CMD_QUERY_FUNC, "CONF?", },
46 { DMM_CMD_START_ACQ, "MEAS", },
47 { DMM_CMD_STOP_ACQ, "ABORT", },
48 { DMM_CMD_QUERY_VALUE, "READ?", },
49 { DMM_CMD_QUERY_PREC, "CONF?", },
50 ALL_ZERO,
51};
52
a1619831 53/*
1c8110db 54 * cmdset_hp is used for the 34401A, which was added to this code after the
a1619831
AG
55 * 34405A and 34465A. It differs in starting the measurement with INIT: using
56 * MEAS without a trailing '?' (as used for the 34405A) is not valid for the
1c8110db
FS
57 * 34401A and gives an error.
58 * I'm surprised the same instruction sequence doesn't work and INIT may
59 * work for both, but I don't have the others to re-test.
a1619831 60 *
85cff5cf
MW
61 * cmdset_hp also works well for the 34410A, using cmdset_agilent throws an
62 * error on 'MEAS' without a '?'.
63 *
1c8110db
FS
64 * On the 34401A,
65 * MEAS <optional parameters> ? configures, arms, triggers and waits
a1619831
AG
66 * for a reading
67 * CONF <parameters> configures
1c8110db 68 * INIT prepares for triggering (trigger mode is not set, assumed
a1619831
AG
69 * internal - external might time out)
70 * *OPC waits for completion, and
71 * READ? retrieves the result
72 */
73static const struct scpi_command cmdset_hp[] = {
74 { DMM_CMD_SETUP_REMOTE, "\n", },
75 { DMM_CMD_SETUP_FUNC, "CONF:%s", },
76 { DMM_CMD_QUERY_FUNC, "CONF?", },
77 { DMM_CMD_START_ACQ, "INIT", },
78 { DMM_CMD_STOP_ACQ, "ABORT", },
79 { DMM_CMD_QUERY_VALUE, "READ?", },
80 { DMM_CMD_QUERY_PREC, "CONF?", },
81 ALL_ZERO,
82};
83
d0b602f0
TK
84static const struct scpi_command cmdset_gwinstek[] = {
85 { DMM_CMD_SETUP_REMOTE, "SYST:REM", },
4c80a272 86 { DMM_CMD_SETUP_LOCAL, "SYST:LOC", },
d0b602f0
TK
87 { DMM_CMD_SETUP_FUNC, "CONF:%s", },
88 { DMM_CMD_QUERY_FUNC, "CONF:STAT:FUNC?", },
89 { DMM_CMD_START_ACQ, "*CLS;SYST:REM", },
d0b602f0
TK
90 { DMM_CMD_QUERY_VALUE, "VAL1?", },
91 { DMM_CMD_QUERY_PREC, "SENS:DET:RATE?", },
92 ALL_ZERO,
93};
94
868fc65e
TK
95static const struct scpi_command cmdset_gwinstek_906x[] = {
96 { DMM_CMD_SETUP_REMOTE, "SYST:REM", },
4c80a272 97 { DMM_CMD_SETUP_LOCAL, "SYST:LOC", },
868fc65e
TK
98 { DMM_CMD_SETUP_FUNC, "CONF:%s", },
99 { DMM_CMD_QUERY_FUNC, "CONF?", },
860ee00a
TK
100 { DMM_CMD_START_ACQ, "INIT", },
101 { DMM_CMD_STOP_ACQ, "ABORT", },
868fc65e
TK
102 { DMM_CMD_QUERY_VALUE, "VAL1?", },
103 { DMM_CMD_QUERY_PREC, "SENS:DET:RATE?", },
104 ALL_ZERO,
105};
106
2111d157
PA
107static const struct scpi_command cmdset_owon[] = {
108 { DMM_CMD_SETUP_REMOTE, "SYST:REM", },
109 { DMM_CMD_SETUP_LOCAL, "SYST:LOC", },
110 { DMM_CMD_SETUP_FUNC, "CONF:%s", },
111 { DMM_CMD_QUERY_FUNC, "FUNC?", },
112 { DMM_CMD_QUERY_VALUE, "MEAS1?", },
113 ALL_ZERO,
114};
115
64d03de1 116static const struct mqopt_item mqopts_agilent_34405a[] = {
3cdad416
GS
117 { SR_MQ_VOLTAGE, SR_MQFLAG_DC, "VOLT:DC", "VOLT ", NO_DFLT_PREC, },
118 { SR_MQ_VOLTAGE, SR_MQFLAG_AC, "VOLT:AC", "VOLT:AC ", NO_DFLT_PREC, },
119 { SR_MQ_CURRENT, SR_MQFLAG_DC, "CURR:DC", "CURR ", NO_DFLT_PREC, },
120 { SR_MQ_CURRENT, SR_MQFLAG_AC, "CURR:AC", "CURR:AC ", NO_DFLT_PREC, },
121 { SR_MQ_RESISTANCE, 0, "RES", "RES ", NO_DFLT_PREC, },
3cdad416
GS
122 { SR_MQ_CONTINUITY, 0, "CONT", "CONT", -1, },
123 { SR_MQ_CAPACITANCE, 0, "CAP", "CAP ", NO_DFLT_PREC, },
124 { SR_MQ_VOLTAGE, SR_MQFLAG_DC | SR_MQFLAG_DIODE, "DIOD", "DIOD", -4, },
125 { SR_MQ_TEMPERATURE, 0, "TEMP", "TEMP ", NO_DFLT_PREC, },
126 { SR_MQ_FREQUENCY, 0, "FREQ", "FREQ ", NO_DFLT_PREC, },
127};
128
a1619831
AG
129static const struct mqopt_item mqopts_agilent_34401a[] = {
130 { SR_MQ_VOLTAGE, SR_MQFLAG_DC, "VOLT:DC", "VOLT ", NO_DFLT_PREC, },
131 { SR_MQ_VOLTAGE, SR_MQFLAG_AC, "VOLT:AC", "VOLT:AC ", NO_DFLT_PREC, },
132 { SR_MQ_CURRENT, SR_MQFLAG_DC, "CURR:DC", "CURR ", NO_DFLT_PREC, },
133 { SR_MQ_CURRENT, SR_MQFLAG_AC, "CURR:AC", "CURR:AC ", NO_DFLT_PREC, },
134 { SR_MQ_RESISTANCE, 0, "RES", "RES ", NO_DFLT_PREC, },
135 { SR_MQ_RESISTANCE, SR_MQFLAG_FOUR_WIRE, "FRES", "FRES ", NO_DFLT_PREC, },
136 { SR_MQ_CONTINUITY, 0, "CONT", "CONT", -1, },
137 { SR_MQ_VOLTAGE, SR_MQFLAG_DC | SR_MQFLAG_DIODE, "DIOD", "DIOD", -4, },
138 { SR_MQ_FREQUENCY, 0, "FREQ", "FREQ ", NO_DFLT_PREC, },
139 { SR_MQ_TIME, 0, "PER", "PER ", NO_DFLT_PREC, },
140};
141
d0b602f0
TK
142static const struct mqopt_item mqopts_gwinstek_gdm8200a[] = {
143 { SR_MQ_VOLTAGE, SR_MQFLAG_DC, "VOLT:DC", "01", NO_DFLT_PREC, },
144 { SR_MQ_VOLTAGE, SR_MQFLAG_AC, "VOLT:AC", "02", NO_DFLT_PREC, },
145 { SR_MQ_CURRENT, SR_MQFLAG_DC, "CURR:DC", "03", NO_DFLT_PREC, },
146 { SR_MQ_CURRENT, SR_MQFLAG_AC, "CURR:AC", "04", NO_DFLT_PREC, },
147 { SR_MQ_CURRENT, SR_MQFLAG_DC, "CURR:DC", "05", NO_DFLT_PREC, }, /* mA */
148 { SR_MQ_CURRENT, SR_MQFLAG_AC, "CURR:AC", "06", NO_DFLT_PREC, }, /* mA */
149 { SR_MQ_RESISTANCE, 0, "RES", "07", NO_DFLT_PREC, },
150 { SR_MQ_RESISTANCE, SR_MQFLAG_FOUR_WIRE, "FRES", "16", NO_DFLT_PREC, },
151 { SR_MQ_CONTINUITY, 0, "CONT", "13", -1, },
152 { SR_MQ_VOLTAGE, SR_MQFLAG_DC | SR_MQFLAG_DIODE, "DIOD", "17", -4, },
153 { SR_MQ_TEMPERATURE, 0, "TEMP", "09", NO_DFLT_PREC, }, /* Celsius */
154 { SR_MQ_TEMPERATURE, 0, "TEMP", "15", NO_DFLT_PREC, }, /* Fahrenheit */
155 { SR_MQ_FREQUENCY, 0, "FREQ", "08", NO_DFLT_PREC, },
156 { SR_MQ_TIME, 0, "PER", "14", NO_DFLT_PREC, },
157};
158
868fc65e
TK
159static const struct mqopt_item mqopts_gwinstek_gdm906x[] = {
160 { SR_MQ_VOLTAGE, SR_MQFLAG_DC, "VOLT:DC", "VOLT ", NO_DFLT_PREC, },
161 { SR_MQ_VOLTAGE, SR_MQFLAG_AC, "VOLT:AC", "VOLT:AC", NO_DFLT_PREC, },
162 { SR_MQ_CURRENT, SR_MQFLAG_DC, "CURR:DC", "CURR ", NO_DFLT_PREC, },
163 { SR_MQ_CURRENT, SR_MQFLAG_AC, "CURR:AC", "CURR:AC", NO_DFLT_PREC, },
164 { SR_MQ_RESISTANCE, 0, "RES", "RES", NO_DFLT_PREC, },
165 { SR_MQ_RESISTANCE, SR_MQFLAG_FOUR_WIRE, "FRES", "FRES", NO_DFLT_PREC, },
166 { SR_MQ_CONTINUITY, 0, "CONT", "CONT", -1, },
167 { SR_MQ_VOLTAGE, SR_MQFLAG_DC | SR_MQFLAG_DIODE, "DIOD", "DIOD", -4, },
168 { SR_MQ_TEMPERATURE, 0, "TEMP", "TEMP", NO_DFLT_PREC, },
169 { SR_MQ_FREQUENCY, 0, "FREQ", "FREQ", NO_DFLT_PREC, },
170 { SR_MQ_TIME, 0, "PER", "PER", NO_DFLT_PREC, },
171 { SR_MQ_CAPACITANCE, 0, "CAP", "CAP", NO_DFLT_PREC, },
172};
173
2111d157
PA
174static const struct mqopt_item mqopts_owon_xdm2041[] = {
175 { SR_MQ_VOLTAGE, SR_MQFLAG_AC, "VOLT:AC", "VOLT AC", NO_DFLT_PREC, },
176 { SR_MQ_VOLTAGE, SR_MQFLAG_DC, "VOLT:DC", "VOLT", NO_DFLT_PREC, },
177 { SR_MQ_CURRENT, SR_MQFLAG_AC, "CURR:AC", "CURR AC", NO_DFLT_PREC, },
178 { SR_MQ_CURRENT, SR_MQFLAG_DC, "CURR:DC", "CURR", NO_DFLT_PREC, },
179 { SR_MQ_RESISTANCE, 0, "RES", "RES", NO_DFLT_PREC, },
180 { SR_MQ_RESISTANCE, SR_MQFLAG_FOUR_WIRE, "FRES", "FRES", NO_DFLT_PREC, },
181 { SR_MQ_CONTINUITY, 0, "CONT", "CONT", -1, },
182 { SR_MQ_VOLTAGE, SR_MQFLAG_DC | SR_MQFLAG_DIODE, "DIOD", "DIOD", -4, },
183 { SR_MQ_TEMPERATURE, 0, "TEMP", "TEMP", NO_DFLT_PREC, },
184 { SR_MQ_FREQUENCY, 0, "FREQ", "FREQ", NO_DFLT_PREC, },
185 { SR_MQ_CAPACITANCE, 0, "CAP", "CAP", NO_DFLT_PREC, },
186};
187
3cdad416
GS
188SR_PRIV const struct scpi_dmm_model models[] = {
189 {
190 "Agilent", "34405A",
64d03de1 191 1, 5, cmdset_agilent, ARRAY_AND_SIZE(mqopts_agilent_34405a),
3cdad416 192 scpi_dmm_get_meas_agilent,
1d2f9963 193 ARRAY_AND_SIZE(devopts_generic),
33306b13 194 0, 0,
3cdad416 195 },
85cff5cf
MW
196 {
197 "Agilent", "34410A",
198 1, 6, cmdset_hp, ARRAY_AND_SIZE(mqopts_agilent_34405a),
199 scpi_dmm_get_meas_agilent,
200 ARRAY_AND_SIZE(devopts_generic),
33306b13 201 0, 0,
85cff5cf 202 },
d0b602f0
TK
203 {
204 "GW", "GDM8251A",
205 1, 6, cmdset_gwinstek, ARRAY_AND_SIZE(mqopts_gwinstek_gdm8200a),
206 scpi_dmm_get_meas_gwinstek,
207 ARRAY_AND_SIZE(devopts_generic),
33306b13 208 1000 * 2500, 0,
d0b602f0
TK
209 },
210 {
211 "GW", "GDM8255A",
212 1, 6, cmdset_gwinstek, ARRAY_AND_SIZE(mqopts_gwinstek_gdm8200a),
213 scpi_dmm_get_meas_gwinstek,
214 ARRAY_AND_SIZE(devopts_generic),
33306b13 215 1000 * 2500, 0,
d0b602f0 216 },
868fc65e
TK
217 {
218 "GWInstek", "GDM9060",
219 1, 6, cmdset_gwinstek_906x, ARRAY_AND_SIZE(mqopts_gwinstek_gdm906x),
220 scpi_dmm_get_meas_agilent,
221 ARRAY_AND_SIZE(devopts_generic),
33306b13 222 0, 0,
868fc65e
TK
223 },
224 {
225 "GWInstek", "GDM9061",
226 1, 6, cmdset_gwinstek_906x, ARRAY_AND_SIZE(mqopts_gwinstek_gdm906x),
227 scpi_dmm_get_meas_agilent,
228 ARRAY_AND_SIZE(devopts_generic),
33306b13 229 0, 0,
868fc65e 230 },
25879a34
GS
231 {
232 "HP", "34401A",
233 1, 6, cmdset_hp, ARRAY_AND_SIZE(mqopts_agilent_34401a),
234 scpi_dmm_get_meas_agilent,
235 ARRAY_AND_SIZE(devopts_generic),
236 /* 34401A: typ. 1020ms for AC readings (default is 1000ms). */
33306b13 237 1000 * 1500, 0,
25879a34
GS
238 },
239 {
240 "Keysight", "34465A",
241 1, 5, cmdset_agilent, ARRAY_AND_SIZE(mqopts_agilent_34405a),
242 scpi_dmm_get_meas_agilent,
243 ARRAY_AND_SIZE(devopts_generic),
33306b13 244 0, 0,
25879a34 245 },
2111d157
PA
246 {
247 "OWON", "XDM2041",
248 1, 5, cmdset_owon, ARRAY_AND_SIZE(mqopts_owon_xdm2041),
249 scpi_dmm_get_meas_gwinstek,
250 ARRAY_AND_SIZE(devopts_generic),
251 0, 1e9,
252 },
3cdad416
GS
253};
254
255static const struct scpi_dmm_model *is_compatible(const char *vendor, const char *model)
7a396ff5 256{
3cdad416
GS
257 size_t i;
258 const struct scpi_dmm_model *entry;
259
260 for (i = 0; i < ARRAY_SIZE(models); i++) {
261 entry = &models[i];
262 if (!entry->vendor || !entry->model)
263 continue;
264 if (strcmp(vendor, entry->vendor) != 0)
265 continue;
266 if (strcmp(model, entry->model) != 0)
267 continue;
268 return entry;
269 }
7a396ff5 270
3cdad416
GS
271 return NULL;
272}
7a396ff5 273
ddeaa49d
PA
274/*
275 * Some devices (such as Owon XDM2041) do not support the standard
276 * OPeration Complete? command. This function tests the command with
277 * a short timeout, and returns TRUE if any reply (busy or not) is received.
278 */
279static gboolean probe_opc_support(struct sr_scpi_dev_inst *scpi)
280{
281 gboolean result;
282 GString *response;
283
284 response = g_string_sized_new(128);
285 result = TRUE;
286 if (sr_scpi_get_data(scpi, SCPI_CMD_OPC, &response) != SR_OK)
287 result = FALSE;
288 g_string_free(response, TRUE);
289
290 return result;
291}
292
3cdad416
GS
293static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
294{
295 struct sr_scpi_hw_info *hw_info;
296 int ret;
297 const char *vendor;
298 const struct scpi_dmm_model *model;
299 struct sr_dev_inst *sdi;
300 struct dev_context *devc;
301 size_t i;
302 gchar *channel_name;
4c80a272 303 const char *command;
3cdad416 304
ddeaa49d
PA
305 if (!probe_opc_support(scpi))
306 scpi->no_opc_command = TRUE;
307
3cdad416 308 scpi_dmm_cmd_delay(scpi);
28877994 309 ret = sr_scpi_get_hw_id(scpi, &hw_info);
3cdad416
GS
310 if (ret != SR_OK) {
311 sr_info("Could not get IDN response.");
312 return NULL;
313 }
314 vendor = sr_vendor_alias(hw_info->manufacturer);
315 model = is_compatible(vendor, hw_info->model);
316 if (!model) {
317 sr_scpi_hw_info_free(hw_info);
318 return NULL;
319 }
7a396ff5 320
3cdad416
GS
321 sdi = g_malloc0(sizeof(*sdi));
322 sdi->vendor = g_strdup(hw_info->manufacturer);
323 sdi->model = g_strdup(hw_info->model);
324 sdi->version = g_strdup(hw_info->firmware_version);
325 sdi->serial_num = g_strdup(hw_info->serial_number);
326 sdi->conn = scpi;
327 sdi->driver = &scpi_dmm_driver_info;
328 sdi->inst_type = SR_INST_SCPI;
7d95afb9
GS
329 ret = sr_scpi_connection_id(scpi, &sdi->connection_id);
330 if (ret != SR_OK) {
331 g_free(sdi->connection_id);
332 sdi->connection_id = NULL;
333 }
3cdad416 334 sr_scpi_hw_info_free(hw_info);
1c8110db 335 if (model->read_timeout_us) /* non-default read timeout */
a1619831 336 scpi->read_timeout_us = model->read_timeout_us;
3cdad416
GS
337 devc = g_malloc0(sizeof(*devc));
338 sdi->priv = devc;
339 devc->num_channels = model->num_channels;
340 devc->cmdset = model->cmdset;
341 devc->model = model;
342
343 for (i = 0; i < devc->num_channels; i++) {
344 channel_name = g_strdup_printf("P%zu", i + 1);
345 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, channel_name);
346 }
347
4c80a272
TK
348 /*
349 * If device has DMM_CMD_SETUP_LOCAL command, send it now. To avoid
350 * leaving device in remote mode (if only a "scan" is run).
351 */
352 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_SETUP_LOCAL);
353 if (command && *command) {
354 scpi_dmm_cmd_delay(scpi);
355 sr_scpi_send(scpi, command);
356 }
357
3cdad416
GS
358 return sdi;
359}
7a396ff5 360
3cdad416
GS
361static GSList *scan(struct sr_dev_driver *di, GSList *options)
362{
363 return sr_scpi_scan(di->context, options, probe_device);
7a396ff5
GS
364}
365
366static int dev_open(struct sr_dev_inst *sdi)
367{
3cdad416
GS
368 struct sr_scpi_dev_inst *scpi;
369 int ret;
7a396ff5 370
3cdad416
GS
371 scpi = sdi->conn;
372 ret = sr_scpi_open(scpi);
373 if (ret < 0) {
374 sr_err("Failed to open SCPI device: %s.", sr_strerror(ret));
375 return SR_ERR;
376 }
7a396ff5
GS
377
378 return SR_OK;
379}
380
381static int dev_close(struct sr_dev_inst *sdi)
382{
4c80a272 383 struct dev_context *devc;
3cdad416 384 struct sr_scpi_dev_inst *scpi;
4c80a272 385 const char *command;
7a396ff5 386
4c80a272 387 devc = sdi->priv;
3cdad416
GS
388 scpi = sdi->conn;
389 if (!scpi)
390 return SR_ERR_BUG;
7a396ff5 391
3cdad416
GS
392 sr_dbg("DIAG: sdi->status %d.", sdi->status - SR_ST_NOT_FOUND);
393 if (sdi->status <= SR_ST_INACTIVE)
394 return SR_OK;
395
4c80a272
TK
396 /*
397 * If device has DMM_CMD_SETUP_LOCAL command, send it now
398 * to avoid leaving device in remote mode.
399 */
400 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_SETUP_LOCAL);
401 if (command && *command) {
402 scpi_dmm_cmd_delay(scpi);
403 sr_scpi_send(scpi, command);
404 }
405
3cdad416 406 return sr_scpi_close(scpi);
7a396ff5
GS
407}
408
409static int config_get(uint32_t key, GVariant **data,
410 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
411{
3cdad416
GS
412 struct dev_context *devc;
413 enum sr_mq mq;
414 enum sr_mqflag mqflag;
415 GVariant *arr[2];
7a396ff5
GS
416 int ret;
417
7a396ff5
GS
418 (void)cg;
419
3cdad416
GS
420 devc = sdi->priv;
421
7a396ff5 422 switch (key) {
7d95afb9
GS
423 case SR_CONF_CONN:
424 if (!sdi || !sdi->connection_id)
425 return SR_ERR_NA;
426 *data = g_variant_new_string(sdi->connection_id);
427 return SR_OK;
3cdad416
GS
428 case SR_CONF_LIMIT_SAMPLES:
429 case SR_CONF_LIMIT_MSEC:
430 return sr_sw_limits_config_get(&devc->limits, key, data);
431 case SR_CONF_MEASURED_QUANTITY:
08f3b427 432 ret = scpi_dmm_get_mq(sdi, &mq, &mqflag, NULL, NULL);
3cdad416
GS
433 if (ret != SR_OK)
434 return ret;
435 arr[0] = g_variant_new_uint32(mq);
436 arr[1] = g_variant_new_uint64(mqflag);
437 *data = g_variant_new_tuple(arr, ARRAY_SIZE(arr));
438 return SR_OK;
7a396ff5
GS
439 default:
440 return SR_ERR_NA;
441 }
7a396ff5
GS
442}
443
444static int config_set(uint32_t key, GVariant *data,
445 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
446{
3cdad416
GS
447 struct dev_context *devc;
448 enum sr_mq mq;
449 enum sr_mqflag mqflag;
450 GVariant *tuple_child;
7a396ff5 451
7a396ff5
GS
452 (void)cg;
453
3cdad416
GS
454 devc = sdi->priv;
455
7a396ff5 456 switch (key) {
3cdad416
GS
457 case SR_CONF_LIMIT_SAMPLES:
458 case SR_CONF_LIMIT_MSEC:
459 return sr_sw_limits_config_set(&devc->limits, key, data);
460 case SR_CONF_MEASURED_QUANTITY:
461 tuple_child = g_variant_get_child_value(data, 0);
462 mq = g_variant_get_uint32(tuple_child);
ff5fb185 463 g_variant_unref(tuple_child);
3cdad416
GS
464 tuple_child = g_variant_get_child_value(data, 1);
465 mqflag = g_variant_get_uint64(tuple_child);
466 g_variant_unref(tuple_child);
467 return scpi_dmm_set_mq(sdi, mq, mqflag);
7a396ff5 468 default:
3cdad416 469 return SR_ERR_NA;
7a396ff5 470 }
7a396ff5
GS
471}
472
473static int config_list(uint32_t key, GVariant **data,
474 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
475{
3cdad416
GS
476 struct dev_context *devc;
477 GVariant *gvar, *arr[2];
478 GVariantBuilder gvb;
479 size_t i;
7a396ff5 480
7a396ff5
GS
481 (void)cg;
482
3cdad416
GS
483 devc = sdi ? sdi->priv : NULL;
484
7a396ff5 485 switch (key) {
3cdad416
GS
486 case SR_CONF_SCAN_OPTIONS:
487 case SR_CONF_DEVICE_OPTIONS:
1d2f9963
GS
488 if (!devc)
489 return STD_CONFIG_LIST(key, data, sdi, cg,
490 scanopts, drvopts, devopts_generic);
491 return std_opts_config_list(key, data, sdi, cg,
492 ARRAY_AND_SIZE(scanopts), ARRAY_AND_SIZE(drvopts),
493 devc->model->devopts, devc->model->devopts_size);
3cdad416
GS
494 case SR_CONF_MEASURED_QUANTITY:
495 /* TODO Use std_gvar_measured_quantities() when available. */
496 if (!devc)
497 return SR_ERR_ARG;
498 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
499 for (i = 0; i < devc->model->mqopt_size; i++) {
500 arr[0] = g_variant_new_uint32(devc->model->mqopts[i].mq);
501 arr[1] = g_variant_new_uint64(devc->model->mqopts[i].mqflag);
502 gvar = g_variant_new_tuple(arr, ARRAY_SIZE(arr));
503 g_variant_builder_add_value(&gvb, gvar);
504 }
505 *data = g_variant_builder_end(&gvb);
506 return SR_OK;
7a396ff5 507 default:
3cdad416 508 (void)devc;
7a396ff5
GS
509 return SR_ERR_NA;
510 }
7a396ff5
GS
511}
512
513static int dev_acquisition_start(const struct sr_dev_inst *sdi)
514{
3cdad416
GS
515 struct sr_scpi_dev_inst *scpi;
516 struct dev_context *devc;
517 int ret;
08f3b427 518 const struct mqopt_item *item;
3cdad416 519 const char *command;
d0b602f0 520 char *response;
3cdad416
GS
521
522 scpi = sdi->conn;
523 devc = sdi->priv;
524
525 ret = scpi_dmm_get_mq(sdi, &devc->start_acq_mq.curr_mq,
08f3b427 526 &devc->start_acq_mq.curr_mqflag, NULL, &item);
3cdad416
GS
527 if (ret != SR_OK)
528 return ret;
529
d0b602f0
TK
530 /*
531 * Query for current precision if DMM supports the command
532 */
533 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_QUERY_PREC);
534 if (command && *command) {
535 scpi_dmm_cmd_delay(scpi);
536 ret = sr_scpi_get_string(scpi, command, &response);
537 if (ret == SR_OK) {
538 g_strstrip(response);
33aa8117
GS
539 g_free(devc->precision);
540 devc->precision = g_strdup(response);
d0b602f0
TK
541 g_free(response);
542 sr_dbg("%s: Precision: '%s'", __func__, devc->precision);
543 } else {
33aa8117
GS
544 sr_info("Precision query ('%s') failed: %d",
545 command, ret);
d0b602f0
TK
546 }
547 }
548
3cdad416
GS
549 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_START_ACQ);
550 if (command && *command) {
3cdad416 551 scpi_dmm_cmd_delay(scpi);
28877994 552 ret = sr_scpi_send(scpi, command);
3cdad416
GS
553 if (ret != SR_OK)
554 return ret;
555 }
7a396ff5 556
3cdad416
GS
557 sr_sw_limits_acquisition_start(&devc->limits);
558 ret = std_session_send_df_header(sdi);
559 if (ret != SR_OK)
560 return ret;
561
562 ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 10,
563 scpi_dmm_receive_data, (void *)sdi);
564 if (ret != SR_OK)
565 return ret;
7a396ff5
GS
566
567 return SR_OK;
568}
569
570static int dev_acquisition_stop(struct sr_dev_inst *sdi)
571{
3cdad416
GS
572 struct sr_scpi_dev_inst *scpi;
573 struct dev_context *devc;
574 const char *command;
575
576 scpi = sdi->conn;
577 devc = sdi->priv;
7a396ff5 578
3cdad416
GS
579 command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_STOP_ACQ);
580 if (command && *command) {
3cdad416 581 scpi_dmm_cmd_delay(scpi);
28877994 582 (void)sr_scpi_send(scpi, command);
3cdad416
GS
583 }
584 sr_scpi_source_remove(sdi->session, scpi);
585
586 std_session_send_df_end(sdi);
7a396ff5 587
33aa8117
GS
588 g_free(devc->precision);
589 devc->precision = NULL;
d0b602f0 590
7a396ff5
GS
591 return SR_OK;
592}
593
0617bb5a 594static struct sr_dev_driver scpi_dmm_driver_info = {
7a396ff5
GS
595 .name = "scpi-dmm",
596 .longname = "SCPI DMM",
597 .api_version = 1,
598 .init = std_init,
599 .cleanup = std_cleanup,
600 .scan = scan,
601 .dev_list = std_dev_list,
602 .dev_clear = std_dev_clear,
603 .config_get = config_get,
604 .config_set = config_set,
605 .config_list = config_list,
606 .dev_open = dev_open,
607 .dev_close = dev_close,
608 .dev_acquisition_start = dev_acquisition_start,
609 .dev_acquisition_stop = dev_acquisition_stop,
610 .context = NULL,
611};
7a396ff5 612SR_REGISTER_DEV_DRIVER(scpi_dmm_driver_info);