]> sigrok.org Git - libsigrok.git/blame_incremental - src/hardware/zketech-ebd-usb/protocol.c
kingst-la2016: fix segfault that often occurs when a capture is aborted
[libsigrok.git] / src / hardware / zketech-ebd-usb / protocol.c
... / ...
CommitLineData
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. */
24static 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 [%zu bytes]", message, buffer, count);
34}
35
36/* Send a command to the device. */
37static 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
48 return (ret == (int)count) ? SR_OK : SR_ERR;
49}
50
51/* Decode high byte and low byte into a float. */
52static float decode_value(uint8_t hi, uint8_t lo, float divisor)
53{
54 return ((float)hi * 240.0 + (float)lo) / divisor;
55}
56
57/* Encode a float into high byte and low byte. */
58static void encode_value(float current, uint8_t *hi, uint8_t *lo, float divisor)
59{
60 int value;
61
62 value = (int)(current * divisor);
63 sr_dbg("Value %d %d %d", value, value / 240, value % 240);
64 *hi = value / 240;
65 *lo = value % 240;
66}
67
68/* Send updated configuration values to the load. */
69static int send_cfg(struct sr_serial_dev_inst *serial, struct dev_context *devc)
70{
71 uint8_t send[] = { 0xfa, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8 };
72
73 encode_value(devc->current_limit, &send[2], &send[3], 1000.0);
74
75 send[8] = send[1] ^ send[2] ^ send[3] ^ send[4] ^ send[5] ^ \
76 send[6] ^ send[7];
77
78 return send_cmd(serial, send, 10);
79}
80
81/* Send the init/connect sequence; drive starts sending voltage and current. */
82SR_PRIV int ebd_init(struct sr_serial_dev_inst *serial, struct dev_context *devc)
83{
84 uint8_t init[] = { 0xfa, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0xf8 };
85
86 (void)devc;
87
88 int ret = send_cmd(serial, init, 10);
89 if (ret == SR_OK)
90 devc->running = TRUE;
91
92 return ret;
93}
94
95/* Start the load functionality. */
96SR_PRIV int ebd_loadstart(struct sr_serial_dev_inst *serial, struct dev_context *devc)
97{
98 uint8_t start[] = { 0xfa, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xf8 };
99 int ret;
100
101 ret = send_cmd(serial, start, 10);
102 if (ret)
103 return ret;
104
105 sr_dbg("Current limit: %f.", devc->current_limit);
106 if (ebd_current_is0(devc))
107 return SR_OK;
108
109 ret = send_cfg(serial, devc);
110 if (ret == SR_OK) {
111 sr_dbg("Load activated.");
112 devc->load_activated = TRUE;
113 }
114
115 return ret;
116}
117
118/* Stop the load functionality. */
119SR_PRIV int ebd_loadstop(struct sr_serial_dev_inst *serial, struct dev_context *devc)
120{
121 int ret;
122 uint8_t stop[] = { 0xfa, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xF8 };
123
124 ret = send_cmd(serial, stop, 10);
125 if (ret == SR_OK)
126 devc->load_activated = FALSE;
127
128 return ret;
129}
130
131/* Stop the drive. */
132SR_PRIV int ebd_stop(struct sr_serial_dev_inst *serial, struct dev_context *devc)
133{
134 uint8_t stop[] = { 0xfa, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0xF8 };
135 int ret;
136
137 (void) devc;
138
139 ret = send_cmd(serial, stop, 10);
140 if (ret == SR_OK) {
141 devc->load_activated = FALSE;
142 devc->running= FALSE;
143 }
144
145 return ret;
146}
147
148/** Read count bytes from the serial connection. */
149SR_PRIV int ebd_read_chars(struct sr_serial_dev_inst *serial, int count, uint8_t *buf)
150{
151 int ret, received, turns;
152
153 received = 0;
154 turns = 0;
155
156 do {
157 ret = serial_read_blocking(serial, buf + received,
158 count - received, serial_timeout(serial, count));
159 if (ret < 0) {
160 sr_err("Error %d reading %d bytes.", ret, count);
161 return ret;
162 }
163 received += ret;
164 turns++;
165 } while ((received < count) && (turns < 100));
166
167 log_buf("Received", buf, received);
168
169 return received;
170}
171
172SR_PRIV int ebd_receive_data(int fd, int revents, void *cb_data)
173{
174 struct sr_dev_inst *sdi;
175 struct dev_context *devc;
176 struct sr_serial_dev_inst *serial;
177 struct sr_datafeed_packet packet;
178 struct sr_datafeed_analog analog;
179 struct sr_analog_encoding encoding;
180 struct sr_analog_meaning meaning;
181 struct sr_analog_spec spec;
182 float voltage, current, current_limit;
183 GSList *l;
184
185 (void)revents;
186 (void)fd;
187
188 if (!(sdi = cb_data))
189 return FALSE;
190
191 if (!(devc = sdi->priv))
192 return FALSE;
193
194 serial = sdi->conn;
195
196 uint8_t reply[MSG_LEN];
197 int ret = ebd_read_chars(serial, MSG_LEN, reply);
198
199 /* Tests for correct message. */
200 if (ret != MSG_LEN) {
201 sr_err("Message invalid [Len].");
202 return (ret < 0) ? ret : SR_ERR;
203 }
204
205 uint8_t xor = reply[1] ^ reply[2] ^ reply[3] ^ reply[4] ^ \
206 reply[5] ^ reply[6] ^ reply[7] ^ reply[8] ^ \
207 reply[9] ^ reply[10] ^ reply[11] ^ reply[12] ^ \
208 reply[13] ^ reply[14] ^ reply[15] ^ reply[16];
209
210 if (reply[MSG_FRAME_BEGIN_POS] != MSG_FRAME_BEGIN || \
211 reply[MSG_FRAME_END_POS] != MSG_FRAME_END || \
212 xor != reply[MSG_CHECKSUM_POS]) {
213 sr_err("Message invalid [XOR, BEGIN/END].");
214 return SR_ERR;
215 }
216
217 /* Calculate values. */
218 sr_dbg("V: %02X %02X A: %02X %02X -- Limit %02X %02X", reply[4],
219 reply[5], reply[2], reply[3], reply[10], reply[11]);
220
221 voltage = decode_value(reply[4], reply[5], 1000.0);
222 current = decode_value(reply[2], reply[3], 10000.0);
223 current_limit = decode_value(reply[10], reply[11], 1000.0);
224
225 sr_dbg("Voltage %f", voltage);
226 sr_dbg("Current %f", current);
227 sr_dbg("Current limit %f", current_limit);
228
229 /* Begin frame. */
230 std_session_send_df_frame_begin(sdi);
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 std_session_send_df_frame_end(sdi);
262
263 sr_sw_limits_update_samples_read(&devc->limits, 1);
264 if (sr_sw_limits_check(&devc->limits))
265 sr_dev_acquisition_stop(sdi);
266
267 return TRUE;
268}
269
270SR_PRIV int ebd_get_current_limit(const struct sr_dev_inst *sdi, float *current)
271{
272 struct dev_context *devc;
273
274 if (!(devc = sdi->priv))
275 return SR_ERR;
276
277 g_mutex_lock(&devc->rw_mutex);
278 *current = devc->current_limit;
279 g_mutex_unlock(&devc->rw_mutex);
280
281 return SR_OK;
282}
283
284SR_PRIV int ebd_set_current_limit(const struct sr_dev_inst *sdi, 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 (ebd_current_is0(devc)) {
305 /* Stop load. */
306 ret = ebd_loadstop(sdi->conn, devc);
307 } else {
308 /* Send new current. */
309 ret = send_cfg(sdi->conn, devc);
310 }
311 } else {
312 if (ebd_current_is0(devc)) {
313 /* Nothing to do. */
314 ret = SR_OK;
315 } else {
316 /* Start load. */
317 ret = ebd_loadstart(sdi->conn, devc);
318 }
319 }
320
321 g_mutex_unlock(&devc->rw_mutex);
322
323 return ret;
324}
325
326SR_PRIV gboolean ebd_current_is0(struct dev_context *devc)
327{
328 return devc->current_limit < 0.001;
329}