]>
Commit | Line | Data |
---|---|---|
8fa9368e BV |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 Bert Vermeulen <bert@biot.com> | |
5 | * | |
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. | |
10 | * | |
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. | |
15 | * | |
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/>. | |
18 | */ | |
19 | ||
6ec6c43b | 20 | #include <config.h> |
be733919 | 21 | #include <string.h> |
8fa9368e BV |
22 | #include "protocol.h" |
23 | ||
7fb8279c | 24 | #define SERIALCOMM "9600/8n1" |
1beccaed | 25 | |
7fb8279c | 26 | /* 23ms is the longest interval between tokens. */ |
1a46cc62 | 27 | #define MAX_SCAN_TIME_US (25 * 1000) |
7fb8279c | 28 | |
a0e0bb41 | 29 | static const uint32_t scanopts[] = { |
7fb8279c BV |
30 | SR_CONF_CONN, |
31 | }; | |
32 | ||
023c73ae | 33 | static const uint32_t drvopts[] = { |
7fb8279c | 34 | SR_CONF_SOUNDLEVELMETER, |
d7125bfa BV |
35 | }; |
36 | ||
023c73ae | 37 | static const uint32_t devopts[] = { |
e91bb0a6 | 38 | SR_CONF_CONTINUOUS, |
023c73ae | 39 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, |
5827f61b BV |
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, | |
7fb8279c BV |
48 | }; |
49 | ||
be733919 BV |
50 | static const char *weight_freq[] = { |
51 | "A", | |
52 | "C", | |
53 | }; | |
7fb8279c | 54 | |
1487ce4f BV |
55 | static const char *weight_time[] = { |
56 | "F", | |
57 | "S", | |
58 | }; | |
59 | ||
f157b2ee BV |
60 | static const uint64_t meas_ranges[][2] = { |
61 | { 30, 130 }, | |
62 | { 30, 80 }, | |
63 | { 50, 100 }, | |
64 | { 80, 130 }, | |
65 | }; | |
66 | ||
662172d4 BV |
67 | static const char *data_sources[] = { |
68 | "Live", | |
69 | "Memory", | |
70 | }; | |
8fa9368e | 71 | |
4f840ce9 | 72 | static GSList *scan(struct sr_dev_driver *di, GSList *options) |
8fa9368e BV |
73 | { |
74 | struct drv_context *drvc; | |
7fb8279c BV |
75 | struct dev_context *devc; |
76 | struct sr_config *src; | |
77 | struct sr_serial_dev_inst *serial; | |
78 | struct sr_dev_inst *sdi; | |
7fb8279c BV |
79 | GSList *l, *devices; |
80 | gint64 start; | |
81 | const char *conn; | |
82 | unsigned char c; | |
83 | ||
84 | conn = NULL; | |
85 | for (l = options; l; l = l->next) { | |
86 | src = l->data; | |
87 | if (src->key == SR_CONF_CONN) | |
88 | conn = g_variant_get_string(src->data, NULL); | |
89 | } | |
90 | if (!conn) | |
91 | return NULL; | |
92 | ||
91219afc | 93 | serial = sr_serial_dev_inst_new(conn, SERIALCOMM); |
8fa9368e | 94 | |
d7b269da | 95 | if (serial_open(serial, SERIAL_RDONLY) != SR_OK) |
7fb8279c | 96 | return NULL; |
8fa9368e BV |
97 | |
98 | devices = NULL; | |
41812aca | 99 | drvc = di->context; |
7fb8279c | 100 | start = g_get_monotonic_time(); |
1a46cc62 | 101 | while (g_get_monotonic_time() - start < MAX_SCAN_TIME_US) { |
d7b269da | 102 | if (serial_read_nonblocking(serial, &c, 1) == 1 && c == 0xa5) { |
7fb8279c | 103 | /* Found one. */ |
aac29cc1 | 104 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
0af636be UH |
105 | sdi->status = SR_ST_INACTIVE; |
106 | sdi->vendor = g_strdup("CEM"); | |
107 | sdi->model = g_strdup("DT-885x"); | |
f57d8ffe | 108 | devc = g_malloc0(sizeof(struct dev_context)); |
e1af0e85 BV |
109 | devc->cur_mqflags = 0; |
110 | devc->recording = -1; | |
f157b2ee | 111 | devc->cur_meas_range = 0; |
662172d4 | 112 | devc->cur_data_source = DATA_SOURCE_LIVE; |
cea26f6e | 113 | devc->enable_data_source_memory = FALSE; |
91219afc | 114 | sdi->conn = sr_serial_dev_inst_new(conn, SERIALCOMM); |
7fb8279c BV |
115 | sdi->inst_type = SR_INST_SERIAL; |
116 | sdi->priv = devc; | |
117 | sdi->driver = di; | |
5e23fcab | 118 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "SPL"); |
7fb8279c BV |
119 | drvc->instances = g_slist_append(drvc->instances, sdi); |
120 | devices = g_slist_append(devices, sdi); | |
121 | break; | |
122 | } | |
123 | /* It takes about 1ms for a byte to come in. */ | |
124 | g_usleep(1000); | |
125 | } | |
8fa9368e | 126 | |
7fb8279c | 127 | serial_close(serial); |
8fa9368e BV |
128 | |
129 | return devices; | |
130 | } | |
131 | ||
584560f1 | 132 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
53b4680f | 133 | const struct sr_channel_group *cg) |
8fa9368e | 134 | { |
7fb8279c | 135 | struct dev_context *devc; |
f157b2ee BV |
136 | GVariant *range[2]; |
137 | uint64_t low, high; | |
a90e480c | 138 | int tmp, ret; |
8fa9368e | 139 | |
53b4680f | 140 | (void)cg; |
d3c74a6f | 141 | |
7fb8279c BV |
142 | if (!sdi) |
143 | return SR_ERR_ARG; | |
8fa9368e | 144 | |
7fb8279c | 145 | devc = sdi->priv; |
a90e480c | 146 | ret = SR_OK; |
8fa9368e | 147 | switch (key) { |
7fb8279c BV |
148 | case SR_CONF_LIMIT_SAMPLES: |
149 | *data = g_variant_new_uint64(devc->limit_samples); | |
150 | break; | |
e1af0e85 | 151 | case SR_CONF_DATALOG: |
0cd9107d BV |
152 | if ((ret = cem_dt_885x_recording_get(sdi, &tmp)) == SR_OK) |
153 | *data = g_variant_new_boolean(tmp); | |
e1af0e85 | 154 | break; |
be733919 BV |
155 | case SR_CONF_SPL_WEIGHT_FREQ: |
156 | tmp = cem_dt_885x_weight_freq_get(sdi); | |
157 | if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_A) | |
158 | *data = g_variant_new_string("A"); | |
159 | else if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_C) | |
160 | *data = g_variant_new_string("C"); | |
161 | else | |
162 | return SR_ERR; | |
163 | break; | |
1487ce4f BV |
164 | case SR_CONF_SPL_WEIGHT_TIME: |
165 | tmp = cem_dt_885x_weight_time_get(sdi); | |
166 | if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_F) | |
167 | *data = g_variant_new_string("F"); | |
168 | else if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_S) | |
169 | *data = g_variant_new_string("S"); | |
170 | else | |
171 | return SR_ERR; | |
172 | break; | |
a90e480c BV |
173 | case SR_CONF_HOLD_MAX: |
174 | if ((ret = cem_dt_885x_holdmode_get(sdi, &tmp)) == SR_OK) | |
175 | *data = g_variant_new_boolean(tmp == SR_MQFLAG_MAX); | |
176 | break; | |
177 | case SR_CONF_HOLD_MIN: | |
178 | if ((ret = cem_dt_885x_holdmode_get(sdi, &tmp)) == SR_OK) | |
179 | *data = g_variant_new_boolean(tmp == SR_MQFLAG_MIN); | |
180 | break; | |
f157b2ee BV |
181 | case SR_CONF_SPL_MEASUREMENT_RANGE: |
182 | if ((ret = cem_dt_885x_meas_range_get(sdi, &low, &high)) == SR_OK) { | |
183 | range[0] = g_variant_new_uint64(low); | |
184 | range[1] = g_variant_new_uint64(high); | |
185 | *data = g_variant_new_tuple(range, 2); | |
186 | } | |
187 | break; | |
4c22355f BV |
188 | case SR_CONF_POWER_OFF: |
189 | *data = g_variant_new_boolean(FALSE); | |
190 | break; | |
662172d4 BV |
191 | case SR_CONF_DATA_SOURCE: |
192 | if (devc->cur_data_source == DATA_SOURCE_LIVE) | |
193 | *data = g_variant_new_string("Live"); | |
194 | else | |
195 | *data = g_variant_new_string("Memory"); | |
196 | break; | |
8fa9368e BV |
197 | default: |
198 | return SR_ERR_NA; | |
199 | } | |
200 | ||
a90e480c | 201 | return ret; |
8fa9368e BV |
202 | } |
203 | ||
584560f1 | 204 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, |
53b4680f | 205 | const struct sr_channel_group *cg) |
8fa9368e | 206 | { |
7fb8279c | 207 | struct dev_context *devc; |
f157b2ee BV |
208 | uint64_t tmp_u64, low, high; |
209 | unsigned int i; | |
a90e480c | 210 | int tmp, ret; |
be733919 | 211 | const char *tmp_str; |
8fa9368e | 212 | |
53b4680f | 213 | (void)cg; |
d3c74a6f | 214 | |
8fa9368e BV |
215 | if (sdi->status != SR_ST_ACTIVE) |
216 | return SR_ERR_DEV_CLOSED; | |
217 | ||
b0baddef | 218 | devc = sdi->priv; |
7fb8279c | 219 | |
8fa9368e BV |
220 | ret = SR_OK; |
221 | switch (key) { | |
7fb8279c BV |
222 | case SR_CONF_LIMIT_SAMPLES: |
223 | tmp_u64 = g_variant_get_uint64(data); | |
224 | devc->limit_samples = tmp_u64; | |
225 | ret = SR_OK; | |
226 | break; | |
e1af0e85 | 227 | case SR_CONF_DATALOG: |
0cd9107d | 228 | ret = cem_dt_885x_recording_set(sdi, g_variant_get_boolean(data)); |
e1af0e85 | 229 | break; |
be733919 BV |
230 | case SR_CONF_SPL_WEIGHT_FREQ: |
231 | tmp_str = g_variant_get_string(data, NULL); | |
232 | if (!strcmp(tmp_str, "A")) | |
233 | ret = cem_dt_885x_weight_freq_set(sdi, | |
234 | SR_MQFLAG_SPL_FREQ_WEIGHT_A); | |
235 | else if (!strcmp(tmp_str, "C")) | |
236 | ret = cem_dt_885x_weight_freq_set(sdi, | |
237 | SR_MQFLAG_SPL_FREQ_WEIGHT_C); | |
238 | else | |
239 | return SR_ERR_ARG; | |
240 | break; | |
1487ce4f BV |
241 | case SR_CONF_SPL_WEIGHT_TIME: |
242 | tmp_str = g_variant_get_string(data, NULL); | |
243 | if (!strcmp(tmp_str, "F")) | |
244 | ret = cem_dt_885x_weight_time_set(sdi, | |
245 | SR_MQFLAG_SPL_TIME_WEIGHT_F); | |
246 | else if (!strcmp(tmp_str, "S")) | |
247 | ret = cem_dt_885x_weight_time_set(sdi, | |
248 | SR_MQFLAG_SPL_TIME_WEIGHT_S); | |
249 | else | |
250 | return SR_ERR_ARG; | |
251 | break; | |
a90e480c BV |
252 | case SR_CONF_HOLD_MAX: |
253 | tmp = g_variant_get_boolean(data) ? SR_MQFLAG_MAX : 0; | |
254 | ret = cem_dt_885x_holdmode_set(sdi, tmp); | |
255 | break; | |
256 | case SR_CONF_HOLD_MIN: | |
257 | tmp = g_variant_get_boolean(data) ? SR_MQFLAG_MIN : 0; | |
258 | ret = cem_dt_885x_holdmode_set(sdi, tmp); | |
259 | break; | |
f157b2ee BV |
260 | case SR_CONF_SPL_MEASUREMENT_RANGE: |
261 | g_variant_get(data, "(tt)", &low, &high); | |
262 | ret = SR_ERR_ARG; | |
263 | for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) { | |
264 | if (meas_ranges[i][0] == low && meas_ranges[i][1] == high) { | |
265 | ret = cem_dt_885x_meas_range_set(sdi, low, high); | |
266 | break; | |
267 | } | |
268 | } | |
269 | break; | |
4c22355f BV |
270 | case SR_CONF_POWER_OFF: |
271 | if (g_variant_get_boolean(data)) | |
272 | ret = cem_dt_885x_power_off(sdi); | |
273 | break; | |
662172d4 BV |
274 | case SR_CONF_DATA_SOURCE: |
275 | tmp_str = g_variant_get_string(data, NULL); | |
276 | if (!strcmp(tmp_str, "Live")) | |
277 | devc->cur_data_source = DATA_SOURCE_LIVE; | |
278 | else if (!strcmp(tmp_str, "Memory")) | |
279 | devc->cur_data_source = DATA_SOURCE_MEMORY; | |
280 | else | |
281 | return SR_ERR; | |
cea26f6e | 282 | devc->enable_data_source_memory = devc->cur_data_source == DATA_SOURCE_MEMORY; |
662172d4 | 283 | break; |
8fa9368e BV |
284 | default: |
285 | ret = SR_ERR_NA; | |
286 | } | |
287 | ||
288 | return ret; | |
289 | } | |
290 | ||
584560f1 | 291 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
53b4680f | 292 | const struct sr_channel_group *cg) |
8fa9368e | 293 | { |
f157b2ee BV |
294 | GVariant *tuple, *range[2]; |
295 | GVariantBuilder gvb; | |
296 | unsigned int i; | |
8fa9368e BV |
297 | int ret; |
298 | ||
53b4680f | 299 | (void)cg; |
8fa9368e BV |
300 | |
301 | ret = SR_OK; | |
d7125bfa BV |
302 | if (!sdi) { |
303 | switch (key) { | |
304 | case SR_CONF_SCAN_OPTIONS: | |
305 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
306 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); | |
307 | break; | |
308 | case SR_CONF_DEVICE_OPTIONS: | |
309 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
023c73ae | 310 | drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t)); |
d7125bfa BV |
311 | break; |
312 | default: | |
313 | return SR_ERR_NA; | |
314 | } | |
315 | } else { | |
316 | switch (key) { | |
317 | case SR_CONF_DEVICE_OPTIONS: | |
318 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
023c73ae | 319 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); |
e742b88f | 320 | break; |
d7125bfa BV |
321 | case SR_CONF_SPL_WEIGHT_FREQ: |
322 | *data = g_variant_new_strv(weight_freq, ARRAY_SIZE(weight_freq)); | |
323 | break; | |
324 | case SR_CONF_SPL_WEIGHT_TIME: | |
325 | *data = g_variant_new_strv(weight_time, ARRAY_SIZE(weight_time)); | |
326 | break; | |
327 | case SR_CONF_SPL_MEASUREMENT_RANGE: | |
328 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
329 | for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) { | |
330 | range[0] = g_variant_new_uint64(meas_ranges[i][0]); | |
331 | range[1] = g_variant_new_uint64(meas_ranges[i][1]); | |
332 | tuple = g_variant_new_tuple(range, 2); | |
333 | g_variant_builder_add_value(&gvb, tuple); | |
334 | } | |
335 | *data = g_variant_builder_end(&gvb); | |
336 | break; | |
337 | case SR_CONF_DATA_SOURCE: | |
338 | *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources)); | |
339 | break; | |
340 | default: | |
341 | return SR_ERR_NA; | |
f157b2ee | 342 | } |
8fa9368e BV |
343 | } |
344 | ||
345 | return ret; | |
346 | } | |
347 | ||
695dc859 | 348 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) |
8fa9368e | 349 | { |
e37c4b39 BV |
350 | struct dev_context *devc; |
351 | struct sr_serial_dev_inst *serial; | |
8fa9368e BV |
352 | |
353 | if (sdi->status != SR_ST_ACTIVE) | |
354 | return SR_ERR_DEV_CLOSED; | |
355 | ||
208c1d35 | 356 | devc = sdi->priv; |
e37c4b39 BV |
357 | devc->state = ST_INIT; |
358 | devc->num_samples = 0; | |
359 | devc->buf_len = 0; | |
360 | ||
695dc859 | 361 | std_session_send_df_header(sdi, LOG_PREFIX); |
e37c4b39 BV |
362 | |
363 | /* Poll every 100ms, or whenever some data comes in. */ | |
364 | serial = sdi->conn; | |
102f1239 BV |
365 | serial_source_add(sdi->session, serial, G_IO_IN, 150, |
366 | cem_dt_885x_receive_data, (void *)sdi); | |
8fa9368e BV |
367 | |
368 | return SR_OK; | |
369 | } | |
370 | ||
695dc859 | 371 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) |
8fa9368e | 372 | { |
8fa9368e BV |
373 | if (sdi->status != SR_ST_ACTIVE) |
374 | return SR_ERR_DEV_CLOSED; | |
375 | ||
6525d819 | 376 | return std_serial_dev_acquisition_stop(sdi, std_serial_dev_close, |
e37c4b39 | 377 | sdi->conn, LOG_PREFIX); |
8fa9368e BV |
378 | } |
379 | ||
dd5c48a6 | 380 | static struct sr_dev_driver cem_dt_885x_driver_info = { |
8fa9368e BV |
381 | .name = "cem-dt-885x", |
382 | .longname = "CEM DT-885x", | |
383 | .api_version = 1, | |
c2fdcc25 | 384 | .init = std_init, |
700d6b64 | 385 | .cleanup = std_cleanup, |
8fa9368e | 386 | .scan = scan, |
c01bf34c | 387 | .dev_list = std_dev_list, |
a6630742 | 388 | .dev_clear = NULL, |
8fa9368e BV |
389 | .config_get = config_get, |
390 | .config_set = config_set, | |
391 | .config_list = config_list, | |
8f878dc6 | 392 | .dev_open = std_serial_dev_open, |
bf2c987f | 393 | .dev_close = std_serial_dev_close, |
8fa9368e BV |
394 | .dev_acquisition_start = dev_acquisition_start, |
395 | .dev_acquisition_stop = dev_acquisition_stop, | |
41812aca | 396 | .context = NULL, |
8fa9368e | 397 | }; |
dd5c48a6 | 398 | SR_REGISTER_DEV_DRIVER(cem_dt_885x_driver_info); |