]> sigrok.org Git - libsigrok.git/blame - src/hardware/scpi-pps/api.c
scpi-pps: Reimplemention of switching channel groups (PSU channels)
[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>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
6ec6c43b 20#include <config.h>
9e45cd41 21#include <string.h>
ba464a12 22#include <strings.h>
91ef511d 23#include "scpi.h"
ca1a7cb5
BV
24#include "protocol.h"
25
dd5c48a6 26static struct sr_dev_driver scpi_pps_driver_info;
c2af709b 27static struct sr_dev_driver hp_ib_pps_driver_info;
9e45cd41 28
584560f1 29static const uint32_t scanopts[] = {
9e45cd41
BV
30 SR_CONF_CONN,
31 SR_CONF_SERIALCOMM,
32};
ca1a7cb5 33
9d9cf1c4 34static const uint32_t drvopts[] = {
a258204e 35 SR_CONF_POWER_SUPPLY,
a258204e
BV
36};
37
329733d9 38static const struct pps_channel_instance pci[] = {
01b0257a
BV
39 { SR_MQ_VOLTAGE, SCPI_CMD_GET_MEAS_VOLTAGE, "V" },
40 { SR_MQ_CURRENT, SCPI_CMD_GET_MEAS_CURRENT, "I" },
41 { SR_MQ_POWER, SCPI_CMD_GET_MEAS_POWER, "P" },
4264f1c0 42 { SR_MQ_FREQUENCY, SCPI_CMD_GET_MEAS_FREQUENCY, "F" },
01b0257a
BV
43};
44
c2af709b 45static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi,
d9251a2c
UH
46 int (*get_hw_id)(struct sr_scpi_dev_inst *scpi,
47 struct sr_scpi_hw_info **scpi_response))
ca1a7cb5 48{
9e45cd41
BV
49 struct dev_context *devc;
50 struct sr_dev_inst *sdi;
51 struct sr_scpi_hw_info *hw_info;
52 struct sr_channel_group *cg;
53 struct sr_channel *ch;
54 const struct scpi_pps *device;
01b0257a 55 struct pps_channel *pch;
4a471029
BV
56 struct channel_spec *channels;
57 struct channel_group_spec *channel_groups, *cgs;
9e45cd41 58 struct pps_channel_group *pcg;
58b77c41
BV
59 GRegex *model_re;
60 GMatchInfo *model_mi;
01b0257a 61 GSList *l;
9e45cd41 62 uint64_t mask;
4a471029
BV
63 unsigned int num_channels, num_channel_groups, ch_num, ch_idx, i, j;
64 int ret;
22c18b03 65 const char *vendor;
01b0257a 66 char ch_name[16];
9e45cd41 67
c2af709b 68 if (get_hw_id(scpi, &hw_info) != SR_OK) {
9e45cd41
BV
69 sr_info("Couldn't get IDN response.");
70 return NULL;
71 }
72
73 device = NULL;
74 for (i = 0; i < num_pps_profiles; i++) {
91ef511d 75 vendor = sr_vendor_alias(hw_info->manufacturer);
34577da6 76 if (g_ascii_strcasecmp(vendor, pps_profiles[i].vendor))
22c18b03 77 continue;
58b77c41
BV
78 model_re = g_regex_new(pps_profiles[i].model, 0, 0, NULL);
79 if (g_regex_match(model_re, hw_info->model, 0, &model_mi))
9e45cd41 80 device = &pps_profiles[i];
58b77c41
BV
81 g_match_info_unref(model_mi);
82 g_regex_unref(model_re);
83 if (device)
9e45cd41 84 break;
9e45cd41
BV
85 }
86 if (!device) {
87 sr_scpi_hw_info_free(hw_info);
88 return NULL;
89 }
90
aac29cc1 91 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
92 sdi->vendor = g_strdup(vendor);
93 sdi->model = g_strdup(hw_info->model);
94 sdi->version = g_strdup(hw_info->firmware_version);
9e45cd41 95 sdi->conn = scpi;
4f840ce9 96 sdi->driver = &scpi_pps_driver_info;
9e45cd41 97 sdi->inst_type = SR_INST_SCPI;
d08667c5
SA
98 sdi->serial_num = g_strdup(hw_info->serial_number);
99
9e45cd41
BV
100 devc = g_malloc0(sizeof(struct dev_context));
101 devc->device = device;
102 sdi->priv = devc;
103
4a471029
BV
104 if (device->num_channels) {
105 /* Static channels and groups. */
329733d9 106 channels = (struct channel_spec *)device->channels;
4a471029 107 num_channels = device->num_channels;
329733d9 108 channel_groups = (struct channel_group_spec *)device->channel_groups;
4a471029
BV
109 num_channel_groups = device->num_channel_groups;
110 } else {
111 /* Channels and groups need to be probed. */
112 ret = device->probe_channels(sdi, hw_info, &channels, &num_channels,
113 &channel_groups, &num_channel_groups);
114 if (ret != SR_OK) {
115 sr_err("Failed to probe for channels.");
116 return NULL;
117 }
118 /*
119 * Since these were dynamically allocated, we'll need to free them
120 * later.
121 */
122 devc->channels = channels;
123 devc->channel_groups = channel_groups;
124 }
125
01b0257a 126 ch_idx = 0;
4a471029 127 for (ch_num = 0; ch_num < num_channels; ch_num++) {
01b0257a 128 /* Create one channel per measurable output unit. */
01b0257a 129 for (i = 0; i < ARRAY_SIZE(pci); i++) {
fa2ce8c7 130 if (!sr_scpi_cmd_get(devc->device->commands, pci[i].command))
01b0257a
BV
131 continue;
132 g_snprintf(ch_name, 16, "%s%s", pci[i].prefix,
4a471029 133 channels[ch_num].name);
5e23fcab
ML
134 ch = sr_channel_new(sdi, ch_idx++, SR_CHANNEL_ANALOG, TRUE,
135 ch_name);
01b0257a
BV
136 pch = g_malloc0(sizeof(struct pps_channel));
137 pch->hw_output_idx = ch_num;
4a471029 138 pch->hwname = channels[ch_num].name;
01b0257a
BV
139 pch->mq = pci[i].mq;
140 ch->priv = pch;
01b0257a 141 }
9e45cd41 142 }
ca1a7cb5 143
4a471029
BV
144 for (i = 0; i < num_channel_groups; i++) {
145 cgs = &channel_groups[i];
9e45cd41
BV
146 cg = g_malloc0(sizeof(struct sr_channel_group));
147 cg->name = g_strdup(cgs->name);
148 for (j = 0, mask = 1; j < 64; j++, mask <<= 1) {
149 if (cgs->channel_index_mask & mask) {
01b0257a
BV
150 for (l = sdi->channels; l; l = l->next) {
151 ch = l->data;
152 pch = ch->priv;
153 if (pch->hw_output_idx == j)
154 cg->channels = g_slist_append(cg->channels, ch);
155 }
9e45cd41
BV
156 }
157 }
158 pcg = g_malloc0(sizeof(struct pps_channel_group));
159 pcg->features = cgs->features;
160 cg->priv = pcg;
161 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
162 }
ca1a7cb5 163
4a471029
BV
164 sr_scpi_hw_info_free(hw_info);
165 hw_info = NULL;
166
17a82e83 167 sr_scpi_cmd(sdi, devc->device->commands, 0, NULL, SCPI_CMD_LOCAL);
ca1a7cb5 168
9e45cd41
BV
169 return sdi;
170}
ca1a7cb5 171
c2af709b 172static gchar *hpib_get_revision(struct sr_scpi_dev_inst *scpi)
9e45cd41 173{
c2af709b
AG
174 int ret;
175 gboolean matches;
176 char *response;
177 GRegex *version_regex;
178
179 ret = sr_scpi_get_string(scpi, "ROM?", &response);
180 if (ret != SR_OK && !response)
181 return NULL;
182
183 /* Example version string: "B01 B01" */
184 version_regex = g_regex_new("[A-Z][0-9]{2} [A-Z][0-9]{2}", 0, 0, NULL);
185 matches = g_regex_match(version_regex, response, 0, NULL);
186 g_regex_unref(version_regex);
187
188 if (!matches) {
189 /* Not a valid version string. Ignore it. */
190 g_free(response);
191 response = NULL;
192 } else {
193 /* Replace space with dot. */
194 response[3] = '.';
195 }
196
197 return response;
198}
199
200/*
201 * This function assumes the response is in the form "HP<model_number>"
202 *
203 * HP made many GPIB (then called HP-IB) instruments before the SCPI command
204 * set was introduced into the standard. We haven't seen any non-HP instruments
205 * which respond to the "ID?" query, so assume all are HP for now.
206 */
207static int hpib_get_hw_id(struct sr_scpi_dev_inst *scpi,
208 struct sr_scpi_hw_info **scpi_response)
209{
210 int ret;
211 char *response;
212 struct sr_scpi_hw_info *hw_info;
213
214 ret = sr_scpi_get_string(scpi, "ID?", &response);
215 if ((ret != SR_OK) || !response)
216 return SR_ERR;
217
218 hw_info = g_malloc0(sizeof(struct sr_scpi_hw_info));
219
220 *scpi_response = hw_info;
221 hw_info->model = response;
222 hw_info->firmware_version = hpib_get_revision(scpi);
223 hw_info->manufacturer = g_strdup("HP");
224
225 return SR_OK;
226}
227
228static struct sr_dev_inst *probe_scpi_pps_device(struct sr_scpi_dev_inst *scpi)
229{
230 return probe_device(scpi, sr_scpi_get_hw_id);
231}
232
233static struct sr_dev_inst *probe_hpib_pps_device(struct sr_scpi_dev_inst *scpi)
234{
235 return probe_device(scpi, hpib_get_hw_id);
236}
237
238static GSList *scan_scpi_pps(struct sr_dev_driver *di, GSList *options)
239{
240 return sr_scpi_scan(di->context, options, probe_scpi_pps_device);
241}
242
243static GSList *scan_hpib_pps(struct sr_dev_driver *di, GSList *options)
244{
245 return sr_scpi_scan(di->context, options, probe_hpib_pps_device);
ca1a7cb5
BV
246}
247
ca1a7cb5
BV
248static int dev_open(struct sr_dev_inst *sdi)
249{
ee2860ee 250 struct dev_context *devc;
9e45cd41 251 struct sr_scpi_dev_inst *scpi;
ee2860ee 252 GVariant *beeper;
9e45cd41 253
9e45cd41
BV
254 scpi = sdi->conn;
255 if (sr_scpi_open(scpi) < 0)
256 return SR_ERR;
ca1a7cb5 257
91ef511d 258 devc = sdi->priv;
17a82e83 259 sr_scpi_cmd(sdi, devc->device->commands, 0, NULL, SCPI_CMD_REMOTE);
ee2860ee 260 devc->beeper_was_set = FALSE;
17a82e83
FS
261 if (sr_scpi_cmd_resp(sdi, devc->device->commands, 0, NULL,
262 &beeper, G_VARIANT_TYPE_BOOLEAN, SCPI_CMD_BEEPER) == SR_OK) {
ee2860ee
BV
263 if (g_variant_get_boolean(beeper)) {
264 devc->beeper_was_set = TRUE;
17a82e83
FS
265 sr_scpi_cmd(sdi, devc->device->commands,
266 0, NULL, SCPI_CMD_BEEPER_DISABLE);
ee2860ee
BV
267 }
268 g_variant_unref(beeper);
269 }
60475cd7 270
ca1a7cb5
BV
271 return SR_OK;
272}
273
274static int dev_close(struct sr_dev_inst *sdi)
275{
9e45cd41 276 struct sr_scpi_dev_inst *scpi;
ee2860ee 277 struct dev_context *devc;
ca1a7cb5 278
ee2860ee 279 devc = sdi->priv;
9e45cd41 280 scpi = sdi->conn;
ca1a7cb5 281
f1ba6b4b
UH
282 if (!scpi)
283 return SR_ERR_BUG;
284
285 if (devc->beeper_was_set)
17a82e83
FS
286 sr_scpi_cmd(sdi, devc->device->commands,
287 0, NULL, SCPI_CMD_BEEPER_ENABLE);
288 sr_scpi_cmd(sdi, devc->device->commands, 0, NULL, SCPI_CMD_LOCAL);
f1ba6b4b
UH
289
290 return sr_scpi_close(scpi);
ca1a7cb5
BV
291}
292
3553451f 293static void clear_helper(struct dev_context *devc)
4a471029 294{
4a471029
BV
295 g_free(devc->channels);
296 g_free(devc->channel_groups);
4a471029
BV
297}
298
1e726f56 299static int dev_clear(const struct sr_dev_driver *di)
ca1a7cb5 300{
3553451f 301 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
ca1a7cb5
BV
302}
303
dd7a72ea
UH
304static int config_get(uint32_t key, GVariant **data,
305 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
ca1a7cb5 306{
9e45cd41 307 struct dev_context *devc;
478c8d92
BV
308 const GVariantType *gvtype;
309 unsigned int i;
17a82e83
FS
310 int channel_group_cmd;
311 char *channel_group_name;
478c8d92 312 int cmd, ret;
069d9f25 313 const char *s;
ca1a7cb5 314
9e45cd41
BV
315 if (!sdi)
316 return SR_ERR_ARG;
317
318 devc = sdi->priv;
ca1a7cb5 319
478c8d92 320 if (cg) {
9e45cd41
BV
321 /*
322 * These options only apply to channel groups with a single
323 * channel -- they're per-channel settings for the device.
324 */
478c8d92
BV
325
326 /*
327 * Config keys are handled below depending on whether a channel
328 * group was provided by the frontend. However some of these
329 * take a CG on one PPS but not on others. Check the device's
330 * profile for that here, and NULL out the channel group as needed.
331 */
332 for (i = 0; i < devc->device->num_devopts; i++) {
333 if (devc->device->devopts[i] == key) {
334 cg = NULL;
335 break;
336 }
337 }
478c8d92 338 }
9e45cd41 339
478c8d92
BV
340 gvtype = NULL;
341 cmd = -1;
342 switch (key) {
7a0b98b5 343 case SR_CONF_ENABLED:
478c8d92
BV
344 gvtype = G_VARIANT_TYPE_BOOLEAN;
345 cmd = SCPI_CMD_GET_OUTPUT_ENABLED;
346 break;
7a0b98b5 347 case SR_CONF_VOLTAGE:
478c8d92
BV
348 gvtype = G_VARIANT_TYPE_DOUBLE;
349 cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
350 break;
7a0b98b5 351 case SR_CONF_VOLTAGE_TARGET:
478c8d92 352 gvtype = G_VARIANT_TYPE_DOUBLE;
ca95e90f 353 cmd = SCPI_CMD_GET_VOLTAGE_TARGET;
478c8d92 354 break;
4264f1c0
AG
355 case SR_CONF_OUTPUT_FREQUENCY:
356 gvtype = G_VARIANT_TYPE_DOUBLE;
357 cmd = SCPI_CMD_GET_MEAS_FREQUENCY;
358 break;
359 case SR_CONF_OUTPUT_FREQUENCY_TARGET:
360 gvtype = G_VARIANT_TYPE_DOUBLE;
361 cmd = SCPI_CMD_GET_FREQUENCY_TARGET;
362 break;
7a0b98b5 363 case SR_CONF_CURRENT:
478c8d92
BV
364 gvtype = G_VARIANT_TYPE_DOUBLE;
365 cmd = SCPI_CMD_GET_MEAS_CURRENT;
366 break;
7a0b98b5 367 case SR_CONF_CURRENT_LIMIT:
478c8d92 368 gvtype = G_VARIANT_TYPE_DOUBLE;
ca95e90f 369 cmd = SCPI_CMD_GET_CURRENT_LIMIT;
478c8d92
BV
370 break;
371 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
372 gvtype = G_VARIANT_TYPE_BOOLEAN;
373 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ENABLED;
374 break;
375 case SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE:
376 gvtype = G_VARIANT_TYPE_BOOLEAN;
377 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ACTIVE;
378 break;
379 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
380 gvtype = G_VARIANT_TYPE_DOUBLE;
381 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_THRESHOLD;
382 break;
383 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
384 gvtype = G_VARIANT_TYPE_BOOLEAN;
385 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ENABLED;
386 break;
387 case SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE:
388 gvtype = G_VARIANT_TYPE_BOOLEAN;
389 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ACTIVE;
390 break;
391 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
392 gvtype = G_VARIANT_TYPE_DOUBLE;
393 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_THRESHOLD;
394 break;
395 case SR_CONF_OVER_TEMPERATURE_PROTECTION:
396 gvtype = G_VARIANT_TYPE_BOOLEAN;
397 cmd = SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION;
398 break;
7a0b98b5 399 case SR_CONF_REGULATION:
60475cd7
BV
400 gvtype = G_VARIANT_TYPE_STRING;
401 cmd = SCPI_CMD_GET_OUTPUT_REGULATION;
478c8d92 402 }
91ef511d
BV
403 if (!gvtype)
404 return SR_ERR_NA;
405
17a82e83
FS
406 channel_group_cmd = 0;
407 channel_group_name = NULL;
408 if (cg) {
409 channel_group_cmd = SCPI_CMD_SELECT_CHANNEL;
410 channel_group_name = g_strdup(cg->name);
411 }
412
413 ret = sr_scpi_cmd_resp(sdi, devc->device->commands,
414 channel_group_cmd, channel_group_name, data, gvtype, cmd);
415 g_free(channel_group_name);
91ef511d
BV
416
417 if (cmd == SCPI_CMD_GET_OUTPUT_REGULATION) {
418 /*
419 * The Rigol DP800 series return CV/CC/UR, Philips PM2800
420 * return VOLT/CURR. We always return a GVariant string in
421 * the Rigol notation.
422 */
d66c93cc 423 s = g_variant_get_string(*data, NULL);
069d9f25
AJ
424 if (!strcmp(s, "VOLT")) {
425 g_variant_unref(*data);
91ef511d 426 *data = g_variant_new_string("CV");
069d9f25
AJ
427 } else if (!strcmp(s, "CURR")) {
428 g_variant_unref(*data);
91ef511d 429 *data = g_variant_new_string("CC");
069d9f25
AJ
430 }
431
432 s = g_variant_get_string(*data, NULL);
433 if (strcmp(s, "CV") && strcmp(s, "CC") && strcmp(s, "UR")) {
91ef511d
BV
434 sr_dbg("Unknown response to SCPI_CMD_GET_OUTPUT_REGULATION: %s", s);
435 ret = SR_ERR_DATA;
436 }
91ef511d 437 }
ca1a7cb5
BV
438
439 return ret;
440}
441
dd7a72ea
UH
442static int config_set(uint32_t key, GVariant *data,
443 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
ca1a7cb5 444{
91ef511d 445 struct dev_context *devc;
9e45cd41 446 double d;
17a82e83
FS
447 int channel_group_cmd;
448 char *channel_group_name;
449 int ret;
ca1a7cb5 450
fdedbfcd
BV
451 if (!sdi)
452 return SR_ERR_ARG;
453
17a82e83
FS
454 channel_group_cmd = 0;
455 channel_group_name = NULL;
456 if (cg) {
457 channel_group_cmd = SCPI_CMD_SELECT_CHANNEL;
458 channel_group_name = g_strdup(cg->name);
459 }
60475cd7 460
91ef511d 461 devc = sdi->priv;
e57057ae 462
60475cd7 463 switch (key) {
7a0b98b5 464 case SR_CONF_ENABLED:
60475cd7 465 if (g_variant_get_boolean(data))
17a82e83
FS
466 ret = sr_scpi_cmd(sdi, devc->device->commands,
467 channel_group_cmd, channel_group_name,
91ef511d 468 SCPI_CMD_SET_OUTPUT_ENABLE);
60475cd7 469 else
17a82e83
FS
470 ret = sr_scpi_cmd(sdi, devc->device->commands,
471 channel_group_cmd, channel_group_name,
91ef511d 472 SCPI_CMD_SET_OUTPUT_DISABLE);
60475cd7 473 break;
7a0b98b5 474 case SR_CONF_VOLTAGE_TARGET:
60475cd7 475 d = g_variant_get_double(data);
17a82e83
FS
476 ret = sr_scpi_cmd(sdi, devc->device->commands,
477 channel_group_cmd, channel_group_name,
91ef511d 478 SCPI_CMD_SET_VOLTAGE_TARGET, d);
60475cd7 479 break;
4264f1c0
AG
480 case SR_CONF_OUTPUT_FREQUENCY_TARGET:
481 d = g_variant_get_double(data);
17a82e83
FS
482 ret = sr_scpi_cmd(sdi, devc->device->commands,
483 channel_group_cmd, channel_group_name,
91ef511d 484 SCPI_CMD_SET_FREQUENCY_TARGET, d);
4264f1c0 485 break;
7a0b98b5 486 case SR_CONF_CURRENT_LIMIT:
60475cd7 487 d = g_variant_get_double(data);
17a82e83
FS
488 ret = sr_scpi_cmd(sdi, devc->device->commands,
489 channel_group_cmd, channel_group_name,
91ef511d 490 SCPI_CMD_SET_CURRENT_LIMIT, d);
60475cd7
BV
491 break;
492 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
493 if (g_variant_get_boolean(data))
17a82e83
FS
494 ret = sr_scpi_cmd(sdi, devc->device->commands,
495 channel_group_cmd, channel_group_name,
91ef511d 496 SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_ENABLE);
60475cd7 497 else
17a82e83
FS
498 ret = sr_scpi_cmd(sdi, devc->device->commands,
499 channel_group_cmd, channel_group_name,
91ef511d 500 SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_DISABLE);
60475cd7
BV
501 break;
502 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
503 d = g_variant_get_double(data);
17a82e83
FS
504 ret = sr_scpi_cmd(sdi, devc->device->commands,
505 channel_group_cmd, channel_group_name,
91ef511d 506 SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_THRESHOLD, d);
60475cd7
BV
507 break;
508 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
509 if (g_variant_get_boolean(data))
17a82e83
FS
510 ret = sr_scpi_cmd(sdi, devc->device->commands,
511 channel_group_cmd, channel_group_name,
91ef511d 512 SCPI_CMD_SET_OVER_CURRENT_PROTECTION_ENABLE);
60475cd7 513 else
17a82e83
FS
514 ret = sr_scpi_cmd(sdi, devc->device->commands,
515 channel_group_cmd, channel_group_name,
91ef511d 516 SCPI_CMD_SET_OVER_CURRENT_PROTECTION_DISABLE);
60475cd7
BV
517 break;
518 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
519 d = g_variant_get_double(data);
17a82e83
FS
520 ret = sr_scpi_cmd(sdi, devc->device->commands,
521 channel_group_cmd, channel_group_name,
91ef511d 522 SCPI_CMD_SET_OVER_CURRENT_PROTECTION_THRESHOLD, d);
60475cd7
BV
523 break;
524 case SR_CONF_OVER_TEMPERATURE_PROTECTION:
525 if (g_variant_get_boolean(data))
17a82e83
FS
526 ret = sr_scpi_cmd(sdi, devc->device->commands,
527 channel_group_cmd, channel_group_name,
91ef511d 528 SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_ENABLE);
60475cd7 529 else
17a82e83
FS
530 ret = sr_scpi_cmd(sdi, devc->device->commands,
531 channel_group_cmd, channel_group_name,
91ef511d 532 SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_DISABLE);
60475cd7
BV
533 break;
534 default:
17a82e83 535 ret = SR_ERR_NA;
ca1a7cb5
BV
536 }
537
17a82e83
FS
538 g_free(channel_group_name);
539
540 return ret;
ca1a7cb5
BV
541}
542
dd7a72ea
UH
543static int config_list(uint32_t key, GVariant **data,
544 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
ca1a7cb5 545{
9e45cd41
BV
546 struct dev_context *devc;
547 struct sr_channel *ch;
329733d9 548 const struct channel_spec *ch_spec;
a9010323 549 int i;
9e45cd41
BV
550 const char *s[16];
551
e66d1892 552 devc = (sdi) ? sdi->priv : NULL;
ca1a7cb5 553
9e45cd41 554 if (!cg) {
9e45cd41 555 switch (key) {
e66d1892 556 case SR_CONF_SCAN_OPTIONS:
9e45cd41 557 case SR_CONF_DEVICE_OPTIONS:
e66d1892 558 return std_opts_config_list(key, data, sdi, cg,
53012da6
UH
559 ARRAY_AND_SIZE(scanopts),
560 ARRAY_AND_SIZE(drvopts),
08eba2d3
GS
561 (devc && devc->device) ? devc->device->devopts : NULL,
562 (devc && devc->device) ? devc->device->num_devopts : 0);
9e45cd41 563 break;
7a0b98b5 564 case SR_CONF_CHANNEL_CONFIG:
08eba2d3
GS
565 if (!devc || !devc->device)
566 return SR_ERR_ARG;
478c8d92 567 /* Not used. */
9e45cd41
BV
568 i = 0;
569 if (devc->device->features & PPS_INDEPENDENT)
570 s[i++] = "Independent";
571 if (devc->device->features & PPS_SERIES)
572 s[i++] = "Series";
573 if (devc->device->features & PPS_PARALLEL)
574 s[i++] = "Parallel";
575 if (i == 0) {
576 /*
577 * Shouldn't happen: independent-only devices
578 * shouldn't advertise this option at all.
579 */
580 return SR_ERR_NA;
581 }
582 *data = g_variant_new_strv(s, i);
583 break;
584 default:
585 return SR_ERR_NA;
586 }
587 } else {
9e45cd41
BV
588 /*
589 * Per-channel-group options depending on a channel are actually
590 * done with the first channel. Channel groups in PPS can have
591 * more than one channel, but they will typically be of equal
01b0257a 592 * specification for use in series or parallel mode.
9e45cd41 593 */
9e45cd41 594 ch = cg->channels->data;
08eba2d3
GS
595 if (!devc || !devc->device)
596 return SR_ERR_ARG;
54d471f4 597 ch_spec = &(devc->device->channels[ch->index]);
9e45cd41
BV
598
599 switch (key) {
600 case SR_CONF_DEVICE_OPTIONS:
105df674 601 *data = std_gvar_array_u32(devc->device->devopts_cg, devc->device->num_devopts_cg);
9e45cd41 602 break;
7a0b98b5 603 case SR_CONF_VOLTAGE_TARGET:
54d471f4 604 *data = std_gvar_min_max_step_array(ch_spec->voltage);
9e45cd41 605 break;
4264f1c0 606 case SR_CONF_OUTPUT_FREQUENCY_TARGET:
54d471f4 607 *data = std_gvar_min_max_step_array(ch_spec->frequency);
4264f1c0 608 break;
7a0b98b5 609 case SR_CONF_CURRENT_LIMIT:
54d471f4 610 *data = std_gvar_min_max_step_array(ch_spec->current);
9e45cd41
BV
611 break;
612 default:
613 return SR_ERR_NA;
614 }
ca1a7cb5
BV
615 }
616
a9010323 617 return SR_OK;
ca1a7cb5
BV
618}
619
695dc859 620static int dev_acquisition_start(const struct sr_dev_inst *sdi)
ca1a7cb5 621{
9e45cd41
BV
622 struct dev_context *devc;
623 struct sr_scpi_dev_inst *scpi;
fa2ce8c7 624 int ret;
ca1a7cb5 625
9e45cd41
BV
626 devc = sdi->priv;
627 scpi = sdi->conn;
9e45cd41 628
17a82e83
FS
629 /* Prime the pipe with the first channel. */
630 devc->cur_acquisition_channel = sr_next_enabled_channel(sdi, NULL);
631
01b0257a 632 if ((ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 10,
9e45cd41
BV
633 scpi_pps_receive_data, (void *)sdi)) != SR_OK)
634 return ret;
bee2b016 635 std_session_send_df_header(sdi);
9e45cd41 636
ca1a7cb5
BV
637 return SR_OK;
638}
639
695dc859 640static int dev_acquisition_stop(struct sr_dev_inst *sdi)
ca1a7cb5 641{
9e45cd41 642 struct sr_scpi_dev_inst *scpi;
bcee1299 643 double d;
9e45cd41 644
9e45cd41
BV
645 scpi = sdi->conn;
646
647 /*
648 * A requested value is certainly on the way. Retrieve it now,
649 * to avoid leaving the device in a state where it's not expecting
650 * commands.
fa2ce8c7
FS
651 *
652 * TODO: Doesn't work for (at least) the HP 66XXB models.
9e45cd41 653 */
bcee1299 654 sr_scpi_get_double(scpi, NULL, &d);
9e45cd41
BV
655 sr_scpi_source_remove(sdi->session, scpi);
656
bee2b016 657 std_session_send_df_end(sdi);
bf48cceb 658
ca1a7cb5
BV
659 return SR_OK;
660}
661
dd5c48a6 662static struct sr_dev_driver scpi_pps_driver_info = {
ca1a7cb5
BV
663 .name = "scpi-pps",
664 .longname = "SCPI PPS",
665 .api_version = 1,
c2fdcc25 666 .init = std_init,
700d6b64 667 .cleanup = std_cleanup,
c2af709b
AG
668 .scan = scan_scpi_pps,
669 .dev_list = std_dev_list,
670 .dev_clear = dev_clear,
671 .config_get = config_get,
672 .config_set = config_set,
673 .config_list = config_list,
674 .dev_open = dev_open,
675 .dev_close = dev_close,
676 .dev_acquisition_start = dev_acquisition_start,
677 .dev_acquisition_stop = dev_acquisition_stop,
678 .context = NULL,
679};
680
681static struct sr_dev_driver hp_ib_pps_driver_info = {
682 .name = "hpib-pps",
683 .longname = "HP-IB PPS",
684 .api_version = 1,
685 .init = std_init,
686 .cleanup = std_cleanup,
687 .scan = scan_hpib_pps,
c01bf34c 688 .dev_list = std_dev_list,
ca1a7cb5
BV
689 .dev_clear = dev_clear,
690 .config_get = config_get,
691 .config_set = config_set,
692 .config_list = config_list,
693 .dev_open = dev_open,
694 .dev_close = dev_close,
695 .dev_acquisition_start = dev_acquisition_start,
696 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 697 .context = NULL,
ca1a7cb5 698};
dd5c48a6 699SR_REGISTER_DEV_DRIVER(scpi_pps_driver_info);
c2af709b 700SR_REGISTER_DEV_DRIVER(hp_ib_pps_driver_info);