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