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