2 * This file is part of the sigrok project.
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
5 * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
6 * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <sys/types.h>
28 #include "libsigrok.h"
29 #include "libsigrok-internal.h"
32 static const int hwopts[] = {
38 static const int hwcaps[] = {
40 SR_HWCAP_LIMIT_SAMPLES,
45 static const char *probe_names[] = {
50 SR_PRIV struct sr_dev_driver digitek_dt4000zc_driver_info;
51 SR_PRIV struct sr_dev_driver tekpower_tp4000zc_driver_info;
52 SR_PRIV struct sr_dev_driver metex_me31_driver_info;
53 SR_PRIV struct sr_dev_driver peaktech_3410_driver_info;
55 static struct sr_dev_driver *di_dt4000zc = &digitek_dt4000zc_driver_info;
56 static struct sr_dev_driver *di_tp4000zc = &tekpower_tp4000zc_driver_info;
57 static struct sr_dev_driver *di_me31 = &metex_me31_driver_info;
58 static struct sr_dev_driver *di_3410 = &peaktech_3410_driver_info;
60 /* After hw_init() this will point to a device-specific entry (see above). */
61 static struct sr_dev_driver *di = NULL;
63 SR_PRIV struct dmm_info dmms[] = {
65 "Digitek", "DT4000ZC", "2400/8n1", 2400,
66 FS9721_PACKET_SIZE, NULL,
67 sr_fs9721_packet_valid, sr_fs9721_parse,
71 "TekPower", "TP4000ZC", "2400/8n1", 2400,
72 FS9721_PACKET_SIZE, NULL,
73 sr_fs9721_packet_valid, sr_fs9721_parse,
77 "Metex", "ME-31", "600/7n2/rts=0/dtr=1", 600,
78 METEX14_PACKET_SIZE, sr_metex14_packet_request,
79 sr_metex14_packet_valid, sr_metex14_parse,
83 "Peaktech", "3410", "600/7n2/rts=0/dtr=1", 600,
84 METEX14_PACKET_SIZE, sr_metex14_packet_request,
85 sr_metex14_packet_valid, sr_metex14_parse,
90 /* Properly close and free all devices. */
91 static int clear_instances(void)
93 struct sr_dev_inst *sdi;
94 struct drv_context *drvc;
95 struct dev_context *devc;
98 if (!(drvc = di->priv))
102 for (l = drvc->instances; l; l = l->next) {
103 if (!(sdi = l->data))
105 if (!(devc = sdi->priv))
107 sr_serial_dev_inst_free(devc->serial);
108 sr_dev_inst_free(sdi);
110 g_slist_free(drvc->instances);
111 drvc->instances = NULL;
116 static int hw_init(int dmm)
118 struct drv_context *drvc;
120 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
121 sr_err("Driver context malloc failed.");
122 return SR_ERR_MALLOC;
125 if (dmm == DIGITEK_DT4000ZC)
127 if (dmm == TEKPOWER_TP4000ZC)
129 if (dmm == METEX_ME31)
131 if (dmm == PEAKTECH_3410)
133 sr_dbg("Selected '%s' subdriver.", di->name);
140 static int hw_init_digitek_dt4000zc(void)
142 return hw_init(DIGITEK_DT4000ZC);
145 static int hw_init_tekpower_tp4000zc(void)
147 return hw_init(TEKPOWER_TP4000ZC);
150 static int hw_init_metex_me31(void)
152 return hw_init(METEX_ME31);
155 static int hw_init_peaktech_3410(void)
157 return hw_init(PEAKTECH_3410);
160 static GSList *scan(const char *conn, const char *serialcomm, int dmm)
162 struct sr_dev_inst *sdi;
163 struct drv_context *drvc;
164 struct dev_context *devc;
165 struct sr_probe *probe;
166 struct sr_serial_dev_inst *serial;
172 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
175 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
178 sr_info("Probing port %s.", conn);
182 serial_flush(serial);
185 * There's no way to get an ID from the multimeter. It just sends data
186 * periodically, so the best we can do is check if the packets match
187 * the expected format.
190 /* Let's get a bit of data and see if we can find a packet. */
193 /* Request a packet if the DMM requires this. */
194 if (dmms[dmm].packet_request) {
195 if ((ret = dmms[dmm].packet_request(serial)) < 0) {
196 sr_err("Failed to request packet: %d.", ret);
201 ret = serial_stream_detect(serial, buf, &len, dmms[dmm].packet_size,
202 dmms[dmm].packet_valid, 1000,
208 * If we dropped more than two packets worth of data, something is
209 * wrong. We shouldn't quit however, since the dropped bytes might be
210 * just zeroes at the beginning of the stream. Those can occur as a
211 * combination of the nonstandard cable that ships with this device and
212 * the serial port or USB to serial adapter.
214 dropped = len - dmms[dmm].packet_size;
215 if (dropped > 2 * dmms[dmm].packet_size)
216 sr_warn("Had to drop too much data.");
218 sr_info("Found device on port %s.", conn);
220 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, dmms[dmm].vendor,
221 dmms[dmm].device, "")))
224 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
225 sr_err("Device context malloc failed.");
229 devc->serial = serial;
233 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
235 sdi->probes = g_slist_append(sdi->probes, probe);
236 drvc->instances = g_slist_append(drvc->instances, sdi);
237 devices = g_slist_append(devices, sdi);
240 serial_close(serial);
245 static GSList *hw_scan(GSList *options)
247 struct sr_hwopt *opt;
249 const char *conn, *serialcomm;
252 conn = serialcomm = NULL;
253 for (l = options; l; l = l->next) {
255 switch (opt->hwopt) {
259 case SR_HWOPT_SERIALCOMM:
260 serialcomm = opt->value;
267 if (!strcmp(di->name, "digitek-dt4000zc"))
269 if (!strcmp(di->name, "tekpower-tp4000zc"))
271 if (!strcmp(di->name, "metex-me31"))
273 if (!strcmp(di->name, "peaktech-3410"))
277 /* Use the provided comm specs. */
278 devices = scan(conn, serialcomm, dmm);
280 /* Try the default. */
281 devices = scan(conn, dmms[dmm].conn, dmm);
287 static GSList *hw_dev_list(void)
289 struct drv_context *drvc;
293 return drvc->instances;
296 static int hw_dev_open(struct sr_dev_inst *sdi)
298 struct dev_context *devc;
300 if (!(devc = sdi->priv)) {
301 sr_err("sdi->priv was NULL.");
305 if (serial_open(devc->serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
308 sdi->status = SR_ST_ACTIVE;
313 static int hw_dev_close(struct sr_dev_inst *sdi)
315 struct dev_context *devc;
317 if (!(devc = sdi->priv)) {
318 sr_err("sdi->priv was NULL.");
322 if (devc->serial && devc->serial->fd != -1) {
323 serial_close(devc->serial);
324 sdi->status = SR_ST_INACTIVE;
330 static int hw_cleanup(void)
337 static int hw_info_get(int info_id, const void **data,
338 const struct sr_dev_inst *sdi)
349 case SR_DI_NUM_PROBES:
350 *data = GINT_TO_POINTER(1);
352 case SR_DI_PROBE_NAMES:
362 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
365 struct dev_context *devc;
367 if (sdi->status != SR_ST_ACTIVE)
370 if (!(devc = sdi->priv)) {
371 sr_err("sdi->priv was NULL.");
376 case SR_HWCAP_LIMIT_SAMPLES:
377 devc->limit_samples = *(const uint64_t *)value;
378 sr_dbg("Setting sample limit to %" PRIu64 ".",
379 devc->limit_samples);
382 sr_err("Unknown capability: %d.", hwcap);
390 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
393 struct sr_datafeed_packet packet;
394 struct sr_datafeed_header header;
395 struct sr_datafeed_meta_analog meta;
396 struct dev_context *devc;
397 int (*receive_data)(int, int, void *) = NULL;
399 if (!(devc = sdi->priv)) {
400 sr_err("sdi->priv was NULL.");
404 sr_dbg("Starting acquisition.");
406 devc->cb_data = cb_data;
409 * Reset the number of samples to take. If we've already collected our
410 * quota, but we start a new session, and don't reset this, we'll just
411 * quit without acquiring any new samples.
413 devc->num_samples = 0;
415 /* Send header packet to the session bus. */
416 sr_dbg("Sending SR_DF_HEADER.");
417 packet.type = SR_DF_HEADER;
418 packet.payload = (uint8_t *)&header;
419 header.feed_version = 1;
420 gettimeofday(&header.starttime, NULL);
421 sr_session_send(devc->cb_data, &packet);
423 /* Send metadata about the SR_DF_ANALOG packets to come. */
424 sr_dbg("Sending SR_DF_META_ANALOG.");
425 packet.type = SR_DF_META_ANALOG;
426 packet.payload = &meta;
428 sr_session_send(devc->cb_data, &packet);
430 if (!strcmp(di->name, "digitek-dt4000zc"))
431 receive_data = digitek_dt4000zc_receive_data;
432 if (!strcmp(di->name, "tekpower-tp4000zc"))
433 receive_data = tekpower_tp4000zc_receive_data;
434 if (!strcmp(di->name, "metex-me31"))
435 receive_data = metex_me31_receive_data;
436 if (!strcmp(di->name, "peaktech-3410"))
437 receive_data = peaktech_3410_receive_data;
439 /* Poll every 50ms, or whenever some data comes in. */
440 sr_source_add(devc->serial->fd, G_IO_IN, 50,
441 receive_data, (void *)sdi);
446 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
448 struct sr_datafeed_packet packet;
449 struct dev_context *devc;
451 if (sdi->status != SR_ST_ACTIVE)
454 if (!(devc = sdi->priv)) {
455 sr_err("sdi->priv was NULL.");
459 sr_dbg("Stopping acquisition.");
461 sr_source_remove(devc->serial->fd);
462 hw_dev_close((struct sr_dev_inst *)sdi);
464 /* Send end packet to the session bus. */
465 sr_dbg("Sending SR_DF_END.");
466 packet.type = SR_DF_END;
467 sr_session_send(cb_data, &packet);
472 SR_PRIV struct sr_dev_driver digitek_dt4000zc_driver_info = {
473 .name = "digitek-dt4000zc",
474 .longname = "Digitek DT4000ZC",
476 .init = hw_init_digitek_dt4000zc,
477 .cleanup = hw_cleanup,
479 .dev_list = hw_dev_list,
480 .dev_clear = clear_instances,
481 .dev_open = hw_dev_open,
482 .dev_close = hw_dev_close,
483 .info_get = hw_info_get,
484 .dev_config_set = hw_dev_config_set,
485 .dev_acquisition_start = hw_dev_acquisition_start,
486 .dev_acquisition_stop = hw_dev_acquisition_stop,
490 SR_PRIV struct sr_dev_driver tekpower_tp4000zc_driver_info = {
491 .name = "tekpower-tp4000zc",
492 .longname = "TekPower TP4000ZC",
494 .init = hw_init_tekpower_tp4000zc,
495 .cleanup = hw_cleanup,
497 .dev_list = hw_dev_list,
498 .dev_clear = clear_instances,
499 .dev_open = hw_dev_open,
500 .dev_close = hw_dev_close,
501 .info_get = hw_info_get,
502 .dev_config_set = hw_dev_config_set,
503 .dev_acquisition_start = hw_dev_acquisition_start,
504 .dev_acquisition_stop = hw_dev_acquisition_stop,
508 SR_PRIV struct sr_dev_driver metex_me31_driver_info = {
509 .name = "metex-me31",
510 .longname = "Metex ME-31",
512 .init = hw_init_metex_me31,
513 .cleanup = hw_cleanup,
515 .dev_list = hw_dev_list,
516 .dev_clear = clear_instances,
517 .dev_open = hw_dev_open,
518 .dev_close = hw_dev_close,
519 .info_get = hw_info_get,
520 .dev_config_set = hw_dev_config_set,
521 .dev_acquisition_start = hw_dev_acquisition_start,
522 .dev_acquisition_stop = hw_dev_acquisition_stop,
526 SR_PRIV struct sr_dev_driver peaktech_3410_driver_info = {
527 .name = "peaktech-3410",
528 .longname = "PeakTech 3410",
530 .init = hw_init_peaktech_3410,
531 .cleanup = hw_cleanup,
533 .dev_list = hw_dev_list,
534 .dev_clear = clear_instances,
535 .dev_open = hw_dev_open,
536 .dev_close = hw_dev_close,
537 .info_get = hw_info_get,
538 .dev_config_set = hw_dev_config_set,
539 .dev_acquisition_start = hw_dev_acquisition_start,
540 .dev_acquisition_stop = hw_dev_acquisition_stop,