]> sigrok.org Git - libsigrok.git/blame - src/hardware/zketech-ebd-usb/protocol.c
zketech-ebd-usb: First version of the driver.
[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>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20#include <config.h>
21#include "protocol.h"
22
9890fb1f
SBO
23/* Send a command to the device. */
24SR_PRIV int zketech_ebd_usb_send(struct sr_serial_dev_inst *serial,
25 uint8_t buf[], size_t count)
26{
27 int ret;
28
29 zketech_ebd_usb_buffer_debug("Sending", buf, count);
30 ret = serial_write_blocking(serial, buf, count, 0);
31 if (ret < 0) {
32 sr_err("Error sending command: %d.", ret);
33 return ret;
34 }
35 sr_dbg("Sent %d bytes.", ret);
36
37 return (ret == (int)count) ? SR_OK : SR_ERR;
38}
39
40/* Send the init/connect sequence; drive starts sending voltage and current. */
41SR_PRIV int zketech_ebd_usb_init(struct sr_serial_dev_inst *serial,
42 struct dev_context *devc)
43{
44 uint8_t init[] = { 0xfa, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0xf8 };
45
46 (void)devc;
47
48 int ret = zketech_ebd_usb_send(serial, init, 10);
49 if (ret == SR_OK)
50 devc->running = TRUE;
51
52 return ret;
53}
54
55/* Start the load functionality. */
56SR_PRIV int zketech_ebd_usb_loadstart(struct sr_serial_dev_inst *serial,
57 struct dev_context *devc)
58{
59 uint8_t start[] = { 0xfa, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xf8 };
60 int ret;
61
62 ret = zketech_ebd_usb_send(serial, start, 10);
63 sr_dbg("Current limit: %f.", devc->current_limit);
64 if (zketech_ebd_usb_current_is0(devc))
65 return SR_OK;
66
67 ret = zketech_ebd_usb_sendcfg(serial, devc);
68 if (ret == SR_OK) {
69 sr_dbg("Load activated.");
70 devc->load_activated = TRUE;
71 }
72
73 return ret;
74}
75
76/* Stop the load functionality. */
77SR_PRIV int zketech_ebd_usb_loadstop(struct sr_serial_dev_inst *serial,
78 struct dev_context *devc)
79{
80 int ret;
81 uint8_t stop[] = { 0xfa, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xF8 };
82
83 ret = zketech_ebd_usb_send(serial, stop, 10);
84 if (ret == SR_OK)
85 devc->load_activated = FALSE;
86
87 return ret;
88}
89
90/* Stop the drive. */
91SR_PRIV int zketech_ebd_usb_stop(struct sr_serial_dev_inst *serial,
92 struct dev_context *devc)
93{
94 uint8_t stop[] = { 0xfa, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0xF8 };
95 int ret;
96
97 (void) devc;
98
99 ret = zketech_ebd_usb_send(serial, stop, 10);
100 if (ret == SR_OK) {
101 devc->load_activated = FALSE;
102 devc->running= FALSE;
103 }
104
105 return ret;
106}
107
108/* Send updated configuration values to the load. */
109SR_PRIV int zketech_ebd_usb_sendcfg(struct sr_serial_dev_inst *serial,
110 struct dev_context *devc)
111{
112 uint8_t send[] = { 0xfa, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8 };
113
114 zketech_ebd_usb_value_encode(devc->current_limit, &send[2],
115 &send[3], 1000.0);
116
117 send[8] = send[1] ^ send[2] ^ send[3] ^ send[4] ^ send[5] ^ \
118 send[6] ^ send[7];
119
120 return zketech_ebd_usb_send(serial, send, 10);
121}
122
123/** Read count bytes from the serial connection. */
124SR_PRIV int zketech_ebd_usb_read_chars(struct sr_serial_dev_inst *serial,
125 int count, uint8_t *buf)
126{
127 int ret, received, turns;
128
129 received = 0;
130 turns = 0;
131
132 do {
133 ret = serial_read_blocking(serial, buf + received,
134 count - received, serial_timeout(serial, count));
135 if (ret < 0) {
136 sr_err("Error %d reading %d bytes.", ret, count);
137 return ret;
138 }
139 received += ret;
140 turns++;
141 } while ((received < count) && (turns < 100));
142
143 zketech_ebd_usb_buffer_debug("Received", buf, received);
144
145 return received;
146}
147
148/* Decode high byte and low byte into a float. */
149SR_PRIV float zketech_ebd_usb_value_decode(uint8_t hi, uint8_t lo, float divisor)
150{
151 return ((float)hi * 240.0 + (float)lo) / divisor;
152}
153
154/* Encode a float into high byte and low byte. */
155SR_PRIV void zketech_ebd_usb_value_encode(float current, uint8_t *hi,
156 uint8_t *lo, float divisor)
157{
158 int value;
159
160 value = (int)(current * divisor);
161 sr_dbg("Value %d %d %d", value, value / 240, value % 240);
162 *hi = value / 240;
163 *lo = value % 240;
164}
165
c527132a
SBO
166SR_PRIV int zketech_ebd_usb_receive_data(int fd, int revents, void *cb_data)
167{
9890fb1f 168 struct sr_dev_inst *sdi;
c527132a 169 struct dev_context *devc;
9890fb1f
SBO
170 struct sr_serial_dev_inst *serial;
171 struct sr_datafeed_packet packet;
172 struct sr_datafeed_analog analog;
173 struct sr_analog_encoding encoding;
174 struct sr_analog_meaning meaning;
175 struct sr_analog_spec spec;
176 float voltage, current, current_limit;
177 GSList *l;
c527132a 178
9890fb1f 179 (void)revents;
c527132a
SBO
180 (void)fd;
181
182 if (!(sdi = cb_data))
9890fb1f 183 return FALSE;
c527132a
SBO
184
185 if (!(devc = sdi->priv))
9890fb1f
SBO
186 return FALSE;
187
188 serial = sdi->conn;
189
190 uint8_t reply[MSGLEN];
191 int ret = zketech_ebd_usb_read_chars(serial, MSGLEN, reply);
192
193 /* Tests for correct message. */
194 if (ret != MSGLEN) {
195 sr_err("Message invalid [Len].");
196 return (ret < 0) ? ret : SR_ERR;
197 }
c527132a 198
9890fb1f
SBO
199 uint8_t xor = reply[1] ^ reply[2] ^ reply[3] ^ reply[4] ^ \
200 reply[5] ^ reply[6] ^ reply[7] ^ reply[8] ^ \
201 reply[9] ^ reply[10] ^ reply[11] ^ reply[12] ^ \
202 reply[13] ^ reply[14] ^ reply[15] ^ reply[16];
203
204 if (reply[MSGFRAMEBEGINPOS] != MSGFRAMEBEGIN || \
205 reply[MSGFRAMEENDPOS] != MSGFRAMEEND || \
206 xor != reply[MSGCHECKSUMPOS]) {
207 sr_err("Message invalid [XOR, BEGIN/END].");
208 return SR_ERR;
c527132a
SBO
209 }
210
9890fb1f
SBO
211 /* Calculate values. */
212 sr_dbg("V: %02X %02X A: %02X %02X -- Limit %02X %02X", reply[4],
213 reply[5], reply[2], reply[3], reply[10], reply[11]);
214
215 voltage = zketech_ebd_usb_value_decode(reply[4], reply[5], 1000.0);
216 current = zketech_ebd_usb_value_decode(reply[2], reply[3], 10000.0);
217 current_limit = zketech_ebd_usb_value_decode(reply[10], reply[11], 1000.0);
218
219 sr_dbg("Voltage %f", voltage);
220 sr_dbg("Current %f", current);
221 sr_dbg("Current limit %f", current_limit);
222
223 /* Begin frame. */
224 packet.type = SR_DF_FRAME_BEGIN;
225 packet.payload = NULL;
226 sr_session_send(sdi, &packet);
227
228 sr_analog_init(&analog, &encoding, &meaning, &spec, 4);
229
230 packet.type = SR_DF_ANALOG;
231 packet.payload = &analog;
232 analog.num_samples = 1;
233
234 /* Voltage */
235 l = g_slist_copy(sdi->channels);
236 l = g_slist_remove_link(l, g_slist_nth(l, 1));
237 meaning.channels = l;
238 meaning.mq = SR_MQ_VOLTAGE;
239 meaning.mqflags = SR_MQFLAG_DC;
240 meaning.unit = SR_UNIT_VOLT;
241 analog.data = &voltage;
242 sr_session_send(sdi, &packet);
243 g_slist_free(l);
244
245 /* Current */
246 l = g_slist_copy(sdi->channels);
247 l = g_slist_remove_link(l, g_slist_nth(l, 0));
248 meaning.channels = l;
249 meaning.mq = SR_MQ_CURRENT;
250 meaning.mqflags = SR_MQFLAG_DC;
251 meaning.unit = SR_UNIT_AMPERE;
252 analog.data = &current;
253 sr_session_send(sdi, &packet);
254 g_slist_free(l);
255
256 /* End frame. */
257 packet.type = SR_DF_FRAME_END;
258 packet.payload = NULL;
259 sr_session_send(sdi, &packet);
260
261 sr_sw_limits_update_samples_read(&devc->limits, 1);
262 if (sr_sw_limits_check(&devc->limits))
263 sr_dev_acquisition_stop(sdi);
264
c527132a
SBO
265 return TRUE;
266}
9890fb1f
SBO
267
268SR_PRIV int zketech_ebd_usb_get_current_limit(const struct sr_dev_inst *sdi,
269 float *current)
270{
271 struct dev_context *devc;
272
273 if (!(devc = sdi->priv))
274 return SR_ERR;
275
276 g_mutex_lock(&devc->rw_mutex);
277 *current = devc->current_limit;
278 g_mutex_unlock(&devc->rw_mutex);
279
280 return SR_OK;
281}
282
283SR_PRIV int zketech_ebd_usb_set_current_limit(const struct sr_dev_inst *sdi,
284 float current)
285{
286 struct dev_context *devc;
287 int ret;
288
289 if (!(devc = sdi->priv))
290 return SR_ERR;
291
292 g_mutex_lock(&devc->rw_mutex);
293 devc->current_limit = current;
294
295 if (!devc->running) {
296 sr_dbg("Setting current limit later.");
297 g_mutex_unlock(&devc->rw_mutex);
298 return SR_OK;
299 }
300
301 sr_dbg("Setting current limit to %fV.", current);
302
303 if (devc->load_activated) {
304 if (zketech_ebd_usb_current_is0(devc)) {
305 /* Stop load. */
306 ret = zketech_ebd_usb_loadstop(sdi->conn, devc);
307 } else {
308 /* Send new current. */
309 ret = zketech_ebd_usb_sendcfg(sdi->conn, devc);
310 }
311 } else {
312 if (zketech_ebd_usb_current_is0(devc)) {
313 /* Nothing to do. */
314 ret = SR_OK;
315 } else {
316 /* Start load. */
317 ret = zketech_ebd_usb_loadstart(sdi->conn, devc);
318 }
319 }
320
321 g_mutex_unlock(&devc->rw_mutex);
322
323 return ret;
324}
325
326SR_PRIV gboolean zketech_ebd_usb_current_is0(struct dev_context *devc)
327{
328 return devc->current_limit < 0.001;
329}
330
331/* Log a byte-array as hex values. */
332SR_PRIV void zketech_ebd_usb_buffer_debug(const char *message, uint8_t buf[],
333 size_t count)
334{
335 char buffer[count * 2 + 1];
336
337 for (size_t j = 0; j < count; j++)
338 sprintf(&buffer[2 * j], "%02X", buf[j]);
339
340 buffer[count * 2] = 0;
341
342 sr_dbg("%s: %s [%lu bytes]", message, buffer, count);
343}