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 int32_t hwopts[] = {
27 static const int32_t hwcaps[] = {
29 SR_CONF_LIMIT_SAMPLES,
34 SR_PRIV struct sr_dev_driver brymen_bm857_driver_info;
35 static struct sr_dev_driver *di = &brymen_bm857_driver_info;
37 static int hw_init(struct sr_context *sr_ctx)
39 return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN);
42 static void free_instance(void *inst)
44 struct sr_dev_inst *sdi;
45 struct dev_context *devc;
49 if (!(devc = sdi->priv))
51 sr_serial_dev_inst_free(devc->serial);
52 sr_dev_inst_free(sdi);
55 /* Properly close and free all devices. */
56 static int clear_instances(void)
58 struct drv_context *drvc;
60 if (!(drvc = di->priv))
63 g_slist_free_full(drvc->instances, free_instance);
64 drvc->instances = NULL;
69 static GSList *brymen_scan(const char *conn, const char *serialcomm)
71 struct sr_dev_inst *sdi;
72 struct dev_context *devc;
73 struct drv_context *drvc;
74 struct sr_probe *probe;
75 struct sr_serial_dev_inst *serial;
81 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
84 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
87 sr_info("Probing port %s.", conn);
92 if ((ret = brymen_packet_request(serial)) < 0) {
93 sr_err("Unable to send command: %d.", ret);
98 ret = brymen_stream_detect(serial, buf, &len, brymen_packet_length,
99 brymen_packet_is_valid, 1000, 9600);
103 sr_info("Found device on port %s.", conn);
105 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Brymen", "BM85x", "")))
108 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
109 sr_err("Device context malloc failed.");
113 devc->serial = serial;
118 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
121 sdi->probes = g_slist_append(sdi->probes, probe);
122 drvc->instances = g_slist_append(drvc->instances, sdi);
123 devices = g_slist_append(devices, sdi);
126 serial_close(serial);
131 static GSList *hw_scan(GSList *options)
133 struct drv_context *drvc;
134 struct sr_config *src;
136 const char *conn, *serialcomm;
142 drvc->instances = NULL;
144 conn = serialcomm = NULL;
145 for (l = options; l; l = l->next) {
149 conn = g_variant_get_string(src->data, NULL);
151 case SR_CONF_SERIALCOMM:
152 serialcomm = g_variant_get_string(src->data, NULL);
161 /* Use the provided comm specs. */
162 devices = brymen_scan(conn, serialcomm);
164 /* But 9600/8n1 should work all of the time. */
165 devices = brymen_scan(conn, "9600/8n1/dtr=1/rts=1");
171 static GSList *hw_dev_list(void)
173 return ((struct drv_context *)(di->priv))->instances;
176 static int hw_dev_open(struct sr_dev_inst *sdi)
178 struct dev_context *devc;
180 if (!(devc = sdi->priv)) {
181 sr_err("sdi->priv was NULL.");
185 if (serial_open(devc->serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
188 sdi->status = SR_ST_ACTIVE;
193 static int hw_dev_close(struct sr_dev_inst *sdi)
195 struct dev_context *devc;
199 if (devc->serial && devc->serial->fd != -1) {
200 serial_close(devc->serial);
201 sdi->status = SR_ST_INACTIVE;
207 static int hw_cleanup(void)
214 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
216 struct dev_context *devc;
219 if (sdi->status != SR_ST_ACTIVE) {
220 sr_err("Device inactive, can't set config options.");
224 if (!(devc = sdi->priv)) {
225 sr_err("sdi->priv was NULL.");
231 case SR_CONF_LIMIT_SAMPLES:
232 devc->limit_samples = g_variant_get_uint64(data);
234 case SR_CONF_LIMIT_MSEC:
235 devc->limit_msec = g_variant_get_uint64(data);
244 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
249 case SR_CONF_SCAN_OPTIONS:
250 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
251 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
253 case SR_CONF_DEVICE_OPTIONS:
254 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
255 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
264 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
267 struct dev_context *devc;
269 if (!(devc = sdi->priv)) {
270 sr_err("sdi->priv was NULL.");
274 devc->cb_data = cb_data;
277 * Reset the number of samples to take. If we've already collected our
278 * quota, but we start a new session, and don't reset this, we'll just
279 * quit without acquiring any new samples.
281 devc->num_samples = 0;
282 devc->starttime = g_get_monotonic_time();
284 /* Send header packet to the session bus. */
285 std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
287 /* Poll every 50ms, or whenever some data comes in. */
288 sr_source_add(devc->serial->fd, G_IO_IN, 50,
289 brymen_dmm_receive_data, (void *)sdi);
294 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
296 return std_hw_dev_acquisition_stop_serial(sdi, cb_data, hw_dev_close,
297 ((struct dev_context *)(sdi->priv))->serial, DRIVER_LOG_DOMAIN);
300 SR_PRIV struct sr_dev_driver brymen_bm857_driver_info = {
301 .name = "brymen-bm857",
302 .longname = "Brymen BM857",
305 .cleanup = hw_cleanup,
307 .dev_list = hw_dev_list,
308 .dev_clear = clear_instances,
310 .config_set = config_set,
311 .config_list = config_list,
312 .dev_open = hw_dev_open,
313 .dev_close = hw_dev_close,
314 .dev_acquisition_start = hw_dev_acquisition_start,
315 .dev_acquisition_stop = hw_dev_acquisition_stop,