2 * This file is part of the libsigrok project.
4 * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "libsigrok.h"
25 #include "libsigrok-internal.h"
28 static const int hwopts[] = {
34 static const int hwcaps[] = {
36 SR_CONF_LIMIT_SAMPLES,
42 SR_PRIV struct sr_dev_driver brymen_dmm_driver_info;
43 static struct sr_dev_driver *di = &brymen_dmm_driver_info;
45 static int hw_init(struct sr_context *sr_ctx)
47 struct drv_context *drvc;
49 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
50 sr_err("Driver context malloc failed.");
54 drvc->sr_ctx = sr_ctx;
60 static void free_instance(void *inst)
62 struct sr_dev_inst *sdi;
63 struct dev_context *devc;
66 if (!(devc = sdi->priv))
68 sr_serial_dev_inst_free(devc->serial);
69 sr_dev_inst_free(sdi);
72 /* Properly close and free all devices. */
73 static int clear_instances(void)
75 struct drv_context *drvc;
77 if (!(drvc = di->priv))
80 g_slist_free_full(drvc->instances, free_instance);
81 drvc->instances = NULL;
86 static GSList *brymen_scan(const char *conn, const char *serialcomm)
88 struct sr_dev_inst *sdi;
89 struct dev_context *devc;
90 struct drv_context *drvc;
91 struct sr_probe *probe;
92 struct sr_serial_dev_inst *serial;
96 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
99 if (serial_open(serial, SERIAL_RDWR|SERIAL_NONBLOCK) != SR_OK)
102 sr_info("Probing port %s.", conn);
106 /* Request reading */
107 if (brymen_packet_request(serial) == -1) {
108 sr_err("Unable to send command. code: %d.", errno);
115 ret = brymen_stream_detect(serial, buf, &len, brymen_packet_length,
116 brymen_packet_is_valid, 1000, 9600);
120 sr_info("Found device on port %s.", conn);
122 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Brymen", "BM85x", "")))
125 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
126 sr_err("Device context malloc failed.");
130 devc->serial = serial;
135 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
138 sdi->probes = g_slist_append(sdi->probes, probe);
139 drvc->instances = g_slist_append(drvc->instances, sdi);
140 devices = g_slist_append(devices, sdi);
144 serial_close(serial);
149 static GSList *hw_scan(GSList *options)
151 struct drv_context *drvc;
152 struct sr_config *src;
154 const char *conn, *serialcomm;
160 drvc->instances = NULL;
162 conn = serialcomm = NULL;
163 for (l = options; l; l = l->next) {
169 case SR_CONF_SERIALCOMM:
170 serialcomm = src->value;
179 /* Use the provided comm specs. */
180 devices = brymen_scan(conn, serialcomm);
182 /* But 9600 8N1 should work all of the time */
183 devices = brymen_scan(conn, "9600/8n1/dtr=1/rts=1");
189 static GSList *hw_dev_list(void)
191 struct drv_context *drvc;
195 return drvc->instances;
198 static int hw_dev_open(struct sr_dev_inst *sdi)
200 struct dev_context *devc;
202 if (!(devc = sdi->priv)) {
203 sr_err("sdi->priv was NULL.");
207 if (serial_open(devc->serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
210 sdi->status = SR_ST_ACTIVE;
215 static int hw_dev_close(struct sr_dev_inst *sdi)
217 struct dev_context *devc;
219 if (!(devc = sdi->priv)) {
220 sr_err("sdi->priv was NULL.");
224 if (devc->serial && devc->serial->fd != -1) {
225 serial_close(devc->serial);
226 sdi->status = SR_ST_INACTIVE;
232 static int hw_cleanup(void)
239 static int config_list(int key, const void **data,
240 const struct sr_dev_inst *sdi)
245 case SR_CONF_SCAN_OPTIONS:
248 case SR_CONF_DEVICE_OPTIONS:
252 sr_err("Unknown config key: %d.", key);
259 static int hw_dev_config_set(int id, const void *value,
260 const struct sr_dev_inst *sdi)
262 struct dev_context *devc;
265 if (sdi->status != SR_ST_ACTIVE) {
266 sr_err("Device inactive, can't set config options.");
270 if (!(devc = sdi->priv)) {
271 sr_err("sdi->priv was NULL.");
277 case SR_CONF_LIMIT_SAMPLES:
278 devc->limit_samples = *(const uint64_t*)value;
280 case SR_CONF_LIMIT_MSEC:
281 devc->limit_msec = *(const uint64_t*)value;
284 sr_err("Unknown hardware capability: %d.", id);
291 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
294 struct sr_datafeed_packet packet;
295 struct sr_datafeed_header header;
296 struct dev_context *devc;
298 if (!(devc = sdi->priv)) {
299 sr_err("sdi->priv was NULL.");
303 sr_dbg("Starting acquisition.");
305 devc->cb_data = cb_data;
308 * Reset the number of samples to take. If we've already collected our
309 * quota, but we start a new session, and don't reset this, we'll just
310 * quit without acquiring any new samples.
312 devc->num_samples = 0;
313 devc->starttime = g_get_monotonic_time();
315 /* Send header packet to the session bus. */
316 sr_dbg("Sending SR_DF_HEADER.");
317 packet.type = SR_DF_HEADER;
318 packet.payload = &header;
319 header.feed_version = 1;
320 gettimeofday(&header.starttime, NULL);
321 sr_session_send(devc->cb_data, &packet);
323 /* Poll every 50ms, or whenever some data comes in. */
324 sr_source_add(devc->serial->fd, G_IO_IN, 50,
325 brymen_dmm_receive_data, (void *)sdi);
330 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
332 struct sr_datafeed_packet packet;
333 struct dev_context *devc;
335 if (sdi->status != SR_ST_ACTIVE) {
336 sr_err("Device inactive, can't stop acquisition.");
340 if (!(devc = sdi->priv)) {
341 sr_err("sdi->priv was NULL.");
345 sr_dbg("Stopping acquisition.");
347 sr_source_remove(devc->serial->fd);
348 hw_dev_close((struct sr_dev_inst *)sdi);
350 /* Send end packet to the session bus. */
351 sr_dbg("Sending SR_DF_END.");
352 packet.type = SR_DF_END;
353 sr_session_send(cb_data, &packet);
358 SR_PRIV struct sr_dev_driver brymen_dmm_driver_info = {
359 .name = "brymen-dmm",
360 .longname = "Brymen BM850 series",
363 .cleanup = hw_cleanup,
365 .dev_list = hw_dev_list,
366 .dev_clear = clear_instances,
367 .dev_open = hw_dev_open,
368 .dev_close = hw_dev_close,
369 .config_list = config_list,
370 .config_set = hw_dev_config_set,
371 .dev_acquisition_start = hw_dev_acquisition_start,
372 .dev_acquisition_stop = hw_dev_acquisition_stop,