]>
Commit | Line | Data |
---|---|---|
00b2a092 AG |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2016 Alexandru Gagniuc <mr.nuke.me@gmail.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 <config.h> | |
db23af7f AG |
21 | #include <scpi.h> |
22 | #include <string.h> | |
00b2a092 AG |
23 | #include "protocol.h" |
24 | ||
db23af7f AG |
25 | static const uint32_t scanopts[] = { |
26 | SR_CONF_CONN, | |
27 | }; | |
28 | ||
29 | static const uint32_t drvopts[] = { | |
30 | SR_CONF_MULTIMETER, | |
31 | }; | |
32 | ||
33 | static const uint32_t devopts[] = { | |
34 | SR_CONF_CONTINUOUS | SR_CONF_SET, | |
35 | SR_CONF_LIMIT_SAMPLES | SR_CONF_SET, | |
36 | SR_CONF_MEASURED_QUANTITY | SR_CONF_SET, | |
37 | SR_CONF_ADC_POWERLINE_CYCLES | SR_CONF_SET | SR_CONF_GET, | |
38 | }; | |
39 | ||
00b2a092 AG |
40 | SR_PRIV struct sr_dev_driver hp_3457a_driver_info; |
41 | ||
db23af7f | 42 | static int create_front_channel(struct sr_dev_inst *sdi, int chan_idx) |
00b2a092 | 43 | { |
db23af7f AG |
44 | struct sr_channel *channel; |
45 | struct sr_channel_group *front; | |
9a093be9 AG |
46 | struct channel_context *chanc; |
47 | ||
48 | chanc = g_malloc(sizeof(*chanc)); | |
49 | chanc->location = CONN_FRONT; | |
db23af7f AG |
50 | |
51 | channel = sr_channel_new(sdi, chan_idx++, SR_CHANNEL_ANALOG, | |
52 | TRUE, "Front"); | |
9a093be9 | 53 | channel->priv = chanc; |
db23af7f AG |
54 | |
55 | front = g_malloc0(sizeof(*front)); | |
56 | front->name = g_strdup("Front"); | |
57 | front->channels = g_slist_append(front->channels, channel); | |
9a093be9 | 58 | |
db23af7f AG |
59 | sdi->channel_groups = g_slist_append(sdi->channel_groups, front); |
60 | ||
61 | return chan_idx; | |
00b2a092 AG |
62 | } |
63 | ||
db23af7f AG |
64 | static int create_rear_channels(struct sr_dev_inst *sdi, int chan_idx, |
65 | const struct rear_card_info *card) | |
66 | { | |
9a093be9 AG |
67 | unsigned int i; |
68 | struct sr_channel *channel; | |
69 | struct sr_channel_group *group; | |
70 | struct channel_context *chanc; | |
71 | char name[16]; | |
db23af7f AG |
72 | |
73 | /* When card is NULL, we couldn't identify the type of card. */ | |
74 | if (!card) | |
75 | return chan_idx; | |
76 | ||
9a093be9 AG |
77 | group = g_malloc0(sizeof(*group)); |
78 | group->priv = NULL; | |
79 | group->name = g_strdup(card->cg_name); | |
80 | sdi->channel_groups = g_slist_append(sdi->channel_groups, group); | |
81 | ||
82 | for (i = 0; i < card->num_channels; i++) { | |
83 | ||
84 | chanc = g_malloc(sizeof(*chanc)); | |
85 | chanc->location = CONN_REAR; | |
86 | ||
87 | if (card->type == REAR_TERMINALS) { | |
88 | chanc->index = -1; | |
89 | g_snprintf(name, sizeof(name), "%s", card->cg_name); | |
90 | } else { | |
91 | chanc->index = i; | |
92 | g_snprintf(name, sizeof(name), "%s%u", card->cg_name, i); | |
93 | } | |
94 | ||
95 | channel = sr_channel_new(sdi, chan_idx++, SR_CHANNEL_ANALOG, | |
96 | FALSE, name); | |
97 | channel->priv = chanc; | |
98 | group->channels = g_slist_append(group->channels, channel); | |
99 | } | |
100 | ||
db23af7f AG |
101 | return chan_idx; |
102 | } | |
103 | ||
104 | static gchar *get_revision(struct sr_scpi_dev_inst *scpi) | |
00b2a092 | 105 | { |
db23af7f AG |
106 | int ret, major, minor; |
107 | GArray *rev_numbers; | |
108 | ||
109 | /* Report a version of '0.0' if we can't parse the response. */ | |
110 | major = minor = 0; | |
111 | ||
112 | ret = sr_scpi_get_floatv(scpi, "REV?", &rev_numbers); | |
113 | if ((ret == SR_OK) && (rev_numbers->len >= 2)) { | |
114 | major = (int)g_array_index(rev_numbers, float, 0); | |
115 | minor = (int)g_array_index(rev_numbers, float, 1); | |
116 | } | |
00b2a092 | 117 | |
db23af7f | 118 | g_array_free(rev_numbers, TRUE); |
00b2a092 | 119 | |
db23af7f AG |
120 | return g_strdup_printf("%d.%d", major, minor); |
121 | } | |
00b2a092 | 122 | |
db23af7f AG |
123 | static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi) |
124 | { | |
125 | int ret, idx; | |
126 | char *response; | |
127 | struct sr_dev_inst *sdi; | |
128 | struct dev_context *devc; | |
129 | ||
130 | /* | |
131 | * This command ensures we receive an EOI after every response, so that | |
132 | * we don't wait the entire timeout after the response is received. | |
133 | */ | |
134 | if (sr_scpi_send(scpi, "END ALWAYS") != SR_OK) | |
135 | return NULL; | |
136 | ||
137 | ret = sr_scpi_get_string(scpi, "ID?", &response); | |
138 | if ((ret != SR_OK) || !response) | |
139 | return NULL; | |
140 | ||
141 | if (strcmp(response, "HP3457A")) | |
142 | return NULL; | |
143 | ||
144 | g_free(response); | |
145 | ||
146 | devc = g_malloc0(sizeof(struct dev_context)); | |
147 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
148 | sdi->vendor = g_strdup("Hewlett-Packard"); | |
149 | sdi->model = g_strdup("3457A"); | |
150 | sdi->version = get_revision(scpi); | |
151 | sdi->conn = scpi; | |
152 | sdi->driver = &hp_3457a_driver_info; | |
153 | sdi->inst_type = SR_INST_SCPI; | |
154 | sdi->priv = devc; | |
155 | ||
156 | /* There is no way to probe the measurement mode. It must be set. */ | |
157 | devc->measurement_mq = 0; | |
158 | devc->measurement_unit = 0; | |
159 | ||
160 | /* Probe rear card option and create channels accordingly (TODO). */ | |
161 | devc->rear_card = hp_3457a_probe_rear_card(scpi); | |
162 | idx = 0; | |
163 | idx = create_front_channel(sdi, idx); | |
164 | create_rear_channels(sdi, idx, devc->rear_card); | |
165 | ||
166 | return sdi; | |
167 | } | |
00b2a092 | 168 | |
db23af7f AG |
169 | static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx) |
170 | { | |
171 | return std_init(sr_ctx, di, LOG_PREFIX); | |
172 | } | |
173 | ||
174 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
175 | { | |
176 | return sr_scpi_scan(di->context, options, probe_device); | |
00b2a092 AG |
177 | } |
178 | ||
179 | static GSList *dev_list(const struct sr_dev_driver *di) | |
180 | { | |
181 | return ((struct drv_context *)(di->context))->instances; | |
182 | } | |
183 | ||
db23af7f AG |
184 | /* |
185 | * We need to set the HP 3457A to a known state, and there are quite a number | |
186 | * of knobs to tweak. Here's a brief explanation of what's going on. For more | |
187 | * details, print out and consult the user manual. | |
188 | * PRESET | |
189 | * Set the instrument to a pre-determined state. This is easier and faster | |
190 | * than sending a few dozen commands. Some of the PRESET defaults include | |
191 | * ASCII output format, and synchronous triggering. See user manual for | |
192 | * more details. | |
193 | * | |
194 | * After the PRESET command, the instrument is in a known state, and only those | |
195 | * parameters for which the default is unsuitable are modified: | |
196 | * INBUF ON | |
197 | * Enable the HP-IB input buffer. This allows the instrument to release the | |
198 | * HP-IB bus before processing the command, and increases throughput on | |
199 | * GPIB buses with more than one device. | |
200 | * TRIG HOLD | |
201 | * Do not trigger new measurements until instructed to do so. | |
202 | */ | |
00b2a092 AG |
203 | static int dev_open(struct sr_dev_inst *sdi) |
204 | { | |
db23af7f AG |
205 | struct sr_scpi_dev_inst *scpi = sdi->conn; |
206 | struct dev_context *devc; | |
207 | ||
208 | if (sr_scpi_open(scpi) != SR_OK) | |
209 | return SR_ERR; | |
00b2a092 | 210 | |
61c90858 | 211 | devc = sdi->priv; |
db23af7f AG |
212 | |
213 | sr_scpi_send(scpi, "PRESET"); | |
214 | sr_scpi_send(scpi, "INBUF ON"); | |
215 | sr_scpi_send(scpi, "TRIG HOLD"); | |
216 | sr_scpi_get_float(scpi, "NPLC?", &devc->nplc); | |
00b2a092 AG |
217 | |
218 | sdi->status = SR_ST_ACTIVE; | |
219 | ||
220 | return SR_OK; | |
221 | } | |
222 | ||
223 | static int dev_close(struct sr_dev_inst *sdi) | |
224 | { | |
db23af7f AG |
225 | struct sr_scpi_dev_inst *scpi = sdi->conn; |
226 | ||
227 | if (sdi->status != SR_ST_ACTIVE) | |
228 | return SR_ERR_DEV_CLOSED; | |
00b2a092 | 229 | |
9a093be9 AG |
230 | /* Disable scan-advance (preserve relay life). */ |
231 | sr_scpi_send(scpi, "SADV HOLD"); | |
db23af7f AG |
232 | /* Switch back to auto-triggering. */ |
233 | sr_scpi_send(scpi, "TRIG AUTO"); | |
234 | ||
235 | sr_scpi_close(scpi); | |
00b2a092 AG |
236 | |
237 | sdi->status = SR_ST_INACTIVE; | |
238 | ||
239 | return SR_OK; | |
240 | } | |
241 | ||
00b2a092 AG |
242 | static int config_get(uint32_t key, GVariant **data, |
243 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
244 | { | |
245 | int ret; | |
db23af7f | 246 | struct dev_context *devc; |
00b2a092 | 247 | |
00b2a092 AG |
248 | (void)cg; |
249 | ||
db23af7f AG |
250 | devc = sdi->priv; |
251 | ||
00b2a092 AG |
252 | ret = SR_OK; |
253 | switch (key) { | |
db23af7f AG |
254 | case SR_CONF_ADC_POWERLINE_CYCLES: |
255 | *data = g_variant_new_double(devc->nplc); | |
256 | break; | |
00b2a092 AG |
257 | default: |
258 | return SR_ERR_NA; | |
259 | } | |
260 | ||
261 | return ret; | |
262 | } | |
263 | ||
264 | static int config_set(uint32_t key, GVariant *data, | |
db23af7f AG |
265 | const struct sr_dev_inst *sdi, |
266 | const struct sr_channel_group *cg) | |
00b2a092 AG |
267 | { |
268 | int ret; | |
db23af7f | 269 | enum sr_mq mq; |
2c04f943 | 270 | enum sr_mqflag mq_flags; |
db23af7f AG |
271 | struct dev_context *devc; |
272 | GVariant *tuple_child; | |
00b2a092 | 273 | |
00b2a092 AG |
274 | (void)cg; |
275 | ||
276 | if (sdi->status != SR_ST_ACTIVE) | |
277 | return SR_ERR_DEV_CLOSED; | |
278 | ||
db23af7f AG |
279 | devc = sdi->priv; |
280 | ||
00b2a092 AG |
281 | ret = SR_OK; |
282 | switch (key) { | |
db23af7f AG |
283 | case SR_CONF_LIMIT_SAMPLES: |
284 | devc->limit_samples = g_variant_get_uint64(data); | |
285 | break; | |
286 | case SR_CONF_MEASURED_QUANTITY: | |
287 | tuple_child = g_variant_get_child_value(data, 0); | |
288 | mq = g_variant_get_uint32(tuple_child); | |
2c04f943 AG |
289 | tuple_child = g_variant_get_child_value(data, 1); |
290 | mq_flags = g_variant_get_uint64(tuple_child); | |
291 | ret = hp_3457a_set_mq(sdi, mq, mq_flags); | |
db23af7f AG |
292 | g_variant_unref(tuple_child); |
293 | break; | |
294 | case SR_CONF_ADC_POWERLINE_CYCLES: | |
295 | ret = hp_3457a_set_nplc(sdi, g_variant_get_double(data)); | |
296 | break; | |
00b2a092 AG |
297 | default: |
298 | ret = SR_ERR_NA; | |
299 | } | |
300 | ||
301 | return ret; | |
302 | } | |
303 | ||
304 | static int config_list(uint32_t key, GVariant **data, | |
305 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
306 | { | |
307 | int ret; | |
308 | ||
db23af7f AG |
309 | if (key == SR_CONF_SCAN_OPTIONS) { |
310 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
311 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); | |
312 | return SR_OK; | |
313 | } else if ((key == SR_CONF_DEVICE_OPTIONS) && !sdi) { | |
314 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
315 | drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t)); | |
316 | return SR_OK; | |
317 | } else if ((key == SR_CONF_DEVICE_OPTIONS) && !cg) { | |
318 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
319 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); | |
320 | return SR_OK; | |
321 | } | |
322 | ||
323 | /* From here on, we're only concerned with channel group config. */ | |
324 | if (!cg) | |
325 | return SR_ERR_NA; | |
326 | ||
327 | /* | |
328 | * TODO: Implement channel group configuration when adding support for | |
329 | * plug-in cards. | |
330 | */ | |
00b2a092 AG |
331 | |
332 | ret = SR_OK; | |
333 | switch (key) { | |
00b2a092 | 334 | default: |
db23af7f | 335 | ret = SR_ERR_NA; |
00b2a092 AG |
336 | } |
337 | ||
338 | return ret; | |
339 | } | |
340 | ||
9a093be9 AG |
341 | static void create_channel_index_list(GSList *channels, GArray **arr) |
342 | { | |
343 | struct sr_channel *channel; | |
344 | struct channel_context *chanc; | |
345 | GSList *list_elem; | |
346 | ||
347 | *arr = g_array_new(FALSE, FALSE, sizeof(unsigned int)); | |
348 | ||
349 | for (list_elem = channels; list_elem; list_elem = list_elem->next) { | |
350 | channel = list_elem->data; | |
351 | chanc = channel->priv; | |
352 | g_array_append_val(*arr, chanc->index); | |
353 | } | |
354 | } | |
355 | ||
db23af7f AG |
356 | /* |
357 | * TRIG SGL | |
358 | * Trigger the first measurement, then hold. We can't let the instrument | |
359 | * auto-trigger because we read several registers to make a complete | |
360 | * reading. If the instrument were auto-triggering, we could get the | |
361 | * reading for sample N, but a new measurement is made and when we read the | |
362 | * HIRES register, it contains data for sample N+1. This would produce | |
363 | * wrong readings. | |
9a093be9 AG |
364 | * SADV AUTO |
365 | * Activate the scan-advance feature. This automatically connects the next | |
366 | * channel in the scan list to the A/D converter. This way, we do not need to | |
367 | * occupy the HP-IB bus to send channel select commands. | |
db23af7f | 368 | */ |
695dc859 | 369 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) |
00b2a092 | 370 | { |
db23af7f | 371 | int ret; |
9a093be9 | 372 | gboolean front_selected, rear_selected; |
db23af7f | 373 | struct sr_scpi_dev_inst *scpi; |
9a093be9 | 374 | struct sr_channel *channel; |
db23af7f | 375 | struct dev_context *devc; |
9a093be9 AG |
376 | struct channel_context *chanc; |
377 | GArray *ch_list; | |
378 | GSList *channels; | |
db23af7f | 379 | |
00b2a092 AG |
380 | if (sdi->status != SR_ST_ACTIVE) |
381 | return SR_ERR_DEV_CLOSED; | |
382 | ||
db23af7f AG |
383 | scpi = sdi->conn; |
384 | devc = sdi->priv; | |
385 | ||
386 | ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 100, | |
387 | hp_3457a_receive_data, (void *)sdi); | |
388 | if (ret != SR_OK) | |
389 | return ret; | |
390 | ||
391 | std_session_send_df_header(sdi, LOG_PREFIX); | |
392 | ||
9a093be9 AG |
393 | front_selected = rear_selected = FALSE; |
394 | devc->active_channels = NULL; | |
395 | ||
396 | for (channels = sdi->channels; channels; channels = channels->next) { | |
397 | channel = channels->data; | |
398 | ||
399 | if (!channel->enabled) | |
400 | continue; | |
401 | ||
402 | chanc = channel->priv; | |
403 | ||
404 | if (chanc->location == CONN_FRONT) | |
405 | front_selected = TRUE; | |
406 | if (chanc->location == CONN_REAR) | |
407 | rear_selected = TRUE; | |
408 | ||
409 | devc->active_channels = g_slist_append(devc->active_channels, channel); | |
410 | } | |
411 | ||
412 | if (front_selected && rear_selected) { | |
413 | sr_err("Can not use front and rear channels at the same time!"); | |
414 | g_slist_free(devc->active_channels); | |
415 | return SR_ERR_ARG; | |
416 | } | |
417 | ||
418 | devc->current_channel = devc->active_channels->data; | |
419 | devc->num_active_channels = g_slist_length(devc->active_channels); | |
420 | ||
421 | hp_3457a_select_input(sdi, front_selected ? CONN_FRONT : CONN_REAR); | |
422 | ||
423 | /* For plug-in cards, use the scan-advance features to scan channels. */ | |
424 | if (rear_selected && (devc->rear_card->card_id != REAR_TERMINALS)) { | |
425 | create_channel_index_list(devc->active_channels, &ch_list); | |
426 | hp_3457a_send_scan_list(sdi, (void *)ch_list->data, ch_list->len); | |
427 | sr_scpi_send(scpi, "SADV AUTO"); | |
428 | g_array_free(ch_list, TRUE); | |
429 | } | |
430 | ||
db23af7f AG |
431 | /* Start first measurement. */ |
432 | sr_scpi_send(scpi, "TRIG SGL"); | |
433 | devc->acq_state = ACQ_TRIGGERED_MEASUREMENT; | |
434 | devc->num_samples = 0; | |
00b2a092 AG |
435 | |
436 | return SR_OK; | |
437 | } | |
438 | ||
695dc859 | 439 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) |
00b2a092 | 440 | { |
9a093be9 | 441 | struct dev_context *devc; |
00b2a092 | 442 | |
9a093be9 AG |
443 | devc = sdi->priv; |
444 | ||
9a093be9 AG |
445 | g_slist_free(devc->active_channels); |
446 | ||
00b2a092 AG |
447 | return SR_OK; |
448 | } | |
449 | ||
450 | SR_PRIV struct sr_dev_driver hp_3457a_driver_info = { | |
451 | .name = "hp-3457a", | |
452 | .longname = "HP 3457A", | |
453 | .api_version = 1, | |
454 | .init = init, | |
700d6b64 | 455 | .cleanup = std_cleanup, |
00b2a092 AG |
456 | .scan = scan, |
457 | .dev_list = dev_list, | |
00b2a092 AG |
458 | .config_get = config_get, |
459 | .config_set = config_set, | |
460 | .config_list = config_list, | |
461 | .dev_open = dev_open, | |
462 | .dev_close = dev_close, | |
463 | .dev_acquisition_start = dev_acquisition_start, | |
464 | .dev_acquisition_stop = dev_acquisition_stop, | |
465 | .context = NULL, | |
466 | }; |