2 * This file is part of the libsigrok project.
4 * Copyright (C) 2018 Sven Bursch-Osewold <sb_git@bursch.com>
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.
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.
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/>.
23 /* Send a command to the device. */
24 SR_PRIV int zketech_ebd_usb_send(struct sr_serial_dev_inst *serial,
25 uint8_t buf[], size_t count)
29 zketech_ebd_usb_buffer_debug("Sending", buf, count);
30 ret = serial_write_blocking(serial, buf, count, 0);
32 sr_err("Error sending command: %d.", ret);
35 sr_dbg("Sent %d bytes.", ret);
37 return (ret == (int)count) ? SR_OK : SR_ERR;
40 /* Send the init/connect sequence; drive starts sending voltage and current. */
41 SR_PRIV int zketech_ebd_usb_init(struct sr_serial_dev_inst *serial,
42 struct dev_context *devc)
44 uint8_t init[] = { 0xfa, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0xf8 };
48 int ret = zketech_ebd_usb_send(serial, init, 10);
55 /* Start the load functionality. */
56 SR_PRIV int zketech_ebd_usb_loadstart(struct sr_serial_dev_inst *serial,
57 struct dev_context *devc)
59 uint8_t start[] = { 0xfa, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xf8 };
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))
67 ret = zketech_ebd_usb_sendcfg(serial, devc);
69 sr_dbg("Load activated.");
70 devc->load_activated = TRUE;
76 /* Stop the load functionality. */
77 SR_PRIV int zketech_ebd_usb_loadstop(struct sr_serial_dev_inst *serial,
78 struct dev_context *devc)
81 uint8_t stop[] = { 0xfa, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xF8 };
83 ret = zketech_ebd_usb_send(serial, stop, 10);
85 devc->load_activated = FALSE;
91 SR_PRIV int zketech_ebd_usb_stop(struct sr_serial_dev_inst *serial,
92 struct dev_context *devc)
94 uint8_t stop[] = { 0xfa, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0xF8 };
99 ret = zketech_ebd_usb_send(serial, stop, 10);
101 devc->load_activated = FALSE;
102 devc->running= FALSE;
108 /* Send updated configuration values to the load. */
109 SR_PRIV int zketech_ebd_usb_sendcfg(struct sr_serial_dev_inst *serial,
110 struct dev_context *devc)
112 uint8_t send[] = { 0xfa, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8 };
114 zketech_ebd_usb_value_encode(devc->current_limit, &send[2],
117 send[8] = send[1] ^ send[2] ^ send[3] ^ send[4] ^ send[5] ^ \
120 return zketech_ebd_usb_send(serial, send, 10);
123 /** Read count bytes from the serial connection. */
124 SR_PRIV int zketech_ebd_usb_read_chars(struct sr_serial_dev_inst *serial,
125 int count, uint8_t *buf)
127 int ret, received, turns;
133 ret = serial_read_blocking(serial, buf + received,
134 count - received, serial_timeout(serial, count));
136 sr_err("Error %d reading %d bytes.", ret, count);
141 } while ((received < count) && (turns < 100));
143 zketech_ebd_usb_buffer_debug("Received", buf, received);
148 /* Decode high byte and low byte into a float. */
149 SR_PRIV float zketech_ebd_usb_value_decode(uint8_t hi, uint8_t lo, float divisor)
151 return ((float)hi * 240.0 + (float)lo) / divisor;
154 /* Encode a float into high byte and low byte. */
155 SR_PRIV void zketech_ebd_usb_value_encode(float current, uint8_t *hi,
156 uint8_t *lo, float divisor)
160 value = (int)(current * divisor);
161 sr_dbg("Value %d %d %d", value, value / 240, value % 240);
166 SR_PRIV int zketech_ebd_usb_receive_data(int fd, int revents, void *cb_data)
168 struct sr_dev_inst *sdi;
169 struct dev_context *devc;
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;
182 if (!(sdi = cb_data))
185 if (!(devc = sdi->priv))
190 uint8_t reply[MSG_LEN];
191 int ret = zketech_ebd_usb_read_chars(serial, MSG_LEN, reply);
193 /* Tests for correct message. */
194 if (ret != MSG_LEN) {
195 sr_err("Message invalid [Len].");
196 return (ret < 0) ? ret : SR_ERR;
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];
204 if (reply[MSG_FRAME_BEGIN_POS] != MSG_FRAME_BEGIN || \
205 reply[MSG_FRAME_END_POS] != MSG_FRAME_END || \
206 xor != reply[MSG_CHECKSUM_POS]) {
207 sr_err("Message invalid [XOR, BEGIN/END].");
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]);
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);
219 sr_dbg("Voltage %f", voltage);
220 sr_dbg("Current %f", current);
221 sr_dbg("Current limit %f", current_limit);
224 packet.type = SR_DF_FRAME_BEGIN;
225 packet.payload = NULL;
226 sr_session_send(sdi, &packet);
228 sr_analog_init(&analog, &encoding, &meaning, &spec, 4);
230 packet.type = SR_DF_ANALOG;
231 packet.payload = &analog;
232 analog.num_samples = 1;
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);
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 = ¤t;
253 sr_session_send(sdi, &packet);
257 packet.type = SR_DF_FRAME_END;
258 packet.payload = NULL;
259 sr_session_send(sdi, &packet);
261 sr_sw_limits_update_samples_read(&devc->limits, 1);
262 if (sr_sw_limits_check(&devc->limits))
263 sr_dev_acquisition_stop(sdi);
268 SR_PRIV int zketech_ebd_usb_get_current_limit(const struct sr_dev_inst *sdi,
271 struct dev_context *devc;
273 if (!(devc = sdi->priv))
276 g_mutex_lock(&devc->rw_mutex);
277 *current = devc->current_limit;
278 g_mutex_unlock(&devc->rw_mutex);
283 SR_PRIV int zketech_ebd_usb_set_current_limit(const struct sr_dev_inst *sdi,
286 struct dev_context *devc;
289 if (!(devc = sdi->priv))
292 g_mutex_lock(&devc->rw_mutex);
293 devc->current_limit = current;
295 if (!devc->running) {
296 sr_dbg("Setting current limit later.");
297 g_mutex_unlock(&devc->rw_mutex);
301 sr_dbg("Setting current limit to %fV.", current);
303 if (devc->load_activated) {
304 if (zketech_ebd_usb_current_is0(devc)) {
306 ret = zketech_ebd_usb_loadstop(sdi->conn, devc);
308 /* Send new current. */
309 ret = zketech_ebd_usb_sendcfg(sdi->conn, devc);
312 if (zketech_ebd_usb_current_is0(devc)) {
317 ret = zketech_ebd_usb_loadstart(sdi->conn, devc);
321 g_mutex_unlock(&devc->rw_mutex);
326 SR_PRIV gboolean zketech_ebd_usb_current_is0(struct dev_context *devc)
328 return devc->current_limit < 0.001;
331 /* Log a byte-array as hex values. */
332 SR_PRIV void zketech_ebd_usb_buffer_debug(const char *message, uint8_t buf[],
335 char buffer[count * 2 + 1];
337 for (size_t j = 0; j < count; j++)
338 sprintf(&buffer[2 * j], "%02X", buf[j]);
340 buffer[count * 2] = 0;
342 sr_dbg("%s: %s [%lu bytes]", message, buffer, count);