]> sigrok.org Git - libsigrok.git/blob - src/hardware/scpi-pps/api.c
scpi-pps: Properly clean up acquisition session.
[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 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" },
37 };
38
39 static int init(struct sr_context *sr_ctx)
40 {
41         return std_init(sr_ctx, di, LOG_PREFIX);
42 }
43
44 static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
45 {
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;
55         GRegex *model_re;
56         GMatchInfo *model_mi;
57         GSList *l;
58         uint64_t mask;
59         unsigned int ch_num, ch_idx, i, j;
60         const char *vendor;
61         char ch_name[16];
62
63         if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
64                 sr_info("Couldn't get IDN response.");
65                 return NULL;
66         }
67
68         device = NULL;
69         for (i = 0; i < num_pps_profiles; i++) {
70                 vendor = get_vendor(hw_info->manufacturer);
71                 if (strcasecmp(vendor, pps_profiles[i].vendor))
72                         continue;
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);
78                 if (device)
79                         break;
80         }
81         if (!device) {
82                 sr_scpi_hw_info_free(hw_info);
83                 return NULL;
84         }
85
86         sdi = sr_dev_inst_new(SR_ST_ACTIVE, vendor, hw_info->model,
87                         hw_info->firmware_version);
88         sdi->conn = scpi;
89         sdi->driver = di;
90         sdi->inst_type = SR_INST_SCPI;
91         devc = g_malloc0(sizeof(struct dev_context));
92         devc->device = device;
93         sdi->priv = devc;
94
95         ch_idx = 0;
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))
100                                 continue;
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;
107                         pch->mq = pci[i].mq;
108                         ch->priv = pch;
109                         sdi->channels = g_slist_append(sdi->channels, ch);
110                 }
111         }
112
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) {
120                                         ch = l->data;
121                                         pch = ch->priv;
122                                         if (pch->hw_output_idx == j)
123                                                 cg->channels = g_slist_append(cg->channels, ch);
124                                 }
125                         }
126                 }
127                 pcg = g_malloc0(sizeof(struct pps_channel_group));
128                 pcg->features = cgs->features;
129                 cg->priv = pcg;
130                 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
131         }
132
133         scpi_cmd(sdi, SCPI_CMD_LOCAL);
134         sr_scpi_close(scpi);
135
136         return sdi;
137 }
138
139 static GSList *scan(GSList *options)
140 {
141         return sr_scpi_scan(di->priv, options, probe_device);
142 }
143
144 static GSList *dev_list(void)
145 {
146         return ((struct drv_context *)(di->priv))->instances;
147 }
148
149 static int dev_clear(void)
150 {
151         return std_dev_clear(di, NULL);
152 }
153
154 static int dev_open(struct sr_dev_inst *sdi)
155 {
156         struct sr_scpi_dev_inst *scpi;
157
158         if (sdi->status != SR_ST_ACTIVE)
159                 return SR_ERR;
160
161         scpi = sdi->conn;
162         if (sr_scpi_open(scpi) < 0)
163                 return SR_ERR;
164
165         sdi->status = SR_ST_ACTIVE;
166
167         scpi_cmd(sdi, SCPI_CMD_REMOTE);
168
169         return SR_OK;
170 }
171
172 static int dev_close(struct sr_dev_inst *sdi)
173 {
174         struct sr_scpi_dev_inst *scpi;
175
176         if (sdi->status != SR_ST_ACTIVE)
177                 return SR_ERR_DEV_CLOSED;
178
179         scpi = sdi->conn;
180         if (scpi) {
181                 scpi_cmd(sdi, SCPI_CMD_LOCAL);
182                 sr_scpi_close(scpi);
183                 sdi->status = SR_ST_INACTIVE;
184         }
185
186         return SR_OK;
187 }
188
189 static int cleanup(void)
190 {
191         return std_dev_clear(di, NULL);
192 }
193
194 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
195                 const struct sr_channel_group *cg)
196 {
197         struct dev_context *devc;
198         const GVariantType *gvtype;
199         unsigned int i;
200         int cmd, ret;
201         const char *s;
202
203         if (!sdi)
204                 return SR_ERR_ARG;
205
206         devc = sdi->priv;
207
208         if (cg) {
209                 /*
210                  * These options only apply to channel groups with a single
211                  * channel -- they're per-channel settings for the device.
212                  */
213
214                 /*
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.
219                  */
220                 for (i = 0; i < devc->device->num_devopts; i++) {
221                         if (devc->device->devopts[i] == key) {
222                                 cg = NULL;
223                                 break;
224                         }
225                 }
226         }
227
228         gvtype = NULL;
229         cmd = -1;
230         switch (key) {
231         case SR_CONF_OUTPUT_ENABLED:
232                 gvtype = G_VARIANT_TYPE_BOOLEAN;
233                 cmd = SCPI_CMD_GET_OUTPUT_ENABLED;
234                 break;
235         case SR_CONF_OUTPUT_VOLTAGE:
236                 gvtype = G_VARIANT_TYPE_DOUBLE;
237                 cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
238                 break;
239         case SR_CONF_OUTPUT_VOLTAGE_TARGET:
240                 gvtype = G_VARIANT_TYPE_DOUBLE;
241                 cmd = SCPI_CMD_GET_VOLTAGE_TARGET;
242                 break;
243         case SR_CONF_OUTPUT_CURRENT:
244                 gvtype = G_VARIANT_TYPE_DOUBLE;
245                 cmd = SCPI_CMD_GET_MEAS_CURRENT;
246                 break;
247         case SR_CONF_OUTPUT_CURRENT_LIMIT:
248                 gvtype = G_VARIANT_TYPE_DOUBLE;
249                 cmd = SCPI_CMD_GET_CURRENT_LIMIT;
250                 break;
251         case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
252                 gvtype = G_VARIANT_TYPE_BOOLEAN;
253                 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ENABLED;
254                 break;
255         case SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE:
256                 gvtype = G_VARIANT_TYPE_BOOLEAN;
257                 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ACTIVE;
258                 break;
259         case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
260                 gvtype = G_VARIANT_TYPE_DOUBLE;
261                 cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_THRESHOLD;
262                 break;
263         case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
264                 gvtype = G_VARIANT_TYPE_BOOLEAN;
265                 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ENABLED;
266                 break;
267         case SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE:
268                 gvtype = G_VARIANT_TYPE_BOOLEAN;
269                 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ACTIVE;
270                 break;
271         case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
272                 gvtype = G_VARIANT_TYPE_DOUBLE;
273                 cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_THRESHOLD;
274                 break;
275         case SR_CONF_OVER_TEMPERATURE_PROTECTION:
276                 gvtype = G_VARIANT_TYPE_BOOLEAN;
277                 cmd = SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION;
278                 break;
279         case SR_CONF_OUTPUT_REGULATION:
280                 gvtype = G_VARIANT_TYPE_STRING;
281                 cmd = SCPI_CMD_GET_OUTPUT_REGULATION;
282         }
283         if (gvtype) {
284                 if (cg)
285                         select_channel(sdi, cg->channels->data);
286                 ret = scpi_cmd_resp(sdi, data, gvtype, cmd);
287
288                 if (gvtype == G_VARIANT_TYPE_STRING && ret == SR_OK) {
289                         /* Non-standard data type responses. */
290                         switch (key) {
291                         case SCPI_CMD_GET_OUTPUT_REGULATION:
292                                 /*
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.
297                                  */
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);
301                                         ret = SR_ERR_DATA;
302                                 }
303                                 break;
304                         }
305                 }
306         } else
307                 ret = SR_ERR_NA;
308
309         return ret;
310 }
311
312 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
313                 const struct sr_channel_group *cg)
314 {
315         double d;
316         int ret;
317
318         if (!sdi)
319                 return SR_ERR_ARG;
320
321         if (sdi->status != SR_ST_ACTIVE)
322                 return SR_ERR_DEV_CLOSED;
323
324         if (cg)
325                 /* Channel group specified. */
326                 select_channel(sdi, cg->channels->data);
327
328         ret = SR_OK;
329         switch (key) {
330         case SR_CONF_OUTPUT_ENABLED:
331                 if (g_variant_get_boolean(data))
332                         ret = scpi_cmd(sdi, SCPI_CMD_SET_OUTPUT_ENABLE);
333                 else
334                         ret = scpi_cmd(sdi, SCPI_CMD_SET_OUTPUT_DISABLE);
335                 break;
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);
339                 break;
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);
343                 break;
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);
347                 else
348                         ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_DISABLE);
349                 break;
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);
353                 break;
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);
357                 else
358                         ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_CURRENT_PROTECTION_DISABLE);
359                 break;
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);
363                 break;
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);
367                 else
368                         ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_DISABLE);
369                 break;
370         default:
371                 ret = SR_ERR_NA;
372         }
373
374         return ret;
375 }
376
377 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
378                 const struct sr_channel_group *cg)
379 {
380         struct dev_context *devc;
381         struct sr_channel *ch;
382         struct channel_spec *ch_spec;
383         GVariant *gvar;
384         GVariantBuilder gvb;
385         int ret, i;
386         const char *s[16];
387
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));
392                 return SR_OK;
393         }
394
395         if (!sdi)
396                 return SR_ERR_ARG;
397         devc = sdi->priv;
398
399         ret = SR_OK;
400         if (!cg) {
401                 /* No channel group: global options. */
402                 switch (key) {
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,
406                                         sizeof(uint32_t));
407                         break;
408                 case SR_CONF_OUTPUT_CHANNEL_CONFIG:
409                         /* Not used. */
410                         i = 0;
411                         if (devc->device->features & PPS_INDEPENDENT)
412                                 s[i++] = "Independent";
413                         if (devc->device->features & PPS_SERIES)
414                                 s[i++] = "Series";
415                         if (devc->device->features & PPS_PARALLEL)
416                                 s[i++] = "Parallel";
417                         if (i == 0) {
418                                 /*
419                                  * Shouldn't happen: independent-only devices
420                                  * shouldn't advertise this option at all.
421                                  */
422                                 return SR_ERR_NA;
423                         }
424                         *data = g_variant_new_strv(s, i);
425                         break;
426                 default:
427                         return SR_ERR_NA;
428                 }
429         } else {
430                 /* Channel group specified. */
431                 /*
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.
436                  */
437                 ch = cg->channels->data;
438
439                 switch (key) {
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,
443                                         sizeof(uint32_t));
444                         break;
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);
452                         }
453                         *data = g_variant_builder_end(&gvb);
454                         break;
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);
462                         }
463                         *data = g_variant_builder_end(&gvb);
464                         break;
465                 default:
466                         return SR_ERR_NA;
467                 }
468         }
469
470         return ret;
471 }
472
473 static int dev_acquisition_start(const struct sr_dev_inst *sdi,
474                 void *cb_data)
475 {
476         struct dev_context *devc;
477         struct sr_scpi_dev_inst *scpi;
478         struct sr_channel *ch;
479         struct pps_channel *pch;
480         int cmd, ret;
481
482         if (sdi->status != SR_ST_ACTIVE)
483                 return SR_ERR_DEV_CLOSED;
484
485         devc = sdi->priv;
486         scpi = sdi->conn;
487         devc->cb_data = cb_data;
488
489         if ((ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 10,
490                         scpi_pps_receive_data, (void *)sdi)) != SR_OK)
491                 return ret;
492         std_session_send_df_header(sdi, LOG_PREFIX);
493
494         /* Prime the pipe with the first channel's fetch. */
495         ch = sdi->channels->data;
496         pch = ch->priv;
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;
504         else
505                 return SR_ERR;
506         scpi_cmd(sdi, cmd, pch->hwname);
507
508         return SR_OK;
509 }
510
511 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
512 {
513         struct sr_datafeed_packet packet;
514         struct sr_scpi_dev_inst *scpi;
515         float f;
516
517         (void)cb_data;
518
519         if (sdi->status != SR_ST_ACTIVE)
520                 return SR_ERR_DEV_CLOSED;
521
522         scpi = sdi->conn;
523
524         /*
525          * A requested value is certainly on the way. Retrieve it now,
526          * to avoid leaving the device in a state where it's not expecting
527          * commands.
528          */
529         sr_scpi_get_float(scpi, NULL, &f);
530         sr_scpi_source_remove(sdi->session, scpi);
531
532         packet.type = SR_DF_END;
533         sr_session_send(sdi, &packet);
534
535         return SR_OK;
536 }
537
538 SR_PRIV struct sr_dev_driver scpi_pps_driver_info = {
539         .name = "scpi-pps",
540         .longname = "SCPI PPS",
541         .api_version = 1,
542         .init = init,
543         .cleanup = cleanup,
544         .scan = scan,
545         .dev_list = dev_list,
546         .dev_clear = dev_clear,
547         .config_get = config_get,
548         .config_set = config_set,
549         .config_list = config_list,
550         .dev_open = dev_open,
551         .dev_close = dev_close,
552         .dev_acquisition_start = dev_acquisition_start,
553         .dev_acquisition_stop = dev_acquisition_stop,
554         .priv = NULL,
555 };