]>
Commit | Line | Data |
---|---|---|
6bcb3ee8 JH |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2014 Janne Huttunen <jahuttun@gmail.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> |
6bcb3ee8 JH |
21 | #include <stdint.h> |
22 | #include <string.h> | |
23 | #include <math.h> | |
24 | #include <glib.h> | |
c1aae900 | 25 | #include <libsigrok/libsigrok.h> |
6bcb3ee8 JH |
26 | #include "libsigrok-internal.h" |
27 | ||
28 | #define LOG_PREFIX "es51919" | |
29 | ||
48b7c346 GS |
30 | #ifdef HAVE_SERIAL_COMM |
31 | ||
6bcb3ee8 JH |
32 | struct dev_buffer { |
33 | /** Total size of the buffer. */ | |
34 | size_t size; | |
35 | /** Amount of data currently in the buffer. */ | |
36 | size_t len; | |
37 | /** Offset where the data starts in the buffer. */ | |
38 | size_t offset; | |
39 | /** Space for the data. */ | |
40 | uint8_t data[]; | |
41 | }; | |
42 | ||
43 | static struct dev_buffer *dev_buffer_new(size_t size) | |
44 | { | |
45 | struct dev_buffer *dbuf; | |
46 | ||
91219afc | 47 | dbuf = g_malloc0(sizeof(struct dev_buffer) + size); |
6bcb3ee8 JH |
48 | dbuf->size = size; |
49 | dbuf->len = 0; | |
50 | dbuf->offset = 0; | |
51 | ||
52 | return dbuf; | |
53 | } | |
54 | ||
55 | static void dev_buffer_destroy(struct dev_buffer *dbuf) | |
56 | { | |
57 | g_free(dbuf); | |
58 | } | |
59 | ||
60 | static int dev_buffer_fill_serial(struct dev_buffer *dbuf, | |
61 | struct sr_dev_inst *sdi) | |
62 | { | |
63 | struct sr_serial_dev_inst *serial; | |
64 | int len; | |
65 | ||
66 | serial = sdi->conn; | |
67 | ||
68 | /* If we already have data, move it to the beginning of the buffer. */ | |
69 | if (dbuf->len > 0 && dbuf->offset > 0) | |
70 | memmove(dbuf->data, dbuf->data + dbuf->offset, dbuf->len); | |
71 | ||
72 | dbuf->offset = 0; | |
73 | ||
74 | len = dbuf->size - dbuf->len; | |
75 | len = serial_read_nonblocking(serial, dbuf->data + dbuf->len, len); | |
76 | if (len < 0) { | |
77 | sr_err("Serial port read error: %d.", len); | |
78 | return len; | |
79 | } | |
80 | ||
81 | dbuf->len += len; | |
82 | ||
83 | return SR_OK; | |
84 | } | |
85 | ||
86 | static uint8_t *dev_buffer_packet_find(struct dev_buffer *dbuf, | |
87 | gboolean (*packet_valid)(const uint8_t *), | |
88 | size_t packet_size) | |
89 | { | |
90 | size_t offset; | |
91 | ||
92 | while (dbuf->len >= packet_size) { | |
93 | if (packet_valid(dbuf->data + dbuf->offset)) { | |
94 | offset = dbuf->offset; | |
95 | dbuf->offset += packet_size; | |
96 | dbuf->len -= packet_size; | |
97 | return dbuf->data + offset; | |
98 | } | |
99 | dbuf->offset++; | |
100 | dbuf->len--; | |
101 | } | |
102 | ||
103 | return NULL; | |
104 | } | |
105 | ||
787ec9db JH |
106 | struct dev_limit_counter { |
107 | /** The current number of received samples/frames/etc. */ | |
6bcb3ee8 | 108 | uint64_t count; |
787ec9db | 109 | /** The limit (in number of samples/frames/etc.). */ |
6bcb3ee8 JH |
110 | uint64_t limit; |
111 | }; | |
112 | ||
787ec9db | 113 | static void dev_limit_counter_start(struct dev_limit_counter *cnt) |
6bcb3ee8 JH |
114 | { |
115 | cnt->count = 0; | |
116 | } | |
117 | ||
787ec9db | 118 | static void dev_limit_counter_inc(struct dev_limit_counter *cnt) |
6bcb3ee8 JH |
119 | { |
120 | cnt->count++; | |
121 | } | |
122 | ||
787ec9db JH |
123 | static void dev_limit_counter_limit_set(struct dev_limit_counter *cnt, |
124 | uint64_t limit) | |
6bcb3ee8 JH |
125 | { |
126 | cnt->limit = limit; | |
127 | } | |
128 | ||
787ec9db | 129 | static gboolean dev_limit_counter_limit_reached(struct dev_limit_counter *cnt) |
6bcb3ee8 JH |
130 | { |
131 | if (cnt->limit && cnt->count >= cnt->limit) { | |
787ec9db | 132 | sr_info("Requested counter limit reached."); |
6bcb3ee8 JH |
133 | return TRUE; |
134 | } | |
135 | ||
136 | return FALSE; | |
137 | } | |
138 | ||
139 | struct dev_time_counter { | |
140 | /** The starting time of current sampling run. */ | |
141 | int64_t starttime; | |
142 | /** The time limit (in milliseconds). */ | |
143 | uint64_t limit; | |
144 | }; | |
145 | ||
146 | static void dev_time_counter_start(struct dev_time_counter *cnt) | |
147 | { | |
148 | cnt->starttime = g_get_monotonic_time(); | |
149 | } | |
150 | ||
151 | static void dev_time_limit_set(struct dev_time_counter *cnt, uint64_t limit) | |
152 | { | |
153 | cnt->limit = limit; | |
154 | } | |
155 | ||
156 | static gboolean dev_time_limit_reached(struct dev_time_counter *cnt) | |
157 | { | |
158 | int64_t time; | |
159 | ||
160 | if (cnt->limit) { | |
161 | time = (g_get_monotonic_time() - cnt->starttime) / 1000; | |
162 | if (time > (int64_t)cnt->limit) { | |
163 | sr_info("Requested time limit reached."); | |
164 | return TRUE; | |
165 | } | |
166 | } | |
167 | ||
168 | return FALSE; | |
169 | } | |
170 | ||
171 | static void serial_conf_get(GSList *options, const char *def_serialcomm, | |
172 | const char **conn, const char **serialcomm) | |
173 | { | |
174 | struct sr_config *src; | |
175 | GSList *l; | |
176 | ||
177 | *conn = *serialcomm = NULL; | |
178 | for (l = options; l; l = l->next) { | |
179 | src = l->data; | |
180 | switch (src->key) { | |
181 | case SR_CONF_CONN: | |
182 | *conn = g_variant_get_string(src->data, NULL); | |
183 | break; | |
184 | case SR_CONF_SERIALCOMM: | |
185 | *serialcomm = g_variant_get_string(src->data, NULL); | |
186 | break; | |
187 | } | |
188 | } | |
189 | ||
190 | if (*serialcomm == NULL) | |
191 | *serialcomm = def_serialcomm; | |
192 | } | |
193 | ||
194 | static struct sr_serial_dev_inst *serial_dev_new(GSList *options, | |
195 | const char *def_serialcomm) | |
196 | ||
197 | { | |
198 | const char *conn, *serialcomm; | |
199 | ||
200 | serial_conf_get(options, def_serialcomm, &conn, &serialcomm); | |
201 | ||
202 | if (!conn) | |
203 | return NULL; | |
204 | ||
205 | return sr_serial_dev_inst_new(conn, serialcomm); | |
206 | } | |
207 | ||
208 | static int serial_stream_check_buf(struct sr_serial_dev_inst *serial, | |
209 | uint8_t *buf, size_t buflen, | |
210 | size_t packet_size, | |
211 | packet_valid_callback is_valid, | |
d03815a0 | 212 | uint64_t timeout_ms) |
6bcb3ee8 JH |
213 | { |
214 | size_t len, dropped; | |
215 | int ret; | |
216 | ||
217 | if ((ret = serial_open(serial, SERIAL_RDWR)) != SR_OK) | |
218 | return ret; | |
219 | ||
220 | serial_flush(serial); | |
221 | ||
222 | len = buflen; | |
223 | ret = serial_stream_detect(serial, buf, &len, packet_size, | |
d03815a0 | 224 | is_valid, timeout_ms); |
6bcb3ee8 JH |
225 | |
226 | serial_close(serial); | |
227 | ||
228 | if (ret != SR_OK) | |
229 | return ret; | |
230 | ||
231 | /* | |
232 | * If we dropped more than two packets worth of data, something is | |
233 | * wrong. We shouldn't quit however, since the dropped bytes might be | |
234 | * just zeroes at the beginning of the stream. Those can occur as a | |
235 | * combination of the nonstandard cable that ships with some devices | |
236 | * and the serial port or USB to serial adapter. | |
237 | */ | |
238 | dropped = len - packet_size; | |
239 | if (dropped > 2 * packet_size) | |
240 | sr_warn("Had to drop too much data."); | |
241 | ||
242 | return SR_OK; | |
243 | } | |
244 | ||
245 | static int serial_stream_check(struct sr_serial_dev_inst *serial, | |
246 | size_t packet_size, | |
247 | packet_valid_callback is_valid, | |
d03815a0 | 248 | uint64_t timeout_ms) |
6bcb3ee8 JH |
249 | { |
250 | uint8_t buf[128]; | |
251 | ||
252 | return serial_stream_check_buf(serial, buf, sizeof(buf), packet_size, | |
d03815a0 | 253 | is_valid, timeout_ms); |
6bcb3ee8 JH |
254 | } |
255 | ||
6bcb3ee8 JH |
256 | /* |
257 | * Cyrustek ES51919 LCR chipset host protocol. | |
258 | * | |
259 | * Public official documentation does not contain the protocol | |
260 | * description, so this is all based on reverse engineering. | |
261 | * | |
262 | * Packet structure (17 bytes): | |
263 | * | |
264 | * 0x00: header1 ?? (0x00) | |
265 | * 0x01: header2 ?? (0x0d) | |
266 | * | |
267 | * 0x02: flags | |
268 | * bit 0 = hold enabled | |
269 | * bit 1 = reference shown (in delta mode) | |
270 | * bit 2 = delta mode | |
271 | * bit 3 = calibration mode | |
272 | * bit 4 = sorting mode | |
273 | * bit 5 = LCR mode | |
274 | * bit 6 = auto mode | |
275 | * bit 7 = parallel measurement (vs. serial) | |
276 | * | |
277 | * 0x03: config | |
278 | * bit 0-4 = ??? (0x10) | |
279 | * bit 5-7 = test frequency | |
280 | * 0 = 100 Hz | |
281 | * 1 = 120 Hz | |
282 | * 2 = 1 kHz | |
283 | * 3 = 10 kHz | |
284 | * 4 = 100 kHz | |
285 | * 5 = 0 Hz (DC) | |
286 | * | |
287 | * 0x04: tolerance (sorting mode) | |
288 | * 0 = not set | |
289 | * 3 = +-0.25% | |
290 | * 4 = +-0.5% | |
291 | * 5 = +-1% | |
292 | * 6 = +-2% | |
293 | * 7 = +-5% | |
294 | * 8 = +-10% | |
295 | * 9 = +-20% | |
296 | * 10 = -20+80% | |
297 | * | |
298 | * 0x05-0x09: primary measurement | |
299 | * 0x05: measured quantity | |
300 | * 1 = inductance | |
301 | * 2 = capacitance | |
302 | * 3 = resistance | |
303 | * 4 = DC resistance | |
304 | * 0x06: measurement MSB (0x4e20 = 20000 = outside limits) | |
305 | * 0x07: measurement LSB | |
306 | * 0x08: measurement info | |
307 | * bit 0-2 = decimal point multiplier (10^-val) | |
308 | * bit 3-7 = unit | |
309 | * 0 = no unit | |
310 | * 1 = Ohm | |
311 | * 2 = kOhm | |
312 | * 3 = MOhm | |
313 | * 5 = uH | |
314 | * 6 = mH | |
315 | * 7 = H | |
316 | * 8 = kH | |
317 | * 9 = pF | |
318 | * 10 = nF | |
319 | * 11 = uF | |
320 | * 12 = mF | |
321 | * 13 = % | |
322 | * 14 = degree | |
323 | * 0x09: measurement status | |
324 | * bit 0-3 = status | |
325 | * 0 = normal (measurement shown) | |
326 | * 1 = blank (nothing shown) | |
327 | * 2 = lines ("----") | |
99d090d8 | 328 | * 3 = outside limits ("OL") |
6bcb3ee8 JH |
329 | * 7 = pass ("PASS") |
330 | * 8 = fail ("FAIL") | |
331 | * 9 = open ("OPEn") | |
332 | * 10 = shorted ("Srt") | |
333 | * bit 4-6 = ??? (maybe part of same field with 0-3) | |
334 | * bit 7 = ??? (some independent flag) | |
335 | * | |
336 | * 0x0a-0x0e: secondary measurement | |
337 | * 0x0a: measured quantity | |
338 | * 0 = none | |
339 | * 1 = dissipation factor | |
340 | * 2 = quality factor | |
341 | * 3 = parallel AC resistance / ESR | |
342 | * 4 = phase angle | |
343 | * 0x0b-0x0e: like primary measurement | |
344 | * | |
345 | * 0x0f: footer1 (0x0d) ? | |
346 | * 0x10: footer2 (0x0a) ? | |
347 | */ | |
348 | ||
349 | #define PACKET_SIZE 17 | |
350 | ||
b94dd07b | 351 | static const double frequencies[] = { |
6bcb3ee8 JH |
352 | 100, 120, 1000, 10000, 100000, 0, |
353 | }; | |
354 | ||
a42a39ac JH |
355 | enum { MODEL_NONE, MODEL_PAR, MODEL_SER, MODEL_AUTO, }; |
356 | ||
357 | static const char *const models[] = { | |
358 | "NONE", "PARALLEL", "SERIES", "AUTO", | |
359 | }; | |
360 | ||
6bcb3ee8 | 361 | struct dev_context { |
787ec9db | 362 | struct dev_limit_counter frame_count; |
6bcb3ee8 | 363 | |
6bcb3ee8 JH |
364 | struct dev_time_counter time_count; |
365 | ||
6bcb3ee8 JH |
366 | struct dev_buffer *buf; |
367 | ||
368 | /** The frequency of the test signal (index to frequencies[]). */ | |
369 | unsigned int freq; | |
a42a39ac | 370 | |
a42a39ac JH |
371 | /** Equivalent circuit model (index to models[]). */ |
372 | unsigned int model; | |
6bcb3ee8 JH |
373 | }; |
374 | ||
a42a39ac | 375 | static const uint8_t *pkt_to_buf(const uint8_t *pkt, int is_secondary) |
6bcb3ee8 | 376 | { |
a42a39ac JH |
377 | return is_secondary ? pkt + 10 : pkt + 5; |
378 | } | |
379 | ||
380 | static int parse_mq(const uint8_t *pkt, int is_secondary, int is_parallel) | |
381 | { | |
382 | const uint8_t *buf; | |
383 | ||
384 | buf = pkt_to_buf(pkt, is_secondary); | |
385 | ||
6bcb3ee8 JH |
386 | switch (is_secondary << 8 | buf[0]) { |
387 | case 0x001: | |
1beccaed | 388 | return is_parallel ? |
c7c8994c | 389 | SR_MQ_PARALLEL_INDUCTANCE : SR_MQ_SERIES_INDUCTANCE; |
6bcb3ee8 JH |
390 | case 0x002: |
391 | return is_parallel ? | |
c7c8994c | 392 | SR_MQ_PARALLEL_CAPACITANCE : SR_MQ_SERIES_CAPACITANCE; |
6bcb3ee8 JH |
393 | case 0x003: |
394 | case 0x103: | |
395 | return is_parallel ? | |
c7c8994c | 396 | SR_MQ_PARALLEL_RESISTANCE : SR_MQ_SERIES_RESISTANCE; |
6bcb3ee8 JH |
397 | case 0x004: |
398 | return SR_MQ_RESISTANCE; | |
399 | case 0x100: | |
400 | return SR_MQ_DIFFERENCE; | |
401 | case 0x101: | |
402 | return SR_MQ_DISSIPATION_FACTOR; | |
403 | case 0x102: | |
404 | return SR_MQ_QUALITY_FACTOR; | |
405 | case 0x104: | |
406 | return SR_MQ_PHASE_ANGLE; | |
407 | } | |
408 | ||
409 | sr_err("Unknown quantity 0x%03x.", is_secondary << 8 | buf[0]); | |
410 | ||
7ffcf587 | 411 | return 0; |
6bcb3ee8 JH |
412 | } |
413 | ||
24b6882f | 414 | static float parse_value(const uint8_t *buf, int *digits) |
6bcb3ee8 | 415 | { |
24b6882f AJ |
416 | static const int exponents[] = {0, -1, -2, -3, -4, -5, -6, -7}; |
417 | int exponent; | |
6bcb3ee8 JH |
418 | int16_t val; |
419 | ||
24b6882f AJ |
420 | exponent = exponents[buf[3] & 7]; |
421 | *digits = -exponent; | |
6bcb3ee8 | 422 | val = (buf[1] << 8) | buf[2]; |
24b6882f | 423 | return (float)val * powf(10, exponent); |
6bcb3ee8 JH |
424 | } |
425 | ||
426 | static void parse_measurement(const uint8_t *pkt, float *floatval, | |
7ffcf587 | 427 | struct sr_datafeed_analog *analog, |
6bcb3ee8 JH |
428 | int is_secondary) |
429 | { | |
430 | static const struct { | |
431 | int unit; | |
24b6882f | 432 | int exponent; |
6bcb3ee8 | 433 | } units[] = { |
d9251a2c UH |
434 | { SR_UNIT_UNITLESS, 0 }, /* no unit */ |
435 | { SR_UNIT_OHM, 0 }, /* Ohm */ | |
436 | { SR_UNIT_OHM, 3 }, /* kOhm */ | |
437 | { SR_UNIT_OHM, 6 }, /* MOhm */ | |
438 | { -1, 0 }, /* ??? */ | |
439 | { SR_UNIT_HENRY, -6 }, /* uH */ | |
440 | { SR_UNIT_HENRY, -3 }, /* mH */ | |
441 | { SR_UNIT_HENRY, 0 }, /* H */ | |
442 | { SR_UNIT_HENRY, 3 }, /* kH */ | |
443 | { SR_UNIT_FARAD, -12 }, /* pF */ | |
444 | { SR_UNIT_FARAD, -9 }, /* nF */ | |
445 | { SR_UNIT_FARAD, -6 }, /* uF */ | |
446 | { SR_UNIT_FARAD, -3 }, /* mF */ | |
447 | { SR_UNIT_PERCENTAGE, 0 }, /* % */ | |
448 | { SR_UNIT_DEGREE, 0 }, /* degree */ | |
6bcb3ee8 JH |
449 | }; |
450 | const uint8_t *buf; | |
24b6882f | 451 | int digits, exponent; |
6bcb3ee8 JH |
452 | int state; |
453 | ||
a42a39ac | 454 | buf = pkt_to_buf(pkt, is_secondary); |
6bcb3ee8 | 455 | |
7ffcf587 UH |
456 | analog->meaning->mq = 0; |
457 | analog->meaning->mqflags = 0; | |
6bcb3ee8 JH |
458 | |
459 | state = buf[4] & 0xf; | |
460 | ||
461 | if (state != 0 && state != 3) | |
462 | return; | |
463 | ||
464 | if (pkt[2] & 0x18) { | |
465 | /* Calibration and Sorting modes not supported. */ | |
466 | return; | |
467 | } | |
468 | ||
469 | if (!is_secondary) { | |
470 | if (pkt[2] & 0x01) | |
7ffcf587 | 471 | analog->meaning->mqflags |= SR_MQFLAG_HOLD; |
6bcb3ee8 | 472 | if (pkt[2] & 0x02) |
7ffcf587 | 473 | analog->meaning->mqflags |= SR_MQFLAG_REFERENCE; |
6bcb3ee8 JH |
474 | } else { |
475 | if (pkt[2] & 0x04) | |
7ffcf587 | 476 | analog->meaning->mqflags |= SR_MQFLAG_RELATIVE; |
6bcb3ee8 JH |
477 | } |
478 | ||
693c5248 | 479 | if ((analog->meaning->mq = parse_mq(pkt, is_secondary, pkt[2] & 0x80)) == 0) |
6bcb3ee8 JH |
480 | return; |
481 | ||
482 | if ((buf[3] >> 3) >= ARRAY_SIZE(units)) { | |
483 | sr_err("Unknown unit %u.", buf[3] >> 3); | |
7ffcf587 | 484 | analog->meaning->mq = 0; |
6bcb3ee8 JH |
485 | return; |
486 | } | |
487 | ||
7ffcf587 | 488 | analog->meaning->unit = units[buf[3] >> 3].unit; |
6bcb3ee8 | 489 | |
24b6882f AJ |
490 | exponent = units[buf[3] >> 3].exponent; |
491 | *floatval = parse_value(buf, &digits); | |
492 | *floatval *= (state == 0) ? powf(10, exponent) : INFINITY; | |
493 | analog->encoding->digits = digits - exponent; | |
494 | analog->spec->spec_digits = digits - exponent; | |
6bcb3ee8 JH |
495 | } |
496 | ||
a42a39ac | 497 | static unsigned int parse_freq(const uint8_t *pkt) |
6bcb3ee8 JH |
498 | { |
499 | unsigned int freq; | |
500 | ||
501 | freq = pkt[3] >> 5; | |
502 | ||
503 | if (freq >= ARRAY_SIZE(frequencies)) { | |
504 | sr_err("Unknown frequency %u.", freq); | |
505 | freq = ARRAY_SIZE(frequencies) - 1; | |
506 | } | |
507 | ||
508 | return freq; | |
509 | } | |
510 | ||
a42a39ac JH |
511 | static unsigned int parse_model(const uint8_t *pkt) |
512 | { | |
513 | if (pkt[2] & 0x40) | |
514 | return MODEL_AUTO; | |
515 | else if (parse_mq(pkt, 0, 0) == SR_MQ_RESISTANCE) | |
516 | return MODEL_NONE; | |
517 | else if (pkt[2] & 0x80) | |
518 | return MODEL_PAR; | |
519 | else | |
520 | return MODEL_SER; | |
a42a39ac JH |
521 | } |
522 | ||
6bcb3ee8 JH |
523 | static gboolean packet_valid(const uint8_t *pkt) |
524 | { | |
525 | /* | |
526 | * If the first two bytes of the packet are indeed a constant | |
527 | * header, they should be checked too. Since we don't know it | |
528 | * for sure, we'll just check the last two for now since they | |
529 | * seem to be constant just like in the other Cyrustek chipset | |
530 | * protocols. | |
531 | */ | |
532 | if (pkt[15] == 0xd && pkt[16] == 0xa) | |
533 | return TRUE; | |
534 | ||
535 | return FALSE; | |
536 | } | |
537 | ||
a42a39ac JH |
538 | static int send_freq_update(struct sr_dev_inst *sdi, unsigned int freq) |
539 | { | |
7d1a4a52 | 540 | return sr_session_send_meta(sdi, SR_CONF_OUTPUT_FREQUENCY, |
b94dd07b | 541 | g_variant_new_double(frequencies[freq])); |
a42a39ac | 542 | } |
6bcb3ee8 | 543 | |
a42a39ac JH |
544 | static int send_model_update(struct sr_dev_inst *sdi, unsigned int model) |
545 | { | |
7d1a4a52 | 546 | return sr_session_send_meta(sdi, SR_CONF_EQUIV_CIRCUIT_MODEL, |
a42a39ac | 547 | g_variant_new_string(models[model])); |
6bcb3ee8 JH |
548 | } |
549 | ||
550 | static void handle_packet(struct sr_dev_inst *sdi, const uint8_t *pkt) | |
551 | { | |
552 | struct sr_datafeed_packet packet; | |
7ffcf587 UH |
553 | struct sr_datafeed_analog analog; |
554 | struct sr_analog_encoding encoding; | |
555 | struct sr_analog_meaning meaning; | |
556 | struct sr_analog_spec spec; | |
6bcb3ee8 | 557 | struct dev_context *devc; |
a42a39ac | 558 | unsigned int val; |
6bcb3ee8 | 559 | float floatval; |
a6413fa5 | 560 | gboolean frame; |
503519b7 | 561 | struct sr_channel *channel; |
6bcb3ee8 JH |
562 | |
563 | devc = sdi->priv; | |
564 | ||
a42a39ac JH |
565 | val = parse_freq(pkt); |
566 | if (val != devc->freq) { | |
567 | if (send_freq_update(sdi, val) == SR_OK) | |
568 | devc->freq = val; | |
569 | else | |
570 | return; | |
571 | } | |
572 | ||
a42a39ac JH |
573 | val = parse_model(pkt); |
574 | if (val != devc->model) { | |
575 | if (send_model_update(sdi, val) == SR_OK) | |
576 | devc->model = val; | |
6bcb3ee8 JH |
577 | else |
578 | return; | |
579 | } | |
580 | ||
a6413fa5 | 581 | frame = FALSE; |
6bcb3ee8 | 582 | |
869c8375 | 583 | /* Note: digits/spec_digits will be overridden later. */ |
7ffcf587 | 584 | sr_analog_init(&analog, &encoding, &meaning, &spec, 0); |
6bcb3ee8 JH |
585 | |
586 | analog.num_samples = 1; | |
587 | analog.data = &floatval; | |
588 | ||
503519b7 GS |
589 | channel = sdi->channels->data; |
590 | analog.meaning->channels = g_slist_append(NULL, channel); | |
6bcb3ee8 JH |
591 | |
592 | parse_measurement(pkt, &floatval, &analog, 0); | |
503519b7 | 593 | if (analog.meaning->mq != 0 && channel->enabled) { |
a6413fa5 JH |
594 | if (!frame) { |
595 | packet.type = SR_DF_FRAME_BEGIN; | |
695dc859 | 596 | sr_session_send(sdi, &packet); |
a6413fa5 JH |
597 | frame = TRUE; |
598 | } | |
599 | ||
7ffcf587 | 600 | packet.type = SR_DF_ANALOG; |
a6413fa5 JH |
601 | packet.payload = &analog; |
602 | ||
695dc859 | 603 | sr_session_send(sdi, &packet); |
6bcb3ee8 JH |
604 | } |
605 | ||
7ffcf587 | 606 | g_slist_free(analog.meaning->channels); |
503519b7 GS |
607 | |
608 | channel = sdi->channels->next->data; | |
609 | analog.meaning->channels = g_slist_append(NULL, channel); | |
6bcb3ee8 JH |
610 | |
611 | parse_measurement(pkt, &floatval, &analog, 1); | |
503519b7 | 612 | if (analog.meaning->mq != 0 && channel->enabled) { |
a6413fa5 JH |
613 | if (!frame) { |
614 | packet.type = SR_DF_FRAME_BEGIN; | |
695dc859 | 615 | sr_session_send(sdi, &packet); |
a6413fa5 JH |
616 | frame = TRUE; |
617 | } | |
618 | ||
7ffcf587 | 619 | packet.type = SR_DF_ANALOG; |
a6413fa5 JH |
620 | packet.payload = &analog; |
621 | ||
695dc859 | 622 | sr_session_send(sdi, &packet); |
6bcb3ee8 JH |
623 | } |
624 | ||
7ffcf587 | 625 | g_slist_free(analog.meaning->channels); |
80e20c10 | 626 | |
a6413fa5 JH |
627 | if (frame) { |
628 | packet.type = SR_DF_FRAME_END; | |
695dc859 | 629 | sr_session_send(sdi, &packet); |
787ec9db | 630 | dev_limit_counter_inc(&devc->frame_count); |
a6413fa5 | 631 | } |
6bcb3ee8 JH |
632 | } |
633 | ||
634 | static int handle_new_data(struct sr_dev_inst *sdi) | |
635 | { | |
636 | struct dev_context *devc; | |
637 | uint8_t *pkt; | |
638 | int ret; | |
639 | ||
640 | devc = sdi->priv; | |
641 | ||
642 | ret = dev_buffer_fill_serial(devc->buf, sdi); | |
643 | if (ret < 0) | |
644 | return ret; | |
645 | ||
646 | while ((pkt = dev_buffer_packet_find(devc->buf, packet_valid, | |
647 | PACKET_SIZE))) | |
648 | handle_packet(sdi, pkt); | |
649 | ||
650 | return SR_OK; | |
651 | } | |
652 | ||
653 | static int receive_data(int fd, int revents, void *cb_data) | |
654 | { | |
655 | struct sr_dev_inst *sdi; | |
656 | struct dev_context *devc; | |
657 | ||
658 | (void)fd; | |
659 | ||
660 | if (!(sdi = cb_data)) | |
661 | return TRUE; | |
662 | ||
663 | if (!(devc = sdi->priv)) | |
664 | return TRUE; | |
665 | ||
666 | if (revents == G_IO_IN) { | |
667 | /* Serial data arrived. */ | |
668 | handle_new_data(sdi); | |
669 | } | |
670 | ||
787ec9db | 671 | if (dev_limit_counter_limit_reached(&devc->frame_count) || |
6bcb3ee8 | 672 | dev_time_limit_reached(&devc->time_count)) |
d2f7c417 | 673 | sr_dev_acquisition_stop(sdi); |
6bcb3ee8 JH |
674 | |
675 | return TRUE; | |
676 | } | |
677 | ||
6bcb3ee8 JH |
678 | static const char *const channel_names[] = { "P1", "P2" }; |
679 | ||
680 | static int setup_channels(struct sr_dev_inst *sdi) | |
681 | { | |
682 | unsigned int i; | |
6bcb3ee8 | 683 | |
5e23fcab ML |
684 | for (i = 0; i < ARRAY_SIZE(channel_names); i++) |
685 | sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE, channel_names[i]); | |
6bcb3ee8 | 686 | |
6b7e644e | 687 | return SR_OK; |
6bcb3ee8 JH |
688 | } |
689 | ||
690 | SR_PRIV void es51919_serial_clean(void *priv) | |
691 | { | |
692 | struct dev_context *devc; | |
693 | ||
694 | if (!(devc = priv)) | |
695 | return; | |
696 | ||
697 | dev_buffer_destroy(devc->buf); | |
6bcb3ee8 JH |
698 | } |
699 | ||
700 | SR_PRIV struct sr_dev_inst *es51919_serial_scan(GSList *options, | |
701 | const char *vendor, | |
702 | const char *model) | |
703 | { | |
704 | struct sr_serial_dev_inst *serial; | |
705 | struct sr_dev_inst *sdi; | |
706 | struct dev_context *devc; | |
707 | int ret; | |
708 | ||
709 | serial = NULL; | |
710 | sdi = NULL; | |
711 | devc = NULL; | |
712 | ||
713 | if (!(serial = serial_dev_new(options, "9600/8n1/rts=1/dtr=1"))) | |
714 | goto scan_cleanup; | |
715 | ||
d03815a0 | 716 | ret = serial_stream_check(serial, PACKET_SIZE, packet_valid, 3000); |
6bcb3ee8 JH |
717 | if (ret != SR_OK) |
718 | goto scan_cleanup; | |
719 | ||
720 | sr_info("Found device on port %s.", serial->port); | |
721 | ||
aac29cc1 | 722 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
0af636be UH |
723 | sdi->status = SR_ST_INACTIVE; |
724 | sdi->vendor = g_strdup(vendor); | |
725 | sdi->model = g_strdup(model); | |
f57d8ffe | 726 | devc = g_malloc0(sizeof(struct dev_context)); |
91219afc | 727 | devc->buf = dev_buffer_new(PACKET_SIZE * 8); |
6bcb3ee8 JH |
728 | sdi->inst_type = SR_INST_SERIAL; |
729 | sdi->conn = serial; | |
6bcb3ee8 JH |
730 | sdi->priv = devc; |
731 | ||
732 | if (setup_channels(sdi) != SR_OK) | |
733 | goto scan_cleanup; | |
734 | ||
735 | return sdi; | |
736 | ||
737 | scan_cleanup: | |
738 | es51919_serial_clean(devc); | |
4bf93988 | 739 | sr_dev_inst_free(sdi); |
04891a99 | 740 | sr_serial_dev_inst_free(serial); |
6bcb3ee8 JH |
741 | |
742 | return NULL; | |
743 | } | |
744 | ||
745 | SR_PRIV int es51919_serial_config_get(uint32_t key, GVariant **data, | |
746 | const struct sr_dev_inst *sdi, | |
747 | const struct sr_channel_group *cg) | |
748 | { | |
749 | struct dev_context *devc; | |
750 | ||
751 | (void)cg; | |
752 | ||
709468ba | 753 | devc = sdi->priv; |
6bcb3ee8 JH |
754 | |
755 | switch (key) { | |
756 | case SR_CONF_OUTPUT_FREQUENCY: | |
b94dd07b | 757 | *data = g_variant_new_double(frequencies[devc->freq]); |
6bcb3ee8 | 758 | break; |
a42a39ac JH |
759 | case SR_CONF_EQUIV_CIRCUIT_MODEL: |
760 | *data = g_variant_new_string(models[devc->model]); | |
761 | break; | |
6bcb3ee8 | 762 | default: |
6bcb3ee8 JH |
763 | return SR_ERR_NA; |
764 | } | |
765 | ||
766 | return SR_OK; | |
767 | } | |
768 | ||
769 | SR_PRIV int es51919_serial_config_set(uint32_t key, GVariant *data, | |
770 | const struct sr_dev_inst *sdi, | |
771 | const struct sr_channel_group *cg) | |
772 | { | |
773 | struct dev_context *devc; | |
774 | uint64_t val; | |
775 | ||
776 | (void)cg; | |
777 | ||
778 | if (!(devc = sdi->priv)) | |
779 | return SR_ERR_BUG; | |
780 | ||
781 | switch (key) { | |
782 | case SR_CONF_LIMIT_MSEC: | |
783 | val = g_variant_get_uint64(data); | |
784 | dev_time_limit_set(&devc->time_count, val); | |
785 | sr_dbg("Setting time limit to %" PRIu64 ".", val); | |
786 | break; | |
787ec9db | 787 | case SR_CONF_LIMIT_FRAMES: |
6bcb3ee8 | 788 | val = g_variant_get_uint64(data); |
787ec9db JH |
789 | dev_limit_counter_limit_set(&devc->frame_count, val); |
790 | sr_dbg("Setting frame limit to %" PRIu64 ".", val); | |
6bcb3ee8 JH |
791 | break; |
792 | default: | |
793 | sr_spew("%s: Unsupported key %u", __func__, key); | |
794 | return SR_ERR_NA; | |
795 | } | |
796 | ||
797 | return SR_OK; | |
798 | } | |
799 | ||
800 | static const uint32_t scanopts[] = { | |
801 | SR_CONF_CONN, | |
802 | SR_CONF_SERIALCOMM, | |
803 | }; | |
804 | ||
05199c0a | 805 | static const uint32_t drvopts[] = { |
b9a348f5 | 806 | SR_CONF_LCRMETER, |
05199c0a UH |
807 | }; |
808 | ||
809 | static const uint32_t devopts[] = { | |
6bcb3ee8 | 810 | SR_CONF_CONTINUOUS, |
787ec9db | 811 | SR_CONF_LIMIT_FRAMES | SR_CONF_SET, |
6bcb3ee8 JH |
812 | SR_CONF_LIMIT_MSEC | SR_CONF_SET, |
813 | SR_CONF_OUTPUT_FREQUENCY | SR_CONF_GET | SR_CONF_LIST, | |
a42a39ac | 814 | SR_CONF_EQUIV_CIRCUIT_MODEL | SR_CONF_GET | SR_CONF_LIST, |
6bcb3ee8 JH |
815 | }; |
816 | ||
6bcb3ee8 JH |
817 | SR_PRIV int es51919_serial_config_list(uint32_t key, GVariant **data, |
818 | const struct sr_dev_inst *sdi, | |
819 | const struct sr_channel_group *cg) | |
820 | { | |
6bcb3ee8 | 821 | switch (key) { |
e66d1892 UH |
822 | case SR_CONF_SCAN_OPTIONS: |
823 | case SR_CONF_DEVICE_OPTIONS: | |
05199c0a | 824 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); |
6bcb3ee8 | 825 | case SR_CONF_OUTPUT_FREQUENCY: |
b94dd07b | 826 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_DOUBLE, |
53012da6 | 827 | ARRAY_AND_SIZE(frequencies), sizeof(double)); |
6bcb3ee8 | 828 | break; |
a42a39ac | 829 | case SR_CONF_EQUIV_CIRCUIT_MODEL: |
53012da6 | 830 | *data = g_variant_new_strv(ARRAY_AND_SIZE(models)); |
a42a39ac | 831 | break; |
6bcb3ee8 | 832 | default: |
6bcb3ee8 JH |
833 | return SR_ERR_NA; |
834 | } | |
835 | ||
836 | return SR_OK; | |
837 | } | |
838 | ||
695dc859 | 839 | SR_PRIV int es51919_serial_acquisition_start(const struct sr_dev_inst *sdi) |
6bcb3ee8 JH |
840 | { |
841 | struct dev_context *devc; | |
842 | struct sr_serial_dev_inst *serial; | |
843 | ||
6bcb3ee8 JH |
844 | if (!(devc = sdi->priv)) |
845 | return SR_ERR_BUG; | |
846 | ||
787ec9db | 847 | dev_limit_counter_start(&devc->frame_count); |
6bcb3ee8 JH |
848 | dev_time_counter_start(&devc->time_count); |
849 | ||
bee2b016 | 850 | std_session_send_df_header(sdi); |
6bcb3ee8 | 851 | |
6bcb3ee8 JH |
852 | serial = sdi->conn; |
853 | serial_source_add(sdi->session, serial, G_IO_IN, 50, | |
854 | receive_data, (void *)sdi); | |
855 | ||
856 | return SR_OK; | |
857 | } | |
48b7c346 GS |
858 | |
859 | #endif |