]> sigrok.org Git - libsigrok.git/blame - src/hardware/scpi-pps/api.c
use common channel group allocation/release code
[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];
d810901a 148 cg = sr_channel_group_new(sdi, cgs->name, NULL);
9e45cd41
BV
149 for (j = 0, mask = 1; j < 64; j++, mask <<= 1) {
150 if (cgs->channel_index_mask & mask) {
01b0257a
BV
151 for (l = sdi->channels; l; l = l->next) {
152 ch = l->data;
153 pch = ch->priv;
f2bbcc33 154 /* Add mqflags from channel_group_spec only to voltage
b89e6db9 155 * and current channels.
f2bbcc33
FS
156 */
157 if (pch->mq == SR_MQ_VOLTAGE || pch->mq == SR_MQ_CURRENT)
158 pch->mqflags = cgs->mqflags;
159 else
160 pch->mqflags = 0;
01b0257a
BV
161 if (pch->hw_output_idx == j)
162 cg->channels = g_slist_append(cg->channels, ch);
163 }
9e45cd41
BV
164 }
165 }
166 pcg = g_malloc0(sizeof(struct pps_channel_group));
167 pcg->features = cgs->features;
168 cg->priv = pcg;
9e45cd41 169 }
ca1a7cb5 170
4a471029
BV
171 sr_scpi_hw_info_free(hw_info);
172 hw_info = NULL;
173
b89e6db9 174 /* Don't send SCPI_CMD_LOCAL for HP 66xxB using SCPI over GPIB. */
26e96658
FS
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);
ca1a7cb5 178
9e45cd41
BV
179 return sdi;
180}
ca1a7cb5 181
c2af709b 182static gchar *hpib_get_revision(struct sr_scpi_dev_inst *scpi)
9e45cd41 183{
c2af709b
AG
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 */
217static 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
238static 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
243static 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
248static 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
253static GSList *scan_hpib_pps(struct sr_dev_driver *di, GSList *options)
254{
7f046384
GS
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
c2af709b 266 return sr_scpi_scan(di->context, options, probe_hpib_pps_device);
ca1a7cb5
BV
267}
268
ca1a7cb5
BV
269static int dev_open(struct sr_dev_inst *sdi)
270{
ee2860ee 271 struct dev_context *devc;
9e45cd41 272 struct sr_scpi_dev_inst *scpi;
ee2860ee 273 GVariant *beeper;
9e45cd41 274
9e45cd41
BV
275 scpi = sdi->conn;
276 if (sr_scpi_open(scpi) < 0)
277 return SR_ERR;
ca1a7cb5 278
91ef511d 279 devc = sdi->priv;
26e96658 280
b89e6db9 281 /* Don't send SCPI_CMD_REMOTE for HP 66xxB using SCPI over GPIB. */
26e96658
FS
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
ee2860ee 286 devc->beeper_was_set = FALSE;
17a82e83
FS
287 if (sr_scpi_cmd_resp(sdi, devc->device->commands, 0, NULL,
288 &beeper, G_VARIANT_TYPE_BOOLEAN, SCPI_CMD_BEEPER) == SR_OK) {
ee2860ee
BV
289 if (g_variant_get_boolean(beeper)) {
290 devc->beeper_was_set = TRUE;
17a82e83
FS
291 sr_scpi_cmd(sdi, devc->device->commands,
292 0, NULL, SCPI_CMD_BEEPER_DISABLE);
ee2860ee
BV
293 }
294 g_variant_unref(beeper);
295 }
60475cd7 296
ca1a7cb5
BV
297 return SR_OK;
298}
299
300static int dev_close(struct sr_dev_inst *sdi)
301{
9e45cd41 302 struct sr_scpi_dev_inst *scpi;
ee2860ee 303 struct dev_context *devc;
ca1a7cb5 304
ee2860ee 305 devc = sdi->priv;
9e45cd41 306 scpi = sdi->conn;
ca1a7cb5 307
f1ba6b4b
UH
308 if (!scpi)
309 return SR_ERR_BUG;
310
311 if (devc->beeper_was_set)
17a82e83
FS
312 sr_scpi_cmd(sdi, devc->device->commands,
313 0, NULL, SCPI_CMD_BEEPER_ENABLE);
26e96658 314
b89e6db9 315 /* Don't send SCPI_CMD_LOCAL for HP 66xxB using SCPI over GPIB. */
26e96658
FS
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);
f1ba6b4b
UH
319
320 return sr_scpi_close(scpi);
ca1a7cb5
BV
321}
322
3553451f 323static void clear_helper(struct dev_context *devc)
4a471029 324{
4a471029
BV
325 g_free(devc->channels);
326 g_free(devc->channel_groups);
4a471029
BV
327}
328
1e726f56 329static int dev_clear(const struct sr_dev_driver *di)
ca1a7cb5 330{
3553451f 331 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
ca1a7cb5
BV
332}
333
dd7a72ea
UH
334static int config_get(uint32_t key, GVariant **data,
335 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
ca1a7cb5 336{
9e45cd41 337 struct dev_context *devc;
478c8d92
BV
338 const GVariantType *gvtype;
339 unsigned int i;
17a82e83
FS
340 int channel_group_cmd;
341 char *channel_group_name;
478c8d92 342 int cmd, ret;
069d9f25 343 const char *s;
8b5eadf4 344 int reg;
bd5f0a14 345 gboolean is_hmp_sqii;
ca1a7cb5 346
9e45cd41
BV
347 if (!sdi)
348 return SR_ERR_ARG;
349
350 devc = sdi->priv;
ca1a7cb5 351
478c8d92 352 if (cg) {
9e45cd41
BV
353 /*
354 * These options only apply to channel groups with a single
355 * channel -- they're per-channel settings for the device.
356 */
478c8d92
BV
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 }
478c8d92 370 }
9e45cd41 371
478c8d92
BV
372 gvtype = NULL;
373 cmd = -1;
374 switch (key) {
7a0b98b5 375 case SR_CONF_ENABLED:
478c8d92
BV
376 gvtype = G_VARIANT_TYPE_BOOLEAN;
377 cmd = SCPI_CMD_GET_OUTPUT_ENABLED;
378 break;
7a0b98b5 379 case SR_CONF_VOLTAGE:
478c8d92
BV
380 gvtype = G_VARIANT_TYPE_DOUBLE;
381 cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
382 break;
7a0b98b5 383 case SR_CONF_VOLTAGE_TARGET:
478c8d92 384 gvtype = G_VARIANT_TYPE_DOUBLE;
ca95e90f 385 cmd = SCPI_CMD_GET_VOLTAGE_TARGET;
478c8d92 386 break;
4264f1c0
AG
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;
7a0b98b5 395 case SR_CONF_CURRENT:
478c8d92
BV
396 gvtype = G_VARIANT_TYPE_DOUBLE;
397 cmd = SCPI_CMD_GET_MEAS_CURRENT;
398 break;
7a0b98b5 399 case SR_CONF_CURRENT_LIMIT:
478c8d92 400 gvtype = G_VARIANT_TYPE_DOUBLE;
ca95e90f 401 cmd = SCPI_CMD_GET_CURRENT_LIMIT;
478c8d92
BV
402 break;
403 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
bd5f0a14
FS
404 if (devc->device->dialect == SCPI_DIALECT_HMP) {
405 /* OVP is always enabled. */
406 *data = g_variant_new_boolean(TRUE);
407 return 0;
408 }
478c8d92
BV
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:
f083ae63
FS
413 if (devc->device->dialect == SCPI_DIALECT_HP_66XXB ||
414 devc->device->dialect == SCPI_DIALECT_HP_COMP)
8b5eadf4
FS
415 gvtype = G_VARIANT_TYPE_STRING;
416 else
417 gvtype = G_VARIANT_TYPE_BOOLEAN;
478c8d92
BV
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:
f083ae63
FS
429 if (devc->device->dialect == SCPI_DIALECT_HP_66XXB ||
430 devc->device->dialect == SCPI_DIALECT_HP_COMP)
8b5eadf4
FS
431 gvtype = G_VARIANT_TYPE_STRING;
432 else
433 gvtype = G_VARIANT_TYPE_BOOLEAN;
478c8d92
BV
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:
bd5f0a14
FS
441 if (devc->device->dialect == SCPI_DIALECT_HMP) {
442 /* OTP is always enabled. */
443 *data = g_variant_new_boolean(TRUE);
444 return 0;
445 }
478c8d92
BV
446 gvtype = G_VARIANT_TYPE_BOOLEAN;
447 cmd = SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION;
448 break;
8b5eadf4 449 case SR_CONF_OVER_TEMPERATURE_PROTECTION_ACTIVE:
f083ae63 450 if (devc->device->dialect == SCPI_DIALECT_HP_66XXB ||
bd5f0a14
FS
451 devc->device->dialect == SCPI_DIALECT_HP_COMP ||
452 devc->device->dialect == SCPI_DIALECT_HMP)
8b5eadf4
FS
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;
7a0b98b5 458 case SR_CONF_REGULATION:
60475cd7
BV
459 gvtype = G_VARIANT_TYPE_STRING;
460 cmd = SCPI_CMD_GET_OUTPUT_REGULATION;
755793e9 461 break;
88e4daa9
ML
462 default:
463 return sr_sw_limits_config_get(&devc->limits, key, data);
478c8d92 464 }
91ef511d
BV
465 if (!gvtype)
466 return SR_ERR_NA;
467
17a82e83
FS
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
bd5f0a14
FS
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 }
17a82e83 491 g_free(channel_group_name);
91ef511d 492
43ff1110
FS
493 /*
494 * Handle special cases
495 */
496
91ef511d 497 if (cmd == SCPI_CMD_GET_OUTPUT_REGULATION) {
43ff1110
FS
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 }
0ad7074c
FS
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, &reg);
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 }
43ff1110
FS
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);
8b5eadf4 531 sr_atoi(s, &reg);
069d9f25 532 g_variant_unref(*data);
8b5eadf4 533 if (reg & (1 << 8))
43ff1110 534 *data = g_variant_new_string("CV");
8b5eadf4 535 else if (reg & (1 << 10))
43ff1110 536 *data = g_variant_new_string("CC");
8b5eadf4 537 else if (reg & (1 << 11))
43ff1110
FS
538 *data = g_variant_new_string("CC-");
539 else
540 *data = g_variant_new_string("UR");
069d9f25 541 }
bd5f0a14
FS
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, &reg);
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 }
069d9f25
AJ
554
555 s = g_variant_get_string(*data, NULL);
0ad7074c
FS
556 if (g_strcmp0(s, "CV") && g_strcmp0(s, "CC") && g_strcmp0(s, "CC-") &&
557 g_strcmp0(s, "UR") && g_strcmp0(s, "")) {
43ff1110
FS
558
559 sr_err("Unknown response to SCPI_CMD_GET_OUTPUT_REGULATION: %s", s);
91ef511d
BV
560 ret = SR_ERR_DATA;
561 }
91ef511d 562 }
ca1a7cb5 563
8b5eadf4 564 if (cmd == SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ACTIVE) {
f083ae63
FS
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, &reg);
569 g_variant_unref(*data);
570 *data = g_variant_new_boolean(reg & (1 << 3));
571 }
8b5eadf4
FS
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, &reg);
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) {
f083ae63
FS
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, &reg);
586 g_variant_unref(*data);
587 *data = g_variant_new_boolean(reg & (1 << 6));
588 }
8b5eadf4
FS
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, &reg);
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) {
f083ae63
FS
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, &reg);
603 g_variant_unref(*data);
604 *data = g_variant_new_boolean(reg & (1 << 4));
605 }
bd5f0a14
FS
606 if (devc->device->dialect == SCPI_DIALECT_HP_66XXB ||
607 devc->device->dialect == SCPI_DIALECT_HMP) {
8b5eadf4
FS
608 /* Evaluate Questionable Status Register bit 4 from a HP 66xxB. */
609 s = g_variant_get_string(*data, NULL);
610 sr_atoi(s, &reg);
611 g_variant_unref(*data);
612 *data = g_variant_new_boolean(reg & (1 << 4));
613 }
614 }
615
ca1a7cb5
BV
616 return ret;
617}
618
dd7a72ea
UH
619static int config_set(uint32_t key, GVariant *data,
620 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
ca1a7cb5 621{
91ef511d 622 struct dev_context *devc;
9e45cd41 623 double d;
17a82e83
FS
624 int channel_group_cmd;
625 char *channel_group_name;
626 int ret;
ca1a7cb5 627
fdedbfcd
BV
628 if (!sdi)
629 return SR_ERR_ARG;
630
17a82e83
FS
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 }
60475cd7 637
91ef511d 638 devc = sdi->priv;
e57057ae 639
60475cd7 640 switch (key) {
7a0b98b5 641 case SR_CONF_ENABLED:
60475cd7 642 if (g_variant_get_boolean(data))
17a82e83
FS
643 ret = sr_scpi_cmd(sdi, devc->device->commands,
644 channel_group_cmd, channel_group_name,
91ef511d 645 SCPI_CMD_SET_OUTPUT_ENABLE);
60475cd7 646 else
17a82e83
FS
647 ret = sr_scpi_cmd(sdi, devc->device->commands,
648 channel_group_cmd, channel_group_name,
91ef511d 649 SCPI_CMD_SET_OUTPUT_DISABLE);
60475cd7 650 break;
7a0b98b5 651 case SR_CONF_VOLTAGE_TARGET:
60475cd7 652 d = g_variant_get_double(data);
17a82e83
FS
653 ret = sr_scpi_cmd(sdi, devc->device->commands,
654 channel_group_cmd, channel_group_name,
91ef511d 655 SCPI_CMD_SET_VOLTAGE_TARGET, d);
60475cd7 656 break;
4264f1c0
AG
657 case SR_CONF_OUTPUT_FREQUENCY_TARGET:
658 d = g_variant_get_double(data);
17a82e83
FS
659 ret = sr_scpi_cmd(sdi, devc->device->commands,
660 channel_group_cmd, channel_group_name,
91ef511d 661 SCPI_CMD_SET_FREQUENCY_TARGET, d);
4264f1c0 662 break;
7a0b98b5 663 case SR_CONF_CURRENT_LIMIT:
60475cd7 664 d = g_variant_get_double(data);
17a82e83
FS
665 ret = sr_scpi_cmd(sdi, devc->device->commands,
666 channel_group_cmd, channel_group_name,
91ef511d 667 SCPI_CMD_SET_CURRENT_LIMIT, d);
60475cd7
BV
668 break;
669 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
670 if (g_variant_get_boolean(data))
17a82e83
FS
671 ret = sr_scpi_cmd(sdi, devc->device->commands,
672 channel_group_cmd, channel_group_name,
91ef511d 673 SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_ENABLE);
60475cd7 674 else
17a82e83
FS
675 ret = sr_scpi_cmd(sdi, devc->device->commands,
676 channel_group_cmd, channel_group_name,
91ef511d 677 SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_DISABLE);
60475cd7
BV
678 break;
679 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
680 d = g_variant_get_double(data);
17a82e83
FS
681 ret = sr_scpi_cmd(sdi, devc->device->commands,
682 channel_group_cmd, channel_group_name,
91ef511d 683 SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_THRESHOLD, d);
60475cd7
BV
684 break;
685 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
686 if (g_variant_get_boolean(data))
17a82e83
FS
687 ret = sr_scpi_cmd(sdi, devc->device->commands,
688 channel_group_cmd, channel_group_name,
91ef511d 689 SCPI_CMD_SET_OVER_CURRENT_PROTECTION_ENABLE);
60475cd7 690 else
17a82e83
FS
691 ret = sr_scpi_cmd(sdi, devc->device->commands,
692 channel_group_cmd, channel_group_name,
91ef511d 693 SCPI_CMD_SET_OVER_CURRENT_PROTECTION_DISABLE);
60475cd7
BV
694 break;
695 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
696 d = g_variant_get_double(data);
17a82e83
FS
697 ret = sr_scpi_cmd(sdi, devc->device->commands,
698 channel_group_cmd, channel_group_name,
91ef511d 699 SCPI_CMD_SET_OVER_CURRENT_PROTECTION_THRESHOLD, d);
60475cd7
BV
700 break;
701 case SR_CONF_OVER_TEMPERATURE_PROTECTION:
702 if (g_variant_get_boolean(data))
17a82e83
FS
703 ret = sr_scpi_cmd(sdi, devc->device->commands,
704 channel_group_cmd, channel_group_name,
91ef511d 705 SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_ENABLE);
60475cd7 706 else
17a82e83
FS
707 ret = sr_scpi_cmd(sdi, devc->device->commands,
708 channel_group_cmd, channel_group_name,
91ef511d 709 SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_DISABLE);
60475cd7
BV
710 break;
711 default:
88e4daa9 712 ret = sr_sw_limits_config_set(&devc->limits, key, data);
ca1a7cb5
BV
713 }
714
17a82e83
FS
715 g_free(channel_group_name);
716
717 return ret;
ca1a7cb5
BV
718}
719
dd7a72ea
UH
720static int config_list(uint32_t key, GVariant **data,
721 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
ca1a7cb5 722{
9e45cd41
BV
723 struct dev_context *devc;
724 struct sr_channel *ch;
8f3c77db 725 struct pps_channel *pch;
329733d9 726 const struct channel_spec *ch_spec;
a9010323 727 int i;
9e45cd41
BV
728 const char *s[16];
729
e66d1892 730 devc = (sdi) ? sdi->priv : NULL;
ca1a7cb5 731
9e45cd41 732 if (!cg) {
9e45cd41 733 switch (key) {
e66d1892 734 case SR_CONF_SCAN_OPTIONS:
9e45cd41 735 case SR_CONF_DEVICE_OPTIONS:
e66d1892 736 return std_opts_config_list(key, data, sdi, cg,
53012da6
UH
737 ARRAY_AND_SIZE(scanopts),
738 ARRAY_AND_SIZE(drvopts),
08eba2d3
GS
739 (devc && devc->device) ? devc->device->devopts : NULL,
740 (devc && devc->device) ? devc->device->num_devopts : 0);
9e45cd41 741 break;
7a0b98b5 742 case SR_CONF_CHANNEL_CONFIG:
08eba2d3
GS
743 if (!devc || !devc->device)
744 return SR_ERR_ARG;
478c8d92 745 /* Not used. */
9e45cd41
BV
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 {
9e45cd41
BV
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
01b0257a 770 * specification for use in series or parallel mode.
9e45cd41 771 */
9e45cd41 772 ch = cg->channels->data;
8f3c77db 773 pch = ch->priv;
08eba2d3
GS
774 if (!devc || !devc->device)
775 return SR_ERR_ARG;
8f3c77db 776 ch_spec = &(devc->device->channels[pch->hw_output_idx]);
9e45cd41
BV
777
778 switch (key) {
779 case SR_CONF_DEVICE_OPTIONS:
105df674 780 *data = std_gvar_array_u32(devc->device->devopts_cg, devc->device->num_devopts_cg);
9e45cd41 781 break;
7a0b98b5 782 case SR_CONF_VOLTAGE_TARGET:
54d471f4 783 *data = std_gvar_min_max_step_array(ch_spec->voltage);
9e45cd41 784 break;
4264f1c0 785 case SR_CONF_OUTPUT_FREQUENCY_TARGET:
54d471f4 786 *data = std_gvar_min_max_step_array(ch_spec->frequency);
4264f1c0 787 break;
7a0b98b5 788 case SR_CONF_CURRENT_LIMIT:
54d471f4 789 *data = std_gvar_min_max_step_array(ch_spec->current);
9e45cd41 790 break;
49a468ed
FS
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;
9e45cd41
BV
797 default:
798 return SR_ERR_NA;
799 }
ca1a7cb5
BV
800 }
801
a9010323 802 return SR_OK;
ca1a7cb5
BV
803}
804
695dc859 805static int dev_acquisition_start(const struct sr_dev_inst *sdi)
ca1a7cb5 806{
9e45cd41
BV
807 struct dev_context *devc;
808 struct sr_scpi_dev_inst *scpi;
fa2ce8c7 809 int ret;
ca1a7cb5 810
9e45cd41
BV
811 devc = sdi->priv;
812 scpi = sdi->conn;
9e45cd41 813
17a82e83
FS
814 /* Prime the pipe with the first channel. */
815 devc->cur_acquisition_channel = sr_next_enabled_channel(sdi, NULL);
816
fd243315
UH
817 /* Device specific initialization before acquisition starts. */
818 if (devc->device->init_acquisition)
819 devc->device->init_acquisition(sdi);
7e66bf05 820
01b0257a 821 if ((ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 10,
9e45cd41
BV
822 scpi_pps_receive_data, (void *)sdi)) != SR_OK)
823 return ret;
bee2b016 824 std_session_send_df_header(sdi);
88e4daa9 825 sr_sw_limits_acquisition_start(&devc->limits);
9e45cd41 826
ca1a7cb5
BV
827 return SR_OK;
828}
829
695dc859 830static int dev_acquisition_stop(struct sr_dev_inst *sdi)
ca1a7cb5 831{
9e45cd41 832 struct sr_scpi_dev_inst *scpi;
9e45cd41 833
9e45cd41
BV
834 scpi = sdi->conn;
835
9e45cd41
BV
836 sr_scpi_source_remove(sdi->session, scpi);
837
bee2b016 838 std_session_send_df_end(sdi);
bf48cceb 839
ca1a7cb5
BV
840 return SR_OK;
841}
842
dd5c48a6 843static struct sr_dev_driver scpi_pps_driver_info = {
ca1a7cb5
BV
844 .name = "scpi-pps",
845 .longname = "SCPI PPS",
846 .api_version = 1,
c2fdcc25 847 .init = std_init,
700d6b64 848 .cleanup = std_cleanup,
c2af709b
AG
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
862static 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,
c01bf34c 869 .dev_list = std_dev_list,
ca1a7cb5
BV
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,
41812aca 878 .context = NULL,
ca1a7cb5 879};
dd5c48a6 880SR_REGISTER_DEV_DRIVER(scpi_pps_driver_info);
c2af709b 881SR_REGISTER_DEV_DRIVER(hp_ib_pps_driver_info);