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