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