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/>.
24 #define SERIALCOMM "9600/8n1"
26 /* 23ms is the longest interval between tokens. */
27 #define MAX_SCAN_TIME_US (25 * 1000)
29 static const uint32_t scanopts[] = {
33 static const uint32_t drvopts[] = {
34 SR_CONF_SOUNDLEVELMETER,
37 static const uint32_t devopts[] = {
38 SR_CONF_CONTINUOUS | SR_CONF_SET,
39 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
40 SR_CONF_SPL_WEIGHT_FREQ | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
41 SR_CONF_SPL_WEIGHT_TIME | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
42 SR_CONF_SPL_MEASUREMENT_RANGE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
43 SR_CONF_DATALOG | SR_CONF_GET | SR_CONF_SET,
44 SR_CONF_HOLD_MAX | SR_CONF_GET | SR_CONF_SET,
45 SR_CONF_HOLD_MIN | SR_CONF_GET | SR_CONF_SET,
46 SR_CONF_POWER_OFF | SR_CONF_GET | SR_CONF_SET,
47 SR_CONF_DATA_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
50 static const char *weight_freq[] = {
55 static const char *weight_time[] = {
60 static const uint64_t meas_ranges[][2] = {
67 static const char *data_sources[] = {
72 SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info;
74 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
76 return std_init(sr_ctx, di, LOG_PREFIX);
79 static GSList *scan(struct sr_dev_driver *di, GSList *options)
81 struct drv_context *drvc;
82 struct dev_context *devc;
83 struct sr_config *src;
84 struct sr_serial_dev_inst *serial;
85 struct sr_dev_inst *sdi;
92 for (l = options; l; l = l->next) {
94 if (src->key == SR_CONF_CONN)
95 conn = g_variant_get_string(src->data, NULL);
100 serial = sr_serial_dev_inst_new(conn, SERIALCOMM);
102 if (serial_open(serial, SERIAL_RDONLY) != SR_OK)
107 start = g_get_monotonic_time();
108 while (g_get_monotonic_time() - start < MAX_SCAN_TIME_US) {
109 if (serial_read_nonblocking(serial, &c, 1) == 1 && c == 0xa5) {
111 sdi = g_malloc0(sizeof(struct sr_dev_inst));
112 sdi->status = SR_ST_INACTIVE;
113 sdi->vendor = g_strdup("CEM");
114 sdi->model = g_strdup("DT-885x");
115 devc = g_malloc0(sizeof(struct dev_context));
116 devc->cur_mqflags = 0;
117 devc->recording = -1;
118 devc->cur_meas_range = 0;
119 devc->cur_data_source = DATA_SOURCE_LIVE;
120 devc->enable_data_source_memory = FALSE;
121 sdi->conn = sr_serial_dev_inst_new(conn, SERIALCOMM);
122 sdi->inst_type = SR_INST_SERIAL;
125 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "SPL");
126 drvc->instances = g_slist_append(drvc->instances, sdi);
127 devices = g_slist_append(devices, sdi);
130 /* It takes about 1ms for a byte to come in. */
134 serial_close(serial);
139 static GSList *dev_list(const struct sr_dev_driver *di)
141 return ((struct drv_context *)(di->context))->instances;
144 static int dev_open(struct sr_dev_inst *sdi)
146 struct sr_serial_dev_inst *serial;
149 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
152 sdi->status = SR_ST_ACTIVE;
157 static int cleanup(const struct sr_dev_driver *di)
159 return std_dev_clear(di, NULL);
162 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
163 const struct sr_channel_group *cg)
165 struct dev_context *devc;
178 case SR_CONF_LIMIT_SAMPLES:
179 *data = g_variant_new_uint64(devc->limit_samples);
181 case SR_CONF_DATALOG:
182 if ((ret = cem_dt_885x_recording_get(sdi, &tmp)) == SR_OK)
183 *data = g_variant_new_boolean(tmp);
185 case SR_CONF_SPL_WEIGHT_FREQ:
186 tmp = cem_dt_885x_weight_freq_get(sdi);
187 if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_A)
188 *data = g_variant_new_string("A");
189 else if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_C)
190 *data = g_variant_new_string("C");
194 case SR_CONF_SPL_WEIGHT_TIME:
195 tmp = cem_dt_885x_weight_time_get(sdi);
196 if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_F)
197 *data = g_variant_new_string("F");
198 else if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_S)
199 *data = g_variant_new_string("S");
203 case SR_CONF_HOLD_MAX:
204 if ((ret = cem_dt_885x_holdmode_get(sdi, &tmp)) == SR_OK)
205 *data = g_variant_new_boolean(tmp == SR_MQFLAG_MAX);
207 case SR_CONF_HOLD_MIN:
208 if ((ret = cem_dt_885x_holdmode_get(sdi, &tmp)) == SR_OK)
209 *data = g_variant_new_boolean(tmp == SR_MQFLAG_MIN);
211 case SR_CONF_SPL_MEASUREMENT_RANGE:
212 if ((ret = cem_dt_885x_meas_range_get(sdi, &low, &high)) == SR_OK) {
213 range[0] = g_variant_new_uint64(low);
214 range[1] = g_variant_new_uint64(high);
215 *data = g_variant_new_tuple(range, 2);
218 case SR_CONF_POWER_OFF:
219 *data = g_variant_new_boolean(FALSE);
221 case SR_CONF_DATA_SOURCE:
222 if (devc->cur_data_source == DATA_SOURCE_LIVE)
223 *data = g_variant_new_string("Live");
225 *data = g_variant_new_string("Memory");
234 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
235 const struct sr_channel_group *cg)
237 struct dev_context *devc;
238 uint64_t tmp_u64, low, high;
245 if (sdi->status != SR_ST_ACTIVE)
246 return SR_ERR_DEV_CLOSED;
248 if (!(devc = sdi->priv)) {
249 sr_err("sdi->priv was NULL.");
255 case SR_CONF_LIMIT_SAMPLES:
256 tmp_u64 = g_variant_get_uint64(data);
257 devc->limit_samples = tmp_u64;
260 case SR_CONF_DATALOG:
261 ret = cem_dt_885x_recording_set(sdi, g_variant_get_boolean(data));
263 case SR_CONF_SPL_WEIGHT_FREQ:
264 tmp_str = g_variant_get_string(data, NULL);
265 if (!strcmp(tmp_str, "A"))
266 ret = cem_dt_885x_weight_freq_set(sdi,
267 SR_MQFLAG_SPL_FREQ_WEIGHT_A);
268 else if (!strcmp(tmp_str, "C"))
269 ret = cem_dt_885x_weight_freq_set(sdi,
270 SR_MQFLAG_SPL_FREQ_WEIGHT_C);
274 case SR_CONF_SPL_WEIGHT_TIME:
275 tmp_str = g_variant_get_string(data, NULL);
276 if (!strcmp(tmp_str, "F"))
277 ret = cem_dt_885x_weight_time_set(sdi,
278 SR_MQFLAG_SPL_TIME_WEIGHT_F);
279 else if (!strcmp(tmp_str, "S"))
280 ret = cem_dt_885x_weight_time_set(sdi,
281 SR_MQFLAG_SPL_TIME_WEIGHT_S);
285 case SR_CONF_HOLD_MAX:
286 tmp = g_variant_get_boolean(data) ? SR_MQFLAG_MAX : 0;
287 ret = cem_dt_885x_holdmode_set(sdi, tmp);
289 case SR_CONF_HOLD_MIN:
290 tmp = g_variant_get_boolean(data) ? SR_MQFLAG_MIN : 0;
291 ret = cem_dt_885x_holdmode_set(sdi, tmp);
293 case SR_CONF_SPL_MEASUREMENT_RANGE:
294 g_variant_get(data, "(tt)", &low, &high);
296 for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) {
297 if (meas_ranges[i][0] == low && meas_ranges[i][1] == high) {
298 ret = cem_dt_885x_meas_range_set(sdi, low, high);
303 case SR_CONF_POWER_OFF:
304 if (g_variant_get_boolean(data))
305 ret = cem_dt_885x_power_off(sdi);
307 case SR_CONF_DATA_SOURCE:
308 tmp_str = g_variant_get_string(data, NULL);
309 if (!strcmp(tmp_str, "Live"))
310 devc->cur_data_source = DATA_SOURCE_LIVE;
311 else if (!strcmp(tmp_str, "Memory"))
312 devc->cur_data_source = DATA_SOURCE_MEMORY;
315 devc->enable_data_source_memory = devc->cur_data_source == DATA_SOURCE_MEMORY;
324 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
325 const struct sr_channel_group *cg)
327 GVariant *tuple, *range[2];
337 case SR_CONF_SCAN_OPTIONS:
338 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
339 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
341 case SR_CONF_DEVICE_OPTIONS:
342 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
343 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
350 case SR_CONF_DEVICE_OPTIONS:
351 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
352 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
354 case SR_CONF_SPL_WEIGHT_FREQ:
355 *data = g_variant_new_strv(weight_freq, ARRAY_SIZE(weight_freq));
357 case SR_CONF_SPL_WEIGHT_TIME:
358 *data = g_variant_new_strv(weight_time, ARRAY_SIZE(weight_time));
360 case SR_CONF_SPL_MEASUREMENT_RANGE:
361 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
362 for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) {
363 range[0] = g_variant_new_uint64(meas_ranges[i][0]);
364 range[1] = g_variant_new_uint64(meas_ranges[i][1]);
365 tuple = g_variant_new_tuple(range, 2);
366 g_variant_builder_add_value(&gvb, tuple);
368 *data = g_variant_builder_end(&gvb);
370 case SR_CONF_DATA_SOURCE:
371 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
381 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
383 struct dev_context *devc;
384 struct sr_serial_dev_inst *serial;
386 if (sdi->status != SR_ST_ACTIVE)
387 return SR_ERR_DEV_CLOSED;
389 if (!(devc = sdi->priv)) {
390 sr_err("sdi->priv was NULL.");
394 devc->cb_data = cb_data;
395 devc->state = ST_INIT;
396 devc->num_samples = 0;
399 /* Send header packet to the session bus. */
400 std_session_send_df_header(cb_data, LOG_PREFIX);
402 /* Poll every 100ms, or whenever some data comes in. */
404 serial_source_add(sdi->session, serial, G_IO_IN, 150,
405 cem_dt_885x_receive_data, (void *)sdi);
410 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
412 if (sdi->status != SR_ST_ACTIVE)
413 return SR_ERR_DEV_CLOSED;
415 return std_serial_dev_acquisition_stop(sdi, cb_data, std_serial_dev_close,
416 sdi->conn, LOG_PREFIX);
419 SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info = {
420 .name = "cem-dt-885x",
421 .longname = "CEM DT-885x",
426 .dev_list = dev_list,
428 .config_get = config_get,
429 .config_set = config_set,
430 .config_list = config_list,
431 .dev_open = dev_open,
432 .dev_close = std_serial_dev_close,
433 .dev_acquisition_start = dev_acquisition_start,
434 .dev_acquisition_stop = dev_acquisition_stop,