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,
38 SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info;
39 static struct sr_dev_driver *di = &cem_dt_885x_driver_info;
42 static int init(struct sr_context *sr_ctx)
44 return std_init(sr_ctx, di, LOG_PREFIX);
47 static GSList *scan(GSList *options)
49 struct drv_context *drvc;
50 struct dev_context *devc;
51 struct sr_config *src;
52 struct sr_serial_dev_inst *serial;
53 struct sr_dev_inst *sdi;
54 struct sr_probe *probe;
61 for (l = options; l; l = l->next) {
63 if (src->key == SR_CONF_CONN)
64 conn = g_variant_get_string(src->data, NULL);
69 if (!(serial = sr_serial_dev_inst_new(conn, SERIALCOMM)))
72 if (serial_open(serial, SERIAL_RDONLY | SERIAL_NONBLOCK) != SR_OK)
77 start = g_get_monotonic_time();
78 while (g_get_monotonic_time() - start < MAX_SCAN_TIME) {
79 if (serial_read(serial, &c, 1) == 1 && c == 0xa5) {
81 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "CEM",
85 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
86 sr_dbg("Device context malloc failed.");
89 devc->cur_mqflags = 0;
92 if (!(sdi->conn = sr_serial_dev_inst_new(conn, SERIALCOMM)))
95 sdi->inst_type = SR_INST_SERIAL;
98 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "SPL")))
100 sdi->probes = g_slist_append(sdi->probes, probe);
101 drvc->instances = g_slist_append(drvc->instances, sdi);
102 devices = g_slist_append(devices, sdi);
105 /* It takes about 1ms for a byte to come in. */
109 serial_close(serial);
114 static GSList *dev_list(void)
116 struct drv_context *drvc;
120 return drvc->instances;
123 static int dev_clear(void)
125 return std_dev_clear(di, NULL);
128 static int dev_open(struct sr_dev_inst *sdi)
130 struct sr_serial_dev_inst *serial;
133 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
136 sdi->status = SR_ST_ACTIVE;
141 static int dev_close(struct sr_dev_inst *sdi)
143 struct sr_serial_dev_inst *serial;
146 if (serial && serial->fd != -1) {
147 serial_close(serial);
148 sdi->status = SR_ST_INACTIVE;
154 static int cleanup(void)
159 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi)
161 struct dev_context *devc;
168 case SR_CONF_LIMIT_SAMPLES:
169 *data = g_variant_new_uint64(devc->limit_samples);
171 case SR_CONF_DATALOG:
172 *data = g_variant_new_boolean(cem_dt_885x_recording_get(sdi));
181 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
183 struct dev_context *devc;
187 if (sdi->status != SR_ST_ACTIVE)
188 return SR_ERR_DEV_CLOSED;
190 if (!(devc = sdi->priv)) {
191 sr_err("sdi->priv was NULL.");
197 case SR_CONF_LIMIT_SAMPLES:
198 tmp_u64 = g_variant_get_uint64(data);
199 devc->limit_samples = tmp_u64;
202 case SR_CONF_DATALOG:
203 if (g_variant_get_boolean(data)) {
205 ret = cem_dt_885x_recording_set(sdi, TRUE);
208 ret = cem_dt_885x_recording_set(sdi, FALSE);
218 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
226 case SR_CONF_SCAN_OPTIONS:
227 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
228 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
230 case SR_CONF_DEVICE_OPTIONS:
231 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
232 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
241 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
243 struct dev_context *devc;
244 struct sr_serial_dev_inst *serial;
246 if (sdi->status != SR_ST_ACTIVE)
247 return SR_ERR_DEV_CLOSED;
249 if (!(devc = sdi->priv)) {
250 sr_err("sdi->priv was NULL.");
254 devc->cb_data = cb_data;
255 devc->state = ST_INIT;
256 devc->num_samples = 0;
259 /* Send header packet to the session bus. */
260 std_session_send_df_header(cb_data, LOG_PREFIX);
262 /* Poll every 100ms, or whenever some data comes in. */
264 sr_source_add(serial->fd, G_IO_IN, 150, cem_dt_885x_receive_data,
270 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
274 if (sdi->status != SR_ST_ACTIVE)
275 return SR_ERR_DEV_CLOSED;
277 return std_dev_acquisition_stop_serial(sdi, cb_data, dev_close,
278 sdi->conn, LOG_PREFIX);
283 SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info = {
284 .name = "cem-dt-885x",
285 .longname = "CEM DT-885x",
290 .dev_list = dev_list,
291 .dev_clear = dev_clear,
292 .config_get = config_get,
293 .config_set = config_set,
294 .config_list = config_list,
295 .dev_open = dev_open,
296 .dev_close = dev_close,
297 .dev_acquisition_start = dev_acquisition_start,
298 .dev_acquisition_stop = dev_acquisition_stop,