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