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