2 * This file is part of the libsigrok project.
4 * Copyright (C) 2018 Sven Bursch-Osewold <sb_git@bursch.com>
5 * Copyright (C) 2019 King Kévin <kingkevin@cuvoodoo.info>
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/>.
24 /* Log a byte-array as hex values. */
25 static void log_buf(const char *message, uint8_t buf[], size_t count)
28 char buffer[count * 2 + 1];
30 for (i = 0; i < count; i++) {
31 sprintf(&buffer[2 * i], "%02X", buf[i]);
34 buffer[count * 2] = 0;
36 sr_dbg("%s: %s [%zu bytes]", message, buffer, count);
39 /* Send a command to the device. */
40 static int send_cmd(struct sr_serial_dev_inst *serial, uint8_t buf[],
45 log_buf("Sending", buf, count);
46 for (size_t byte = 0; byte < count; byte++) {
47 ret = serial_write_blocking(serial, &buf[byte], 1, 0);
49 sr_err("Error sending command: %d.", ret);
53 * Wait between bytes to prevent data loss at the receiving
59 return (ret == (int)count) ? SR_OK : SR_ERR;
62 /* Decode high byte and low byte into a float. */
63 static float decode_value(uint8_t hi, uint8_t lo, float divisor)
65 return ((float)hi * 240.0 + (float)lo) / divisor;
68 /* Encode a float into high byte and low byte. */
69 static void encode_value(float current, uint8_t *hi, uint8_t *lo, float divisor)
73 value = (int)(current * divisor);
74 sr_dbg("Value %d %d %d", value, value / 240, value % 240);
79 /* Send updated configuration values to the load. */
80 static int send_cfg(struct sr_serial_dev_inst *serial, struct dev_context *devc)
83 { 0xfa, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8 };
85 encode_value(devc->current_limit, &send[2], &send[3], 1000.0);
86 encode_value(devc->uvc_threshold, &send[4], &send[5], 100.0);
88 send[8] = send[1] ^ send[2] ^ send[3] ^ send[4] ^ send[5]
91 return send_cmd(serial, send, 10);
94 /* Send the init/connect sequence; drive starts sending voltage and current. */
95 SR_PRIV int ebd_init(struct sr_serial_dev_inst *serial, struct dev_context *devc)
98 { 0xfa, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0xf8 };
102 return send_cmd(serial, init, 10);
105 /* Start the load functionality. */
106 SR_PRIV int ebd_loadstart(struct sr_serial_dev_inst *serial,
107 struct dev_context *devc)
111 { 0xfa, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xf8 };
113 encode_value(devc->current_limit, &start[2], &start[3], 1000.0);
114 encode_value(devc->uvc_threshold, &start[4], &start[5], 100.0);
116 start[8] = start[1] ^ start[2] ^ start[3] ^ start[4] ^ start[5]
117 ^ start[6] ^ start[7];
119 sr_info("Activating load");
120 ret = send_cmd(serial, start, 10);
124 sr_dbg("current limit: %.03f", devc->current_limit);
125 sr_dbg("under-voltage threshold: %.02f", devc->uvc_threshold);
126 if (ebd_current_is0(devc))
132 /* Toggle the load functionality. */
133 SR_PRIV int ebd_loadtoggle(struct sr_serial_dev_inst *serial,
134 struct dev_context *devc)
137 { 0xfa, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xF8 };
141 sr_info("Toggling load");
142 return send_cmd(serial, toggle, 10);
145 /* Stop the drive. */
146 SR_PRIV int ebd_stop(struct sr_serial_dev_inst *serial, struct dev_context *devc)
149 { 0xfa, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0xF8 };
153 return send_cmd(serial, stop, 10);
157 * Receive a complete message.
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
163 * @return packet length (0 = timeout, -1 = error)
165 SR_PRIV int ebd_read_message(struct sr_serial_dev_inst *serial, size_t length,
169 gboolean message_complete;
170 size_t turn, max_turns;
171 size_t message_length;
173 /* Check parameters. */
174 if (serial == NULL) {
175 sr_err("Serial device to receive packet missing.");
179 sr_err("Packet buffer not large enough.");
183 sr_err("Packet buffer missing.");
187 message_complete = FALSE;
191 buf[message_length] = 0;
193 /* Try to read data. */
194 while (!message_complete && turn < max_turns) {
195 /* Wait for header byte. */
197 while (buf[0] != MSG_FRAME_BEGIN && turn < max_turns) {
198 ret = serial_read_blocking(serial, &buf[0], 1,
199 serial_timeout(serial, 1));
201 sr_err("Error %d reading byte.", ret);
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]);
208 sr_dbg("Message header received: %02x",
209 buf[message_length]);
210 message_length += ret;
215 /* Read until end byte. */
216 while (buf[message_length - 1] != MSG_FRAME_END
217 && message_length < length && turn < max_turns) {
219 ret = serial_read_blocking(serial, &buf[message_length],
220 1, serial_timeout(serial, 1));
222 sr_err("Error %d reading byte.", ret);
224 } else if (ret == 1) {
225 if (buf[message_length] == MSG_FRAME_BEGIN) {
226 sr_warn("Frame begin before end received");
229 sr_dbg("Message data received: %02x",
230 buf[message_length]);
231 message_length += ret;
237 if (turn < max_turns) {
238 if (buf[message_length - 1] == MSG_FRAME_END) {
239 message_complete = TRUE;
240 sr_dbg("Message end received");
242 sr_warn("Frame end not received");
245 sr_warn("Invalid data and timeout");
249 if (message_complete && message_length > 2) {
250 ret = message_length;
251 } else if (turn >= max_turns) {
259 static void ebd_send_value(const struct sr_dev_inst *sdi, struct sr_channel *ch,
260 float value, enum sr_mq mq, enum sr_unit unit, int digits)
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;
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;
276 packet.type = SR_DF_ANALOG;
277 packet.payload = &analog;
278 sr_session_send(sdi, &packet);
279 g_slist_free(analog.meaning->channels);
282 SR_PRIV int ebd_receive_data(int fd, int revents, void *cb_data)
284 struct sr_dev_inst *sdi;
285 struct dev_context *devc;
286 struct sr_serial_dev_inst *serial;
287 float current, current_limit;
288 float voltage, voltage_dp, voltage_dm, uvc_threshold;
289 uint8_t reply[MSG_MAX_LEN];
296 if (!(sdi = cb_data))
299 if (!(devc = sdi->priv))
303 current_limit = devc->current_limit;
304 uvc_threshold = devc->uvc_threshold;
306 ret = ebd_read_message(serial, MSG_MAX_LEN, reply);
308 /* Tests for correct message. */
310 sr_err("Can't receive messages");
312 } else if (ret == 0) {
313 sr_err("No messages received");
314 devc->running = FALSE;
316 } else if (ret != 19 ||
317 (reply[1] != 0x00 && reply[1] != 0x0a && reply[1] != 0x64 && reply[1] != 0x6e)) {
319 sr_info("Not measurement message received");
323 /* Verify checksum */
325 for (i = 1; i < ret - 1; i++) {
326 checksum ^= reply[i];
329 sr_warn("Invalid checksum");
330 /* Don't exit on wrong checksum, the device can recover */
334 devc->running = TRUE;
335 if (reply[1] == 0x00 || reply[1] == 0x64)
336 devc->load_activated = FALSE;
337 else if (reply[1] == 0x0a || reply[1] == 0x6e)
338 devc->load_activated = TRUE;
340 /* Calculate values. */
341 current = decode_value(reply[2], reply[3], 10000.0);
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);
345 if (reply[1] == 0x0a) {
346 current_limit = decode_value(reply[10], reply[11], 1000.0);
347 uvc_threshold = decode_value(reply[12], reply[13], 100.0);
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);
354 if (reply[1] == 0x0a) {
355 sr_dbg("Current limit %.03f A", current_limit);
356 sr_dbg("UVC threshold %.03f V", uvc_threshold);
359 /* Update load state. */
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);
364 } else if (devc->load_activated &&
365 (current_limit != devc->current_limit || uvc_threshold != devc->uvc_threshold)) {
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);
370 send_cfg(serial, devc);
374 std_session_send_df_frame_begin(sdi);
377 ebd_send_value(sdi, sdi->channels->data, voltage,
378 SR_MQ_VOLTAGE, SR_UNIT_VOLT, 3);
379 ebd_send_value(sdi, sdi->channels->next->data, current,
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);
387 std_session_send_df_frame_end(sdi);
389 sr_sw_limits_update_samples_read(&devc->limits, 1);
390 if (sr_sw_limits_check(&devc->limits))
391 sr_dev_acquisition_stop(sdi);
396 SR_PRIV int ebd_get_current_limit(const struct sr_dev_inst *sdi, float *current)
398 struct dev_context *devc;
400 if (!(devc = sdi->priv))
403 g_mutex_lock(&devc->rw_mutex);
404 *current = devc->current_limit;
405 g_mutex_unlock(&devc->rw_mutex);
410 SR_PRIV int ebd_set_current_limit(const struct sr_dev_inst *sdi, float current)
412 struct dev_context *devc;
415 if (!(devc = sdi->priv))
418 g_mutex_lock(&devc->rw_mutex);
419 devc->current_limit = current;
421 if (!devc->running) {
422 sr_dbg("Setting current limit later.");
423 g_mutex_unlock(&devc->rw_mutex);
427 sr_dbg("Setting current limit to %fV.", current);
429 if (devc->load_activated) {
430 if (ebd_current_is0(devc)) {
432 ret = ebd_loadtoggle(sdi->conn, devc);
434 /* Send new current. */
435 ret = send_cfg(sdi->conn, devc);
438 if (ebd_current_is0(devc)) {
443 ret = ebd_loadstart(sdi->conn, devc);
447 g_mutex_unlock(&devc->rw_mutex);
452 SR_PRIV int ebd_get_uvc_threshold(const struct sr_dev_inst *sdi, float *voltage)
454 struct dev_context *devc;
456 if (!(devc = sdi->priv))
459 g_mutex_lock(&devc->rw_mutex);
460 *voltage = devc->uvc_threshold;
461 g_mutex_unlock(&devc->rw_mutex);
466 SR_PRIV int ebd_set_uvc_threshold(const struct sr_dev_inst *sdi, float voltage)
468 struct dev_context *devc;
471 if (!(devc = sdi->priv))
474 g_mutex_lock(&devc->rw_mutex);
475 devc->uvc_threshold = voltage;
477 if (!devc->running) {
478 sr_dbg("Setting uvc threshold later.");
479 g_mutex_unlock(&devc->rw_mutex);
483 sr_dbg("Setting uvc threshold to %fV.", voltage);
485 if (devc->load_activated) {
486 if (ebd_current_is0(devc)) {
488 ret = ebd_loadtoggle(sdi->conn, devc);
490 /* Send new current. */
491 ret = send_cfg(sdi->conn, devc);
494 if (ebd_current_is0(devc)) {
499 ret = ebd_loadstart(sdi->conn, devc);
503 g_mutex_unlock(&devc->rw_mutex);
508 SR_PRIV gboolean ebd_current_is0(struct dev_context *devc)
510 return devc->current_limit < 0.001;