]>
Commit | Line | Data |
---|---|---|
6e8d31d4 UH |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
803db07a | 4 | * Copyright (C) 2015-2016 Uwe Hermann <uwe@hermann-uwe.de> |
6e8d31d4 UH |
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 2 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 | |
2ea1fdf1 | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
6e8d31d4 UH |
18 | */ |
19 | ||
20 | #include <config.h> | |
9edda1d2 | 21 | #include <math.h> |
803db07a | 22 | #include <string.h> |
6e8d31d4 UH |
23 | #include "protocol.h" |
24 | ||
b3e715e5 | 25 | #define READ_TIMEOUT_MS 500 |
803db07a UH |
26 | |
27 | static int send_cmd(const struct sr_dev_inst *sdi, const char *cmd, | |
28 | char *replybuf, int replybufsize) | |
29 | { | |
30 | char *bufptr; | |
31 | int len, ret; | |
a2172899 | 32 | struct dev_context *devc; |
803db07a UH |
33 | struct sr_serial_dev_inst *serial; |
34 | ||
a2172899 | 35 | devc = sdi->priv; |
803db07a UH |
36 | serial = sdi->conn; |
37 | ||
38 | /* Send the command (blocking, with timeout). */ | |
39 | if ((ret = serial_write_blocking(serial, cmd, | |
40 | strlen(cmd), serial_timeout(serial, | |
41 | strlen(cmd)))) < (int)strlen(cmd)) { | |
42 | sr_err("Unable to send command."); | |
43 | return SR_ERR; | |
44 | } | |
45 | ||
a2172899 FS |
46 | if (!devc->acquisition_running) { |
47 | /* Read the reply (blocking, with timeout). */ | |
48 | memset(replybuf, 0, replybufsize); | |
49 | bufptr = replybuf; | |
803db07a UH |
50 | len = replybufsize; |
51 | ret = serial_readline(serial, &bufptr, &len, READ_TIMEOUT_MS); | |
803db07a | 52 | |
a2172899 FS |
53 | /* If we got 0 characters (possibly one \r or \n), retry once. */ |
54 | if (len == 0) { | |
55 | len = replybufsize; | |
56 | ret = serial_readline(serial, &bufptr, &len, READ_TIMEOUT_MS); | |
57 | } | |
58 | ||
59 | if (g_str_has_prefix((const char *)&bufptr, "err ")) { | |
60 | sr_err("Device replied with an error: '%s'.", bufptr); | |
61 | return SR_ERR; | |
62 | } | |
803db07a UH |
63 | } |
64 | ||
65 | return ret; | |
66 | } | |
67 | ||
68 | SR_PRIV int reloadpro_set_current_limit(const struct sr_dev_inst *sdi, | |
b3e715e5 | 69 | float current_limit) |
803db07a | 70 | { |
b3e715e5 | 71 | struct dev_context *devc; |
803db07a UH |
72 | int ret, ma; |
73 | char buf[100]; | |
74 | char *cmd; | |
75 | ||
b3e715e5 FS |
76 | devc = sdi->priv; |
77 | ||
78 | if (current_limit < 0 || current_limit > 6) { | |
79 | sr_err("The current limit must be 0-6 A (was %f A).", current_limit); | |
803db07a UH |
80 | return SR_ERR_ARG; |
81 | } | |
82 | ||
b3e715e5 FS |
83 | /* Hardware expects current limit in mA, integer (0..6000). */ |
84 | ma = (int)round(current_limit * 1000); | |
803db07a UH |
85 | |
86 | cmd = g_strdup_printf("set %d\n", ma); | |
b3e715e5 FS |
87 | g_mutex_lock(&devc->acquisition_mutex); |
88 | ret = send_cmd(sdi, cmd, (char *)&buf, sizeof(buf)); | |
89 | g_mutex_unlock(&devc->acquisition_mutex); | |
90 | g_free(cmd); | |
91 | ||
92 | if (ret < 0) { | |
803db07a | 93 | sr_err("Error sending current limit command: %d.", ret); |
803db07a UH |
94 | return SR_ERR; |
95 | } | |
803db07a UH |
96 | return SR_OK; |
97 | } | |
98 | ||
8501448c UH |
99 | SR_PRIV int reloadpro_set_on_off(const struct sr_dev_inst *sdi, gboolean on) |
100 | { | |
b3e715e5 | 101 | struct dev_context *devc; |
8501448c UH |
102 | int ret; |
103 | char buf[100]; | |
104 | const char *cmd; | |
105 | ||
b3e715e5 FS |
106 | devc = sdi->priv; |
107 | ||
8501448c | 108 | cmd = (on) ? "on\n" : "off\n"; |
b3e715e5 FS |
109 | g_mutex_lock(&devc->acquisition_mutex); |
110 | ret = send_cmd(sdi, cmd, (char *)&buf, sizeof(buf)); | |
111 | g_mutex_unlock(&devc->acquisition_mutex); | |
112 | ||
113 | if (ret < 0) { | |
8501448c UH |
114 | sr_err("Error sending on/off command: %d.", ret); |
115 | return SR_ERR; | |
116 | } | |
8501448c UH |
117 | return SR_OK; |
118 | } | |
119 | ||
3d70d777 FS |
120 | SR_PRIV int reloadpro_set_under_voltage_threshold(const struct sr_dev_inst *sdi, |
121 | float voltage) | |
122 | { | |
b3e715e5 | 123 | struct dev_context *devc; |
3d70d777 FS |
124 | int ret, mv; |
125 | char buf[100]; | |
126 | char *cmd; | |
127 | ||
b3e715e5 FS |
128 | devc = sdi->priv; |
129 | ||
3d70d777 FS |
130 | if (voltage < 0 || voltage > 60) { |
131 | sr_err("The under voltage threshold must be 0-60 V (was %f V).", | |
132 | voltage); | |
133 | return SR_ERR_ARG; | |
134 | } | |
135 | ||
136 | /* Hardware expects voltage in mV, integer (0..60000). */ | |
137 | mv = (int)round(voltage * 1000); | |
138 | ||
139 | sr_spew("Setting under voltage threshold to %f V (%d mV).", voltage, mv); | |
140 | ||
141 | cmd = g_strdup_printf("uvlo %d\n", mv); | |
b3e715e5 FS |
142 | g_mutex_lock(&devc->acquisition_mutex); |
143 | ret = send_cmd(sdi, cmd, (char *)&buf, sizeof(buf)); | |
144 | g_mutex_unlock(&devc->acquisition_mutex); | |
145 | g_free(cmd); | |
146 | ||
147 | if (ret < 0) { | |
3d70d777 | 148 | sr_err("Error sending under voltage threshold command: %d.", ret); |
3d70d777 FS |
149 | return SR_ERR; |
150 | } | |
3d70d777 FS |
151 | return SR_OK; |
152 | } | |
153 | ||
803db07a | 154 | SR_PRIV int reloadpro_get_current_limit(const struct sr_dev_inst *sdi, |
b3e715e5 | 155 | float *current_limit) |
803db07a | 156 | { |
b3e715e5 | 157 | struct dev_context *devc; |
803db07a UH |
158 | int ret; |
159 | char buf[100]; | |
b3e715e5 | 160 | gint64 end_time; |
a2172899 FS |
161 | |
162 | devc = sdi->priv; | |
803db07a | 163 | |
b3e715e5 | 164 | g_mutex_lock(&devc->acquisition_mutex); |
803db07a UH |
165 | if ((ret = send_cmd(sdi, "set\n", (char *)&buf, sizeof(buf))) < 0) { |
166 | sr_err("Error sending current limit query: %d.", ret); | |
167 | return SR_ERR; | |
168 | } | |
169 | ||
b3e715e5 FS |
170 | if (devc->acquisition_running) { |
171 | end_time = g_get_monotonic_time () + 5 * G_TIME_SPAN_SECOND; | |
172 | if (!g_cond_wait_until(&devc->current_limit_cond, | |
173 | &devc->acquisition_mutex, end_time)) { | |
c3f8e1ab | 174 | /* Timeout has passed. */ |
b3e715e5 FS |
175 | g_mutex_unlock(&devc->acquisition_mutex); |
176 | return SR_ERR; | |
177 | } | |
178 | } else { | |
179 | /* Hardware sends current limit in mA, integer (0..6000). */ | |
180 | devc->current_limit = g_ascii_strtod(buf + 4, NULL) / 1000; | |
a2172899 | 181 | } |
b3e715e5 FS |
182 | g_mutex_unlock(&devc->acquisition_mutex); |
183 | ||
184 | if (current_limit) | |
185 | *current_limit = devc->current_limit; | |
803db07a UH |
186 | |
187 | return SR_OK; | |
188 | } | |
189 | ||
3d70d777 | 190 | SR_PRIV int reloadpro_get_under_voltage_threshold(const struct sr_dev_inst *sdi, |
b3e715e5 | 191 | float *uvc_threshold) |
3d70d777 | 192 | { |
b3e715e5 | 193 | struct dev_context *devc; |
3d70d777 FS |
194 | int ret; |
195 | char buf[100]; | |
b3e715e5 | 196 | gint64 end_time; |
3d70d777 FS |
197 | |
198 | devc = sdi->priv; | |
199 | ||
b3e715e5 | 200 | g_mutex_lock(&devc->acquisition_mutex); |
3d70d777 FS |
201 | if ((ret = send_cmd(sdi, "uvlo\n", (char *)&buf, sizeof(buf))) < 0) { |
202 | sr_err("Error sending under voltage threshold query: %d.", ret); | |
203 | return SR_ERR; | |
204 | } | |
205 | ||
b3e715e5 FS |
206 | if (devc->acquisition_running) { |
207 | end_time = g_get_monotonic_time () + 5 * G_TIME_SPAN_SECOND; | |
208 | if (!g_cond_wait_until(&devc->uvc_threshold_cond, | |
209 | &devc->acquisition_mutex, end_time)) { | |
c3f8e1ab | 210 | /* Timeout has passed. */ |
b3e715e5 FS |
211 | g_mutex_unlock(&devc->acquisition_mutex); |
212 | return SR_ERR; | |
213 | } | |
214 | } else { | |
3d70d777 | 215 | /* Hardware sends voltage in mV, integer (0..60000). */ |
b3e715e5 | 216 | devc->uvc_threshold = g_ascii_strtod(buf + 5, NULL) / 1000; |
3d70d777 | 217 | } |
b3e715e5 FS |
218 | g_mutex_unlock(&devc->acquisition_mutex); |
219 | ||
220 | if (uvc_threshold) | |
221 | *uvc_threshold = devc->uvc_threshold; | |
3d70d777 FS |
222 | |
223 | return SR_OK; | |
224 | } | |
225 | ||
803db07a UH |
226 | SR_PRIV int reloadpro_get_voltage_current(const struct sr_dev_inst *sdi, |
227 | float *voltage, float *current) | |
228 | { | |
b3e715e5 | 229 | struct dev_context *devc; |
803db07a UH |
230 | int ret; |
231 | char buf[100]; | |
232 | char **tokens; | |
b3e715e5 | 233 | gint64 end_time; |
a2172899 FS |
234 | |
235 | devc = sdi->priv; | |
803db07a | 236 | |
b3e715e5 | 237 | g_mutex_lock(&devc->acquisition_mutex); |
803db07a UH |
238 | if ((ret = send_cmd(sdi, "read\n", (char *)&buf, sizeof(buf))) < 0) { |
239 | sr_err("Error sending voltage/current query: %d.", ret); | |
240 | return SR_ERR; | |
241 | } | |
242 | ||
b3e715e5 FS |
243 | if (devc->acquisition_running) { |
244 | end_time = g_get_monotonic_time () + 5 * G_TIME_SPAN_SECOND; | |
245 | if (!g_cond_wait_until(&devc->voltage_cond, | |
246 | &devc->acquisition_mutex, end_time)) { | |
c3f8e1ab | 247 | /* Timeout has passed. */ |
b3e715e5 FS |
248 | g_mutex_unlock(&devc->acquisition_mutex); |
249 | return SR_ERR; | |
250 | } | |
251 | } else { | |
a2172899 FS |
252 | /* Reply: "read <current> <voltage>". */ |
253 | tokens = g_strsplit((const char *)&buf, " ", 3); | |
b3e715e5 FS |
254 | devc->voltage = g_ascii_strtod(tokens[2], NULL) / 1000; |
255 | devc->current = g_ascii_strtod(tokens[1], NULL) / 1000; | |
a2172899 FS |
256 | g_strfreev(tokens); |
257 | } | |
b3e715e5 FS |
258 | g_mutex_unlock(&devc->acquisition_mutex); |
259 | ||
260 | if (voltage) | |
261 | *voltage = devc->voltage; | |
262 | if (current) | |
263 | *current = devc->current; | |
803db07a UH |
264 | |
265 | return SR_OK; | |
266 | } | |
267 | ||
268 | static void handle_packet(const struct sr_dev_inst *sdi) | |
6e8d31d4 | 269 | { |
803db07a | 270 | struct sr_datafeed_packet packet; |
6e68da51 UH |
271 | struct sr_datafeed_analog analog; |
272 | struct sr_analog_encoding encoding; | |
273 | struct sr_analog_meaning meaning; | |
274 | struct sr_analog_spec spec; | |
6e8d31d4 | 275 | struct dev_context *devc; |
803db07a UH |
276 | char **tokens; |
277 | GSList *l; | |
278 | ||
279 | devc = sdi->priv; | |
280 | ||
281 | if (g_str_has_prefix((const char *)devc->buf, "overtemp")) { | |
2217be1d | 282 | sr_warn("Overtemperature condition!"); |
803db07a | 283 | devc->otp_active = TRUE; |
7d1a4a52 | 284 | sr_session_send_meta(sdi, SR_CONF_OVER_TEMPERATURE_PROTECTION_ACTIVE, |
2472271e | 285 | g_variant_new_boolean(TRUE)); |
803db07a UH |
286 | return; |
287 | } | |
288 | ||
2217be1d UH |
289 | if (g_str_has_prefix((const char *)devc->buf, "undervolt")) { |
290 | sr_warn("Undervoltage condition!"); | |
291 | devc->uvc_active = TRUE; | |
7d1a4a52 | 292 | sr_session_send_meta(sdi, SR_CONF_UNDER_VOLTAGE_CONDITION_ACTIVE, |
2472271e FS |
293 | g_variant_new_boolean(TRUE)); |
294 | return; | |
295 | } | |
296 | ||
297 | if (g_str_has_prefix((const char *)devc->buf, "err ")) { | |
298 | sr_err("Device replied with an error: '%s'.", devc->buf); | |
299 | return; | |
300 | } | |
301 | ||
302 | if (g_str_has_prefix((const char *)devc->buf, "set ")) { | |
303 | tokens = g_strsplit((const char *)devc->buf, " ", 2); | |
b3e715e5 | 304 | devc->current_limit = g_ascii_strtod(tokens[1], NULL) / 1000; |
2472271e | 305 | g_strfreev(tokens); |
b3e715e5 | 306 | g_cond_signal(&devc->current_limit_cond); |
7d1a4a52 | 307 | sr_session_send_meta(sdi, SR_CONF_CURRENT_LIMIT, |
b3e715e5 | 308 | g_variant_new_double(devc->current_limit)); |
2217be1d UH |
309 | return; |
310 | } | |
311 | ||
3d70d777 FS |
312 | if (g_str_has_prefix((const char *)devc->buf, "uvlo ")) { |
313 | tokens = g_strsplit((const char *)devc->buf, " ", 2); | |
b3e715e5 | 314 | devc->uvc_threshold = g_ascii_strtod(tokens[1], NULL) / 1000; |
3d70d777 | 315 | g_strfreev(tokens); |
b3e715e5 FS |
316 | g_cond_signal(&devc->uvc_threshold_cond); |
317 | if (devc->uvc_threshold == .0) { | |
7d1a4a52 | 318 | sr_session_send_meta(sdi, SR_CONF_UNDER_VOLTAGE_CONDITION, |
3d70d777 FS |
319 | g_variant_new_boolean(FALSE)); |
320 | } else { | |
7d1a4a52 | 321 | sr_session_send_meta(sdi, SR_CONF_UNDER_VOLTAGE_CONDITION, |
3d70d777 | 322 | g_variant_new_boolean(TRUE)); |
7d1a4a52 | 323 | sr_session_send_meta(sdi, |
3d70d777 | 324 | SR_CONF_UNDER_VOLTAGE_CONDITION_THRESHOLD, |
b3e715e5 | 325 | g_variant_new_double(devc->uvc_threshold)); |
3d70d777 FS |
326 | } |
327 | return; | |
328 | } | |
329 | ||
803db07a UH |
330 | if (!g_str_has_prefix((const char *)devc->buf, "read ")) { |
331 | sr_dbg("Unknown packet: '%s'.", devc->buf); | |
332 | return; | |
333 | } | |
334 | ||
335 | tokens = g_strsplit((const char *)devc->buf, " ", 3); | |
b3e715e5 FS |
336 | devc->voltage = g_ascii_strtod(tokens[2], NULL) / 1000; |
337 | devc->current = g_ascii_strtod(tokens[1], NULL) / 1000; | |
803db07a UH |
338 | g_strfreev(tokens); |
339 | ||
803db07a UH |
340 | /* Begin frame. */ |
341 | packet.type = SR_DF_FRAME_BEGIN; | |
6e68da51 | 342 | packet.payload = NULL; |
803db07a UH |
343 | sr_session_send(sdi, &packet); |
344 | ||
6e68da51 UH |
345 | sr_analog_init(&analog, &encoding, &meaning, &spec, 4); |
346 | ||
347 | packet.type = SR_DF_ANALOG; | |
803db07a UH |
348 | packet.payload = &analog; |
349 | analog.num_samples = 1; | |
350 | ||
351 | /* Voltage */ | |
352 | l = g_slist_copy(sdi->channels); | |
353 | l = g_slist_remove_link(l, g_slist_nth(l, 1)); | |
6e68da51 UH |
354 | meaning.channels = l; |
355 | meaning.mq = SR_MQ_VOLTAGE; | |
356 | meaning.mqflags = SR_MQFLAG_DC; | |
357 | meaning.unit = SR_UNIT_VOLT; | |
d7e348f4 | 358 | encoding.digits = 3; |
b3e715e5 | 359 | analog.data = &devc->voltage; |
803db07a UH |
360 | sr_session_send(sdi, &packet); |
361 | g_slist_free(l); | |
362 | ||
363 | /* Current */ | |
364 | l = g_slist_copy(sdi->channels); | |
365 | l = g_slist_remove_link(l, g_slist_nth(l, 0)); | |
6e68da51 UH |
366 | meaning.channels = l; |
367 | meaning.mq = SR_MQ_CURRENT; | |
368 | meaning.mqflags = SR_MQFLAG_DC; | |
369 | meaning.unit = SR_UNIT_AMPERE; | |
d7e348f4 | 370 | encoding.digits = 3; |
b3e715e5 | 371 | analog.data = &devc->current; |
803db07a UH |
372 | sr_session_send(sdi, &packet); |
373 | g_slist_free(l); | |
374 | ||
375 | /* End frame. */ | |
376 | packet.type = SR_DF_FRAME_END; | |
6e68da51 | 377 | packet.payload = NULL; |
803db07a UH |
378 | sr_session_send(sdi, &packet); |
379 | ||
014c7f93 | 380 | sr_sw_limits_update_samples_read(&devc->limits, 1); |
803db07a UH |
381 | } |
382 | ||
383 | static void handle_new_data(const struct sr_dev_inst *sdi) | |
384 | { | |
385 | int len; | |
386 | struct dev_context *devc; | |
387 | struct sr_serial_dev_inst *serial; | |
a2172899 | 388 | char *buf; |
803db07a UH |
389 | |
390 | devc = sdi->priv; | |
391 | serial = sdi->conn; | |
392 | ||
803db07a | 393 | len = RELOADPRO_BUFSIZE - devc->buflen; |
a2172899 | 394 | buf = devc->buf; |
b3e715e5 | 395 | g_mutex_lock(&devc->acquisition_mutex); |
a2172899 | 396 | if (serial_readline(serial, &buf, &len, 250) != SR_OK) { |
b3e715e5 | 397 | g_mutex_unlock(&devc->acquisition_mutex); |
a2172899 FS |
398 | return; |
399 | } | |
400 | ||
b3e715e5 FS |
401 | if (len == 0) { |
402 | g_mutex_unlock(&devc->acquisition_mutex); | |
803db07a | 403 | return; /* No new bytes, nothing to do. */ |
b3e715e5 | 404 | } |
803db07a UH |
405 | if (len < 0) { |
406 | sr_err("Serial port read error: %d.", len); | |
b3e715e5 | 407 | g_mutex_unlock(&devc->acquisition_mutex); |
803db07a UH |
408 | return; |
409 | } | |
410 | devc->buflen += len; | |
411 | ||
a2172899 | 412 | handle_packet(sdi); |
b3e715e5 | 413 | g_mutex_unlock(&devc->acquisition_mutex); |
a2172899 FS |
414 | memset(devc->buf, 0, RELOADPRO_BUFSIZE); |
415 | devc->buflen = 0; | |
803db07a UH |
416 | } |
417 | ||
418 | SR_PRIV int reloadpro_receive_data(int fd, int revents, void *cb_data) | |
419 | { | |
420 | struct sr_dev_inst *sdi; | |
421 | struct dev_context *devc; | |
6e8d31d4 UH |
422 | |
423 | (void)fd; | |
424 | ||
803db07a UH |
425 | sdi = cb_data; |
426 | devc = sdi->priv; | |
427 | ||
428 | if (revents != G_IO_IN) | |
6e8d31d4 UH |
429 | return TRUE; |
430 | ||
803db07a UH |
431 | handle_new_data(sdi); |
432 | ||
014c7f93 | 433 | if (sr_sw_limits_check(&devc->limits)) |
d2f7c417 | 434 | sr_dev_acquisition_stop(sdi); |
6e8d31d4 UH |
435 | |
436 | return TRUE; | |
437 | } |