2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Janne Huttunen <jahuttun@gmail.com>
5 * Copyright (C) 2019 Gerhard Sittig <gerhard.sittig@gmx.net>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <libsigrok/libsigrok.h>
24 #include "libsigrok-internal.h"
29 #define LOG_PREFIX "es51919"
31 #ifdef HAVE_SERIAL_COMM
34 * Cyrustek ES51919 LCR chipset host protocol.
36 * Public official documentation does not contain the protocol
37 * description, so this is all based on reverse engineering.
39 * Packet structure (17 bytes):
41 * 0x00: header1 ?? (0x00)
42 * 0x01: header2 ?? (0x0d)
45 * bit 0 = hold enabled
46 * bit 1 = reference shown (in delta mode)
48 * bit 3 = calibration mode
49 * bit 4 = sorting mode
52 * bit 7 = parallel measurement (vs. serial)
55 * bit 0-4 = ??? (0x10)
56 * bit 5-7 = test frequency
64 * 0x04: tolerance (sorting mode)
75 * 0x05-0x09: primary measurement
76 * 0x05: measured quantity
81 * 0x06: measurement MSB (0x4e20 = 20000 = outside limits)
82 * 0x07: measurement LSB
83 * 0x08: measurement info
84 * bit 0-2 = decimal point multiplier (10^-val)
100 * 0x09: measurement status
102 * 0 = normal (measurement shown)
103 * 1 = blank (nothing shown)
105 * 3 = outside limits ("OL")
109 * 10 = shorted ("Srt")
110 * bit 4-6 = ??? (maybe part of same field with 0-3)
111 * bit 7 = ??? (some independent flag)
113 * 0x0a-0x0e: secondary measurement
114 * 0x0a: measured quantity
116 * 1 = dissipation factor
118 * 3 = parallel AC resistance / ESR
120 * 0x0b-0x0e: like primary measurement
122 * 0x0f: footer1 (0x0d) ?
123 * 0x10: footer2 (0x0a) ?
126 static const double frequencies[] = {
127 SR_HZ(0), SR_HZ(100), SR_HZ(120),
128 SR_KHZ(1), SR_KHZ(10), SR_KHZ(100),
131 static const size_t freq_code_map[] = {
135 static uint64_t get_frequency(size_t code)
139 if (code >= ARRAY_SIZE(freq_code_map)) {
140 sr_err("Unknown output frequency code %zu.", code);
141 return frequencies[0];
144 code = freq_code_map[code];
145 freq = frequencies[code];
150 enum { MODEL_NONE, MODEL_PAR, MODEL_SER, MODEL_AUTO, };
152 static const char *const circuit_models[] = {
153 "NONE", "PARALLEL", "SERIES", "AUTO",
156 static const char *get_equiv_model(size_t code)
158 if (code >= ARRAY_SIZE(circuit_models)) {
159 sr_err("Unknown equivalent circuit model code %zu.", code);
163 return circuit_models[code];
166 static const uint8_t *pkt_to_buf(const uint8_t *pkt, int is_secondary)
168 return is_secondary ? pkt + 10 : pkt + 5;
171 static int parse_mq(const uint8_t *pkt, int is_secondary, int is_parallel)
175 buf = pkt_to_buf(pkt, is_secondary);
177 switch (is_secondary << 8 | buf[0]) {
180 SR_MQ_PARALLEL_INDUCTANCE : SR_MQ_SERIES_INDUCTANCE;
183 SR_MQ_PARALLEL_CAPACITANCE : SR_MQ_SERIES_CAPACITANCE;
187 SR_MQ_PARALLEL_RESISTANCE : SR_MQ_SERIES_RESISTANCE;
189 return SR_MQ_RESISTANCE;
191 return SR_MQ_DIFFERENCE;
193 return SR_MQ_DISSIPATION_FACTOR;
195 return SR_MQ_QUALITY_FACTOR;
197 return SR_MQ_PHASE_ANGLE;
200 sr_err("Unknown quantity 0x%03x.", is_secondary << 8 | buf[0]);
205 static float parse_value(const uint8_t *buf, int *digits)
207 static const int exponents[] = {0, -1, -2, -3, -4, -5, -6, -7};
213 exponent = exponents[buf[3] & 7];
215 val = (buf[1] << 8) | buf[2];
217 fval *= powf(10, exponent);
222 static void parse_measurement(const uint8_t *pkt, float *floatval,
223 struct sr_datafeed_analog *analog, int is_secondary)
225 static const struct {
229 { SR_UNIT_UNITLESS, 0 }, /* no unit */
230 { SR_UNIT_OHM, 0 }, /* Ohm */
231 { SR_UNIT_OHM, 3 }, /* kOhm */
232 { SR_UNIT_OHM, 6 }, /* MOhm */
234 { SR_UNIT_HENRY, -6 }, /* uH */
235 { SR_UNIT_HENRY, -3 }, /* mH */
236 { SR_UNIT_HENRY, 0 }, /* H */
237 { SR_UNIT_HENRY, 3 }, /* kH */
238 { SR_UNIT_FARAD, -12 }, /* pF */
239 { SR_UNIT_FARAD, -9 }, /* nF */
240 { SR_UNIT_FARAD, -6 }, /* uF */
241 { SR_UNIT_FARAD, -3 }, /* mF */
242 { SR_UNIT_PERCENTAGE, 0 }, /* % */
243 { SR_UNIT_DEGREE, 0 }, /* degree */
247 int digits, exponent;
250 buf = pkt_to_buf(pkt, is_secondary);
252 analog->meaning->mq = 0;
253 analog->meaning->mqflags = 0;
255 state = buf[4] & 0xf;
257 if (state != 0 && state != 3)
261 /* Calibration and Sorting modes not supported. */
267 analog->meaning->mqflags |= SR_MQFLAG_HOLD;
269 analog->meaning->mqflags |= SR_MQFLAG_REFERENCE;
272 analog->meaning->mqflags |= SR_MQFLAG_RELATIVE;
275 if ((analog->meaning->mq = parse_mq(pkt, is_secondary, pkt[2] & 0x80)) == 0)
278 if ((buf[3] >> 3) >= ARRAY_SIZE(units)) {
279 sr_err("Unknown unit %u.", buf[3] >> 3);
280 analog->meaning->mq = 0;
284 analog->meaning->unit = units[buf[3] >> 3].unit;
286 exponent = units[buf[3] >> 3].exponent;
287 *floatval = parse_value(buf, &digits);
288 *floatval *= (state == 0) ? powf(10, exponent) : INFINITY;
289 analog->encoding->digits = digits - exponent;
290 analog->spec->spec_digits = digits - exponent;
293 static uint64_t parse_freq(const uint8_t *pkt)
295 return get_frequency(pkt[3] >> 5);
298 static const char *parse_model(const uint8_t *pkt)
304 else if (parse_mq(pkt, 0, 0) == SR_MQ_RESISTANCE)
307 code = (pkt[2] & 0x80) ? MODEL_PAR : MODEL_SER;
309 return get_equiv_model(code);
312 SR_PRIV gboolean es51919_packet_valid(const uint8_t *pkt)
315 /* Check for fixed 0x00 0x0d prefix. */
316 if (pkt[0] != 0x00 || pkt[1] != 0x0d)
319 /* Check for fixed 0x0d 0x0a suffix. */
320 if (pkt[15] != 0x0d || pkt[16] != 0x0a)
323 /* Packet appears to be valid. */
327 SR_PRIV int es51919_packet_parse(const uint8_t *pkt, float *val,
328 struct sr_datafeed_analog *analog, void *info)
330 struct lcr_parse_info *parse_info;
333 if (!parse_info->ch_idx) {
334 parse_info->output_freq = parse_freq(pkt);
335 parse_info->circuit_model = parse_model(pkt);
338 parse_measurement(pkt, val, analog, parse_info->ch_idx == 1);
344 * These are the get/set/list routines for the _chip_ specific parameters,
345 * the _device_ driver resides in src/hardware/serial-lcr/ instead.
348 SR_PRIV int es51919_config_list(uint32_t key, GVariant **data,
349 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
356 case SR_CONF_OUTPUT_FREQUENCY:
357 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_DOUBLE,
358 ARRAY_AND_SIZE(frequencies), sizeof(frequencies[0]));
360 case SR_CONF_EQUIV_CIRCUIT_MODEL:
361 *data = g_variant_new_strv(ARRAY_AND_SIZE(circuit_models));