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 return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN);
50 static void free_instance(void *inst)
52 struct sr_dev_inst *sdi;
53 struct dev_context *devc;
56 if (!(devc = sdi->priv))
58 sr_serial_dev_inst_free(devc->serial);
59 sr_dev_inst_free(sdi);
62 /* Properly close and free all devices. */
63 static int clear_instances(void)
65 struct drv_context *drvc;
67 if (!(drvc = di->priv))
70 g_slist_free_full(drvc->instances, free_instance);
71 drvc->instances = NULL;
76 static GSList *brymen_scan(const char *conn, const char *serialcomm)
78 struct sr_dev_inst *sdi;
79 struct dev_context *devc;
80 struct drv_context *drvc;
81 struct sr_probe *probe;
82 struct sr_serial_dev_inst *serial;
86 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
89 if (serial_open(serial, SERIAL_RDWR|SERIAL_NONBLOCK) != SR_OK)
92 sr_info("Probing port %s.", conn);
97 if (brymen_packet_request(serial) == -1) {
98 sr_err("Unable to send command. code: %d.", errno);
105 ret = brymen_stream_detect(serial, buf, &len, brymen_packet_length,
106 brymen_packet_is_valid, 1000, 9600);
110 sr_info("Found device on port %s.", conn);
112 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Brymen", "BM85x", "")))
115 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
116 sr_err("Device context malloc failed.");
120 devc->serial = serial;
125 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
128 sdi->probes = g_slist_append(sdi->probes, probe);
129 drvc->instances = g_slist_append(drvc->instances, sdi);
130 devices = g_slist_append(devices, sdi);
134 serial_close(serial);
139 static GSList *hw_scan(GSList *options)
141 struct drv_context *drvc;
142 struct sr_config *src;
144 const char *conn, *serialcomm;
150 drvc->instances = NULL;
152 conn = serialcomm = NULL;
153 for (l = options; l; l = l->next) {
159 case SR_CONF_SERIALCOMM:
160 serialcomm = src->value;
169 /* Use the provided comm specs. */
170 devices = brymen_scan(conn, serialcomm);
172 /* But 9600 8N1 should work all of the time */
173 devices = brymen_scan(conn, "9600/8n1/dtr=1/rts=1");
179 static GSList *hw_dev_list(void)
181 struct drv_context *drvc;
185 return drvc->instances;
188 static int hw_dev_open(struct sr_dev_inst *sdi)
190 struct dev_context *devc;
192 if (!(devc = sdi->priv)) {
193 sr_err("sdi->priv was NULL.");
197 if (serial_open(devc->serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
200 sdi->status = SR_ST_ACTIVE;
205 static int hw_dev_close(struct sr_dev_inst *sdi)
207 struct dev_context *devc;
209 if (!(devc = sdi->priv)) {
210 sr_err("sdi->priv was NULL.");
214 if (devc->serial && devc->serial->fd != -1) {
215 serial_close(devc->serial);
216 sdi->status = SR_ST_INACTIVE;
222 static int hw_cleanup(void)
229 static int config_list(int key, const void **data,
230 const struct sr_dev_inst *sdi)
235 case SR_CONF_SCAN_OPTIONS:
238 case SR_CONF_DEVICE_OPTIONS:
242 sr_err("Unknown config key: %d.", key);
249 static int hw_dev_config_set(int id, const void *value,
250 const struct sr_dev_inst *sdi)
252 struct dev_context *devc;
255 if (sdi->status != SR_ST_ACTIVE) {
256 sr_err("Device inactive, can't set config options.");
260 if (!(devc = sdi->priv)) {
261 sr_err("sdi->priv was NULL.");
267 case SR_CONF_LIMIT_SAMPLES:
268 devc->limit_samples = *(const uint64_t*)value;
270 case SR_CONF_LIMIT_MSEC:
271 devc->limit_msec = *(const uint64_t*)value;
274 sr_err("Unknown hardware capability: %d.", id);
281 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
284 struct sr_datafeed_packet packet;
285 struct sr_datafeed_header header;
286 struct dev_context *devc;
288 if (!(devc = sdi->priv)) {
289 sr_err("sdi->priv was NULL.");
293 sr_dbg("Starting acquisition.");
295 devc->cb_data = cb_data;
298 * Reset the number of samples to take. If we've already collected our
299 * quota, but we start a new session, and don't reset this, we'll just
300 * quit without acquiring any new samples.
302 devc->num_samples = 0;
303 devc->starttime = g_get_monotonic_time();
305 /* Send header packet to the session bus. */
306 sr_dbg("Sending SR_DF_HEADER.");
307 packet.type = SR_DF_HEADER;
308 packet.payload = &header;
309 header.feed_version = 1;
310 gettimeofday(&header.starttime, NULL);
311 sr_session_send(devc->cb_data, &packet);
313 /* Poll every 50ms, or whenever some data comes in. */
314 sr_source_add(devc->serial->fd, G_IO_IN, 50,
315 brymen_dmm_receive_data, (void *)sdi);
320 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
322 struct sr_datafeed_packet packet;
323 struct dev_context *devc;
325 if (sdi->status != SR_ST_ACTIVE) {
326 sr_err("Device inactive, can't stop acquisition.");
330 if (!(devc = sdi->priv)) {
331 sr_err("sdi->priv was NULL.");
335 sr_dbg("Stopping acquisition.");
337 sr_source_remove(devc->serial->fd);
338 hw_dev_close((struct sr_dev_inst *)sdi);
340 /* Send end packet to the session bus. */
341 sr_dbg("Sending SR_DF_END.");
342 packet.type = SR_DF_END;
343 sr_session_send(cb_data, &packet);
348 SR_PRIV struct sr_dev_driver brymen_dmm_driver_info = {
349 .name = "brymen-dmm",
350 .longname = "Brymen BM850 series",
353 .cleanup = hw_cleanup,
355 .dev_list = hw_dev_list,
356 .dev_clear = clear_instances,
357 .dev_open = hw_dev_open,
358 .dev_close = hw_dev_close,
359 .config_list = config_list,
360 .config_set = hw_dev_config_set,
361 .dev_acquisition_start = hw_dev_acquisition_start,
362 .dev_acquisition_stop = hw_dev_acquisition_stop,