]> sigrok.org Git - libsigrok.git/blame - src/hardware/scpi-pps/api.c
output/csv: use intermediate time_t var, silence compiler warning
[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;
aec7d855
MS
440 case SR_CONF_OVER_CURRENT_PROTECTION_DELAY:
441 gvtype = G_VARIANT_TYPE_DOUBLE;
442 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_DELAY;
443 break;
478c8d92 444 case SR_CONF_OVER_TEMPERATURE_PROTECTION:
bd5f0a14
FS
445 if (devc->device->dialect == SCPI_DIALECT_HMP) {
446 /* OTP is always enabled. */
447 *data = g_variant_new_boolean(TRUE);
448 return 0;
449 }
478c8d92
BV
450 gvtype = G_VARIANT_TYPE_BOOLEAN;
451 cmd = SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION;
452 break;
8b5eadf4 453 case SR_CONF_OVER_TEMPERATURE_PROTECTION_ACTIVE:
f083ae63 454 if (devc->device->dialect == SCPI_DIALECT_HP_66XXB ||
bd5f0a14
FS
455 devc->device->dialect == SCPI_DIALECT_HP_COMP ||
456 devc->device->dialect == SCPI_DIALECT_HMP)
8b5eadf4
FS
457 gvtype = G_VARIANT_TYPE_STRING;
458 else
459 gvtype = G_VARIANT_TYPE_BOOLEAN;
460 cmd = SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION_ACTIVE;
461 break;
7a0b98b5 462 case SR_CONF_REGULATION:
60475cd7
BV
463 gvtype = G_VARIANT_TYPE_STRING;
464 cmd = SCPI_CMD_GET_OUTPUT_REGULATION;
755793e9 465 break;
88e4daa9
ML
466 default:
467 return sr_sw_limits_config_get(&devc->limits, key, data);
478c8d92 468 }
91ef511d
BV
469 if (!gvtype)
470 return SR_ERR_NA;
471
17a82e83
FS
472 channel_group_cmd = 0;
473 channel_group_name = NULL;
474 if (cg) {
475 channel_group_cmd = SCPI_CMD_SELECT_CHANNEL;
476 channel_group_name = g_strdup(cg->name);
477 }
478
bd5f0a14
FS
479 is_hmp_sqii = FALSE;
480 is_hmp_sqii |= cmd == SCPI_CMD_GET_OUTPUT_REGULATION;
481 is_hmp_sqii |= cmd == SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION_ACTIVE;
482 is_hmp_sqii &= devc->device->dialect == SCPI_DIALECT_HMP;
483 if (is_hmp_sqii) {
484 if (!cg) {
485 /* STAT:QUES:INST:ISUMx query requires channel spec. */
486 sr_err("Need a channel group for regulation or OTP-active query.");
487 return SR_ERR_NA;
488 }
489 ret = sr_scpi_cmd_resp(sdi, devc->device->commands,
490 0, NULL, data, gvtype, cmd, channel_group_name);
491 } else {
492 ret = sr_scpi_cmd_resp(sdi, devc->device->commands,
493 channel_group_cmd, channel_group_name, data, gvtype, cmd);
494 }
17a82e83 495 g_free(channel_group_name);
91ef511d 496
43ff1110
FS
497 /*
498 * Handle special cases
499 */
500
91ef511d 501 if (cmd == SCPI_CMD_GET_OUTPUT_REGULATION) {
43ff1110
FS
502 if (devc->device->dialect == SCPI_DIALECT_PHILIPS) {
503 /*
504 * The Philips PM2800 series returns VOLT/CURR. We always return
505 * a GVariant string in the Rigol notation (CV/CC/UR).
506 */
507 s = g_variant_get_string(*data, NULL);
508 if (!g_strcmp0(s, "VOLT")) {
509 g_variant_unref(*data);
510 *data = g_variant_new_string("CV");
511 } else if (!g_strcmp0(s, "CURR")) {
512 g_variant_unref(*data);
513 *data = g_variant_new_string("CC");
514 }
515 }
0ad7074c
FS
516 if (devc->device->dialect == SCPI_DIALECT_HP_COMP) {
517 /* Evaluate Status Register from a HP 66xx in COMP mode. */
518 s = g_variant_get_string(*data, NULL);
519 sr_atoi(s, &reg);
520 g_variant_unref(*data);
521 if (reg & (1 << 0))
522 *data = g_variant_new_string("CV");
523 else if (reg & (1 << 1))
524 *data = g_variant_new_string("CC");
525 else if (reg & (1 << 2))
526 *data = g_variant_new_string("UR");
527 else if (reg & (1 << 9))
528 *data = g_variant_new_string("CC-");
529 else
530 *data = g_variant_new_string("");
531 }
43ff1110
FS
532 if (devc->device->dialect == SCPI_DIALECT_HP_66XXB) {
533 /* Evaluate Operational Status Register from a HP 66xxB. */
534 s = g_variant_get_string(*data, NULL);
8b5eadf4 535 sr_atoi(s, &reg);
069d9f25 536 g_variant_unref(*data);
8b5eadf4 537 if (reg & (1 << 8))
43ff1110 538 *data = g_variant_new_string("CV");
8b5eadf4 539 else if (reg & (1 << 10))
43ff1110 540 *data = g_variant_new_string("CC");
8b5eadf4 541 else if (reg & (1 << 11))
43ff1110
FS
542 *data = g_variant_new_string("CC-");
543 else
544 *data = g_variant_new_string("UR");
069d9f25 545 }
bd5f0a14
FS
546 if (devc->device->dialect == SCPI_DIALECT_HMP) {
547 /* Evaluate Condition Status Register from a HMP series device. */
548 s = g_variant_get_string(*data, NULL);
549 sr_atoi(s, &reg);
550 g_variant_unref(*data);
551 if (reg & (1 << 0))
552 *data = g_variant_new_string("CC");
553 else if (reg & (1 << 1))
554 *data = g_variant_new_string("CV");
555 else
556 *data = g_variant_new_string("UR");
557 }
069d9f25
AJ
558
559 s = g_variant_get_string(*data, NULL);
0ad7074c
FS
560 if (g_strcmp0(s, "CV") && g_strcmp0(s, "CC") && g_strcmp0(s, "CC-") &&
561 g_strcmp0(s, "UR") && g_strcmp0(s, "")) {
43ff1110
FS
562
563 sr_err("Unknown response to SCPI_CMD_GET_OUTPUT_REGULATION: %s", s);
91ef511d
BV
564 ret = SR_ERR_DATA;
565 }
91ef511d 566 }
ca1a7cb5 567
8b5eadf4 568 if (cmd == SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ACTIVE) {
f083ae63
FS
569 if (devc->device->dialect == SCPI_DIALECT_HP_COMP) {
570 /* Evaluate Status Register from a HP 66xx in COMP mode. */
571 s = g_variant_get_string(*data, NULL);
572 sr_atoi(s, &reg);
573 g_variant_unref(*data);
574 *data = g_variant_new_boolean(reg & (1 << 3));
575 }
8b5eadf4
FS
576 if (devc->device->dialect == SCPI_DIALECT_HP_66XXB) {
577 /* Evaluate Questionable Status Register bit 0 from a HP 66xxB. */
578 s = g_variant_get_string(*data, NULL);
579 sr_atoi(s, &reg);
580 g_variant_unref(*data);
581 *data = g_variant_new_boolean(reg & (1 << 0));
582 }
583 }
584
585 if (cmd == SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ACTIVE) {
f083ae63
FS
586 if (devc->device->dialect == SCPI_DIALECT_HP_COMP) {
587 /* Evaluate Status Register from a HP 66xx in COMP mode. */
588 s = g_variant_get_string(*data, NULL);
589 sr_atoi(s, &reg);
590 g_variant_unref(*data);
591 *data = g_variant_new_boolean(reg & (1 << 6));
592 }
8b5eadf4
FS
593 if (devc->device->dialect == SCPI_DIALECT_HP_66XXB) {
594 /* Evaluate Questionable Status Register bit 1 from a HP 66xxB. */
595 s = g_variant_get_string(*data, NULL);
596 sr_atoi(s, &reg);
597 g_variant_unref(*data);
598 *data = g_variant_new_boolean(reg & (1 << 1));
599 }
600 }
601
602 if (cmd == SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION_ACTIVE) {
f083ae63
FS
603 if (devc->device->dialect == SCPI_DIALECT_HP_COMP) {
604 /* Evaluate Status Register from a HP 66xx in COMP mode. */
605 s = g_variant_get_string(*data, NULL);
606 sr_atoi(s, &reg);
607 g_variant_unref(*data);
608 *data = g_variant_new_boolean(reg & (1 << 4));
609 }
bd5f0a14
FS
610 if (devc->device->dialect == SCPI_DIALECT_HP_66XXB ||
611 devc->device->dialect == SCPI_DIALECT_HMP) {
8b5eadf4
FS
612 /* Evaluate Questionable Status Register bit 4 from a HP 66xxB. */
613 s = g_variant_get_string(*data, NULL);
614 sr_atoi(s, &reg);
615 g_variant_unref(*data);
616 *data = g_variant_new_boolean(reg & (1 << 4));
617 }
618 }
619
ca1a7cb5
BV
620 return ret;
621}
622
dd7a72ea
UH
623static int config_set(uint32_t key, GVariant *data,
624 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
ca1a7cb5 625{
91ef511d 626 struct dev_context *devc;
9e45cd41 627 double d;
17a82e83
FS
628 int channel_group_cmd;
629 char *channel_group_name;
630 int ret;
ca1a7cb5 631
fdedbfcd
BV
632 if (!sdi)
633 return SR_ERR_ARG;
634
17a82e83
FS
635 channel_group_cmd = 0;
636 channel_group_name = NULL;
637 if (cg) {
638 channel_group_cmd = SCPI_CMD_SELECT_CHANNEL;
639 channel_group_name = g_strdup(cg->name);
640 }
60475cd7 641
91ef511d 642 devc = sdi->priv;
e57057ae 643
60475cd7 644 switch (key) {
7a0b98b5 645 case SR_CONF_ENABLED:
60475cd7 646 if (g_variant_get_boolean(data))
17a82e83
FS
647 ret = sr_scpi_cmd(sdi, devc->device->commands,
648 channel_group_cmd, channel_group_name,
91ef511d 649 SCPI_CMD_SET_OUTPUT_ENABLE);
60475cd7 650 else
17a82e83
FS
651 ret = sr_scpi_cmd(sdi, devc->device->commands,
652 channel_group_cmd, channel_group_name,
91ef511d 653 SCPI_CMD_SET_OUTPUT_DISABLE);
60475cd7 654 break;
7a0b98b5 655 case SR_CONF_VOLTAGE_TARGET:
60475cd7 656 d = g_variant_get_double(data);
17a82e83
FS
657 ret = sr_scpi_cmd(sdi, devc->device->commands,
658 channel_group_cmd, channel_group_name,
91ef511d 659 SCPI_CMD_SET_VOLTAGE_TARGET, d);
60475cd7 660 break;
4264f1c0
AG
661 case SR_CONF_OUTPUT_FREQUENCY_TARGET:
662 d = g_variant_get_double(data);
17a82e83
FS
663 ret = sr_scpi_cmd(sdi, devc->device->commands,
664 channel_group_cmd, channel_group_name,
91ef511d 665 SCPI_CMD_SET_FREQUENCY_TARGET, d);
4264f1c0 666 break;
7a0b98b5 667 case SR_CONF_CURRENT_LIMIT:
60475cd7 668 d = g_variant_get_double(data);
17a82e83
FS
669 ret = sr_scpi_cmd(sdi, devc->device->commands,
670 channel_group_cmd, channel_group_name,
91ef511d 671 SCPI_CMD_SET_CURRENT_LIMIT, d);
60475cd7
BV
672 break;
673 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
674 if (g_variant_get_boolean(data))
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_ENABLE);
60475cd7 678 else
17a82e83
FS
679 ret = sr_scpi_cmd(sdi, devc->device->commands,
680 channel_group_cmd, channel_group_name,
91ef511d 681 SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_DISABLE);
60475cd7
BV
682 break;
683 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
684 d = g_variant_get_double(data);
17a82e83
FS
685 ret = sr_scpi_cmd(sdi, devc->device->commands,
686 channel_group_cmd, channel_group_name,
91ef511d 687 SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_THRESHOLD, d);
60475cd7
BV
688 break;
689 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
690 if (g_variant_get_boolean(data))
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_ENABLE);
60475cd7 694 else
17a82e83
FS
695 ret = sr_scpi_cmd(sdi, devc->device->commands,
696 channel_group_cmd, channel_group_name,
91ef511d 697 SCPI_CMD_SET_OVER_CURRENT_PROTECTION_DISABLE);
60475cd7
BV
698 break;
699 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
700 d = g_variant_get_double(data);
17a82e83
FS
701 ret = sr_scpi_cmd(sdi, devc->device->commands,
702 channel_group_cmd, channel_group_name,
91ef511d 703 SCPI_CMD_SET_OVER_CURRENT_PROTECTION_THRESHOLD, d);
60475cd7 704 break;
aec7d855
MS
705 case SR_CONF_OVER_CURRENT_PROTECTION_DELAY:
706 d = g_variant_get_double(data);
707 ret = sr_scpi_cmd(sdi, devc->device->commands,
708 channel_group_cmd, channel_group_name,
709 SCPI_CMD_SET_OVER_CURRENT_PROTECTION_DELAY, d);
710 break;
60475cd7
BV
711 case SR_CONF_OVER_TEMPERATURE_PROTECTION:
712 if (g_variant_get_boolean(data))
17a82e83
FS
713 ret = sr_scpi_cmd(sdi, devc->device->commands,
714 channel_group_cmd, channel_group_name,
91ef511d 715 SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_ENABLE);
60475cd7 716 else
17a82e83
FS
717 ret = sr_scpi_cmd(sdi, devc->device->commands,
718 channel_group_cmd, channel_group_name,
91ef511d 719 SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_DISABLE);
60475cd7
BV
720 break;
721 default:
88e4daa9 722 ret = sr_sw_limits_config_set(&devc->limits, key, data);
ca1a7cb5
BV
723 }
724
17a82e83
FS
725 g_free(channel_group_name);
726
727 return ret;
ca1a7cb5
BV
728}
729
dd7a72ea
UH
730static int config_list(uint32_t key, GVariant **data,
731 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
ca1a7cb5 732{
9e45cd41
BV
733 struct dev_context *devc;
734 struct sr_channel *ch;
8f3c77db 735 struct pps_channel *pch;
329733d9 736 const struct channel_spec *ch_spec;
a9010323 737 int i;
9e45cd41
BV
738 const char *s[16];
739
e66d1892 740 devc = (sdi) ? sdi->priv : NULL;
ca1a7cb5 741
9e45cd41 742 if (!cg) {
9e45cd41 743 switch (key) {
e66d1892 744 case SR_CONF_SCAN_OPTIONS:
9e45cd41 745 case SR_CONF_DEVICE_OPTIONS:
e66d1892 746 return std_opts_config_list(key, data, sdi, cg,
53012da6
UH
747 ARRAY_AND_SIZE(scanopts),
748 ARRAY_AND_SIZE(drvopts),
08eba2d3
GS
749 (devc && devc->device) ? devc->device->devopts : NULL,
750 (devc && devc->device) ? devc->device->num_devopts : 0);
9e45cd41 751 break;
7a0b98b5 752 case SR_CONF_CHANNEL_CONFIG:
08eba2d3
GS
753 if (!devc || !devc->device)
754 return SR_ERR_ARG;
478c8d92 755 /* Not used. */
9e45cd41
BV
756 i = 0;
757 if (devc->device->features & PPS_INDEPENDENT)
758 s[i++] = "Independent";
759 if (devc->device->features & PPS_SERIES)
760 s[i++] = "Series";
761 if (devc->device->features & PPS_PARALLEL)
762 s[i++] = "Parallel";
763 if (i == 0) {
764 /*
765 * Shouldn't happen: independent-only devices
766 * shouldn't advertise this option at all.
767 */
768 return SR_ERR_NA;
769 }
770 *data = g_variant_new_strv(s, i);
771 break;
772 default:
773 return SR_ERR_NA;
774 }
775 } else {
9e45cd41
BV
776 /*
777 * Per-channel-group options depending on a channel are actually
778 * done with the first channel. Channel groups in PPS can have
779 * more than one channel, but they will typically be of equal
01b0257a 780 * specification for use in series or parallel mode.
9e45cd41 781 */
9e45cd41 782 ch = cg->channels->data;
8f3c77db 783 pch = ch->priv;
08eba2d3
GS
784 if (!devc || !devc->device)
785 return SR_ERR_ARG;
8f3c77db 786 ch_spec = &(devc->device->channels[pch->hw_output_idx]);
9e45cd41
BV
787
788 switch (key) {
789 case SR_CONF_DEVICE_OPTIONS:
105df674 790 *data = std_gvar_array_u32(devc->device->devopts_cg, devc->device->num_devopts_cg);
9e45cd41 791 break;
7a0b98b5 792 case SR_CONF_VOLTAGE_TARGET:
54d471f4 793 *data = std_gvar_min_max_step_array(ch_spec->voltage);
9e45cd41 794 break;
4264f1c0 795 case SR_CONF_OUTPUT_FREQUENCY_TARGET:
54d471f4 796 *data = std_gvar_min_max_step_array(ch_spec->frequency);
4264f1c0 797 break;
7a0b98b5 798 case SR_CONF_CURRENT_LIMIT:
54d471f4 799 *data = std_gvar_min_max_step_array(ch_spec->current);
9e45cd41 800 break;
49a468ed
FS
801 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
802 *data = std_gvar_min_max_step_array(ch_spec->ovp);
803 break;
804 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
805 *data = std_gvar_min_max_step_array(ch_spec->ocp);
806 break;
aec7d855
MS
807 case SR_CONF_OVER_CURRENT_PROTECTION_DELAY:
808 *data = std_gvar_min_max_step_array(ch_spec->ocp_delay);
809 break;
9e45cd41
BV
810 default:
811 return SR_ERR_NA;
812 }
ca1a7cb5
BV
813 }
814
a9010323 815 return SR_OK;
ca1a7cb5
BV
816}
817
695dc859 818static int dev_acquisition_start(const struct sr_dev_inst *sdi)
ca1a7cb5 819{
9e45cd41
BV
820 struct dev_context *devc;
821 struct sr_scpi_dev_inst *scpi;
fa2ce8c7 822 int ret;
ca1a7cb5 823
9e45cd41
BV
824 devc = sdi->priv;
825 scpi = sdi->conn;
9e45cd41 826
17a82e83
FS
827 /* Prime the pipe with the first channel. */
828 devc->cur_acquisition_channel = sr_next_enabled_channel(sdi, NULL);
829
fd243315
UH
830 /* Device specific initialization before acquisition starts. */
831 if (devc->device->init_acquisition)
832 devc->device->init_acquisition(sdi);
7e66bf05 833
01b0257a 834 if ((ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 10,
9e45cd41
BV
835 scpi_pps_receive_data, (void *)sdi)) != SR_OK)
836 return ret;
bee2b016 837 std_session_send_df_header(sdi);
88e4daa9 838 sr_sw_limits_acquisition_start(&devc->limits);
9e45cd41 839
ca1a7cb5
BV
840 return SR_OK;
841}
842
695dc859 843static int dev_acquisition_stop(struct sr_dev_inst *sdi)
ca1a7cb5 844{
9e45cd41 845 struct sr_scpi_dev_inst *scpi;
9e45cd41 846
9e45cd41
BV
847 scpi = sdi->conn;
848
9e45cd41
BV
849 sr_scpi_source_remove(sdi->session, scpi);
850
bee2b016 851 std_session_send_df_end(sdi);
bf48cceb 852
ca1a7cb5
BV
853 return SR_OK;
854}
855
dd5c48a6 856static struct sr_dev_driver scpi_pps_driver_info = {
ca1a7cb5
BV
857 .name = "scpi-pps",
858 .longname = "SCPI PPS",
859 .api_version = 1,
c2fdcc25 860 .init = std_init,
700d6b64 861 .cleanup = std_cleanup,
c2af709b
AG
862 .scan = scan_scpi_pps,
863 .dev_list = std_dev_list,
864 .dev_clear = dev_clear,
865 .config_get = config_get,
866 .config_set = config_set,
867 .config_list = config_list,
868 .dev_open = dev_open,
869 .dev_close = dev_close,
870 .dev_acquisition_start = dev_acquisition_start,
871 .dev_acquisition_stop = dev_acquisition_stop,
872 .context = NULL,
873};
874
875static struct sr_dev_driver hp_ib_pps_driver_info = {
876 .name = "hpib-pps",
877 .longname = "HP-IB PPS",
878 .api_version = 1,
879 .init = std_init,
880 .cleanup = std_cleanup,
881 .scan = scan_hpib_pps,
c01bf34c 882 .dev_list = std_dev_list,
ca1a7cb5
BV
883 .dev_clear = dev_clear,
884 .config_get = config_get,
885 .config_set = config_set,
886 .config_list = config_list,
887 .dev_open = dev_open,
888 .dev_close = dev_close,
889 .dev_acquisition_start = dev_acquisition_start,
890 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 891 .context = NULL,
ca1a7cb5 892};
dd5c48a6 893SR_REGISTER_DEV_DRIVER(scpi_pps_driver_info);
c2af709b 894SR_REGISTER_DEV_DRIVER(hp_ib_pps_driver_info);