]>
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 | sr_sw_limits_init(&devc->limits); | |
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 (!sr_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 | sr_scpi_cmd(sdi, devc->device->commands, 0, NULL, SCPI_CMD_LOCAL); | |
170 | ||
171 | return sdi; | |
172 | } | |
173 | ||
174 | static gchar *hpib_get_revision(struct sr_scpi_dev_inst *scpi) | |
175 | { | |
176 | int ret; | |
177 | gboolean matches; | |
178 | char *response; | |
179 | GRegex *version_regex; | |
180 | ||
181 | ret = sr_scpi_get_string(scpi, "ROM?", &response); | |
182 | if (ret != SR_OK && !response) | |
183 | return NULL; | |
184 | ||
185 | /* Example version string: "B01 B01" */ | |
186 | version_regex = g_regex_new("[A-Z][0-9]{2} [A-Z][0-9]{2}", 0, 0, NULL); | |
187 | matches = g_regex_match(version_regex, response, 0, NULL); | |
188 | g_regex_unref(version_regex); | |
189 | ||
190 | if (!matches) { | |
191 | /* Not a valid version string. Ignore it. */ | |
192 | g_free(response); | |
193 | response = NULL; | |
194 | } else { | |
195 | /* Replace space with dot. */ | |
196 | response[3] = '.'; | |
197 | } | |
198 | ||
199 | return response; | |
200 | } | |
201 | ||
202 | /* | |
203 | * This function assumes the response is in the form "HP<model_number>" | |
204 | * | |
205 | * HP made many GPIB (then called HP-IB) instruments before the SCPI command | |
206 | * set was introduced into the standard. We haven't seen any non-HP instruments | |
207 | * which respond to the "ID?" query, so assume all are HP for now. | |
208 | */ | |
209 | static int hpib_get_hw_id(struct sr_scpi_dev_inst *scpi, | |
210 | struct sr_scpi_hw_info **scpi_response) | |
211 | { | |
212 | int ret; | |
213 | char *response; | |
214 | struct sr_scpi_hw_info *hw_info; | |
215 | ||
216 | ret = sr_scpi_get_string(scpi, "ID?", &response); | |
217 | if ((ret != SR_OK) || !response) | |
218 | return SR_ERR; | |
219 | ||
220 | hw_info = g_malloc0(sizeof(struct sr_scpi_hw_info)); | |
221 | ||
222 | *scpi_response = hw_info; | |
223 | hw_info->model = response; | |
224 | hw_info->firmware_version = hpib_get_revision(scpi); | |
225 | hw_info->manufacturer = g_strdup("HP"); | |
226 | ||
227 | return SR_OK; | |
228 | } | |
229 | ||
230 | static struct sr_dev_inst *probe_scpi_pps_device(struct sr_scpi_dev_inst *scpi) | |
231 | { | |
232 | return probe_device(scpi, sr_scpi_get_hw_id); | |
233 | } | |
234 | ||
235 | static struct sr_dev_inst *probe_hpib_pps_device(struct sr_scpi_dev_inst *scpi) | |
236 | { | |
237 | return probe_device(scpi, hpib_get_hw_id); | |
238 | } | |
239 | ||
240 | static GSList *scan_scpi_pps(struct sr_dev_driver *di, GSList *options) | |
241 | { | |
242 | return sr_scpi_scan(di->context, options, probe_scpi_pps_device); | |
243 | } | |
244 | ||
245 | static GSList *scan_hpib_pps(struct sr_dev_driver *di, GSList *options) | |
246 | { | |
247 | return sr_scpi_scan(di->context, options, probe_hpib_pps_device); | |
248 | } | |
249 | ||
250 | static int dev_open(struct sr_dev_inst *sdi) | |
251 | { | |
252 | struct dev_context *devc; | |
253 | struct sr_scpi_dev_inst *scpi; | |
254 | GVariant *beeper; | |
255 | ||
256 | scpi = sdi->conn; | |
257 | if (sr_scpi_open(scpi) < 0) | |
258 | return SR_ERR; | |
259 | ||
260 | devc = sdi->priv; | |
261 | sr_scpi_cmd(sdi, devc->device->commands, 0, NULL, SCPI_CMD_REMOTE); | |
262 | devc->beeper_was_set = FALSE; | |
263 | if (sr_scpi_cmd_resp(sdi, devc->device->commands, 0, NULL, | |
264 | &beeper, G_VARIANT_TYPE_BOOLEAN, SCPI_CMD_BEEPER) == SR_OK) { | |
265 | if (g_variant_get_boolean(beeper)) { | |
266 | devc->beeper_was_set = TRUE; | |
267 | sr_scpi_cmd(sdi, devc->device->commands, | |
268 | 0, NULL, SCPI_CMD_BEEPER_DISABLE); | |
269 | } | |
270 | g_variant_unref(beeper); | |
271 | } | |
272 | ||
273 | return SR_OK; | |
274 | } | |
275 | ||
276 | static int dev_close(struct sr_dev_inst *sdi) | |
277 | { | |
278 | struct sr_scpi_dev_inst *scpi; | |
279 | struct dev_context *devc; | |
280 | ||
281 | devc = sdi->priv; | |
282 | scpi = sdi->conn; | |
283 | ||
284 | if (!scpi) | |
285 | return SR_ERR_BUG; | |
286 | ||
287 | if (devc->beeper_was_set) | |
288 | sr_scpi_cmd(sdi, devc->device->commands, | |
289 | 0, NULL, SCPI_CMD_BEEPER_ENABLE); | |
290 | sr_scpi_cmd(sdi, devc->device->commands, 0, NULL, SCPI_CMD_LOCAL); | |
291 | ||
292 | return sr_scpi_close(scpi); | |
293 | } | |
294 | ||
295 | static void clear_helper(struct dev_context *devc) | |
296 | { | |
297 | g_free(devc->channels); | |
298 | g_free(devc->channel_groups); | |
299 | } | |
300 | ||
301 | static int dev_clear(const struct sr_dev_driver *di) | |
302 | { | |
303 | return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper); | |
304 | } | |
305 | ||
306 | static int config_get(uint32_t key, GVariant **data, | |
307 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
308 | { | |
309 | struct dev_context *devc; | |
310 | const GVariantType *gvtype; | |
311 | unsigned int i; | |
312 | int channel_group_cmd; | |
313 | char *channel_group_name; | |
314 | int cmd, ret; | |
315 | const char *s; | |
316 | int oper_cond; | |
317 | ||
318 | if (!sdi) | |
319 | return SR_ERR_ARG; | |
320 | ||
321 | devc = sdi->priv; | |
322 | ||
323 | if (cg) { | |
324 | /* | |
325 | * These options only apply to channel groups with a single | |
326 | * channel -- they're per-channel settings for the device. | |
327 | */ | |
328 | ||
329 | /* | |
330 | * Config keys are handled below depending on whether a channel | |
331 | * group was provided by the frontend. However some of these | |
332 | * take a CG on one PPS but not on others. Check the device's | |
333 | * profile for that here, and NULL out the channel group as needed. | |
334 | */ | |
335 | for (i = 0; i < devc->device->num_devopts; i++) { | |
336 | if (devc->device->devopts[i] == key) { | |
337 | cg = NULL; | |
338 | break; | |
339 | } | |
340 | } | |
341 | } | |
342 | ||
343 | gvtype = NULL; | |
344 | cmd = -1; | |
345 | switch (key) { | |
346 | case SR_CONF_ENABLED: | |
347 | gvtype = G_VARIANT_TYPE_BOOLEAN; | |
348 | cmd = SCPI_CMD_GET_OUTPUT_ENABLED; | |
349 | break; | |
350 | case SR_CONF_VOLTAGE: | |
351 | gvtype = G_VARIANT_TYPE_DOUBLE; | |
352 | cmd = SCPI_CMD_GET_MEAS_VOLTAGE; | |
353 | break; | |
354 | case SR_CONF_VOLTAGE_TARGET: | |
355 | gvtype = G_VARIANT_TYPE_DOUBLE; | |
356 | cmd = SCPI_CMD_GET_VOLTAGE_TARGET; | |
357 | break; | |
358 | case SR_CONF_OUTPUT_FREQUENCY: | |
359 | gvtype = G_VARIANT_TYPE_DOUBLE; | |
360 | cmd = SCPI_CMD_GET_MEAS_FREQUENCY; | |
361 | break; | |
362 | case SR_CONF_OUTPUT_FREQUENCY_TARGET: | |
363 | gvtype = G_VARIANT_TYPE_DOUBLE; | |
364 | cmd = SCPI_CMD_GET_FREQUENCY_TARGET; | |
365 | break; | |
366 | case SR_CONF_CURRENT: | |
367 | gvtype = G_VARIANT_TYPE_DOUBLE; | |
368 | cmd = SCPI_CMD_GET_MEAS_CURRENT; | |
369 | break; | |
370 | case SR_CONF_CURRENT_LIMIT: | |
371 | gvtype = G_VARIANT_TYPE_DOUBLE; | |
372 | cmd = SCPI_CMD_GET_CURRENT_LIMIT; | |
373 | break; | |
374 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: | |
375 | gvtype = G_VARIANT_TYPE_BOOLEAN; | |
376 | cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ENABLED; | |
377 | break; | |
378 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE: | |
379 | gvtype = G_VARIANT_TYPE_BOOLEAN; | |
380 | cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ACTIVE; | |
381 | break; | |
382 | case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD: | |
383 | gvtype = G_VARIANT_TYPE_DOUBLE; | |
384 | cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_THRESHOLD; | |
385 | break; | |
386 | case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: | |
387 | gvtype = G_VARIANT_TYPE_BOOLEAN; | |
388 | cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ENABLED; | |
389 | break; | |
390 | case SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE: | |
391 | gvtype = G_VARIANT_TYPE_BOOLEAN; | |
392 | cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ACTIVE; | |
393 | break; | |
394 | case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD: | |
395 | gvtype = G_VARIANT_TYPE_DOUBLE; | |
396 | cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_THRESHOLD; | |
397 | break; | |
398 | case SR_CONF_OVER_TEMPERATURE_PROTECTION: | |
399 | gvtype = G_VARIANT_TYPE_BOOLEAN; | |
400 | cmd = SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION; | |
401 | break; | |
402 | case SR_CONF_REGULATION: | |
403 | gvtype = G_VARIANT_TYPE_STRING; | |
404 | cmd = SCPI_CMD_GET_OUTPUT_REGULATION; | |
405 | break; | |
406 | default: | |
407 | return sr_sw_limits_config_get(&devc->limits, key, data); | |
408 | } | |
409 | if (!gvtype) | |
410 | return SR_ERR_NA; | |
411 | ||
412 | channel_group_cmd = 0; | |
413 | channel_group_name = NULL; | |
414 | if (cg) { | |
415 | channel_group_cmd = SCPI_CMD_SELECT_CHANNEL; | |
416 | channel_group_name = g_strdup(cg->name); | |
417 | } | |
418 | ||
419 | ret = sr_scpi_cmd_resp(sdi, devc->device->commands, | |
420 | channel_group_cmd, channel_group_name, data, gvtype, cmd); | |
421 | g_free(channel_group_name); | |
422 | ||
423 | /* | |
424 | * Handle special cases | |
425 | */ | |
426 | ||
427 | if (cmd == SCPI_CMD_GET_OUTPUT_REGULATION) { | |
428 | if (devc->device->dialect == SCPI_DIALECT_PHILIPS) { | |
429 | /* | |
430 | * The Philips PM2800 series returns VOLT/CURR. We always return | |
431 | * a GVariant string in the Rigol notation (CV/CC/UR). | |
432 | */ | |
433 | s = g_variant_get_string(*data, NULL); | |
434 | if (!g_strcmp0(s, "VOLT")) { | |
435 | g_variant_unref(*data); | |
436 | *data = g_variant_new_string("CV"); | |
437 | } else if (!g_strcmp0(s, "CURR")) { | |
438 | g_variant_unref(*data); | |
439 | *data = g_variant_new_string("CC"); | |
440 | } | |
441 | } | |
442 | if (devc->device->dialect == SCPI_DIALECT_HP_66XXB) { | |
443 | /* Evaluate Operational Status Register from a HP 66xxB. */ | |
444 | s = g_variant_get_string(*data, NULL); | |
445 | sr_atoi(s, &oper_cond); | |
446 | g_variant_unref(*data); | |
447 | if (oper_cond & (1 << 8)) | |
448 | *data = g_variant_new_string("CV"); | |
449 | else if (oper_cond & (1 << 10)) | |
450 | *data = g_variant_new_string("CC"); | |
451 | else if (oper_cond & (1 << 11)) | |
452 | *data = g_variant_new_string("CC-"); | |
453 | else | |
454 | *data = g_variant_new_string("UR"); | |
455 | } | |
456 | ||
457 | s = g_variant_get_string(*data, NULL); | |
458 | if (g_strcmp0(s, "CV") && g_strcmp0(s, "CC") && | |
459 | g_strcmp0(s, "CC-") && g_strcmp0(s, "UR")) { | |
460 | ||
461 | sr_err("Unknown response to SCPI_CMD_GET_OUTPUT_REGULATION: %s", s); | |
462 | ret = SR_ERR_DATA; | |
463 | } | |
464 | } | |
465 | ||
466 | return ret; | |
467 | } | |
468 | ||
469 | static int config_set(uint32_t key, GVariant *data, | |
470 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
471 | { | |
472 | struct dev_context *devc; | |
473 | double d; | |
474 | int channel_group_cmd; | |
475 | char *channel_group_name; | |
476 | int ret; | |
477 | ||
478 | if (!sdi) | |
479 | return SR_ERR_ARG; | |
480 | ||
481 | channel_group_cmd = 0; | |
482 | channel_group_name = NULL; | |
483 | if (cg) { | |
484 | channel_group_cmd = SCPI_CMD_SELECT_CHANNEL; | |
485 | channel_group_name = g_strdup(cg->name); | |
486 | } | |
487 | ||
488 | devc = sdi->priv; | |
489 | ||
490 | switch (key) { | |
491 | case SR_CONF_ENABLED: | |
492 | if (g_variant_get_boolean(data)) | |
493 | ret = sr_scpi_cmd(sdi, devc->device->commands, | |
494 | channel_group_cmd, channel_group_name, | |
495 | SCPI_CMD_SET_OUTPUT_ENABLE); | |
496 | else | |
497 | ret = sr_scpi_cmd(sdi, devc->device->commands, | |
498 | channel_group_cmd, channel_group_name, | |
499 | SCPI_CMD_SET_OUTPUT_DISABLE); | |
500 | break; | |
501 | case SR_CONF_VOLTAGE_TARGET: | |
502 | d = g_variant_get_double(data); | |
503 | ret = sr_scpi_cmd(sdi, devc->device->commands, | |
504 | channel_group_cmd, channel_group_name, | |
505 | SCPI_CMD_SET_VOLTAGE_TARGET, d); | |
506 | break; | |
507 | case SR_CONF_OUTPUT_FREQUENCY_TARGET: | |
508 | d = g_variant_get_double(data); | |
509 | ret = sr_scpi_cmd(sdi, devc->device->commands, | |
510 | channel_group_cmd, channel_group_name, | |
511 | SCPI_CMD_SET_FREQUENCY_TARGET, d); | |
512 | break; | |
513 | case SR_CONF_CURRENT_LIMIT: | |
514 | d = g_variant_get_double(data); | |
515 | ret = sr_scpi_cmd(sdi, devc->device->commands, | |
516 | channel_group_cmd, channel_group_name, | |
517 | SCPI_CMD_SET_CURRENT_LIMIT, d); | |
518 | break; | |
519 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: | |
520 | if (g_variant_get_boolean(data)) | |
521 | ret = sr_scpi_cmd(sdi, devc->device->commands, | |
522 | channel_group_cmd, channel_group_name, | |
523 | SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_ENABLE); | |
524 | else | |
525 | ret = sr_scpi_cmd(sdi, devc->device->commands, | |
526 | channel_group_cmd, channel_group_name, | |
527 | SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_DISABLE); | |
528 | break; | |
529 | case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD: | |
530 | d = g_variant_get_double(data); | |
531 | ret = sr_scpi_cmd(sdi, devc->device->commands, | |
532 | channel_group_cmd, channel_group_name, | |
533 | SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_THRESHOLD, d); | |
534 | break; | |
535 | case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: | |
536 | if (g_variant_get_boolean(data)) | |
537 | ret = sr_scpi_cmd(sdi, devc->device->commands, | |
538 | channel_group_cmd, channel_group_name, | |
539 | SCPI_CMD_SET_OVER_CURRENT_PROTECTION_ENABLE); | |
540 | else | |
541 | ret = sr_scpi_cmd(sdi, devc->device->commands, | |
542 | channel_group_cmd, channel_group_name, | |
543 | SCPI_CMD_SET_OVER_CURRENT_PROTECTION_DISABLE); | |
544 | break; | |
545 | case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD: | |
546 | d = g_variant_get_double(data); | |
547 | ret = sr_scpi_cmd(sdi, devc->device->commands, | |
548 | channel_group_cmd, channel_group_name, | |
549 | SCPI_CMD_SET_OVER_CURRENT_PROTECTION_THRESHOLD, d); | |
550 | break; | |
551 | case SR_CONF_OVER_TEMPERATURE_PROTECTION: | |
552 | if (g_variant_get_boolean(data)) | |
553 | ret = sr_scpi_cmd(sdi, devc->device->commands, | |
554 | channel_group_cmd, channel_group_name, | |
555 | SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_ENABLE); | |
556 | else | |
557 | ret = sr_scpi_cmd(sdi, devc->device->commands, | |
558 | channel_group_cmd, channel_group_name, | |
559 | SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_DISABLE); | |
560 | break; | |
561 | default: | |
562 | ret = sr_sw_limits_config_set(&devc->limits, key, data); | |
563 | } | |
564 | ||
565 | g_free(channel_group_name); | |
566 | ||
567 | return ret; | |
568 | } | |
569 | ||
570 | static int config_list(uint32_t key, GVariant **data, | |
571 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
572 | { | |
573 | struct dev_context *devc; | |
574 | struct sr_channel *ch; | |
575 | const struct channel_spec *ch_spec; | |
576 | int i; | |
577 | const char *s[16]; | |
578 | ||
579 | devc = (sdi) ? sdi->priv : NULL; | |
580 | ||
581 | if (!cg) { | |
582 | switch (key) { | |
583 | case SR_CONF_SCAN_OPTIONS: | |
584 | case SR_CONF_DEVICE_OPTIONS: | |
585 | return std_opts_config_list(key, data, sdi, cg, | |
586 | ARRAY_AND_SIZE(scanopts), | |
587 | ARRAY_AND_SIZE(drvopts), | |
588 | (devc && devc->device) ? devc->device->devopts : NULL, | |
589 | (devc && devc->device) ? devc->device->num_devopts : 0); | |
590 | break; | |
591 | case SR_CONF_CHANNEL_CONFIG: | |
592 | if (!devc || !devc->device) | |
593 | return SR_ERR_ARG; | |
594 | /* Not used. */ | |
595 | i = 0; | |
596 | if (devc->device->features & PPS_INDEPENDENT) | |
597 | s[i++] = "Independent"; | |
598 | if (devc->device->features & PPS_SERIES) | |
599 | s[i++] = "Series"; | |
600 | if (devc->device->features & PPS_PARALLEL) | |
601 | s[i++] = "Parallel"; | |
602 | if (i == 0) { | |
603 | /* | |
604 | * Shouldn't happen: independent-only devices | |
605 | * shouldn't advertise this option at all. | |
606 | */ | |
607 | return SR_ERR_NA; | |
608 | } | |
609 | *data = g_variant_new_strv(s, i); | |
610 | break; | |
611 | default: | |
612 | return SR_ERR_NA; | |
613 | } | |
614 | } else { | |
615 | /* | |
616 | * Per-channel-group options depending on a channel are actually | |
617 | * done with the first channel. Channel groups in PPS can have | |
618 | * more than one channel, but they will typically be of equal | |
619 | * specification for use in series or parallel mode. | |
620 | */ | |
621 | ch = cg->channels->data; | |
622 | if (!devc || !devc->device) | |
623 | return SR_ERR_ARG; | |
624 | ch_spec = &(devc->device->channels[ch->index]); | |
625 | ||
626 | switch (key) { | |
627 | case SR_CONF_DEVICE_OPTIONS: | |
628 | *data = std_gvar_array_u32(devc->device->devopts_cg, devc->device->num_devopts_cg); | |
629 | break; | |
630 | case SR_CONF_VOLTAGE_TARGET: | |
631 | *data = std_gvar_min_max_step_array(ch_spec->voltage); | |
632 | break; | |
633 | case SR_CONF_OUTPUT_FREQUENCY_TARGET: | |
634 | *data = std_gvar_min_max_step_array(ch_spec->frequency); | |
635 | break; | |
636 | case SR_CONF_CURRENT_LIMIT: | |
637 | *data = std_gvar_min_max_step_array(ch_spec->current); | |
638 | break; | |
639 | case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD: | |
640 | *data = std_gvar_min_max_step_array(ch_spec->ovp); | |
641 | break; | |
642 | case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD: | |
643 | *data = std_gvar_min_max_step_array(ch_spec->ocp); | |
644 | break; | |
645 | default: | |
646 | return SR_ERR_NA; | |
647 | } | |
648 | } | |
649 | ||
650 | return SR_OK; | |
651 | } | |
652 | ||
653 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
654 | { | |
655 | struct dev_context *devc; | |
656 | struct sr_scpi_dev_inst *scpi; | |
657 | int ret; | |
658 | ||
659 | devc = sdi->priv; | |
660 | scpi = sdi->conn; | |
661 | ||
662 | /* Prime the pipe with the first channel. */ | |
663 | devc->cur_acquisition_channel = sr_next_enabled_channel(sdi, NULL); | |
664 | ||
665 | if ((ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 10, | |
666 | scpi_pps_receive_data, (void *)sdi)) != SR_OK) | |
667 | return ret; | |
668 | std_session_send_df_header(sdi); | |
669 | sr_sw_limits_acquisition_start(&devc->limits); | |
670 | ||
671 | return SR_OK; | |
672 | } | |
673 | ||
674 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
675 | { | |
676 | struct sr_scpi_dev_inst *scpi; | |
677 | ||
678 | scpi = sdi->conn; | |
679 | ||
680 | sr_scpi_source_remove(sdi->session, scpi); | |
681 | ||
682 | std_session_send_df_end(sdi); | |
683 | ||
684 | return SR_OK; | |
685 | } | |
686 | ||
687 | static struct sr_dev_driver scpi_pps_driver_info = { | |
688 | .name = "scpi-pps", | |
689 | .longname = "SCPI PPS", | |
690 | .api_version = 1, | |
691 | .init = std_init, | |
692 | .cleanup = std_cleanup, | |
693 | .scan = scan_scpi_pps, | |
694 | .dev_list = std_dev_list, | |
695 | .dev_clear = dev_clear, | |
696 | .config_get = config_get, | |
697 | .config_set = config_set, | |
698 | .config_list = config_list, | |
699 | .dev_open = dev_open, | |
700 | .dev_close = dev_close, | |
701 | .dev_acquisition_start = dev_acquisition_start, | |
702 | .dev_acquisition_stop = dev_acquisition_stop, | |
703 | .context = NULL, | |
704 | }; | |
705 | ||
706 | static struct sr_dev_driver hp_ib_pps_driver_info = { | |
707 | .name = "hpib-pps", | |
708 | .longname = "HP-IB PPS", | |
709 | .api_version = 1, | |
710 | .init = std_init, | |
711 | .cleanup = std_cleanup, | |
712 | .scan = scan_hpib_pps, | |
713 | .dev_list = std_dev_list, | |
714 | .dev_clear = dev_clear, | |
715 | .config_get = config_get, | |
716 | .config_set = config_set, | |
717 | .config_list = config_list, | |
718 | .dev_open = dev_open, | |
719 | .dev_close = dev_close, | |
720 | .dev_acquisition_start = dev_acquisition_start, | |
721 | .dev_acquisition_stop = dev_acquisition_stop, | |
722 | .context = NULL, | |
723 | }; | |
724 | SR_REGISTER_DEV_DRIVER(scpi_pps_driver_info); | |
725 | SR_REGISTER_DEV_DRIVER(hp_ib_pps_driver_info); |