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, "SPL")))
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, void *cb_data)
228 struct dev_context *devc;
229 struct sr_serial_dev_inst *serial;
231 if (sdi->status != SR_ST_ACTIVE)
232 return SR_ERR_DEV_CLOSED;
234 if (!(devc = sdi->priv)) {
235 sr_err("sdi->priv was NULL.");
239 devc->cb_data = cb_data;
240 devc->state = ST_INIT;
241 devc->num_samples = 0;
244 /* Send header packet to the session bus. */
245 std_session_send_df_header(cb_data, LOG_PREFIX);
247 /* Poll every 100ms, or whenever some data comes in. */
249 sr_source_add(serial->fd, G_IO_IN, 150, cem_dt_885x_receive_data,
255 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
259 if (sdi->status != SR_ST_ACTIVE)
260 return SR_ERR_DEV_CLOSED;
262 return std_dev_acquisition_stop_serial(sdi, cb_data, dev_close,
263 sdi->conn, LOG_PREFIX);
268 SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info = {
269 .name = "cem-dt-885x",
270 .longname = "CEM DT-885x",
275 .dev_list = dev_list,
276 .dev_clear = dev_clear,
277 .config_get = config_get,
278 .config_set = config_set,
279 .config_list = config_list,
280 .dev_open = dev_open,
281 .dev_close = dev_close,
282 .dev_acquisition_start = dev_acquisition_start,
283 .dev_acquisition_stop = dev_acquisition_stop,