]>
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_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 | ||
52 | static const struct mqopt_item mqopts_agilent_34405a[] = { | |
53 | { SR_MQ_VOLTAGE, SR_MQFLAG_DC, "VOLT:DC", "VOLT ", NO_DFLT_PREC, }, | |
54 | { SR_MQ_VOLTAGE, SR_MQFLAG_AC, "VOLT:AC", "VOLT:AC ", NO_DFLT_PREC, }, | |
55 | { SR_MQ_CURRENT, SR_MQFLAG_DC, "CURR:DC", "CURR ", NO_DFLT_PREC, }, | |
56 | { SR_MQ_CURRENT, SR_MQFLAG_AC, "CURR:AC", "CURR:AC ", NO_DFLT_PREC, }, | |
57 | { SR_MQ_RESISTANCE, 0, "RES", "RES ", NO_DFLT_PREC, }, | |
58 | { SR_MQ_CONTINUITY, 0, "CONT", "CONT", -1, }, | |
59 | { SR_MQ_CAPACITANCE, 0, "CAP", "CAP ", NO_DFLT_PREC, }, | |
60 | { SR_MQ_VOLTAGE, SR_MQFLAG_DC | SR_MQFLAG_DIODE, "DIOD", "DIOD", -4, }, | |
61 | { SR_MQ_TEMPERATURE, 0, "TEMP", "TEMP ", NO_DFLT_PREC, }, | |
62 | { SR_MQ_FREQUENCY, 0, "FREQ", "FREQ ", NO_DFLT_PREC, }, | |
63 | }; | |
64 | ||
65 | SR_PRIV const struct scpi_dmm_model models[] = { | |
66 | { | |
67 | "Agilent", "34405A", | |
68 | 1, 5, cmdset_agilent, ARRAY_AND_SIZE(mqopts_agilent_34405a), | |
69 | scpi_dmm_get_meas_agilent, | |
70 | ARRAY_AND_SIZE(devopts_generic), | |
71 | }, | |
72 | }; | |
73 | ||
74 | static const struct scpi_dmm_model *is_compatible(const char *vendor, const char *model) | |
75 | { | |
76 | size_t i; | |
77 | const struct scpi_dmm_model *entry; | |
78 | ||
79 | for (i = 0; i < ARRAY_SIZE(models); i++) { | |
80 | entry = &models[i]; | |
81 | if (!entry->vendor || !entry->model) | |
82 | continue; | |
83 | if (strcmp(vendor, entry->vendor) != 0) | |
84 | continue; | |
85 | if (strcmp(model, entry->model) != 0) | |
86 | continue; | |
87 | return entry; | |
88 | } | |
89 | ||
90 | return NULL; | |
91 | } | |
92 | ||
93 | static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi) | |
94 | { | |
95 | struct sr_scpi_hw_info *hw_info; | |
96 | int ret; | |
97 | const char *vendor; | |
98 | const struct scpi_dmm_model *model; | |
99 | struct sr_dev_inst *sdi; | |
100 | struct dev_context *devc; | |
101 | size_t i; | |
102 | gchar *channel_name; | |
103 | ||
104 | scpi_dmm_cmd_delay(scpi); | |
105 | ret = sr_scpi_get_hw_id(scpi, &hw_info); | |
106 | if (ret != SR_OK) { | |
107 | sr_info("Could not get IDN response."); | |
108 | return NULL; | |
109 | } | |
110 | vendor = sr_vendor_alias(hw_info->manufacturer); | |
111 | model = is_compatible(vendor, hw_info->model); | |
112 | if (!model) { | |
113 | sr_scpi_hw_info_free(hw_info); | |
114 | return NULL; | |
115 | } | |
116 | ||
117 | sdi = g_malloc0(sizeof(*sdi)); | |
118 | sdi->vendor = g_strdup(hw_info->manufacturer); | |
119 | sdi->model = g_strdup(hw_info->model); | |
120 | sdi->version = g_strdup(hw_info->firmware_version); | |
121 | sdi->serial_num = g_strdup(hw_info->serial_number); | |
122 | sdi->conn = scpi; | |
123 | sdi->driver = &scpi_dmm_driver_info; | |
124 | sdi->inst_type = SR_INST_SCPI; | |
125 | sr_scpi_hw_info_free(hw_info); | |
126 | ||
127 | devc = g_malloc0(sizeof(*devc)); | |
128 | sdi->priv = devc; | |
129 | devc->num_channels = model->num_channels; | |
130 | devc->cmdset = model->cmdset; | |
131 | devc->model = model; | |
132 | ||
133 | for (i = 0; i < devc->num_channels; i++) { | |
134 | channel_name = g_strdup_printf("P%zu", i + 1); | |
135 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, channel_name); | |
136 | } | |
137 | ||
138 | return sdi; | |
139 | } | |
140 | ||
141 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
142 | { | |
143 | return sr_scpi_scan(di->context, options, probe_device); | |
144 | } | |
145 | ||
146 | static int dev_open(struct sr_dev_inst *sdi) | |
147 | { | |
148 | struct sr_scpi_dev_inst *scpi; | |
149 | int ret; | |
150 | ||
151 | scpi = sdi->conn; | |
152 | ret = sr_scpi_open(scpi); | |
153 | if (ret < 0) { | |
154 | sr_err("Failed to open SCPI device: %s.", sr_strerror(ret)); | |
155 | return SR_ERR; | |
156 | } | |
157 | ||
158 | return SR_OK; | |
159 | } | |
160 | ||
161 | static int dev_close(struct sr_dev_inst *sdi) | |
162 | { | |
163 | struct sr_scpi_dev_inst *scpi; | |
164 | ||
165 | scpi = sdi->conn; | |
166 | if (!scpi) | |
167 | return SR_ERR_BUG; | |
168 | ||
169 | sr_dbg("DIAG: sdi->status %d.", sdi->status - SR_ST_NOT_FOUND); | |
170 | if (sdi->status <= SR_ST_INACTIVE) | |
171 | return SR_OK; | |
172 | ||
173 | return sr_scpi_close(scpi); | |
174 | } | |
175 | ||
176 | static int config_get(uint32_t key, GVariant **data, | |
177 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
178 | { | |
179 | struct dev_context *devc; | |
180 | enum sr_mq mq; | |
181 | enum sr_mqflag mqflag; | |
182 | GVariant *arr[2]; | |
183 | int ret; | |
184 | ||
185 | (void)cg; | |
186 | ||
187 | devc = sdi->priv; | |
188 | ||
189 | switch (key) { | |
190 | case SR_CONF_LIMIT_SAMPLES: | |
191 | case SR_CONF_LIMIT_MSEC: | |
192 | return sr_sw_limits_config_get(&devc->limits, key, data); | |
193 | case SR_CONF_MEASURED_QUANTITY: | |
194 | ret = scpi_dmm_get_mq(sdi, &mq, &mqflag, NULL, NULL); | |
195 | if (ret != SR_OK) | |
196 | return ret; | |
197 | arr[0] = g_variant_new_uint32(mq); | |
198 | arr[1] = g_variant_new_uint64(mqflag); | |
199 | *data = g_variant_new_tuple(arr, ARRAY_SIZE(arr)); | |
200 | return SR_OK; | |
201 | default: | |
202 | return SR_ERR_NA; | |
203 | } | |
204 | } | |
205 | ||
206 | static int config_set(uint32_t key, GVariant *data, | |
207 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
208 | { | |
209 | struct dev_context *devc; | |
210 | enum sr_mq mq; | |
211 | enum sr_mqflag mqflag; | |
212 | GVariant *tuple_child; | |
213 | ||
214 | (void)cg; | |
215 | ||
216 | devc = sdi->priv; | |
217 | ||
218 | switch (key) { | |
219 | case SR_CONF_LIMIT_SAMPLES: | |
220 | case SR_CONF_LIMIT_MSEC: | |
221 | return sr_sw_limits_config_set(&devc->limits, key, data); | |
222 | case SR_CONF_MEASURED_QUANTITY: | |
223 | tuple_child = g_variant_get_child_value(data, 0); | |
224 | mq = g_variant_get_uint32(tuple_child); | |
225 | tuple_child = g_variant_get_child_value(data, 1); | |
226 | mqflag = g_variant_get_uint64(tuple_child); | |
227 | g_variant_unref(tuple_child); | |
228 | return scpi_dmm_set_mq(sdi, mq, mqflag); | |
229 | default: | |
230 | return SR_ERR_NA; | |
231 | } | |
232 | } | |
233 | ||
234 | static int config_list(uint32_t key, GVariant **data, | |
235 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
236 | { | |
237 | struct dev_context *devc; | |
238 | GVariant *gvar, *arr[2]; | |
239 | GVariantBuilder gvb; | |
240 | size_t i; | |
241 | ||
242 | (void)cg; | |
243 | ||
244 | devc = sdi ? sdi->priv : NULL; | |
245 | ||
246 | switch (key) { | |
247 | case SR_CONF_SCAN_OPTIONS: | |
248 | case SR_CONF_DEVICE_OPTIONS: | |
249 | if (!devc) | |
250 | return STD_CONFIG_LIST(key, data, sdi, cg, | |
251 | scanopts, drvopts, devopts_generic); | |
252 | return std_opts_config_list(key, data, sdi, cg, | |
253 | ARRAY_AND_SIZE(scanopts), ARRAY_AND_SIZE(drvopts), | |
254 | devc->model->devopts, devc->model->devopts_size); | |
255 | case SR_CONF_MEASURED_QUANTITY: | |
256 | /* TODO Use std_gvar_measured_quantities() when available. */ | |
257 | if (!devc) | |
258 | return SR_ERR_ARG; | |
259 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
260 | for (i = 0; i < devc->model->mqopt_size; i++) { | |
261 | arr[0] = g_variant_new_uint32(devc->model->mqopts[i].mq); | |
262 | arr[1] = g_variant_new_uint64(devc->model->mqopts[i].mqflag); | |
263 | gvar = g_variant_new_tuple(arr, ARRAY_SIZE(arr)); | |
264 | g_variant_builder_add_value(&gvb, gvar); | |
265 | } | |
266 | *data = g_variant_builder_end(&gvb); | |
267 | return SR_OK; | |
268 | default: | |
269 | (void)devc; | |
270 | return SR_ERR_NA; | |
271 | } | |
272 | } | |
273 | ||
274 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
275 | { | |
276 | struct sr_scpi_dev_inst *scpi; | |
277 | struct dev_context *devc; | |
278 | int ret; | |
279 | const struct mqopt_item *item; | |
280 | const char *command; | |
281 | ||
282 | scpi = sdi->conn; | |
283 | devc = sdi->priv; | |
284 | ||
285 | ret = scpi_dmm_get_mq(sdi, &devc->start_acq_mq.curr_mq, | |
286 | &devc->start_acq_mq.curr_mqflag, NULL, &item); | |
287 | if (ret != SR_OK) | |
288 | return ret; | |
289 | ||
290 | command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_START_ACQ); | |
291 | if (command && *command) { | |
292 | scpi_dmm_cmd_delay(scpi); | |
293 | ret = sr_scpi_send(scpi, command); | |
294 | if (ret != SR_OK) | |
295 | return ret; | |
296 | } | |
297 | ||
298 | sr_sw_limits_acquisition_start(&devc->limits); | |
299 | ret = std_session_send_df_header(sdi); | |
300 | if (ret != SR_OK) | |
301 | return ret; | |
302 | ||
303 | ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 10, | |
304 | scpi_dmm_receive_data, (void *)sdi); | |
305 | if (ret != SR_OK) | |
306 | return ret; | |
307 | ||
308 | return SR_OK; | |
309 | } | |
310 | ||
311 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
312 | { | |
313 | struct sr_scpi_dev_inst *scpi; | |
314 | struct dev_context *devc; | |
315 | const char *command; | |
316 | ||
317 | scpi = sdi->conn; | |
318 | devc = sdi->priv; | |
319 | ||
320 | command = sr_scpi_cmd_get(devc->cmdset, DMM_CMD_STOP_ACQ); | |
321 | if (command && *command) { | |
322 | scpi_dmm_cmd_delay(scpi); | |
323 | (void)sr_scpi_send(scpi, command); | |
324 | } | |
325 | sr_scpi_source_remove(sdi->session, scpi); | |
326 | ||
327 | std_session_send_df_end(sdi); | |
328 | ||
329 | return SR_OK; | |
330 | } | |
331 | ||
332 | static struct sr_dev_driver scpi_dmm_driver_info = { | |
333 | .name = "scpi-dmm", | |
334 | .longname = "SCPI DMM", | |
335 | .api_version = 1, | |
336 | .init = std_init, | |
337 | .cleanup = std_cleanup, | |
338 | .scan = scan, | |
339 | .dev_list = std_dev_list, | |
340 | .dev_clear = std_dev_clear, | |
341 | .config_get = config_get, | |
342 | .config_set = config_set, | |
343 | .config_list = config_list, | |
344 | .dev_open = dev_open, | |
345 | .dev_close = dev_close, | |
346 | .dev_acquisition_start = dev_acquisition_start, | |
347 | .dev_acquisition_stop = dev_acquisition_stop, | |
348 | .context = NULL, | |
349 | }; | |
350 | SR_REGISTER_DEV_DRIVER(scpi_dmm_driver_info); |