2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
23 SR_PRIV struct sr_dev_driver scpi_pps_driver_info;
24 static struct sr_dev_driver *di = &scpi_pps_driver_info;
25 extern unsigned int num_pps_profiles;
26 extern const struct scpi_pps pps_profiles[];
28 static const int32_t scanopts[] = {
33 static int init(struct sr_context *sr_ctx)
35 return std_init(sr_ctx, di, LOG_PREFIX);
38 static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
40 struct dev_context *devc;
41 struct sr_dev_inst *sdi;
42 struct sr_scpi_hw_info *hw_info;
43 struct sr_channel_group *cg;
44 struct sr_channel *ch;
45 const struct scpi_pps *device;
46 const struct channel_group_spec *cgs;
47 struct pps_channel_group *pcg;
52 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
53 sr_info("Couldn't get IDN response.");
58 for (i = 0; i < num_pps_profiles; i++) {
59 vendor = get_vendor(hw_info->manufacturer);
60 if (strcasecmp(vendor, pps_profiles[i].idn_vendor))
62 if (!strcmp(hw_info->model, pps_profiles[i].idn_model)) {
63 device = &pps_profiles[i];
68 sr_scpi_hw_info_free(hw_info);
72 sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, vendor,
73 device->idn_model, hw_info->firmware_version);
76 sdi->inst_type = SR_INST_SCPI;
77 devc = g_malloc0(sizeof(struct dev_context));
78 devc->device = device;
81 for (i = 0; i < device->num_channels; i++) {
82 ch = sr_channel_new(i, SR_CHANNEL_ANALOG, TRUE,
83 device->channels[i].name);
84 sdi->channels = g_slist_append(sdi->channels, ch);
87 for (i = 0; i < device->num_channel_groups; i++) {
88 cgs = &device->channel_groups[i];
89 cg = g_malloc0(sizeof(struct sr_channel_group));
90 cg->name = g_strdup(cgs->name);
91 for (j = 0, mask = 1; j < 64; j++, mask <<= 1) {
92 if (cgs->channel_index_mask & mask) {
93 ch = g_slist_nth_data(sdi->channels, j);
94 cg->channels = g_slist_append(cg->channels, ch);
97 pcg = g_malloc0(sizeof(struct pps_channel_group));
98 pcg->features = cgs->features;
100 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
103 /* SCPI devices commonly lock the panel keys when accessed remotely. */
104 scpi_cmd(sdi, SCPI_CMD_KEY_UNLOCK);
110 static GSList *scan(GSList *options)
112 return sr_scpi_scan(di->priv, options, probe_device);
115 static GSList *dev_list(void)
117 return ((struct drv_context *)(di->priv))->instances;
120 static int dev_clear(void)
122 return std_dev_clear(di, NULL);
125 static int dev_open(struct sr_dev_inst *sdi)
127 struct sr_scpi_dev_inst *scpi;
129 if (sdi->status != SR_ST_ACTIVE)
133 if (sr_scpi_open(scpi) < 0)
136 sdi->status = SR_ST_ACTIVE;
141 static int dev_close(struct sr_dev_inst *sdi)
143 struct sr_scpi_dev_inst *scpi;
145 if (sdi->status != SR_ST_ACTIVE)
146 return SR_ERR_DEV_CLOSED;
150 scpi_cmd(sdi, SCPI_CMD_KEY_UNLOCK);
152 sdi->status = SR_ST_INACTIVE;
158 static int cleanup(void)
163 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
164 const struct sr_channel_group *cg)
166 struct dev_context *devc;
167 struct sr_scpi_dev_inst *scpi;
168 struct sr_channel *ch;
181 /* No channel group: global options. */
183 case SR_CONF_OVER_TEMPERATURE_PROTECTION:
185 if (scpi_cmd(sdi, SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION) == SR_OK) {
186 if (sr_scpi_get_string(scpi, NULL, &s) == SR_OK) {
187 *data = g_variant_new_boolean(!strcmp(s, "ON"));
194 case SR_CONF_OUTPUT_CHANNEL_CONFIG:
196 if (scpi_cmd(sdi, SCPI_CMD_GET_OUTPUT_CHANNEL_CONFIG) == SR_OK) {
197 if (sr_scpi_get_string(scpi, NULL, &s) == SR_OK) {
198 *data = g_variant_new_string(s);
209 * These options only apply to channel groups with a single
210 * channel -- they're per-channel settings for the device.
212 if (g_slist_length(cg->channels) > 1)
214 ch = cg->channels->data;
217 case SR_CONF_OUTPUT_REGULATION:
219 if (scpi_cmd(sdi, SCPI_CMD_GET_OUTPUT_REGULATION, ch->name) == SR_OK) {
220 if (sr_scpi_get_string(scpi, NULL, &s) == SR_OK) {
221 if (strcmp(s, "CC") && strcmp(s, "CV") && strcmp(s, "UR")) {
222 sr_dbg("Unknown response to SCPI_CMD_GET_OUTPUT_REGULATION: %s", s);
224 *data = g_variant_new_string(s);
231 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
233 if (scpi_cmd(sdi, SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ENABLED,
234 ch->name) == SR_OK) {
235 if (sr_scpi_get_string(scpi, NULL, &s) == SR_OK) {
236 *data = g_variant_new_boolean(!strcmp(s, "ON"));
241 case SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE:
243 if (scpi_cmd(sdi, SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ACTIVE,
244 ch->name) == SR_OK) {
245 if (sr_scpi_get_string(scpi, NULL, &s) == SR_OK) {
246 *data = g_variant_new_boolean(!strcmp(s, "YES"));
251 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
253 if (scpi_cmd(sdi, SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_THRESHOLD,
254 ch->name) == SR_OK) {
255 if (sr_scpi_get_double(scpi, NULL, &d) == SR_OK) {
256 *data = g_variant_new_double(d);
261 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
263 if (scpi_cmd(sdi, SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ENABLED,
264 ch->name) == SR_OK) {
265 if (sr_scpi_get_string(scpi, NULL, &s) == SR_OK) {
266 *data = g_variant_new_boolean(!strcmp(s, "ON"));
271 case SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE:
273 if (scpi_cmd(sdi, SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ACTIVE,
274 ch->name) == SR_OK) {
275 if (sr_scpi_get_string(scpi, NULL, &s) == SR_OK) {
276 *data = g_variant_new_boolean(!strcmp(s, "YES"));
281 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
283 if (scpi_cmd(sdi, SCPI_CMD_GET_OVER_CURRENT_PROTECTION_THRESHOLD,
284 ch->name) == SR_OK) {
285 if (sr_scpi_get_double(scpi, NULL, &d) == SR_OK) {
286 *data = g_variant_new_double(d);
291 case SR_CONF_OUTPUT_VOLTAGE:
293 if (scpi_cmd(sdi, SCPI_CMD_GET_MEAS_VOLTAGE, ch->name) == SR_OK) {
294 if (sr_scpi_get_double(scpi, NULL, &d) == SR_OK) {
295 *data = g_variant_new_double(d);
300 case SR_CONF_OUTPUT_VOLTAGE_MAX:
302 if (scpi_cmd(sdi, SCPI_CMD_GET_VOLTAGE_MAX, ch->name) == SR_OK) {
303 if (sr_scpi_get_double(scpi, NULL, &d) == SR_OK) {
304 *data = g_variant_new_double(d);
309 case SR_CONF_OUTPUT_CURRENT:
311 if (scpi_cmd(sdi, SCPI_CMD_GET_MEAS_CURRENT, ch->name) == SR_OK) {
312 if (sr_scpi_get_double(scpi, NULL, &d) == SR_OK) {
313 *data = g_variant_new_double(d);
318 case SR_CONF_OUTPUT_CURRENT_MAX:
320 if (scpi_cmd(sdi, SCPI_CMD_GET_CURRENT_MAX, ch->name) == SR_OK) {
321 if (sr_scpi_get_double(scpi, NULL, &d) == SR_OK) {
322 *data = g_variant_new_double(d);
327 case SR_CONF_OUTPUT_ENABLED:
329 if (scpi_cmd(sdi, SCPI_CMD_GET_OUTPUT_ENABLED, ch->name) == SR_OK) {
330 if (sr_scpi_get_string(scpi, NULL, &s) == SR_OK) {
331 *data = g_variant_new_boolean(!strcmp(s, "ON"));
344 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
345 const struct sr_channel_group *cg)
347 struct sr_channel *ch;
352 if (sdi->status != SR_ST_ACTIVE)
353 return SR_ERR_DEV_CLOSED;
359 /* No channel group: global options. */
360 case SR_CONF_OVER_TEMPERATURE_PROTECTION:
361 s = g_variant_get_boolean(data) ? "ON" : "OFF";
362 if (scpi_cmd(sdi, SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION, s) < 0)
365 case SR_CONF_OUTPUT_CHANNEL_CONFIG:
366 s = g_variant_get_string(data, NULL);
367 if (scpi_cmd(sdi, SCPI_CMD_SET_OUTPUT_CHANNEL_CONFIG, s) < 0)
374 /* Channel group specified. */
377 if (g_slist_length(cg->channels) > 1)
379 ch = cg->channels->data;
381 case SR_CONF_OUTPUT_VOLTAGE_MAX:
382 d = g_variant_get_double(data);
383 if (scpi_cmd(sdi, SCPI_CMD_SET_VOLTAGE_MAX, ch->name, d) < 0)
386 case SR_CONF_OUTPUT_CURRENT_MAX:
387 d = g_variant_get_double(data);
388 if (scpi_cmd(sdi, SCPI_CMD_SET_CURRENT_MAX, ch->name, d) < 0)
391 case SR_CONF_OUTPUT_ENABLED:
392 s = g_variant_get_boolean(data) ? "ON" : "OFF";
393 if (scpi_cmd(sdi, SCPI_CMD_SET_OUTPUT_ENABLED, ch->name, s) < 0)
396 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
397 s = g_variant_get_boolean(data) ? "ON" : "OFF";
398 if (scpi_cmd(sdi, SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_ENABLED,
402 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
403 d = g_variant_get_double(data);
404 if (scpi_cmd(sdi, SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_THRESHOLD,
408 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
409 s = g_variant_get_boolean(data) ? "ON" : "OFF";
410 if (scpi_cmd(sdi, SCPI_CMD_SET_OVER_CURRENT_PROTECTION_ENABLED,
414 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
415 d = g_variant_get_double(data);
416 if (scpi_cmd(sdi, SCPI_CMD_SET_OVER_CURRENT_PROTECTION_THRESHOLD,
428 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
429 const struct sr_channel_group *cg)
431 struct dev_context *devc;
432 struct sr_channel *ch;
433 struct channel_spec *ch_spec;
439 /* Always available, even without sdi. */
440 if (key == SR_CONF_SCAN_OPTIONS) {
441 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
442 scanopts, ARRAY_SIZE(scanopts), sizeof(int32_t));
452 /* No channel group: global options. */
454 case SR_CONF_DEVICE_OPTIONS:
455 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
456 devc->device->devopts, devc->device->num_devopts,
459 case SR_CONF_OUTPUT_CHANNEL_CONFIG:
461 if (devc->device->features & PPS_INDEPENDENT)
462 s[i++] = "Independent";
463 if (devc->device->features & PPS_SERIES)
465 if (devc->device->features & PPS_PARALLEL)
469 * Shouldn't happen: independent-only devices
470 * shouldn't advertise this option at all.
474 *data = g_variant_new_strv(s, i);
480 /* Channel group specified. */
484 * Per-channel-group options depending on a channel are actually
485 * done with the first channel. Channel groups in PPS can have
486 * more than one channel, but they will typically be of equal
487 * specification for use in series or parallel mode. Drop requests
488 * for groups with more than one channel just to make sure.
490 if (g_slist_length(cg->channels) > 1)
492 ch = cg->channels->data;
495 case SR_CONF_DEVICE_OPTIONS:
496 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
497 devc->device->devopts_cg, devc->device->num_devopts_cg,
500 case SR_CONF_OUTPUT_VOLTAGE_MAX:
501 ch_spec = &(devc->device->channels[ch->index]);
502 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
503 /* Min, max, write resolution. */
504 for (i = 0; i < 3; i++) {
505 gvar = g_variant_new_double(ch_spec->voltage[i]);
506 g_variant_builder_add_value(&gvb, gvar);
508 *data = g_variant_builder_end(&gvb);
510 case SR_CONF_OUTPUT_CURRENT_MAX:
511 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
512 /* Min, max, step. */
513 for (i = 0; i < 3; i++) {
514 ch_spec = &(devc->device->channels[ch->index]);
515 gvar = g_variant_new_double(ch_spec->current[i]);
516 g_variant_builder_add_value(&gvb, gvar);
518 *data = g_variant_builder_end(&gvb);
528 static int dev_acquisition_start(const struct sr_dev_inst *sdi,
531 struct dev_context *devc;
532 struct sr_scpi_dev_inst *scpi;
533 struct sr_channel *ch;
536 if (sdi->status != SR_ST_ACTIVE)
537 return SR_ERR_DEV_CLOSED;
541 devc->cb_data = cb_data;
543 if ((ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 100,
544 scpi_pps_receive_data, (void *)sdi)) != SR_OK)
546 std_session_send_df_header(sdi, LOG_PREFIX);
548 /* Prime the pipe. */
549 devc->state = STATE_VOLTAGE;
550 ch = sdi->channels->data;
551 devc->cur_channel = ch;
552 scpi_cmd(sdi, SCPI_CMD_GET_MEAS_VOLTAGE, ch->name);
557 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
559 struct dev_context *devc;
560 struct sr_scpi_dev_inst *scpi;
565 if (sdi->status != SR_ST_ACTIVE)
566 return SR_ERR_DEV_CLOSED;
572 * A requested value is certainly on the way. Retrieve it now,
573 * to avoid leaving the device in a state where it's not expecting
576 sr_scpi_get_float(scpi, NULL, &f);
577 sr_scpi_source_remove(sdi->session, scpi);
579 /* Just in case something is queued up. */
580 devc->state = STATE_STOP;
585 SR_PRIV struct sr_dev_driver scpi_pps_driver_info = {
587 .longname = "SCPI PPS",
592 .dev_list = dev_list,
593 .dev_clear = dev_clear,
594 .config_get = config_get,
595 .config_set = config_set,
596 .config_list = config_list,
597 .dev_open = dev_open,
598 .dev_close = dev_close,
599 .dev_acquisition_start = dev_acquisition_start,
600 .dev_acquisition_stop = dev_acquisition_stop,