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