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>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/types.h>
27 #include "libsigrok.h"
28 #include "libsigrok-internal.h"
31 #define SERIALCOMM "4800/8n1"
33 static const int hwopts[] = {
39 static const int hwcaps[] = {
41 SR_HWCAP_LIMIT_SAMPLES,
46 static const char *probe_names[] = {
51 SR_PRIV struct sr_dev_driver radioshackdmm_driver_info;
52 static struct sr_dev_driver *di = &radioshackdmm_driver_info;
54 /* Properly close and free all devices. */
55 static int clear_instances(void)
57 struct sr_dev_inst *sdi;
58 struct drv_context *drvc;
59 struct dev_context *devc;
62 if (!(drvc = di->priv))
66 for (l = drvc->instances; l; l = l->next) {
69 if (!(devc = sdi->priv))
71 sr_serial_dev_inst_free(devc->serial);
72 sr_dev_inst_free(sdi);
74 g_slist_free(drvc->instances);
75 drvc->instances = NULL;
80 static int hw_init(void)
82 struct drv_context *drvc;
84 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
85 sr_err("Driver context malloc failed.");
94 static GSList *rs_22_812_scan(const char *conn, const char *serialcomm)
96 struct sr_dev_inst *sdi;
97 struct drv_context *drvc;
98 struct dev_context *devc;
99 struct sr_probe *probe;
100 struct sr_serial_dev_inst *serial;
103 size_t len, i, good_packets, dropped;
105 const struct rs_22_812_packet *rs_packet;
107 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
110 if (serial_open(serial, O_RDONLY|O_NONBLOCK) != SR_OK)
113 sr_info("Probing port '%s' readonly.", conn);
121 * There's no way to get an ID from the multimeter. It just sends data
122 * periodically, so the best we can do is check if the packets match
123 * the expected format.
125 while (!devices && retry < 3) {
128 serial_flush(serial);
130 /* Let's get a bit of data and see if we can find a packet. */
132 serial_readline(serial, &b, &len, 250);
133 if ((len == 0) || (len < RS_22_812_PACKET_SIZE)) {
134 /* Not enough data received, is the DMM connected? */
138 /* Treat our buffer as stream, and find any valid packets. */
139 for (i = 0; i < len - RS_22_812_PACKET_SIZE + 1;) {
140 rs_packet = (void *)(&buf[i]);
141 if (!rs_22_812_packet_valid(rs_packet)) {
146 i += RS_22_812_PACKET_SIZE;
149 /* If we dropped more than two packets, something is wrong. */
150 dropped = len - (good_packets * RS_22_812_PACKET_SIZE);
151 if (dropped > 2 * RS_22_812_PACKET_SIZE)
154 /* Let's see if we have anything good. */
155 if (good_packets == 0)
158 sr_info("Found RadioShack 22-812 on port '%s'.", conn);
160 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "RadioShack",
164 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
165 sr_err("Device context malloc failed.");
169 devc->serial = serial;
173 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
175 sdi->probes = g_slist_append(sdi->probes, probe);
176 drvc->instances = g_slist_append(drvc->instances, sdi);
177 devices = g_slist_append(devices, sdi);
180 serial_close(serial);
185 static GSList *hw_scan(GSList * options)
187 struct sr_hwopt *opt;
189 const char *conn, *serialcomm;
191 conn = serialcomm = NULL;
192 for (l = options; l; l = l->next) {
194 switch (opt->hwopt) {
198 case SR_HWOPT_SERIALCOMM:
199 serialcomm = opt->value;
207 /* Use the provided comm specs. */
208 devices = rs_22_812_scan(conn, serialcomm);
210 /* Try the default. */
211 devices = rs_22_812_scan(conn, SERIALCOMM);
216 static GSList *hw_dev_list(void)
218 struct drv_context *drvc;
222 return drvc->instances;
225 static int hw_dev_open(struct sr_dev_inst *sdi)
227 struct dev_context *devc;
229 if (!(devc = sdi->priv)) {
230 sr_err("sdi->priv was NULL.");
234 if (serial_open(devc->serial, O_RDONLY) != SR_OK)
237 sdi->status = SR_ST_ACTIVE;
242 static int hw_dev_close(struct sr_dev_inst *sdi)
244 struct dev_context *devc;
246 if (!(devc = sdi->priv)) {
247 sr_err("sdi->priv was NULL.");
251 if (devc->serial && devc->serial->fd != -1) {
252 serial_close(devc->serial);
253 sdi->status = SR_ST_INACTIVE;
259 static int hw_cleanup(void)
266 static int hw_info_get(int info_id, const void **data,
267 const struct sr_dev_inst *sdi)
278 case SR_DI_NUM_PROBES:
279 *data = GINT_TO_POINTER(1);
281 case SR_DI_PROBE_NAMES:
285 sr_err("Unknown info_id: %d.", info_id);
292 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
295 struct dev_context *devc;
297 if (sdi->status != SR_ST_ACTIVE)
300 if (!(devc = sdi->priv)) {
301 sr_err("sdi->priv was NULL.");
306 case SR_HWCAP_LIMIT_SAMPLES:
307 devc->limit_samples = *(const uint64_t *)value;
308 sr_dbg("Setting sample limit to %" PRIu64 ".",
309 devc->limit_samples);
312 sr_err("Unknown capability: %d.", hwcap);
320 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
323 struct sr_datafeed_packet packet;
324 struct sr_datafeed_header header;
325 struct sr_datafeed_meta_analog meta;
326 struct dev_context *devc;
328 if (!(devc = sdi->priv)) {
329 sr_err("sdi->priv was NULL.");
333 sr_dbg("Starting acquisition.");
335 devc->cb_data = cb_data;
338 * Reset the number of samples to take. If we've already collected our
339 * quota, but we start a new session, and don't reset this, we'll just
340 * quit without aquiring any new samples.
342 devc->num_samples = 0;
344 /* Send header packet to the session bus. */
345 sr_dbg("Sending SR_DF_HEADER.");
346 packet.type = SR_DF_HEADER;
347 packet.payload = (uint8_t *) & header;
348 header.feed_version = 1;
349 gettimeofday(&header.starttime, NULL);
350 sr_session_send(devc->cb_data, &packet);
352 /* Send metadata about the SR_DF_ANALOG packets to come. */
353 sr_dbg("Sending SR_DF_META_ANALOG.");
354 packet.type = SR_DF_META_ANALOG;
355 packet.payload = &meta;
357 sr_session_send(devc->cb_data, &packet);
359 /* Poll every 50ms, or whenever some data comes in. */
360 sr_source_add(devc->serial->fd, G_IO_IN, 50,
361 radioshack_dmm_receive_data, (void *)sdi);
366 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
368 struct sr_datafeed_packet packet;
369 struct dev_context *devc;
371 if (sdi->status != SR_ST_ACTIVE)
374 if (!(devc = sdi->priv)) {
375 sr_err("sdi->priv was NULL.");
379 sr_dbg("Stopping acquisition.");
381 sr_source_remove(devc->serial->fd);
382 hw_dev_close((struct sr_dev_inst *)sdi);
384 /* Send end packet to the session bus. */
385 sr_dbg("Sending SR_DF_END.");
386 packet.type = SR_DF_END;
387 sr_session_send(cb_data, &packet);
392 SR_PRIV struct sr_dev_driver radioshackdmm_driver_info = {
393 .name = "radioshack-dmm",
394 .longname = "RadioShack 22-812/22-039 DMMs",
397 .cleanup = hw_cleanup,
399 .dev_list = hw_dev_list,
400 .dev_clear = clear_instances,
401 .dev_open = hw_dev_open,
402 .dev_close = hw_dev_close,
403 .info_get = hw_info_get,
404 .dev_config_set = hw_dev_config_set,
405 .dev_acquisition_start = hw_dev_acquisition_start,
406 .dev_acquisition_stop = hw_dev_acquisition_stop,