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