]> sigrok.org Git - libsigrok.git/blob - src/hardware/scpi-pps/api.c
scpi-pps: Fix driver options.
[libsigrok.git] / src / hardware / scpi-pps / api.c
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
20 #include <string.h>
21 #include "protocol.h"
22
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[];
27
28 static const uint32_t scanopts[] = {
29         SR_CONF_CONN,
30         SR_CONF_SERIALCOMM,
31 };
32
33 static const uint32_t drvopts[] = {
34         SR_CONF_POWER_SUPPLY,
35 };
36
37 static 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 };
42
43 static int init(struct sr_context *sr_ctx)
44 {
45         return std_init(sr_ctx, di, LOG_PREFIX);
46 }
47
48 static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
49 {
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;
56         struct pps_channel *pch;
57         struct channel_spec *channels;
58         struct channel_group_spec *channel_groups, *cgs;
59         struct pps_channel_group *pcg;
60         GRegex *model_re;
61         GMatchInfo *model_mi;
62         GSList *l;
63         uint64_t mask;
64         unsigned int num_channels, num_channel_groups, ch_num, ch_idx, i, j;
65         int ret;
66         const char *vendor;
67         char ch_name[16];
68
69         if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
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++) {
76                 vendor = get_vendor(hw_info->manufacturer);
77                 if (strcasecmp(vendor, pps_profiles[i].vendor))
78                         continue;
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))
81                         device = &pps_profiles[i];
82                 g_match_info_unref(model_mi);
83                 g_regex_unref(model_re);
84                 if (device)
85                         break;
86         }
87         if (!device) {
88                 sr_scpi_hw_info_free(hw_info);
89                 return NULL;
90         }
91
92         sdi = sr_dev_inst_new();
93         sdi->status = SR_ST_ACTIVE;
94         sdi->vendor = g_strdup(vendor);
95         sdi->model = g_strdup(hw_info->model);
96         sdi->version = g_strdup(hw_info->firmware_version);
97         sdi->conn = scpi;
98         sdi->driver = di;
99         sdi->inst_type = SR_INST_SCPI;
100         sdi->serial_num = g_strdup(hw_info->serial_number);
101
102         devc = g_malloc0(sizeof(struct dev_context));
103         devc->device = device;
104         sdi->priv = devc;
105
106         if (device->num_channels) {
107                 /* Static channels and groups. */
108                 channels = device->channels;
109                 num_channels = device->num_channels;
110                 channel_groups = device->channel_groups;
111                 num_channel_groups = device->num_channel_groups;
112         } else {
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);
116                 if (ret != SR_OK) {
117                         sr_err("Failed to probe for channels.");
118                         return NULL;
119                 }
120                 /*
121                  * Since these were dynamically allocated, we'll need to free them
122                  * later.
123                  */
124                 devc->channels = channels;
125                 devc->channel_groups = channel_groups;
126         }
127
128         ch_idx = 0;
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(sdi, pci[i].command))
133                                 continue;
134                         g_snprintf(ch_name, 16, "%s%s", pci[i].prefix,
135                                         channels[ch_num].name);
136                         ch = sr_channel_new(ch_idx++, SR_CHANNEL_ANALOG, TRUE, ch_name);
137                         pch = g_malloc0(sizeof(struct pps_channel));
138                         pch->hw_output_idx = ch_num;
139                         pch->hwname = channels[ch_num].name;
140                         pch->mq = pci[i].mq;
141                         ch->priv = pch;
142                         sdi->channels = g_slist_append(sdi->channels, ch);
143                 }
144         }
145
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) {
153                                         ch = l->data;
154                                         pch = ch->priv;
155                                         if (pch->hw_output_idx == j)
156                                                 cg->channels = g_slist_append(cg->channels, ch);
157                                 }
158                         }
159                 }
160                 pcg = g_malloc0(sizeof(struct pps_channel_group));
161                 pcg->features = cgs->features;
162                 cg->priv = pcg;
163                 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
164         }
165
166         sr_scpi_hw_info_free(hw_info);
167         hw_info = NULL;
168
169         scpi_cmd(sdi, SCPI_CMD_LOCAL);
170         sr_scpi_close(scpi);
171
172         return sdi;
173 }
174
175 static GSList *scan(GSList *options)
176 {
177         return sr_scpi_scan(di->priv, options, probe_device);
178 }
179
180 static GSList *dev_list(void)
181 {
182         return ((struct drv_context *)(di->priv))->instances;
183 }
184
185 static int dev_clear(void)
186 {
187         return std_dev_clear(di, NULL);
188 }
189
190 static int dev_open(struct sr_dev_inst *sdi)
191 {
192         struct dev_context *devc;
193         struct sr_scpi_dev_inst *scpi;
194         GVariant *beeper;
195
196         if (sdi->status != SR_ST_ACTIVE)
197                 return SR_ERR;
198
199         scpi = sdi->conn;
200         if (sr_scpi_open(scpi) < 0)
201                 return SR_ERR;
202
203         sdi->status = SR_ST_ACTIVE;
204
205         scpi_cmd(sdi, SCPI_CMD_REMOTE);
206         devc = sdi->priv;
207         devc->beeper_was_set = FALSE;
208         if (scpi_cmd_resp(sdi, &beeper, G_VARIANT_TYPE_BOOLEAN, SCPI_CMD_BEEPER) == SR_OK) {
209                 if (g_variant_get_boolean(beeper)) {
210                         devc->beeper_was_set = TRUE;
211                         scpi_cmd(sdi, SCPI_CMD_BEEPER_DISABLE);
212                 }
213                 g_variant_unref(beeper);
214         }
215
216         return SR_OK;
217 }
218
219 static int dev_close(struct sr_dev_inst *sdi)
220 {
221         struct sr_scpi_dev_inst *scpi;
222         struct dev_context *devc;
223
224         if (sdi->status != SR_ST_ACTIVE)
225                 return SR_ERR_DEV_CLOSED;
226
227         devc = sdi->priv;
228         scpi = sdi->conn;
229         if (scpi) {
230                 if (devc->beeper_was_set)
231                         scpi_cmd(sdi, SCPI_CMD_BEEPER_ENABLE);
232                 scpi_cmd(sdi, SCPI_CMD_LOCAL);
233                 sr_scpi_close(scpi);
234                 sdi->status = SR_ST_INACTIVE;
235         }
236
237         return SR_OK;
238 }
239
240 static void clear_helper(void *priv)
241 {
242         struct dev_context *devc;
243
244         devc = priv;
245         g_free(devc->channels);
246         g_free(devc->channel_groups);
247         g_free(devc);
248 }
249
250 static int cleanup(void)
251 {
252         return std_dev_clear(di, clear_helper);
253 }
254
255 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
256                 const struct sr_channel_group *cg)
257 {
258         struct dev_context *devc;
259         const GVariantType *gvtype;
260         unsigned int i;
261         int cmd, ret;
262
263         if (!sdi)
264                 return SR_ERR_ARG;
265
266         devc = sdi->priv;
267
268         if (cg) {
269                 /*
270                  * These options only apply to channel groups with a single
271                  * channel -- they're per-channel settings for the device.
272                  */
273
274                 /*
275                  * Config keys are handled below depending on whether a channel
276                  * group was provided by the frontend. However some of these
277                  * take a CG on one PPS but not on others. Check the device's
278                  * profile for that here, and NULL out the channel group as needed.
279                  */
280                 for (i = 0; i < devc->device->num_devopts; i++) {
281                         if (devc->device->devopts[i] == key) {
282                                 cg = NULL;
283                                 break;
284                         }
285                 }
286         }
287
288         gvtype = NULL;
289         cmd = -1;
290         switch (key) {
291         case SR_CONF_OUTPUT_ENABLED:
292                 gvtype = G_VARIANT_TYPE_BOOLEAN;
293                 cmd = SCPI_CMD_GET_OUTPUT_ENABLED;
294                 break;
295         case SR_CONF_OUTPUT_VOLTAGE:
296                 gvtype = G_VARIANT_TYPE_DOUBLE;
297                 cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
298                 break;
299         case SR_CONF_OUTPUT_VOLTAGE_TARGET:
300                 gvtype = G_VARIANT_TYPE_DOUBLE;
301                 cmd = SCPI_CMD_GET_VOLTAGE_TARGET;
302                 break;
303         case SR_CONF_OUTPUT_CURRENT:
304                 gvtype = G_VARIANT_TYPE_DOUBLE;
305                 cmd = SCPI_CMD_GET_MEAS_CURRENT;
306                 break;
307         case SR_CONF_OUTPUT_CURRENT_LIMIT:
308                 gvtype = G_VARIANT_TYPE_DOUBLE;
309                 cmd = SCPI_CMD_GET_CURRENT_LIMIT;
310                 break;
311         case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
312                 gvtype = G_VARIANT_TYPE_BOOLEAN;
313                 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ENABLED;
314                 break;
315         case SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE:
316                 gvtype = G_VARIANT_TYPE_BOOLEAN;
317                 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ACTIVE;
318                 break;
319         case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
320                 gvtype = G_VARIANT_TYPE_DOUBLE;
321                 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_THRESHOLD;
322                 break;
323         case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
324                 gvtype = G_VARIANT_TYPE_BOOLEAN;
325                 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ENABLED;
326                 break;
327         case SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE:
328                 gvtype = G_VARIANT_TYPE_BOOLEAN;
329                 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ACTIVE;
330                 break;
331         case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
332                 gvtype = G_VARIANT_TYPE_DOUBLE;
333                 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_THRESHOLD;
334                 break;
335         case SR_CONF_OVER_TEMPERATURE_PROTECTION:
336                 gvtype = G_VARIANT_TYPE_BOOLEAN;
337                 cmd = SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION;
338                 break;
339         case SR_CONF_OUTPUT_REGULATION:
340                 gvtype = G_VARIANT_TYPE_STRING;
341                 cmd = SCPI_CMD_GET_OUTPUT_REGULATION;
342         }
343         if (gvtype) {
344                 if (cg)
345                         select_channel(sdi, cg->channels->data);
346                 ret = scpi_cmd_resp(sdi, data, gvtype, cmd);
347         } else
348                 ret = SR_ERR_NA;
349
350         return ret;
351 }
352
353 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
354                 const struct sr_channel_group *cg)
355 {
356         double d;
357         int ret;
358
359         if (!sdi)
360                 return SR_ERR_ARG;
361
362         if (sdi->status != SR_ST_ACTIVE)
363                 return SR_ERR_DEV_CLOSED;
364
365         if (cg)
366                 /* Channel group specified. */
367                 select_channel(sdi, cg->channels->data);
368
369         ret = SR_OK;
370         switch (key) {
371         case SR_CONF_OUTPUT_ENABLED:
372                 if (g_variant_get_boolean(data))
373                         ret = scpi_cmd(sdi, SCPI_CMD_SET_OUTPUT_ENABLE);
374                 else
375                         ret = scpi_cmd(sdi, SCPI_CMD_SET_OUTPUT_DISABLE);
376                 break;
377         case SR_CONF_OUTPUT_VOLTAGE_TARGET:
378                 d = g_variant_get_double(data);
379                 ret = scpi_cmd(sdi, SCPI_CMD_SET_VOLTAGE_TARGET, d);
380                 break;
381         case SR_CONF_OUTPUT_CURRENT_LIMIT:
382                 d = g_variant_get_double(data);
383                 ret = scpi_cmd(sdi, SCPI_CMD_SET_CURRENT_LIMIT, d);
384                 break;
385         case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
386                 if (g_variant_get_boolean(data))
387                         ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_ENABLE);
388                 else
389                         ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_DISABLE);
390                 break;
391         case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
392                 d = g_variant_get_double(data);
393                 ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_THRESHOLD, d);
394                 break;
395         case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
396                 if (g_variant_get_boolean(data))
397                         ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_CURRENT_PROTECTION_ENABLE);
398                 else
399                         ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_CURRENT_PROTECTION_DISABLE);
400                 break;
401         case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
402                 d = g_variant_get_double(data);
403                 ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_CURRENT_PROTECTION_THRESHOLD, d);
404                 break;
405         case SR_CONF_OVER_TEMPERATURE_PROTECTION:
406                 if (g_variant_get_boolean(data))
407                         ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_ENABLE);
408                 else
409                         ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_DISABLE);
410                 break;
411         default:
412                 ret = SR_ERR_NA;
413         }
414
415         return ret;
416 }
417
418 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
419                 const struct sr_channel_group *cg)
420 {
421         struct dev_context *devc;
422         struct sr_channel *ch;
423         struct channel_spec *ch_spec;
424         GVariant *gvar;
425         GVariantBuilder gvb;
426         int ret, i;
427         const char *s[16];
428
429         /* Always available, even without sdi. */
430         if (key == SR_CONF_SCAN_OPTIONS) {
431                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
432                                 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
433                 return SR_OK;
434         } else if (key == SR_CONF_DEVICE_OPTIONS && !sdi) {
435                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
436                                 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
437                 return SR_OK;
438         }
439
440         if (!sdi)
441                 return SR_ERR_ARG;
442         devc = sdi->priv;
443
444         ret = SR_OK;
445         if (!cg) {
446                 /* No channel group: global options. */
447                 switch (key) {
448                 case SR_CONF_DEVICE_OPTIONS:
449                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
450                                         devc->device->devopts, devc->device->num_devopts,
451                                         sizeof(uint32_t));
452                         break;
453                 case SR_CONF_OUTPUT_CHANNEL_CONFIG:
454                         /* Not used. */
455                         i = 0;
456                         if (devc->device->features & PPS_INDEPENDENT)
457                                 s[i++] = "Independent";
458                         if (devc->device->features & PPS_SERIES)
459                                 s[i++] = "Series";
460                         if (devc->device->features & PPS_PARALLEL)
461                                 s[i++] = "Parallel";
462                         if (i == 0) {
463                                 /*
464                                  * Shouldn't happen: independent-only devices
465                                  * shouldn't advertise this option at all.
466                                  */
467                                 return SR_ERR_NA;
468                         }
469                         *data = g_variant_new_strv(s, i);
470                         break;
471                 default:
472                         return SR_ERR_NA;
473                 }
474         } else {
475                 /* Channel group specified. */
476                 /*
477                  * Per-channel-group options depending on a channel are actually
478                  * done with the first channel. Channel groups in PPS can have
479                  * more than one channel, but they will typically be of equal
480                  * specification for use in series or parallel mode.
481                  */
482                 ch = cg->channels->data;
483
484                 switch (key) {
485                 case SR_CONF_DEVICE_OPTIONS:
486                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
487                                         devc->device->devopts_cg, devc->device->num_devopts_cg,
488                                         sizeof(uint32_t));
489                         break;
490                 case SR_CONF_OUTPUT_VOLTAGE_TARGET:
491                         ch_spec = &(devc->device->channels[ch->index]);
492                         g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
493                         /* Min, max, write resolution. */
494                         for (i = 0; i < 3; i++) {
495                                 gvar = g_variant_new_double(ch_spec->voltage[i]);
496                                 g_variant_builder_add_value(&gvb, gvar);
497                         }
498                         *data = g_variant_builder_end(&gvb);
499                         break;
500                 case SR_CONF_OUTPUT_CURRENT_LIMIT:
501                         g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
502                         /* Min, max, step. */
503                         for (i = 0; i < 3; i++) {
504                                 ch_spec = &(devc->device->channels[ch->index]);
505                                 gvar = g_variant_new_double(ch_spec->current[i]);
506                                 g_variant_builder_add_value(&gvb, gvar);
507                         }
508                         *data = g_variant_builder_end(&gvb);
509                         break;
510                 default:
511                         return SR_ERR_NA;
512                 }
513         }
514
515         return ret;
516 }
517
518 static int dev_acquisition_start(const struct sr_dev_inst *sdi,
519                 void *cb_data)
520 {
521         struct dev_context *devc;
522         struct sr_scpi_dev_inst *scpi;
523         struct sr_channel *ch;
524         struct pps_channel *pch;
525         int cmd, ret;
526
527         if (sdi->status != SR_ST_ACTIVE)
528                 return SR_ERR_DEV_CLOSED;
529
530         devc = sdi->priv;
531         scpi = sdi->conn;
532         devc->cb_data = cb_data;
533
534         if ((ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 10,
535                         scpi_pps_receive_data, (void *)sdi)) != SR_OK)
536                 return ret;
537         std_session_send_df_header(sdi, LOG_PREFIX);
538
539         /* Prime the pipe with the first channel's fetch. */
540         ch = next_enabled_channel(sdi, NULL);
541         pch = ch->priv;
542         if ((ret = select_channel(sdi, ch)) != SR_OK)
543                 return ret;
544         if (pch->mq == SR_MQ_VOLTAGE)
545                 cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
546         else if (pch->mq == SR_MQ_CURRENT)
547                 cmd = SCPI_CMD_GET_MEAS_CURRENT;
548         else if (pch->mq == SR_MQ_POWER)
549                 cmd = SCPI_CMD_GET_MEAS_POWER;
550         else
551                 return SR_ERR;
552         scpi_cmd(sdi, cmd, pch->hwname);
553
554         return SR_OK;
555 }
556
557 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
558 {
559         struct sr_datafeed_packet packet;
560         struct sr_scpi_dev_inst *scpi;
561         float f;
562
563         (void)cb_data;
564
565         if (sdi->status != SR_ST_ACTIVE)
566                 return SR_ERR_DEV_CLOSED;
567
568         scpi = sdi->conn;
569
570         /*
571          * A requested value is certainly on the way. Retrieve it now,
572          * to avoid leaving the device in a state where it's not expecting
573          * commands.
574          */
575         sr_scpi_get_float(scpi, NULL, &f);
576         sr_scpi_source_remove(sdi->session, scpi);
577
578         packet.type = SR_DF_END;
579         sr_session_send(sdi, &packet);
580
581         return SR_OK;
582 }
583
584 SR_PRIV struct sr_dev_driver scpi_pps_driver_info = {
585         .name = "scpi-pps",
586         .longname = "SCPI PPS",
587         .api_version = 1,
588         .init = init,
589         .cleanup = cleanup,
590         .scan = scan,
591         .dev_list = dev_list,
592         .dev_clear = dev_clear,
593         .config_get = config_get,
594         .config_set = config_set,
595         .config_list = config_list,
596         .dev_open = dev_open,
597         .dev_close = dev_close,
598         .dev_acquisition_start = dev_acquisition_start,
599         .dev_acquisition_stop = dev_acquisition_stop,
600         .priv = NULL,
601 };