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