]> sigrok.org Git - libsigrok.git/blame - src/hardware/scpi-pps/api.c
Use driver name as the log prefix in standard functions
[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
AG
45static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi,
46 int (*get_hw_id)(struct sr_scpi_dev_inst *scpi,
47 struct sr_scpi_hw_info **scpi_response)
48 )
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;
103 sdi->priv = devc;
104
4a471029
BV
105 if (device->num_channels) {
106 /* Static channels and groups. */
329733d9 107 channels = (struct channel_spec *)device->channels;
4a471029 108 num_channels = device->num_channels;
329733d9 109 channel_groups = (struct channel_group_spec *)device->channel_groups;
4a471029
BV
110 num_channel_groups = device->num_channel_groups;
111 } else {
112 /* Channels and groups need to be probed. */
113 ret = device->probe_channels(sdi, hw_info, &channels, &num_channels,
114 &channel_groups, &num_channel_groups);
115 if (ret != SR_OK) {
116 sr_err("Failed to probe for channels.");
117 return NULL;
118 }
119 /*
120 * Since these were dynamically allocated, we'll need to free them
121 * later.
122 */
123 devc->channels = channels;
124 devc->channel_groups = channel_groups;
125 }
126
01b0257a 127 ch_idx = 0;
4a471029 128 for (ch_num = 0; ch_num < num_channels; ch_num++) {
01b0257a 129 /* Create one channel per measurable output unit. */
01b0257a 130 for (i = 0; i < ARRAY_SIZE(pci); i++) {
91ef511d 131 if (!scpi_cmd_get(devc->device->commands, pci[i].command))
01b0257a
BV
132 continue;
133 g_snprintf(ch_name, 16, "%s%s", pci[i].prefix,
4a471029 134 channels[ch_num].name);
5e23fcab
ML
135 ch = sr_channel_new(sdi, ch_idx++, SR_CHANNEL_ANALOG, TRUE,
136 ch_name);
01b0257a
BV
137 pch = g_malloc0(sizeof(struct pps_channel));
138 pch->hw_output_idx = ch_num;
4a471029 139 pch->hwname = channels[ch_num].name;
01b0257a
BV
140 pch->mq = pci[i].mq;
141 ch->priv = pch;
01b0257a 142 }
9e45cd41 143 }
ca1a7cb5 144
4a471029
BV
145 for (i = 0; i < num_channel_groups; i++) {
146 cgs = &channel_groups[i];
9e45cd41
BV
147 cg = g_malloc0(sizeof(struct sr_channel_group));
148 cg->name = g_strdup(cgs->name);
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;
154 if (pch->hw_output_idx == j)
155 cg->channels = g_slist_append(cg->channels, ch);
156 }
9e45cd41
BV
157 }
158 }
159 pcg = g_malloc0(sizeof(struct pps_channel_group));
160 pcg->features = cgs->features;
161 cg->priv = pcg;
162 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
163 }
ca1a7cb5 164
4a471029
BV
165 sr_scpi_hw_info_free(hw_info);
166 hw_info = NULL;
167
91ef511d 168 scpi_cmd(sdi, devc->device->commands, SCPI_CMD_LOCAL);
ca1a7cb5 169
9e45cd41
BV
170 return sdi;
171}
ca1a7cb5 172
c2af709b 173static gchar *hpib_get_revision(struct sr_scpi_dev_inst *scpi)
9e45cd41 174{
c2af709b
AG
175 int ret;
176 gboolean matches;
177 char *response;
178 GRegex *version_regex;
179
180 ret = sr_scpi_get_string(scpi, "ROM?", &response);
181 if (ret != SR_OK && !response)
182 return NULL;
183
184 /* Example version string: "B01 B01" */
185 version_regex = g_regex_new("[A-Z][0-9]{2} [A-Z][0-9]{2}", 0, 0, NULL);
186 matches = g_regex_match(version_regex, response, 0, NULL);
187 g_regex_unref(version_regex);
188
189 if (!matches) {
190 /* Not a valid version string. Ignore it. */
191 g_free(response);
192 response = NULL;
193 } else {
194 /* Replace space with dot. */
195 response[3] = '.';
196 }
197
198 return response;
199}
200
201/*
202 * This function assumes the response is in the form "HP<model_number>"
203 *
204 * HP made many GPIB (then called HP-IB) instruments before the SCPI command
205 * set was introduced into the standard. We haven't seen any non-HP instruments
206 * which respond to the "ID?" query, so assume all are HP for now.
207 */
208static int hpib_get_hw_id(struct sr_scpi_dev_inst *scpi,
209 struct sr_scpi_hw_info **scpi_response)
210{
211 int ret;
212 char *response;
213 struct sr_scpi_hw_info *hw_info;
214
215 ret = sr_scpi_get_string(scpi, "ID?", &response);
216 if ((ret != SR_OK) || !response)
217 return SR_ERR;
218
219 hw_info = g_malloc0(sizeof(struct sr_scpi_hw_info));
220
221 *scpi_response = hw_info;
222 hw_info->model = response;
223 hw_info->firmware_version = hpib_get_revision(scpi);
224 hw_info->manufacturer = g_strdup("HP");
225
226 return SR_OK;
227}
228
229static struct sr_dev_inst *probe_scpi_pps_device(struct sr_scpi_dev_inst *scpi)
230{
231 return probe_device(scpi, sr_scpi_get_hw_id);
232}
233
234static struct sr_dev_inst *probe_hpib_pps_device(struct sr_scpi_dev_inst *scpi)
235{
236 return probe_device(scpi, hpib_get_hw_id);
237}
238
239static GSList *scan_scpi_pps(struct sr_dev_driver *di, GSList *options)
240{
241 return sr_scpi_scan(di->context, options, probe_scpi_pps_device);
242}
243
244static GSList *scan_hpib_pps(struct sr_dev_driver *di, GSList *options)
245{
246 return sr_scpi_scan(di->context, options, probe_hpib_pps_device);
ca1a7cb5
BV
247}
248
ca1a7cb5
BV
249static int dev_open(struct sr_dev_inst *sdi)
250{
ee2860ee 251 struct dev_context *devc;
9e45cd41 252 struct sr_scpi_dev_inst *scpi;
ee2860ee 253 GVariant *beeper;
9e45cd41 254
7d4f1741 255 if (sdi->status != SR_ST_INACTIVE)
9e45cd41 256 return SR_ERR;
ca1a7cb5 257
9e45cd41
BV
258 scpi = sdi->conn;
259 if (sr_scpi_open(scpi) < 0)
260 return SR_ERR;
ca1a7cb5
BV
261
262 sdi->status = SR_ST_ACTIVE;
263
91ef511d
BV
264 devc = sdi->priv;
265 scpi_cmd(sdi, devc->device->commands, SCPI_CMD_REMOTE);
ee2860ee 266 devc->beeper_was_set = FALSE;
91ef511d
BV
267 if (scpi_cmd_resp(sdi, devc->device->commands, &beeper,
268 G_VARIANT_TYPE_BOOLEAN, SCPI_CMD_BEEPER) == SR_OK) {
ee2860ee
BV
269 if (g_variant_get_boolean(beeper)) {
270 devc->beeper_was_set = TRUE;
91ef511d 271 scpi_cmd(sdi, devc->device->commands, SCPI_CMD_BEEPER_DISABLE);
ee2860ee
BV
272 }
273 g_variant_unref(beeper);
274 }
60475cd7 275
ca1a7cb5
BV
276 return SR_OK;
277}
278
279static int dev_close(struct sr_dev_inst *sdi)
280{
9e45cd41 281 struct sr_scpi_dev_inst *scpi;
ee2860ee 282 struct dev_context *devc;
ca1a7cb5 283
9e45cd41
BV
284 if (sdi->status != SR_ST_ACTIVE)
285 return SR_ERR_DEV_CLOSED;
ca1a7cb5 286
ee2860ee 287 devc = sdi->priv;
9e45cd41
BV
288 scpi = sdi->conn;
289 if (scpi) {
ee2860ee 290 if (devc->beeper_was_set)
91ef511d
BV
291 scpi_cmd(sdi, devc->device->commands, SCPI_CMD_BEEPER_ENABLE);
292 scpi_cmd(sdi, devc->device->commands, SCPI_CMD_LOCAL);
9e45cd41
BV
293 sr_scpi_close(scpi);
294 sdi->status = SR_ST_INACTIVE;
295 }
ca1a7cb5
BV
296
297 return SR_OK;
298}
299
4a471029
BV
300static void clear_helper(void *priv)
301{
302 struct dev_context *devc;
303
304 devc = priv;
305 g_free(devc->channels);
306 g_free(devc->channel_groups);
307 g_free(devc);
308}
309
1e726f56 310static int dev_clear(const struct sr_dev_driver *di)
ca1a7cb5 311{
4a471029 312 return std_dev_clear(di, clear_helper);
ca1a7cb5
BV
313}
314
584560f1 315static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
ca1a7cb5
BV
316 const struct sr_channel_group *cg)
317{
9e45cd41 318 struct dev_context *devc;
478c8d92
BV
319 const GVariantType *gvtype;
320 unsigned int i;
321 int cmd, ret;
069d9f25 322 const char *s;
ca1a7cb5 323
9e45cd41
BV
324 if (!sdi)
325 return SR_ERR_ARG;
326
327 devc = sdi->priv;
ca1a7cb5 328
478c8d92 329 if (cg) {
9e45cd41
BV
330 /*
331 * These options only apply to channel groups with a single
332 * channel -- they're per-channel settings for the device.
333 */
478c8d92
BV
334
335 /*
336 * Config keys are handled below depending on whether a channel
337 * group was provided by the frontend. However some of these
338 * take a CG on one PPS but not on others. Check the device's
339 * profile for that here, and NULL out the channel group as needed.
340 */
341 for (i = 0; i < devc->device->num_devopts; i++) {
342 if (devc->device->devopts[i] == key) {
343 cg = NULL;
344 break;
345 }
346 }
478c8d92 347 }
9e45cd41 348
478c8d92
BV
349 gvtype = NULL;
350 cmd = -1;
351 switch (key) {
7a0b98b5 352 case SR_CONF_ENABLED:
478c8d92
BV
353 gvtype = G_VARIANT_TYPE_BOOLEAN;
354 cmd = SCPI_CMD_GET_OUTPUT_ENABLED;
355 break;
7a0b98b5 356 case SR_CONF_VOLTAGE:
478c8d92
BV
357 gvtype = G_VARIANT_TYPE_DOUBLE;
358 cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
359 break;
7a0b98b5 360 case SR_CONF_VOLTAGE_TARGET:
478c8d92 361 gvtype = G_VARIANT_TYPE_DOUBLE;
ca95e90f 362 cmd = SCPI_CMD_GET_VOLTAGE_TARGET;
478c8d92 363 break;
4264f1c0
AG
364 case SR_CONF_OUTPUT_FREQUENCY:
365 gvtype = G_VARIANT_TYPE_DOUBLE;
366 cmd = SCPI_CMD_GET_MEAS_FREQUENCY;
367 break;
368 case SR_CONF_OUTPUT_FREQUENCY_TARGET:
369 gvtype = G_VARIANT_TYPE_DOUBLE;
370 cmd = SCPI_CMD_GET_FREQUENCY_TARGET;
371 break;
7a0b98b5 372 case SR_CONF_CURRENT:
478c8d92
BV
373 gvtype = G_VARIANT_TYPE_DOUBLE;
374 cmd = SCPI_CMD_GET_MEAS_CURRENT;
375 break;
7a0b98b5 376 case SR_CONF_CURRENT_LIMIT:
478c8d92 377 gvtype = G_VARIANT_TYPE_DOUBLE;
ca95e90f 378 cmd = SCPI_CMD_GET_CURRENT_LIMIT;
478c8d92
BV
379 break;
380 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
381 gvtype = G_VARIANT_TYPE_BOOLEAN;
382 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ENABLED;
383 break;
384 case SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE:
385 gvtype = G_VARIANT_TYPE_BOOLEAN;
386 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ACTIVE;
387 break;
388 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
389 gvtype = G_VARIANT_TYPE_DOUBLE;
390 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_THRESHOLD;
391 break;
392 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
393 gvtype = G_VARIANT_TYPE_BOOLEAN;
394 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ENABLED;
395 break;
396 case SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE:
397 gvtype = G_VARIANT_TYPE_BOOLEAN;
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;
7a0b98b5 408 case SR_CONF_REGULATION:
60475cd7
BV
409 gvtype = G_VARIANT_TYPE_STRING;
410 cmd = SCPI_CMD_GET_OUTPUT_REGULATION;
478c8d92 411 }
91ef511d
BV
412 if (!gvtype)
413 return SR_ERR_NA;
414
415 if (cg)
416 select_channel(sdi, cg->channels->data);
417 ret = scpi_cmd_resp(sdi, devc->device->commands, data, gvtype, cmd);
418
419 if (cmd == SCPI_CMD_GET_OUTPUT_REGULATION) {
420 /*
421 * The Rigol DP800 series return CV/CC/UR, Philips PM2800
422 * return VOLT/CURR. We always return a GVariant string in
423 * the Rigol notation.
424 */
d66c93cc 425 s = g_variant_get_string(*data, NULL);
069d9f25
AJ
426 if (!strcmp(s, "VOLT")) {
427 g_variant_unref(*data);
91ef511d 428 *data = g_variant_new_string("CV");
069d9f25
AJ
429 } else if (!strcmp(s, "CURR")) {
430 g_variant_unref(*data);
91ef511d 431 *data = g_variant_new_string("CC");
069d9f25
AJ
432 }
433
434 s = g_variant_get_string(*data, NULL);
435 if (strcmp(s, "CV") && strcmp(s, "CC") && strcmp(s, "UR")) {
91ef511d
BV
436 sr_dbg("Unknown response to SCPI_CMD_GET_OUTPUT_REGULATION: %s", s);
437 ret = SR_ERR_DATA;
438 }
91ef511d 439 }
ca1a7cb5
BV
440
441 return ret;
442}
443
584560f1 444static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
ca1a7cb5
BV
445 const struct sr_channel_group *cg)
446{
91ef511d 447 struct dev_context *devc;
9e45cd41 448 double d;
ca1a7cb5 449 int ret;
ca1a7cb5 450
fdedbfcd
BV
451 if (!sdi)
452 return SR_ERR_ARG;
453
ca1a7cb5
BV
454 if (sdi->status != SR_ST_ACTIVE)
455 return SR_ERR_DEV_CLOSED;
456
60475cd7 457 if (cg)
9e45cd41 458 /* Channel group specified. */
60475cd7
BV
459 select_channel(sdi, cg->channels->data);
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))
91ef511d
BV
466 ret = scpi_cmd(sdi, devc->device->commands,
467 SCPI_CMD_SET_OUTPUT_ENABLE);
60475cd7 468 else
91ef511d
BV
469 ret = scpi_cmd(sdi, devc->device->commands,
470 SCPI_CMD_SET_OUTPUT_DISABLE);
60475cd7 471 break;
7a0b98b5 472 case SR_CONF_VOLTAGE_TARGET:
60475cd7 473 d = g_variant_get_double(data);
91ef511d
BV
474 ret = scpi_cmd(sdi, devc->device->commands,
475 SCPI_CMD_SET_VOLTAGE_TARGET, d);
60475cd7 476 break;
4264f1c0
AG
477 case SR_CONF_OUTPUT_FREQUENCY_TARGET:
478 d = g_variant_get_double(data);
91ef511d
BV
479 ret = scpi_cmd(sdi, devc->device->commands,
480 SCPI_CMD_SET_FREQUENCY_TARGET, d);
4264f1c0 481 break;
7a0b98b5 482 case SR_CONF_CURRENT_LIMIT:
60475cd7 483 d = g_variant_get_double(data);
91ef511d
BV
484 ret = scpi_cmd(sdi, devc->device->commands,
485 SCPI_CMD_SET_CURRENT_LIMIT, d);
60475cd7
BV
486 break;
487 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
488 if (g_variant_get_boolean(data))
91ef511d
BV
489 ret = scpi_cmd(sdi, devc->device->commands,
490 SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_ENABLE);
60475cd7 491 else
91ef511d
BV
492 ret = scpi_cmd(sdi, devc->device->commands,
493 SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_DISABLE);
60475cd7
BV
494 break;
495 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
496 d = g_variant_get_double(data);
91ef511d
BV
497 ret = scpi_cmd(sdi, devc->device->commands,
498 SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_THRESHOLD, d);
60475cd7
BV
499 break;
500 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
501 if (g_variant_get_boolean(data))
91ef511d
BV
502 ret = scpi_cmd(sdi, devc->device->commands,
503 SCPI_CMD_SET_OVER_CURRENT_PROTECTION_ENABLE);
60475cd7 504 else
91ef511d
BV
505 ret = scpi_cmd(sdi, devc->device->commands,
506 SCPI_CMD_SET_OVER_CURRENT_PROTECTION_DISABLE);
60475cd7
BV
507 break;
508 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
509 d = g_variant_get_double(data);
91ef511d
BV
510 ret = scpi_cmd(sdi, devc->device->commands,
511 SCPI_CMD_SET_OVER_CURRENT_PROTECTION_THRESHOLD, d);
60475cd7
BV
512 break;
513 case SR_CONF_OVER_TEMPERATURE_PROTECTION:
514 if (g_variant_get_boolean(data))
91ef511d
BV
515 ret = scpi_cmd(sdi, devc->device->commands,
516 SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_ENABLE);
60475cd7 517 else
91ef511d
BV
518 ret = scpi_cmd(sdi, devc->device->commands,
519 SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_DISABLE);
60475cd7
BV
520 break;
521 default:
522 ret = SR_ERR_NA;
ca1a7cb5
BV
523 }
524
525 return ret;
526}
527
584560f1 528static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
ca1a7cb5
BV
529 const struct sr_channel_group *cg)
530{
9e45cd41
BV
531 struct dev_context *devc;
532 struct sr_channel *ch;
329733d9 533 const struct channel_spec *ch_spec;
9e45cd41
BV
534 GVariant *gvar;
535 GVariantBuilder gvb;
536 int ret, i;
537 const char *s[16];
538
539 /* Always available, even without sdi. */
540 if (key == SR_CONF_SCAN_OPTIONS) {
584560f1
BV
541 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
542 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
9e45cd41 543 return SR_OK;
a258204e
BV
544 } else if (key == SR_CONF_DEVICE_OPTIONS && !sdi) {
545 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
9d9cf1c4 546 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
a258204e 547 return SR_OK;
9e45cd41 548 }
ca1a7cb5 549
9e45cd41
BV
550 if (!sdi)
551 return SR_ERR_ARG;
552 devc = sdi->priv;
ca1a7cb5
BV
553
554 ret = SR_OK;
9e45cd41
BV
555 if (!cg) {
556 /* No channel group: global options. */
557 switch (key) {
558 case SR_CONF_DEVICE_OPTIONS:
584560f1 559 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
9e45cd41 560 devc->device->devopts, devc->device->num_devopts,
584560f1 561 sizeof(uint32_t));
9e45cd41 562 break;
7a0b98b5 563 case SR_CONF_CHANNEL_CONFIG:
478c8d92 564 /* Not used. */
9e45cd41
BV
565 i = 0;
566 if (devc->device->features & PPS_INDEPENDENT)
567 s[i++] = "Independent";
568 if (devc->device->features & PPS_SERIES)
569 s[i++] = "Series";
570 if (devc->device->features & PPS_PARALLEL)
571 s[i++] = "Parallel";
572 if (i == 0) {
573 /*
574 * Shouldn't happen: independent-only devices
575 * shouldn't advertise this option at all.
576 */
577 return SR_ERR_NA;
578 }
579 *data = g_variant_new_strv(s, i);
580 break;
581 default:
582 return SR_ERR_NA;
583 }
584 } else {
585 /* Channel group specified. */
9e45cd41
BV
586 /*
587 * Per-channel-group options depending on a channel are actually
588 * done with the first channel. Channel groups in PPS can have
589 * more than one channel, but they will typically be of equal
01b0257a 590 * specification for use in series or parallel mode.
9e45cd41 591 */
9e45cd41
BV
592 ch = cg->channels->data;
593
594 switch (key) {
595 case SR_CONF_DEVICE_OPTIONS:
584560f1 596 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
9e45cd41 597 devc->device->devopts_cg, devc->device->num_devopts_cg,
584560f1 598 sizeof(uint32_t));
9e45cd41 599 break;
7a0b98b5 600 case SR_CONF_VOLTAGE_TARGET:
9e45cd41
BV
601 ch_spec = &(devc->device->channels[ch->index]);
602 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
603 /* Min, max, write resolution. */
604 for (i = 0; i < 3; i++) {
605 gvar = g_variant_new_double(ch_spec->voltage[i]);
606 g_variant_builder_add_value(&gvb, gvar);
607 }
608 *data = g_variant_builder_end(&gvb);
609 break;
4264f1c0
AG
610 case SR_CONF_OUTPUT_FREQUENCY_TARGET:
611 ch_spec = &(devc->device->channels[ch->index]);
612 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
613 /* Min, max, write resolution. */
614 for (i = 0; i < 3; i++) {
615 gvar = g_variant_new_double(ch_spec->frequency[i]);
616 g_variant_builder_add_value(&gvb, gvar);
617 }
618 *data = g_variant_builder_end(&gvb);
619 break;
7a0b98b5 620 case SR_CONF_CURRENT_LIMIT:
9e45cd41
BV
621 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
622 /* Min, max, step. */
623 for (i = 0; i < 3; i++) {
624 ch_spec = &(devc->device->channels[ch->index]);
625 gvar = g_variant_new_double(ch_spec->current[i]);
626 g_variant_builder_add_value(&gvb, gvar);
627 }
628 *data = g_variant_builder_end(&gvb);
629 break;
630 default:
631 return SR_ERR_NA;
632 }
ca1a7cb5
BV
633 }
634
635 return ret;
636}
637
695dc859 638static int dev_acquisition_start(const struct sr_dev_inst *sdi)
ca1a7cb5 639{
9e45cd41
BV
640 struct dev_context *devc;
641 struct sr_scpi_dev_inst *scpi;
642 struct sr_channel *ch;
01b0257a
BV
643 struct pps_channel *pch;
644 int cmd, ret;
ca1a7cb5
BV
645
646 if (sdi->status != SR_ST_ACTIVE)
647 return SR_ERR_DEV_CLOSED;
648
9e45cd41
BV
649 devc = sdi->priv;
650 scpi = sdi->conn;
9e45cd41 651
01b0257a 652 if ((ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 10,
9e45cd41
BV
653 scpi_pps_receive_data, (void *)sdi)) != SR_OK)
654 return ret;
bee2b016 655 std_session_send_df_header(sdi);
9e45cd41 656
01b0257a 657 /* Prime the pipe with the first channel's fetch. */
9c24d16a 658 ch = sr_next_enabled_channel(sdi, NULL);
01b0257a 659 pch = ch->priv;
c1603f45 660 if ((ret = select_channel(sdi, ch)) < 0)
984e4b0d 661 return ret;
01b0257a
BV
662 if (pch->mq == SR_MQ_VOLTAGE)
663 cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
4264f1c0
AG
664 else if (pch->mq == SR_MQ_FREQUENCY)
665 cmd = SCPI_CMD_GET_MEAS_FREQUENCY;
01b0257a
BV
666 else if (pch->mq == SR_MQ_CURRENT)
667 cmd = SCPI_CMD_GET_MEAS_CURRENT;
668 else if (pch->mq == SR_MQ_POWER)
669 cmd = SCPI_CMD_GET_MEAS_POWER;
670 else
671 return SR_ERR;
91ef511d 672 scpi_cmd(sdi, devc->device->commands, cmd, pch->hwname);
ca1a7cb5
BV
673
674 return SR_OK;
675}
676
695dc859 677static int dev_acquisition_stop(struct sr_dev_inst *sdi)
ca1a7cb5 678{
9e45cd41
BV
679 struct sr_scpi_dev_inst *scpi;
680 float f;
681
ca1a7cb5
BV
682 if (sdi->status != SR_ST_ACTIVE)
683 return SR_ERR_DEV_CLOSED;
684
9e45cd41
BV
685 scpi = sdi->conn;
686
687 /*
688 * A requested value is certainly on the way. Retrieve it now,
689 * to avoid leaving the device in a state where it's not expecting
690 * commands.
691 */
692 sr_scpi_get_float(scpi, NULL, &f);
693 sr_scpi_source_remove(sdi->session, scpi);
694
bee2b016 695 std_session_send_df_end(sdi);
bf48cceb 696
ca1a7cb5
BV
697 return SR_OK;
698}
699
dd5c48a6 700static struct sr_dev_driver scpi_pps_driver_info = {
ca1a7cb5
BV
701 .name = "scpi-pps",
702 .longname = "SCPI PPS",
703 .api_version = 1,
c2fdcc25 704 .init = std_init,
700d6b64 705 .cleanup = std_cleanup,
c2af709b
AG
706 .scan = scan_scpi_pps,
707 .dev_list = std_dev_list,
708 .dev_clear = dev_clear,
709 .config_get = config_get,
710 .config_set = config_set,
711 .config_list = config_list,
712 .dev_open = dev_open,
713 .dev_close = dev_close,
714 .dev_acquisition_start = dev_acquisition_start,
715 .dev_acquisition_stop = dev_acquisition_stop,
716 .context = NULL,
717};
718
719static struct sr_dev_driver hp_ib_pps_driver_info = {
720 .name = "hpib-pps",
721 .longname = "HP-IB PPS",
722 .api_version = 1,
723 .init = std_init,
724 .cleanup = std_cleanup,
725 .scan = scan_hpib_pps,
c01bf34c 726 .dev_list = std_dev_list,
ca1a7cb5
BV
727 .dev_clear = dev_clear,
728 .config_get = config_get,
729 .config_set = config_set,
730 .config_list = config_list,
731 .dev_open = dev_open,
732 .dev_close = dev_close,
733 .dev_acquisition_start = dev_acquisition_start,
734 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 735 .context = NULL,
ca1a7cb5 736};
dd5c48a6 737SR_REGISTER_DEV_DRIVER(scpi_pps_driver_info);
c2af709b 738SR_REGISTER_DEV_DRIVER(hp_ib_pps_driver_info);