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 uint32_t scanopts[] = {
33 static struct pps_channel_instance pci[] = {
34 { SR_MQ_VOLTAGE, SCPI_CMD_GET_MEAS_VOLTAGE, "V" },
35 { SR_MQ_CURRENT, SCPI_CMD_GET_MEAS_CURRENT, "I" },
36 { SR_MQ_POWER, SCPI_CMD_GET_MEAS_POWER, "P" },
39 static int init(struct sr_context *sr_ctx)
41 return std_init(sr_ctx, di, LOG_PREFIX);
44 static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
46 struct dev_context *devc;
47 struct sr_dev_inst *sdi;
48 struct sr_scpi_hw_info *hw_info;
49 struct sr_channel_group *cg;
50 struct sr_channel *ch;
51 const struct scpi_pps *device;
52 struct pps_channel *pch;
53 const struct channel_group_spec *cgs;
54 struct pps_channel_group *pcg;
59 unsigned int ch_num, ch_idx, i, j;
63 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
64 sr_info("Couldn't get IDN response.");
69 for (i = 0; i < num_pps_profiles; i++) {
70 vendor = get_vendor(hw_info->manufacturer);
71 if (strcasecmp(vendor, pps_profiles[i].vendor))
73 model_re = g_regex_new(pps_profiles[i].model, 0, 0, NULL);
74 if (g_regex_match(model_re, hw_info->model, 0, &model_mi))
75 device = &pps_profiles[i];
76 g_match_info_unref(model_mi);
77 g_regex_unref(model_re);
82 sr_scpi_hw_info_free(hw_info);
86 sdi = sr_dev_inst_new(SR_ST_ACTIVE, vendor, hw_info->model,
87 hw_info->firmware_version);
90 sdi->inst_type = SR_INST_SCPI;
91 devc = g_malloc0(sizeof(struct dev_context));
92 devc->device = device;
96 for (ch_num = 0; ch_num < device->num_channels; ch_num++) {
97 /* Create one channel per measurable output unit. */
98 for (i = 0; i < ARRAY_SIZE(pci); i++) {
99 if (!scpi_cmd_get(sdi, pci[i].command))
101 g_snprintf(ch_name, 16, "%s%s", pci[i].prefix,
102 device->channels[ch_num].name);
103 ch = sr_channel_new(ch_idx++, SR_CHANNEL_ANALOG, TRUE, ch_name);
104 pch = g_malloc0(sizeof(struct pps_channel));
105 pch->hw_output_idx = ch_num;
106 pch->hwname = device->channels[ch_num].name;
109 sdi->channels = g_slist_append(sdi->channels, ch);
113 for (i = 0; i < device->num_channel_groups; i++) {
114 cgs = &device->channel_groups[i];
115 cg = g_malloc0(sizeof(struct sr_channel_group));
116 cg->name = g_strdup(cgs->name);
117 for (j = 0, mask = 1; j < 64; j++, mask <<= 1) {
118 if (cgs->channel_index_mask & mask) {
119 for (l = sdi->channels; l; l = l->next) {
122 if (pch->hw_output_idx == j)
123 cg->channels = g_slist_append(cg->channels, ch);
127 pcg = g_malloc0(sizeof(struct pps_channel_group));
128 pcg->features = cgs->features;
130 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
133 scpi_cmd(sdi, SCPI_CMD_LOCAL);
139 static GSList *scan(GSList *options)
141 return sr_scpi_scan(di->priv, options, probe_device);
144 static GSList *dev_list(void)
146 return ((struct drv_context *)(di->priv))->instances;
149 static int dev_clear(void)
151 return std_dev_clear(di, NULL);
154 static int dev_open(struct sr_dev_inst *sdi)
156 struct sr_scpi_dev_inst *scpi;
158 if (sdi->status != SR_ST_ACTIVE)
162 if (sr_scpi_open(scpi) < 0)
165 sdi->status = SR_ST_ACTIVE;
167 scpi_cmd(sdi, SCPI_CMD_REMOTE);
172 static int dev_close(struct sr_dev_inst *sdi)
174 struct sr_scpi_dev_inst *scpi;
176 if (sdi->status != SR_ST_ACTIVE)
177 return SR_ERR_DEV_CLOSED;
181 scpi_cmd(sdi, SCPI_CMD_LOCAL);
183 sdi->status = SR_ST_INACTIVE;
189 static int cleanup(void)
194 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
195 const struct sr_channel_group *cg)
197 struct dev_context *devc;
198 const GVariantType *gvtype;
210 * These options only apply to channel groups with a single
211 * channel -- they're per-channel settings for the device.
215 * Config keys are handled below depending on whether a channel
216 * group was provided by the frontend. However some of these
217 * take a CG on one PPS but not on others. Check the device's
218 * profile for that here, and NULL out the channel group as needed.
220 for (i = 0; i < devc->device->num_devopts; i++) {
221 if (devc->device->devopts[i] == key) {
231 case SR_CONF_OUTPUT_ENABLED:
232 gvtype = G_VARIANT_TYPE_BOOLEAN;
233 cmd = SCPI_CMD_GET_OUTPUT_ENABLED;
235 case SR_CONF_OUTPUT_VOLTAGE:
236 gvtype = G_VARIANT_TYPE_DOUBLE;
237 cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
239 case SR_CONF_OUTPUT_VOLTAGE_TARGET:
240 gvtype = G_VARIANT_TYPE_DOUBLE;
241 cmd = SCPI_CMD_GET_VOLTAGE_TARGET;
243 case SR_CONF_OUTPUT_CURRENT:
244 gvtype = G_VARIANT_TYPE_DOUBLE;
245 cmd = SCPI_CMD_GET_MEAS_CURRENT;
247 case SR_CONF_OUTPUT_CURRENT_LIMIT:
248 gvtype = G_VARIANT_TYPE_DOUBLE;
249 cmd = SCPI_CMD_GET_CURRENT_LIMIT;
251 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
252 gvtype = G_VARIANT_TYPE_BOOLEAN;
253 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ENABLED;
255 case SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE:
256 gvtype = G_VARIANT_TYPE_BOOLEAN;
257 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ACTIVE;
259 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
260 gvtype = G_VARIANT_TYPE_DOUBLE;
261 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_THRESHOLD;
263 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
264 gvtype = G_VARIANT_TYPE_BOOLEAN;
265 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ENABLED;
267 case SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE:
268 gvtype = G_VARIANT_TYPE_BOOLEAN;
269 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ACTIVE;
271 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
272 gvtype = G_VARIANT_TYPE_DOUBLE;
273 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_THRESHOLD;
275 case SR_CONF_OVER_TEMPERATURE_PROTECTION:
276 gvtype = G_VARIANT_TYPE_BOOLEAN;
277 cmd = SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION;
279 case SR_CONF_OUTPUT_REGULATION:
280 gvtype = G_VARIANT_TYPE_STRING;
281 cmd = SCPI_CMD_GET_OUTPUT_REGULATION;
285 select_channel(sdi, cg->channels->data);
286 ret = scpi_cmd_resp(sdi, data, gvtype, cmd);
288 if (gvtype == G_VARIANT_TYPE_STRING && ret == SR_OK) {
289 /* Non-standard data type responses. */
291 case SCPI_CMD_GET_OUTPUT_REGULATION:
293 * This is specific to the Rigol DP800 series.
294 * We return the same string for now until more
295 * models with this key are supported. Do a check
296 * just for the hell of it.
298 s = g_variant_get_string(*data, NULL);
299 if (strcmp(s, "CC") && strcmp(s, "CV") && strcmp(s, "UR")) {
300 sr_dbg("Unknown response to SCPI_CMD_GET_OUTPUT_REGULATION: %s", s);
312 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
313 const struct sr_channel_group *cg)
321 if (sdi->status != SR_ST_ACTIVE)
322 return SR_ERR_DEV_CLOSED;
325 /* Channel group specified. */
326 select_channel(sdi, cg->channels->data);
330 case SR_CONF_OUTPUT_ENABLED:
331 if (g_variant_get_boolean(data))
332 ret = scpi_cmd(sdi, SCPI_CMD_SET_OUTPUT_ENABLE);
334 ret = scpi_cmd(sdi, SCPI_CMD_SET_OUTPUT_DISABLE);
336 case SR_CONF_OUTPUT_VOLTAGE_TARGET:
337 d = g_variant_get_double(data);
338 ret = scpi_cmd(sdi, SCPI_CMD_SET_VOLTAGE_TARGET, d);
340 case SR_CONF_OUTPUT_CURRENT_LIMIT:
341 d = g_variant_get_double(data);
342 ret = scpi_cmd(sdi, SCPI_CMD_SET_CURRENT_LIMIT, d);
344 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
345 if (g_variant_get_boolean(data))
346 ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_ENABLE);
348 ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_DISABLE);
350 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
351 d = g_variant_get_double(data);
352 ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_THRESHOLD, d);
354 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
355 if (g_variant_get_boolean(data))
356 ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_CURRENT_PROTECTION_ENABLE);
358 ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_CURRENT_PROTECTION_DISABLE);
360 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
361 d = g_variant_get_double(data);
362 ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_CURRENT_PROTECTION_THRESHOLD, d);
364 case SR_CONF_OVER_TEMPERATURE_PROTECTION:
365 if (g_variant_get_boolean(data))
366 ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_ENABLE);
368 ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_DISABLE);
377 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
378 const struct sr_channel_group *cg)
380 struct dev_context *devc;
381 struct sr_channel *ch;
382 struct channel_spec *ch_spec;
388 /* Always available, even without sdi. */
389 if (key == SR_CONF_SCAN_OPTIONS) {
390 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
391 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
401 /* No channel group: global options. */
403 case SR_CONF_DEVICE_OPTIONS:
404 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
405 devc->device->devopts, devc->device->num_devopts,
408 case SR_CONF_OUTPUT_CHANNEL_CONFIG:
411 if (devc->device->features & PPS_INDEPENDENT)
412 s[i++] = "Independent";
413 if (devc->device->features & PPS_SERIES)
415 if (devc->device->features & PPS_PARALLEL)
419 * Shouldn't happen: independent-only devices
420 * shouldn't advertise this option at all.
424 *data = g_variant_new_strv(s, i);
430 /* Channel group specified. */
432 * Per-channel-group options depending on a channel are actually
433 * done with the first channel. Channel groups in PPS can have
434 * more than one channel, but they will typically be of equal
435 * specification for use in series or parallel mode.
437 ch = cg->channels->data;
440 case SR_CONF_DEVICE_OPTIONS:
441 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
442 devc->device->devopts_cg, devc->device->num_devopts_cg,
445 case SR_CONF_OUTPUT_VOLTAGE_TARGET:
446 ch_spec = &(devc->device->channels[ch->index]);
447 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
448 /* Min, max, write resolution. */
449 for (i = 0; i < 3; i++) {
450 gvar = g_variant_new_double(ch_spec->voltage[i]);
451 g_variant_builder_add_value(&gvb, gvar);
453 *data = g_variant_builder_end(&gvb);
455 case SR_CONF_OUTPUT_CURRENT_LIMIT:
456 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
457 /* Min, max, step. */
458 for (i = 0; i < 3; i++) {
459 ch_spec = &(devc->device->channels[ch->index]);
460 gvar = g_variant_new_double(ch_spec->current[i]);
461 g_variant_builder_add_value(&gvb, gvar);
463 *data = g_variant_builder_end(&gvb);
473 static int dev_acquisition_start(const struct sr_dev_inst *sdi,
476 struct dev_context *devc;
477 struct sr_scpi_dev_inst *scpi;
478 struct sr_channel *ch;
479 struct pps_channel *pch;
482 if (sdi->status != SR_ST_ACTIVE)
483 return SR_ERR_DEV_CLOSED;
487 devc->cb_data = cb_data;
489 if ((ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 10,
490 scpi_pps_receive_data, (void *)sdi)) != SR_OK)
492 std_session_send_df_header(sdi, LOG_PREFIX);
494 /* Prime the pipe with the first channel's fetch. */
495 ch = sdi->channels->data;
497 select_channel(sdi, ch);
498 if (pch->mq == SR_MQ_VOLTAGE)
499 cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
500 else if (pch->mq == SR_MQ_CURRENT)
501 cmd = SCPI_CMD_GET_MEAS_CURRENT;
502 else if (pch->mq == SR_MQ_POWER)
503 cmd = SCPI_CMD_GET_MEAS_POWER;
506 scpi_cmd(sdi, cmd, pch->hwname);
511 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
513 struct sr_scpi_dev_inst *scpi;
518 if (sdi->status != SR_ST_ACTIVE)
519 return SR_ERR_DEV_CLOSED;
524 * A requested value is certainly on the way. Retrieve it now,
525 * to avoid leaving the device in a state where it's not expecting
528 sr_scpi_get_float(scpi, NULL, &f);
529 sr_scpi_source_remove(sdi->session, scpi);
534 SR_PRIV struct sr_dev_driver scpi_pps_driver_info = {
536 .longname = "SCPI PPS",
541 .dev_list = dev_list,
542 .dev_clear = dev_clear,
543 .config_get = config_get,
544 .config_set = config_set,
545 .config_list = config_list,
546 .dev_open = dev_open,
547 .dev_close = dev_close,
548 .dev_acquisition_start = dev_acquisition_start,
549 .dev_acquisition_stop = dev_acquisition_stop,