]>
Commit | Line | Data |
---|---|---|
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 |
23 | static struct sr_dev_driver scpi_dmm_driver_info; |
24 | ||
3cdad416 GS |
25 | static const uint32_t scanopts[] = { |
26 | SR_CONF_CONN, | |
31e65c62 | 27 | SR_CONF_SERIALCOMM, |
3cdad416 GS |
28 | }; |
29 | ||
30 | static const uint32_t drvopts[] = { | |
31 | SR_CONF_MULTIMETER, | |
32 | }; | |
33 | ||
1d2f9963 | 34 | static const uint32_t devopts_generic[] = { |
3cdad416 GS |
35 | SR_CONF_CONTINUOUS, |
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, | |
39 | }; | |
40 | ||
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?", }, | |
49 | ALL_ZERO, | |
50 | }; | |
51 | ||
a1619831 | 52 | /* |
1c8110db | 53 | * cmdset_hp is used for the 34401A, which was added to this code after the |
a1619831 AG |
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 | |
1c8110db FS |
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. | |
a1619831 | 59 | * |
85cff5cf MW |
60 | * cmdset_hp also works well for the 34410A, using cmdset_agilent throws an |
61 | * error on 'MEAS' without a '?'. | |
62 | * | |
1c8110db FS |
63 | * On the 34401A, |
64 | * MEAS <optional parameters> ? configures, arms, triggers and waits | |
a1619831 AG |
65 | * for a reading |
66 | * CONF <parameters> configures | |
1c8110db | 67 | * INIT prepares for triggering (trigger mode is not set, assumed |
a1619831 AG |
68 | * internal - external might time out) |
69 | * *OPC waits for completion, and | |
70 | * READ? retrieves the result | |
71 | */ | |
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?", }, | |
80 | ALL_ZERO, | |
81 | }; | |
82 | ||
64d03de1 | 83 | static const struct mqopt_item mqopts_agilent_34405a[] = { |
3cdad416 GS |
84 | { SR_MQ_VOLTAGE, SR_MQFLAG_DC, "VOLT:DC", "VOLT ", NO_DFLT_PREC, }, |
85 | { SR_MQ_VOLTAGE, SR_MQFLAG_AC, "VOLT:AC", "VOLT:AC ", NO_DFLT_PREC, }, | |
86 | { SR_MQ_CURRENT, SR_MQFLAG_DC, "CURR:DC", "CURR ", NO_DFLT_PREC, }, | |
87 | { SR_MQ_CURRENT, SR_MQFLAG_AC, "CURR:AC", "CURR:AC ", NO_DFLT_PREC, }, | |
88 | { SR_MQ_RESISTANCE, 0, "RES", "RES ", NO_DFLT_PREC, }, | |
3cdad416 GS |
89 | { SR_MQ_CONTINUITY, 0, "CONT", "CONT", -1, }, |
90 | { SR_MQ_CAPACITANCE, 0, "CAP", "CAP ", NO_DFLT_PREC, }, | |
91 | { SR_MQ_VOLTAGE, SR_MQFLAG_DC | SR_MQFLAG_DIODE, "DIOD", "DIOD", -4, }, | |
92 | { SR_MQ_TEMPERATURE, 0, "TEMP", "TEMP ", NO_DFLT_PREC, }, | |
93 | { SR_MQ_FREQUENCY, 0, "FREQ", "FREQ ", NO_DFLT_PREC, }, | |
94 | }; | |
95 | ||
a1619831 AG |
96 | static const struct mqopt_item mqopts_agilent_34401a[] = { |
97 | { SR_MQ_VOLTAGE, SR_MQFLAG_DC, "VOLT:DC", "VOLT ", NO_DFLT_PREC, }, | |
98 | { SR_MQ_VOLTAGE, SR_MQFLAG_AC, "VOLT:AC", "VOLT:AC ", NO_DFLT_PREC, }, | |
99 | { SR_MQ_CURRENT, SR_MQFLAG_DC, "CURR:DC", "CURR ", NO_DFLT_PREC, }, | |
100 | { SR_MQ_CURRENT, SR_MQFLAG_AC, "CURR:AC", "CURR:AC ", NO_DFLT_PREC, }, | |
101 | { SR_MQ_RESISTANCE, 0, "RES", "RES ", NO_DFLT_PREC, }, | |
102 | { SR_MQ_RESISTANCE, SR_MQFLAG_FOUR_WIRE, "FRES", "FRES ", NO_DFLT_PREC, }, | |
103 | { SR_MQ_CONTINUITY, 0, "CONT", "CONT", -1, }, | |
104 | { SR_MQ_VOLTAGE, SR_MQFLAG_DC | SR_MQFLAG_DIODE, "DIOD", "DIOD", -4, }, | |
105 | { SR_MQ_FREQUENCY, 0, "FREQ", "FREQ ", NO_DFLT_PREC, }, | |
106 | { SR_MQ_TIME, 0, "PER", "PER ", NO_DFLT_PREC, }, | |
107 | }; | |
108 | ||
3cdad416 GS |
109 | SR_PRIV const struct scpi_dmm_model models[] = { |
110 | { | |
111 | "Agilent", "34405A", | |
64d03de1 | 112 | 1, 5, cmdset_agilent, ARRAY_AND_SIZE(mqopts_agilent_34405a), |
3cdad416 | 113 | scpi_dmm_get_meas_agilent, |
1d2f9963 | 114 | ARRAY_AND_SIZE(devopts_generic), |
1c8110db | 115 | 0, |
3cdad416 | 116 | }, |
85cff5cf MW |
117 | { |
118 | "Agilent", "34410A", | |
119 | 1, 6, cmdset_hp, ARRAY_AND_SIZE(mqopts_agilent_34405a), | |
120 | scpi_dmm_get_meas_agilent, | |
121 | ARRAY_AND_SIZE(devopts_generic), | |
122 | 0, | |
123 | }, | |
08e8c0e5 MW |
124 | { |
125 | "Keysight", "34465A", | |
126 | 1, 5, cmdset_agilent, ARRAY_AND_SIZE(mqopts_agilent_34405a), | |
127 | scpi_dmm_get_meas_agilent, | |
128 | ARRAY_AND_SIZE(devopts_generic), | |
1c8110db | 129 | 0, |
a1619831 AG |
130 | }, |
131 | { | |
132 | "HP", "34401A", | |
133 | 1, 6, cmdset_hp, ARRAY_AND_SIZE(mqopts_agilent_34401a), | |
134 | scpi_dmm_get_meas_agilent, | |
135 | ARRAY_AND_SIZE(devopts_generic), | |
136 | /* 34401A: typ. 1020ms for AC readings (default is 1000ms). */ | |
1c8110db | 137 | 1000 * 1500, |
08e8c0e5 | 138 | }, |
3cdad416 GS |
139 | }; |
140 | ||
141 | static const struct scpi_dmm_model *is_compatible(const char *vendor, const char *model) | |
7a396ff5 | 142 | { |
3cdad416 GS |
143 | size_t i; |
144 | const struct scpi_dmm_model *entry; | |
145 | ||
146 | for (i = 0; i < ARRAY_SIZE(models); i++) { | |
147 | entry = &models[i]; | |
148 | if (!entry->vendor || !entry->model) | |
149 | continue; | |
150 | if (strcmp(vendor, entry->vendor) != 0) | |
151 | continue; | |
152 | if (strcmp(model, entry->model) != 0) | |
153 | continue; | |
154 | return entry; | |
155 | } | |
7a396ff5 | 156 | |
3cdad416 GS |
157 | return NULL; |
158 | } | |
7a396ff5 | 159 | |
3cdad416 GS |
160 | static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi) |
161 | { | |
162 | struct sr_scpi_hw_info *hw_info; | |
163 | int ret; | |
164 | const char *vendor; | |
165 | const struct scpi_dmm_model *model; | |
166 | struct sr_dev_inst *sdi; | |
167 | struct dev_context *devc; | |
168 | size_t i; | |
169 | gchar *channel_name; | |
170 | ||
3cdad416 | 171 | scpi_dmm_cmd_delay(scpi); |
28877994 | 172 | ret = sr_scpi_get_hw_id(scpi, &hw_info); |
3cdad416 GS |
173 | if (ret != SR_OK) { |
174 | sr_info("Could not get IDN response."); | |
175 | return NULL; | |
176 | } | |
177 | vendor = sr_vendor_alias(hw_info->manufacturer); | |
178 | model = is_compatible(vendor, hw_info->model); | |
179 | if (!model) { | |
180 | sr_scpi_hw_info_free(hw_info); | |
181 | return NULL; | |
182 | } | |
7a396ff5 | 183 | |
3cdad416 GS |
184 | sdi = g_malloc0(sizeof(*sdi)); |
185 | sdi->vendor = g_strdup(hw_info->manufacturer); | |
186 | sdi->model = g_strdup(hw_info->model); | |
187 | sdi->version = g_strdup(hw_info->firmware_version); | |
188 | sdi->serial_num = g_strdup(hw_info->serial_number); | |
189 | sdi->conn = scpi; | |
190 | sdi->driver = &scpi_dmm_driver_info; | |
191 | sdi->inst_type = SR_INST_SCPI; | |
192 | sr_scpi_hw_info_free(hw_info); | |
1c8110db | 193 | if (model->read_timeout_us) /* non-default read timeout */ |
a1619831 | 194 | scpi->read_timeout_us = model->read_timeout_us; |
3cdad416 GS |
195 | devc = g_malloc0(sizeof(*devc)); |
196 | sdi->priv = devc; | |
197 | devc->num_channels = model->num_channels; | |
198 | devc->cmdset = model->cmdset; | |
199 | devc->model = model; | |
200 | ||
201 | for (i = 0; i < devc->num_channels; i++) { | |
202 | channel_name = g_strdup_printf("P%zu", i + 1); | |
203 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, channel_name); | |
204 | } | |
205 | ||
206 | return sdi; | |
207 | } | |
7a396ff5 | 208 | |
3cdad416 GS |
209 | static GSList *scan(struct sr_dev_driver *di, GSList *options) |
210 | { | |
211 | return sr_scpi_scan(di->context, options, probe_device); | |
7a396ff5 GS |
212 | } |
213 | ||
214 | static int dev_open(struct sr_dev_inst *sdi) | |
215 | { | |
3cdad416 GS |
216 | struct sr_scpi_dev_inst *scpi; |
217 | int ret; | |
7a396ff5 | 218 | |
3cdad416 GS |
219 | scpi = sdi->conn; |
220 | ret = sr_scpi_open(scpi); | |
221 | if (ret < 0) { | |
222 | sr_err("Failed to open SCPI device: %s.", sr_strerror(ret)); | |
223 | return SR_ERR; | |
224 | } | |
7a396ff5 GS |
225 | |
226 | return SR_OK; | |
227 | } | |
228 | ||
229 | static int dev_close(struct sr_dev_inst *sdi) | |
230 | { | |
3cdad416 | 231 | struct sr_scpi_dev_inst *scpi; |
7a396ff5 | 232 | |
3cdad416 GS |
233 | scpi = sdi->conn; |
234 | if (!scpi) | |
235 | return SR_ERR_BUG; | |
7a396ff5 | 236 | |
3cdad416 GS |
237 | sr_dbg("DIAG: sdi->status %d.", sdi->status - SR_ST_NOT_FOUND); |
238 | if (sdi->status <= SR_ST_INACTIVE) | |
239 | return SR_OK; | |
240 | ||
241 | return sr_scpi_close(scpi); | |
7a396ff5 GS |
242 | } |
243 | ||
244 | static int config_get(uint32_t key, GVariant **data, | |
245 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
246 | { | |
3cdad416 GS |
247 | struct dev_context *devc; |
248 | enum sr_mq mq; | |
249 | enum sr_mqflag mqflag; | |
250 | GVariant *arr[2]; | |
7a396ff5 GS |
251 | int ret; |
252 | ||
7a396ff5 GS |
253 | (void)cg; |
254 | ||
3cdad416 GS |
255 | devc = sdi->priv; |
256 | ||
7a396ff5 | 257 | switch (key) { |
3cdad416 GS |
258 | case SR_CONF_LIMIT_SAMPLES: |
259 | case SR_CONF_LIMIT_MSEC: | |
260 | return sr_sw_limits_config_get(&devc->limits, key, data); | |
261 | case SR_CONF_MEASURED_QUANTITY: | |
08f3b427 | 262 | ret = scpi_dmm_get_mq(sdi, &mq, &mqflag, NULL, NULL); |
3cdad416 GS |
263 | if (ret != SR_OK) |
264 | return ret; | |
265 | arr[0] = g_variant_new_uint32(mq); | |
266 | arr[1] = g_variant_new_uint64(mqflag); | |
267 | *data = g_variant_new_tuple(arr, ARRAY_SIZE(arr)); | |
268 | return SR_OK; | |
7a396ff5 GS |
269 | default: |
270 | return SR_ERR_NA; | |
271 | } | |
7a396ff5 GS |
272 | } |
273 | ||
274 | static int config_set(uint32_t key, GVariant *data, | |
275 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
276 | { | |
3cdad416 GS |
277 | struct dev_context *devc; |
278 | enum sr_mq mq; | |
279 | enum sr_mqflag mqflag; | |
280 | GVariant *tuple_child; | |
7a396ff5 | 281 | |
7a396ff5 GS |
282 | (void)cg; |
283 | ||
3cdad416 GS |
284 | devc = sdi->priv; |
285 | ||
7a396ff5 | 286 | switch (key) { |
3cdad416 GS |
287 | case SR_CONF_LIMIT_SAMPLES: |
288 | case SR_CONF_LIMIT_MSEC: | |
289 | return sr_sw_limits_config_set(&devc->limits, key, data); | |
290 | case SR_CONF_MEASURED_QUANTITY: | |
291 | tuple_child = g_variant_get_child_value(data, 0); | |
292 | mq = g_variant_get_uint32(tuple_child); | |
ff5fb185 | 293 | g_variant_unref(tuple_child); |
3cdad416 GS |
294 | tuple_child = g_variant_get_child_value(data, 1); |
295 | mqflag = g_variant_get_uint64(tuple_child); | |
296 | g_variant_unref(tuple_child); | |
297 | return scpi_dmm_set_mq(sdi, mq, mqflag); | |
7a396ff5 | 298 | default: |
3cdad416 | 299 | return SR_ERR_NA; |
7a396ff5 | 300 | } |
7a396ff5 GS |
301 | } |
302 | ||
303 | static int config_list(uint32_t key, GVariant **data, | |
304 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
305 | { | |
3cdad416 GS |
306 | struct dev_context *devc; |
307 | GVariant *gvar, *arr[2]; | |
308 | GVariantBuilder gvb; | |
309 | size_t i; | |
7a396ff5 | 310 | |
7a396ff5 GS |
311 | (void)cg; |
312 | ||
3cdad416 GS |
313 | devc = sdi ? sdi->priv : NULL; |
314 | ||
7a396ff5 | 315 | switch (key) { |
3cdad416 GS |
316 | case SR_CONF_SCAN_OPTIONS: |
317 | case SR_CONF_DEVICE_OPTIONS: | |
1d2f9963 GS |
318 | if (!devc) |
319 | return STD_CONFIG_LIST(key, data, sdi, cg, | |
320 | scanopts, drvopts, devopts_generic); | |
321 | return std_opts_config_list(key, data, sdi, cg, | |
322 | ARRAY_AND_SIZE(scanopts), ARRAY_AND_SIZE(drvopts), | |
323 | devc->model->devopts, devc->model->devopts_size); | |
3cdad416 GS |
324 | case SR_CONF_MEASURED_QUANTITY: |
325 | /* TODO Use std_gvar_measured_quantities() when available. */ | |
326 | if (!devc) | |
327 | return SR_ERR_ARG; | |
328 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
329 | for (i = 0; i < devc->model->mqopt_size; i++) { | |
330 | arr[0] = g_variant_new_uint32(devc->model->mqopts[i].mq); | |
331 | arr[1] = g_variant_new_uint64(devc->model->mqopts[i].mqflag); | |
332 | gvar = g_variant_new_tuple(arr, ARRAY_SIZE(arr)); | |
333 | g_variant_builder_add_value(&gvb, gvar); | |
334 | } | |
335 | *data = g_variant_builder_end(&gvb); | |
336 | return SR_OK; | |
7a396ff5 | 337 | default: |
3cdad416 | 338 | (void)devc; |
7a396ff5 GS |
339 | return SR_ERR_NA; |
340 | } | |
7a396ff5 GS |
341 | } |
342 | ||
343 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
344 | { | |
3cdad416 GS |
345 | struct sr_scpi_dev_inst *scpi; |
346 | struct dev_context *devc; | |
347 | int ret; | |
08f3b427 | 348 | const struct mqopt_item *item; |
3cdad416 GS |
349 | const char *command; |
350 | ||
351 | scpi = sdi->conn; | |
352 | devc = sdi->priv; | |
353 | ||
354 | ret = scpi_dmm_get_mq(sdi, &devc->start_acq_mq.curr_mq, | |
08f3b427 | 355 | &devc->start_acq_mq.curr_mqflag, NULL, &item); |
3cdad416 GS |
356 | if (ret != SR_OK) |
357 | return ret; | |
358 | ||
359 | command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_START_ACQ); | |
360 | if (command && *command) { | |
3cdad416 | 361 | scpi_dmm_cmd_delay(scpi); |
28877994 | 362 | ret = sr_scpi_send(scpi, command); |
3cdad416 GS |
363 | if (ret != SR_OK) |
364 | return ret; | |
365 | } | |
7a396ff5 | 366 | |
3cdad416 GS |
367 | sr_sw_limits_acquisition_start(&devc->limits); |
368 | ret = std_session_send_df_header(sdi); | |
369 | if (ret != SR_OK) | |
370 | return ret; | |
371 | ||
372 | ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 10, | |
373 | scpi_dmm_receive_data, (void *)sdi); | |
374 | if (ret != SR_OK) | |
375 | return ret; | |
7a396ff5 GS |
376 | |
377 | return SR_OK; | |
378 | } | |
379 | ||
380 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
381 | { | |
3cdad416 GS |
382 | struct sr_scpi_dev_inst *scpi; |
383 | struct dev_context *devc; | |
384 | const char *command; | |
385 | ||
386 | scpi = sdi->conn; | |
387 | devc = sdi->priv; | |
7a396ff5 | 388 | |
3cdad416 GS |
389 | command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_STOP_ACQ); |
390 | if (command && *command) { | |
3cdad416 | 391 | scpi_dmm_cmd_delay(scpi); |
28877994 | 392 | (void)sr_scpi_send(scpi, command); |
3cdad416 GS |
393 | } |
394 | sr_scpi_source_remove(sdi->session, scpi); | |
395 | ||
396 | std_session_send_df_end(sdi); | |
7a396ff5 GS |
397 | |
398 | return SR_OK; | |
399 | } | |
400 | ||
0617bb5a | 401 | static struct sr_dev_driver scpi_dmm_driver_info = { |
7a396ff5 GS |
402 | .name = "scpi-dmm", |
403 | .longname = "SCPI DMM", | |
404 | .api_version = 1, | |
405 | .init = std_init, | |
406 | .cleanup = std_cleanup, | |
407 | .scan = scan, | |
408 | .dev_list = std_dev_list, | |
409 | .dev_clear = std_dev_clear, | |
410 | .config_get = config_get, | |
411 | .config_set = config_set, | |
412 | .config_list = config_list, | |
413 | .dev_open = dev_open, | |
414 | .dev_close = dev_close, | |
415 | .dev_acquisition_start = dev_acquisition_start, | |
416 | .dev_acquisition_stop = dev_acquisition_stop, | |
417 | .context = NULL, | |
418 | }; | |
7a396ff5 | 419 | SR_REGISTER_DEV_DRIVER(scpi_dmm_driver_info); |