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