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