]>
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; | |
9e45cd41 | 25 | |
584560f1 | 26 | static const uint32_t scanopts[] = { |
9e45cd41 BV |
27 | SR_CONF_CONN, |
28 | SR_CONF_SERIALCOMM, | |
29 | }; | |
ca1a7cb5 | 30 | |
9d9cf1c4 | 31 | static const uint32_t drvopts[] = { |
a258204e | 32 | SR_CONF_POWER_SUPPLY, |
a258204e BV |
33 | }; |
34 | ||
329733d9 | 35 | static const struct pps_channel_instance pci[] = { |
01b0257a BV |
36 | { SR_MQ_VOLTAGE, SCPI_CMD_GET_MEAS_VOLTAGE, "V" }, |
37 | { SR_MQ_CURRENT, SCPI_CMD_GET_MEAS_CURRENT, "I" }, | |
38 | { SR_MQ_POWER, SCPI_CMD_GET_MEAS_POWER, "P" }, | |
4264f1c0 | 39 | { SR_MQ_FREQUENCY, SCPI_CMD_GET_MEAS_FREQUENCY, "F" }, |
01b0257a BV |
40 | }; |
41 | ||
4f840ce9 | 42 | static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx) |
ca1a7cb5 BV |
43 | { |
44 | return std_init(sr_ctx, di, LOG_PREFIX); | |
45 | } | |
46 | ||
9e45cd41 | 47 | static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi) |
ca1a7cb5 | 48 | { |
9e45cd41 BV |
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; | |
01b0257a | 55 | struct pps_channel *pch; |
4a471029 BV |
56 | struct channel_spec *channels; |
57 | struct channel_group_spec *channel_groups, *cgs; | |
9e45cd41 | 58 | struct pps_channel_group *pcg; |
58b77c41 BV |
59 | GRegex *model_re; |
60 | GMatchInfo *model_mi; | |
01b0257a | 61 | GSList *l; |
9e45cd41 | 62 | uint64_t mask; |
4a471029 BV |
63 | unsigned int num_channels, num_channel_groups, ch_num, ch_idx, i, j; |
64 | int ret; | |
22c18b03 | 65 | const char *vendor; |
01b0257a | 66 | char ch_name[16]; |
9e45cd41 BV |
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++) { | |
22c18b03 | 75 | vendor = get_vendor(hw_info->manufacturer); |
58b77c41 | 76 | if (strcasecmp(vendor, pps_profiles[i].vendor)) |
22c18b03 | 77 | continue; |
58b77c41 BV |
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)) | |
9e45cd41 | 80 | device = &pps_profiles[i]; |
58b77c41 BV |
81 | g_match_info_unref(model_mi); |
82 | g_regex_unref(model_re); | |
83 | if (device) | |
9e45cd41 | 84 | break; |
9e45cd41 BV |
85 | } |
86 | if (!device) { | |
87 | sr_scpi_hw_info_free(hw_info); | |
88 | return NULL; | |
89 | } | |
90 | ||
aac29cc1 | 91 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
7d4f1741 | 92 | sdi->status = SR_ST_INACTIVE; |
0af636be UH |
93 | sdi->vendor = g_strdup(vendor); |
94 | sdi->model = g_strdup(hw_info->model); | |
95 | sdi->version = g_strdup(hw_info->firmware_version); | |
9e45cd41 | 96 | sdi->conn = scpi; |
4f840ce9 | 97 | sdi->driver = &scpi_pps_driver_info; |
9e45cd41 | 98 | sdi->inst_type = SR_INST_SCPI; |
d08667c5 SA |
99 | sdi->serial_num = g_strdup(hw_info->serial_number); |
100 | ||
9e45cd41 BV |
101 | devc = g_malloc0(sizeof(struct dev_context)); |
102 | devc->device = device; | |
103 | sdi->priv = devc; | |
104 | ||
4a471029 BV |
105 | if (device->num_channels) { |
106 | /* Static channels and groups. */ | |
329733d9 | 107 | channels = (struct channel_spec *)device->channels; |
4a471029 | 108 | num_channels = device->num_channels; |
329733d9 | 109 | channel_groups = (struct channel_group_spec *)device->channel_groups; |
4a471029 BV |
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 | ||
01b0257a | 127 | ch_idx = 0; |
4a471029 | 128 | for (ch_num = 0; ch_num < num_channels; ch_num++) { |
01b0257a | 129 | /* Create one channel per measurable output unit. */ |
01b0257a BV |
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, | |
4a471029 | 134 | channels[ch_num].name); |
5e23fcab ML |
135 | ch = sr_channel_new(sdi, ch_idx++, SR_CHANNEL_ANALOG, TRUE, |
136 | ch_name); | |
01b0257a BV |
137 | pch = g_malloc0(sizeof(struct pps_channel)); |
138 | pch->hw_output_idx = ch_num; | |
4a471029 | 139 | pch->hwname = channels[ch_num].name; |
01b0257a BV |
140 | pch->mq = pci[i].mq; |
141 | ch->priv = pch; | |
01b0257a | 142 | } |
9e45cd41 | 143 | } |
ca1a7cb5 | 144 | |
4a471029 BV |
145 | for (i = 0; i < num_channel_groups; i++) { |
146 | cgs = &channel_groups[i]; | |
9e45cd41 BV |
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) { | |
01b0257a BV |
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 | } | |
9e45cd41 BV |
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 | } | |
ca1a7cb5 | 164 | |
4a471029 BV |
165 | sr_scpi_hw_info_free(hw_info); |
166 | hw_info = NULL; | |
167 | ||
60475cd7 | 168 | scpi_cmd(sdi, SCPI_CMD_LOCAL); |
9e45cd41 | 169 | sr_scpi_close(scpi); |
ca1a7cb5 | 170 | |
9e45cd41 BV |
171 | return sdi; |
172 | } | |
ca1a7cb5 | 173 | |
4f840ce9 | 174 | static GSList *scan(struct sr_dev_driver *di, GSList *options) |
9e45cd41 BV |
175 | { |
176 | return sr_scpi_scan(di->priv, options, probe_device); | |
ca1a7cb5 BV |
177 | } |
178 | ||
4f840ce9 | 179 | static GSList *dev_list(const struct sr_dev_driver *di) |
ca1a7cb5 BV |
180 | { |
181 | return ((struct drv_context *)(di->priv))->instances; | |
182 | } | |
183 | ||
4f840ce9 | 184 | static int dev_clear(const struct sr_dev_driver *di) |
ca1a7cb5 BV |
185 | { |
186 | return std_dev_clear(di, NULL); | |
187 | } | |
188 | ||
189 | static int dev_open(struct sr_dev_inst *sdi) | |
190 | { | |
ee2860ee | 191 | struct dev_context *devc; |
9e45cd41 | 192 | struct sr_scpi_dev_inst *scpi; |
ee2860ee | 193 | GVariant *beeper; |
9e45cd41 | 194 | |
7d4f1741 | 195 | if (sdi->status != SR_ST_INACTIVE) |
9e45cd41 | 196 | return SR_ERR; |
ca1a7cb5 | 197 | |
9e45cd41 BV |
198 | scpi = sdi->conn; |
199 | if (sr_scpi_open(scpi) < 0) | |
200 | return SR_ERR; | |
ca1a7cb5 BV |
201 | |
202 | sdi->status = SR_ST_ACTIVE; | |
203 | ||
60475cd7 | 204 | scpi_cmd(sdi, SCPI_CMD_REMOTE); |
ee2860ee BV |
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 | } | |
60475cd7 | 214 | |
ca1a7cb5 BV |
215 | return SR_OK; |
216 | } | |
217 | ||
218 | static int dev_close(struct sr_dev_inst *sdi) | |
219 | { | |
9e45cd41 | 220 | struct sr_scpi_dev_inst *scpi; |
ee2860ee | 221 | struct dev_context *devc; |
ca1a7cb5 | 222 | |
9e45cd41 BV |
223 | if (sdi->status != SR_ST_ACTIVE) |
224 | return SR_ERR_DEV_CLOSED; | |
ca1a7cb5 | 225 | |
ee2860ee | 226 | devc = sdi->priv; |
9e45cd41 BV |
227 | scpi = sdi->conn; |
228 | if (scpi) { | |
ee2860ee BV |
229 | if (devc->beeper_was_set) |
230 | scpi_cmd(sdi, SCPI_CMD_BEEPER_ENABLE); | |
60475cd7 | 231 | scpi_cmd(sdi, SCPI_CMD_LOCAL); |
9e45cd41 BV |
232 | sr_scpi_close(scpi); |
233 | sdi->status = SR_ST_INACTIVE; | |
234 | } | |
ca1a7cb5 BV |
235 | |
236 | return SR_OK; | |
237 | } | |
238 | ||
4a471029 BV |
239 | static 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 | ||
4f840ce9 | 249 | static int cleanup(const struct sr_dev_driver *di) |
ca1a7cb5 | 250 | { |
4a471029 | 251 | return std_dev_clear(di, clear_helper); |
ca1a7cb5 BV |
252 | } |
253 | ||
584560f1 | 254 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
ca1a7cb5 BV |
255 | const struct sr_channel_group *cg) |
256 | { | |
9e45cd41 | 257 | struct dev_context *devc; |
478c8d92 BV |
258 | const GVariantType *gvtype; |
259 | unsigned int i; | |
260 | int cmd, ret; | |
ca1a7cb5 | 261 | |
9e45cd41 BV |
262 | if (!sdi) |
263 | return SR_ERR_ARG; | |
264 | ||
265 | devc = sdi->priv; | |
ca1a7cb5 | 266 | |
478c8d92 | 267 | if (cg) { |
9e45cd41 BV |
268 | /* |
269 | * These options only apply to channel groups with a single | |
270 | * channel -- they're per-channel settings for the device. | |
271 | */ | |
478c8d92 BV |
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 | } | |
478c8d92 | 285 | } |
9e45cd41 | 286 | |
478c8d92 BV |
287 | gvtype = NULL; |
288 | cmd = -1; | |
289 | switch (key) { | |
7a0b98b5 | 290 | case SR_CONF_ENABLED: |
478c8d92 BV |
291 | gvtype = G_VARIANT_TYPE_BOOLEAN; |
292 | cmd = SCPI_CMD_GET_OUTPUT_ENABLED; | |
293 | break; | |
7a0b98b5 | 294 | case SR_CONF_VOLTAGE: |
478c8d92 BV |
295 | gvtype = G_VARIANT_TYPE_DOUBLE; |
296 | cmd = SCPI_CMD_GET_MEAS_VOLTAGE; | |
297 | break; | |
7a0b98b5 | 298 | case SR_CONF_VOLTAGE_TARGET: |
478c8d92 | 299 | gvtype = G_VARIANT_TYPE_DOUBLE; |
ca95e90f | 300 | cmd = SCPI_CMD_GET_VOLTAGE_TARGET; |
478c8d92 | 301 | break; |
4264f1c0 AG |
302 | case SR_CONF_OUTPUT_FREQUENCY: |
303 | gvtype = G_VARIANT_TYPE_DOUBLE; | |
304 | cmd = SCPI_CMD_GET_MEAS_FREQUENCY; | |
305 | break; | |
306 | case SR_CONF_OUTPUT_FREQUENCY_TARGET: | |
307 | gvtype = G_VARIANT_TYPE_DOUBLE; | |
308 | cmd = SCPI_CMD_GET_FREQUENCY_TARGET; | |
309 | break; | |
7a0b98b5 | 310 | case SR_CONF_CURRENT: |
478c8d92 BV |
311 | gvtype = G_VARIANT_TYPE_DOUBLE; |
312 | cmd = SCPI_CMD_GET_MEAS_CURRENT; | |
313 | break; | |
7a0b98b5 | 314 | case SR_CONF_CURRENT_LIMIT: |
478c8d92 | 315 | gvtype = G_VARIANT_TYPE_DOUBLE; |
ca95e90f | 316 | cmd = SCPI_CMD_GET_CURRENT_LIMIT; |
478c8d92 BV |
317 | break; |
318 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: | |
319 | gvtype = G_VARIANT_TYPE_BOOLEAN; | |
320 | cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ENABLED; | |
321 | break; | |
322 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE: | |
323 | gvtype = G_VARIANT_TYPE_BOOLEAN; | |
324 | cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ACTIVE; | |
325 | break; | |
326 | case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD: | |
327 | gvtype = G_VARIANT_TYPE_DOUBLE; | |
328 | cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_THRESHOLD; | |
329 | break; | |
330 | case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: | |
331 | gvtype = G_VARIANT_TYPE_BOOLEAN; | |
332 | cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ENABLED; | |
333 | break; | |
334 | case SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE: | |
335 | gvtype = G_VARIANT_TYPE_BOOLEAN; | |
336 | cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ACTIVE; | |
337 | break; | |
338 | case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD: | |
339 | gvtype = G_VARIANT_TYPE_DOUBLE; | |
340 | cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_THRESHOLD; | |
341 | break; | |
342 | case SR_CONF_OVER_TEMPERATURE_PROTECTION: | |
343 | gvtype = G_VARIANT_TYPE_BOOLEAN; | |
344 | cmd = SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION; | |
345 | break; | |
7a0b98b5 | 346 | case SR_CONF_REGULATION: |
60475cd7 BV |
347 | gvtype = G_VARIANT_TYPE_STRING; |
348 | cmd = SCPI_CMD_GET_OUTPUT_REGULATION; | |
478c8d92 BV |
349 | } |
350 | if (gvtype) { | |
351 | if (cg) | |
60475cd7 BV |
352 | select_channel(sdi, cg->channels->data); |
353 | ret = scpi_cmd_resp(sdi, data, gvtype, cmd); | |
478c8d92 BV |
354 | } else |
355 | ret = SR_ERR_NA; | |
ca1a7cb5 BV |
356 | |
357 | return ret; | |
358 | } | |
359 | ||
584560f1 | 360 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, |
ca1a7cb5 BV |
361 | const struct sr_channel_group *cg) |
362 | { | |
9e45cd41 | 363 | double d; |
ca1a7cb5 | 364 | int ret; |
ca1a7cb5 | 365 | |
fdedbfcd BV |
366 | if (!sdi) |
367 | return SR_ERR_ARG; | |
368 | ||
ca1a7cb5 BV |
369 | if (sdi->status != SR_ST_ACTIVE) |
370 | return SR_ERR_DEV_CLOSED; | |
371 | ||
60475cd7 | 372 | if (cg) |
9e45cd41 | 373 | /* Channel group specified. */ |
60475cd7 BV |
374 | select_channel(sdi, cg->channels->data); |
375 | ||
376 | ret = SR_OK; | |
377 | switch (key) { | |
7a0b98b5 | 378 | case SR_CONF_ENABLED: |
60475cd7 BV |
379 | if (g_variant_get_boolean(data)) |
380 | ret = scpi_cmd(sdi, SCPI_CMD_SET_OUTPUT_ENABLE); | |
381 | else | |
382 | ret = scpi_cmd(sdi, SCPI_CMD_SET_OUTPUT_DISABLE); | |
383 | break; | |
7a0b98b5 | 384 | case SR_CONF_VOLTAGE_TARGET: |
60475cd7 BV |
385 | d = g_variant_get_double(data); |
386 | ret = scpi_cmd(sdi, SCPI_CMD_SET_VOLTAGE_TARGET, d); | |
387 | break; | |
4264f1c0 AG |
388 | case SR_CONF_OUTPUT_FREQUENCY_TARGET: |
389 | d = g_variant_get_double(data); | |
390 | ret = scpi_cmd(sdi, SCPI_CMD_SET_FREQUENCY_TARGET, d); | |
391 | break; | |
7a0b98b5 | 392 | case SR_CONF_CURRENT_LIMIT: |
60475cd7 BV |
393 | d = g_variant_get_double(data); |
394 | ret = scpi_cmd(sdi, SCPI_CMD_SET_CURRENT_LIMIT, d); | |
395 | break; | |
396 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: | |
397 | if (g_variant_get_boolean(data)) | |
398 | ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_ENABLE); | |
399 | else | |
400 | ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_DISABLE); | |
401 | break; | |
402 | case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD: | |
403 | d = g_variant_get_double(data); | |
404 | ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_THRESHOLD, d); | |
405 | break; | |
406 | case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: | |
407 | if (g_variant_get_boolean(data)) | |
408 | ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_CURRENT_PROTECTION_ENABLE); | |
409 | else | |
410 | ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_CURRENT_PROTECTION_DISABLE); | |
411 | break; | |
412 | case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD: | |
413 | d = g_variant_get_double(data); | |
414 | ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_CURRENT_PROTECTION_THRESHOLD, d); | |
415 | break; | |
416 | case SR_CONF_OVER_TEMPERATURE_PROTECTION: | |
417 | if (g_variant_get_boolean(data)) | |
418 | ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_ENABLE); | |
419 | else | |
420 | ret = scpi_cmd(sdi, SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_DISABLE); | |
421 | break; | |
422 | default: | |
423 | ret = SR_ERR_NA; | |
ca1a7cb5 BV |
424 | } |
425 | ||
426 | return ret; | |
427 | } | |
428 | ||
584560f1 | 429 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
ca1a7cb5 BV |
430 | const struct sr_channel_group *cg) |
431 | { | |
9e45cd41 BV |
432 | struct dev_context *devc; |
433 | struct sr_channel *ch; | |
329733d9 | 434 | const struct channel_spec *ch_spec; |
9e45cd41 BV |
435 | GVariant *gvar; |
436 | GVariantBuilder gvb; | |
437 | int ret, i; | |
438 | const char *s[16]; | |
439 | ||
440 | /* Always available, even without sdi. */ | |
441 | if (key == SR_CONF_SCAN_OPTIONS) { | |
584560f1 BV |
442 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, |
443 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); | |
9e45cd41 | 444 | return SR_OK; |
a258204e BV |
445 | } else if (key == SR_CONF_DEVICE_OPTIONS && !sdi) { |
446 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
9d9cf1c4 | 447 | drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t)); |
a258204e | 448 | return SR_OK; |
9e45cd41 | 449 | } |
ca1a7cb5 | 450 | |
9e45cd41 BV |
451 | if (!sdi) |
452 | return SR_ERR_ARG; | |
453 | devc = sdi->priv; | |
ca1a7cb5 BV |
454 | |
455 | ret = SR_OK; | |
9e45cd41 BV |
456 | if (!cg) { |
457 | /* No channel group: global options. */ | |
458 | switch (key) { | |
459 | case SR_CONF_DEVICE_OPTIONS: | |
584560f1 | 460 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, |
9e45cd41 | 461 | devc->device->devopts, devc->device->num_devopts, |
584560f1 | 462 | sizeof(uint32_t)); |
9e45cd41 | 463 | break; |
7a0b98b5 | 464 | case SR_CONF_CHANNEL_CONFIG: |
478c8d92 | 465 | /* Not used. */ |
9e45cd41 BV |
466 | i = 0; |
467 | if (devc->device->features & PPS_INDEPENDENT) | |
468 | s[i++] = "Independent"; | |
469 | if (devc->device->features & PPS_SERIES) | |
470 | s[i++] = "Series"; | |
471 | if (devc->device->features & PPS_PARALLEL) | |
472 | s[i++] = "Parallel"; | |
473 | if (i == 0) { | |
474 | /* | |
475 | * Shouldn't happen: independent-only devices | |
476 | * shouldn't advertise this option at all. | |
477 | */ | |
478 | return SR_ERR_NA; | |
479 | } | |
480 | *data = g_variant_new_strv(s, i); | |
481 | break; | |
482 | default: | |
483 | return SR_ERR_NA; | |
484 | } | |
485 | } else { | |
486 | /* Channel group specified. */ | |
9e45cd41 BV |
487 | /* |
488 | * Per-channel-group options depending on a channel are actually | |
489 | * done with the first channel. Channel groups in PPS can have | |
490 | * more than one channel, but they will typically be of equal | |
01b0257a | 491 | * specification for use in series or parallel mode. |
9e45cd41 | 492 | */ |
9e45cd41 BV |
493 | ch = cg->channels->data; |
494 | ||
495 | switch (key) { | |
496 | case SR_CONF_DEVICE_OPTIONS: | |
584560f1 | 497 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, |
9e45cd41 | 498 | devc->device->devopts_cg, devc->device->num_devopts_cg, |
584560f1 | 499 | sizeof(uint32_t)); |
9e45cd41 | 500 | break; |
7a0b98b5 | 501 | case SR_CONF_VOLTAGE_TARGET: |
9e45cd41 BV |
502 | ch_spec = &(devc->device->channels[ch->index]); |
503 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
504 | /* Min, max, write resolution. */ | |
505 | for (i = 0; i < 3; i++) { | |
506 | gvar = g_variant_new_double(ch_spec->voltage[i]); | |
507 | g_variant_builder_add_value(&gvb, gvar); | |
508 | } | |
509 | *data = g_variant_builder_end(&gvb); | |
510 | break; | |
4264f1c0 AG |
511 | case SR_CONF_OUTPUT_FREQUENCY_TARGET: |
512 | ch_spec = &(devc->device->channels[ch->index]); | |
513 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
514 | /* Min, max, write resolution. */ | |
515 | for (i = 0; i < 3; i++) { | |
516 | gvar = g_variant_new_double(ch_spec->frequency[i]); | |
517 | g_variant_builder_add_value(&gvb, gvar); | |
518 | } | |
519 | *data = g_variant_builder_end(&gvb); | |
520 | break; | |
7a0b98b5 | 521 | case SR_CONF_CURRENT_LIMIT: |
9e45cd41 BV |
522 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); |
523 | /* Min, max, step. */ | |
524 | for (i = 0; i < 3; i++) { | |
525 | ch_spec = &(devc->device->channels[ch->index]); | |
526 | gvar = g_variant_new_double(ch_spec->current[i]); | |
527 | g_variant_builder_add_value(&gvb, gvar); | |
528 | } | |
529 | *data = g_variant_builder_end(&gvb); | |
530 | break; | |
531 | default: | |
532 | return SR_ERR_NA; | |
533 | } | |
ca1a7cb5 BV |
534 | } |
535 | ||
536 | return ret; | |
537 | } | |
538 | ||
539 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, | |
9e45cd41 | 540 | void *cb_data) |
ca1a7cb5 | 541 | { |
9e45cd41 BV |
542 | struct dev_context *devc; |
543 | struct sr_scpi_dev_inst *scpi; | |
544 | struct sr_channel *ch; | |
01b0257a BV |
545 | struct pps_channel *pch; |
546 | int cmd, ret; | |
ca1a7cb5 BV |
547 | |
548 | if (sdi->status != SR_ST_ACTIVE) | |
549 | return SR_ERR_DEV_CLOSED; | |
550 | ||
9e45cd41 BV |
551 | devc = sdi->priv; |
552 | scpi = sdi->conn; | |
553 | devc->cb_data = cb_data; | |
554 | ||
01b0257a | 555 | if ((ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 10, |
9e45cd41 BV |
556 | scpi_pps_receive_data, (void *)sdi)) != SR_OK) |
557 | return ret; | |
558 | std_session_send_df_header(sdi, LOG_PREFIX); | |
559 | ||
01b0257a | 560 | /* Prime the pipe with the first channel's fetch. */ |
984e4b0d | 561 | ch = next_enabled_channel(sdi, NULL); |
01b0257a | 562 | pch = ch->priv; |
c1603f45 | 563 | if ((ret = select_channel(sdi, ch)) < 0) |
984e4b0d | 564 | return ret; |
01b0257a BV |
565 | if (pch->mq == SR_MQ_VOLTAGE) |
566 | cmd = SCPI_CMD_GET_MEAS_VOLTAGE; | |
4264f1c0 AG |
567 | else if (pch->mq == SR_MQ_FREQUENCY) |
568 | cmd = SCPI_CMD_GET_MEAS_FREQUENCY; | |
01b0257a BV |
569 | else if (pch->mq == SR_MQ_CURRENT) |
570 | cmd = SCPI_CMD_GET_MEAS_CURRENT; | |
571 | else if (pch->mq == SR_MQ_POWER) | |
572 | cmd = SCPI_CMD_GET_MEAS_POWER; | |
573 | else | |
574 | return SR_ERR; | |
575 | scpi_cmd(sdi, cmd, pch->hwname); | |
ca1a7cb5 BV |
576 | |
577 | return SR_OK; | |
578 | } | |
579 | ||
580 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
581 | { | |
bf48cceb | 582 | struct sr_datafeed_packet packet; |
9e45cd41 BV |
583 | struct sr_scpi_dev_inst *scpi; |
584 | float f; | |
585 | ||
ca1a7cb5 BV |
586 | (void)cb_data; |
587 | ||
588 | if (sdi->status != SR_ST_ACTIVE) | |
589 | return SR_ERR_DEV_CLOSED; | |
590 | ||
9e45cd41 BV |
591 | scpi = sdi->conn; |
592 | ||
593 | /* | |
594 | * A requested value is certainly on the way. Retrieve it now, | |
595 | * to avoid leaving the device in a state where it's not expecting | |
596 | * commands. | |
597 | */ | |
598 | sr_scpi_get_float(scpi, NULL, &f); | |
599 | sr_scpi_source_remove(sdi->session, scpi); | |
600 | ||
bf48cceb BV |
601 | packet.type = SR_DF_END; |
602 | sr_session_send(sdi, &packet); | |
603 | ||
ca1a7cb5 BV |
604 | return SR_OK; |
605 | } | |
606 | ||
607 | SR_PRIV struct sr_dev_driver scpi_pps_driver_info = { | |
608 | .name = "scpi-pps", | |
609 | .longname = "SCPI PPS", | |
610 | .api_version = 1, | |
611 | .init = init, | |
612 | .cleanup = cleanup, | |
613 | .scan = scan, | |
614 | .dev_list = dev_list, | |
615 | .dev_clear = dev_clear, | |
616 | .config_get = config_get, | |
617 | .config_set = config_set, | |
618 | .config_list = config_list, | |
619 | .dev_open = dev_open, | |
620 | .dev_close = dev_close, | |
621 | .dev_acquisition_start = dev_acquisition_start, | |
622 | .dev_acquisition_stop = dev_acquisition_stop, | |
623 | .priv = NULL, | |
624 | }; |