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