2 * This file is part of the libsigrok project.
4 * Copyright (C) 2018-2020 Andreas Sandberg <andreas@sandberg.pp.se>
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 #include <libsigrok/libsigrok.h>
28 #include "libsigrok-internal.h"
31 #define SERIAL_WRITE_TIMEOUT_MS 1
33 #define UM_POLL_LEN 130
34 #define UM_POLL_PERIOD_MS 100
35 #define UM_TIMEOUT_MS 1000
37 #define UM_CMD_POLL 0xf0
39 static const struct binary_analog_channel rdtech_default_channels[] = {
40 { "V", { 2, BVT_BE_UINT16, 0.01, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT },
41 { "I", { 4, BVT_BE_UINT16, 0.001, }, 3, SR_MQ_CURRENT, SR_UNIT_AMPERE },
42 { "D+", { 96, BVT_BE_UINT16, 0.01, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT },
43 { "D-", { 98, BVT_BE_UINT16, 0.01, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT },
44 { "T", { 10, BVT_BE_UINT16, 1.0, }, 0, SR_MQ_TEMPERATURE, SR_UNIT_CELSIUS },
45 /* Threshold-based recording (mWh) */
46 { "E", { 106, BVT_BE_UINT32, 0.001, }, 3, SR_MQ_ENERGY, SR_UNIT_WATT_HOUR },
50 static const struct binary_analog_channel rdtech_um25c_channels[] = {
51 { "V", { 2, BVT_BE_UINT16, 0.001, }, 3, SR_MQ_VOLTAGE, SR_UNIT_VOLT },
52 { "I", { 4, BVT_BE_UINT16, 0.0001, }, 4, SR_MQ_CURRENT, SR_UNIT_AMPERE },
53 { "D+", { 96, BVT_BE_UINT16, 0.01, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT },
54 { "D-", { 98, BVT_BE_UINT16, 0.01, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT },
55 { "T", { 10, BVT_BE_UINT16, 1.0, }, 0, SR_MQ_TEMPERATURE, SR_UNIT_CELSIUS },
56 /* Threshold-based recording (mWh) */
57 { "E", { 106, BVT_BE_UINT32, 0.001, }, 3, SR_MQ_ENERGY, SR_UNIT_WATT_HOUR },
61 static gboolean csum_ok_fff1(const uint8_t *buf, size_t len)
65 if (len != UM_POLL_LEN)
68 csum_recv = read_u16be(&buf[len - sizeof(uint16_t)]);
69 if (csum_recv != 0xfff1)
75 static gboolean csum_ok_um34c(const uint8_t *buf, size_t len)
77 static const int positions[] = {
78 1, 3, 7, 9, 15, 17, 19, 23, 31, 39, 41, 45, 49, 53,
79 55, 57, 59, 63, 67, 69, 73, 79, 83, 89, 97, 99, 109,
80 111, 113, 119, 121, 127,
84 uint8_t csum_calc, csum_recv;
86 if (len != UM_POLL_LEN)
90 for (i = 0; i < ARRAY_SIZE(positions); i++)
91 csum_calc ^= buf[positions[i]];
92 csum_recv = read_u8(&buf[len - sizeof(uint8_t)]);
93 if (csum_recv != csum_calc)
99 static const struct rdtech_um_profile um_profiles[] = {
100 { "UM24C", RDTECH_UM24C, rdtech_default_channels, csum_ok_fff1, },
101 { "UM25C", RDTECH_UM25C, rdtech_um25c_channels, csum_ok_fff1, },
102 { "UM34C", RDTECH_UM34C, rdtech_default_channels, csum_ok_um34c, },
105 static const struct rdtech_um_profile *find_profile(uint16_t id)
109 for (i = 0; i < ARRAY_SIZE(um_profiles); i++) {
110 if (um_profiles[i].model_id == id)
111 return &um_profiles[i];
116 SR_PRIV const struct rdtech_um_profile *rdtech_um_probe(struct sr_serial_dev_inst *serial)
118 const struct rdtech_um_profile *p;
120 uint8_t buf[RDTECH_UM_BUFSIZE];
124 request = UM_CMD_POLL;
125 if (serial_write_blocking(serial, &request, sizeof(request),
126 SERIAL_WRITE_TIMEOUT_MS) < 0) {
127 sr_err("Unable to send probe request.");
131 len = serial_read_blocking(serial, buf, UM_POLL_LEN, UM_TIMEOUT_MS);
132 if (len != UM_POLL_LEN) {
133 sr_err("Failed to read probe response.");
137 model_id = read_u16be(&buf[0]);
138 p = find_profile(model_id);
140 sr_err("Unrecognized UM device (0x%.4" PRIx16 ").", model_id);
144 if (!p->csum_ok(buf, len)) {
145 sr_err("Probe response fails checksum verification.");
152 SR_PRIV int rdtech_um_poll(const struct sr_dev_inst *sdi, gboolean force)
154 struct dev_context *devc;
155 int64_t now, elapsed;
156 struct sr_serial_dev_inst *serial;
159 /* Check for expired intervals or forced requests. */
161 now = g_get_monotonic_time() / 1000;
162 elapsed = now - devc->cmd_sent_at;
163 if (!force && elapsed < UM_POLL_PERIOD_MS)
166 /* Send another poll request. Update interval only on success. */
168 request = UM_CMD_POLL;
169 if (serial_write_blocking(serial, &request, sizeof(request),
170 SERIAL_WRITE_TIMEOUT_MS) < 0) {
171 sr_err("Unable to send poll request.");
174 devc->cmd_sent_at = now;
179 static void handle_poll_data(const struct sr_dev_inst *sdi)
181 struct dev_context *devc;
186 sr_spew("Received poll packet (len: %zu).", devc->buflen);
187 if (devc->buflen != UM_POLL_LEN) {
188 sr_err("Unexpected poll packet length: %zu", devc->buflen);
192 for (ch = sdi->channels, i = 0; ch; ch = g_slist_next(ch), i++) {
193 bv_send_analog_channel(sdi, ch->data,
194 &devc->profile->channels[i],
195 devc->buf, devc->buflen);
198 sr_sw_limits_update_samples_read(&devc->limits, 1);
201 static void recv_poll_data(struct sr_dev_inst *sdi, struct sr_serial_dev_inst *serial)
203 struct dev_context *devc;
204 const struct rdtech_um_profile *p;
207 /* Serial data arrived. */
210 while (devc->buflen < UM_POLL_LEN) {
211 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, 1);
216 /* Check if the poll model ID matches the profile. */
217 if (devc->buflen == 2 && RB16(devc->buf) != p->model_id) {
218 sr_warn("Illegal model ID in poll response (0x%.4" PRIx16 "),"
222 memmove(devc->buf, devc->buf + 1, devc->buflen);
226 if (devc->buflen != UM_POLL_LEN)
227 sr_warn("Skipping packet, unexpected receive length.");
228 else if (!p->csum_ok(devc->buf, devc->buflen))
229 sr_warn("Skipping packet, checksum verification failed.");
231 handle_poll_data(sdi);
235 SR_PRIV int rdtech_um_receive_data(int fd, int revents, void *cb_data)
237 struct sr_dev_inst *sdi;
238 struct dev_context *devc;
239 struct sr_serial_dev_inst *serial;
243 if (!(sdi = cb_data))
246 if (!(devc = sdi->priv))
250 if (revents == G_IO_IN)
251 recv_poll_data(sdi, serial);
253 if (sr_sw_limits_check(&devc->limits)) {
254 sr_dev_acquisition_stop(sdi);
258 (void)rdtech_um_poll(sdi, FALSE);