]> sigrok.org Git - libsigrok.git/blame - src/hardware/hp-3457a/api.c
sr_dev_close(): Set status to SR_ST_INACTIVE.
[libsigrok.git] / src / hardware / hp-3457a / api.c
CommitLineData
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
25static const uint32_t scanopts[] = {
26 SR_CONF_CONN,
27};
28
29static const uint32_t drvopts[] = {
30 SR_CONF_MULTIMETER,
31};
32
33static 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 40static struct sr_dev_driver hp_3457a_driver_info;
00b2a092 41
db23af7f 42static 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
64static 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
104static 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
123static 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
169static GSList *scan(struct sr_dev_driver *di, GSList *options)
170{
171 return sr_scpi_scan(di->context, options, probe_device);
00b2a092
AG
172}
173
db23af7f
AG
174/*
175 * We need to set the HP 3457A to a known state, and there are quite a number
176 * of knobs to tweak. Here's a brief explanation of what's going on. For more
177 * details, print out and consult the user manual.
178 * PRESET
179 * Set the instrument to a pre-determined state. This is easier and faster
180 * than sending a few dozen commands. Some of the PRESET defaults include
181 * ASCII output format, and synchronous triggering. See user manual for
182 * more details.
183 *
184 * After the PRESET command, the instrument is in a known state, and only those
185 * parameters for which the default is unsuitable are modified:
186 * INBUF ON
187 * Enable the HP-IB input buffer. This allows the instrument to release the
188 * HP-IB bus before processing the command, and increases throughput on
189 * GPIB buses with more than one device.
190 * TRIG HOLD
191 * Do not trigger new measurements until instructed to do so.
192 */
00b2a092
AG
193static int dev_open(struct sr_dev_inst *sdi)
194{
db23af7f
AG
195 struct sr_scpi_dev_inst *scpi = sdi->conn;
196 struct dev_context *devc;
197
198 if (sr_scpi_open(scpi) != SR_OK)
199 return SR_ERR;
00b2a092 200
61c90858 201 devc = sdi->priv;
db23af7f
AG
202
203 sr_scpi_send(scpi, "PRESET");
204 sr_scpi_send(scpi, "INBUF ON");
205 sr_scpi_send(scpi, "TRIG HOLD");
206 sr_scpi_get_float(scpi, "NPLC?", &devc->nplc);
00b2a092 207
00b2a092
AG
208 return SR_OK;
209}
210
211static int dev_close(struct sr_dev_inst *sdi)
212{
db23af7f
AG
213 struct sr_scpi_dev_inst *scpi = sdi->conn;
214
9a093be9
AG
215 /* Disable scan-advance (preserve relay life). */
216 sr_scpi_send(scpi, "SADV HOLD");
db23af7f
AG
217 /* Switch back to auto-triggering. */
218 sr_scpi_send(scpi, "TRIG AUTO");
219
220 sr_scpi_close(scpi);
00b2a092 221
00b2a092
AG
222 return SR_OK;
223}
224
00b2a092
AG
225static int config_get(uint32_t key, GVariant **data,
226 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
227{
228 int ret;
db23af7f 229 struct dev_context *devc;
00b2a092 230
00b2a092
AG
231 (void)cg;
232
db23af7f
AG
233 devc = sdi->priv;
234
00b2a092
AG
235 ret = SR_OK;
236 switch (key) {
db23af7f
AG
237 case SR_CONF_ADC_POWERLINE_CYCLES:
238 *data = g_variant_new_double(devc->nplc);
239 break;
00b2a092
AG
240 default:
241 return SR_ERR_NA;
242 }
243
244 return ret;
245}
246
247static int config_set(uint32_t key, GVariant *data,
db23af7f
AG
248 const struct sr_dev_inst *sdi,
249 const struct sr_channel_group *cg)
00b2a092
AG
250{
251 int ret;
db23af7f 252 enum sr_mq mq;
2c04f943 253 enum sr_mqflag mq_flags;
db23af7f
AG
254 struct dev_context *devc;
255 GVariant *tuple_child;
00b2a092 256
00b2a092
AG
257 (void)cg;
258
db23af7f
AG
259 devc = sdi->priv;
260
00b2a092
AG
261 ret = SR_OK;
262 switch (key) {
db23af7f
AG
263 case SR_CONF_LIMIT_SAMPLES:
264 devc->limit_samples = g_variant_get_uint64(data);
265 break;
266 case SR_CONF_MEASURED_QUANTITY:
267 tuple_child = g_variant_get_child_value(data, 0);
268 mq = g_variant_get_uint32(tuple_child);
2c04f943
AG
269 tuple_child = g_variant_get_child_value(data, 1);
270 mq_flags = g_variant_get_uint64(tuple_child);
271 ret = hp_3457a_set_mq(sdi, mq, mq_flags);
db23af7f
AG
272 g_variant_unref(tuple_child);
273 break;
274 case SR_CONF_ADC_POWERLINE_CYCLES:
275 ret = hp_3457a_set_nplc(sdi, g_variant_get_double(data));
276 break;
00b2a092
AG
277 default:
278 ret = SR_ERR_NA;
279 }
280
281 return ret;
282}
283
284static int config_list(uint32_t key, GVariant **data,
285 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
286{
287 int ret;
288
db23af7f
AG
289 if (key == SR_CONF_SCAN_OPTIONS) {
290 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
291 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
292 return SR_OK;
293 } else if ((key == SR_CONF_DEVICE_OPTIONS) && !sdi) {
294 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
295 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
296 return SR_OK;
297 } else if ((key == SR_CONF_DEVICE_OPTIONS) && !cg) {
298 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
299 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
300 return SR_OK;
301 }
302
303 /* From here on, we're only concerned with channel group config. */
304 if (!cg)
305 return SR_ERR_NA;
306
307 /*
308 * TODO: Implement channel group configuration when adding support for
309 * plug-in cards.
310 */
00b2a092
AG
311
312 ret = SR_OK;
313 switch (key) {
00b2a092 314 default:
db23af7f 315 ret = SR_ERR_NA;
00b2a092
AG
316 }
317
318 return ret;
319}
320
9a093be9
AG
321static void create_channel_index_list(GSList *channels, GArray **arr)
322{
323 struct sr_channel *channel;
324 struct channel_context *chanc;
325 GSList *list_elem;
326
327 *arr = g_array_new(FALSE, FALSE, sizeof(unsigned int));
328
329 for (list_elem = channels; list_elem; list_elem = list_elem->next) {
330 channel = list_elem->data;
331 chanc = channel->priv;
332 g_array_append_val(*arr, chanc->index);
333 }
334}
335
db23af7f
AG
336/*
337 * TRIG SGL
338 * Trigger the first measurement, then hold. We can't let the instrument
339 * auto-trigger because we read several registers to make a complete
340 * reading. If the instrument were auto-triggering, we could get the
341 * reading for sample N, but a new measurement is made and when we read the
342 * HIRES register, it contains data for sample N+1. This would produce
343 * wrong readings.
9a093be9
AG
344 * SADV AUTO
345 * Activate the scan-advance feature. This automatically connects the next
346 * channel in the scan list to the A/D converter. This way, we do not need to
347 * occupy the HP-IB bus to send channel select commands.
db23af7f 348 */
695dc859 349static int dev_acquisition_start(const struct sr_dev_inst *sdi)
00b2a092 350{
db23af7f 351 int ret;
9a093be9 352 gboolean front_selected, rear_selected;
db23af7f 353 struct sr_scpi_dev_inst *scpi;
9a093be9 354 struct sr_channel *channel;
db23af7f 355 struct dev_context *devc;
9a093be9
AG
356 struct channel_context *chanc;
357 GArray *ch_list;
358 GSList *channels;
db23af7f 359
db23af7f
AG
360 scpi = sdi->conn;
361 devc = sdi->priv;
362
363 ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 100,
364 hp_3457a_receive_data, (void *)sdi);
365 if (ret != SR_OK)
366 return ret;
367
bee2b016 368 std_session_send_df_header(sdi);
db23af7f 369
9a093be9
AG
370 front_selected = rear_selected = FALSE;
371 devc->active_channels = NULL;
372
373 for (channels = sdi->channels; channels; channels = channels->next) {
374 channel = channels->data;
375
376 if (!channel->enabled)
377 continue;
378
379 chanc = channel->priv;
380
381 if (chanc->location == CONN_FRONT)
382 front_selected = TRUE;
383 if (chanc->location == CONN_REAR)
384 rear_selected = TRUE;
385
386 devc->active_channels = g_slist_append(devc->active_channels, channel);
387 }
388
389 if (front_selected && rear_selected) {
390 sr_err("Can not use front and rear channels at the same time!");
391 g_slist_free(devc->active_channels);
392 return SR_ERR_ARG;
393 }
394
395 devc->current_channel = devc->active_channels->data;
396 devc->num_active_channels = g_slist_length(devc->active_channels);
397
398 hp_3457a_select_input(sdi, front_selected ? CONN_FRONT : CONN_REAR);
399
400 /* For plug-in cards, use the scan-advance features to scan channels. */
401 if (rear_selected && (devc->rear_card->card_id != REAR_TERMINALS)) {
402 create_channel_index_list(devc->active_channels, &ch_list);
403 hp_3457a_send_scan_list(sdi, (void *)ch_list->data, ch_list->len);
404 sr_scpi_send(scpi, "SADV AUTO");
405 g_array_free(ch_list, TRUE);
406 }
407
db23af7f
AG
408 /* Start first measurement. */
409 sr_scpi_send(scpi, "TRIG SGL");
410 devc->acq_state = ACQ_TRIGGERED_MEASUREMENT;
411 devc->num_samples = 0;
00b2a092
AG
412
413 return SR_OK;
414}
415
695dc859 416static int dev_acquisition_stop(struct sr_dev_inst *sdi)
00b2a092 417{
9a093be9 418 struct dev_context *devc;
00b2a092 419
9a093be9
AG
420 devc = sdi->priv;
421
9a093be9
AG
422 g_slist_free(devc->active_channels);
423
00b2a092
AG
424 return SR_OK;
425}
426
dd5c48a6 427static struct sr_dev_driver hp_3457a_driver_info = {
00b2a092
AG
428 .name = "hp-3457a",
429 .longname = "HP 3457A",
430 .api_version = 1,
c2fdcc25 431 .init = std_init,
700d6b64 432 .cleanup = std_cleanup,
00b2a092 433 .scan = scan,
c01bf34c 434 .dev_list = std_dev_list,
00b2a092
AG
435 .config_get = config_get,
436 .config_set = config_set,
437 .config_list = config_list,
438 .dev_open = dev_open,
439 .dev_close = dev_close,
440 .dev_acquisition_start = dev_acquisition_start,
441 .dev_acquisition_stop = dev_acquisition_stop,
442 .context = NULL,
443};
dd5c48a6 444SR_REGISTER_DEV_DRIVER(hp_3457a_driver_info);