]> sigrok.org Git - libsigrok.git/blame - src/hardware/agilent-dmm/api.c
agilent-dmm: Now really fix driver options.
[libsigrok.git] / src / hardware / agilent-dmm / api.c
CommitLineData
e93cdf42 1/*
50985c20 2 * This file is part of the libsigrok project.
e93cdf42
BV
3 *
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.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 <glib.h>
e93cdf42
BV
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <fcntl.h>
24#include <string.h>
25#include <errno.h>
6f22a8ef
UH
26#include "libsigrok.h"
27#include "libsigrok-internal.h"
28#include "agilent-dmm.h"
e93cdf42 29
a0e0bb41 30static const uint32_t scanopts[] = {
1953564a
BV
31 SR_CONF_CONN,
32 SR_CONF_SERIALCOMM,
e93cdf42
BV
33};
34
f9dada0b 35static const uint32_t drvopts[] = {
1953564a 36 SR_CONF_MULTIMETER,
f9dada0b
BV
37};
38
39static const uint32_t devopts[] = {
40 SR_CONF_CONTINUOUS | SR_CONF_SET,
5827f61b
BV
41 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
42 SR_CONF_LIMIT_MSEC | SR_CONF_SET,
e93cdf42
BV
43};
44
f857bd92 45extern const struct agdmm_job agdmm_jobs_u12xx[];
8c0152f2 46extern const struct agdmm_recv agdmm_recvs_u123x[];
4edba404 47extern const struct agdmm_recv agdmm_recvs_u124x[];
8c0152f2 48extern const struct agdmm_recv agdmm_recvs_u125x[];
e93cdf42 49
74ac7d7f
BV
50/* This works on all the Agilent U12xxA series, although the
51 * U127xA can apparently also run at 19200/8n1. */
52#define SERIALCOMM "9600/8n1"
53
e93cdf42 54static const struct agdmm_profile supported_agdmm[] = {
f857bd92
BV
55 { AGILENT_U1231, "U1231A", agdmm_jobs_u12xx, agdmm_recvs_u123x },
56 { AGILENT_U1232, "U1232A", agdmm_jobs_u12xx, agdmm_recvs_u123x },
57 { AGILENT_U1233, "U1233A", agdmm_jobs_u12xx, agdmm_recvs_u123x },
58
4edba404
BV
59 { AGILENT_U1241, "U1241A", agdmm_jobs_u12xx, agdmm_recvs_u124x },
60 { AGILENT_U1242, "U1242A", agdmm_jobs_u12xx, agdmm_recvs_u124x },
ee2bcdfc
BV
61 { AGILENT_U1241, "U1241B", agdmm_jobs_u12xx, agdmm_recvs_u124x },
62 { AGILENT_U1242, "U1242B", agdmm_jobs_u12xx, agdmm_recvs_u124x },
4edba404 63
f857bd92
BV
64 { AGILENT_U1251, "U1251A", agdmm_jobs_u12xx, agdmm_recvs_u125x },
65 { AGILENT_U1252, "U1252A", agdmm_jobs_u12xx, agdmm_recvs_u125x },
66 { AGILENT_U1253, "U1253A", agdmm_jobs_u12xx, agdmm_recvs_u125x },
67 { AGILENT_U1251, "U1251B", agdmm_jobs_u12xx, agdmm_recvs_u125x },
68 { AGILENT_U1252, "U1252B", agdmm_jobs_u12xx, agdmm_recvs_u125x },
69 { AGILENT_U1253, "U1253B", agdmm_jobs_u12xx, agdmm_recvs_u125x },
70 ALL_ZERO
e93cdf42
BV
71};
72
73SR_PRIV struct sr_dev_driver agdmm_driver_info;
74static struct sr_dev_driver *di = &agdmm_driver_info;
75
6078d2c9 76static int init(struct sr_context *sr_ctx)
e93cdf42 77{
f6beaac5 78 return std_init(sr_ctx, di, LOG_PREFIX);
e93cdf42
BV
79}
80
6078d2c9 81static GSList *scan(GSList *options)
e93cdf42
BV
82{
83 struct sr_dev_inst *sdi;
84 struct drv_context *drvc;
85 struct dev_context *devc;
1987b8d6 86 struct sr_config *src;
ba7dd8bb 87 struct sr_channel *ch;
109a3ba4 88 struct sr_serial_dev_inst *serial;
e93cdf42 89 GSList *l, *devices;
109a3ba4 90 int len, i;
e93cdf42
BV
91 const char *conn, *serialcomm;
92 char *buf, **tokens;
93
94 drvc = di->priv;
95 drvc->instances = NULL;
96
97 devices = NULL;
98 conn = serialcomm = NULL;
99 for (l = options; l; l = l->next) {
1987b8d6
BV
100 src = l->data;
101 switch (src->key) {
1953564a 102 case SR_CONF_CONN:
e44ac12a 103 conn = g_variant_get_string(src->data, NULL);
e93cdf42 104 break;
1953564a 105 case SR_CONF_SERIALCOMM:
e44ac12a 106 serialcomm = g_variant_get_string(src->data, NULL);
e93cdf42
BV
107 break;
108 }
109 }
74ac7d7f 110 if (!conn)
e93cdf42 111 return NULL;
109a3ba4
BV
112 if (!serialcomm)
113 serialcomm = SERIALCOMM;
e93cdf42 114
109a3ba4 115 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
e93cdf42 116 return NULL;
e93cdf42 117
64c536be 118 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
e93cdf42 119 return NULL;
e93cdf42 120
109a3ba4 121 serial_flush(serial);
95779b43 122 if (serial_write_blocking(serial, "*IDN?\r\n", 7, SERIAL_WRITE_TIMEOUT_MS) < 7) {
081c214e 123 sr_err("Unable to send identification string.");
e93cdf42
BV
124 return NULL;
125 }
126
127 len = 128;
886a52b6
UH
128 if (!(buf = g_try_malloc(len))) {
129 sr_err("Serial buffer malloc failed.");
130 return NULL;
131 }
ee2bcdfc 132 serial_readline(serial, &buf, &len, 250);
e93cdf42
BV
133 if (!len)
134 return NULL;
135
136 tokens = g_strsplit(buf, ",", 4);
137 if (!strcmp("Agilent Technologies", tokens[0])
fbf07e02 138 && tokens[1] && tokens[2] && tokens[3]) {
e93cdf42
BV
139 for (i = 0; supported_agdmm[i].model; i++) {
140 if (strcmp(supported_agdmm[i].modelname, tokens[1]))
141 continue;
0af636be
UH
142 sdi = sr_dev_inst_new();
143 sdi->status = SR_ST_INACTIVE;
144 sdi->vendor = g_strdup("Agilent");
145 sdi->model = g_strdup(tokens[1]);
146 sdi->version = g_strdup(tokens[3]);
e93cdf42 147 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
886a52b6 148 sr_err("Device context malloc failed.");
e93cdf42
BV
149 return NULL;
150 }
151 devc->profile = &supported_agdmm[i];
e93cdf42 152 devc->cur_mq = -1;
fb3a1505
BV
153 sdi->inst_type = SR_INST_SERIAL;
154 sdi->conn = serial;
e93cdf42
BV
155 sdi->priv = devc;
156 sdi->driver = di;
3f239f08 157 if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "P1")))
e93cdf42 158 return NULL;
ba7dd8bb 159 sdi->channels = g_slist_append(sdi->channels, ch);
e93cdf42
BV
160 drvc->instances = g_slist_append(drvc->instances, sdi);
161 devices = g_slist_append(devices, sdi);
162 break;
163 }
164 }
165 g_strfreev(tokens);
166 g_free(buf);
167
109a3ba4
BV
168 serial_close(serial);
169 if (!devices)
170 sr_serial_dev_inst_free(serial);
e93cdf42
BV
171
172 return devices;
173}
174
6078d2c9 175static GSList *dev_list(void)
e93cdf42 176{
0e94d524 177 return ((struct drv_context *)(di->priv))->instances;
e93cdf42
BV
178}
179
6078d2c9 180static int cleanup(void)
e93cdf42 181{
a6630742 182 return std_dev_clear(di, NULL);
e93cdf42
BV
183}
184
584560f1 185static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
53b4680f 186 const struct sr_channel_group *cg)
e93cdf42
BV
187{
188 struct dev_context *devc;
189
53b4680f 190 (void)cg;
8f996b89 191
e93cdf42 192 if (sdi->status != SR_ST_ACTIVE)
e73ffd42 193 return SR_ERR_DEV_CLOSED;
e93cdf42
BV
194
195 if (!(devc = sdi->priv)) {
38d326e8 196 sr_err("sdi->priv was NULL.");
e93cdf42
BV
197 return SR_ERR_BUG;
198 }
199
584560f1 200 switch (key) {
1953564a 201 case SR_CONF_LIMIT_MSEC:
e93cdf42 202 /* TODO: not yet implemented */
e44ac12a 203 if (g_variant_get_uint64(data) == 0) {
38d326e8 204 sr_err("LIMIT_MSEC can't be 0.");
e93cdf42
BV
205 return SR_ERR;
206 }
e44ac12a 207 devc->limit_msec = g_variant_get_uint64(data);
38d326e8 208 sr_dbg("Setting time limit to %" PRIu64 "ms.",
e93cdf42
BV
209 devc->limit_msec);
210 break;
1953564a 211 case SR_CONF_LIMIT_SAMPLES:
e44ac12a 212 devc->limit_samples = g_variant_get_uint64(data);
38d326e8 213 sr_dbg("Setting sample limit to %" PRIu64 ".",
e93cdf42
BV
214 devc->limit_samples);
215 break;
216 default:
bd6fbf62 217 return SR_ERR_NA;
e93cdf42
BV
218 }
219
220 return SR_OK;
221}
222
584560f1 223static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 224 const struct sr_channel_group *cg)
a1c743fc 225{
a1c743fc 226 (void)sdi;
53b4680f 227 (void)cg;
a1c743fc
BV
228
229 switch (key) {
0d485e30 230 case SR_CONF_SCAN_OPTIONS:
584560f1 231 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
a0e0bb41 232 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
0d485e30 233 break;
9a6517d1 234 case SR_CONF_DEVICE_OPTIONS:
ce4bd052
BV
235 if (!sdi)
236 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
f9dada0b 237 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
ce4bd052
BV
238 else
239 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
f9dada0b 240 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
9a6517d1 241 break;
a1c743fc 242 default:
bd6fbf62 243 return SR_ERR_NA;
a1c743fc
BV
244 }
245
246 return SR_OK;
247}
248
6078d2c9 249static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
e93cdf42 250{
e93cdf42 251 struct dev_context *devc;
fb3a1505 252 struct sr_serial_dev_inst *serial;
e93cdf42 253
e73ffd42
BV
254 if (sdi->status != SR_ST_ACTIVE)
255 return SR_ERR_DEV_CLOSED;
256
e93cdf42 257 if (!(devc = sdi->priv)) {
38d326e8 258 sr_err("sdi->priv was NULL.");
e93cdf42
BV
259 return SR_ERR_BUG;
260 }
261
e93cdf42
BV
262 devc->cb_data = cb_data;
263
264 /* Send header packet to the session bus. */
29a27196 265 std_session_send_df_header(cb_data, LOG_PREFIX);
e93cdf42 266
e93cdf42 267 /* Poll every 100ms, or whenever some data comes in. */
fb3a1505 268 serial = sdi->conn;
102f1239
BV
269 serial_source_add(sdi->session, serial, G_IO_IN, 100,
270 agdmm_receive_data, (void *)sdi);
e93cdf42
BV
271
272 return SR_OK;
273}
274
6078d2c9 275static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
e93cdf42 276{
d43b0908 277 return std_serial_dev_acquisition_stop(sdi, cb_data, std_serial_dev_close,
29a27196 278 sdi->conn, LOG_PREFIX);
e93cdf42
BV
279}
280
281SR_PRIV struct sr_dev_driver agdmm_driver_info = {
282 .name = "agilent-dmm",
283 .longname = "Agilent U12xx series DMMs",
284 .api_version = 1,
6078d2c9
UH
285 .init = init,
286 .cleanup = cleanup,
287 .scan = scan,
288 .dev_list = dev_list,
a6630742 289 .dev_clear = NULL,
6fab7b8f 290 .config_get = NULL,
035a1078 291 .config_set = config_set,
a1c743fc 292 .config_list = config_list,
854434de 293 .dev_open = std_serial_dev_open,
bf2c987f 294 .dev_close = std_serial_dev_close,
6078d2c9
UH
295 .dev_acquisition_start = dev_acquisition_start,
296 .dev_acquisition_stop = dev_acquisition_stop,
e93cdf42
BV
297 .priv = NULL,
298};