]> sigrok.org Git - libsigrok.git/blame - src/hardware/zketech-ebd-usb/protocol.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / src / hardware / zketech-ebd-usb / protocol.c
CommitLineData
c527132a
SBO
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2018 Sven Bursch-Osewold <sb_git@bursch.com>
ab3e9c8a 5 * Copyright (C) 2019 King Kévin <kingkevin@cuvoodoo.info>
c527132a
SBO
6 *
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.
11 *
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.
16 *
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/>.
19 */
20
21#include <config.h>
22#include "protocol.h"
23
cb8a0efc
UH
24/* Log a byte-array as hex values. */
25static void log_buf(const char *message, uint8_t buf[], size_t count)
26{
27 char buffer[count * 2 + 1];
28
b29f7be9 29 for (size_t i = 0; i < count; i++) {
4df6f174
FS
30 sprintf(&buffer[2 * i], "%02X", buf[i]);
31 }
cb8a0efc
UH
32
33 buffer[count * 2] = 0;
34
b8a954c5 35 sr_dbg("%s: %s [%zu bytes]", message, buffer, count);
cb8a0efc
UH
36}
37
9890fb1f 38/* Send a command to the device. */
4df6f174
FS
39static int send_cmd(struct sr_serial_dev_inst *serial, uint8_t buf[],
40 size_t count)
9890fb1f
SBO
41{
42 int ret;
43
cb8a0efc 44 log_buf("Sending", buf, count);
ab3e9c8a
KK
45 for (size_t byte = 0; byte < count; byte++) {
46 ret = serial_write_blocking(serial, &buf[byte], 1, 0);
47 if (ret < 0) {
48 sr_err("Error sending command: %d.", ret);
49 return ret;
50 }
4df6f174
FS
51 /*
52 * Wait between bytes to prevent data loss at the receiving
53 * side.
54 */
55 g_usleep(10000);
9890fb1f 56 }
9890fb1f
SBO
57
58 return (ret == (int)count) ? SR_OK : SR_ERR;
59}
60
cb8a0efc
UH
61/* Decode high byte and low byte into a float. */
62static float decode_value(uint8_t hi, uint8_t lo, float divisor)
63{
64 return ((float)hi * 240.0 + (float)lo) / divisor;
65}
66
67/* Encode a float into high byte and low byte. */
68static void encode_value(float current, uint8_t *hi, uint8_t *lo, float divisor)
69{
70 int value;
71
72 value = (int)(current * divisor);
73 sr_dbg("Value %d %d %d", value, value / 240, value % 240);
74 *hi = value / 240;
75 *lo = value % 240;
76}
77
78/* Send updated configuration values to the load. */
79static int send_cfg(struct sr_serial_dev_inst *serial, struct dev_context *devc)
80{
b29f7be9 81 uint8_t send[] = {0xfa, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8};
cb8a0efc
UH
82
83 encode_value(devc->current_limit, &send[2], &send[3], 1000.0);
4df6f174 84 encode_value(devc->uvc_threshold, &send[4], &send[5], 100.0);
cb8a0efc 85
b29f7be9 86 send[8] = send[1] ^ send[2] ^ send[3] ^ send[4] ^ send[5] ^ send[6] ^ send[7];
cb8a0efc
UH
87
88 return send_cmd(serial, send, 10);
89}
90
9890fb1f 91/* Send the init/connect sequence; drive starts sending voltage and current. */
330a32b2 92SR_PRIV int ebd_init(struct sr_serial_dev_inst *serial, struct dev_context *devc)
9890fb1f 93{
b29f7be9 94 uint8_t init[] = { 0xfa, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0xf8 };
9890fb1f
SBO
95
96 (void)devc;
97
4df6f174 98 return send_cmd(serial, init, 10);
9890fb1f
SBO
99}
100
101/* Start the load functionality. */
4df6f174
FS
102SR_PRIV int ebd_loadstart(struct sr_serial_dev_inst *serial,
103 struct dev_context *devc)
9890fb1f 104{
9890fb1f 105 int ret;
b29f7be9 106 uint8_t start[] = { 0xfa, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xf8 };
9890fb1f 107
ab3e9c8a 108 encode_value(devc->current_limit, &start[2], &start[3], 1000.0);
4df6f174 109 encode_value(devc->uvc_threshold, &start[4], &start[5], 100.0);
ab3e9c8a 110
b29f7be9 111 start[8] = start[1] ^ start[2] ^ start[3] ^ start[4] ^ start[5] ^ start[6] ^ start[7];
ab3e9c8a
KK
112
113 sr_info("Activating load");
cb8a0efc 114 ret = send_cmd(serial, start, 10);
b41562f6
FR
115 if (ret)
116 return ret;
117
ab3e9c8a 118 sr_dbg("current limit: %.03f", devc->current_limit);
4df6f174 119 sr_dbg("under-voltage threshold: %.02f", devc->uvc_threshold);
330a32b2 120 if (ebd_current_is0(devc))
9890fb1f
SBO
121 return SR_OK;
122
9890fb1f
SBO
123 return ret;
124}
125
ab3e9c8a 126/* Toggle the load functionality. */
4df6f174
FS
127SR_PRIV int ebd_loadtoggle(struct sr_serial_dev_inst *serial,
128 struct dev_context *devc)
9890fb1f 129{
b29f7be9 130 uint8_t toggle[] = { 0xfa, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xF8 };
9890fb1f 131
b29f7be9 132 (void)devc;
ab3e9c8a
KK
133
134 sr_info("Toggling load");
4df6f174 135 return send_cmd(serial, toggle, 10);
9890fb1f
SBO
136}
137
138/* Stop the drive. */
330a32b2 139SR_PRIV int ebd_stop(struct sr_serial_dev_inst *serial, struct dev_context *devc)
9890fb1f 140{
b29f7be9 141 uint8_t stop[] = { 0xfa, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0xF8 };
9890fb1f 142
b29f7be9 143 (void)devc;
9890fb1f 144
4df6f174 145 return send_cmd(serial, stop, 10);
9890fb1f
SBO
146}
147
4df6f174
FS
148/**
149 * Receive a complete message.
150 *
151 * @param[in] serial Serial port from which to read the packet
152 * @param[in] length Buffer length
153 * @param[out] buf Buffer to write packet to
154 *
155 * @return packet length (0 = timeout, -1 = error)
ab3e9c8a 156 */
4df6f174
FS
157SR_PRIV int ebd_read_message(struct sr_serial_dev_inst *serial, size_t length,
158 uint8_t *buf)
9890fb1f 159{
4df6f174
FS
160 int ret;
161 gboolean message_complete;
162 size_t turn, max_turns;
163 size_t message_length;
164
165 /* Check parameters. */
166 if (serial == NULL) {
ab3e9c8a
KK
167 sr_err("Serial device to receive packet missing.");
168 return -1;
169 }
170 if (length < 3) {
171 sr_err("Packet buffer not large enough.");
172 return -1;
173 }
4df6f174 174 if (buf == NULL) {
ab3e9c8a
KK
175 sr_err("Packet buffer missing.");
176 return -1;
177 }
9890fb1f 178
4df6f174
FS
179 message_complete = FALSE;
180 turn = 0;
181 max_turns = 200;
182 message_length = 0;
ab3e9c8a 183 buf[message_length] = 0;
4df6f174
FS
184
185 /* Try to read data. */
186 while (!message_complete && turn < max_turns) {
187 /* Wait for header byte. */
ab3e9c8a 188 message_length = 0;
b29f7be9
SA
189 while ((buf[0] != MSG_FRAME_BEGIN) && (turn < max_turns)) {
190 ret = serial_read_blocking(serial, &buf[0], 1, serial_timeout(serial, 1));
ab3e9c8a
KK
191 if (ret < 0) {
192 sr_err("Error %d reading byte.", ret);
193 return ret;
4df6f174
FS
194 } else if (ret == 1) {
195 if (buf[message_length] != MSG_FRAME_BEGIN) {
196 sr_warn("Not frame begin byte %02x received",
197 buf[message_length]);
ab3e9c8a 198 } else {
4df6f174
FS
199 sr_dbg("Message header received: %02x",
200 buf[message_length]);
ab3e9c8a
KK
201 message_length += ret;
202 }
203 }
204 turn++;
205 }
4df6f174 206 /* Read until end byte. */
b29f7be9
SA
207 while ((buf[message_length - 1] != MSG_FRAME_END)
208 && (message_length < length) && (turn < max_turns)) {
4df6f174 209
b29f7be9 210 ret = serial_read_blocking(serial, &buf[message_length], 1, serial_timeout(serial, 1));
ab3e9c8a
KK
211 if (ret < 0) {
212 sr_err("Error %d reading byte.", ret);
213 return ret;
4df6f174
FS
214 } else if (ret == 1) {
215 if (buf[message_length] == MSG_FRAME_BEGIN) {
ab3e9c8a
KK
216 sr_warn("Frame begin before end received");
217 message_length = 1;
218 } else {
4df6f174
FS
219 sr_dbg("Message data received: %02x",
220 buf[message_length]);
ab3e9c8a
KK
221 message_length += ret;
222 }
223 }
224 turn++;
9890fb1f 225 }
4df6f174
FS
226 /* Verify frame. */
227 if (turn < max_turns) {
228 if (buf[message_length - 1] == MSG_FRAME_END) {
ab3e9c8a
KK
229 message_complete = TRUE;
230 sr_dbg("Message end received");
231 } else {
232 sr_warn("Frame end not received");
233 }
234 } else {
235 sr_warn("Invalid data and timeout");
236 }
237 }
9890fb1f 238
ab3e9c8a
KK
239 if (message_complete && message_length > 2) {
240 ret = message_length;
4df6f174 241 } else if (turn >= max_turns) {
ab3e9c8a
KK
242 ret = 0;
243 } else {
244 ret = -1;
245 }
246 return ret;
247}
248
249static void ebd_send_value(const struct sr_dev_inst *sdi, struct sr_channel *ch,
4df6f174 250 float value, enum sr_mq mq, enum sr_unit unit, int digits)
ab3e9c8a
KK
251{
252 struct sr_datafeed_packet packet;
253 struct sr_datafeed_analog analog;
254 struct sr_analog_encoding encoding;
255 struct sr_analog_meaning meaning;
256 struct sr_analog_spec spec;
9890fb1f 257
ab3e9c8a
KK
258 sr_analog_init(&analog, &encoding, &meaning, &spec, digits);
259 analog.meaning->channels = g_slist_append(NULL, ch);
260 analog.num_samples = 1;
261 analog.data = &value;
262 analog.meaning->mq = mq;
263 analog.meaning->unit = unit;
264 analog.meaning->mqflags = SR_MQFLAG_DC;
265
266 packet.type = SR_DF_ANALOG;
267 packet.payload = &analog;
268 sr_session_send(sdi, &packet);
269 g_slist_free(analog.meaning->channels);
9890fb1f
SBO
270}
271
330a32b2 272SR_PRIV int ebd_receive_data(int fd, int revents, void *cb_data)
c527132a 273{
9890fb1f 274 struct sr_dev_inst *sdi;
c527132a 275 struct dev_context *devc;
9890fb1f 276 struct sr_serial_dev_inst *serial;
4df6f174
FS
277 float current, current_limit;
278 float voltage, voltage_dp, voltage_dm, uvc_threshold;
279 uint8_t reply[MSG_MAX_LEN];
280 int ret, i;
281 uint8_t checksum;
c527132a 282
9890fb1f 283 (void)revents;
c527132a
SBO
284 (void)fd;
285
286 if (!(sdi = cb_data))
9890fb1f 287 return FALSE;
c527132a
SBO
288
289 if (!(devc = sdi->priv))
9890fb1f
SBO
290 return FALSE;
291
292 serial = sdi->conn;
ab3e9c8a 293 current_limit = devc->current_limit;
4df6f174 294 uvc_threshold = devc->uvc_threshold;
9890fb1f 295
4df6f174 296 ret = ebd_read_message(serial, MSG_MAX_LEN, reply);
9890fb1f
SBO
297
298 /* Tests for correct message. */
4df6f174 299 if (ret == -1) {
ab3e9c8a
KK
300 sr_err("Can't receive messages");
301 return SR_ERR;
4df6f174 302 } else if (ret == 0) {
ab3e9c8a
KK
303 sr_err("No messages received");
304 devc->running = FALSE;
305 return 0;
b29f7be9
SA
306 } else if ((ret != 19) ||
307 ((reply[1] != 0x00) && (reply[1] != 0x0a) && (reply[1] != 0x64) && (reply[1] != 0x6e))) {
4df6f174 308
ab3e9c8a
KK
309 sr_info("Not measurement message received");
310 return ret;
9890fb1f 311 }
c527132a 312
ab3e9c8a 313 /* Verify checksum */
4df6f174
FS
314 checksum = 0;
315 for (i = 1; i < ret - 1; i++) {
ab3e9c8a
KK
316 checksum ^= reply[i];
317 }
4df6f174 318 if (checksum != 0) {
ab3e9c8a 319 sr_warn("Invalid checksum");
4df6f174
FS
320 /* Don't exit on wrong checksum, the device can recover */
321 return ret;
ab3e9c8a 322 }
9890fb1f 323
ab3e9c8a 324 devc->running = TRUE;
b29f7be9 325 if ((reply[1] == 0x00) || (reply[1] == 0x64))
ab3e9c8a 326 devc->load_activated = FALSE;
b29f7be9 327 else if ((reply[1] == 0x0a) || (reply[1] == 0x6e))
ab3e9c8a 328 devc->load_activated = TRUE;
c527132a 329
9890fb1f 330 /* Calculate values. */
cb8a0efc 331 current = decode_value(reply[2], reply[3], 10000.0);
ab3e9c8a
KK
332 voltage = decode_value(reply[4], reply[5], 1000.0);
333 voltage_dp = decode_value(reply[6], reply[7], 1000.0);
334 voltage_dm = decode_value(reply[8], reply[9], 1000.0);
4df6f174 335 if (reply[1] == 0x0a) {
ab3e9c8a 336 current_limit = decode_value(reply[10], reply[11], 1000.0);
4df6f174 337 uvc_threshold = decode_value(reply[12], reply[13], 100.0);
ab3e9c8a 338 }
9890fb1f 339
ab3e9c8a
KK
340 sr_dbg("VBUS current %.04f A", current);
341 sr_dbg("VBUS voltage %.03f V", voltage);
342 sr_dbg("D+ voltage %.03f V", voltage_dp);
343 sr_dbg("D- voltage %.03f V", voltage_dm);
4df6f174 344 if (reply[1] == 0x0a) {
ab3e9c8a 345 sr_dbg("Current limit %.03f A", current_limit);
4df6f174 346 sr_dbg("UVC threshold %.03f V", uvc_threshold);
ab3e9c8a
KK
347 }
348
4df6f174 349 /* Update load state. */
ab3e9c8a
KK
350 if (devc->load_activated && ebd_current_is0(devc)) {
351 ebd_loadtoggle(serial, devc);
352 } else if (!devc->load_activated && !ebd_current_is0(devc)) {
353 ebd_loadstart(serial, devc);
4df6f174 354 } else if (devc->load_activated &&
b29f7be9 355 ((current_limit != devc->current_limit) || (uvc_threshold != devc->uvc_threshold))) {
4df6f174
FS
356
357 sr_dbg("Adjusting limit from %.03f A %.03f V to %.03f A %.03f V",
358 current_limit, uvc_threshold, devc->current_limit,
359 devc->uvc_threshold);
ab3e9c8a
KK
360 send_cfg(serial, devc);
361 }
9890fb1f
SBO
362
363 /* Begin frame. */
4c5f7006 364 std_session_send_df_frame_begin(sdi);
9890fb1f 365
ab3e9c8a 366 /* Values */
4df6f174 367 ebd_send_value(sdi, sdi->channels->data, voltage,
ab3e9c8a 368 SR_MQ_VOLTAGE, SR_UNIT_VOLT, 3);
4df6f174 369 ebd_send_value(sdi, sdi->channels->next->data, current,
ab3e9c8a
KK
370 SR_MQ_CURRENT, SR_UNIT_AMPERE, 4);
371 ebd_send_value(sdi, sdi->channels->next->next->data, voltage_dp,
372 SR_MQ_VOLTAGE, SR_UNIT_VOLT, 3);
373 ebd_send_value(sdi, sdi->channels->next->next->next->data, voltage_dm,
374 SR_MQ_VOLTAGE, SR_UNIT_VOLT, 3);
9890fb1f
SBO
375
376 /* End frame. */
4c5f7006 377 std_session_send_df_frame_end(sdi);
9890fb1f
SBO
378
379 sr_sw_limits_update_samples_read(&devc->limits, 1);
380 if (sr_sw_limits_check(&devc->limits))
381 sr_dev_acquisition_stop(sdi);
382
c527132a
SBO
383 return TRUE;
384}
9890fb1f 385
330a32b2 386SR_PRIV int ebd_get_current_limit(const struct sr_dev_inst *sdi, float *current)
9890fb1f
SBO
387{
388 struct dev_context *devc;
389
390 if (!(devc = sdi->priv))
391 return SR_ERR;
392
393 g_mutex_lock(&devc->rw_mutex);
394 *current = devc->current_limit;
395 g_mutex_unlock(&devc->rw_mutex);
396
397 return SR_OK;
398}
399
330a32b2 400SR_PRIV int ebd_set_current_limit(const struct sr_dev_inst *sdi, float current)
9890fb1f
SBO
401{
402 struct dev_context *devc;
403 int ret;
404
405 if (!(devc = sdi->priv))
406 return SR_ERR;
407
408 g_mutex_lock(&devc->rw_mutex);
409 devc->current_limit = current;
410
411 if (!devc->running) {
412 sr_dbg("Setting current limit later.");
413 g_mutex_unlock(&devc->rw_mutex);
414 return SR_OK;
415 }
416
417 sr_dbg("Setting current limit to %fV.", current);
418
419 if (devc->load_activated) {
330a32b2 420 if (ebd_current_is0(devc)) {
9890fb1f 421 /* Stop load. */
ab3e9c8a
KK
422 ret = ebd_loadtoggle(sdi->conn, devc);
423 } else {
424 /* Send new current. */
425 ret = send_cfg(sdi->conn, devc);
426 }
427 } else {
428 if (ebd_current_is0(devc)) {
429 /* Nothing to do. */
430 ret = SR_OK;
431 } else {
432 /* Start load. */
433 ret = ebd_loadstart(sdi->conn, devc);
434 }
435 }
436
437 g_mutex_unlock(&devc->rw_mutex);
438
439 return ret;
440}
441
4df6f174 442SR_PRIV int ebd_get_uvc_threshold(const struct sr_dev_inst *sdi, float *voltage)
ab3e9c8a
KK
443{
444 struct dev_context *devc;
445
446 if (!(devc = sdi->priv))
447 return SR_ERR;
448
449 g_mutex_lock(&devc->rw_mutex);
4df6f174 450 *voltage = devc->uvc_threshold;
ab3e9c8a
KK
451 g_mutex_unlock(&devc->rw_mutex);
452
453 return SR_OK;
454}
455
4df6f174 456SR_PRIV int ebd_set_uvc_threshold(const struct sr_dev_inst *sdi, float voltage)
ab3e9c8a
KK
457{
458 struct dev_context *devc;
459 int ret;
460
461 if (!(devc = sdi->priv))
462 return SR_ERR;
463
464 g_mutex_lock(&devc->rw_mutex);
4df6f174 465 devc->uvc_threshold = voltage;
ab3e9c8a
KK
466
467 if (!devc->running) {
4df6f174 468 sr_dbg("Setting uvc threshold later.");
ab3e9c8a
KK
469 g_mutex_unlock(&devc->rw_mutex);
470 return SR_OK;
471 }
472
4df6f174 473 sr_dbg("Setting uvc threshold to %fV.", voltage);
ab3e9c8a
KK
474
475 if (devc->load_activated) {
476 if (ebd_current_is0(devc)) {
477 /* Stop load. */
478 ret = ebd_loadtoggle(sdi->conn, devc);
9890fb1f
SBO
479 } else {
480 /* Send new current. */
cb8a0efc 481 ret = send_cfg(sdi->conn, devc);
9890fb1f
SBO
482 }
483 } else {
330a32b2 484 if (ebd_current_is0(devc)) {
9890fb1f
SBO
485 /* Nothing to do. */
486 ret = SR_OK;
487 } else {
488 /* Start load. */
330a32b2 489 ret = ebd_loadstart(sdi->conn, devc);
9890fb1f
SBO
490 }
491 }
492
493 g_mutex_unlock(&devc->rw_mutex);
494
495 return ret;
496}
497
330a32b2 498SR_PRIV gboolean ebd_current_is0(struct dev_context *devc)
9890fb1f
SBO
499{
500 return devc->current_limit < 0.001;
501}