2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.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 #define SERIALCOMM "9600/8n1"
23 /* 23ms is the longest interval between tokens. */
24 #define MAX_SCAN_TIME 25 * 1000
26 static const int32_t hwopts[] = {
30 static const int32_t hwcaps[] = {
31 SR_CONF_SOUNDLEVELMETER,
32 SR_CONF_LIMIT_SAMPLES,
37 SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info;
38 static struct sr_dev_driver *di = &cem_dt_885x_driver_info;
41 static int init(struct sr_context *sr_ctx)
43 return std_init(sr_ctx, di, LOG_PREFIX);
46 static GSList *scan(GSList *options)
48 struct drv_context *drvc;
49 struct dev_context *devc;
50 struct sr_config *src;
51 struct sr_serial_dev_inst *serial;
52 struct sr_dev_inst *sdi;
53 struct sr_probe *probe;
60 for (l = options; l; l = l->next) {
62 if (src->key == SR_CONF_CONN)
63 conn = g_variant_get_string(src->data, NULL);
68 if (!(serial = sr_serial_dev_inst_new(conn, SERIALCOMM)))
71 if (serial_open(serial, SERIAL_RDONLY | SERIAL_NONBLOCK) != SR_OK)
76 start = g_get_monotonic_time();
77 while (g_get_monotonic_time() - start < MAX_SCAN_TIME) {
78 if (serial_read(serial, &c, 1) == 1 && c == 0xa5) {
80 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "CEM",
84 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
85 sr_dbg("Device context malloc failed.");
89 if (!(sdi->conn = sr_serial_dev_inst_new(conn, SERIALCOMM)))
92 sdi->inst_type = SR_INST_SERIAL;
95 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
97 sdi->probes = g_slist_append(sdi->probes, probe);
98 drvc->instances = g_slist_append(drvc->instances, sdi);
99 devices = g_slist_append(devices, sdi);
102 /* It takes about 1ms for a byte to come in. */
106 serial_close(serial);
111 static GSList *dev_list(void)
113 struct drv_context *drvc;
117 return drvc->instances;
120 static int dev_clear(void)
122 return std_dev_clear(di, NULL);
125 static int dev_open(struct sr_dev_inst *sdi)
127 struct sr_serial_dev_inst *serial;
130 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
133 sdi->status = SR_ST_ACTIVE;
138 static int dev_close(struct sr_dev_inst *sdi)
140 struct sr_serial_dev_inst *serial;
143 if (serial && serial->fd != -1) {
144 serial_close(serial);
145 sdi->status = SR_ST_INACTIVE;
151 static int cleanup(void)
156 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi)
158 struct dev_context *devc;
165 case SR_CONF_LIMIT_SAMPLES:
166 *data = g_variant_new_uint64(devc->limit_samples);
175 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
177 struct dev_context *devc;
181 if (sdi->status != SR_ST_ACTIVE)
182 return SR_ERR_DEV_CLOSED;
184 if (!(devc = sdi->priv)) {
185 sr_err("sdi->priv was NULL.");
191 case SR_CONF_LIMIT_SAMPLES:
192 tmp_u64 = g_variant_get_uint64(data);
193 devc->limit_samples = tmp_u64;
203 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
211 case SR_CONF_SCAN_OPTIONS:
212 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
213 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
215 case SR_CONF_DEVICE_OPTIONS:
216 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
217 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
226 static int dev_acquisition_start(const struct sr_dev_inst *sdi,
232 if (sdi->status != SR_ST_ACTIVE)
233 return SR_ERR_DEV_CLOSED;
235 /* TODO: configure hardware, reset acquisition state, set up
236 * callbacks and send header packet. */
241 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
245 if (sdi->status != SR_ST_ACTIVE)
246 return SR_ERR_DEV_CLOSED;
248 /* TODO: stop acquisition. */
253 SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info = {
254 .name = "cem-dt-885x",
255 .longname = "CEM DT-885x",
260 .dev_list = dev_list,
261 .dev_clear = dev_clear,
262 .config_get = config_get,
263 .config_set = config_set,
264 .config_list = config_list,
265 .dev_open = dev_open,
266 .dev_close = dev_close,
267 .dev_acquisition_start = dev_acquisition_start,
268 .dev_acquisition_stop = dev_acquisition_stop,