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;
53 static struct sr_dev_driver *di_digitek_dt4000zc = &digitek_dt4000zc_driver_info;
54 static struct sr_dev_driver *di_tekpower_tp4000zc = &tekpower_tp4000zc_driver_info;
56 /* After hw_init() this will point to a device-specific entry (see above). */
57 static struct sr_dev_driver *di = NULL;
59 SR_PRIV struct dmm_info dmms[] = {
61 "Digitek", "DT4000ZC",
64 sr_fs9721_packet_valid,
69 "TekPower", "TP4000ZC",
72 sr_fs9721_packet_valid,
78 /* Properly close and free all devices. */
79 static int clear_instances(void)
81 struct sr_dev_inst *sdi;
82 struct drv_context *drvc;
83 struct dev_context *devc;
86 if (!(drvc = di->priv))
90 for (l = drvc->instances; l; l = l->next) {
93 if (!(devc = sdi->priv))
95 sr_serial_dev_inst_free(devc->serial);
96 sr_dev_inst_free(sdi);
98 g_slist_free(drvc->instances);
99 drvc->instances = NULL;
104 static int hw_init(int dmm)
106 struct drv_context *drvc;
108 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
109 sr_err("Driver context malloc failed.");
110 return SR_ERR_MALLOC;
113 if (dmm == DIGITEK_DT4000ZC)
114 di = di_digitek_dt4000zc;
115 if (dmm == TEKPOWER_TP4000ZC)
116 di = di_tekpower_tp4000zc;
117 sr_dbg("Selected '%s' subdriver.", di->name);
124 static int hw_init_digitek_dt4000zc(void)
126 return hw_init(DIGITEK_DT4000ZC);
129 static int hw_init_tekpower_tp4000zc(void)
131 return hw_init(TEKPOWER_TP4000ZC);
134 static GSList *scan(const char *conn, const char *serialcomm, int dmm)
136 struct sr_dev_inst *sdi;
137 struct drv_context *drvc;
138 struct dev_context *devc;
139 struct sr_probe *probe;
140 struct sr_serial_dev_inst *serial;
146 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
149 if (serial_open(serial, SERIAL_RDONLY | SERIAL_NONBLOCK) != SR_OK)
152 sr_info("Probing port %s readonly.", conn);
156 serial_flush(serial);
159 * There's no way to get an ID from the multimeter. It just sends data
160 * periodically, so the best we can do is check if the packets match
161 * the expected format.
164 /* Let's get a bit of data and see if we can find a packet. */
167 ret = serial_stream_detect(serial, buf, &len, dmms[dmm].packet_size,
168 dmms[dmm].packet_valid, 1000,
174 * If we dropped more than two packets worth of data, something is
175 * wrong. We shouldn't quit however, since the dropped bytes might be
176 * just zeroes at the beginning of the stream. Those can occur as a
177 * combination of the nonstandard cable that ships with this device and
178 * the serial port or USB to serial adapter.
180 dropped = len - dmms[dmm].packet_size;
181 if (dropped > 2 * dmms[dmm].packet_size)
182 sr_warn("Had to drop too much data.");
184 sr_info("Found device on port %s.", conn);
186 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, dmms[dmm].vendor,
187 dmms[dmm].device, "")))
190 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
191 sr_err("Device context malloc failed.");
195 devc->serial = serial;
199 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
201 sdi->probes = g_slist_append(sdi->probes, probe);
202 drvc->instances = g_slist_append(drvc->instances, sdi);
203 devices = g_slist_append(devices, sdi);
206 serial_close(serial);
211 static GSList *hw_scan(GSList *options)
213 struct sr_hwopt *opt;
215 const char *conn, *serialcomm;
218 conn = serialcomm = NULL;
219 for (l = options; l; l = l->next) {
221 switch (opt->hwopt) {
225 case SR_HWOPT_SERIALCOMM:
226 serialcomm = opt->value;
233 if (!strcmp(di->name, "digitek-dt4000zc"))
235 if (!strcmp(di->name, "tekpower-tp4000zc"))
239 /* Use the provided comm specs. */
240 devices = scan(conn, serialcomm, dmm);
242 /* Try the default. */
243 devices = scan(conn, dmms[dmm].conn, dmm);
249 static GSList *hw_dev_list(void)
251 struct drv_context *drvc;
255 return drvc->instances;
258 static int hw_dev_open(struct sr_dev_inst *sdi)
260 struct dev_context *devc;
262 if (!(devc = sdi->priv)) {
263 sr_err("sdi->priv was NULL.");
267 if (serial_open(devc->serial, SERIAL_RDONLY) != SR_OK)
270 sdi->status = SR_ST_ACTIVE;
275 static int hw_dev_close(struct sr_dev_inst *sdi)
277 struct dev_context *devc;
279 if (!(devc = sdi->priv)) {
280 sr_err("sdi->priv was NULL.");
284 if (devc->serial && devc->serial->fd != -1) {
285 serial_close(devc->serial);
286 sdi->status = SR_ST_INACTIVE;
292 static int hw_cleanup(void)
299 static int hw_info_get(int info_id, const void **data,
300 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_SAMPLES:
339 devc->limit_samples = *(const uint64_t *)value;
340 sr_dbg("Setting sample limit to %" PRIu64 ".",
341 devc->limit_samples);
344 sr_err("Unknown capability: %d.", hwcap);
352 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
355 struct sr_datafeed_packet packet;
356 struct sr_datafeed_header header;
357 struct sr_datafeed_meta_analog meta;
358 struct dev_context *devc;
359 int (*receive_data)(int, int, void *) = NULL;
361 if (!(devc = sdi->priv)) {
362 sr_err("sdi->priv was NULL.");
366 sr_dbg("Starting acquisition.");
368 devc->cb_data = cb_data;
371 * Reset the number of samples to take. If we've already collected our
372 * quota, but we start a new session, and don't reset this, we'll just
373 * quit without acquiring any new samples.
375 devc->num_samples = 0;
377 /* Send header packet to the session bus. */
378 sr_dbg("Sending SR_DF_HEADER.");
379 packet.type = SR_DF_HEADER;
380 packet.payload = (uint8_t *)&header;
381 header.feed_version = 1;
382 gettimeofday(&header.starttime, NULL);
383 sr_session_send(devc->cb_data, &packet);
385 /* Send metadata about the SR_DF_ANALOG packets to come. */
386 sr_dbg("Sending SR_DF_META_ANALOG.");
387 packet.type = SR_DF_META_ANALOG;
388 packet.payload = &meta;
390 sr_session_send(devc->cb_data, &packet);
392 if (!strcmp(di->name, "digitek-dt4000zc"))
393 receive_data = digitek_dt4000zc_receive_data;
394 if (!strcmp(di->name, "tekpower-tp4000zc"))
395 receive_data = tekpower_tp4000zc_receive_data;
397 /* Poll every 50ms, or whenever some data comes in. */
398 sr_source_add(devc->serial->fd, G_IO_IN, 50,
399 receive_data, (void *)sdi);
404 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
406 struct sr_datafeed_packet packet;
407 struct dev_context *devc;
409 if (sdi->status != SR_ST_ACTIVE)
412 if (!(devc = sdi->priv)) {
413 sr_err("sdi->priv was NULL.");
417 sr_dbg("Stopping acquisition.");
419 sr_source_remove(devc->serial->fd);
420 hw_dev_close((struct sr_dev_inst *)sdi);
422 /* Send end packet to the session bus. */
423 sr_dbg("Sending SR_DF_END.");
424 packet.type = SR_DF_END;
425 sr_session_send(cb_data, &packet);
430 SR_PRIV struct sr_dev_driver digitek_dt4000zc_driver_info = {
431 .name = "digitek-dt4000zc",
432 .longname = "Digitek DT4000ZC",
434 .init = hw_init_digitek_dt4000zc,
435 .cleanup = hw_cleanup,
437 .dev_list = hw_dev_list,
438 .dev_clear = clear_instances,
439 .dev_open = hw_dev_open,
440 .dev_close = hw_dev_close,
441 .info_get = hw_info_get,
442 .dev_config_set = hw_dev_config_set,
443 .dev_acquisition_start = hw_dev_acquisition_start,
444 .dev_acquisition_stop = hw_dev_acquisition_stop,
448 SR_PRIV struct sr_dev_driver tekpower_tp4000zc_driver_info = {
449 .name = "tekpower-tp4000zc",
450 .longname = "TekPower TP4000ZC",
452 .init = hw_init_tekpower_tp4000zc,
453 .cleanup = hw_cleanup,
455 .dev_list = hw_dev_list,
456 .dev_clear = clear_instances,
457 .dev_open = hw_dev_open,
458 .dev_close = hw_dev_close,
459 .info_get = hw_info_get,
460 .dev_config_set = hw_dev_config_set,
461 .dev_acquisition_start = hw_dev_acquisition_start,
462 .dev_acquisition_stop = hw_dev_acquisition_stop,