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