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/>.
23 #define SERIALCOMM "9600/8n1"
24 /* 23ms is the longest interval between tokens. */
25 #define MAX_SCAN_TIME 25 * 1000
27 static const int32_t hwopts[] = {
31 static const int32_t hwcaps[] = {
32 SR_CONF_SOUNDLEVELMETER,
33 SR_CONF_LIMIT_SAMPLES,
36 SR_CONF_SPL_WEIGHT_FREQ,
39 static const char *weight_freq[] = {
44 SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info;
45 static struct sr_dev_driver *di = &cem_dt_885x_driver_info;
48 static int init(struct sr_context *sr_ctx)
50 return std_init(sr_ctx, di, LOG_PREFIX);
53 static GSList *scan(GSList *options)
55 struct drv_context *drvc;
56 struct dev_context *devc;
57 struct sr_config *src;
58 struct sr_serial_dev_inst *serial;
59 struct sr_dev_inst *sdi;
60 struct sr_probe *probe;
67 for (l = options; l; l = l->next) {
69 if (src->key == SR_CONF_CONN)
70 conn = g_variant_get_string(src->data, NULL);
75 if (!(serial = sr_serial_dev_inst_new(conn, SERIALCOMM)))
78 if (serial_open(serial, SERIAL_RDONLY | SERIAL_NONBLOCK) != SR_OK)
83 start = g_get_monotonic_time();
84 while (g_get_monotonic_time() - start < MAX_SCAN_TIME) {
85 if (serial_read(serial, &c, 1) == 1 && c == 0xa5) {
87 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "CEM",
91 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
92 sr_dbg("Device context malloc failed.");
95 devc->cur_mqflags = 0;
98 if (!(sdi->conn = sr_serial_dev_inst_new(conn, SERIALCOMM)))
101 sdi->inst_type = SR_INST_SERIAL;
104 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "SPL")))
106 sdi->probes = g_slist_append(sdi->probes, probe);
107 drvc->instances = g_slist_append(drvc->instances, sdi);
108 devices = g_slist_append(devices, sdi);
111 /* It takes about 1ms for a byte to come in. */
115 serial_close(serial);
120 static GSList *dev_list(void)
122 struct drv_context *drvc;
126 return drvc->instances;
129 static int dev_clear(void)
131 return std_dev_clear(di, NULL);
134 static int dev_open(struct sr_dev_inst *sdi)
136 struct sr_serial_dev_inst *serial;
139 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
142 sdi->status = SR_ST_ACTIVE;
147 static int dev_close(struct sr_dev_inst *sdi)
149 struct sr_serial_dev_inst *serial;
152 if (serial && serial->fd != -1) {
153 serial_close(serial);
154 sdi->status = SR_ST_INACTIVE;
160 static int cleanup(void)
165 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi)
167 struct dev_context *devc;
175 case SR_CONF_LIMIT_SAMPLES:
176 *data = g_variant_new_uint64(devc->limit_samples);
178 case SR_CONF_DATALOG:
179 *data = g_variant_new_boolean(cem_dt_885x_recording_get(sdi));
181 case SR_CONF_SPL_WEIGHT_FREQ:
182 tmp = cem_dt_885x_weight_freq_get(sdi);
183 if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_A)
184 *data = g_variant_new_string("A");
185 else if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_C)
186 *data = g_variant_new_string("C");
197 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
199 struct dev_context *devc;
204 if (sdi->status != SR_ST_ACTIVE)
205 return SR_ERR_DEV_CLOSED;
207 if (!(devc = sdi->priv)) {
208 sr_err("sdi->priv was NULL.");
214 case SR_CONF_LIMIT_SAMPLES:
215 tmp_u64 = g_variant_get_uint64(data);
216 devc->limit_samples = tmp_u64;
219 case SR_CONF_DATALOG:
220 if (g_variant_get_boolean(data)) {
222 ret = cem_dt_885x_recording_set(sdi, TRUE);
225 ret = cem_dt_885x_recording_set(sdi, FALSE);
228 case SR_CONF_SPL_WEIGHT_FREQ:
229 tmp_str = g_variant_get_string(data, NULL);
230 if (!strcmp(tmp_str, "A"))
231 ret = cem_dt_885x_weight_freq_set(sdi,
232 SR_MQFLAG_SPL_FREQ_WEIGHT_A);
233 else if (!strcmp(tmp_str, "C"))
234 ret = cem_dt_885x_weight_freq_set(sdi,
235 SR_MQFLAG_SPL_FREQ_WEIGHT_C);
246 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
254 case SR_CONF_SCAN_OPTIONS:
255 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
256 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
258 case SR_CONF_DEVICE_OPTIONS:
259 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
260 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
262 case SR_CONF_SPL_WEIGHT_FREQ:
263 *data = g_variant_new_strv(weight_freq, ARRAY_SIZE(weight_freq));
272 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
274 struct dev_context *devc;
275 struct sr_serial_dev_inst *serial;
277 if (sdi->status != SR_ST_ACTIVE)
278 return SR_ERR_DEV_CLOSED;
280 if (!(devc = sdi->priv)) {
281 sr_err("sdi->priv was NULL.");
285 devc->cb_data = cb_data;
286 devc->state = ST_INIT;
287 devc->num_samples = 0;
290 /* Send header packet to the session bus. */
291 std_session_send_df_header(cb_data, LOG_PREFIX);
293 /* Poll every 100ms, or whenever some data comes in. */
295 sr_source_add(serial->fd, G_IO_IN, 150, cem_dt_885x_receive_data,
301 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
305 if (sdi->status != SR_ST_ACTIVE)
306 return SR_ERR_DEV_CLOSED;
308 return std_dev_acquisition_stop_serial(sdi, cb_data, dev_close,
309 sdi->conn, LOG_PREFIX);
314 SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info = {
315 .name = "cem-dt-885x",
316 .longname = "CEM DT-885x",
321 .dev_list = dev_list,
322 .dev_clear = dev_clear,
323 .config_get = config_get,
324 .config_set = config_set,
325 .config_list = config_list,
326 .dev_open = dev_open,
327 .dev_close = dev_close,
328 .dev_acquisition_start = dev_acquisition_start,
329 .dev_acquisition_stop = dev_acquisition_stop,