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