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/>.
26 SR_PRIV struct sr_dev_driver scpi_pps_driver_info;
28 static const uint32_t scanopts[] = {
33 static const uint32_t drvopts[] = {
37 static const struct pps_channel_instance pci[] = {
38 { SR_MQ_VOLTAGE, SCPI_CMD_GET_MEAS_VOLTAGE, "V" },
39 { SR_MQ_CURRENT, SCPI_CMD_GET_MEAS_CURRENT, "I" },
40 { SR_MQ_POWER, SCPI_CMD_GET_MEAS_POWER, "P" },
41 { SR_MQ_FREQUENCY, SCPI_CMD_GET_MEAS_FREQUENCY, "F" },
44 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
46 return std_init(di, sr_ctx, LOG_PREFIX);
49 static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
51 struct dev_context *devc;
52 struct sr_dev_inst *sdi;
53 struct sr_scpi_hw_info *hw_info;
54 struct sr_channel_group *cg;
55 struct sr_channel *ch;
56 const struct scpi_pps *device;
57 struct pps_channel *pch;
58 struct channel_spec *channels;
59 struct channel_group_spec *channel_groups, *cgs;
60 struct pps_channel_group *pcg;
65 unsigned int num_channels, num_channel_groups, ch_num, ch_idx, i, j;
70 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
71 sr_info("Couldn't get IDN response.");
76 for (i = 0; i < num_pps_profiles; i++) {
77 vendor = sr_vendor_alias(hw_info->manufacturer);
78 if (g_ascii_strcasecmp(vendor, pps_profiles[i].vendor))
80 model_re = g_regex_new(pps_profiles[i].model, 0, 0, NULL);
81 if (g_regex_match(model_re, hw_info->model, 0, &model_mi))
82 device = &pps_profiles[i];
83 g_match_info_unref(model_mi);
84 g_regex_unref(model_re);
89 sr_scpi_hw_info_free(hw_info);
93 sdi = g_malloc0(sizeof(struct sr_dev_inst));
94 sdi->vendor = g_strdup(vendor);
95 sdi->model = g_strdup(hw_info->model);
96 sdi->version = g_strdup(hw_info->firmware_version);
98 sdi->driver = &scpi_pps_driver_info;
99 sdi->inst_type = SR_INST_SCPI;
100 sdi->serial_num = g_strdup(hw_info->serial_number);
102 devc = g_malloc0(sizeof(struct dev_context));
103 devc->device = device;
106 if (device->num_channels) {
107 /* Static channels and groups. */
108 channels = (struct channel_spec *)device->channels;
109 num_channels = device->num_channels;
110 channel_groups = (struct channel_group_spec *)device->channel_groups;
111 num_channel_groups = device->num_channel_groups;
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);
117 sr_err("Failed to probe for channels.");
121 * Since these were dynamically allocated, we'll need to free them
124 devc->channels = channels;
125 devc->channel_groups = channel_groups;
129 for (ch_num = 0; ch_num < num_channels; ch_num++) {
130 /* Create one channel per measurable output unit. */
131 for (i = 0; i < ARRAY_SIZE(pci); i++) {
132 if (!scpi_cmd_get(devc->device->commands, pci[i].command))
134 g_snprintf(ch_name, 16, "%s%s", pci[i].prefix,
135 channels[ch_num].name);
136 ch = sr_channel_new(sdi, ch_idx++, SR_CHANNEL_ANALOG, TRUE,
138 pch = g_malloc0(sizeof(struct pps_channel));
139 pch->hw_output_idx = ch_num;
140 pch->hwname = channels[ch_num].name;
146 for (i = 0; i < num_channel_groups; i++) {
147 cgs = &channel_groups[i];
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) {
152 for (l = sdi->channels; l; l = l->next) {
155 if (pch->hw_output_idx == j)
156 cg->channels = g_slist_append(cg->channels, ch);
160 pcg = g_malloc0(sizeof(struct pps_channel_group));
161 pcg->features = cgs->features;
163 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
166 sr_scpi_hw_info_free(hw_info);
169 scpi_cmd(sdi, devc->device->commands, SCPI_CMD_LOCAL);
174 static GSList *scan(struct sr_dev_driver *di, GSList *options)
176 return sr_scpi_scan(di->context, options, probe_device);
179 static int dev_open(struct sr_dev_inst *sdi)
181 struct dev_context *devc;
182 struct sr_scpi_dev_inst *scpi;
185 if (sdi->status != SR_ST_INACTIVE)
189 if (sr_scpi_open(scpi) < 0)
192 sdi->status = SR_ST_ACTIVE;
195 scpi_cmd(sdi, devc->device->commands, SCPI_CMD_REMOTE);
196 devc->beeper_was_set = FALSE;
197 if (scpi_cmd_resp(sdi, devc->device->commands, &beeper,
198 G_VARIANT_TYPE_BOOLEAN, SCPI_CMD_BEEPER) == SR_OK) {
199 if (g_variant_get_boolean(beeper)) {
200 devc->beeper_was_set = TRUE;
201 scpi_cmd(sdi, devc->device->commands, SCPI_CMD_BEEPER_DISABLE);
203 g_variant_unref(beeper);
209 static int dev_close(struct sr_dev_inst *sdi)
211 struct sr_scpi_dev_inst *scpi;
212 struct dev_context *devc;
214 if (sdi->status != SR_ST_ACTIVE)
215 return SR_ERR_DEV_CLOSED;
220 if (devc->beeper_was_set)
221 scpi_cmd(sdi, devc->device->commands, SCPI_CMD_BEEPER_ENABLE);
222 scpi_cmd(sdi, devc->device->commands, SCPI_CMD_LOCAL);
224 sdi->status = SR_ST_INACTIVE;
230 static void clear_helper(void *priv)
232 struct dev_context *devc;
235 g_free(devc->channels);
236 g_free(devc->channel_groups);
240 static int dev_clear(const struct sr_dev_driver *di)
242 return std_dev_clear(di, clear_helper);
245 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
246 const struct sr_channel_group *cg)
248 struct dev_context *devc;
249 const GVariantType *gvtype;
261 * These options only apply to channel groups with a single
262 * channel -- they're per-channel settings for the device.
266 * Config keys are handled below depending on whether a channel
267 * group was provided by the frontend. However some of these
268 * take a CG on one PPS but not on others. Check the device's
269 * profile for that here, and NULL out the channel group as needed.
271 for (i = 0; i < devc->device->num_devopts; i++) {
272 if (devc->device->devopts[i] == key) {
282 case SR_CONF_ENABLED:
283 gvtype = G_VARIANT_TYPE_BOOLEAN;
284 cmd = SCPI_CMD_GET_OUTPUT_ENABLED;
286 case SR_CONF_VOLTAGE:
287 gvtype = G_VARIANT_TYPE_DOUBLE;
288 cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
290 case SR_CONF_VOLTAGE_TARGET:
291 gvtype = G_VARIANT_TYPE_DOUBLE;
292 cmd = SCPI_CMD_GET_VOLTAGE_TARGET;
294 case SR_CONF_OUTPUT_FREQUENCY:
295 gvtype = G_VARIANT_TYPE_DOUBLE;
296 cmd = SCPI_CMD_GET_MEAS_FREQUENCY;
298 case SR_CONF_OUTPUT_FREQUENCY_TARGET:
299 gvtype = G_VARIANT_TYPE_DOUBLE;
300 cmd = SCPI_CMD_GET_FREQUENCY_TARGET;
302 case SR_CONF_CURRENT:
303 gvtype = G_VARIANT_TYPE_DOUBLE;
304 cmd = SCPI_CMD_GET_MEAS_CURRENT;
306 case SR_CONF_CURRENT_LIMIT:
307 gvtype = G_VARIANT_TYPE_DOUBLE;
308 cmd = SCPI_CMD_GET_CURRENT_LIMIT;
310 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
311 gvtype = G_VARIANT_TYPE_BOOLEAN;
312 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ENABLED;
314 case SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE:
315 gvtype = G_VARIANT_TYPE_BOOLEAN;
316 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ACTIVE;
318 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
319 gvtype = G_VARIANT_TYPE_DOUBLE;
320 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_THRESHOLD;
322 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
323 gvtype = G_VARIANT_TYPE_BOOLEAN;
324 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ENABLED;
326 case SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE:
327 gvtype = G_VARIANT_TYPE_BOOLEAN;
328 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ACTIVE;
330 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
331 gvtype = G_VARIANT_TYPE_DOUBLE;
332 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_THRESHOLD;
334 case SR_CONF_OVER_TEMPERATURE_PROTECTION:
335 gvtype = G_VARIANT_TYPE_BOOLEAN;
336 cmd = SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION;
338 case SR_CONF_REGULATION:
339 gvtype = G_VARIANT_TYPE_STRING;
340 cmd = SCPI_CMD_GET_OUTPUT_REGULATION;
346 select_channel(sdi, cg->channels->data);
347 ret = scpi_cmd_resp(sdi, devc->device->commands, data, gvtype, cmd);
349 if (cmd == SCPI_CMD_GET_OUTPUT_REGULATION) {
351 * The Rigol DP800 series return CV/CC/UR, Philips PM2800
352 * return VOLT/CURR. We always return a GVariant string in
353 * the Rigol notation.
355 s = g_variant_get_string(*data, NULL);
356 if (!strcmp(s, "VOLT")) {
357 g_variant_unref(*data);
358 *data = g_variant_new_string("CV");
359 } else if (!strcmp(s, "CURR")) {
360 g_variant_unref(*data);
361 *data = g_variant_new_string("CC");
364 s = g_variant_get_string(*data, NULL);
365 if (strcmp(s, "CV") && strcmp(s, "CC") && strcmp(s, "UR")) {
366 sr_dbg("Unknown response to SCPI_CMD_GET_OUTPUT_REGULATION: %s", s);
374 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
375 const struct sr_channel_group *cg)
377 struct dev_context *devc;
384 if (sdi->status != SR_ST_ACTIVE)
385 return SR_ERR_DEV_CLOSED;
388 /* Channel group specified. */
389 select_channel(sdi, cg->channels->data);
394 case SR_CONF_ENABLED:
395 if (g_variant_get_boolean(data))
396 ret = scpi_cmd(sdi, devc->device->commands,
397 SCPI_CMD_SET_OUTPUT_ENABLE);
399 ret = scpi_cmd(sdi, devc->device->commands,
400 SCPI_CMD_SET_OUTPUT_DISABLE);
402 case SR_CONF_VOLTAGE_TARGET:
403 d = g_variant_get_double(data);
404 ret = scpi_cmd(sdi, devc->device->commands,
405 SCPI_CMD_SET_VOLTAGE_TARGET, d);
407 case SR_CONF_OUTPUT_FREQUENCY_TARGET:
408 d = g_variant_get_double(data);
409 ret = scpi_cmd(sdi, devc->device->commands,
410 SCPI_CMD_SET_FREQUENCY_TARGET, d);
412 case SR_CONF_CURRENT_LIMIT:
413 d = g_variant_get_double(data);
414 ret = scpi_cmd(sdi, devc->device->commands,
415 SCPI_CMD_SET_CURRENT_LIMIT, d);
417 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
418 if (g_variant_get_boolean(data))
419 ret = scpi_cmd(sdi, devc->device->commands,
420 SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_ENABLE);
422 ret = scpi_cmd(sdi, devc->device->commands,
423 SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_DISABLE);
425 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
426 d = g_variant_get_double(data);
427 ret = scpi_cmd(sdi, devc->device->commands,
428 SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_THRESHOLD, d);
430 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
431 if (g_variant_get_boolean(data))
432 ret = scpi_cmd(sdi, devc->device->commands,
433 SCPI_CMD_SET_OVER_CURRENT_PROTECTION_ENABLE);
435 ret = scpi_cmd(sdi, devc->device->commands,
436 SCPI_CMD_SET_OVER_CURRENT_PROTECTION_DISABLE);
438 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
439 d = g_variant_get_double(data);
440 ret = scpi_cmd(sdi, devc->device->commands,
441 SCPI_CMD_SET_OVER_CURRENT_PROTECTION_THRESHOLD, d);
443 case SR_CONF_OVER_TEMPERATURE_PROTECTION:
444 if (g_variant_get_boolean(data))
445 ret = scpi_cmd(sdi, devc->device->commands,
446 SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_ENABLE);
448 ret = scpi_cmd(sdi, devc->device->commands,
449 SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_DISABLE);
458 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
459 const struct sr_channel_group *cg)
461 struct dev_context *devc;
462 struct sr_channel *ch;
463 const struct channel_spec *ch_spec;
469 /* Always available, even without sdi. */
470 if (key == SR_CONF_SCAN_OPTIONS) {
471 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
472 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
474 } else if (key == SR_CONF_DEVICE_OPTIONS && !sdi) {
475 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
476 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
486 /* No channel group: global options. */
488 case SR_CONF_DEVICE_OPTIONS:
489 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
490 devc->device->devopts, devc->device->num_devopts,
493 case SR_CONF_CHANNEL_CONFIG:
496 if (devc->device->features & PPS_INDEPENDENT)
497 s[i++] = "Independent";
498 if (devc->device->features & PPS_SERIES)
500 if (devc->device->features & PPS_PARALLEL)
504 * Shouldn't happen: independent-only devices
505 * shouldn't advertise this option at all.
509 *data = g_variant_new_strv(s, i);
515 /* Channel group specified. */
517 * Per-channel-group options depending on a channel are actually
518 * done with the first channel. Channel groups in PPS can have
519 * more than one channel, but they will typically be of equal
520 * specification for use in series or parallel mode.
522 ch = cg->channels->data;
525 case SR_CONF_DEVICE_OPTIONS:
526 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
527 devc->device->devopts_cg, devc->device->num_devopts_cg,
530 case SR_CONF_VOLTAGE_TARGET:
531 ch_spec = &(devc->device->channels[ch->index]);
532 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
533 /* Min, max, write resolution. */
534 for (i = 0; i < 3; i++) {
535 gvar = g_variant_new_double(ch_spec->voltage[i]);
536 g_variant_builder_add_value(&gvb, gvar);
538 *data = g_variant_builder_end(&gvb);
540 case SR_CONF_OUTPUT_FREQUENCY_TARGET:
541 ch_spec = &(devc->device->channels[ch->index]);
542 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
543 /* Min, max, write resolution. */
544 for (i = 0; i < 3; i++) {
545 gvar = g_variant_new_double(ch_spec->frequency[i]);
546 g_variant_builder_add_value(&gvb, gvar);
548 *data = g_variant_builder_end(&gvb);
550 case SR_CONF_CURRENT_LIMIT:
551 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
552 /* Min, max, step. */
553 for (i = 0; i < 3; i++) {
554 ch_spec = &(devc->device->channels[ch->index]);
555 gvar = g_variant_new_double(ch_spec->current[i]);
556 g_variant_builder_add_value(&gvb, gvar);
558 *data = g_variant_builder_end(&gvb);
568 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
570 struct dev_context *devc;
571 struct sr_scpi_dev_inst *scpi;
572 struct sr_channel *ch;
573 struct pps_channel *pch;
576 if (sdi->status != SR_ST_ACTIVE)
577 return SR_ERR_DEV_CLOSED;
582 if ((ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 10,
583 scpi_pps_receive_data, (void *)sdi)) != SR_OK)
585 std_session_send_df_header(sdi, LOG_PREFIX);
587 /* Prime the pipe with the first channel's fetch. */
588 ch = sr_next_enabled_channel(sdi, NULL);
590 if ((ret = select_channel(sdi, ch)) < 0)
592 if (pch->mq == SR_MQ_VOLTAGE)
593 cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
594 else if (pch->mq == SR_MQ_FREQUENCY)
595 cmd = SCPI_CMD_GET_MEAS_FREQUENCY;
596 else if (pch->mq == SR_MQ_CURRENT)
597 cmd = SCPI_CMD_GET_MEAS_CURRENT;
598 else if (pch->mq == SR_MQ_POWER)
599 cmd = SCPI_CMD_GET_MEAS_POWER;
602 scpi_cmd(sdi, devc->device->commands, cmd, pch->hwname);
607 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
609 struct sr_scpi_dev_inst *scpi;
612 if (sdi->status != SR_ST_ACTIVE)
613 return SR_ERR_DEV_CLOSED;
618 * A requested value is certainly on the way. Retrieve it now,
619 * to avoid leaving the device in a state where it's not expecting
622 sr_scpi_get_float(scpi, NULL, &f);
623 sr_scpi_source_remove(sdi->session, scpi);
625 std_session_send_df_end(sdi, LOG_PREFIX);
630 SR_PRIV struct sr_dev_driver scpi_pps_driver_info = {
632 .longname = "SCPI PPS",
635 .cleanup = std_cleanup,
637 .dev_list = std_dev_list,
638 .dev_clear = dev_clear,
639 .config_get = config_get,
640 .config_set = config_set,
641 .config_list = config_list,
642 .dev_open = dev_open,
643 .dev_close = dev_close,
644 .dev_acquisition_start = dev_acquisition_start,
645 .dev_acquisition_stop = dev_acquisition_stop,