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