]>
Commit | Line | Data |
---|---|---|
e93cdf42 BV |
1 | /* |
2 | * This file is part of the sigrok project. | |
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 BV |
29 | |
30 | static const int hwopts[] = { | |
31 | SR_HWOPT_CONN, | |
32 | SR_HWOPT_SERIALCOMM, | |
33 | 0, | |
34 | }; | |
35 | ||
36 | static const int hwcaps[] = { | |
37 | SR_HWCAP_MULTIMETER, | |
38 | SR_HWCAP_LIMIT_SAMPLES, | |
39 | SR_HWCAP_LIMIT_MSEC, | |
40 | SR_HWCAP_CONTINUOUS, | |
41 | 0, | |
42 | }; | |
43 | ||
8c0152f2 BV |
44 | extern const struct agdmm_job agdmm_jobs_u123x[]; |
45 | extern const struct agdmm_recv agdmm_recvs_u123x[]; | |
46 | extern const struct agdmm_job agdmm_jobs_u125x[]; | |
47 | extern const struct agdmm_recv agdmm_recvs_u125x[]; | |
e93cdf42 | 48 | |
74ac7d7f BV |
49 | /* This works on all the Agilent U12xxA series, although the |
50 | * U127xA can apparently also run at 19200/8n1. */ | |
51 | #define SERIALCOMM "9600/8n1" | |
52 | ||
e93cdf42 | 53 | static const struct agdmm_profile supported_agdmm[] = { |
74ac7d7f BV |
54 | { AGILENT_U1231A, "U1231A", agdmm_jobs_u123x, agdmm_recvs_u123x }, |
55 | { AGILENT_U1232A, "U1232A", agdmm_jobs_u123x, agdmm_recvs_u123x }, | |
56 | { AGILENT_U1233A, "U1233A", agdmm_jobs_u123x, agdmm_recvs_u123x }, | |
57 | { AGILENT_U1251A, "U1251A", agdmm_jobs_u125x, agdmm_recvs_u125x }, | |
58 | { AGILENT_U1252A, "U1252A", agdmm_jobs_u125x, agdmm_recvs_u125x }, | |
59 | { AGILENT_U1253A, "U1253A", agdmm_jobs_u125x, agdmm_recvs_u125x }, | |
60 | { 0, NULL, NULL, NULL } | |
e93cdf42 BV |
61 | }; |
62 | ||
63 | SR_PRIV struct sr_dev_driver agdmm_driver_info; | |
64 | static struct sr_dev_driver *di = &agdmm_driver_info; | |
65 | ||
66 | /* Properly close and free all devices. */ | |
67 | static int clear_instances(void) | |
68 | { | |
69 | struct sr_dev_inst *sdi; | |
70 | struct drv_context *drvc; | |
71 | struct dev_context *devc; | |
72 | GSList *l; | |
73 | ||
74 | if (!(drvc = di->priv)) | |
75 | return SR_OK; | |
76 | ||
77 | drvc = di->priv; | |
78 | for (l = drvc->instances; l; l = l->next) { | |
79 | if (!(sdi = l->data)) | |
80 | continue; | |
81 | if (!(devc = sdi->priv)) | |
82 | continue; | |
83 | sr_serial_dev_inst_free(devc->serial); | |
84 | sr_dev_inst_free(sdi); | |
85 | } | |
86 | g_slist_free(drvc->instances); | |
87 | drvc->instances = NULL; | |
88 | ||
89 | return SR_OK; | |
90 | } | |
91 | ||
34f06b90 | 92 | static int hw_init(struct sr_context *sr_ctx) |
e93cdf42 BV |
93 | { |
94 | struct drv_context *drvc; | |
95 | ||
96 | if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) { | |
38d326e8 | 97 | sr_err("Driver context malloc failed."); |
886a52b6 | 98 | return SR_ERR_MALLOC; |
e93cdf42 BV |
99 | } |
100 | ||
1ebe4b4e | 101 | drvc->sr_ctx = sr_ctx; |
e93cdf42 BV |
102 | di->priv = drvc; |
103 | ||
104 | return SR_OK; | |
105 | } | |
106 | ||
e93cdf42 BV |
107 | static GSList *hw_scan(GSList *options) |
108 | { | |
109 | struct sr_dev_inst *sdi; | |
110 | struct drv_context *drvc; | |
111 | struct dev_context *devc; | |
112 | struct sr_hwopt *opt; | |
113 | struct sr_probe *probe; | |
109a3ba4 | 114 | struct sr_serial_dev_inst *serial; |
e93cdf42 | 115 | GSList *l, *devices; |
109a3ba4 | 116 | int len, i; |
e93cdf42 BV |
117 | const char *conn, *serialcomm; |
118 | char *buf, **tokens; | |
119 | ||
120 | drvc = di->priv; | |
121 | drvc->instances = NULL; | |
122 | ||
123 | devices = NULL; | |
124 | conn = serialcomm = NULL; | |
125 | for (l = options; l; l = l->next) { | |
126 | opt = l->data; | |
127 | switch (opt->hwopt) { | |
128 | case SR_HWOPT_CONN: | |
129 | conn = opt->value; | |
130 | break; | |
131 | case SR_HWOPT_SERIALCOMM: | |
132 | serialcomm = opt->value; | |
133 | break; | |
134 | } | |
135 | } | |
74ac7d7f | 136 | if (!conn) |
e93cdf42 | 137 | return NULL; |
109a3ba4 BV |
138 | if (!serialcomm) |
139 | serialcomm = SERIALCOMM; | |
e93cdf42 | 140 | |
109a3ba4 | 141 | if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) |
e93cdf42 | 142 | return NULL; |
e93cdf42 | 143 | |
a54dd31e | 144 | if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) |
e93cdf42 | 145 | return NULL; |
e93cdf42 | 146 | |
109a3ba4 BV |
147 | serial_flush(serial); |
148 | if (serial_write(serial, "*IDN?\r\n", 7) == -1) { | |
38d326e8 UH |
149 | sr_err("Unable to send identification string: %s.", |
150 | strerror(errno)); | |
e93cdf42 BV |
151 | return NULL; |
152 | } | |
153 | ||
154 | len = 128; | |
886a52b6 UH |
155 | if (!(buf = g_try_malloc(len))) { |
156 | sr_err("Serial buffer malloc failed."); | |
157 | return NULL; | |
158 | } | |
109a3ba4 | 159 | serial_readline(serial, &buf, &len, 150); |
e93cdf42 BV |
160 | if (!len) |
161 | return NULL; | |
162 | ||
163 | tokens = g_strsplit(buf, ",", 4); | |
164 | if (!strcmp("Agilent Technologies", tokens[0]) | |
165 | && tokens[2] && tokens[3]) { | |
166 | for (i = 0; supported_agdmm[i].model; i++) { | |
167 | if (strcmp(supported_agdmm[i].modelname, tokens[1])) | |
168 | continue; | |
169 | if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, tokens[0], | |
170 | tokens[1], tokens[3]))) | |
171 | return NULL; | |
172 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { | |
886a52b6 | 173 | sr_err("Device context malloc failed."); |
e93cdf42 BV |
174 | return NULL; |
175 | } | |
176 | devc->profile = &supported_agdmm[i]; | |
109a3ba4 | 177 | devc->serial = serial; |
e93cdf42 BV |
178 | devc->cur_mq = -1; |
179 | sdi->priv = devc; | |
180 | sdi->driver = di; | |
181 | if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1"))) | |
182 | return NULL; | |
183 | sdi->probes = g_slist_append(sdi->probes, probe); | |
184 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
185 | devices = g_slist_append(devices, sdi); | |
186 | break; | |
187 | } | |
188 | } | |
189 | g_strfreev(tokens); | |
190 | g_free(buf); | |
191 | ||
109a3ba4 BV |
192 | serial_close(serial); |
193 | if (!devices) | |
194 | sr_serial_dev_inst_free(serial); | |
e93cdf42 BV |
195 | |
196 | return devices; | |
197 | } | |
198 | ||
199 | static GSList *hw_dev_list(void) | |
200 | { | |
201 | struct drv_context *drvc; | |
202 | ||
203 | drvc = di->priv; | |
204 | ||
205 | return drvc->instances; | |
206 | } | |
207 | ||
208 | static int hw_dev_open(struct sr_dev_inst *sdi) | |
209 | { | |
210 | struct dev_context *devc; | |
211 | ||
212 | if (!(devc = sdi->priv)) { | |
38d326e8 | 213 | sr_err("sdi->priv was NULL."); |
e93cdf42 BV |
214 | return SR_ERR_BUG; |
215 | } | |
216 | ||
a54dd31e | 217 | if (serial_open(devc->serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) |
e93cdf42 | 218 | return SR_ERR; |
109a3ba4 | 219 | |
e93cdf42 BV |
220 | sdi->status = SR_ST_ACTIVE; |
221 | ||
222 | return SR_OK; | |
223 | } | |
224 | ||
225 | static int hw_dev_close(struct sr_dev_inst *sdi) | |
226 | { | |
227 | struct dev_context *devc; | |
228 | ||
229 | if (!(devc = sdi->priv)) { | |
38d326e8 | 230 | sr_err("sdi->priv was NULL."); |
e93cdf42 BV |
231 | return SR_ERR_BUG; |
232 | } | |
233 | ||
234 | if (devc->serial && devc->serial->fd != -1) { | |
109a3ba4 | 235 | serial_close(devc->serial); |
e93cdf42 BV |
236 | sdi->status = SR_ST_INACTIVE; |
237 | } | |
238 | ||
239 | return SR_OK; | |
240 | } | |
241 | ||
242 | static int hw_cleanup(void) | |
243 | { | |
244 | ||
245 | clear_instances(); | |
246 | ||
247 | return SR_OK; | |
248 | } | |
249 | ||
250 | static int hw_info_get(int info_id, const void **data, | |
251 | const struct sr_dev_inst *sdi) | |
252 | { | |
e93cdf42 BV |
253 | (void)sdi; |
254 | ||
255 | switch (info_id) { | |
256 | case SR_DI_HWOPTS: | |
257 | *data = hwopts; | |
258 | break; | |
259 | case SR_DI_HWCAPS: | |
260 | *data = hwcaps; | |
261 | break; | |
e93cdf42 BV |
262 | default: |
263 | return SR_ERR_ARG; | |
264 | } | |
265 | ||
266 | return SR_OK; | |
267 | } | |
268 | ||
269 | static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap, | |
270 | const void *value) | |
271 | { | |
272 | struct dev_context *devc; | |
273 | ||
274 | if (sdi->status != SR_ST_ACTIVE) | |
275 | return SR_ERR; | |
276 | ||
277 | if (!(devc = sdi->priv)) { | |
38d326e8 | 278 | sr_err("sdi->priv was NULL."); |
e93cdf42 BV |
279 | return SR_ERR_BUG; |
280 | } | |
281 | ||
282 | switch (hwcap) { | |
283 | case SR_HWCAP_LIMIT_MSEC: | |
284 | /* TODO: not yet implemented */ | |
285 | if (*(const uint64_t *)value == 0) { | |
38d326e8 | 286 | sr_err("LIMIT_MSEC can't be 0."); |
e93cdf42 BV |
287 | return SR_ERR; |
288 | } | |
289 | devc->limit_msec = *(const uint64_t *)value; | |
38d326e8 | 290 | sr_dbg("Setting time limit to %" PRIu64 "ms.", |
e93cdf42 BV |
291 | devc->limit_msec); |
292 | break; | |
293 | case SR_HWCAP_LIMIT_SAMPLES: | |
294 | devc->limit_samples = *(const uint64_t *)value; | |
38d326e8 | 295 | sr_dbg("Setting sample limit to %" PRIu64 ".", |
e93cdf42 BV |
296 | devc->limit_samples); |
297 | break; | |
298 | default: | |
38d326e8 | 299 | sr_err("Unknown capability: %d.", hwcap); |
e93cdf42 BV |
300 | return SR_ERR; |
301 | break; | |
302 | } | |
303 | ||
304 | return SR_OK; | |
305 | } | |
306 | ||
307 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, | |
308 | void *cb_data) | |
309 | { | |
310 | struct sr_datafeed_packet packet; | |
311 | struct sr_datafeed_header header; | |
312 | struct sr_datafeed_meta_analog meta; | |
313 | struct dev_context *devc; | |
314 | ||
315 | if (!(devc = sdi->priv)) { | |
38d326e8 | 316 | sr_err("sdi->priv was NULL."); |
e93cdf42 BV |
317 | return SR_ERR_BUG; |
318 | } | |
319 | ||
38d326e8 | 320 | sr_dbg("Starting acquisition."); |
e93cdf42 BV |
321 | |
322 | devc->cb_data = cb_data; | |
323 | ||
324 | /* Send header packet to the session bus. */ | |
38d326e8 | 325 | sr_dbg("Sending SR_DF_HEADER."); |
e93cdf42 BV |
326 | packet.type = SR_DF_HEADER; |
327 | packet.payload = (uint8_t *)&header; | |
328 | header.feed_version = 1; | |
329 | gettimeofday(&header.starttime, NULL); | |
330 | sr_session_send(devc->cb_data, &packet); | |
331 | ||
332 | /* Send metadata about the SR_DF_ANALOG packets to come. */ | |
38d326e8 | 333 | sr_dbg("Sending SR_DF_META_ANALOG."); |
e93cdf42 BV |
334 | packet.type = SR_DF_META_ANALOG; |
335 | packet.payload = &meta; | |
336 | meta.num_probes = 1; | |
337 | sr_session_send(devc->cb_data, &packet); | |
338 | ||
339 | /* Poll every 100ms, or whenever some data comes in. */ | |
340 | sr_source_add(devc->serial->fd, G_IO_IN, 100, agdmm_receive_data, (void *)sdi); | |
341 | ||
342 | return SR_OK; | |
343 | } | |
344 | ||
69b07d14 | 345 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
e93cdf42 BV |
346 | { |
347 | struct sr_datafeed_packet packet; | |
348 | struct dev_context *devc; | |
349 | ||
350 | if (sdi->status != SR_ST_ACTIVE) | |
351 | return SR_ERR; | |
352 | ||
353 | if (!(devc = sdi->priv)) { | |
38d326e8 | 354 | sr_err("sdi->priv was NULL."); |
e93cdf42 BV |
355 | return SR_ERR_BUG; |
356 | } | |
357 | ||
38d326e8 | 358 | sr_dbg("Stopping acquisition."); |
e93cdf42 BV |
359 | |
360 | sr_source_remove(devc->serial->fd); | |
361 | hw_dev_close((struct sr_dev_inst *)sdi); | |
362 | ||
363 | /* Send end packet to the session bus. */ | |
38d326e8 | 364 | sr_dbg("Sending SR_DF_END."); |
e93cdf42 BV |
365 | packet.type = SR_DF_END; |
366 | sr_session_send(cb_data, &packet); | |
367 | ||
368 | ||
369 | return SR_OK; | |
370 | } | |
371 | ||
372 | SR_PRIV struct sr_dev_driver agdmm_driver_info = { | |
373 | .name = "agilent-dmm", | |
374 | .longname = "Agilent U12xx series DMMs", | |
375 | .api_version = 1, | |
376 | .init = hw_init, | |
377 | .cleanup = hw_cleanup, | |
378 | .scan = hw_scan, | |
379 | .dev_list = hw_dev_list, | |
380 | .dev_clear = clear_instances, | |
381 | .dev_open = hw_dev_open, | |
382 | .dev_close = hw_dev_close, | |
383 | .info_get = hw_info_get, | |
384 | .dev_config_set = hw_dev_config_set, | |
385 | .dev_acquisition_start = hw_dev_acquisition_start, | |
386 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
387 | .priv = NULL, | |
388 | }; |