]> sigrok.org Git - libsigrok.git/blame - src/hardware/scpi-pps/api.c
scpi-pps: Add SR_CONF_REGULATION for HP 66xxA power supplies.
[libsigrok.git] / src / hardware / scpi-pps / api.c
CommitLineData
ca1a7cb5
BV
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
7e66bf05 5 * Copyright (C) 2017,2019 Frank Stettner <frank-stettner@gmx.net>
ca1a7cb5
BV
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
6ec6c43b 21#include <config.h>
9e45cd41 22#include <string.h>
ba464a12 23#include <strings.h>
91ef511d 24#include "scpi.h"
ca1a7cb5
BV
25#include "protocol.h"
26
dd5c48a6 27static struct sr_dev_driver scpi_pps_driver_info;
c2af709b 28static struct sr_dev_driver hp_ib_pps_driver_info;
9e45cd41 29
584560f1 30static const uint32_t scanopts[] = {
9e45cd41
BV
31 SR_CONF_CONN,
32 SR_CONF_SERIALCOMM,
33};
ca1a7cb5 34
9d9cf1c4 35static const uint32_t drvopts[] = {
a258204e 36 SR_CONF_POWER_SUPPLY,
a258204e
BV
37};
38
329733d9 39static const struct pps_channel_instance pci[] = {
01b0257a
BV
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" },
4264f1c0 43 { SR_MQ_FREQUENCY, SCPI_CMD_GET_MEAS_FREQUENCY, "F" },
01b0257a
BV
44};
45
c2af709b 46static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi,
d9251a2c
UH
47 int (*get_hw_id)(struct sr_scpi_dev_inst *scpi,
48 struct sr_scpi_hw_info **scpi_response))
ca1a7cb5 49{
9e45cd41
BV
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;
01b0257a 56 struct pps_channel *pch;
4a471029
BV
57 struct channel_spec *channels;
58 struct channel_group_spec *channel_groups, *cgs;
9e45cd41 59 struct pps_channel_group *pcg;
58b77c41
BV
60 GRegex *model_re;
61 GMatchInfo *model_mi;
01b0257a 62 GSList *l;
9e45cd41 63 uint64_t mask;
4a471029
BV
64 unsigned int num_channels, num_channel_groups, ch_num, ch_idx, i, j;
65 int ret;
22c18b03 66 const char *vendor;
01b0257a 67 char ch_name[16];
9e45cd41 68
c2af709b 69 if (get_hw_id(scpi, &hw_info) != SR_OK) {
9e45cd41
BV
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++) {
91ef511d 76 vendor = sr_vendor_alias(hw_info->manufacturer);
34577da6 77 if (g_ascii_strcasecmp(vendor, pps_profiles[i].vendor))
22c18b03 78 continue;
58b77c41
BV
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))
9e45cd41 81 device = &pps_profiles[i];
58b77c41
BV
82 g_match_info_unref(model_mi);
83 g_regex_unref(model_re);
84 if (device)
9e45cd41 85 break;
9e45cd41
BV
86 }
87 if (!device) {
88 sr_scpi_hw_info_free(hw_info);
89 return NULL;
90 }
91
aac29cc1 92 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
93 sdi->vendor = g_strdup(vendor);
94 sdi->model = g_strdup(hw_info->model);
95 sdi->version = g_strdup(hw_info->firmware_version);
9e45cd41 96 sdi->conn = scpi;
4f840ce9 97 sdi->driver = &scpi_pps_driver_info;
9e45cd41 98 sdi->inst_type = SR_INST_SCPI;
d08667c5
SA
99 sdi->serial_num = g_strdup(hw_info->serial_number);
100
9e45cd41
BV
101 devc = g_malloc0(sizeof(struct dev_context));
102 devc->device = device;
88e4daa9 103 sr_sw_limits_init(&devc->limits);
9e45cd41
BV
104 sdi->priv = devc;
105
4a471029
BV
106 if (device->num_channels) {
107 /* Static channels and groups. */
329733d9 108 channels = (struct channel_spec *)device->channels;
4a471029 109 num_channels = device->num_channels;
329733d9 110 channel_groups = (struct channel_group_spec *)device->channel_groups;
4a471029
BV
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
01b0257a 128 ch_idx = 0;
4a471029 129 for (ch_num = 0; ch_num < num_channels; ch_num++) {
01b0257a 130 /* Create one channel per measurable output unit. */
01b0257a 131 for (i = 0; i < ARRAY_SIZE(pci); i++) {
fa2ce8c7 132 if (!sr_scpi_cmd_get(devc->device->commands, pci[i].command))
01b0257a
BV
133 continue;
134 g_snprintf(ch_name, 16, "%s%s", pci[i].prefix,
4a471029 135 channels[ch_num].name);
5e23fcab
ML
136 ch = sr_channel_new(sdi, ch_idx++, SR_CHANNEL_ANALOG, TRUE,
137 ch_name);
01b0257a
BV
138 pch = g_malloc0(sizeof(struct pps_channel));
139 pch->hw_output_idx = ch_num;
4a471029 140 pch->hwname = channels[ch_num].name;
01b0257a
BV
141 pch->mq = pci[i].mq;
142 ch->priv = pch;
01b0257a 143 }
9e45cd41 144 }
ca1a7cb5 145
4a471029
BV
146 for (i = 0; i < num_channel_groups; i++) {
147 cgs = &channel_groups[i];
9e45cd41
BV
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) {
01b0257a
BV
152 for (l = sdi->channels; l; l = l->next) {
153 ch = l->data;
154 pch = ch->priv;
f2bbcc33
FS
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;
01b0257a
BV
162 if (pch->hw_output_idx == j)
163 cg->channels = g_slist_append(cg->channels, ch);
164 }
9e45cd41
BV
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 }
ca1a7cb5 172
4a471029
BV
173 sr_scpi_hw_info_free(hw_info);
174 hw_info = NULL;
175
17a82e83 176 sr_scpi_cmd(sdi, devc->device->commands, 0, NULL, SCPI_CMD_LOCAL);
ca1a7cb5 177
9e45cd41
BV
178 return sdi;
179}
ca1a7cb5 180
c2af709b 181static gchar *hpib_get_revision(struct sr_scpi_dev_inst *scpi)
9e45cd41 182{
c2af709b
AG
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 */
216static 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
237static 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
242static 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
247static 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
252static GSList *scan_hpib_pps(struct sr_dev_driver *di, GSList *options)
253{
254 return sr_scpi_scan(di->context, options, probe_hpib_pps_device);
ca1a7cb5
BV
255}
256
ca1a7cb5
BV
257static int dev_open(struct sr_dev_inst *sdi)
258{
ee2860ee 259 struct dev_context *devc;
9e45cd41 260 struct sr_scpi_dev_inst *scpi;
ee2860ee 261 GVariant *beeper;
9e45cd41 262
9e45cd41
BV
263 scpi = sdi->conn;
264 if (sr_scpi_open(scpi) < 0)
265 return SR_ERR;
ca1a7cb5 266
91ef511d 267 devc = sdi->priv;
17a82e83 268 sr_scpi_cmd(sdi, devc->device->commands, 0, NULL, SCPI_CMD_REMOTE);
ee2860ee 269 devc->beeper_was_set = FALSE;
17a82e83
FS
270 if (sr_scpi_cmd_resp(sdi, devc->device->commands, 0, NULL,
271 &beeper, G_VARIANT_TYPE_BOOLEAN, SCPI_CMD_BEEPER) == SR_OK) {
ee2860ee
BV
272 if (g_variant_get_boolean(beeper)) {
273 devc->beeper_was_set = TRUE;
17a82e83
FS
274 sr_scpi_cmd(sdi, devc->device->commands,
275 0, NULL, SCPI_CMD_BEEPER_DISABLE);
ee2860ee
BV
276 }
277 g_variant_unref(beeper);
278 }
60475cd7 279
ca1a7cb5
BV
280 return SR_OK;
281}
282
283static int dev_close(struct sr_dev_inst *sdi)
284{
9e45cd41 285 struct sr_scpi_dev_inst *scpi;
ee2860ee 286 struct dev_context *devc;
ca1a7cb5 287
ee2860ee 288 devc = sdi->priv;
9e45cd41 289 scpi = sdi->conn;
ca1a7cb5 290
f1ba6b4b
UH
291 if (!scpi)
292 return SR_ERR_BUG;
293
294 if (devc->beeper_was_set)
17a82e83
FS
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);
f1ba6b4b
UH
298
299 return sr_scpi_close(scpi);
ca1a7cb5
BV
300}
301
3553451f 302static void clear_helper(struct dev_context *devc)
4a471029 303{
4a471029
BV
304 g_free(devc->channels);
305 g_free(devc->channel_groups);
4a471029
BV
306}
307
1e726f56 308static int dev_clear(const struct sr_dev_driver *di)
ca1a7cb5 309{
3553451f 310 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
ca1a7cb5
BV
311}
312
dd7a72ea
UH
313static int config_get(uint32_t key, GVariant **data,
314 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
ca1a7cb5 315{
9e45cd41 316 struct dev_context *devc;
478c8d92
BV
317 const GVariantType *gvtype;
318 unsigned int i;
17a82e83
FS
319 int channel_group_cmd;
320 char *channel_group_name;
478c8d92 321 int cmd, ret;
069d9f25 322 const char *s;
8b5eadf4 323 int reg;
ca1a7cb5 324
9e45cd41
BV
325 if (!sdi)
326 return SR_ERR_ARG;
327
328 devc = sdi->priv;
ca1a7cb5 329
478c8d92 330 if (cg) {
9e45cd41
BV
331 /*
332 * These options only apply to channel groups with a single
333 * channel -- they're per-channel settings for the device.
334 */
478c8d92
BV
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 }
478c8d92 348 }
9e45cd41 349
478c8d92
BV
350 gvtype = NULL;
351 cmd = -1;
352 switch (key) {
7a0b98b5 353 case SR_CONF_ENABLED:
478c8d92
BV
354 gvtype = G_VARIANT_TYPE_BOOLEAN;
355 cmd = SCPI_CMD_GET_OUTPUT_ENABLED;
356 break;
7a0b98b5 357 case SR_CONF_VOLTAGE:
478c8d92
BV
358 gvtype = G_VARIANT_TYPE_DOUBLE;
359 cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
360 break;
7a0b98b5 361 case SR_CONF_VOLTAGE_TARGET:
478c8d92 362 gvtype = G_VARIANT_TYPE_DOUBLE;
ca95e90f 363 cmd = SCPI_CMD_GET_VOLTAGE_TARGET;
478c8d92 364 break;
4264f1c0
AG
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;
7a0b98b5 373 case SR_CONF_CURRENT:
478c8d92
BV
374 gvtype = G_VARIANT_TYPE_DOUBLE;
375 cmd = SCPI_CMD_GET_MEAS_CURRENT;
376 break;
7a0b98b5 377 case SR_CONF_CURRENT_LIMIT:
478c8d92 378 gvtype = G_VARIANT_TYPE_DOUBLE;
ca95e90f 379 cmd = SCPI_CMD_GET_CURRENT_LIMIT;
478c8d92
BV
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:
8b5eadf4
FS
386 if (devc->device->dialect == SCPI_DIALECT_HP_66XXB)
387 gvtype = G_VARIANT_TYPE_STRING;
388 else
389 gvtype = G_VARIANT_TYPE_BOOLEAN;
478c8d92
BV
390 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ACTIVE;
391 break;
392 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
393 gvtype = G_VARIANT_TYPE_DOUBLE;
394 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_THRESHOLD;
395 break;
396 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
397 gvtype = G_VARIANT_TYPE_BOOLEAN;
398 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ENABLED;
399 break;
400 case SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE:
8b5eadf4
FS
401 if (devc->device->dialect == SCPI_DIALECT_HP_66XXB)
402 gvtype = G_VARIANT_TYPE_STRING;
403 else
404 gvtype = G_VARIANT_TYPE_BOOLEAN;
478c8d92
BV
405 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ACTIVE;
406 break;
407 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
408 gvtype = G_VARIANT_TYPE_DOUBLE;
409 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_THRESHOLD;
410 break;
411 case SR_CONF_OVER_TEMPERATURE_PROTECTION:
412 gvtype = G_VARIANT_TYPE_BOOLEAN;
413 cmd = SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION;
414 break;
8b5eadf4
FS
415 case SR_CONF_OVER_TEMPERATURE_PROTECTION_ACTIVE:
416 if (devc->device->dialect == SCPI_DIALECT_HP_66XXB)
417 gvtype = G_VARIANT_TYPE_STRING;
418 else
419 gvtype = G_VARIANT_TYPE_BOOLEAN;
420 cmd = SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION_ACTIVE;
421 break;
7a0b98b5 422 case SR_CONF_REGULATION:
60475cd7
BV
423 gvtype = G_VARIANT_TYPE_STRING;
424 cmd = SCPI_CMD_GET_OUTPUT_REGULATION;
755793e9 425 break;
88e4daa9
ML
426 default:
427 return sr_sw_limits_config_get(&devc->limits, key, data);
478c8d92 428 }
91ef511d
BV
429 if (!gvtype)
430 return SR_ERR_NA;
431
17a82e83
FS
432 channel_group_cmd = 0;
433 channel_group_name = NULL;
434 if (cg) {
435 channel_group_cmd = SCPI_CMD_SELECT_CHANNEL;
436 channel_group_name = g_strdup(cg->name);
437 }
438
439 ret = sr_scpi_cmd_resp(sdi, devc->device->commands,
440 channel_group_cmd, channel_group_name, data, gvtype, cmd);
441 g_free(channel_group_name);
91ef511d 442
43ff1110
FS
443 /*
444 * Handle special cases
445 */
446
91ef511d 447 if (cmd == SCPI_CMD_GET_OUTPUT_REGULATION) {
43ff1110
FS
448 if (devc->device->dialect == SCPI_DIALECT_PHILIPS) {
449 /*
450 * The Philips PM2800 series returns VOLT/CURR. We always return
451 * a GVariant string in the Rigol notation (CV/CC/UR).
452 */
453 s = g_variant_get_string(*data, NULL);
454 if (!g_strcmp0(s, "VOLT")) {
455 g_variant_unref(*data);
456 *data = g_variant_new_string("CV");
457 } else if (!g_strcmp0(s, "CURR")) {
458 g_variant_unref(*data);
459 *data = g_variant_new_string("CC");
460 }
461 }
0ad7074c
FS
462 if (devc->device->dialect == SCPI_DIALECT_HP_COMP) {
463 /* Evaluate Status Register from a HP 66xx in COMP mode. */
464 s = g_variant_get_string(*data, NULL);
465 sr_atoi(s, &reg);
466 g_variant_unref(*data);
467 if (reg & (1 << 0))
468 *data = g_variant_new_string("CV");
469 else if (reg & (1 << 1))
470 *data = g_variant_new_string("CC");
471 else if (reg & (1 << 2))
472 *data = g_variant_new_string("UR");
473 else if (reg & (1 << 9))
474 *data = g_variant_new_string("CC-");
475 else
476 *data = g_variant_new_string("");
477 }
43ff1110
FS
478 if (devc->device->dialect == SCPI_DIALECT_HP_66XXB) {
479 /* Evaluate Operational Status Register from a HP 66xxB. */
480 s = g_variant_get_string(*data, NULL);
8b5eadf4 481 sr_atoi(s, &reg);
069d9f25 482 g_variant_unref(*data);
8b5eadf4 483 if (reg & (1 << 8))
43ff1110 484 *data = g_variant_new_string("CV");
8b5eadf4 485 else if (reg & (1 << 10))
43ff1110 486 *data = g_variant_new_string("CC");
8b5eadf4 487 else if (reg & (1 << 11))
43ff1110
FS
488 *data = g_variant_new_string("CC-");
489 else
490 *data = g_variant_new_string("UR");
069d9f25
AJ
491 }
492
493 s = g_variant_get_string(*data, NULL);
0ad7074c
FS
494 if (g_strcmp0(s, "CV") && g_strcmp0(s, "CC") && g_strcmp0(s, "CC-") &&
495 g_strcmp0(s, "UR") && g_strcmp0(s, "")) {
43ff1110
FS
496
497 sr_err("Unknown response to SCPI_CMD_GET_OUTPUT_REGULATION: %s", s);
91ef511d
BV
498 ret = SR_ERR_DATA;
499 }
91ef511d 500 }
ca1a7cb5 501
8b5eadf4
FS
502 if (cmd == SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ACTIVE) {
503 if (devc->device->dialect == SCPI_DIALECT_HP_66XXB) {
504 /* Evaluate Questionable Status Register bit 0 from a HP 66xxB. */
505 s = g_variant_get_string(*data, NULL);
506 sr_atoi(s, &reg);
507 g_variant_unref(*data);
508 *data = g_variant_new_boolean(reg & (1 << 0));
509 }
510 }
511
512 if (cmd == SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ACTIVE) {
513 if (devc->device->dialect == SCPI_DIALECT_HP_66XXB) {
514 /* Evaluate Questionable Status Register bit 1 from a HP 66xxB. */
515 s = g_variant_get_string(*data, NULL);
516 sr_atoi(s, &reg);
517 g_variant_unref(*data);
518 *data = g_variant_new_boolean(reg & (1 << 1));
519 }
520 }
521
522 if (cmd == SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION_ACTIVE) {
523 if (devc->device->dialect == SCPI_DIALECT_HP_66XXB) {
524 /* Evaluate Questionable Status Register bit 4 from a HP 66xxB. */
525 s = g_variant_get_string(*data, NULL);
526 sr_atoi(s, &reg);
527 g_variant_unref(*data);
528 *data = g_variant_new_boolean(reg & (1 << 4));
529 }
530 }
531
ca1a7cb5
BV
532 return ret;
533}
534
dd7a72ea
UH
535static int config_set(uint32_t key, GVariant *data,
536 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
ca1a7cb5 537{
91ef511d 538 struct dev_context *devc;
9e45cd41 539 double d;
17a82e83
FS
540 int channel_group_cmd;
541 char *channel_group_name;
542 int ret;
ca1a7cb5 543
fdedbfcd
BV
544 if (!sdi)
545 return SR_ERR_ARG;
546
17a82e83
FS
547 channel_group_cmd = 0;
548 channel_group_name = NULL;
549 if (cg) {
550 channel_group_cmd = SCPI_CMD_SELECT_CHANNEL;
551 channel_group_name = g_strdup(cg->name);
552 }
60475cd7 553
91ef511d 554 devc = sdi->priv;
e57057ae 555
60475cd7 556 switch (key) {
7a0b98b5 557 case SR_CONF_ENABLED:
60475cd7 558 if (g_variant_get_boolean(data))
17a82e83
FS
559 ret = sr_scpi_cmd(sdi, devc->device->commands,
560 channel_group_cmd, channel_group_name,
91ef511d 561 SCPI_CMD_SET_OUTPUT_ENABLE);
60475cd7 562 else
17a82e83
FS
563 ret = sr_scpi_cmd(sdi, devc->device->commands,
564 channel_group_cmd, channel_group_name,
91ef511d 565 SCPI_CMD_SET_OUTPUT_DISABLE);
60475cd7 566 break;
7a0b98b5 567 case SR_CONF_VOLTAGE_TARGET:
60475cd7 568 d = g_variant_get_double(data);
17a82e83
FS
569 ret = sr_scpi_cmd(sdi, devc->device->commands,
570 channel_group_cmd, channel_group_name,
91ef511d 571 SCPI_CMD_SET_VOLTAGE_TARGET, d);
60475cd7 572 break;
4264f1c0
AG
573 case SR_CONF_OUTPUT_FREQUENCY_TARGET:
574 d = g_variant_get_double(data);
17a82e83
FS
575 ret = sr_scpi_cmd(sdi, devc->device->commands,
576 channel_group_cmd, channel_group_name,
91ef511d 577 SCPI_CMD_SET_FREQUENCY_TARGET, d);
4264f1c0 578 break;
7a0b98b5 579 case SR_CONF_CURRENT_LIMIT:
60475cd7 580 d = g_variant_get_double(data);
17a82e83
FS
581 ret = sr_scpi_cmd(sdi, devc->device->commands,
582 channel_group_cmd, channel_group_name,
91ef511d 583 SCPI_CMD_SET_CURRENT_LIMIT, d);
60475cd7
BV
584 break;
585 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
586 if (g_variant_get_boolean(data))
17a82e83
FS
587 ret = sr_scpi_cmd(sdi, devc->device->commands,
588 channel_group_cmd, channel_group_name,
91ef511d 589 SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_ENABLE);
60475cd7 590 else
17a82e83
FS
591 ret = sr_scpi_cmd(sdi, devc->device->commands,
592 channel_group_cmd, channel_group_name,
91ef511d 593 SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_DISABLE);
60475cd7
BV
594 break;
595 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
596 d = g_variant_get_double(data);
17a82e83
FS
597 ret = sr_scpi_cmd(sdi, devc->device->commands,
598 channel_group_cmd, channel_group_name,
91ef511d 599 SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_THRESHOLD, d);
60475cd7
BV
600 break;
601 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
602 if (g_variant_get_boolean(data))
17a82e83
FS
603 ret = sr_scpi_cmd(sdi, devc->device->commands,
604 channel_group_cmd, channel_group_name,
91ef511d 605 SCPI_CMD_SET_OVER_CURRENT_PROTECTION_ENABLE);
60475cd7 606 else
17a82e83
FS
607 ret = sr_scpi_cmd(sdi, devc->device->commands,
608 channel_group_cmd, channel_group_name,
91ef511d 609 SCPI_CMD_SET_OVER_CURRENT_PROTECTION_DISABLE);
60475cd7
BV
610 break;
611 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
612 d = g_variant_get_double(data);
17a82e83
FS
613 ret = sr_scpi_cmd(sdi, devc->device->commands,
614 channel_group_cmd, channel_group_name,
91ef511d 615 SCPI_CMD_SET_OVER_CURRENT_PROTECTION_THRESHOLD, d);
60475cd7
BV
616 break;
617 case SR_CONF_OVER_TEMPERATURE_PROTECTION:
618 if (g_variant_get_boolean(data))
17a82e83
FS
619 ret = sr_scpi_cmd(sdi, devc->device->commands,
620 channel_group_cmd, channel_group_name,
91ef511d 621 SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_ENABLE);
60475cd7 622 else
17a82e83
FS
623 ret = sr_scpi_cmd(sdi, devc->device->commands,
624 channel_group_cmd, channel_group_name,
91ef511d 625 SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_DISABLE);
60475cd7
BV
626 break;
627 default:
88e4daa9 628 ret = sr_sw_limits_config_set(&devc->limits, key, data);
ca1a7cb5
BV
629 }
630
17a82e83
FS
631 g_free(channel_group_name);
632
633 return ret;
ca1a7cb5
BV
634}
635
dd7a72ea
UH
636static int config_list(uint32_t key, GVariant **data,
637 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
ca1a7cb5 638{
9e45cd41
BV
639 struct dev_context *devc;
640 struct sr_channel *ch;
329733d9 641 const struct channel_spec *ch_spec;
a9010323 642 int i;
9e45cd41
BV
643 const char *s[16];
644
e66d1892 645 devc = (sdi) ? sdi->priv : NULL;
ca1a7cb5 646
9e45cd41 647 if (!cg) {
9e45cd41 648 switch (key) {
e66d1892 649 case SR_CONF_SCAN_OPTIONS:
9e45cd41 650 case SR_CONF_DEVICE_OPTIONS:
e66d1892 651 return std_opts_config_list(key, data, sdi, cg,
53012da6
UH
652 ARRAY_AND_SIZE(scanopts),
653 ARRAY_AND_SIZE(drvopts),
08eba2d3
GS
654 (devc && devc->device) ? devc->device->devopts : NULL,
655 (devc && devc->device) ? devc->device->num_devopts : 0);
9e45cd41 656 break;
7a0b98b5 657 case SR_CONF_CHANNEL_CONFIG:
08eba2d3
GS
658 if (!devc || !devc->device)
659 return SR_ERR_ARG;
478c8d92 660 /* Not used. */
9e45cd41
BV
661 i = 0;
662 if (devc->device->features & PPS_INDEPENDENT)
663 s[i++] = "Independent";
664 if (devc->device->features & PPS_SERIES)
665 s[i++] = "Series";
666 if (devc->device->features & PPS_PARALLEL)
667 s[i++] = "Parallel";
668 if (i == 0) {
669 /*
670 * Shouldn't happen: independent-only devices
671 * shouldn't advertise this option at all.
672 */
673 return SR_ERR_NA;
674 }
675 *data = g_variant_new_strv(s, i);
676 break;
677 default:
678 return SR_ERR_NA;
679 }
680 } else {
9e45cd41
BV
681 /*
682 * Per-channel-group options depending on a channel are actually
683 * done with the first channel. Channel groups in PPS can have
684 * more than one channel, but they will typically be of equal
01b0257a 685 * specification for use in series or parallel mode.
9e45cd41 686 */
9e45cd41 687 ch = cg->channels->data;
08eba2d3
GS
688 if (!devc || !devc->device)
689 return SR_ERR_ARG;
54d471f4 690 ch_spec = &(devc->device->channels[ch->index]);
9e45cd41
BV
691
692 switch (key) {
693 case SR_CONF_DEVICE_OPTIONS:
105df674 694 *data = std_gvar_array_u32(devc->device->devopts_cg, devc->device->num_devopts_cg);
9e45cd41 695 break;
7a0b98b5 696 case SR_CONF_VOLTAGE_TARGET:
54d471f4 697 *data = std_gvar_min_max_step_array(ch_spec->voltage);
9e45cd41 698 break;
4264f1c0 699 case SR_CONF_OUTPUT_FREQUENCY_TARGET:
54d471f4 700 *data = std_gvar_min_max_step_array(ch_spec->frequency);
4264f1c0 701 break;
7a0b98b5 702 case SR_CONF_CURRENT_LIMIT:
54d471f4 703 *data = std_gvar_min_max_step_array(ch_spec->current);
9e45cd41 704 break;
49a468ed
FS
705 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
706 *data = std_gvar_min_max_step_array(ch_spec->ovp);
707 break;
708 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
709 *data = std_gvar_min_max_step_array(ch_spec->ocp);
710 break;
9e45cd41
BV
711 default:
712 return SR_ERR_NA;
713 }
ca1a7cb5
BV
714 }
715
a9010323 716 return SR_OK;
ca1a7cb5
BV
717}
718
695dc859 719static int dev_acquisition_start(const struct sr_dev_inst *sdi)
ca1a7cb5 720{
9e45cd41
BV
721 struct dev_context *devc;
722 struct sr_scpi_dev_inst *scpi;
fa2ce8c7 723 int ret;
ca1a7cb5 724
9e45cd41
BV
725 devc = sdi->priv;
726 scpi = sdi->conn;
9e45cd41 727
17a82e83
FS
728 /* Prime the pipe with the first channel. */
729 devc->cur_acquisition_channel = sr_next_enabled_channel(sdi, NULL);
730
7e66bf05
FS
731 /* Device specific initialization before aquisition starts. */
732 if (devc->device->init_aquisition)
733 devc->device->init_aquisition(sdi);
734
01b0257a 735 if ((ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 10,
9e45cd41
BV
736 scpi_pps_receive_data, (void *)sdi)) != SR_OK)
737 return ret;
bee2b016 738 std_session_send_df_header(sdi);
88e4daa9 739 sr_sw_limits_acquisition_start(&devc->limits);
9e45cd41 740
ca1a7cb5
BV
741 return SR_OK;
742}
743
695dc859 744static int dev_acquisition_stop(struct sr_dev_inst *sdi)
ca1a7cb5 745{
9e45cd41 746 struct sr_scpi_dev_inst *scpi;
9e45cd41 747
9e45cd41
BV
748 scpi = sdi->conn;
749
9e45cd41
BV
750 sr_scpi_source_remove(sdi->session, scpi);
751
bee2b016 752 std_session_send_df_end(sdi);
bf48cceb 753
ca1a7cb5
BV
754 return SR_OK;
755}
756
dd5c48a6 757static struct sr_dev_driver scpi_pps_driver_info = {
ca1a7cb5
BV
758 .name = "scpi-pps",
759 .longname = "SCPI PPS",
760 .api_version = 1,
c2fdcc25 761 .init = std_init,
700d6b64 762 .cleanup = std_cleanup,
c2af709b
AG
763 .scan = scan_scpi_pps,
764 .dev_list = std_dev_list,
765 .dev_clear = dev_clear,
766 .config_get = config_get,
767 .config_set = config_set,
768 .config_list = config_list,
769 .dev_open = dev_open,
770 .dev_close = dev_close,
771 .dev_acquisition_start = dev_acquisition_start,
772 .dev_acquisition_stop = dev_acquisition_stop,
773 .context = NULL,
774};
775
776static struct sr_dev_driver hp_ib_pps_driver_info = {
777 .name = "hpib-pps",
778 .longname = "HP-IB PPS",
779 .api_version = 1,
780 .init = std_init,
781 .cleanup = std_cleanup,
782 .scan = scan_hpib_pps,
c01bf34c 783 .dev_list = std_dev_list,
ca1a7cb5
BV
784 .dev_clear = dev_clear,
785 .config_get = config_get,
786 .config_set = config_set,
787 .config_list = config_list,
788 .dev_open = dev_open,
789 .dev_close = dev_close,
790 .dev_acquisition_start = dev_acquisition_start,
791 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 792 .context = NULL,
ca1a7cb5 793};
dd5c48a6 794SR_REGISTER_DEV_DRIVER(scpi_pps_driver_info);
c2af709b 795SR_REGISTER_DEV_DRIVER(hp_ib_pps_driver_info);