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,
37 SR_CONF_SPL_WEIGHT_TIME,
40 static const char *weight_freq[] = {
45 static const char *weight_time[] = {
50 SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info;
51 static struct sr_dev_driver *di = &cem_dt_885x_driver_info;
54 static int init(struct sr_context *sr_ctx)
56 return std_init(sr_ctx, di, LOG_PREFIX);
59 static GSList *scan(GSList *options)
61 struct drv_context *drvc;
62 struct dev_context *devc;
63 struct sr_config *src;
64 struct sr_serial_dev_inst *serial;
65 struct sr_dev_inst *sdi;
66 struct sr_probe *probe;
73 for (l = options; l; l = l->next) {
75 if (src->key == SR_CONF_CONN)
76 conn = g_variant_get_string(src->data, NULL);
81 if (!(serial = sr_serial_dev_inst_new(conn, SERIALCOMM)))
84 if (serial_open(serial, SERIAL_RDONLY | SERIAL_NONBLOCK) != SR_OK)
89 start = g_get_monotonic_time();
90 while (g_get_monotonic_time() - start < MAX_SCAN_TIME) {
91 if (serial_read(serial, &c, 1) == 1 && c == 0xa5) {
93 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "CEM",
97 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
98 sr_dbg("Device context malloc failed.");
101 devc->cur_mqflags = 0;
102 devc->recording = -1;
104 if (!(sdi->conn = sr_serial_dev_inst_new(conn, SERIALCOMM)))
107 sdi->inst_type = SR_INST_SERIAL;
110 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "SPL")))
112 sdi->probes = g_slist_append(sdi->probes, probe);
113 drvc->instances = g_slist_append(drvc->instances, sdi);
114 devices = g_slist_append(devices, sdi);
117 /* It takes about 1ms for a byte to come in. */
121 serial_close(serial);
126 static GSList *dev_list(void)
128 struct drv_context *drvc;
132 return drvc->instances;
135 static int dev_clear(void)
137 return std_dev_clear(di, NULL);
140 static int dev_open(struct sr_dev_inst *sdi)
142 struct sr_serial_dev_inst *serial;
145 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
148 sdi->status = SR_ST_ACTIVE;
153 static int dev_close(struct sr_dev_inst *sdi)
155 struct sr_serial_dev_inst *serial;
158 if (serial && serial->fd != -1) {
159 serial_close(serial);
160 sdi->status = SR_ST_INACTIVE;
166 static int cleanup(void)
171 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi)
173 struct dev_context *devc;
181 case SR_CONF_LIMIT_SAMPLES:
182 *data = g_variant_new_uint64(devc->limit_samples);
184 case SR_CONF_DATALOG:
185 *data = g_variant_new_boolean(cem_dt_885x_recording_get(sdi));
187 case SR_CONF_SPL_WEIGHT_FREQ:
188 tmp = cem_dt_885x_weight_freq_get(sdi);
189 if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_A)
190 *data = g_variant_new_string("A");
191 else if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_C)
192 *data = g_variant_new_string("C");
196 case SR_CONF_SPL_WEIGHT_TIME:
197 tmp = cem_dt_885x_weight_time_get(sdi);
198 if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_F)
199 *data = g_variant_new_string("F");
200 else if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_S)
201 *data = g_variant_new_string("S");
212 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
214 struct dev_context *devc;
219 if (sdi->status != SR_ST_ACTIVE)
220 return SR_ERR_DEV_CLOSED;
222 if (!(devc = sdi->priv)) {
223 sr_err("sdi->priv was NULL.");
229 case SR_CONF_LIMIT_SAMPLES:
230 tmp_u64 = g_variant_get_uint64(data);
231 devc->limit_samples = tmp_u64;
234 case SR_CONF_DATALOG:
235 if (g_variant_get_boolean(data)) {
237 ret = cem_dt_885x_recording_set(sdi, TRUE);
240 ret = cem_dt_885x_recording_set(sdi, FALSE);
243 case SR_CONF_SPL_WEIGHT_FREQ:
244 tmp_str = g_variant_get_string(data, NULL);
245 if (!strcmp(tmp_str, "A"))
246 ret = cem_dt_885x_weight_freq_set(sdi,
247 SR_MQFLAG_SPL_FREQ_WEIGHT_A);
248 else if (!strcmp(tmp_str, "C"))
249 ret = cem_dt_885x_weight_freq_set(sdi,
250 SR_MQFLAG_SPL_FREQ_WEIGHT_C);
254 case SR_CONF_SPL_WEIGHT_TIME:
255 tmp_str = g_variant_get_string(data, NULL);
256 if (!strcmp(tmp_str, "F"))
257 ret = cem_dt_885x_weight_time_set(sdi,
258 SR_MQFLAG_SPL_TIME_WEIGHT_F);
259 else if (!strcmp(tmp_str, "S"))
260 ret = cem_dt_885x_weight_time_set(sdi,
261 SR_MQFLAG_SPL_TIME_WEIGHT_S);
272 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
280 case SR_CONF_SCAN_OPTIONS:
281 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
282 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
284 case SR_CONF_DEVICE_OPTIONS:
285 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
286 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
288 case SR_CONF_SPL_WEIGHT_FREQ:
289 *data = g_variant_new_strv(weight_freq, ARRAY_SIZE(weight_freq));
291 case SR_CONF_SPL_WEIGHT_TIME:
292 *data = g_variant_new_strv(weight_time, ARRAY_SIZE(weight_time));
301 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
303 struct dev_context *devc;
304 struct sr_serial_dev_inst *serial;
306 if (sdi->status != SR_ST_ACTIVE)
307 return SR_ERR_DEV_CLOSED;
309 if (!(devc = sdi->priv)) {
310 sr_err("sdi->priv was NULL.");
314 devc->cb_data = cb_data;
315 devc->state = ST_INIT;
316 devc->num_samples = 0;
319 /* Send header packet to the session bus. */
320 std_session_send_df_header(cb_data, LOG_PREFIX);
322 /* Poll every 100ms, or whenever some data comes in. */
324 sr_source_add(serial->fd, G_IO_IN, 150, cem_dt_885x_receive_data,
330 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
334 if (sdi->status != SR_ST_ACTIVE)
335 return SR_ERR_DEV_CLOSED;
337 return std_dev_acquisition_stop_serial(sdi, cb_data, dev_close,
338 sdi->conn, LOG_PREFIX);
343 SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info = {
344 .name = "cem-dt-885x",
345 .longname = "CEM DT-885x",
350 .dev_list = dev_list,
351 .dev_clear = dev_clear,
352 .config_get = config_get,
353 .config_set = config_set,
354 .config_list = config_list,
355 .dev_open = dev_open,
356 .dev_close = dev_close,
357 .dev_acquisition_start = dev_acquisition_start,
358 .dev_acquisition_stop = dev_acquisition_stop,