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 static struct sr_dev_driver scpi_pps_driver_info;
27 static struct sr_dev_driver hp_ib_pps_driver_info;
29 static const uint32_t scanopts[] = {
34 static const uint32_t drvopts[] = {
38 static const struct pps_channel_instance pci[] = {
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" },
42 { SR_MQ_FREQUENCY, SCPI_CMD_GET_MEAS_FREQUENCY, "F" },
45 static 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))
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;
55 struct pps_channel *pch;
56 struct channel_spec *channels;
57 struct channel_group_spec *channel_groups, *cgs;
58 struct pps_channel_group *pcg;
63 unsigned int num_channels, num_channel_groups, ch_num, ch_idx, i, j;
68 if (get_hw_id(scpi, &hw_info) != SR_OK) {
69 sr_info("Couldn't get IDN response.");
74 for (i = 0; i < num_pps_profiles; i++) {
75 vendor = sr_vendor_alias(hw_info->manufacturer);
76 if (g_ascii_strcasecmp(vendor, pps_profiles[i].vendor))
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))
80 device = &pps_profiles[i];
81 g_match_info_unref(model_mi);
82 g_regex_unref(model_re);
87 sr_scpi_hw_info_free(hw_info);
91 sdi = g_malloc0(sizeof(struct sr_dev_inst));
92 sdi->vendor = g_strdup(vendor);
93 sdi->model = g_strdup(hw_info->model);
94 sdi->version = g_strdup(hw_info->firmware_version);
96 sdi->driver = &scpi_pps_driver_info;
97 sdi->inst_type = SR_INST_SCPI;
98 sdi->serial_num = g_strdup(hw_info->serial_number);
100 devc = g_malloc0(sizeof(struct dev_context));
101 devc->device = device;
104 if (device->num_channels) {
105 /* Static channels and groups. */
106 channels = (struct channel_spec *)device->channels;
107 num_channels = device->num_channels;
108 channel_groups = (struct channel_group_spec *)device->channel_groups;
109 num_channel_groups = device->num_channel_groups;
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);
115 sr_err("Failed to probe for channels.");
119 * Since these were dynamically allocated, we'll need to free them
122 devc->channels = channels;
123 devc->channel_groups = channel_groups;
127 for (ch_num = 0; ch_num < num_channels; ch_num++) {
128 /* Create one channel per measurable output unit. */
129 for (i = 0; i < ARRAY_SIZE(pci); i++) {
130 if (!scpi_cmd_get(devc->device->commands, pci[i].command))
132 g_snprintf(ch_name, 16, "%s%s", pci[i].prefix,
133 channels[ch_num].name);
134 ch = sr_channel_new(sdi, ch_idx++, SR_CHANNEL_ANALOG, TRUE,
136 pch = g_malloc0(sizeof(struct pps_channel));
137 pch->hw_output_idx = ch_num;
138 pch->hwname = channels[ch_num].name;
144 for (i = 0; i < num_channel_groups; i++) {
145 cgs = &channel_groups[i];
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) {
150 for (l = sdi->channels; l; l = l->next) {
153 if (pch->hw_output_idx == j)
154 cg->channels = g_slist_append(cg->channels, ch);
158 pcg = g_malloc0(sizeof(struct pps_channel_group));
159 pcg->features = cgs->features;
161 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
164 sr_scpi_hw_info_free(hw_info);
167 scpi_cmd(sdi, devc->device->commands, SCPI_CMD_LOCAL);
172 static gchar *hpib_get_revision(struct sr_scpi_dev_inst *scpi)
177 GRegex *version_regex;
179 ret = sr_scpi_get_string(scpi, "ROM?", &response);
180 if (ret != SR_OK && !response)
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);
189 /* Not a valid version string. Ignore it. */
193 /* Replace space with dot. */
201 * This function assumes the response is in the form "HP<model_number>"
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.
207 static int hpib_get_hw_id(struct sr_scpi_dev_inst *scpi,
208 struct sr_scpi_hw_info **scpi_response)
212 struct sr_scpi_hw_info *hw_info;
214 ret = sr_scpi_get_string(scpi, "ID?", &response);
215 if ((ret != SR_OK) || !response)
218 hw_info = g_malloc0(sizeof(struct sr_scpi_hw_info));
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");
228 static struct sr_dev_inst *probe_scpi_pps_device(struct sr_scpi_dev_inst *scpi)
230 return probe_device(scpi, sr_scpi_get_hw_id);
233 static struct sr_dev_inst *probe_hpib_pps_device(struct sr_scpi_dev_inst *scpi)
235 return probe_device(scpi, hpib_get_hw_id);
238 static GSList *scan_scpi_pps(struct sr_dev_driver *di, GSList *options)
240 return sr_scpi_scan(di->context, options, probe_scpi_pps_device);
243 static GSList *scan_hpib_pps(struct sr_dev_driver *di, GSList *options)
245 return sr_scpi_scan(di->context, options, probe_hpib_pps_device);
248 static int dev_open(struct sr_dev_inst *sdi)
250 struct dev_context *devc;
251 struct sr_scpi_dev_inst *scpi;
255 if (sr_scpi_open(scpi) < 0)
259 scpi_cmd(sdi, devc->device->commands, SCPI_CMD_REMOTE);
260 devc->beeper_was_set = FALSE;
261 if (scpi_cmd_resp(sdi, devc->device->commands, &beeper,
262 G_VARIANT_TYPE_BOOLEAN, SCPI_CMD_BEEPER) == SR_OK) {
263 if (g_variant_get_boolean(beeper)) {
264 devc->beeper_was_set = TRUE;
265 scpi_cmd(sdi, devc->device->commands, SCPI_CMD_BEEPER_DISABLE);
267 g_variant_unref(beeper);
273 static int dev_close(struct sr_dev_inst *sdi)
275 struct sr_scpi_dev_inst *scpi;
276 struct dev_context *devc;
284 if (devc->beeper_was_set)
285 scpi_cmd(sdi, devc->device->commands, SCPI_CMD_BEEPER_ENABLE);
286 scpi_cmd(sdi, devc->device->commands, SCPI_CMD_LOCAL);
288 return sr_scpi_close(scpi);
291 static void clear_helper(struct dev_context *devc)
293 g_free(devc->channels);
294 g_free(devc->channel_groups);
297 static int dev_clear(const struct sr_dev_driver *di)
299 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
302 static int config_get(uint32_t key, GVariant **data,
303 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
305 struct dev_context *devc;
306 const GVariantType *gvtype;
318 * These options only apply to channel groups with a single
319 * channel -- they're per-channel settings for the device.
323 * Config keys are handled below depending on whether a channel
324 * group was provided by the frontend. However some of these
325 * take a CG on one PPS but not on others. Check the device's
326 * profile for that here, and NULL out the channel group as needed.
328 for (i = 0; i < devc->device->num_devopts; i++) {
329 if (devc->device->devopts[i] == key) {
339 case SR_CONF_ENABLED:
340 gvtype = G_VARIANT_TYPE_BOOLEAN;
341 cmd = SCPI_CMD_GET_OUTPUT_ENABLED;
343 case SR_CONF_VOLTAGE:
344 gvtype = G_VARIANT_TYPE_DOUBLE;
345 cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
347 case SR_CONF_VOLTAGE_TARGET:
348 gvtype = G_VARIANT_TYPE_DOUBLE;
349 cmd = SCPI_CMD_GET_VOLTAGE_TARGET;
351 case SR_CONF_OUTPUT_FREQUENCY:
352 gvtype = G_VARIANT_TYPE_DOUBLE;
353 cmd = SCPI_CMD_GET_MEAS_FREQUENCY;
355 case SR_CONF_OUTPUT_FREQUENCY_TARGET:
356 gvtype = G_VARIANT_TYPE_DOUBLE;
357 cmd = SCPI_CMD_GET_FREQUENCY_TARGET;
359 case SR_CONF_CURRENT:
360 gvtype = G_VARIANT_TYPE_DOUBLE;
361 cmd = SCPI_CMD_GET_MEAS_CURRENT;
363 case SR_CONF_CURRENT_LIMIT:
364 gvtype = G_VARIANT_TYPE_DOUBLE;
365 cmd = SCPI_CMD_GET_CURRENT_LIMIT;
367 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
368 gvtype = G_VARIANT_TYPE_BOOLEAN;
369 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ENABLED;
371 case SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE:
372 gvtype = G_VARIANT_TYPE_BOOLEAN;
373 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ACTIVE;
375 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
376 gvtype = G_VARIANT_TYPE_DOUBLE;
377 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_THRESHOLD;
379 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
380 gvtype = G_VARIANT_TYPE_BOOLEAN;
381 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ENABLED;
383 case SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE:
384 gvtype = G_VARIANT_TYPE_BOOLEAN;
385 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ACTIVE;
387 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
388 gvtype = G_VARIANT_TYPE_DOUBLE;
389 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_THRESHOLD;
391 case SR_CONF_OVER_TEMPERATURE_PROTECTION:
392 gvtype = G_VARIANT_TYPE_BOOLEAN;
393 cmd = SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION;
395 case SR_CONF_REGULATION:
396 gvtype = G_VARIANT_TYPE_STRING;
397 cmd = SCPI_CMD_GET_OUTPUT_REGULATION;
403 select_channel(sdi, cg->channels->data);
404 ret = scpi_cmd_resp(sdi, devc->device->commands, data, gvtype, cmd);
406 if (cmd == SCPI_CMD_GET_OUTPUT_REGULATION) {
408 * The Rigol DP800 series return CV/CC/UR, Philips PM2800
409 * return VOLT/CURR. We always return a GVariant string in
410 * the Rigol notation.
412 s = g_variant_get_string(*data, NULL);
413 if (!strcmp(s, "VOLT")) {
414 g_variant_unref(*data);
415 *data = g_variant_new_string("CV");
416 } else if (!strcmp(s, "CURR")) {
417 g_variant_unref(*data);
418 *data = g_variant_new_string("CC");
421 s = g_variant_get_string(*data, NULL);
422 if (strcmp(s, "CV") && strcmp(s, "CC") && strcmp(s, "UR")) {
423 sr_dbg("Unknown response to SCPI_CMD_GET_OUTPUT_REGULATION: %s", s);
431 static int config_set(uint32_t key, GVariant *data,
432 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
434 struct dev_context *devc;
441 select_channel(sdi, cg->channels->data);
446 case SR_CONF_ENABLED:
447 if (g_variant_get_boolean(data))
448 return scpi_cmd(sdi, devc->device->commands,
449 SCPI_CMD_SET_OUTPUT_ENABLE);
451 return scpi_cmd(sdi, devc->device->commands,
452 SCPI_CMD_SET_OUTPUT_DISABLE);
454 case SR_CONF_VOLTAGE_TARGET:
455 d = g_variant_get_double(data);
456 return scpi_cmd(sdi, devc->device->commands,
457 SCPI_CMD_SET_VOLTAGE_TARGET, d);
459 case SR_CONF_OUTPUT_FREQUENCY_TARGET:
460 d = g_variant_get_double(data);
461 return scpi_cmd(sdi, devc->device->commands,
462 SCPI_CMD_SET_FREQUENCY_TARGET, d);
464 case SR_CONF_CURRENT_LIMIT:
465 d = g_variant_get_double(data);
466 return scpi_cmd(sdi, devc->device->commands,
467 SCPI_CMD_SET_CURRENT_LIMIT, d);
469 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
470 if (g_variant_get_boolean(data))
471 return scpi_cmd(sdi, devc->device->commands,
472 SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_ENABLE);
474 return scpi_cmd(sdi, devc->device->commands,
475 SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_DISABLE);
477 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
478 d = g_variant_get_double(data);
479 return scpi_cmd(sdi, devc->device->commands,
480 SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_THRESHOLD, d);
482 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
483 if (g_variant_get_boolean(data))
484 return scpi_cmd(sdi, devc->device->commands,
485 SCPI_CMD_SET_OVER_CURRENT_PROTECTION_ENABLE);
487 return scpi_cmd(sdi, devc->device->commands,
488 SCPI_CMD_SET_OVER_CURRENT_PROTECTION_DISABLE);
490 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
491 d = g_variant_get_double(data);
492 return scpi_cmd(sdi, devc->device->commands,
493 SCPI_CMD_SET_OVER_CURRENT_PROTECTION_THRESHOLD, d);
495 case SR_CONF_OVER_TEMPERATURE_PROTECTION:
496 if (g_variant_get_boolean(data))
497 return scpi_cmd(sdi, devc->device->commands,
498 SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_ENABLE);
500 return scpi_cmd(sdi, devc->device->commands,
501 SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_DISABLE);
510 static int config_list(uint32_t key, GVariant **data,
511 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
513 struct dev_context *devc;
514 struct sr_channel *ch;
515 const struct channel_spec *ch_spec;
519 devc = (sdi) ? sdi->priv : NULL;
523 case SR_CONF_SCAN_OPTIONS:
524 case SR_CONF_DEVICE_OPTIONS:
525 return std_opts_config_list(key, data, sdi, cg,
526 ARRAY_AND_SIZE(scanopts),
527 ARRAY_AND_SIZE(drvopts),
528 (devc && devc->device) ? devc->device->devopts : NULL,
529 (devc && devc->device) ? devc->device->num_devopts : 0);
531 case SR_CONF_CHANNEL_CONFIG:
532 if (!devc || !devc->device)
536 if (devc->device->features & PPS_INDEPENDENT)
537 s[i++] = "Independent";
538 if (devc->device->features & PPS_SERIES)
540 if (devc->device->features & PPS_PARALLEL)
544 * Shouldn't happen: independent-only devices
545 * shouldn't advertise this option at all.
549 *data = g_variant_new_strv(s, i);
556 * Per-channel-group options depending on a channel are actually
557 * done with the first channel. Channel groups in PPS can have
558 * more than one channel, but they will typically be of equal
559 * specification for use in series or parallel mode.
561 ch = cg->channels->data;
562 if (!devc || !devc->device)
564 ch_spec = &(devc->device->channels[ch->index]);
567 case SR_CONF_DEVICE_OPTIONS:
568 *data = std_gvar_array_u32(devc->device->devopts_cg, devc->device->num_devopts_cg);
570 case SR_CONF_VOLTAGE_TARGET:
571 *data = std_gvar_min_max_step_array(ch_spec->voltage);
573 case SR_CONF_OUTPUT_FREQUENCY_TARGET:
574 *data = std_gvar_min_max_step_array(ch_spec->frequency);
576 case SR_CONF_CURRENT_LIMIT:
577 *data = std_gvar_min_max_step_array(ch_spec->current);
587 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
589 struct dev_context *devc;
590 struct sr_scpi_dev_inst *scpi;
591 struct sr_channel *ch;
592 struct pps_channel *pch;
598 if ((ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 10,
599 scpi_pps_receive_data, (void *)sdi)) != SR_OK)
601 std_session_send_df_header(sdi);
603 /* Prime the pipe with the first channel's fetch. */
604 ch = sr_next_enabled_channel(sdi, NULL);
606 if ((ret = select_channel(sdi, ch)) < 0)
608 if (pch->mq == SR_MQ_VOLTAGE)
609 cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
610 else if (pch->mq == SR_MQ_FREQUENCY)
611 cmd = SCPI_CMD_GET_MEAS_FREQUENCY;
612 else if (pch->mq == SR_MQ_CURRENT)
613 cmd = SCPI_CMD_GET_MEAS_CURRENT;
614 else if (pch->mq == SR_MQ_POWER)
615 cmd = SCPI_CMD_GET_MEAS_POWER;
618 scpi_cmd(sdi, devc->device->commands, cmd, pch->hwname);
623 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
625 struct sr_scpi_dev_inst *scpi;
631 * A requested value is certainly on the way. Retrieve it now,
632 * to avoid leaving the device in a state where it's not expecting
635 sr_scpi_get_double(scpi, NULL, &d);
636 sr_scpi_source_remove(sdi->session, scpi);
638 std_session_send_df_end(sdi);
643 static struct sr_dev_driver scpi_pps_driver_info = {
645 .longname = "SCPI PPS",
648 .cleanup = std_cleanup,
649 .scan = scan_scpi_pps,
650 .dev_list = std_dev_list,
651 .dev_clear = dev_clear,
652 .config_get = config_get,
653 .config_set = config_set,
654 .config_list = config_list,
655 .dev_open = dev_open,
656 .dev_close = dev_close,
657 .dev_acquisition_start = dev_acquisition_start,
658 .dev_acquisition_stop = dev_acquisition_stop,
662 static struct sr_dev_driver hp_ib_pps_driver_info = {
664 .longname = "HP-IB PPS",
667 .cleanup = std_cleanup,
668 .scan = scan_hpib_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,
680 SR_REGISTER_DEV_DRIVER(scpi_pps_driver_info);
681 SR_REGISTER_DEV_DRIVER(hp_ib_pps_driver_info);