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