2 * This file is part of the sigrok project.
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
21 #include <sys/types.h>
26 #include "libsigrok.h"
27 #include "libsigrok-internal.h"
28 #include "agilent-dmm.h"
30 static const int hwopts[] = {
36 static const int hwcaps[] = {
38 SR_HWCAP_LIMIT_SAMPLES,
44 static const char *probe_names[] = {
49 extern const struct agdmm_job agdmm_jobs_u123x[];
50 extern const struct agdmm_recv agdmm_recvs_u123x[];
51 extern const struct agdmm_job agdmm_jobs_u125x[];
52 extern const struct agdmm_recv agdmm_recvs_u125x[];
54 static const struct agdmm_profile supported_agdmm[] = {
55 { AGILENT_U1231A, "U1231A", 9600, agdmm_jobs_u123x, agdmm_recvs_u123x },
56 { AGILENT_U1232A, "U1232A", 9600, agdmm_jobs_u123x, agdmm_recvs_u123x },
57 { AGILENT_U1233A, "U1233A", 9600, agdmm_jobs_u123x, agdmm_recvs_u123x },
58 { AGILENT_U1251A, "U1251A", 9600, agdmm_jobs_u125x, agdmm_recvs_u125x },
59 { AGILENT_U1252A, "U1252A", 9600, agdmm_jobs_u125x, agdmm_recvs_u125x },
60 { AGILENT_U1253A, "U1253A", 9600, agdmm_jobs_u125x, agdmm_recvs_u125x },
61 { 0, NULL, 0, NULL, NULL }
64 SR_PRIV struct sr_dev_driver agdmm_driver_info;
65 static struct sr_dev_driver *di = &agdmm_driver_info;
67 /* Properly close and free all devices. */
68 static int clear_instances(void)
70 struct sr_dev_inst *sdi;
71 struct drv_context *drvc;
72 struct dev_context *devc;
75 if (!(drvc = di->priv))
79 for (l = drvc->instances; l; l = l->next) {
82 if (!(devc = sdi->priv))
84 sr_serial_dev_inst_free(devc->serial);
85 sr_dev_inst_free(sdi);
87 g_slist_free(drvc->instances);
88 drvc->instances = NULL;
93 static int hw_init(void)
95 struct drv_context *drvc;
97 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
98 sr_err("Driver context malloc failed.");
107 /* TODO: Merge into serial_readline() from serial.c. */
108 static int serial_readline2(int fd, char **buf, int *buflen, uint64_t timeout_ms)
114 start = g_get_monotonic_time();
119 len = maxlen - *buflen - 1;
122 len = serial_read(fd, *buf + *buflen, len);
125 *(*buf + *buflen) = '\0';
126 if (*buflen > 0 && *(*buf + *buflen - 1) == '\n')
130 if (g_get_monotonic_time() - start > timeout_ms)
138 if (*(*buf + *buflen - 1) == '\r' || *(*buf + *buflen - 1) == '\n')
139 *(*buf + --*buflen) = '\0';
144 sr_dbg("Received '%s'.", *buf);
149 static GSList *hw_scan(GSList *options)
151 struct sr_dev_inst *sdi;
152 struct drv_context *drvc;
153 struct dev_context *devc;
154 struct sr_hwopt *opt;
155 struct sr_probe *probe;
158 const char *conn, *serialcomm;
162 drvc->instances = NULL;
165 conn = serialcomm = NULL;
166 for (l = options; l; l = l->next) {
168 switch (opt->hwopt) {
172 case SR_HWOPT_SERIALCOMM:
173 serialcomm = opt->value;
177 if (!conn || !serialcomm)
180 if ((fd = serial_open(conn, O_RDWR|O_NONBLOCK)) == -1) {
181 sr_err("Unable to open %s: %s.", conn, strerror(errno));
185 if (serial_set_paramstr(fd, serialcomm) != SR_OK) {
186 sr_err("Unable to set serial parameters: %s.",
192 if (serial_write(fd, "*IDN?\r\n", 7) == -1) {
193 sr_err("Unable to send identification string: %s.",
199 if (!(buf = g_try_malloc(len))) {
200 sr_err("Serial buffer malloc failed.");
203 serial_readline2(fd, &buf, &len, 150);
207 tokens = g_strsplit(buf, ",", 4);
208 if (!strcmp("Agilent Technologies", tokens[0])
209 && tokens[2] && tokens[3]) {
210 for (i = 0; supported_agdmm[i].model; i++) {
211 if (strcmp(supported_agdmm[i].modelname, tokens[1]))
213 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, tokens[0],
214 tokens[1], tokens[3])))
216 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
217 sr_err("Device context malloc failed.");
220 devc->profile = &supported_agdmm[i];
221 devc->serial = sr_serial_dev_inst_new(conn, -1);
225 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
227 sdi->probes = g_slist_append(sdi->probes, probe);
228 drvc->instances = g_slist_append(drvc->instances, sdi);
229 devices = g_slist_append(devices, sdi);
241 static GSList *hw_dev_list(void)
243 struct drv_context *drvc;
247 return drvc->instances;
250 static int hw_dev_open(struct sr_dev_inst *sdi)
252 struct dev_context *devc;
254 if (!(devc = sdi->priv)) {
255 sr_err("sdi->priv was NULL.");
259 devc->serial->fd = serial_open(devc->serial->port, O_RDWR | O_NONBLOCK);
260 if (devc->serial->fd == -1) {
261 sr_err("Couldn't open serial port '%s'.",
265 serial_set_params(devc->serial->fd, devc->profile->serial_speed, 8,
266 SERIAL_PARITY_NONE, 1, 0);
267 sdi->status = SR_ST_ACTIVE;
272 static int hw_dev_close(struct sr_dev_inst *sdi)
274 struct dev_context *devc;
276 if (!(devc = sdi->priv)) {
277 sr_err("sdi->priv was NULL.");
281 if (devc->serial && devc->serial->fd != -1) {
282 serial_close(devc->serial->fd);
283 devc->serial->fd = -1;
284 sdi->status = SR_ST_INACTIVE;
290 static int hw_cleanup(void)
298 static int hw_info_get(int info_id, const void **data,
299 const struct sr_dev_inst *sdi)
311 case SR_DI_NUM_PROBES:
312 *data = GINT_TO_POINTER(1);
314 case SR_DI_PROBE_NAMES:
324 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
327 struct dev_context *devc;
329 if (sdi->status != SR_ST_ACTIVE)
332 if (!(devc = sdi->priv)) {
333 sr_err("sdi->priv was NULL.");
338 case SR_HWCAP_LIMIT_MSEC:
339 /* TODO: not yet implemented */
340 if (*(const uint64_t *)value == 0) {
341 sr_err("LIMIT_MSEC can't be 0.");
344 devc->limit_msec = *(const uint64_t *)value;
345 sr_dbg("Setting time limit to %" PRIu64 "ms.",
348 case SR_HWCAP_LIMIT_SAMPLES:
349 devc->limit_samples = *(const uint64_t *)value;
350 sr_dbg("Setting sample limit to %" PRIu64 ".",
351 devc->limit_samples);
354 sr_err("Unknown capability: %d.", hwcap);
362 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
365 struct sr_datafeed_packet packet;
366 struct sr_datafeed_header header;
367 struct sr_datafeed_meta_analog meta;
368 struct dev_context *devc;
370 if (!(devc = sdi->priv)) {
371 sr_err("sdi->priv was NULL.");
375 sr_dbg("Starting acquisition.");
377 devc->cb_data = cb_data;
379 /* Send header packet to the session bus. */
380 sr_dbg("Sending SR_DF_HEADER.");
381 packet.type = SR_DF_HEADER;
382 packet.payload = (uint8_t *)&header;
383 header.feed_version = 1;
384 gettimeofday(&header.starttime, NULL);
385 sr_session_send(devc->cb_data, &packet);
387 /* Send metadata about the SR_DF_ANALOG packets to come. */
388 sr_dbg("Sending SR_DF_META_ANALOG.");
389 packet.type = SR_DF_META_ANALOG;
390 packet.payload = &meta;
392 sr_session_send(devc->cb_data, &packet);
394 /* Poll every 100ms, or whenever some data comes in. */
395 sr_source_add(devc->serial->fd, G_IO_IN, 100, agdmm_receive_data, (void *)sdi);
400 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
403 struct sr_datafeed_packet packet;
404 struct dev_context *devc;
406 if (sdi->status != SR_ST_ACTIVE)
409 if (!(devc = sdi->priv)) {
410 sr_err("sdi->priv was NULL.");
414 sr_dbg("Stopping acquisition.");
416 sr_source_remove(devc->serial->fd);
417 hw_dev_close((struct sr_dev_inst *)sdi);
419 /* Send end packet to the session bus. */
420 sr_dbg("Sending SR_DF_END.");
421 packet.type = SR_DF_END;
422 sr_session_send(cb_data, &packet);
428 SR_PRIV struct sr_dev_driver agdmm_driver_info = {
429 .name = "agilent-dmm",
430 .longname = "Agilent U12xx series DMMs",
433 .cleanup = hw_cleanup,
435 .dev_list = hw_dev_list,
436 .dev_clear = clear_instances,
437 .dev_open = hw_dev_open,
438 .dev_close = hw_dev_close,
439 .info_get = hw_info_get,
440 .dev_config_set = hw_dev_config_set,
441 .dev_acquisition_start = hw_dev_acquisition_start,
442 .dev_acquisition_stop = hw_dev_acquisition_stop,