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,
35 SR_CONF_SPL_WEIGHT_FREQ,
36 SR_CONF_SPL_WEIGHT_TIME,
37 SR_CONF_SPL_MEASUREMENT_RANGE,
45 static const char *weight_freq[] = {
50 static const char *weight_time[] = {
55 static const uint64_t meas_ranges[][2] = {
62 static const char *data_sources[] = {
66 SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info;
67 static struct sr_dev_driver *di = &cem_dt_885x_driver_info;
70 static int init(struct sr_context *sr_ctx)
72 return std_init(sr_ctx, di, LOG_PREFIX);
75 static GSList *scan(GSList *options)
77 struct drv_context *drvc;
78 struct dev_context *devc;
79 struct sr_config *src;
80 struct sr_serial_dev_inst *serial;
81 struct sr_dev_inst *sdi;
82 struct sr_probe *probe;
89 for (l = options; l; l = l->next) {
91 if (src->key == SR_CONF_CONN)
92 conn = g_variant_get_string(src->data, NULL);
97 if (!(serial = sr_serial_dev_inst_new(conn, SERIALCOMM)))
100 if (serial_open(serial, SERIAL_RDONLY | SERIAL_NONBLOCK) != SR_OK)
105 start = g_get_monotonic_time();
106 while (g_get_monotonic_time() - start < MAX_SCAN_TIME) {
107 if (serial_read(serial, &c, 1) == 1 && c == 0xa5) {
109 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "CEM",
113 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
114 sr_dbg("Device context malloc failed.");
117 devc->cur_mqflags = 0;
118 devc->recording = -1;
119 devc->cur_meas_range = 0;
120 devc->cur_data_source = DATA_SOURCE_LIVE;
121 devc->enable_data_source_memory = FALSE;
123 if (!(sdi->conn = sr_serial_dev_inst_new(conn, SERIALCOMM)))
126 sdi->inst_type = SR_INST_SERIAL;
129 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "SPL")))
131 sdi->probes = g_slist_append(sdi->probes, probe);
132 drvc->instances = g_slist_append(drvc->instances, sdi);
133 devices = g_slist_append(devices, sdi);
136 /* It takes about 1ms for a byte to come in. */
140 serial_close(serial);
145 static GSList *dev_list(void)
147 return ((struct drv_context *)(di->priv))->instances;
150 static int dev_clear(void)
152 return std_dev_clear(di, NULL);
155 static int dev_open(struct sr_dev_inst *sdi)
157 struct sr_serial_dev_inst *serial;
160 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
163 sdi->status = SR_ST_ACTIVE;
168 static int cleanup(void)
173 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
174 const struct sr_probe_group *probe_group)
176 struct dev_context *devc;
189 case SR_CONF_LIMIT_SAMPLES:
190 *data = g_variant_new_uint64(devc->limit_samples);
192 case SR_CONF_DATALOG:
193 if ((ret = cem_dt_885x_recording_get(sdi, &tmp)) == SR_OK)
194 *data = g_variant_new_boolean(tmp);
196 case SR_CONF_SPL_WEIGHT_FREQ:
197 tmp = cem_dt_885x_weight_freq_get(sdi);
198 if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_A)
199 *data = g_variant_new_string("A");
200 else if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_C)
201 *data = g_variant_new_string("C");
205 case SR_CONF_SPL_WEIGHT_TIME:
206 tmp = cem_dt_885x_weight_time_get(sdi);
207 if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_F)
208 *data = g_variant_new_string("F");
209 else if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_S)
210 *data = g_variant_new_string("S");
214 case SR_CONF_HOLD_MAX:
215 if ((ret = cem_dt_885x_holdmode_get(sdi, &tmp)) == SR_OK)
216 *data = g_variant_new_boolean(tmp == SR_MQFLAG_MAX);
218 case SR_CONF_HOLD_MIN:
219 if ((ret = cem_dt_885x_holdmode_get(sdi, &tmp)) == SR_OK)
220 *data = g_variant_new_boolean(tmp == SR_MQFLAG_MIN);
222 case SR_CONF_SPL_MEASUREMENT_RANGE:
223 if ((ret = cem_dt_885x_meas_range_get(sdi, &low, &high)) == SR_OK) {
224 range[0] = g_variant_new_uint64(low);
225 range[1] = g_variant_new_uint64(high);
226 *data = g_variant_new_tuple(range, 2);
229 case SR_CONF_POWER_OFF:
230 *data = g_variant_new_boolean(FALSE);
232 case SR_CONF_DATA_SOURCE:
233 if (devc->cur_data_source == DATA_SOURCE_LIVE)
234 *data = g_variant_new_string("Live");
236 *data = g_variant_new_string("Memory");
245 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
246 const struct sr_probe_group *probe_group)
248 struct dev_context *devc;
249 uint64_t tmp_u64, low, high;
256 if (sdi->status != SR_ST_ACTIVE)
257 return SR_ERR_DEV_CLOSED;
259 if (!(devc = sdi->priv)) {
260 sr_err("sdi->priv was NULL.");
266 case SR_CONF_LIMIT_SAMPLES:
267 tmp_u64 = g_variant_get_uint64(data);
268 devc->limit_samples = tmp_u64;
271 case SR_CONF_DATALOG:
272 ret = cem_dt_885x_recording_set(sdi, g_variant_get_boolean(data));
274 case SR_CONF_SPL_WEIGHT_FREQ:
275 tmp_str = g_variant_get_string(data, NULL);
276 if (!strcmp(tmp_str, "A"))
277 ret = cem_dt_885x_weight_freq_set(sdi,
278 SR_MQFLAG_SPL_FREQ_WEIGHT_A);
279 else if (!strcmp(tmp_str, "C"))
280 ret = cem_dt_885x_weight_freq_set(sdi,
281 SR_MQFLAG_SPL_FREQ_WEIGHT_C);
285 case SR_CONF_SPL_WEIGHT_TIME:
286 tmp_str = g_variant_get_string(data, NULL);
287 if (!strcmp(tmp_str, "F"))
288 ret = cem_dt_885x_weight_time_set(sdi,
289 SR_MQFLAG_SPL_TIME_WEIGHT_F);
290 else if (!strcmp(tmp_str, "S"))
291 ret = cem_dt_885x_weight_time_set(sdi,
292 SR_MQFLAG_SPL_TIME_WEIGHT_S);
296 case SR_CONF_HOLD_MAX:
297 tmp = g_variant_get_boolean(data) ? SR_MQFLAG_MAX : 0;
298 ret = cem_dt_885x_holdmode_set(sdi, tmp);
300 case SR_CONF_HOLD_MIN:
301 tmp = g_variant_get_boolean(data) ? SR_MQFLAG_MIN : 0;
302 ret = cem_dt_885x_holdmode_set(sdi, tmp);
304 case SR_CONF_SPL_MEASUREMENT_RANGE:
305 g_variant_get(data, "(tt)", &low, &high);
307 for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) {
308 if (meas_ranges[i][0] == low && meas_ranges[i][1] == high) {
309 ret = cem_dt_885x_meas_range_set(sdi, low, high);
314 case SR_CONF_POWER_OFF:
315 if (g_variant_get_boolean(data))
316 ret = cem_dt_885x_power_off(sdi);
318 case SR_CONF_DATA_SOURCE:
319 tmp_str = g_variant_get_string(data, NULL);
320 if (!strcmp(tmp_str, "Live"))
321 devc->cur_data_source = DATA_SOURCE_LIVE;
322 else if (!strcmp(tmp_str, "Memory"))
323 devc->cur_data_source = DATA_SOURCE_MEMORY;
326 devc->enable_data_source_memory = devc->cur_data_source == DATA_SOURCE_MEMORY;
335 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
336 const struct sr_probe_group *probe_group)
338 GVariant *tuple, *range[2];
348 case SR_CONF_SCAN_OPTIONS:
349 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
350 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
352 case SR_CONF_DEVICE_OPTIONS:
353 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
354 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
356 case SR_CONF_SPL_WEIGHT_FREQ:
357 *data = g_variant_new_strv(weight_freq, ARRAY_SIZE(weight_freq));
359 case SR_CONF_SPL_WEIGHT_TIME:
360 *data = g_variant_new_strv(weight_time, ARRAY_SIZE(weight_time));
362 case SR_CONF_SPL_MEASUREMENT_RANGE:
363 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
364 for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) {
365 range[0] = g_variant_new_uint64(meas_ranges[i][0]);
366 range[1] = g_variant_new_uint64(meas_ranges[i][1]);
367 tuple = g_variant_new_tuple(range, 2);
368 g_variant_builder_add_value(&gvb, tuple);
370 *data = g_variant_builder_end(&gvb);
372 case SR_CONF_DATA_SOURCE:
373 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
382 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
384 struct dev_context *devc;
385 struct sr_serial_dev_inst *serial;
387 if (sdi->status != SR_ST_ACTIVE)
388 return SR_ERR_DEV_CLOSED;
390 if (!(devc = sdi->priv)) {
391 sr_err("sdi->priv was NULL.");
395 devc->cb_data = cb_data;
396 devc->state = ST_INIT;
397 devc->num_samples = 0;
400 /* Send header packet to the session bus. */
401 std_session_send_df_header(cb_data, LOG_PREFIX);
403 /* Poll every 100ms, or whenever some data comes in. */
405 serial_source_add(serial, G_IO_IN, 150, cem_dt_885x_receive_data,
411 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
415 if (sdi->status != SR_ST_ACTIVE)
416 return SR_ERR_DEV_CLOSED;
418 return std_serial_dev_acquisition_stop(sdi, cb_data, std_serial_dev_close,
419 sdi->conn, LOG_PREFIX);
422 SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info = {
423 .name = "cem-dt-885x",
424 .longname = "CEM DT-885x",
429 .dev_list = dev_list,
430 .dev_clear = dev_clear,
431 .config_get = config_get,
432 .config_set = config_set,
433 .config_list = config_list,
434 .dev_open = dev_open,
435 .dev_close = std_serial_dev_close,
436 .dev_acquisition_start = dev_acquisition_start,
437 .dev_acquisition_stop = dev_acquisition_stop,