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