cc5d4cb7a172ccc227a1a4082023d1d50027022a
[libsigrok.git] / src / hardware / hp-3478a / api.c
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
24 static const uint32_t scanopts[] = {
25         SR_CONF_CONN,
26 };
27
28 static const uint32_t drvopts[] = {
29         SR_CONF_MULTIMETER,
30 };
31
32 static 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         SR_CONF_DIGITS | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
39 };
40
41 static const struct {
42         enum sr_mq mq;
43         enum sr_mqflag mqflag;
44 } mqopts[] = {
45         {SR_MQ_VOLTAGE, SR_MQFLAG_DC},
46         {SR_MQ_VOLTAGE, SR_MQFLAG_AC},
47         {SR_MQ_CURRENT, SR_MQFLAG_DC},
48         {SR_MQ_CURRENT, SR_MQFLAG_AC},
49         {SR_MQ_RESISTANCE, 0},
50         {SR_MQ_RESISTANCE, SR_MQFLAG_FOUR_WIRE},
51 };
52
53 static 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
100 /** Available digits as strings. */
101 static const char *digits[] = {
102         "3.5", "4.5", "5.5",
103 };
104
105 /** Mapping between devc->spec_digits and digits string. */
106 static const char *digits_map[] = {
107         "", "", "", "", "3.5", "4.5", "5.5",
108 };
109
110 static struct sr_dev_driver hp_3478a_driver_info;
111
112 static int create_front_channel(struct sr_dev_inst *sdi, int chan_idx)
113 {
114         struct sr_channel *channel;
115         struct channel_context *chanc;
116
117         chanc = g_malloc(sizeof(*chanc));
118         chanc->location = TERMINAL_FRONT;
119
120         channel = sr_channel_new(sdi, chan_idx++, SR_CHANNEL_ANALOG, TRUE, "P1");
121         channel->priv = chanc;
122
123         return chan_idx;
124 }
125
126 static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
127 {
128         int ret;
129         struct sr_dev_inst *sdi;
130         struct dev_context *devc;
131
132         sdi = g_malloc0(sizeof(struct sr_dev_inst));
133         sdi->vendor = g_strdup("Hewlett-Packard");
134         sdi->model = g_strdup("3478A");
135         sdi->conn = scpi;
136         sdi->driver = &hp_3478a_driver_info;
137         sdi->inst_type = SR_INST_SCPI;
138
139         devc = g_malloc0(sizeof(struct dev_context));
140         sr_sw_limits_init(&devc->limits);
141         sdi->priv = devc;
142
143         /* Get actual status (function, digits, ...). */
144         ret = hp_3478a_get_status_bytes(sdi);
145         if (ret != SR_OK)
146                 return NULL;
147
148         create_front_channel(sdi, 0);
149
150         return sdi;
151 }
152
153 static GSList *scan(struct sr_dev_driver *di, GSList *options)
154 {
155         return sr_scpi_scan(di->context, options, probe_device);
156 }
157
158 static int dev_open(struct sr_dev_inst *sdi)
159 {
160         return sr_scpi_open(sdi->conn);
161 }
162
163 static int dev_close(struct sr_dev_inst *sdi)
164 {
165         return sr_scpi_close(sdi->conn);
166 }
167
168 static int config_get(uint32_t key, GVariant **data,
169         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
170 {
171         struct dev_context *devc;
172         int ret;
173         GVariant *arr[2];
174         unsigned int i;
175         const char *range_str;
176
177         (void)cg;
178
179         devc = sdi->priv;
180
181         switch (key) {
182         case SR_CONF_LIMIT_SAMPLES:
183         case SR_CONF_LIMIT_MSEC:
184                 return sr_sw_limits_config_get(&devc->limits, key, data);
185         case SR_CONF_MEASURED_QUANTITY:
186                 ret = hp_3478a_get_status_bytes(sdi);
187                 if (ret != SR_OK)
188                         return ret;
189                 arr[0] = g_variant_new_uint32(devc->measurement_mq);
190                 arr[1] = g_variant_new_uint64(devc->measurement_mq_flags);
191                 *data = g_variant_new_tuple(arr, 2);
192                 break;
193         case SR_CONF_RANGE:
194                 ret = hp_3478a_get_status_bytes(sdi);
195                 if (ret != SR_OK)
196                         return ret;
197                 range_str = "Auto";
198                 for (i = 0; i < ARRAY_SIZE(rangeopts); i++) {
199                         if (rangeopts[i].mq == devc->measurement_mq &&
200                                         rangeopts[i].mqflag == devc->measurement_mq_flags &&
201                                         rangeopts[i].range_exp == devc->range_exp) {
202                                 range_str = rangeopts[i].range_str;
203                                 break;
204                         }
205                 }
206                 *data = g_variant_new_string(range_str);
207                 break;
208         case SR_CONF_DIGITS:
209                 ret = hp_3478a_get_status_bytes(sdi);
210                 if (ret != SR_OK)
211                         return ret;
212                 *data = g_variant_new_string(digits_map[devc->spec_digits]);
213                 break;
214         default:
215                 return SR_ERR_NA;
216         }
217
218         return SR_OK;
219 }
220
221 static int config_set(uint32_t key, GVariant *data,
222         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
223 {
224         struct dev_context *devc;
225         enum sr_mq mq;
226         enum sr_mqflag mq_flags;
227         GVariant *tuple_child;
228         unsigned int i;
229         const char *range_str;
230         const char *digits_str;
231
232         (void)cg;
233
234         devc = sdi->priv;
235
236         switch (key) {
237         case SR_CONF_LIMIT_SAMPLES:
238         case SR_CONF_LIMIT_MSEC:
239                 return sr_sw_limits_config_set(&devc->limits, key, data);
240         case SR_CONF_MEASURED_QUANTITY:
241                 tuple_child = g_variant_get_child_value(data, 0);
242                 mq = g_variant_get_uint32(tuple_child);
243                 g_variant_unref(tuple_child);
244                 tuple_child = g_variant_get_child_value(data, 1);
245                 mq_flags = g_variant_get_uint64(tuple_child);
246                 g_variant_unref(tuple_child);
247                 return hp_3478a_set_mq(sdi, mq, mq_flags);
248         case SR_CONF_RANGE:
249                 range_str = g_variant_get_string(data, NULL);
250                 for (i = 0; i < ARRAY_SIZE(rangeopts); i++) {
251                         if (rangeopts[i].mq == devc->measurement_mq &&
252                                         rangeopts[i].mqflag == devc->measurement_mq_flags &&
253                                         g_strcmp0(rangeopts[i].range_str, range_str) == 0) {
254                                 return hp_3478a_set_range(sdi, rangeopts[i].range_exp);
255                         }
256                 }
257                 return SR_ERR_NA;
258         case SR_CONF_DIGITS:
259                 digits_str = g_variant_get_string(data, NULL);
260                 for (i = 0; i < ARRAY_SIZE(rangeopts); i++) {
261                         if (g_strcmp0(digits_map[i], digits_str) == 0)
262                                 return hp_3478a_set_digits(sdi, i);
263                 }
264                 return SR_ERR_NA;
265         default:
266                 return SR_ERR_NA;
267         }
268
269         return SR_OK;
270 }
271
272 static int config_list(uint32_t key, GVariant **data,
273         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
274 {
275         struct dev_context *devc;
276         int ret;
277         unsigned int i;
278         GVariant *gvar, *arr[2];
279         GVariantBuilder gvb;
280
281         devc = sdi->priv;
282
283         switch (key) {
284         case SR_CONF_SCAN_OPTIONS:
285         case SR_CONF_DEVICE_OPTIONS:
286                 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
287         case SR_CONF_MEASURED_QUANTITY:
288                 /*
289                  * TODO: move to std.c as
290                  *       SR_PRIV GVariant *std_gvar_measured_quantities()
291                  */
292                 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
293                 for (i = 0; i < ARRAY_SIZE(mqopts); i++) {
294                         arr[0] = g_variant_new_uint32(mqopts[i].mq);
295                         arr[1] = g_variant_new_uint64(mqopts[i].mqflag);
296                         gvar = g_variant_new_tuple(arr, 2);
297                         g_variant_builder_add_value(&gvb, gvar);
298                 }
299                 *data = g_variant_builder_end(&gvb);
300                 break;
301         case SR_CONF_RANGE:
302                 ret = hp_3478a_get_status_bytes(sdi);
303                 if (ret != SR_OK)
304                         return ret;
305                 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
306                 for (i = 0; i < ARRAY_SIZE(rangeopts); i++) {
307                         if (rangeopts[i].mq == devc->measurement_mq &&
308                                         rangeopts[i].mqflag == devc->measurement_mq_flags) {
309                                 g_variant_builder_add(&gvb, "s", rangeopts[i].range_str);
310                         }
311                 }
312                 *data = g_variant_builder_end(&gvb);
313                 break;
314         case SR_CONF_DIGITS:
315                 *data = g_variant_new_strv(ARRAY_AND_SIZE(digits));
316                 break;
317         default:
318                 return SR_ERR_NA;
319         }
320
321         return SR_OK;
322 }
323
324 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
325 {
326         struct sr_scpi_dev_inst *scpi;
327         struct dev_context *devc;
328
329         scpi = sdi->conn;
330         devc = sdi->priv;
331
332         sr_sw_limits_acquisition_start(&devc->limits);
333         std_session_send_df_header(sdi);
334
335         /*
336          * NOTE: For faster readings, there are some things one can do:
337          *     - Turn off the display: sr_scpi_send(scpi, "D3SIGROK").
338          *     - Set the line frequency to 60Hz via switch (back of the unit).
339          *     - Set to 3.5 digits measurement.
340          */
341
342         /* Set to internal trigger. */
343         sr_scpi_send(scpi, "T1");
344         /* Get device status. */
345         hp_3478a_get_status_bytes(sdi);
346
347         return sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 100,
348                         hp_3478a_receive_data, (void *)sdi);
349 }
350
351 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
352 {
353         struct sr_scpi_dev_inst *scpi;
354
355         scpi = sdi->conn;
356
357         sr_scpi_source_remove(sdi->session, scpi);
358         std_session_send_df_end(sdi);
359
360         /* Set to internal trigger. */
361         sr_scpi_send(scpi, "T1");
362         /* Turn on display. */
363         sr_scpi_send(scpi, "D1");
364
365         return SR_OK;
366 }
367
368 static struct sr_dev_driver hp_3478a_driver_info = {
369         .name = "hp-3478a",
370         .longname = "HP 3478A",
371         .api_version = 1,
372         .init = std_init,
373         .cleanup = std_cleanup,
374         .scan = scan,
375         .dev_list = std_dev_list,
376         .dev_clear = std_dev_clear,
377         .config_get = config_get,
378         .config_set = config_set,
379         .config_list = config_list,
380         .dev_open = dev_open,
381         .dev_close = dev_close,
382         .dev_acquisition_start = dev_acquisition_start,
383         .dev_acquisition_stop = dev_acquisition_stop,
384         .context = NULL,
385 };
386 SR_REGISTER_DEV_DRIVER(hp_3478a_driver_info);