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 SR_CONF_SPL_MEASUREMENT_RANGE,
43 static const char *weight_freq[] = {
48 static const char *weight_time[] = {
53 static const uint64_t meas_ranges[][2] = {
60 SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info;
61 static struct sr_dev_driver *di = &cem_dt_885x_driver_info;
64 static int init(struct sr_context *sr_ctx)
66 return std_init(sr_ctx, di, LOG_PREFIX);
69 static GSList *scan(GSList *options)
71 struct drv_context *drvc;
72 struct dev_context *devc;
73 struct sr_config *src;
74 struct sr_serial_dev_inst *serial;
75 struct sr_dev_inst *sdi;
76 struct sr_probe *probe;
83 for (l = options; l; l = l->next) {
85 if (src->key == SR_CONF_CONN)
86 conn = g_variant_get_string(src->data, NULL);
91 if (!(serial = sr_serial_dev_inst_new(conn, SERIALCOMM)))
94 if (serial_open(serial, SERIAL_RDONLY | SERIAL_NONBLOCK) != SR_OK)
99 start = g_get_monotonic_time();
100 while (g_get_monotonic_time() - start < MAX_SCAN_TIME) {
101 if (serial_read(serial, &c, 1) == 1 && c == 0xa5) {
103 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "CEM",
107 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
108 sr_dbg("Device context malloc failed.");
111 devc->cur_mqflags = 0;
112 devc->recording = -1;
113 devc->cur_meas_range = 0;
115 if (!(sdi->conn = sr_serial_dev_inst_new(conn, SERIALCOMM)))
118 sdi->inst_type = SR_INST_SERIAL;
121 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "SPL")))
123 sdi->probes = g_slist_append(sdi->probes, probe);
124 drvc->instances = g_slist_append(drvc->instances, sdi);
125 devices = g_slist_append(devices, sdi);
128 /* It takes about 1ms for a byte to come in. */
132 serial_close(serial);
137 static GSList *dev_list(void)
139 struct drv_context *drvc;
143 return drvc->instances;
146 static int dev_clear(void)
148 return std_dev_clear(di, NULL);
151 static int dev_open(struct sr_dev_inst *sdi)
153 struct sr_serial_dev_inst *serial;
156 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
159 sdi->status = SR_ST_ACTIVE;
164 static int dev_close(struct sr_dev_inst *sdi)
166 struct sr_serial_dev_inst *serial;
169 if (serial && serial->fd != -1) {
170 serial_close(serial);
171 sdi->status = SR_ST_INACTIVE;
177 static int cleanup(void)
182 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi)
184 struct dev_context *devc;
195 case SR_CONF_LIMIT_SAMPLES:
196 *data = g_variant_new_uint64(devc->limit_samples);
198 case SR_CONF_DATALOG:
199 if ((ret = cem_dt_885x_recording_get(sdi, &tmp)) == SR_OK)
200 *data = g_variant_new_boolean(tmp);
202 case SR_CONF_SPL_WEIGHT_FREQ:
203 tmp = cem_dt_885x_weight_freq_get(sdi);
204 if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_A)
205 *data = g_variant_new_string("A");
206 else if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_C)
207 *data = g_variant_new_string("C");
211 case SR_CONF_SPL_WEIGHT_TIME:
212 tmp = cem_dt_885x_weight_time_get(sdi);
213 if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_F)
214 *data = g_variant_new_string("F");
215 else if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_S)
216 *data = g_variant_new_string("S");
220 case SR_CONF_HOLD_MAX:
221 if ((ret = cem_dt_885x_holdmode_get(sdi, &tmp)) == SR_OK)
222 *data = g_variant_new_boolean(tmp == SR_MQFLAG_MAX);
224 case SR_CONF_HOLD_MIN:
225 if ((ret = cem_dt_885x_holdmode_get(sdi, &tmp)) == SR_OK)
226 *data = g_variant_new_boolean(tmp == SR_MQFLAG_MIN);
228 case SR_CONF_SPL_MEASUREMENT_RANGE:
229 if ((ret = cem_dt_885x_meas_range_get(sdi, &low, &high)) == SR_OK) {
230 range[0] = g_variant_new_uint64(low);
231 range[1] = g_variant_new_uint64(high);
232 *data = g_variant_new_tuple(range, 2);
242 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
244 struct dev_context *devc;
245 uint64_t tmp_u64, low, high;
250 if (sdi->status != SR_ST_ACTIVE)
251 return SR_ERR_DEV_CLOSED;
253 if (!(devc = sdi->priv)) {
254 sr_err("sdi->priv was NULL.");
260 case SR_CONF_LIMIT_SAMPLES:
261 tmp_u64 = g_variant_get_uint64(data);
262 devc->limit_samples = tmp_u64;
265 case SR_CONF_DATALOG:
266 ret = cem_dt_885x_recording_set(sdi, g_variant_get_boolean(data));
268 case SR_CONF_SPL_WEIGHT_FREQ:
269 tmp_str = g_variant_get_string(data, NULL);
270 if (!strcmp(tmp_str, "A"))
271 ret = cem_dt_885x_weight_freq_set(sdi,
272 SR_MQFLAG_SPL_FREQ_WEIGHT_A);
273 else if (!strcmp(tmp_str, "C"))
274 ret = cem_dt_885x_weight_freq_set(sdi,
275 SR_MQFLAG_SPL_FREQ_WEIGHT_C);
279 case SR_CONF_SPL_WEIGHT_TIME:
280 tmp_str = g_variant_get_string(data, NULL);
281 if (!strcmp(tmp_str, "F"))
282 ret = cem_dt_885x_weight_time_set(sdi,
283 SR_MQFLAG_SPL_TIME_WEIGHT_F);
284 else if (!strcmp(tmp_str, "S"))
285 ret = cem_dt_885x_weight_time_set(sdi,
286 SR_MQFLAG_SPL_TIME_WEIGHT_S);
290 case SR_CONF_HOLD_MAX:
291 tmp = g_variant_get_boolean(data) ? SR_MQFLAG_MAX : 0;
292 ret = cem_dt_885x_holdmode_set(sdi, tmp);
294 case SR_CONF_HOLD_MIN:
295 tmp = g_variant_get_boolean(data) ? SR_MQFLAG_MIN : 0;
296 ret = cem_dt_885x_holdmode_set(sdi, tmp);
298 case SR_CONF_SPL_MEASUREMENT_RANGE:
299 g_variant_get(data, "(tt)", &low, &high);
301 for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) {
302 if (meas_ranges[i][0] == low && meas_ranges[i][1] == high) {
303 ret = cem_dt_885x_meas_range_set(sdi, low, high);
315 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
317 GVariant *tuple, *range[2];
326 case SR_CONF_SCAN_OPTIONS:
327 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
328 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
330 case SR_CONF_DEVICE_OPTIONS:
331 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
332 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
334 case SR_CONF_SPL_WEIGHT_FREQ:
335 *data = g_variant_new_strv(weight_freq, ARRAY_SIZE(weight_freq));
337 case SR_CONF_SPL_WEIGHT_TIME:
338 *data = g_variant_new_strv(weight_time, ARRAY_SIZE(weight_time));
340 case SR_CONF_SPL_MEASUREMENT_RANGE:
341 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
342 for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) {
343 range[0] = g_variant_new_uint64(meas_ranges[i][0]);
344 range[1] = g_variant_new_uint64(meas_ranges[i][1]);
345 tuple = g_variant_new_tuple(range, 2);
346 g_variant_builder_add_value(&gvb, tuple);
348 *data = g_variant_builder_end(&gvb);
357 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
359 struct dev_context *devc;
360 struct sr_serial_dev_inst *serial;
362 if (sdi->status != SR_ST_ACTIVE)
363 return SR_ERR_DEV_CLOSED;
365 if (!(devc = sdi->priv)) {
366 sr_err("sdi->priv was NULL.");
370 devc->cb_data = cb_data;
371 devc->state = ST_INIT;
372 devc->num_samples = 0;
375 /* Send header packet to the session bus. */
376 std_session_send_df_header(cb_data, LOG_PREFIX);
378 /* Poll every 100ms, or whenever some data comes in. */
380 sr_source_add(serial->fd, G_IO_IN, 150, cem_dt_885x_receive_data,
386 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
390 if (sdi->status != SR_ST_ACTIVE)
391 return SR_ERR_DEV_CLOSED;
393 return std_dev_acquisition_stop_serial(sdi, cb_data, dev_close,
394 sdi->conn, LOG_PREFIX);
399 SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info = {
400 .name = "cem-dt-885x",
401 .longname = "CEM DT-885x",
406 .dev_list = dev_list,
407 .dev_clear = dev_clear,
408 .config_get = config_get,
409 .config_set = config_set,
410 .config_list = config_list,
411 .dev_open = dev_open,
412 .dev_close = dev_close,
413 .dev_acquisition_start = dev_acquisition_start,
414 .dev_acquisition_stop = dev_acquisition_stop,