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