]> sigrok.org Git - libsigrok.git/blame_incremental - src/hardware/hp-3478a/api.c
hp-3478a: Add get/set/list of measurement ranges.
[libsigrok.git] / src / hardware / hp-3478a / api.c
... / ...
CommitLineData
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2017 Frank Stettner <frank-stettner@gmx.net>
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>
21#include "scpi.h"
22#include "protocol.h"
23
24static const uint32_t scanopts[] = {
25 SR_CONF_CONN,
26};
27
28static const uint32_t drvopts[] = {
29 SR_CONF_MULTIMETER,
30};
31
32static const uint32_t devopts[] = {
33 SR_CONF_CONTINUOUS,
34 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
35 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
36 SR_CONF_MEASURED_QUANTITY | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
37 SR_CONF_RANGE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
38};
39
40static const struct {
41 enum sr_mq mq;
42 enum sr_mqflag mqflag;
43} mqopts[] = {
44 {SR_MQ_VOLTAGE, SR_MQFLAG_DC},
45 {SR_MQ_VOLTAGE, SR_MQFLAG_AC},
46 {SR_MQ_CURRENT, SR_MQFLAG_DC},
47 {SR_MQ_CURRENT, SR_MQFLAG_AC},
48 {SR_MQ_RESISTANCE, 0},
49 {SR_MQ_RESISTANCE, SR_MQFLAG_FOUR_WIRE},
50};
51
52
53static const struct {
54 enum sr_mq mq;
55 enum sr_mqflag mqflag;
56 int range_exp;
57 const char *range_str;
58} rangeopts[] = {
59 /* -99 is a dummy exponent for auto ranging. */
60 {SR_MQ_VOLTAGE, SR_MQFLAG_DC, -99, "Auto"},
61 {SR_MQ_VOLTAGE, SR_MQFLAG_DC, -2, "30mV"},
62 {SR_MQ_VOLTAGE, SR_MQFLAG_DC, -1, "300mV"},
63 {SR_MQ_VOLTAGE, SR_MQFLAG_DC, 0, "3V"},
64 {SR_MQ_VOLTAGE, SR_MQFLAG_DC, 1, "30V"},
65 {SR_MQ_VOLTAGE, SR_MQFLAG_DC, 2, "300V"},
66 /* -99 is a dummy exponent for auto ranging. */
67 {SR_MQ_VOLTAGE, SR_MQFLAG_AC, -99, "Auto"},
68 {SR_MQ_VOLTAGE, SR_MQFLAG_AC, -1, "300mV"},
69 {SR_MQ_VOLTAGE, SR_MQFLAG_AC, 0, "3V"},
70 {SR_MQ_VOLTAGE, SR_MQFLAG_AC, 1, "30V"},
71 {SR_MQ_VOLTAGE, SR_MQFLAG_AC, 2, "300V"},
72 /* -99 is a dummy exponent for auto ranging. */
73 {SR_MQ_CURRENT, SR_MQFLAG_DC, -99, "Auto"},
74 {SR_MQ_CURRENT, SR_MQFLAG_DC, -1, "300mV"},
75 {SR_MQ_CURRENT, SR_MQFLAG_DC, 0, "3V"},
76 /* -99 is a dummy exponent for auto ranging. */
77 {SR_MQ_CURRENT, SR_MQFLAG_AC, -99, "Auto"},
78 {SR_MQ_CURRENT, SR_MQFLAG_AC, -1, "300mV"},
79 {SR_MQ_CURRENT, SR_MQFLAG_AC, 0, "3V"},
80 /* -99 is a dummy exponent for auto ranging. */
81 {SR_MQ_RESISTANCE, 0, -99, "Auto"},
82 {SR_MQ_RESISTANCE, 0, 1, "30"},
83 {SR_MQ_RESISTANCE, 0, 2, "300"},
84 {SR_MQ_RESISTANCE, 0, 3, "3k"},
85 {SR_MQ_RESISTANCE, 0, 4, "30k"},
86 {SR_MQ_RESISTANCE, 0, 5, "300k"},
87 {SR_MQ_RESISTANCE, 0, 6, "3M"},
88 {SR_MQ_RESISTANCE, 0, 7, "30M"},
89 /* -99 is a dummy exponent for auto ranging. */
90 {SR_MQ_RESISTANCE, SR_MQFLAG_FOUR_WIRE, -99, "Auto"},
91 {SR_MQ_RESISTANCE, SR_MQFLAG_FOUR_WIRE, 1, "30R"},
92 {SR_MQ_RESISTANCE, SR_MQFLAG_FOUR_WIRE, 2, "300R"},
93 {SR_MQ_RESISTANCE, SR_MQFLAG_FOUR_WIRE, 3, "3kR"},
94 {SR_MQ_RESISTANCE, SR_MQFLAG_FOUR_WIRE, 4, "30kR"},
95 {SR_MQ_RESISTANCE, SR_MQFLAG_FOUR_WIRE, 5, "300kR"},
96 {SR_MQ_RESISTANCE, SR_MQFLAG_FOUR_WIRE, 6, "3MR"},
97 {SR_MQ_RESISTANCE, SR_MQFLAG_FOUR_WIRE, 7, "30MR"},
98};
99
100static struct sr_dev_driver hp_3478a_driver_info;
101
102static int create_front_channel(struct sr_dev_inst *sdi, int chan_idx)
103{
104 struct sr_channel *channel;
105 struct channel_context *chanc;
106
107 chanc = g_malloc(sizeof(*chanc));
108 chanc->location = TERMINAL_FRONT;
109
110 channel = sr_channel_new(sdi, chan_idx++, SR_CHANNEL_ANALOG, TRUE, "P1");
111 channel->priv = chanc;
112
113 return chan_idx;
114}
115
116static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
117{
118 int ret;
119 struct sr_dev_inst *sdi;
120 struct dev_context *devc;
121
122 sdi = g_malloc0(sizeof(struct sr_dev_inst));
123 sdi->vendor = g_strdup("Hewlett-Packard");
124 sdi->model = g_strdup("3478A");
125 sdi->conn = scpi;
126 sdi->driver = &hp_3478a_driver_info;
127 sdi->inst_type = SR_INST_SCPI;
128
129 devc = g_malloc0(sizeof(struct dev_context));
130 sr_sw_limits_init(&devc->limits);
131 sdi->priv = devc;
132
133 /* Get actual status (function, digits, ...). */
134 ret = hp_3478a_get_status_bytes(sdi);
135 if (ret != SR_OK)
136 return NULL;
137
138 create_front_channel(sdi, 0);
139
140 return sdi;
141}
142
143static GSList *scan(struct sr_dev_driver *di, GSList *options)
144{
145 return sr_scpi_scan(di->context, options, probe_device);
146}
147
148static int dev_open(struct sr_dev_inst *sdi)
149{
150 return sr_scpi_open(sdi->conn);
151}
152
153static int dev_close(struct sr_dev_inst *sdi)
154{
155 return sr_scpi_close(sdi->conn);
156}
157
158static int config_get(uint32_t key, GVariant **data,
159 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
160{
161 struct dev_context *devc;
162 int ret;
163 GVariant *arr[2];
164 unsigned int i;
165 const char *range_str;
166
167 (void)cg;
168
169 devc = sdi->priv;
170
171 switch (key) {
172 case SR_CONF_LIMIT_SAMPLES:
173 case SR_CONF_LIMIT_MSEC:
174 return sr_sw_limits_config_get(&devc->limits, key, data);
175 case SR_CONF_MEASURED_QUANTITY:
176 ret = hp_3478a_get_status_bytes(sdi);
177 if (ret != SR_OK)
178 return ret;
179 arr[0] = g_variant_new_uint32(devc->measurement_mq);
180 arr[1] = g_variant_new_uint64(devc->measurement_mq_flags);
181 *data = g_variant_new_tuple(arr, 2);
182 break;
183 case SR_CONF_RANGE:
184 ret = hp_3478a_get_status_bytes(sdi);
185 if (ret != SR_OK)
186 return ret;
187 range_str = "Auto";
188 for (i = 0; i < ARRAY_SIZE(rangeopts); i++) {
189 if (rangeopts[i].mq == devc->measurement_mq &&
190 rangeopts[i].mqflag == devc->measurement_mq_flags &&
191 rangeopts[i].range_exp == devc->range_exp) {
192 range_str = rangeopts[i].range_str;
193 break;
194 }
195 }
196 *data = g_variant_new_string(range_str);
197 break;
198 default:
199 return SR_ERR_NA;
200 }
201
202 return SR_OK;
203}
204
205static int config_set(uint32_t key, GVariant *data,
206 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
207{
208 struct dev_context *devc;
209 enum sr_mq mq;
210 enum sr_mqflag mq_flags;
211 GVariant *tuple_child;
212 unsigned int i;
213 const char *range_str;
214
215 (void)cg;
216
217 devc = sdi->priv;
218
219 switch (key) {
220 case SR_CONF_LIMIT_SAMPLES:
221 case SR_CONF_LIMIT_MSEC:
222 return sr_sw_limits_config_set(&devc->limits, key, data);
223 case SR_CONF_MEASURED_QUANTITY:
224 tuple_child = g_variant_get_child_value(data, 0);
225 mq = g_variant_get_uint32(tuple_child);
226 tuple_child = g_variant_get_child_value(data, 1);
227 mq_flags = g_variant_get_uint64(tuple_child);
228 g_variant_unref(tuple_child);
229 return hp_3478a_set_mq(sdi, mq, mq_flags);
230 case SR_CONF_RANGE:
231 range_str = g_variant_get_string(data, NULL);
232 for (i = 0; i < ARRAY_SIZE(rangeopts); i++) {
233 if (rangeopts[i].mq == devc->measurement_mq &&
234 rangeopts[i].mqflag == devc->measurement_mq_flags &&
235 g_strcmp0(rangeopts[i].range_str, range_str) == 0) {
236 return hp_3478a_set_range(sdi, rangeopts[i].range_exp);
237 }
238 }
239 return SR_ERR_NA;
240 default:
241 return SR_ERR_NA;
242 }
243
244 return SR_OK;
245}
246
247static int config_list(uint32_t key, GVariant **data,
248 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
249{
250 struct dev_context *devc;
251 int ret;
252 unsigned int i;
253 GVariant *gvar, *arr[2];
254 GVariantBuilder gvb;
255
256 devc = sdi->priv;
257
258 switch (key) {
259 case SR_CONF_SCAN_OPTIONS:
260 case SR_CONF_DEVICE_OPTIONS:
261 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
262 case SR_CONF_MEASURED_QUANTITY:
263 /*
264 * TODO: move to std.c as
265 * SR_PRIV GVariant *std_gvar_measured_quantities()
266 */
267 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
268 for (i = 0; i < ARRAY_SIZE(mqopts); i++) {
269 arr[0] = g_variant_new_uint32(mqopts[i].mq);
270 arr[1] = g_variant_new_uint64(mqopts[i].mqflag);
271 gvar = g_variant_new_tuple(arr, 2);
272 g_variant_builder_add_value(&gvb, gvar);
273 }
274 *data = g_variant_builder_end(&gvb);
275 break;
276 case SR_CONF_RANGE:
277 ret = hp_3478a_get_status_bytes(sdi);
278 if (ret != SR_OK)
279 return ret;
280 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
281 for (i = 0; i < ARRAY_SIZE(rangeopts); i++) {
282 if (rangeopts[i].mq == devc->measurement_mq &&
283 rangeopts[i].mqflag == devc->measurement_mq_flags) {
284 g_variant_builder_add(&gvb, "s", rangeopts[i].range_str);
285 }
286 }
287 *data = g_variant_builder_end(&gvb);
288 break;
289 default:
290 return SR_ERR_NA;
291 }
292
293 return SR_OK;
294}
295
296static int dev_acquisition_start(const struct sr_dev_inst *sdi)
297{
298 struct sr_scpi_dev_inst *scpi;
299 struct dev_context *devc;
300
301 scpi = sdi->conn;
302 devc = sdi->priv;
303
304 sr_sw_limits_acquisition_start(&devc->limits);
305 std_session_send_df_header(sdi);
306
307 /*
308 * NOTE: For faster readings, there are some things one can do:
309 * - Turn off the display: sr_scpi_send(scpi, "D3SIGROK").
310 * - Set the line frequency to 60Hz via switch (back of the unit).
311 * - Set to 3.5 digits measurement (add config key SR_CONF_DIGITS).
312 */
313
314 /* Set to internal trigger. */
315 sr_scpi_send(scpi, "T1");
316 /* Get device status. */
317 hp_3478a_get_status_bytes(sdi);
318
319 return sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 100,
320 hp_3478a_receive_data, (void *)sdi);
321}
322
323static int dev_acquisition_stop(struct sr_dev_inst *sdi)
324{
325 struct sr_scpi_dev_inst *scpi;
326
327 scpi = sdi->conn;
328
329 sr_scpi_source_remove(sdi->session, scpi);
330 std_session_send_df_end(sdi);
331
332 /* Set to internal trigger. */
333 sr_scpi_send(scpi, "T1");
334 /* Turn on display. */
335 sr_scpi_send(scpi, "D1");
336
337 return SR_OK;
338}
339
340static struct sr_dev_driver hp_3478a_driver_info = {
341 .name = "hp-3478a",
342 .longname = "HP 3478A",
343 .api_version = 1,
344 .init = std_init,
345 .cleanup = std_cleanup,
346 .scan = scan,
347 .dev_list = std_dev_list,
348 .dev_clear = std_dev_clear,
349 .config_get = config_get,
350 .config_set = config_set,
351 .config_list = config_list,
352 .dev_open = dev_open,
353 .dev_close = dev_close,
354 .dev_acquisition_start = dev_acquisition_start,
355 .dev_acquisition_stop = dev_acquisition_stop,
356 .context = NULL,
357};
358SR_REGISTER_DEV_DRIVER(hp_3478a_driver_info);