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 tekpower_tp4000zc_driver_info;
51 static struct sr_dev_driver *di_tekpower_tp4000zc = &tekpower_tp4000zc_driver_info;
53 /* After hw_init() this will point to a device-specific entry (see above). */
54 static struct sr_dev_driver *di = NULL;
56 SR_PRIV struct dmm_info dmms[] = {
58 "TekPower", "TP4000ZC",
61 sr_fs9721_packet_valid,
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(int dmm)
95 struct drv_context *drvc;
97 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
98 sr_err("Driver context malloc failed.");
102 if (dmm == TEKPOWER_TP4000ZC)
103 di = di_tekpower_tp4000zc;
104 sr_dbg("Selected '%s' subdriver.", di->name);
111 static int hw_init_tekpower_tp4000zc(void)
113 return hw_init(TEKPOWER_TP4000ZC);
116 static GSList *scan(const char *conn, const char *serialcomm, int dmm)
118 struct sr_dev_inst *sdi;
119 struct drv_context *drvc;
120 struct dev_context *devc;
121 struct sr_probe *probe;
122 struct sr_serial_dev_inst *serial;
128 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
131 if (serial_open(serial, SERIAL_RDONLY | SERIAL_NONBLOCK) != SR_OK)
134 sr_info("Probing port %s readonly.", conn);
138 serial_flush(serial);
141 * There's no way to get an ID from the multimeter. It just sends data
142 * periodically, so the best we can do is check if the packets match
143 * the expected format.
146 /* Let's get a bit of data and see if we can find a packet. */
149 ret = serial_stream_detect(serial, buf, &len, dmms[dmm].packet_size,
150 dmms[dmm].packet_valid, 1000,
156 * If we dropped more than two packets worth of data, something is
157 * wrong. We shouldn't quit however, since the dropped bytes might be
158 * just zeroes at the beginning of the stream. Those can occur as a
159 * combination of the nonstandard cable that ships with this device and
160 * the serial port or USB to serial adapter.
162 dropped = len - dmms[dmm].packet_size;
163 if (dropped > 2 * dmms[dmm].packet_size)
164 sr_warn("Had to drop too much data.");
166 sr_info("Found device on port %s.", conn);
168 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, dmms[dmm].vendor,
169 dmms[dmm].device, "")))
172 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
173 sr_err("Device context malloc failed.");
177 devc->serial = serial;
181 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
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);
188 serial_close(serial);
193 static GSList *hw_scan(GSList *options)
195 struct sr_hwopt *opt;
197 const char *conn, *serialcomm;
200 conn = serialcomm = NULL;
201 for (l = options; l; l = l->next) {
203 switch (opt->hwopt) {
207 case SR_HWOPT_SERIALCOMM:
208 serialcomm = opt->value;
215 if (!strcmp(di->name, "tekpower-tp4000zc"))
219 /* Use the provided comm specs. */
220 devices = scan(conn, serialcomm, dmm);
222 /* Try the default. */
223 devices = scan(conn, dmms[dmm].conn, dmm);
229 static GSList *hw_dev_list(void)
231 struct drv_context *drvc;
235 return drvc->instances;
238 static int hw_dev_open(struct sr_dev_inst *sdi)
240 struct dev_context *devc;
242 if (!(devc = sdi->priv)) {
243 sr_err("sdi->priv was NULL.");
247 if (serial_open(devc->serial, SERIAL_RDONLY) != SR_OK)
250 sdi->status = SR_ST_ACTIVE;
255 static int hw_dev_close(struct sr_dev_inst *sdi)
257 struct dev_context *devc;
259 if (!(devc = sdi->priv)) {
260 sr_err("sdi->priv was NULL.");
264 if (devc->serial && devc->serial->fd != -1) {
265 serial_close(devc->serial);
266 sdi->status = SR_ST_INACTIVE;
272 static int hw_cleanup(void)
279 static int hw_info_get(int info_id, const void **data,
280 const struct sr_dev_inst *sdi)
291 case SR_DI_NUM_PROBES:
292 *data = GINT_TO_POINTER(1);
294 case SR_DI_PROBE_NAMES:
304 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
307 struct dev_context *devc;
309 if (sdi->status != SR_ST_ACTIVE)
312 if (!(devc = sdi->priv)) {
313 sr_err("sdi->priv was NULL.");
318 case SR_HWCAP_LIMIT_SAMPLES:
319 devc->limit_samples = *(const uint64_t *)value;
320 sr_dbg("Setting sample limit to %" PRIu64 ".",
321 devc->limit_samples);
324 sr_err("Unknown capability: %d.", hwcap);
332 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
335 struct sr_datafeed_packet packet;
336 struct sr_datafeed_header header;
337 struct sr_datafeed_meta_analog meta;
338 struct dev_context *devc;
339 int (*receive_data)(int, int, void *) = NULL;
341 if (!(devc = sdi->priv)) {
342 sr_err("sdi->priv was NULL.");
346 sr_dbg("Starting acquisition.");
348 devc->cb_data = cb_data;
351 * Reset the number of samples to take. If we've already collected our
352 * quota, but we start a new session, and don't reset this, we'll just
353 * quit without acquiring any new samples.
355 devc->num_samples = 0;
357 /* Send header packet to the session bus. */
358 sr_dbg("Sending SR_DF_HEADER.");
359 packet.type = SR_DF_HEADER;
360 packet.payload = (uint8_t *)&header;
361 header.feed_version = 1;
362 gettimeofday(&header.starttime, NULL);
363 sr_session_send(devc->cb_data, &packet);
365 /* Send metadata about the SR_DF_ANALOG packets to come. */
366 sr_dbg("Sending SR_DF_META_ANALOG.");
367 packet.type = SR_DF_META_ANALOG;
368 packet.payload = &meta;
370 sr_session_send(devc->cb_data, &packet);
372 if (!strcmp(di->name, "tekpower-tp4000zc"))
373 receive_data = tekpower_tp4000zc_receive_data;
375 /* Poll every 50ms, or whenever some data comes in. */
376 sr_source_add(devc->serial->fd, G_IO_IN, 50,
377 receive_data, (void *)sdi);
382 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
384 struct sr_datafeed_packet packet;
385 struct dev_context *devc;
387 if (sdi->status != SR_ST_ACTIVE)
390 if (!(devc = sdi->priv)) {
391 sr_err("sdi->priv was NULL.");
395 sr_dbg("Stopping acquisition.");
397 sr_source_remove(devc->serial->fd);
398 hw_dev_close((struct sr_dev_inst *)sdi);
400 /* Send end packet to the session bus. */
401 sr_dbg("Sending SR_DF_END.");
402 packet.type = SR_DF_END;
403 sr_session_send(cb_data, &packet);
408 SR_PRIV struct sr_dev_driver tekpower_tp4000zc_driver_info = {
409 .name = "tekpower-tp4000zc",
410 .longname = "TekPower TP4000ZC",
412 .init = hw_init_tekpower_tp4000zc,
413 .cleanup = hw_cleanup,
415 .dev_list = hw_dev_list,
416 .dev_clear = clear_instances,
417 .dev_open = hw_dev_open,
418 .dev_close = hw_dev_close,
419 .info_get = hw_info_get,
420 .dev_config_set = hw_dev_config_set,
421 .dev_acquisition_start = hw_dev_acquisition_start,
422 .dev_acquisition_stop = hw_dev_acquisition_stop,