]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2014 Bert Vermeulen <bert@biot.com> | |
5 | * Copyright (C) 2017,2019 Frank Stettner <frank-stettner@gmx.net> | |
6 | * | |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
21 | #include <config.h> | |
22 | #include <string.h> | |
23 | #include <strings.h> | |
24 | #include "scpi.h" | |
25 | #include "protocol.h" | |
26 | ||
27 | static struct sr_dev_driver scpi_pps_driver_info; | |
28 | static struct sr_dev_driver hp_ib_pps_driver_info; | |
29 | ||
30 | static const uint32_t scanopts[] = { | |
31 | SR_CONF_CONN, | |
32 | SR_CONF_SERIALCOMM, | |
33 | }; | |
34 | ||
35 | static const uint32_t drvopts[] = { | |
36 | SR_CONF_POWER_SUPPLY, | |
37 | }; | |
38 | ||
39 | static const struct pps_channel_instance pci[] = { | |
40 | { SR_MQ_VOLTAGE, SCPI_CMD_GET_MEAS_VOLTAGE, "V" }, | |
41 | { SR_MQ_CURRENT, SCPI_CMD_GET_MEAS_CURRENT, "I" }, | |
42 | { SR_MQ_POWER, SCPI_CMD_GET_MEAS_POWER, "P" }, | |
43 | { SR_MQ_FREQUENCY, SCPI_CMD_GET_MEAS_FREQUENCY, "F" }, | |
44 | }; | |
45 | ||
46 | static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi, | |
47 | int (*get_hw_id)(struct sr_scpi_dev_inst *scpi, | |
48 | struct sr_scpi_hw_info **scpi_response)) | |
49 | { | |
50 | struct dev_context *devc; | |
51 | struct sr_dev_inst *sdi; | |
52 | struct sr_scpi_hw_info *hw_info; | |
53 | struct sr_channel_group *cg; | |
54 | struct sr_channel *ch; | |
55 | const struct scpi_pps *device; | |
56 | struct pps_channel *pch; | |
57 | struct channel_spec *channels; | |
58 | struct channel_group_spec *channel_groups, *cgs; | |
59 | struct pps_channel_group *pcg; | |
60 | GRegex *model_re; | |
61 | GMatchInfo *model_mi; | |
62 | GSList *l; | |
63 | uint64_t mask; | |
64 | unsigned int num_channels, num_channel_groups, ch_num, ch_idx, i, j; | |
65 | int ret; | |
66 | const char *vendor; | |
67 | char ch_name[16]; | |
68 | ||
69 | if (get_hw_id(scpi, &hw_info) != SR_OK) { | |
70 | sr_info("Couldn't get IDN response."); | |
71 | return NULL; | |
72 | } | |
73 | ||
74 | device = NULL; | |
75 | for (i = 0; i < num_pps_profiles; i++) { | |
76 | vendor = sr_vendor_alias(hw_info->manufacturer); | |
77 | if (g_ascii_strcasecmp(vendor, pps_profiles[i].vendor)) | |
78 | continue; | |
79 | model_re = g_regex_new(pps_profiles[i].model, 0, 0, NULL); | |
80 | if (g_regex_match(model_re, hw_info->model, 0, &model_mi)) | |
81 | device = &pps_profiles[i]; | |
82 | g_match_info_unref(model_mi); | |
83 | g_regex_unref(model_re); | |
84 | if (device) | |
85 | break; | |
86 | } | |
87 | if (!device) { | |
88 | sr_scpi_hw_info_free(hw_info); | |
89 | return NULL; | |
90 | } | |
91 | ||
92 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
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 | sr_sw_limits_init(&devc->limits); | |
104 | sdi->priv = devc; | |
105 | ||
106 | if (device->num_channels) { | |
107 | /* Static channels and groups. */ | |
108 | channels = (struct channel_spec *)device->channels; | |
109 | num_channels = device->num_channels; | |
110 | channel_groups = (struct channel_group_spec *)device->channel_groups; | |
111 | num_channel_groups = device->num_channel_groups; | |
112 | } else { | |
113 | /* Channels and groups need to be probed. */ | |
114 | ret = device->probe_channels(sdi, hw_info, &channels, &num_channels, | |
115 | &channel_groups, &num_channel_groups); | |
116 | if (ret != SR_OK) { | |
117 | sr_err("Failed to probe for channels."); | |
118 | return NULL; | |
119 | } | |
120 | /* | |
121 | * Since these were dynamically allocated, we'll need to free them | |
122 | * later. | |
123 | */ | |
124 | devc->channels = channels; | |
125 | devc->channel_groups = channel_groups; | |
126 | } | |
127 | ||
128 | ch_idx = 0; | |
129 | for (ch_num = 0; ch_num < num_channels; ch_num++) { | |
130 | /* Create one channel per measurable output unit. */ | |
131 | for (i = 0; i < ARRAY_SIZE(pci); i++) { | |
132 | if (!sr_scpi_cmd_get(devc->device->commands, pci[i].command)) | |
133 | continue; | |
134 | g_snprintf(ch_name, 16, "%s%s", pci[i].prefix, | |
135 | channels[ch_num].name); | |
136 | ch = sr_channel_new(sdi, ch_idx++, SR_CHANNEL_ANALOG, TRUE, | |
137 | 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 | } | |
144 | } | |
145 | ||
146 | for (i = 0; i < num_channel_groups; i++) { | |
147 | cgs = &channel_groups[i]; | |
148 | cg = sr_channel_group_new(sdi, cgs->name, NULL); | |
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 | /* Add mqflags from channel_group_spec only to voltage | |
155 | * and current channels. | |
156 | */ | |
157 | if (pch->mq == SR_MQ_VOLTAGE || pch->mq == SR_MQ_CURRENT) | |
158 | pch->mqflags = cgs->mqflags; | |
159 | else | |
160 | pch->mqflags = 0; | |
161 | if (pch->hw_output_idx == j) | |
162 | cg->channels = g_slist_append(cg->channels, ch); | |
163 | } | |
164 | } | |
165 | } | |
166 | pcg = g_malloc0(sizeof(struct pps_channel_group)); | |
167 | pcg->features = cgs->features; | |
168 | cg->priv = pcg; | |
169 | } | |
170 | ||
171 | sr_scpi_hw_info_free(hw_info); | |
172 | hw_info = NULL; | |
173 | ||
174 | /* Don't send SCPI_CMD_LOCAL for HP 66xxB using SCPI over GPIB. */ | |
175 | if (!(devc->device->dialect == SCPI_DIALECT_HP_66XXB && | |
176 | scpi->transport == SCPI_TRANSPORT_LIBGPIB)) | |
177 | sr_scpi_cmd(sdi, devc->device->commands, 0, NULL, SCPI_CMD_LOCAL); | |
178 | ||
179 | return sdi; | |
180 | } | |
181 | ||
182 | static gchar *hpib_get_revision(struct sr_scpi_dev_inst *scpi) | |
183 | { | |
184 | int ret; | |
185 | gboolean matches; | |
186 | char *response; | |
187 | GRegex *version_regex; | |
188 | ||
189 | ret = sr_scpi_get_string(scpi, "ROM?", &response); | |
190 | if (ret != SR_OK && !response) | |
191 | return NULL; | |
192 | ||
193 | /* Example version string: "B01 B01" */ | |
194 | version_regex = g_regex_new("[A-Z][0-9]{2} [A-Z][0-9]{2}", 0, 0, NULL); | |
195 | matches = g_regex_match(version_regex, response, 0, NULL); | |
196 | g_regex_unref(version_regex); | |
197 | ||
198 | if (!matches) { | |
199 | /* Not a valid version string. Ignore it. */ | |
200 | g_free(response); | |
201 | response = NULL; | |
202 | } else { | |
203 | /* Replace space with dot. */ | |
204 | response[3] = '.'; | |
205 | } | |
206 | ||
207 | return response; | |
208 | } | |
209 | ||
210 | /* | |
211 | * This function assumes the response is in the form "HP<model_number>" | |
212 | * | |
213 | * HP made many GPIB (then called HP-IB) instruments before the SCPI command | |
214 | * set was introduced into the standard. We haven't seen any non-HP instruments | |
215 | * which respond to the "ID?" query, so assume all are HP for now. | |
216 | */ | |
217 | static int hpib_get_hw_id(struct sr_scpi_dev_inst *scpi, | |
218 | struct sr_scpi_hw_info **scpi_response) | |
219 | { | |
220 | int ret; | |
221 | char *response; | |
222 | struct sr_scpi_hw_info *hw_info; | |
223 | ||
224 | ret = sr_scpi_get_string(scpi, "ID?", &response); | |
225 | if ((ret != SR_OK) || !response) | |
226 | return SR_ERR; | |
227 | ||
228 | hw_info = g_malloc0(sizeof(struct sr_scpi_hw_info)); | |
229 | ||
230 | *scpi_response = hw_info; | |
231 | hw_info->model = response; | |
232 | hw_info->firmware_version = hpib_get_revision(scpi); | |
233 | hw_info->manufacturer = g_strdup("HP"); | |
234 | ||
235 | return SR_OK; | |
236 | } | |
237 | ||
238 | static struct sr_dev_inst *probe_scpi_pps_device(struct sr_scpi_dev_inst *scpi) | |
239 | { | |
240 | return probe_device(scpi, sr_scpi_get_hw_id); | |
241 | } | |
242 | ||
243 | static struct sr_dev_inst *probe_hpib_pps_device(struct sr_scpi_dev_inst *scpi) | |
244 | { | |
245 | return probe_device(scpi, hpib_get_hw_id); | |
246 | } | |
247 | ||
248 | static GSList *scan_scpi_pps(struct sr_dev_driver *di, GSList *options) | |
249 | { | |
250 | return sr_scpi_scan(di->context, options, probe_scpi_pps_device); | |
251 | } | |
252 | ||
253 | static GSList *scan_hpib_pps(struct sr_dev_driver *di, GSList *options) | |
254 | { | |
255 | const char *conn; | |
256 | ||
257 | /* | |
258 | * Only scan for HP-IB devices when conn= was specified, to not | |
259 | * break SCPI devices' operation. | |
260 | */ | |
261 | conn = NULL; | |
262 | (void)sr_serial_extract_options(options, &conn, NULL); | |
263 | if (!conn) | |
264 | return NULL; | |
265 | ||
266 | return sr_scpi_scan(di->context, options, probe_hpib_pps_device); | |
267 | } | |
268 | ||
269 | static int dev_open(struct sr_dev_inst *sdi) | |
270 | { | |
271 | struct dev_context *devc; | |
272 | struct sr_scpi_dev_inst *scpi; | |
273 | GVariant *beeper; | |
274 | ||
275 | scpi = sdi->conn; | |
276 | if (sr_scpi_open(scpi) < 0) | |
277 | return SR_ERR; | |
278 | ||
279 | devc = sdi->priv; | |
280 | ||
281 | /* Don't send SCPI_CMD_REMOTE for HP 66xxB using SCPI over GPIB. */ | |
282 | if (!(devc->device->dialect == SCPI_DIALECT_HP_66XXB && | |
283 | scpi->transport == SCPI_TRANSPORT_LIBGPIB)) | |
284 | sr_scpi_cmd(sdi, devc->device->commands, 0, NULL, SCPI_CMD_REMOTE); | |
285 | ||
286 | devc->beeper_was_set = FALSE; | |
287 | if (sr_scpi_cmd_resp(sdi, devc->device->commands, 0, NULL, | |
288 | &beeper, G_VARIANT_TYPE_BOOLEAN, SCPI_CMD_BEEPER) == SR_OK) { | |
289 | if (g_variant_get_boolean(beeper)) { | |
290 | devc->beeper_was_set = TRUE; | |
291 | sr_scpi_cmd(sdi, devc->device->commands, | |
292 | 0, NULL, SCPI_CMD_BEEPER_DISABLE); | |
293 | } | |
294 | g_variant_unref(beeper); | |
295 | } | |
296 | ||
297 | return SR_OK; | |
298 | } | |
299 | ||
300 | static int dev_close(struct sr_dev_inst *sdi) | |
301 | { | |
302 | struct sr_scpi_dev_inst *scpi; | |
303 | struct dev_context *devc; | |
304 | ||
305 | devc = sdi->priv; | |
306 | scpi = sdi->conn; | |
307 | ||
308 | if (!scpi) | |
309 | return SR_ERR_BUG; | |
310 | ||
311 | if (devc->beeper_was_set) | |
312 | sr_scpi_cmd(sdi, devc->device->commands, | |
313 | 0, NULL, SCPI_CMD_BEEPER_ENABLE); | |
314 | ||
315 | /* Don't send SCPI_CMD_LOCAL for HP 66xxB using SCPI over GPIB. */ | |
316 | if (!(devc->device->dialect == SCPI_DIALECT_HP_66XXB && | |
317 | scpi->transport == SCPI_TRANSPORT_LIBGPIB)) | |
318 | sr_scpi_cmd(sdi, devc->device->commands, 0, NULL, SCPI_CMD_LOCAL); | |
319 | ||
320 | return sr_scpi_close(scpi); | |
321 | } | |
322 | ||
323 | static void clear_helper(struct dev_context *devc) | |
324 | { | |
325 | g_free(devc->channels); | |
326 | g_free(devc->channel_groups); | |
327 | } | |
328 | ||
329 | static int dev_clear(const struct sr_dev_driver *di) | |
330 | { | |
331 | return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper); | |
332 | } | |
333 | ||
334 | static int config_get(uint32_t key, GVariant **data, | |
335 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
336 | { | |
337 | struct dev_context *devc; | |
338 | const GVariantType *gvtype; | |
339 | unsigned int i; | |
340 | int channel_group_cmd; | |
341 | char *channel_group_name; | |
342 | int cmd, ret; | |
343 | const char *s; | |
344 | int reg; | |
345 | gboolean is_hmp_sqii; | |
346 | ||
347 | if (!sdi) | |
348 | return SR_ERR_ARG; | |
349 | ||
350 | devc = sdi->priv; | |
351 | ||
352 | if (cg) { | |
353 | /* | |
354 | * These options only apply to channel groups with a single | |
355 | * channel -- they're per-channel settings for the device. | |
356 | */ | |
357 | ||
358 | /* | |
359 | * Config keys are handled below depending on whether a channel | |
360 | * group was provided by the frontend. However some of these | |
361 | * take a CG on one PPS but not on others. Check the device's | |
362 | * profile for that here, and NULL out the channel group as needed. | |
363 | */ | |
364 | for (i = 0; i < devc->device->num_devopts; i++) { | |
365 | if (devc->device->devopts[i] == key) { | |
366 | cg = NULL; | |
367 | break; | |
368 | } | |
369 | } | |
370 | } | |
371 | ||
372 | gvtype = NULL; | |
373 | cmd = -1; | |
374 | switch (key) { | |
375 | case SR_CONF_ENABLED: | |
376 | gvtype = G_VARIANT_TYPE_BOOLEAN; | |
377 | cmd = SCPI_CMD_GET_OUTPUT_ENABLED; | |
378 | break; | |
379 | case SR_CONF_VOLTAGE: | |
380 | gvtype = G_VARIANT_TYPE_DOUBLE; | |
381 | cmd = SCPI_CMD_GET_MEAS_VOLTAGE; | |
382 | break; | |
383 | case SR_CONF_VOLTAGE_TARGET: | |
384 | gvtype = G_VARIANT_TYPE_DOUBLE; | |
385 | cmd = SCPI_CMD_GET_VOLTAGE_TARGET; | |
386 | break; | |
387 | case SR_CONF_OUTPUT_FREQUENCY: | |
388 | gvtype = G_VARIANT_TYPE_DOUBLE; | |
389 | cmd = SCPI_CMD_GET_MEAS_FREQUENCY; | |
390 | break; | |
391 | case SR_CONF_OUTPUT_FREQUENCY_TARGET: | |
392 | gvtype = G_VARIANT_TYPE_DOUBLE; | |
393 | cmd = SCPI_CMD_GET_FREQUENCY_TARGET; | |
394 | break; | |
395 | case SR_CONF_CURRENT: | |
396 | gvtype = G_VARIANT_TYPE_DOUBLE; | |
397 | cmd = SCPI_CMD_GET_MEAS_CURRENT; | |
398 | break; | |
399 | case SR_CONF_CURRENT_LIMIT: | |
400 | gvtype = G_VARIANT_TYPE_DOUBLE; | |
401 | cmd = SCPI_CMD_GET_CURRENT_LIMIT; | |
402 | break; | |
403 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: | |
404 | if (devc->device->dialect == SCPI_DIALECT_HMP) { | |
405 | /* OVP is always enabled. */ | |
406 | *data = g_variant_new_boolean(TRUE); | |
407 | return 0; | |
408 | } | |
409 | gvtype = G_VARIANT_TYPE_BOOLEAN; | |
410 | cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ENABLED; | |
411 | break; | |
412 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE: | |
413 | if (devc->device->dialect == SCPI_DIALECT_HP_66XXB || | |
414 | devc->device->dialect == SCPI_DIALECT_HP_COMP) | |
415 | gvtype = G_VARIANT_TYPE_STRING; | |
416 | else | |
417 | gvtype = G_VARIANT_TYPE_BOOLEAN; | |
418 | cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ACTIVE; | |
419 | break; | |
420 | case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD: | |
421 | gvtype = G_VARIANT_TYPE_DOUBLE; | |
422 | cmd = SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_THRESHOLD; | |
423 | break; | |
424 | case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: | |
425 | gvtype = G_VARIANT_TYPE_BOOLEAN; | |
426 | cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ENABLED; | |
427 | break; | |
428 | case SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE: | |
429 | if (devc->device->dialect == SCPI_DIALECT_HP_66XXB || | |
430 | devc->device->dialect == SCPI_DIALECT_HP_COMP) | |
431 | gvtype = G_VARIANT_TYPE_STRING; | |
432 | else | |
433 | gvtype = G_VARIANT_TYPE_BOOLEAN; | |
434 | cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ACTIVE; | |
435 | break; | |
436 | case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD: | |
437 | gvtype = G_VARIANT_TYPE_DOUBLE; | |
438 | cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_THRESHOLD; | |
439 | break; | |
440 | case SR_CONF_OVER_CURRENT_PROTECTION_DELAY: | |
441 | gvtype = G_VARIANT_TYPE_DOUBLE; | |
442 | cmd = SCPI_CMD_GET_OVER_CURRENT_PROTECTION_DELAY; | |
443 | break; | |
444 | case SR_CONF_OVER_TEMPERATURE_PROTECTION: | |
445 | if (devc->device->dialect == SCPI_DIALECT_HMP) { | |
446 | /* OTP is always enabled. */ | |
447 | *data = g_variant_new_boolean(TRUE); | |
448 | return 0; | |
449 | } | |
450 | gvtype = G_VARIANT_TYPE_BOOLEAN; | |
451 | cmd = SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION; | |
452 | break; | |
453 | case SR_CONF_OVER_TEMPERATURE_PROTECTION_ACTIVE: | |
454 | if (devc->device->dialect == SCPI_DIALECT_HP_66XXB || | |
455 | devc->device->dialect == SCPI_DIALECT_HP_COMP || | |
456 | devc->device->dialect == SCPI_DIALECT_HMP) | |
457 | gvtype = G_VARIANT_TYPE_STRING; | |
458 | else | |
459 | gvtype = G_VARIANT_TYPE_BOOLEAN; | |
460 | cmd = SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION_ACTIVE; | |
461 | break; | |
462 | case SR_CONF_REGULATION: | |
463 | gvtype = G_VARIANT_TYPE_STRING; | |
464 | cmd = SCPI_CMD_GET_OUTPUT_REGULATION; | |
465 | break; | |
466 | default: | |
467 | return sr_sw_limits_config_get(&devc->limits, key, data); | |
468 | } | |
469 | if (!gvtype) | |
470 | return SR_ERR_NA; | |
471 | ||
472 | channel_group_cmd = 0; | |
473 | channel_group_name = NULL; | |
474 | if (cg) { | |
475 | channel_group_cmd = SCPI_CMD_SELECT_CHANNEL; | |
476 | channel_group_name = g_strdup(cg->name); | |
477 | } | |
478 | ||
479 | is_hmp_sqii = FALSE; | |
480 | is_hmp_sqii |= cmd == SCPI_CMD_GET_OUTPUT_REGULATION; | |
481 | is_hmp_sqii |= cmd == SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION_ACTIVE; | |
482 | is_hmp_sqii &= devc->device->dialect == SCPI_DIALECT_HMP; | |
483 | if (is_hmp_sqii) { | |
484 | if (!cg) { | |
485 | /* STAT:QUES:INST:ISUMx query requires channel spec. */ | |
486 | sr_err("Need a channel group for regulation or OTP-active query."); | |
487 | return SR_ERR_NA; | |
488 | } | |
489 | ret = sr_scpi_cmd_resp(sdi, devc->device->commands, | |
490 | 0, NULL, data, gvtype, cmd, channel_group_name); | |
491 | } else { | |
492 | ret = sr_scpi_cmd_resp(sdi, devc->device->commands, | |
493 | channel_group_cmd, channel_group_name, data, gvtype, cmd); | |
494 | } | |
495 | g_free(channel_group_name); | |
496 | ||
497 | /* | |
498 | * Handle special cases | |
499 | */ | |
500 | ||
501 | if (cmd == SCPI_CMD_GET_OUTPUT_REGULATION) { | |
502 | if (devc->device->dialect == SCPI_DIALECT_PHILIPS) { | |
503 | /* | |
504 | * The Philips PM2800 series returns VOLT/CURR. We always return | |
505 | * a GVariant string in the Rigol notation (CV/CC/UR). | |
506 | */ | |
507 | s = g_variant_get_string(*data, NULL); | |
508 | if (!g_strcmp0(s, "VOLT")) { | |
509 | g_variant_unref(*data); | |
510 | *data = g_variant_new_string("CV"); | |
511 | } else if (!g_strcmp0(s, "CURR")) { | |
512 | g_variant_unref(*data); | |
513 | *data = g_variant_new_string("CC"); | |
514 | } | |
515 | } | |
516 | if (devc->device->dialect == SCPI_DIALECT_HP_COMP) { | |
517 | /* Evaluate Status Register from a HP 66xx in COMP mode. */ | |
518 | s = g_variant_get_string(*data, NULL); | |
519 | sr_atoi(s, ®); | |
520 | g_variant_unref(*data); | |
521 | if (reg & (1 << 0)) | |
522 | *data = g_variant_new_string("CV"); | |
523 | else if (reg & (1 << 1)) | |
524 | *data = g_variant_new_string("CC"); | |
525 | else if (reg & (1 << 2)) | |
526 | *data = g_variant_new_string("UR"); | |
527 | else if (reg & (1 << 9)) | |
528 | *data = g_variant_new_string("CC-"); | |
529 | else | |
530 | *data = g_variant_new_string(""); | |
531 | } | |
532 | if (devc->device->dialect == SCPI_DIALECT_HP_66XXB) { | |
533 | /* Evaluate Operational Status Register from a HP 66xxB. */ | |
534 | s = g_variant_get_string(*data, NULL); | |
535 | sr_atoi(s, ®); | |
536 | g_variant_unref(*data); | |
537 | if (reg & (1 << 8)) | |
538 | *data = g_variant_new_string("CV"); | |
539 | else if (reg & (1 << 10)) | |
540 | *data = g_variant_new_string("CC"); | |
541 | else if (reg & (1 << 11)) | |
542 | *data = g_variant_new_string("CC-"); | |
543 | else | |
544 | *data = g_variant_new_string("UR"); | |
545 | } | |
546 | if (devc->device->dialect == SCPI_DIALECT_HMP) { | |
547 | /* Evaluate Condition Status Register from a HMP series device. */ | |
548 | s = g_variant_get_string(*data, NULL); | |
549 | sr_atoi(s, ®); | |
550 | g_variant_unref(*data); | |
551 | if (reg & (1 << 0)) | |
552 | *data = g_variant_new_string("CC"); | |
553 | else if (reg & (1 << 1)) | |
554 | *data = g_variant_new_string("CV"); | |
555 | else | |
556 | *data = g_variant_new_string("UR"); | |
557 | } | |
558 | ||
559 | s = g_variant_get_string(*data, NULL); | |
560 | if (g_strcmp0(s, "CV") && g_strcmp0(s, "CC") && g_strcmp0(s, "CC-") && | |
561 | g_strcmp0(s, "UR") && g_strcmp0(s, "")) { | |
562 | ||
563 | sr_err("Unknown response to SCPI_CMD_GET_OUTPUT_REGULATION: %s", s); | |
564 | ret = SR_ERR_DATA; | |
565 | } | |
566 | } | |
567 | ||
568 | if (cmd == SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ACTIVE) { | |
569 | if (devc->device->dialect == SCPI_DIALECT_HP_COMP) { | |
570 | /* Evaluate Status Register from a HP 66xx in COMP mode. */ | |
571 | s = g_variant_get_string(*data, NULL); | |
572 | sr_atoi(s, ®); | |
573 | g_variant_unref(*data); | |
574 | *data = g_variant_new_boolean(reg & (1 << 3)); | |
575 | } | |
576 | if (devc->device->dialect == SCPI_DIALECT_HP_66XXB) { | |
577 | /* Evaluate Questionable Status Register bit 0 from a HP 66xxB. */ | |
578 | s = g_variant_get_string(*data, NULL); | |
579 | sr_atoi(s, ®); | |
580 | g_variant_unref(*data); | |
581 | *data = g_variant_new_boolean(reg & (1 << 0)); | |
582 | } | |
583 | } | |
584 | ||
585 | if (cmd == SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ACTIVE) { | |
586 | if (devc->device->dialect == SCPI_DIALECT_HP_COMP) { | |
587 | /* Evaluate Status Register from a HP 66xx in COMP mode. */ | |
588 | s = g_variant_get_string(*data, NULL); | |
589 | sr_atoi(s, ®); | |
590 | g_variant_unref(*data); | |
591 | *data = g_variant_new_boolean(reg & (1 << 6)); | |
592 | } | |
593 | if (devc->device->dialect == SCPI_DIALECT_HP_66XXB) { | |
594 | /* Evaluate Questionable Status Register bit 1 from a HP 66xxB. */ | |
595 | s = g_variant_get_string(*data, NULL); | |
596 | sr_atoi(s, ®); | |
597 | g_variant_unref(*data); | |
598 | *data = g_variant_new_boolean(reg & (1 << 1)); | |
599 | } | |
600 | } | |
601 | ||
602 | if (cmd == SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION_ACTIVE) { | |
603 | if (devc->device->dialect == SCPI_DIALECT_HP_COMP) { | |
604 | /* Evaluate Status Register from a HP 66xx in COMP mode. */ | |
605 | s = g_variant_get_string(*data, NULL); | |
606 | sr_atoi(s, ®); | |
607 | g_variant_unref(*data); | |
608 | *data = g_variant_new_boolean(reg & (1 << 4)); | |
609 | } | |
610 | if (devc->device->dialect == SCPI_DIALECT_HP_66XXB || | |
611 | devc->device->dialect == SCPI_DIALECT_HMP) { | |
612 | /* Evaluate Questionable Status Register bit 4 from a HP 66xxB. */ | |
613 | s = g_variant_get_string(*data, NULL); | |
614 | sr_atoi(s, ®); | |
615 | g_variant_unref(*data); | |
616 | *data = g_variant_new_boolean(reg & (1 << 4)); | |
617 | } | |
618 | } | |
619 | ||
620 | return ret; | |
621 | } | |
622 | ||
623 | static int config_set(uint32_t key, GVariant *data, | |
624 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
625 | { | |
626 | struct dev_context *devc; | |
627 | double d; | |
628 | int channel_group_cmd; | |
629 | char *channel_group_name; | |
630 | int ret; | |
631 | ||
632 | if (!sdi) | |
633 | return SR_ERR_ARG; | |
634 | ||
635 | channel_group_cmd = 0; | |
636 | channel_group_name = NULL; | |
637 | if (cg) { | |
638 | channel_group_cmd = SCPI_CMD_SELECT_CHANNEL; | |
639 | channel_group_name = g_strdup(cg->name); | |
640 | } | |
641 | ||
642 | devc = sdi->priv; | |
643 | ||
644 | switch (key) { | |
645 | case SR_CONF_ENABLED: | |
646 | if (g_variant_get_boolean(data)) | |
647 | ret = sr_scpi_cmd(sdi, devc->device->commands, | |
648 | channel_group_cmd, channel_group_name, | |
649 | SCPI_CMD_SET_OUTPUT_ENABLE); | |
650 | else | |
651 | ret = sr_scpi_cmd(sdi, devc->device->commands, | |
652 | channel_group_cmd, channel_group_name, | |
653 | SCPI_CMD_SET_OUTPUT_DISABLE); | |
654 | break; | |
655 | case SR_CONF_VOLTAGE_TARGET: | |
656 | d = g_variant_get_double(data); | |
657 | ret = sr_scpi_cmd(sdi, devc->device->commands, | |
658 | channel_group_cmd, channel_group_name, | |
659 | SCPI_CMD_SET_VOLTAGE_TARGET, d); | |
660 | break; | |
661 | case SR_CONF_OUTPUT_FREQUENCY_TARGET: | |
662 | d = g_variant_get_double(data); | |
663 | ret = sr_scpi_cmd(sdi, devc->device->commands, | |
664 | channel_group_cmd, channel_group_name, | |
665 | SCPI_CMD_SET_FREQUENCY_TARGET, d); | |
666 | break; | |
667 | case SR_CONF_CURRENT_LIMIT: | |
668 | d = g_variant_get_double(data); | |
669 | ret = sr_scpi_cmd(sdi, devc->device->commands, | |
670 | channel_group_cmd, channel_group_name, | |
671 | SCPI_CMD_SET_CURRENT_LIMIT, d); | |
672 | break; | |
673 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: | |
674 | if (g_variant_get_boolean(data)) | |
675 | ret = sr_scpi_cmd(sdi, devc->device->commands, | |
676 | channel_group_cmd, channel_group_name, | |
677 | SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_ENABLE); | |
678 | else | |
679 | ret = sr_scpi_cmd(sdi, devc->device->commands, | |
680 | channel_group_cmd, channel_group_name, | |
681 | SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_DISABLE); | |
682 | break; | |
683 | case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD: | |
684 | d = g_variant_get_double(data); | |
685 | ret = sr_scpi_cmd(sdi, devc->device->commands, | |
686 | channel_group_cmd, channel_group_name, | |
687 | SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_THRESHOLD, d); | |
688 | break; | |
689 | case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: | |
690 | if (g_variant_get_boolean(data)) | |
691 | ret = sr_scpi_cmd(sdi, devc->device->commands, | |
692 | channel_group_cmd, channel_group_name, | |
693 | SCPI_CMD_SET_OVER_CURRENT_PROTECTION_ENABLE); | |
694 | else | |
695 | ret = sr_scpi_cmd(sdi, devc->device->commands, | |
696 | channel_group_cmd, channel_group_name, | |
697 | SCPI_CMD_SET_OVER_CURRENT_PROTECTION_DISABLE); | |
698 | break; | |
699 | case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD: | |
700 | d = g_variant_get_double(data); | |
701 | ret = sr_scpi_cmd(sdi, devc->device->commands, | |
702 | channel_group_cmd, channel_group_name, | |
703 | SCPI_CMD_SET_OVER_CURRENT_PROTECTION_THRESHOLD, d); | |
704 | break; | |
705 | case SR_CONF_OVER_CURRENT_PROTECTION_DELAY: | |
706 | d = g_variant_get_double(data); | |
707 | ret = sr_scpi_cmd(sdi, devc->device->commands, | |
708 | channel_group_cmd, channel_group_name, | |
709 | SCPI_CMD_SET_OVER_CURRENT_PROTECTION_DELAY, d); | |
710 | break; | |
711 | case SR_CONF_OVER_TEMPERATURE_PROTECTION: | |
712 | if (g_variant_get_boolean(data)) | |
713 | ret = sr_scpi_cmd(sdi, devc->device->commands, | |
714 | channel_group_cmd, channel_group_name, | |
715 | SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_ENABLE); | |
716 | else | |
717 | ret = sr_scpi_cmd(sdi, devc->device->commands, | |
718 | channel_group_cmd, channel_group_name, | |
719 | SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_DISABLE); | |
720 | break; | |
721 | default: | |
722 | ret = sr_sw_limits_config_set(&devc->limits, key, data); | |
723 | } | |
724 | ||
725 | g_free(channel_group_name); | |
726 | ||
727 | return ret; | |
728 | } | |
729 | ||
730 | static int config_list(uint32_t key, GVariant **data, | |
731 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
732 | { | |
733 | struct dev_context *devc; | |
734 | struct sr_channel *ch; | |
735 | struct pps_channel *pch; | |
736 | const struct channel_spec *ch_spec; | |
737 | int i; | |
738 | const char *s[16]; | |
739 | ||
740 | devc = (sdi) ? sdi->priv : NULL; | |
741 | ||
742 | if (!cg) { | |
743 | switch (key) { | |
744 | case SR_CONF_SCAN_OPTIONS: | |
745 | case SR_CONF_DEVICE_OPTIONS: | |
746 | return std_opts_config_list(key, data, sdi, cg, | |
747 | ARRAY_AND_SIZE(scanopts), | |
748 | ARRAY_AND_SIZE(drvopts), | |
749 | (devc && devc->device) ? devc->device->devopts : NULL, | |
750 | (devc && devc->device) ? devc->device->num_devopts : 0); | |
751 | break; | |
752 | case SR_CONF_CHANNEL_CONFIG: | |
753 | if (!devc || !devc->device) | |
754 | return SR_ERR_ARG; | |
755 | /* Not used. */ | |
756 | i = 0; | |
757 | if (devc->device->features & PPS_INDEPENDENT) | |
758 | s[i++] = "Independent"; | |
759 | if (devc->device->features & PPS_SERIES) | |
760 | s[i++] = "Series"; | |
761 | if (devc->device->features & PPS_PARALLEL) | |
762 | s[i++] = "Parallel"; | |
763 | if (i == 0) { | |
764 | /* | |
765 | * Shouldn't happen: independent-only devices | |
766 | * shouldn't advertise this option at all. | |
767 | */ | |
768 | return SR_ERR_NA; | |
769 | } | |
770 | *data = g_variant_new_strv(s, i); | |
771 | break; | |
772 | default: | |
773 | return SR_ERR_NA; | |
774 | } | |
775 | } else { | |
776 | /* | |
777 | * Per-channel-group options depending on a channel are actually | |
778 | * done with the first channel. Channel groups in PPS can have | |
779 | * more than one channel, but they will typically be of equal | |
780 | * specification for use in series or parallel mode. | |
781 | */ | |
782 | ch = cg->channels->data; | |
783 | pch = ch->priv; | |
784 | if (!devc || !devc->device) | |
785 | return SR_ERR_ARG; | |
786 | ch_spec = &(devc->device->channels[pch->hw_output_idx]); | |
787 | ||
788 | switch (key) { | |
789 | case SR_CONF_DEVICE_OPTIONS: | |
790 | *data = std_gvar_array_u32(devc->device->devopts_cg, devc->device->num_devopts_cg); | |
791 | break; | |
792 | case SR_CONF_VOLTAGE_TARGET: | |
793 | *data = std_gvar_min_max_step_array(ch_spec->voltage); | |
794 | break; | |
795 | case SR_CONF_OUTPUT_FREQUENCY_TARGET: | |
796 | *data = std_gvar_min_max_step_array(ch_spec->frequency); | |
797 | break; | |
798 | case SR_CONF_CURRENT_LIMIT: | |
799 | *data = std_gvar_min_max_step_array(ch_spec->current); | |
800 | break; | |
801 | case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD: | |
802 | *data = std_gvar_min_max_step_array(ch_spec->ovp); | |
803 | break; | |
804 | case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD: | |
805 | *data = std_gvar_min_max_step_array(ch_spec->ocp); | |
806 | break; | |
807 | case SR_CONF_OVER_CURRENT_PROTECTION_DELAY: | |
808 | *data = std_gvar_min_max_step_array(ch_spec->ocp_delay); | |
809 | break; | |
810 | default: | |
811 | return SR_ERR_NA; | |
812 | } | |
813 | } | |
814 | ||
815 | return SR_OK; | |
816 | } | |
817 | ||
818 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
819 | { | |
820 | struct dev_context *devc; | |
821 | struct sr_scpi_dev_inst *scpi; | |
822 | int ret; | |
823 | ||
824 | devc = sdi->priv; | |
825 | scpi = sdi->conn; | |
826 | ||
827 | /* Prime the pipe with the first channel. */ | |
828 | devc->cur_acquisition_channel = sr_next_enabled_channel(sdi, NULL); | |
829 | ||
830 | /* Device specific initialization before acquisition starts. */ | |
831 | if (devc->device->init_acquisition) | |
832 | devc->device->init_acquisition(sdi); | |
833 | ||
834 | if ((ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 10, | |
835 | scpi_pps_receive_data, (void *)sdi)) != SR_OK) | |
836 | return ret; | |
837 | std_session_send_df_header(sdi); | |
838 | sr_sw_limits_acquisition_start(&devc->limits); | |
839 | ||
840 | return SR_OK; | |
841 | } | |
842 | ||
843 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
844 | { | |
845 | struct sr_scpi_dev_inst *scpi; | |
846 | ||
847 | scpi = sdi->conn; | |
848 | ||
849 | sr_scpi_source_remove(sdi->session, scpi); | |
850 | ||
851 | std_session_send_df_end(sdi); | |
852 | ||
853 | return SR_OK; | |
854 | } | |
855 | ||
856 | static struct sr_dev_driver scpi_pps_driver_info = { | |
857 | .name = "scpi-pps", | |
858 | .longname = "SCPI PPS", | |
859 | .api_version = 1, | |
860 | .init = std_init, | |
861 | .cleanup = std_cleanup, | |
862 | .scan = scan_scpi_pps, | |
863 | .dev_list = std_dev_list, | |
864 | .dev_clear = dev_clear, | |
865 | .config_get = config_get, | |
866 | .config_set = config_set, | |
867 | .config_list = config_list, | |
868 | .dev_open = dev_open, | |
869 | .dev_close = dev_close, | |
870 | .dev_acquisition_start = dev_acquisition_start, | |
871 | .dev_acquisition_stop = dev_acquisition_stop, | |
872 | .context = NULL, | |
873 | }; | |
874 | ||
875 | static struct sr_dev_driver hp_ib_pps_driver_info = { | |
876 | .name = "hpib-pps", | |
877 | .longname = "HP-IB PPS", | |
878 | .api_version = 1, | |
879 | .init = std_init, | |
880 | .cleanup = std_cleanup, | |
881 | .scan = scan_hpib_pps, | |
882 | .dev_list = std_dev_list, | |
883 | .dev_clear = dev_clear, | |
884 | .config_get = config_get, | |
885 | .config_set = config_set, | |
886 | .config_list = config_list, | |
887 | .dev_open = dev_open, | |
888 | .dev_close = dev_close, | |
889 | .dev_acquisition_start = dev_acquisition_start, | |
890 | .dev_acquisition_stop = dev_acquisition_stop, | |
891 | .context = NULL, | |
892 | }; | |
893 | SR_REGISTER_DEV_DRIVER(scpi_pps_driver_info); | |
894 | SR_REGISTER_DEV_DRIVER(hp_ib_pps_driver_info); |