]>
Commit | Line | Data |
---|---|---|
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 | ||
cb8a0efc UH |
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 | ||
9890fb1f | 36 | /* Send a command to the device. */ |
cb8a0efc | 37 | static int send_cmd(struct sr_serial_dev_inst *serial, uint8_t buf[], size_t count) |
9890fb1f SBO |
38 | { |
39 | int ret; | |
40 | ||
cb8a0efc | 41 | log_buf("Sending", buf, count); |
9890fb1f SBO |
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 | ||
cb8a0efc UH |
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 | ||
9890fb1f SBO |
82 | /* Send the init/connect sequence; drive starts sending voltage and current. */ |
83 | SR_PRIV int zketech_ebd_usb_init(struct sr_serial_dev_inst *serial, | |
84 | struct dev_context *devc) | |
85 | { | |
86 | uint8_t init[] = { 0xfa, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0xf8 }; | |
87 | ||
88 | (void)devc; | |
89 | ||
cb8a0efc | 90 | int ret = send_cmd(serial, init, 10); |
9890fb1f SBO |
91 | if (ret == SR_OK) |
92 | devc->running = TRUE; | |
93 | ||
94 | return ret; | |
95 | } | |
96 | ||
97 | /* Start the load functionality. */ | |
98 | SR_PRIV int zketech_ebd_usb_loadstart(struct sr_serial_dev_inst *serial, | |
99 | struct dev_context *devc) | |
100 | { | |
101 | uint8_t start[] = { 0xfa, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xf8 }; | |
102 | int ret; | |
103 | ||
cb8a0efc | 104 | ret = send_cmd(serial, start, 10); |
9890fb1f SBO |
105 | sr_dbg("Current limit: %f.", devc->current_limit); |
106 | if (zketech_ebd_usb_current_is0(devc)) | |
107 | return SR_OK; | |
108 | ||
cb8a0efc | 109 | ret = send_cfg(serial, devc); |
9890fb1f SBO |
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. */ | |
119 | SR_PRIV int zketech_ebd_usb_loadstop(struct sr_serial_dev_inst *serial, | |
120 | struct dev_context *devc) | |
121 | { | |
122 | int ret; | |
123 | uint8_t stop[] = { 0xfa, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xF8 }; | |
124 | ||
cb8a0efc | 125 | ret = send_cmd(serial, stop, 10); |
9890fb1f SBO |
126 | if (ret == SR_OK) |
127 | devc->load_activated = FALSE; | |
128 | ||
129 | return ret; | |
130 | } | |
131 | ||
132 | /* Stop the drive. */ | |
133 | SR_PRIV int zketech_ebd_usb_stop(struct sr_serial_dev_inst *serial, | |
134 | struct dev_context *devc) | |
135 | { | |
136 | uint8_t stop[] = { 0xfa, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0xF8 }; | |
137 | int ret; | |
138 | ||
139 | (void) devc; | |
140 | ||
cb8a0efc | 141 | ret = send_cmd(serial, stop, 10); |
9890fb1f SBO |
142 | if (ret == SR_OK) { |
143 | devc->load_activated = FALSE; | |
144 | devc->running= FALSE; | |
145 | } | |
146 | ||
147 | return ret; | |
148 | } | |
149 | ||
9890fb1f SBO |
150 | /** Read count bytes from the serial connection. */ |
151 | SR_PRIV int zketech_ebd_usb_read_chars(struct sr_serial_dev_inst *serial, | |
152 | int count, uint8_t *buf) | |
153 | { | |
154 | int ret, received, turns; | |
155 | ||
156 | received = 0; | |
157 | turns = 0; | |
158 | ||
159 | do { | |
160 | ret = serial_read_blocking(serial, buf + received, | |
161 | count - received, serial_timeout(serial, count)); | |
162 | if (ret < 0) { | |
163 | sr_err("Error %d reading %d bytes.", ret, count); | |
164 | return ret; | |
165 | } | |
166 | received += ret; | |
167 | turns++; | |
168 | } while ((received < count) && (turns < 100)); | |
169 | ||
cb8a0efc | 170 | log_buf("Received", buf, received); |
9890fb1f SBO |
171 | |
172 | return received; | |
173 | } | |
174 | ||
c527132a SBO |
175 | SR_PRIV int zketech_ebd_usb_receive_data(int fd, int revents, void *cb_data) |
176 | { | |
9890fb1f | 177 | struct sr_dev_inst *sdi; |
c527132a | 178 | struct dev_context *devc; |
9890fb1f SBO |
179 | struct sr_serial_dev_inst *serial; |
180 | struct sr_datafeed_packet packet; | |
181 | struct sr_datafeed_analog analog; | |
182 | struct sr_analog_encoding encoding; | |
183 | struct sr_analog_meaning meaning; | |
184 | struct sr_analog_spec spec; | |
185 | float voltage, current, current_limit; | |
186 | GSList *l; | |
c527132a | 187 | |
9890fb1f | 188 | (void)revents; |
c527132a SBO |
189 | (void)fd; |
190 | ||
191 | if (!(sdi = cb_data)) | |
9890fb1f | 192 | return FALSE; |
c527132a SBO |
193 | |
194 | if (!(devc = sdi->priv)) | |
9890fb1f SBO |
195 | return FALSE; |
196 | ||
197 | serial = sdi->conn; | |
198 | ||
ec4806dc UH |
199 | uint8_t reply[MSG_LEN]; |
200 | int ret = zketech_ebd_usb_read_chars(serial, MSG_LEN, reply); | |
9890fb1f SBO |
201 | |
202 | /* Tests for correct message. */ | |
ec4806dc | 203 | if (ret != MSG_LEN) { |
9890fb1f SBO |
204 | sr_err("Message invalid [Len]."); |
205 | return (ret < 0) ? ret : SR_ERR; | |
206 | } | |
c527132a | 207 | |
9890fb1f SBO |
208 | uint8_t xor = reply[1] ^ reply[2] ^ reply[3] ^ reply[4] ^ \ |
209 | reply[5] ^ reply[6] ^ reply[7] ^ reply[8] ^ \ | |
210 | reply[9] ^ reply[10] ^ reply[11] ^ reply[12] ^ \ | |
211 | reply[13] ^ reply[14] ^ reply[15] ^ reply[16]; | |
212 | ||
ec4806dc UH |
213 | if (reply[MSG_FRAME_BEGIN_POS] != MSG_FRAME_BEGIN || \ |
214 | reply[MSG_FRAME_END_POS] != MSG_FRAME_END || \ | |
215 | xor != reply[MSG_CHECKSUM_POS]) { | |
9890fb1f SBO |
216 | sr_err("Message invalid [XOR, BEGIN/END]."); |
217 | return SR_ERR; | |
c527132a SBO |
218 | } |
219 | ||
9890fb1f SBO |
220 | /* Calculate values. */ |
221 | sr_dbg("V: %02X %02X A: %02X %02X -- Limit %02X %02X", reply[4], | |
222 | reply[5], reply[2], reply[3], reply[10], reply[11]); | |
223 | ||
cb8a0efc UH |
224 | voltage = decode_value(reply[4], reply[5], 1000.0); |
225 | current = decode_value(reply[2], reply[3], 10000.0); | |
226 | current_limit = decode_value(reply[10], reply[11], 1000.0); | |
9890fb1f SBO |
227 | |
228 | sr_dbg("Voltage %f", voltage); | |
229 | sr_dbg("Current %f", current); | |
230 | sr_dbg("Current limit %f", current_limit); | |
231 | ||
232 | /* Begin frame. */ | |
233 | packet.type = SR_DF_FRAME_BEGIN; | |
234 | packet.payload = NULL; | |
235 | sr_session_send(sdi, &packet); | |
236 | ||
237 | sr_analog_init(&analog, &encoding, &meaning, &spec, 4); | |
238 | ||
239 | packet.type = SR_DF_ANALOG; | |
240 | packet.payload = &analog; | |
241 | analog.num_samples = 1; | |
242 | ||
243 | /* Voltage */ | |
244 | l = g_slist_copy(sdi->channels); | |
245 | l = g_slist_remove_link(l, g_slist_nth(l, 1)); | |
246 | meaning.channels = l; | |
247 | meaning.mq = SR_MQ_VOLTAGE; | |
248 | meaning.mqflags = SR_MQFLAG_DC; | |
249 | meaning.unit = SR_UNIT_VOLT; | |
250 | analog.data = &voltage; | |
251 | sr_session_send(sdi, &packet); | |
252 | g_slist_free(l); | |
253 | ||
254 | /* Current */ | |
255 | l = g_slist_copy(sdi->channels); | |
256 | l = g_slist_remove_link(l, g_slist_nth(l, 0)); | |
257 | meaning.channels = l; | |
258 | meaning.mq = SR_MQ_CURRENT; | |
259 | meaning.mqflags = SR_MQFLAG_DC; | |
260 | meaning.unit = SR_UNIT_AMPERE; | |
261 | analog.data = ¤t; | |
262 | sr_session_send(sdi, &packet); | |
263 | g_slist_free(l); | |
264 | ||
265 | /* End frame. */ | |
266 | packet.type = SR_DF_FRAME_END; | |
267 | packet.payload = NULL; | |
268 | sr_session_send(sdi, &packet); | |
269 | ||
270 | sr_sw_limits_update_samples_read(&devc->limits, 1); | |
271 | if (sr_sw_limits_check(&devc->limits)) | |
272 | sr_dev_acquisition_stop(sdi); | |
273 | ||
c527132a SBO |
274 | return TRUE; |
275 | } | |
9890fb1f SBO |
276 | |
277 | SR_PRIV int zketech_ebd_usb_get_current_limit(const struct sr_dev_inst *sdi, | |
278 | float *current) | |
279 | { | |
280 | struct dev_context *devc; | |
281 | ||
282 | if (!(devc = sdi->priv)) | |
283 | return SR_ERR; | |
284 | ||
285 | g_mutex_lock(&devc->rw_mutex); | |
286 | *current = devc->current_limit; | |
287 | g_mutex_unlock(&devc->rw_mutex); | |
288 | ||
289 | return SR_OK; | |
290 | } | |
291 | ||
292 | SR_PRIV int zketech_ebd_usb_set_current_limit(const struct sr_dev_inst *sdi, | |
293 | float current) | |
294 | { | |
295 | struct dev_context *devc; | |
296 | int ret; | |
297 | ||
298 | if (!(devc = sdi->priv)) | |
299 | return SR_ERR; | |
300 | ||
301 | g_mutex_lock(&devc->rw_mutex); | |
302 | devc->current_limit = current; | |
303 | ||
304 | if (!devc->running) { | |
305 | sr_dbg("Setting current limit later."); | |
306 | g_mutex_unlock(&devc->rw_mutex); | |
307 | return SR_OK; | |
308 | } | |
309 | ||
310 | sr_dbg("Setting current limit to %fV.", current); | |
311 | ||
312 | if (devc->load_activated) { | |
313 | if (zketech_ebd_usb_current_is0(devc)) { | |
314 | /* Stop load. */ | |
315 | ret = zketech_ebd_usb_loadstop(sdi->conn, devc); | |
316 | } else { | |
317 | /* Send new current. */ | |
cb8a0efc | 318 | ret = send_cfg(sdi->conn, devc); |
9890fb1f SBO |
319 | } |
320 | } else { | |
321 | if (zketech_ebd_usb_current_is0(devc)) { | |
322 | /* Nothing to do. */ | |
323 | ret = SR_OK; | |
324 | } else { | |
325 | /* Start load. */ | |
326 | ret = zketech_ebd_usb_loadstart(sdi->conn, devc); | |
327 | } | |
328 | } | |
329 | ||
330 | g_mutex_unlock(&devc->rw_mutex); | |
331 | ||
332 | return ret; | |
333 | } | |
334 | ||
335 | SR_PRIV gboolean zketech_ebd_usb_current_is0(struct dev_context *devc) | |
336 | { | |
337 | return devc->current_limit < 0.001; | |
338 | } |