]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2014 Bert Vermeulen <bert@biot.com> | |
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 <config.h> | |
21 | #include <string.h> | |
22 | #include <strings.h> | |
23 | #include "scpi.h" | |
24 | #include "protocol.h" | |
25 | ||
26 | static struct sr_dev_driver scpi_pps_driver_info; | |
27 | static struct sr_dev_driver hp_ib_pps_driver_info; | |
28 | ||
29 | static const uint32_t scanopts[] = { | |
30 | SR_CONF_CONN, | |
31 | SR_CONF_SERIALCOMM, | |
32 | }; | |
33 | ||
34 | static const uint32_t drvopts[] = { | |
35 | SR_CONF_POWER_SUPPLY, | |
36 | }; | |
37 | ||
38 | static const struct pps_channel_instance pci[] = { | |
39 | { SR_MQ_VOLTAGE, SCPI_CMD_GET_MEAS_VOLTAGE, "V" }, | |
40 | { SR_MQ_CURRENT, SCPI_CMD_GET_MEAS_CURRENT, "I" }, | |
41 | { SR_MQ_POWER, SCPI_CMD_GET_MEAS_POWER, "P" }, | |
42 | { SR_MQ_FREQUENCY, SCPI_CMD_GET_MEAS_FREQUENCY, "F" }, | |
43 | }; | |
44 | ||
45 | static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi, | |
46 | int (*get_hw_id)(struct sr_scpi_dev_inst *scpi, | |
47 | struct sr_scpi_hw_info **scpi_response)) | |
48 | { | |
49 | struct dev_context *devc; | |
50 | struct sr_dev_inst *sdi; | |
51 | struct sr_scpi_hw_info *hw_info; | |
52 | struct sr_channel_group *cg; | |
53 | struct sr_channel *ch; | |
54 | const struct scpi_pps *device; | |
55 | struct pps_channel *pch; | |
56 | struct channel_spec *channels; | |
57 | struct channel_group_spec *channel_groups, *cgs; | |
58 | struct pps_channel_group *pcg; | |
59 | GRegex *model_re; | |
60 | GMatchInfo *model_mi; | |
61 | GSList *l; | |
62 | uint64_t mask; | |
63 | unsigned int num_channels, num_channel_groups, ch_num, ch_idx, i, j; | |
64 | int ret; | |
65 | const char *vendor; | |
66 | char ch_name[16]; | |
67 | ||
68 | if (get_hw_id(scpi, &hw_info) != SR_OK) { | |
69 | sr_info("Couldn't get IDN response."); | |
70 | return NULL; | |
71 | } | |
72 | ||
73 | device = NULL; | |
74 | for (i = 0; i < num_pps_profiles; i++) { | |
75 | vendor = sr_vendor_alias(hw_info->manufacturer); | |
76 | if (g_ascii_strcasecmp(vendor, pps_profiles[i].vendor)) | |
77 | continue; | |
78 | model_re = g_regex_new(pps_profiles[i].model, 0, 0, NULL); | |
79 | if (g_regex_match(model_re, hw_info->model, 0, &model_mi)) | |
80 | device = &pps_profiles[i]; | |
81 | g_match_info_unref(model_mi); | |
82 | g_regex_unref(model_re); | |
83 | if (device) | |
84 | break; | |
85 | } | |
86 | if (!device) { | |
87 | sr_scpi_hw_info_free(hw_info); | |
88 | return NULL; | |
89 | } | |
90 | ||
91 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
92 | sdi->vendor = g_strdup(vendor); | |
93 | sdi->model = g_strdup(hw_info->model); | |
94 | sdi->version = g_strdup(hw_info->firmware_version); | |
95 | sdi->conn = scpi; | |
96 | sdi->driver = &scpi_pps_driver_info; | |
97 | sdi->inst_type = SR_INST_SCPI; | |
98 | sdi->serial_num = g_strdup(hw_info->serial_number); | |
99 | ||
100 | devc = g_malloc0(sizeof(struct dev_context)); | |
101 | devc->device = device; | |
102 | sdi->priv = devc; | |
103 | ||
104 | if (device->num_channels) { | |
105 | /* Static channels and groups. */ | |
106 | channels = (struct channel_spec *)device->channels; | |
107 | num_channels = device->num_channels; | |
108 | channel_groups = (struct channel_group_spec *)device->channel_groups; | |
109 | num_channel_groups = device->num_channel_groups; | |
110 | } else { | |
111 | /* Channels and groups need to be probed. */ | |
112 | ret = device->probe_channels(sdi, hw_info, &channels, &num_channels, | |
113 | &channel_groups, &num_channel_groups); | |
114 | if (ret != SR_OK) { | |
115 | sr_err("Failed to probe for channels."); | |
116 | return NULL; | |
117 | } | |
118 | /* | |
119 | * Since these were dynamically allocated, we'll need to free them | |
120 | * later. | |
121 | */ | |
122 | devc->channels = channels; | |
123 | devc->channel_groups = channel_groups; | |
124 | } | |
125 | ||
126 | ch_idx = 0; | |
127 | for (ch_num = 0; ch_num < num_channels; ch_num++) { | |
128 | /* Create one channel per measurable output unit. */ | |
129 | for (i = 0; i < ARRAY_SIZE(pci); i++) { | |
130 | if (!scpi_cmd_get(devc->device->commands, pci[i].command)) | |
131 | continue; | |
132 | g_snprintf(ch_name, 16, "%s%s", pci[i].prefix, | |
133 | channels[ch_num].name); | |
134 | ch = sr_channel_new(sdi, ch_idx++, SR_CHANNEL_ANALOG, TRUE, | |
135 | ch_name); | |
136 | pch = g_malloc0(sizeof(struct pps_channel)); | |
137 | pch->hw_output_idx = ch_num; | |
138 | pch->hwname = channels[ch_num].name; | |
139 | pch->mq = pci[i].mq; | |
140 | ch->priv = pch; | |
141 | } | |
142 | } | |
143 | ||
144 | for (i = 0; i < num_channel_groups; i++) { | |
145 | cgs = &channel_groups[i]; | |
146 | cg = g_malloc0(sizeof(struct sr_channel_group)); | |
147 | cg->name = g_strdup(cgs->name); | |
148 | for (j = 0, mask = 1; j < 64; j++, mask <<= 1) { | |
149 | if (cgs->channel_index_mask & mask) { | |
150 | for (l = sdi->channels; l; l = l->next) { | |
151 | ch = l->data; | |
152 | pch = ch->priv; | |
153 | if (pch->hw_output_idx == j) | |
154 | cg->channels = g_slist_append(cg->channels, ch); | |
155 | } | |
156 | } | |
157 | } | |
158 | pcg = g_malloc0(sizeof(struct pps_channel_group)); | |
159 | pcg->features = cgs->features; | |
160 | cg->priv = pcg; | |
161 | sdi->channel_groups = g_slist_append(sdi->channel_groups, cg); | |
162 | } | |
163 | ||
164 | sr_scpi_hw_info_free(hw_info); | |
165 | hw_info = NULL; | |
166 | ||
167 | scpi_cmd(sdi, devc->device->commands, SCPI_CMD_LOCAL); | |
168 | ||
169 | return sdi; | |
170 | } | |
171 | ||
172 | static gchar *hpib_get_revision(struct sr_scpi_dev_inst *scpi) | |
173 | { | |
174 | int ret; | |
175 | gboolean matches; | |
176 | char *response; | |
177 | GRegex *version_regex; | |
178 | ||
179 | ret = sr_scpi_get_string(scpi, "ROM?", &response); | |
180 | if (ret != SR_OK && !response) | |
181 | return NULL; | |
182 | ||
183 | /* Example version string: "B01 B01" */ | |
184 | version_regex = g_regex_new("[A-Z][0-9]{2} [A-Z][0-9]{2}", 0, 0, NULL); | |
185 | matches = g_regex_match(version_regex, response, 0, NULL); | |
186 | g_regex_unref(version_regex); | |
187 | ||
188 | if (!matches) { | |
189 | /* Not a valid version string. Ignore it. */ | |
190 | g_free(response); | |
191 | response = NULL; | |
192 | } else { | |
193 | /* Replace space with dot. */ | |
194 | response[3] = '.'; | |
195 | } | |
196 | ||
197 | return response; | |
198 | } | |
199 | ||
200 | /* | |
201 | * This function assumes the response is in the form "HP<model_number>" | |
202 | * | |
203 | * HP made many GPIB (then called HP-IB) instruments before the SCPI command | |
204 | * set was introduced into the standard. We haven't seen any non-HP instruments | |
205 | * which respond to the "ID?" query, so assume all are HP for now. | |
206 | */ | |
207 | static int hpib_get_hw_id(struct sr_scpi_dev_inst *scpi, | |
208 | struct sr_scpi_hw_info **scpi_response) | |
209 | { | |
210 | int ret; | |
211 | char *response; | |
212 | struct sr_scpi_hw_info *hw_info; | |
213 | ||
214 | ret = sr_scpi_get_string(scpi, "ID?", &response); | |
215 | if ((ret != SR_OK) || !response) | |
216 | return SR_ERR; | |
217 | ||
218 | hw_info = g_malloc0(sizeof(struct sr_scpi_hw_info)); | |
219 | ||
220 | *scpi_response = hw_info; | |
221 | hw_info->model = response; | |
222 | hw_info->firmware_version = hpib_get_revision(scpi); | |
223 | hw_info->manufacturer = g_strdup("HP"); | |
224 | ||
225 | return SR_OK; | |
226 | } | |
227 | ||
228 | static struct sr_dev_inst *probe_scpi_pps_device(struct sr_scpi_dev_inst *scpi) | |
229 | { | |
230 | return probe_device(scpi, sr_scpi_get_hw_id); | |
231 | } | |
232 | ||
233 | static struct sr_dev_inst *probe_hpib_pps_device(struct sr_scpi_dev_inst *scpi) | |
234 | { | |
235 | return probe_device(scpi, hpib_get_hw_id); | |
236 | } | |
237 | ||
238 | static GSList *scan_scpi_pps(struct sr_dev_driver *di, GSList *options) | |
239 | { | |
240 | return sr_scpi_scan(di->context, options, probe_scpi_pps_device); | |
241 | } | |
242 | ||
243 | static GSList *scan_hpib_pps(struct sr_dev_driver *di, GSList *options) | |
244 | { | |
245 | return sr_scpi_scan(di->context, options, probe_hpib_pps_device); | |
246 | } | |
247 | ||
248 | static int dev_open(struct sr_dev_inst *sdi) | |
249 | { | |
250 | struct dev_context *devc; | |
251 | struct sr_scpi_dev_inst *scpi; | |
252 | GVariant *beeper; | |
253 | ||
254 | scpi = sdi->conn; | |
255 | if (sr_scpi_open(scpi) < 0) | |
256 | return SR_ERR; | |
257 | ||
258 | sdi->status = SR_ST_ACTIVE; | |
259 | ||
260 | devc = sdi->priv; | |
261 | scpi_cmd(sdi, devc->device->commands, SCPI_CMD_REMOTE); | |
262 | devc->beeper_was_set = FALSE; | |
263 | if (scpi_cmd_resp(sdi, devc->device->commands, &beeper, | |
264 | G_VARIANT_TYPE_BOOLEAN, SCPI_CMD_BEEPER) == SR_OK) { | |
265 | if (g_variant_get_boolean(beeper)) { | |
266 | devc->beeper_was_set = TRUE; | |
267 | scpi_cmd(sdi, devc->device->commands, SCPI_CMD_BEEPER_DISABLE); | |
268 | } | |
269 | g_variant_unref(beeper); | |
270 | } | |
271 | ||
272 | return SR_OK; | |
273 | } | |
274 | ||
275 | static int dev_close(struct sr_dev_inst *sdi) | |
276 | { | |
277 | struct sr_scpi_dev_inst *scpi; | |
278 | struct dev_context *devc; | |
279 | ||
280 | devc = sdi->priv; | |
281 | scpi = sdi->conn; | |
282 | if (scpi) { | |
283 | if (devc->beeper_was_set) | |
284 | scpi_cmd(sdi, devc->device->commands, SCPI_CMD_BEEPER_ENABLE); | |
285 | scpi_cmd(sdi, devc->device->commands, SCPI_CMD_LOCAL); | |
286 | sr_scpi_close(scpi); | |
287 | sdi->status = SR_ST_INACTIVE; | |
288 | } | |
289 | ||
290 | return SR_OK; | |
291 | } | |
292 | ||
293 | static void clear_helper(void *priv) | |
294 | { | |
295 | struct dev_context *devc; | |
296 | ||
297 | devc = priv; | |
298 | g_free(devc->channels); | |
299 | g_free(devc->channel_groups); | |
300 | g_free(devc); | |
301 | } | |
302 | ||
303 | static int dev_clear(const struct sr_dev_driver *di) | |
304 | { | |
305 | return std_dev_clear(di, clear_helper); | |
306 | } | |
307 | ||
308 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
309 | const struct sr_channel_group *cg) | |
310 | { | |
311 | struct dev_context *devc; | |
312 | const GVariantType *gvtype; | |
313 | unsigned int i; | |
314 | int cmd, ret; | |
315 | const char *s; | |
316 | ||
317 | if (!sdi) | |
318 | return SR_ERR_ARG; | |
319 | ||
320 | devc = sdi->priv; | |
321 | ||
322 | if (cg) { | |
323 | /* | |
324 | * These options only apply to channel groups with a single | |
325 | * channel -- they're per-channel settings for the device. | |
326 | */ | |
327 | ||
328 | /* | |
329 | * Config keys are handled below depending on whether a channel | |
330 | * group was provided by the frontend. However some of these | |
331 | * take a CG on one PPS but not on others. Check the device's | |
332 | * profile for that here, and NULL out the channel group as needed. | |
333 | */ | |
334 | for (i = 0; i < devc->device->num_devopts; i++) { | |
335 | if (devc->device->devopts[i] == key) { | |
336 | cg = NULL; | |
337 | break; | |
338 | } | |
339 | } | |
340 | } | |
341 | ||
342 | gvtype = NULL; | |
343 | cmd = -1; | |
344 | switch (key) { | |
345 | case SR_CONF_ENABLED: | |
346 | gvtype = G_VARIANT_TYPE_BOOLEAN; | |
347 | cmd = SCPI_CMD_GET_OUTPUT_ENABLED; | |
348 | break; | |
349 | case SR_CONF_VOLTAGE: | |
350 | gvtype = G_VARIANT_TYPE_DOUBLE; | |
351 | cmd = SCPI_CMD_GET_MEAS_VOLTAGE; | |
352 | break; | |
353 | case SR_CONF_VOLTAGE_TARGET: | |
354 | gvtype = G_VARIANT_TYPE_DOUBLE; | |
355 | cmd = SCPI_CMD_GET_VOLTAGE_TARGET; | |
356 | break; | |
357 | case SR_CONF_OUTPUT_FREQUENCY: | |
358 | gvtype = G_VARIANT_TYPE_DOUBLE; | |
359 | cmd = SCPI_CMD_GET_MEAS_FREQUENCY; | |
360 | break; | |
361 | case SR_CONF_OUTPUT_FREQUENCY_TARGET: | |
362 | gvtype = G_VARIANT_TYPE_DOUBLE; | |
363 | cmd = SCPI_CMD_GET_FREQUENCY_TARGET; | |
364 | break; | |
365 | case SR_CONF_CURRENT: | |
366 | gvtype = G_VARIANT_TYPE_DOUBLE; | |
367 | cmd = SCPI_CMD_GET_MEAS_CURRENT; | |
368 | break; | |
369 | case SR_CONF_CURRENT_LIMIT: | |
370 | gvtype = G_VARIANT_TYPE_DOUBLE; | |
371 | cmd = SCPI_CMD_GET_CURRENT_LIMIT; | |
372 | break; | |
373 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: | |
374 | gvtype = G_VARIANT_TYPE_BOOLEAN; | |
375 | cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ENABLED; | |
376 | break; | |
377 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE: | |
378 | gvtype = G_VARIANT_TYPE_BOOLEAN; | |
379 | cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ACTIVE; | |
380 | break; | |
381 | case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD: | |
382 | gvtype = G_VARIANT_TYPE_DOUBLE; | |
383 | cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_THRESHOLD; | |
384 | break; | |
385 | case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: | |
386 | gvtype = G_VARIANT_TYPE_BOOLEAN; | |
387 | cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ENABLED; | |
388 | break; | |
389 | case SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE: | |
390 | gvtype = G_VARIANT_TYPE_BOOLEAN; | |
391 | cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ACTIVE; | |
392 | break; | |
393 | case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD: | |
394 | gvtype = G_VARIANT_TYPE_DOUBLE; | |
395 | cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_THRESHOLD; | |
396 | break; | |
397 | case SR_CONF_OVER_TEMPERATURE_PROTECTION: | |
398 | gvtype = G_VARIANT_TYPE_BOOLEAN; | |
399 | cmd = SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION; | |
400 | break; | |
401 | case SR_CONF_REGULATION: | |
402 | gvtype = G_VARIANT_TYPE_STRING; | |
403 | cmd = SCPI_CMD_GET_OUTPUT_REGULATION; | |
404 | } | |
405 | if (!gvtype) | |
406 | return SR_ERR_NA; | |
407 | ||
408 | if (cg) | |
409 | select_channel(sdi, cg->channels->data); | |
410 | ret = scpi_cmd_resp(sdi, devc->device->commands, data, gvtype, cmd); | |
411 | ||
412 | if (cmd == SCPI_CMD_GET_OUTPUT_REGULATION) { | |
413 | /* | |
414 | * The Rigol DP800 series return CV/CC/UR, Philips PM2800 | |
415 | * return VOLT/CURR. We always return a GVariant string in | |
416 | * the Rigol notation. | |
417 | */ | |
418 | s = g_variant_get_string(*data, NULL); | |
419 | if (!strcmp(s, "VOLT")) { | |
420 | g_variant_unref(*data); | |
421 | *data = g_variant_new_string("CV"); | |
422 | } else if (!strcmp(s, "CURR")) { | |
423 | g_variant_unref(*data); | |
424 | *data = g_variant_new_string("CC"); | |
425 | } | |
426 | ||
427 | s = g_variant_get_string(*data, NULL); | |
428 | if (strcmp(s, "CV") && strcmp(s, "CC") && strcmp(s, "UR")) { | |
429 | sr_dbg("Unknown response to SCPI_CMD_GET_OUTPUT_REGULATION: %s", s); | |
430 | ret = SR_ERR_DATA; | |
431 | } | |
432 | } | |
433 | ||
434 | return ret; | |
435 | } | |
436 | ||
437 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, | |
438 | const struct sr_channel_group *cg) | |
439 | { | |
440 | struct dev_context *devc; | |
441 | double d; | |
442 | int ret; | |
443 | ||
444 | if (!sdi) | |
445 | return SR_ERR_ARG; | |
446 | ||
447 | if (cg) | |
448 | /* Channel group specified. */ | |
449 | select_channel(sdi, cg->channels->data); | |
450 | ||
451 | devc = sdi->priv; | |
452 | ||
453 | switch (key) { | |
454 | case SR_CONF_ENABLED: | |
455 | if (g_variant_get_boolean(data)) | |
456 | ret = scpi_cmd(sdi, devc->device->commands, | |
457 | SCPI_CMD_SET_OUTPUT_ENABLE); | |
458 | else | |
459 | ret = scpi_cmd(sdi, devc->device->commands, | |
460 | SCPI_CMD_SET_OUTPUT_DISABLE); | |
461 | break; | |
462 | case SR_CONF_VOLTAGE_TARGET: | |
463 | d = g_variant_get_double(data); | |
464 | ret = scpi_cmd(sdi, devc->device->commands, | |
465 | SCPI_CMD_SET_VOLTAGE_TARGET, d); | |
466 | break; | |
467 | case SR_CONF_OUTPUT_FREQUENCY_TARGET: | |
468 | d = g_variant_get_double(data); | |
469 | ret = scpi_cmd(sdi, devc->device->commands, | |
470 | SCPI_CMD_SET_FREQUENCY_TARGET, d); | |
471 | break; | |
472 | case SR_CONF_CURRENT_LIMIT: | |
473 | d = g_variant_get_double(data); | |
474 | ret = scpi_cmd(sdi, devc->device->commands, | |
475 | SCPI_CMD_SET_CURRENT_LIMIT, d); | |
476 | break; | |
477 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: | |
478 | if (g_variant_get_boolean(data)) | |
479 | ret = scpi_cmd(sdi, devc->device->commands, | |
480 | SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_ENABLE); | |
481 | else | |
482 | ret = scpi_cmd(sdi, devc->device->commands, | |
483 | SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_DISABLE); | |
484 | break; | |
485 | case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD: | |
486 | d = g_variant_get_double(data); | |
487 | ret = scpi_cmd(sdi, devc->device->commands, | |
488 | SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_THRESHOLD, d); | |
489 | break; | |
490 | case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: | |
491 | if (g_variant_get_boolean(data)) | |
492 | ret = scpi_cmd(sdi, devc->device->commands, | |
493 | SCPI_CMD_SET_OVER_CURRENT_PROTECTION_ENABLE); | |
494 | else | |
495 | ret = scpi_cmd(sdi, devc->device->commands, | |
496 | SCPI_CMD_SET_OVER_CURRENT_PROTECTION_DISABLE); | |
497 | break; | |
498 | case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD: | |
499 | d = g_variant_get_double(data); | |
500 | ret = scpi_cmd(sdi, devc->device->commands, | |
501 | SCPI_CMD_SET_OVER_CURRENT_PROTECTION_THRESHOLD, d); | |
502 | break; | |
503 | case SR_CONF_OVER_TEMPERATURE_PROTECTION: | |
504 | if (g_variant_get_boolean(data)) | |
505 | ret = scpi_cmd(sdi, devc->device->commands, | |
506 | SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_ENABLE); | |
507 | else | |
508 | ret = scpi_cmd(sdi, devc->device->commands, | |
509 | SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_DISABLE); | |
510 | break; | |
511 | default: | |
512 | ret = SR_ERR_NA; | |
513 | } | |
514 | ||
515 | return ret; | |
516 | } | |
517 | ||
518 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
519 | const struct sr_channel_group *cg) | |
520 | { | |
521 | struct dev_context *devc; | |
522 | struct sr_channel *ch; | |
523 | const struct channel_spec *ch_spec; | |
524 | GVariant *gvar; | |
525 | GVariantBuilder gvb; | |
526 | int ret, i; | |
527 | const char *s[16]; | |
528 | ||
529 | /* Always available, even without sdi. */ | |
530 | if (key == SR_CONF_SCAN_OPTIONS) { | |
531 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
532 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); | |
533 | return SR_OK; | |
534 | } else if (key == SR_CONF_DEVICE_OPTIONS && !sdi) { | |
535 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
536 | drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t)); | |
537 | return SR_OK; | |
538 | } | |
539 | ||
540 | if (!sdi) | |
541 | return SR_ERR_ARG; | |
542 | devc = sdi->priv; | |
543 | ||
544 | ret = SR_OK; | |
545 | if (!cg) { | |
546 | /* No channel group: global options. */ | |
547 | switch (key) { | |
548 | case SR_CONF_DEVICE_OPTIONS: | |
549 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
550 | devc->device->devopts, devc->device->num_devopts, | |
551 | sizeof(uint32_t)); | |
552 | break; | |
553 | case SR_CONF_CHANNEL_CONFIG: | |
554 | /* Not used. */ | |
555 | i = 0; | |
556 | if (devc->device->features & PPS_INDEPENDENT) | |
557 | s[i++] = "Independent"; | |
558 | if (devc->device->features & PPS_SERIES) | |
559 | s[i++] = "Series"; | |
560 | if (devc->device->features & PPS_PARALLEL) | |
561 | s[i++] = "Parallel"; | |
562 | if (i == 0) { | |
563 | /* | |
564 | * Shouldn't happen: independent-only devices | |
565 | * shouldn't advertise this option at all. | |
566 | */ | |
567 | return SR_ERR_NA; | |
568 | } | |
569 | *data = g_variant_new_strv(s, i); | |
570 | break; | |
571 | default: | |
572 | return SR_ERR_NA; | |
573 | } | |
574 | } else { | |
575 | /* Channel group specified. */ | |
576 | /* | |
577 | * Per-channel-group options depending on a channel are actually | |
578 | * done with the first channel. Channel groups in PPS can have | |
579 | * more than one channel, but they will typically be of equal | |
580 | * specification for use in series or parallel mode. | |
581 | */ | |
582 | ch = cg->channels->data; | |
583 | ||
584 | switch (key) { | |
585 | case SR_CONF_DEVICE_OPTIONS: | |
586 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
587 | devc->device->devopts_cg, devc->device->num_devopts_cg, | |
588 | sizeof(uint32_t)); | |
589 | break; | |
590 | case SR_CONF_VOLTAGE_TARGET: | |
591 | ch_spec = &(devc->device->channels[ch->index]); | |
592 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
593 | /* Min, max, write resolution. */ | |
594 | for (i = 0; i < 3; i++) { | |
595 | gvar = g_variant_new_double(ch_spec->voltage[i]); | |
596 | g_variant_builder_add_value(&gvb, gvar); | |
597 | } | |
598 | *data = g_variant_builder_end(&gvb); | |
599 | break; | |
600 | case SR_CONF_OUTPUT_FREQUENCY_TARGET: | |
601 | ch_spec = &(devc->device->channels[ch->index]); | |
602 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
603 | /* Min, max, write resolution. */ | |
604 | for (i = 0; i < 3; i++) { | |
605 | gvar = g_variant_new_double(ch_spec->frequency[i]); | |
606 | g_variant_builder_add_value(&gvb, gvar); | |
607 | } | |
608 | *data = g_variant_builder_end(&gvb); | |
609 | break; | |
610 | case SR_CONF_CURRENT_LIMIT: | |
611 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
612 | /* Min, max, step. */ | |
613 | for (i = 0; i < 3; i++) { | |
614 | ch_spec = &(devc->device->channels[ch->index]); | |
615 | gvar = g_variant_new_double(ch_spec->current[i]); | |
616 | g_variant_builder_add_value(&gvb, gvar); | |
617 | } | |
618 | *data = g_variant_builder_end(&gvb); | |
619 | break; | |
620 | default: | |
621 | return SR_ERR_NA; | |
622 | } | |
623 | } | |
624 | ||
625 | return ret; | |
626 | } | |
627 | ||
628 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
629 | { | |
630 | struct dev_context *devc; | |
631 | struct sr_scpi_dev_inst *scpi; | |
632 | struct sr_channel *ch; | |
633 | struct pps_channel *pch; | |
634 | int cmd, ret; | |
635 | ||
636 | devc = sdi->priv; | |
637 | scpi = sdi->conn; | |
638 | ||
639 | if ((ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 10, | |
640 | scpi_pps_receive_data, (void *)sdi)) != SR_OK) | |
641 | return ret; | |
642 | std_session_send_df_header(sdi); | |
643 | ||
644 | /* Prime the pipe with the first channel's fetch. */ | |
645 | ch = sr_next_enabled_channel(sdi, NULL); | |
646 | pch = ch->priv; | |
647 | if ((ret = select_channel(sdi, ch)) < 0) | |
648 | return ret; | |
649 | if (pch->mq == SR_MQ_VOLTAGE) | |
650 | cmd = SCPI_CMD_GET_MEAS_VOLTAGE; | |
651 | else if (pch->mq == SR_MQ_FREQUENCY) | |
652 | cmd = SCPI_CMD_GET_MEAS_FREQUENCY; | |
653 | else if (pch->mq == SR_MQ_CURRENT) | |
654 | cmd = SCPI_CMD_GET_MEAS_CURRENT; | |
655 | else if (pch->mq == SR_MQ_POWER) | |
656 | cmd = SCPI_CMD_GET_MEAS_POWER; | |
657 | else | |
658 | return SR_ERR; | |
659 | scpi_cmd(sdi, devc->device->commands, cmd, pch->hwname); | |
660 | ||
661 | return SR_OK; | |
662 | } | |
663 | ||
664 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
665 | { | |
666 | struct sr_scpi_dev_inst *scpi; | |
667 | float f; | |
668 | ||
669 | scpi = sdi->conn; | |
670 | ||
671 | /* | |
672 | * A requested value is certainly on the way. Retrieve it now, | |
673 | * to avoid leaving the device in a state where it's not expecting | |
674 | * commands. | |
675 | */ | |
676 | sr_scpi_get_float(scpi, NULL, &f); | |
677 | sr_scpi_source_remove(sdi->session, scpi); | |
678 | ||
679 | std_session_send_df_end(sdi); | |
680 | ||
681 | return SR_OK; | |
682 | } | |
683 | ||
684 | static struct sr_dev_driver scpi_pps_driver_info = { | |
685 | .name = "scpi-pps", | |
686 | .longname = "SCPI PPS", | |
687 | .api_version = 1, | |
688 | .init = std_init, | |
689 | .cleanup = std_cleanup, | |
690 | .scan = scan_scpi_pps, | |
691 | .dev_list = std_dev_list, | |
692 | .dev_clear = dev_clear, | |
693 | .config_get = config_get, | |
694 | .config_set = config_set, | |
695 | .config_list = config_list, | |
696 | .dev_open = dev_open, | |
697 | .dev_close = dev_close, | |
698 | .dev_acquisition_start = dev_acquisition_start, | |
699 | .dev_acquisition_stop = dev_acquisition_stop, | |
700 | .context = NULL, | |
701 | }; | |
702 | ||
703 | static struct sr_dev_driver hp_ib_pps_driver_info = { | |
704 | .name = "hpib-pps", | |
705 | .longname = "HP-IB PPS", | |
706 | .api_version = 1, | |
707 | .init = std_init, | |
708 | .cleanup = std_cleanup, | |
709 | .scan = scan_hpib_pps, | |
710 | .dev_list = std_dev_list, | |
711 | .dev_clear = dev_clear, | |
712 | .config_get = config_get, | |
713 | .config_set = config_set, | |
714 | .config_list = config_list, | |
715 | .dev_open = dev_open, | |
716 | .dev_close = dev_close, | |
717 | .dev_acquisition_start = dev_acquisition_start, | |
718 | .dev_acquisition_stop = dev_acquisition_stop, | |
719 | .context = NULL, | |
720 | }; | |
721 | SR_REGISTER_DEV_DRIVER(scpi_pps_driver_info); | |
722 | SR_REGISTER_DEV_DRIVER(hp_ib_pps_driver_info); |