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/>.
22 static const int hwopts[] = {
28 static const int hwcaps[] = {
30 SR_CONF_LIMIT_SAMPLES,
36 SR_PRIV struct sr_dev_driver brymen_bm857_driver_info;
37 static struct sr_dev_driver *di = &brymen_bm857_driver_info;
39 static int hw_init(struct sr_context *sr_ctx)
41 return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN);
44 static void free_instance(void *inst)
46 struct sr_dev_inst *sdi;
47 struct dev_context *devc;
51 if (!(devc = sdi->priv))
53 sr_serial_dev_inst_free(devc->serial);
54 sr_dev_inst_free(sdi);
57 /* Properly close and free all devices. */
58 static int clear_instances(void)
60 struct drv_context *drvc;
62 if (!(drvc = di->priv))
65 g_slist_free_full(drvc->instances, free_instance);
66 drvc->instances = NULL;
71 static GSList *brymen_scan(const char *conn, const char *serialcomm)
73 struct sr_dev_inst *sdi;
74 struct dev_context *devc;
75 struct drv_context *drvc;
76 struct sr_probe *probe;
77 struct sr_serial_dev_inst *serial;
83 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
86 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
89 sr_info("Probing port %s.", conn);
94 if ((ret = brymen_packet_request(serial)) < 0) {
95 sr_err("Unable to send command: %d.", ret);
100 ret = brymen_stream_detect(serial, buf, &len, brymen_packet_length,
101 brymen_packet_is_valid, 1000, 9600);
105 sr_info("Found device on port %s.", conn);
107 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Brymen", "BM85x", "")))
110 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
111 sr_err("Device context malloc failed.");
115 devc->serial = serial;
120 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
123 sdi->probes = g_slist_append(sdi->probes, probe);
124 drvc->instances = g_slist_append(drvc->instances, sdi);
125 devices = g_slist_append(devices, sdi);
128 serial_close(serial);
133 static GSList *hw_scan(GSList *options)
135 struct drv_context *drvc;
136 struct sr_config *src;
138 const char *conn, *serialcomm;
144 drvc->instances = NULL;
146 conn = serialcomm = NULL;
147 for (l = options; l; l = l->next) {
153 case SR_CONF_SERIALCOMM:
154 serialcomm = src->value;
163 /* Use the provided comm specs. */
164 devices = brymen_scan(conn, serialcomm);
166 /* But 9600/8n1 should work all of the time. */
167 devices = brymen_scan(conn, "9600/8n1/dtr=1/rts=1");
173 static GSList *hw_dev_list(void)
175 return ((struct drv_context *)(di->priv))->instances;
178 static int hw_dev_open(struct sr_dev_inst *sdi)
180 struct dev_context *devc;
182 if (!(devc = sdi->priv)) {
183 sr_err("sdi->priv was NULL.");
187 if (serial_open(devc->serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
190 sdi->status = SR_ST_ACTIVE;
195 static int hw_dev_close(struct sr_dev_inst *sdi)
197 struct dev_context *devc;
199 if (!(devc = sdi->priv)) {
200 sr_err("sdi->priv was NULL.");
204 if (devc->serial && devc->serial->fd != -1) {
205 serial_close(devc->serial);
206 sdi->status = SR_ST_INACTIVE;
212 static int hw_cleanup(void)
219 static int config_list(int key, const void **data,
220 const struct sr_dev_inst *sdi)
225 case SR_CONF_SCAN_OPTIONS:
228 case SR_CONF_DEVICE_OPTIONS:
232 sr_err("Unknown config key: %d.", key);
239 static int hw_dev_config_set(int id, const void *value,
240 const struct sr_dev_inst *sdi)
242 struct dev_context *devc;
245 if (sdi->status != SR_ST_ACTIVE) {
246 sr_err("Device inactive, can't set config options.");
250 if (!(devc = sdi->priv)) {
251 sr_err("sdi->priv was NULL.");
257 case SR_CONF_LIMIT_SAMPLES:
258 devc->limit_samples = *(const uint64_t *)value;
260 case SR_CONF_LIMIT_MSEC:
261 devc->limit_msec = *(const uint64_t *)value;
264 sr_err("Unknown hardware capability: %d.", id);
271 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
274 struct sr_datafeed_packet packet;
275 struct sr_datafeed_header header;
276 struct dev_context *devc;
278 if (!(devc = sdi->priv)) {
279 sr_err("sdi->priv was NULL.");
283 sr_dbg("Starting acquisition.");
285 devc->cb_data = cb_data;
288 * Reset the number of samples to take. If we've already collected our
289 * quota, but we start a new session, and don't reset this, we'll just
290 * quit without acquiring any new samples.
292 devc->num_samples = 0;
293 devc->starttime = g_get_monotonic_time();
295 /* Send header packet to the session bus. */
296 sr_dbg("Sending SR_DF_HEADER.");
297 packet.type = SR_DF_HEADER;
298 packet.payload = &header;
299 header.feed_version = 1;
300 gettimeofday(&header.starttime, NULL);
301 sr_session_send(devc->cb_data, &packet);
303 /* Poll every 50ms, or whenever some data comes in. */
304 sr_source_add(devc->serial->fd, G_IO_IN, 50,
305 brymen_dmm_receive_data, (void *)sdi);
310 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
312 struct sr_datafeed_packet packet;
313 struct dev_context *devc;
315 if (sdi->status != SR_ST_ACTIVE) {
316 sr_err("Device inactive, can't stop acquisition.");
320 if (!(devc = sdi->priv)) {
321 sr_err("sdi->priv was NULL.");
325 sr_dbg("Stopping acquisition.");
327 sr_source_remove(devc->serial->fd);
328 hw_dev_close((struct sr_dev_inst *)sdi);
330 /* Send end packet to the session bus. */
331 sr_dbg("Sending SR_DF_END.");
332 packet.type = SR_DF_END;
333 sr_session_send(cb_data, &packet);
338 SR_PRIV struct sr_dev_driver brymen_bm857_driver_info = {
339 .name = "brymen-bm857",
340 .longname = "Brymen BM857",
343 .cleanup = hw_cleanup,
345 .dev_list = hw_dev_list,
346 .dev_clear = clear_instances,
347 .dev_open = hw_dev_open,
348 .dev_close = hw_dev_close,
349 .config_list = config_list,
350 .config_set = hw_dev_config_set,
351 .dev_acquisition_start = hw_dev_acquisition_start,
352 .dev_acquisition_stop = hw_dev_acquisition_stop,