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