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;
54 static struct sr_dev_driver *di_dt4000zc = &digitek_dt4000zc_driver_info;
55 static struct sr_dev_driver *di_tp4000zc = &tekpower_tp4000zc_driver_info;
56 static struct sr_dev_driver *di_me31 = &metex_me31_driver_info;
58 /* After hw_init() this will point to a device-specific entry (see above). */
59 static struct sr_dev_driver *di = NULL;
61 SR_PRIV struct dmm_info dmms[] = {
63 "Digitek", "DT4000ZC", "2400/8n1", 2400,
64 FS9721_PACKET_SIZE, NULL,
65 sr_fs9721_packet_valid, sr_fs9721_parse,
69 "TekPower", "TP4000ZC", "2400/8n1", 2400,
70 FS9721_PACKET_SIZE, NULL,
71 sr_fs9721_packet_valid, sr_fs9721_parse,
75 "Metex", "ME-31", "600/7n2/rts=0/dtr=1", 600,
76 METEX14_PACKET_SIZE, sr_metex14_packet_request,
77 sr_metex14_packet_valid, sr_metex14_parse,
82 /* Properly close and free all devices. */
83 static int clear_instances(void)
85 struct sr_dev_inst *sdi;
86 struct drv_context *drvc;
87 struct dev_context *devc;
90 if (!(drvc = di->priv))
94 for (l = drvc->instances; l; l = l->next) {
97 if (!(devc = sdi->priv))
99 sr_serial_dev_inst_free(devc->serial);
100 sr_dev_inst_free(sdi);
102 g_slist_free(drvc->instances);
103 drvc->instances = NULL;
108 static int hw_init(int dmm)
110 struct drv_context *drvc;
112 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
113 sr_err("Driver context malloc failed.");
114 return SR_ERR_MALLOC;
117 if (dmm == DIGITEK_DT4000ZC)
119 if (dmm == TEKPOWER_TP4000ZC)
121 if (dmm == METEX_ME31)
123 sr_dbg("Selected '%s' subdriver.", di->name);
130 static int hw_init_digitek_dt4000zc(void)
132 return hw_init(DIGITEK_DT4000ZC);
135 static int hw_init_tekpower_tp4000zc(void)
137 return hw_init(TEKPOWER_TP4000ZC);
140 static int hw_init_metex_me31(void)
142 return hw_init(METEX_ME31);
145 static GSList *scan(const char *conn, const char *serialcomm, int dmm)
147 struct sr_dev_inst *sdi;
148 struct drv_context *drvc;
149 struct dev_context *devc;
150 struct sr_probe *probe;
151 struct sr_serial_dev_inst *serial;
157 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
160 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
163 sr_info("Probing port %s.", conn);
167 serial_flush(serial);
170 * There's no way to get an ID from the multimeter. It just sends data
171 * periodically, so the best we can do is check if the packets match
172 * the expected format.
175 /* Let's get a bit of data and see if we can find a packet. */
178 /* Request a packet if the DMM requires this. */
179 if (dmms[dmm].packet_request) {
180 if ((ret = dmms[dmm].packet_request(serial)) < 0) {
181 sr_err("Failed to request packet: %d.", ret);
186 ret = serial_stream_detect(serial, buf, &len, dmms[dmm].packet_size,
187 dmms[dmm].packet_valid, 1000,
193 * If we dropped more than two packets worth of data, something is
194 * wrong. We shouldn't quit however, since the dropped bytes might be
195 * just zeroes at the beginning of the stream. Those can occur as a
196 * combination of the nonstandard cable that ships with this device and
197 * the serial port or USB to serial adapter.
199 dropped = len - dmms[dmm].packet_size;
200 if (dropped > 2 * dmms[dmm].packet_size)
201 sr_warn("Had to drop too much data.");
203 sr_info("Found device on port %s.", conn);
205 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, dmms[dmm].vendor,
206 dmms[dmm].device, "")))
209 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
210 sr_err("Device context malloc failed.");
214 devc->serial = serial;
218 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
220 sdi->probes = g_slist_append(sdi->probes, probe);
221 drvc->instances = g_slist_append(drvc->instances, sdi);
222 devices = g_slist_append(devices, sdi);
225 serial_close(serial);
230 static GSList *hw_scan(GSList *options)
232 struct sr_hwopt *opt;
234 const char *conn, *serialcomm;
237 conn = serialcomm = NULL;
238 for (l = options; l; l = l->next) {
240 switch (opt->hwopt) {
244 case SR_HWOPT_SERIALCOMM:
245 serialcomm = opt->value;
252 if (!strcmp(di->name, "digitek-dt4000zc"))
254 if (!strcmp(di->name, "tekpower-tp4000zc"))
256 if (!strcmp(di->name, "metex-me31"))
260 /* Use the provided comm specs. */
261 devices = scan(conn, serialcomm, dmm);
263 /* Try the default. */
264 devices = scan(conn, dmms[dmm].conn, dmm);
270 static GSList *hw_dev_list(void)
272 struct drv_context *drvc;
276 return drvc->instances;
279 static int hw_dev_open(struct sr_dev_inst *sdi)
281 struct dev_context *devc;
283 if (!(devc = sdi->priv)) {
284 sr_err("sdi->priv was NULL.");
288 if (serial_open(devc->serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
291 sdi->status = SR_ST_ACTIVE;
296 static int hw_dev_close(struct sr_dev_inst *sdi)
298 struct dev_context *devc;
300 if (!(devc = sdi->priv)) {
301 sr_err("sdi->priv was NULL.");
305 if (devc->serial && devc->serial->fd != -1) {
306 serial_close(devc->serial);
307 sdi->status = SR_ST_INACTIVE;
313 static int hw_cleanup(void)
320 static int hw_info_get(int info_id, const void **data,
321 const struct sr_dev_inst *sdi)
332 case SR_DI_NUM_PROBES:
333 *data = GINT_TO_POINTER(1);
335 case SR_DI_PROBE_NAMES:
345 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
348 struct dev_context *devc;
350 if (sdi->status != SR_ST_ACTIVE)
353 if (!(devc = sdi->priv)) {
354 sr_err("sdi->priv was NULL.");
359 case SR_HWCAP_LIMIT_SAMPLES:
360 devc->limit_samples = *(const uint64_t *)value;
361 sr_dbg("Setting sample limit to %" PRIu64 ".",
362 devc->limit_samples);
365 sr_err("Unknown capability: %d.", hwcap);
373 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
376 struct sr_datafeed_packet packet;
377 struct sr_datafeed_header header;
378 struct sr_datafeed_meta_analog meta;
379 struct dev_context *devc;
380 int (*receive_data)(int, int, void *) = NULL;
382 if (!(devc = sdi->priv)) {
383 sr_err("sdi->priv was NULL.");
387 sr_dbg("Starting acquisition.");
389 devc->cb_data = cb_data;
392 * Reset the number of samples to take. If we've already collected our
393 * quota, but we start a new session, and don't reset this, we'll just
394 * quit without acquiring any new samples.
396 devc->num_samples = 0;
398 /* Send header packet to the session bus. */
399 sr_dbg("Sending SR_DF_HEADER.");
400 packet.type = SR_DF_HEADER;
401 packet.payload = (uint8_t *)&header;
402 header.feed_version = 1;
403 gettimeofday(&header.starttime, NULL);
404 sr_session_send(devc->cb_data, &packet);
406 /* Send metadata about the SR_DF_ANALOG packets to come. */
407 sr_dbg("Sending SR_DF_META_ANALOG.");
408 packet.type = SR_DF_META_ANALOG;
409 packet.payload = &meta;
411 sr_session_send(devc->cb_data, &packet);
413 if (!strcmp(di->name, "digitek-dt4000zc"))
414 receive_data = digitek_dt4000zc_receive_data;
415 if (!strcmp(di->name, "tekpower-tp4000zc"))
416 receive_data = tekpower_tp4000zc_receive_data;
417 if (!strcmp(di->name, "metex-me31"))
418 receive_data = metex_me31_receive_data;
420 /* Poll every 50ms, or whenever some data comes in. */
421 sr_source_add(devc->serial->fd, G_IO_IN, 50,
422 receive_data, (void *)sdi);
427 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
429 struct sr_datafeed_packet packet;
430 struct dev_context *devc;
432 if (sdi->status != SR_ST_ACTIVE)
435 if (!(devc = sdi->priv)) {
436 sr_err("sdi->priv was NULL.");
440 sr_dbg("Stopping acquisition.");
442 sr_source_remove(devc->serial->fd);
443 hw_dev_close((struct sr_dev_inst *)sdi);
445 /* Send end packet to the session bus. */
446 sr_dbg("Sending SR_DF_END.");
447 packet.type = SR_DF_END;
448 sr_session_send(cb_data, &packet);
453 SR_PRIV struct sr_dev_driver digitek_dt4000zc_driver_info = {
454 .name = "digitek-dt4000zc",
455 .longname = "Digitek DT4000ZC",
457 .init = hw_init_digitek_dt4000zc,
458 .cleanup = hw_cleanup,
460 .dev_list = hw_dev_list,
461 .dev_clear = clear_instances,
462 .dev_open = hw_dev_open,
463 .dev_close = hw_dev_close,
464 .info_get = hw_info_get,
465 .dev_config_set = hw_dev_config_set,
466 .dev_acquisition_start = hw_dev_acquisition_start,
467 .dev_acquisition_stop = hw_dev_acquisition_stop,
471 SR_PRIV struct sr_dev_driver tekpower_tp4000zc_driver_info = {
472 .name = "tekpower-tp4000zc",
473 .longname = "TekPower TP4000ZC",
475 .init = hw_init_tekpower_tp4000zc,
476 .cleanup = hw_cleanup,
478 .dev_list = hw_dev_list,
479 .dev_clear = clear_instances,
480 .dev_open = hw_dev_open,
481 .dev_close = hw_dev_close,
482 .info_get = hw_info_get,
483 .dev_config_set = hw_dev_config_set,
484 .dev_acquisition_start = hw_dev_acquisition_start,
485 .dev_acquisition_stop = hw_dev_acquisition_stop,
489 SR_PRIV struct sr_dev_driver metex_me31_driver_info = {
490 .name = "metex-me31",
491 .longname = "Metex ME-31",
493 .init = hw_init_metex_me31,
494 .cleanup = hw_cleanup,
496 .dev_list = hw_dev_list,
497 .dev_clear = clear_instances,
498 .dev_open = hw_dev_open,
499 .dev_close = hw_dev_close,
500 .info_get = hw_info_get,
501 .dev_config_set = hw_dev_config_set,
502 .dev_acquisition_start = hw_dev_acquisition_start,
503 .dev_acquisition_stop = hw_dev_acquisition_stop,