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