2 * This file is part of the libsigrok project.
4 * Copyright (C) 2015-2016 Uwe Hermann <uwe@hermann-uwe.de>
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.
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/>.
25 #define READ_TIMEOUT_MS 500
27 static int send_cmd(const struct sr_dev_inst *sdi, const char *cmd,
28 char *replybuf, int replybufsize)
32 struct dev_context *devc;
33 struct sr_serial_dev_inst *serial;
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.");
46 if (!devc->acquisition_running) {
47 /* Read the reply (blocking, with timeout). */
48 memset(replybuf, 0, replybufsize);
51 ret = serial_readline(serial, &bufptr, &len, READ_TIMEOUT_MS);
53 /* If we got 0 characters (possibly one \r or \n), retry once. */
56 ret = serial_readline(serial, &bufptr, &len, READ_TIMEOUT_MS);
59 if (g_str_has_prefix((const char *)&bufptr, "err ")) {
60 sr_err("Device replied with an error: '%s'.", bufptr);
68 SR_PRIV int reloadpro_set_current_limit(const struct sr_dev_inst *sdi,
71 struct dev_context *devc;
78 if (current_limit < 0 || current_limit > 6) {
79 sr_err("The current limit must be 0-6 A (was %f A).", current_limit);
83 /* Hardware expects current limit in mA, integer (0..6000). */
84 ma = (int)round(current_limit * 1000);
86 cmd = g_strdup_printf("set %d\n", ma);
87 g_mutex_lock(&devc->acquisition_mutex);
88 ret = send_cmd(sdi, cmd, (char *)&buf, sizeof(buf));
89 g_mutex_unlock(&devc->acquisition_mutex);
93 sr_err("Error sending current limit command: %d.", ret);
99 SR_PRIV int reloadpro_set_on_off(const struct sr_dev_inst *sdi, gboolean on)
101 struct dev_context *devc;
108 cmd = (on) ? "on\n" : "off\n";
109 g_mutex_lock(&devc->acquisition_mutex);
110 ret = send_cmd(sdi, cmd, (char *)&buf, sizeof(buf));
111 g_mutex_unlock(&devc->acquisition_mutex);
114 sr_err("Error sending on/off command: %d.", ret);
120 SR_PRIV int reloadpro_set_under_voltage_threshold(const struct sr_dev_inst *sdi,
123 struct dev_context *devc;
130 if (voltage < 0 || voltage > 60) {
131 sr_err("The under voltage threshold must be 0-60 V (was %f V).",
136 /* Hardware expects voltage in mV, integer (0..60000). */
137 mv = (int)round(voltage * 1000);
139 sr_spew("Setting under voltage threshold to %f V (%d mV).", voltage, mv);
141 cmd = g_strdup_printf("uvlo %d\n", mv);
142 g_mutex_lock(&devc->acquisition_mutex);
143 ret = send_cmd(sdi, cmd, (char *)&buf, sizeof(buf));
144 g_mutex_unlock(&devc->acquisition_mutex);
148 sr_err("Error sending under voltage threshold command: %d.", ret);
154 SR_PRIV int reloadpro_get_current_limit(const struct sr_dev_inst *sdi,
155 float *current_limit)
157 struct dev_context *devc;
164 g_mutex_lock(&devc->acquisition_mutex);
165 if ((ret = send_cmd(sdi, "set\n", (char *)&buf, sizeof(buf))) < 0) {
166 sr_err("Error sending current limit query: %d.", ret);
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)) {
174 /* Timeout has passed. */
175 g_mutex_unlock(&devc->acquisition_mutex);
179 /* Hardware sends current limit in mA, integer (0..6000). */
180 devc->current_limit = g_ascii_strtod(buf + 4, NULL) / 1000;
182 g_mutex_unlock(&devc->acquisition_mutex);
185 *current_limit = devc->current_limit;
190 SR_PRIV int reloadpro_get_under_voltage_threshold(const struct sr_dev_inst *sdi,
191 float *uvc_threshold)
193 struct dev_context *devc;
200 g_mutex_lock(&devc->acquisition_mutex);
201 if ((ret = send_cmd(sdi, "uvlo\n", (char *)&buf, sizeof(buf))) < 0) {
202 sr_err("Error sending under voltage threshold query: %d.", ret);
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)) {
210 /* Timeout has passed. */
211 g_mutex_unlock(&devc->acquisition_mutex);
215 /* Hardware sends voltage in mV, integer (0..60000). */
216 devc->uvc_threshold = g_ascii_strtod(buf + 5, NULL) / 1000;
218 g_mutex_unlock(&devc->acquisition_mutex);
221 *uvc_threshold = devc->uvc_threshold;
226 SR_PRIV int reloadpro_get_voltage_current(const struct sr_dev_inst *sdi,
227 float *voltage, float *current)
229 struct dev_context *devc;
237 g_mutex_lock(&devc->acquisition_mutex);
238 if ((ret = send_cmd(sdi, "read\n", (char *)&buf, sizeof(buf))) < 0) {
239 sr_err("Error sending voltage/current query: %d.", ret);
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)) {
247 /* Timeout has passed. */
248 g_mutex_unlock(&devc->acquisition_mutex);
252 /* Reply: "read <current> <voltage>". */
253 tokens = g_strsplit((const char *)&buf, " ", 3);
254 devc->voltage = g_ascii_strtod(tokens[2], NULL) / 1000;
255 devc->current = g_ascii_strtod(tokens[1], NULL) / 1000;
258 g_mutex_unlock(&devc->acquisition_mutex);
261 *voltage = devc->voltage;
263 *current = devc->current;
268 static int send_config_update_key(const struct sr_dev_inst *sdi,
269 uint32_t key, GVariant *var)
271 struct sr_config *cfg;
272 struct sr_datafeed_packet packet;
273 struct sr_datafeed_meta meta;
276 cfg = sr_config_new(key, var);
280 memset(&meta, 0, sizeof(meta));
282 packet.type = SR_DF_META;
283 packet.payload = &meta;
285 meta.config = g_slist_append(meta.config, cfg);
287 ret = sr_session_send(sdi, &packet);
293 static void handle_packet(const struct sr_dev_inst *sdi)
295 struct sr_datafeed_packet packet;
296 struct sr_datafeed_analog analog;
297 struct sr_analog_encoding encoding;
298 struct sr_analog_meaning meaning;
299 struct sr_analog_spec spec;
300 struct dev_context *devc;
306 if (g_str_has_prefix((const char *)devc->buf, "overtemp")) {
307 sr_warn("Overtemperature condition!");
308 devc->otp_active = TRUE;
309 send_config_update_key(sdi, SR_CONF_OVER_TEMPERATURE_PROTECTION_ACTIVE,
310 g_variant_new_boolean(TRUE));
314 if (g_str_has_prefix((const char *)devc->buf, "undervolt")) {
315 sr_warn("Undervoltage condition!");
316 devc->uvc_active = TRUE;
317 send_config_update_key(sdi, SR_CONF_UNDER_VOLTAGE_CONDITION_ACTIVE,
318 g_variant_new_boolean(TRUE));
322 if (g_str_has_prefix((const char *)devc->buf, "err ")) {
323 sr_err("Device replied with an error: '%s'.", devc->buf);
327 if (g_str_has_prefix((const char *)devc->buf, "set ")) {
328 tokens = g_strsplit((const char *)devc->buf, " ", 2);
329 devc->current_limit = g_ascii_strtod(tokens[1], NULL) / 1000;
331 g_cond_signal(&devc->current_limit_cond);
332 send_config_update_key(sdi, SR_CONF_CURRENT_LIMIT,
333 g_variant_new_double(devc->current_limit));
337 if (g_str_has_prefix((const char *)devc->buf, "uvlo ")) {
338 tokens = g_strsplit((const char *)devc->buf, " ", 2);
339 devc->uvc_threshold = g_ascii_strtod(tokens[1], NULL) / 1000;
341 g_cond_signal(&devc->uvc_threshold_cond);
342 if (devc->uvc_threshold == .0) {
343 send_config_update_key(sdi, SR_CONF_UNDER_VOLTAGE_CONDITION,
344 g_variant_new_boolean(FALSE));
346 send_config_update_key(sdi, SR_CONF_UNDER_VOLTAGE_CONDITION,
347 g_variant_new_boolean(TRUE));
348 send_config_update_key(sdi,
349 SR_CONF_UNDER_VOLTAGE_CONDITION_THRESHOLD,
350 g_variant_new_double(devc->uvc_threshold));
355 if (!g_str_has_prefix((const char *)devc->buf, "read ")) {
356 sr_dbg("Unknown packet: '%s'.", devc->buf);
360 tokens = g_strsplit((const char *)devc->buf, " ", 3);
361 devc->voltage = g_ascii_strtod(tokens[2], NULL) / 1000;
362 devc->current = g_ascii_strtod(tokens[1], NULL) / 1000;
366 packet.type = SR_DF_FRAME_BEGIN;
367 packet.payload = NULL;
368 sr_session_send(sdi, &packet);
370 sr_analog_init(&analog, &encoding, &meaning, &spec, 4);
372 packet.type = SR_DF_ANALOG;
373 packet.payload = &analog;
374 analog.num_samples = 1;
377 l = g_slist_copy(sdi->channels);
378 l = g_slist_remove_link(l, g_slist_nth(l, 1));
379 meaning.channels = l;
380 meaning.mq = SR_MQ_VOLTAGE;
381 meaning.mqflags = SR_MQFLAG_DC;
382 meaning.unit = SR_UNIT_VOLT;
384 analog.data = &devc->voltage;
385 sr_session_send(sdi, &packet);
389 l = g_slist_copy(sdi->channels);
390 l = g_slist_remove_link(l, g_slist_nth(l, 0));
391 meaning.channels = l;
392 meaning.mq = SR_MQ_CURRENT;
393 meaning.mqflags = SR_MQFLAG_DC;
394 meaning.unit = SR_UNIT_AMPERE;
396 analog.data = &devc->current;
397 sr_session_send(sdi, &packet);
401 packet.type = SR_DF_FRAME_END;
402 packet.payload = NULL;
403 sr_session_send(sdi, &packet);
405 sr_sw_limits_update_samples_read(&devc->limits, 1);
408 static void handle_new_data(const struct sr_dev_inst *sdi)
411 struct dev_context *devc;
412 struct sr_serial_dev_inst *serial;
418 len = RELOADPRO_BUFSIZE - devc->buflen;
420 g_mutex_lock(&devc->acquisition_mutex);
421 if (serial_readline(serial, &buf, &len, 250) != SR_OK) {
422 g_mutex_unlock(&devc->acquisition_mutex);
427 g_mutex_unlock(&devc->acquisition_mutex);
428 return; /* No new bytes, nothing to do. */
431 sr_err("Serial port read error: %d.", len);
432 g_mutex_unlock(&devc->acquisition_mutex);
438 g_mutex_unlock(&devc->acquisition_mutex);
439 memset(devc->buf, 0, RELOADPRO_BUFSIZE);
443 SR_PRIV int reloadpro_receive_data(int fd, int revents, void *cb_data)
445 struct sr_dev_inst *sdi;
446 struct dev_context *devc;
453 if (revents != G_IO_IN)
456 handle_new_data(sdi);
458 if (sr_sw_limits_check(&devc->limits))
459 sr_dev_acquisition_stop(sdi);