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 1000
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,
75 if (current < 0 || current > 6) {
76 sr_err("The current limit must be 0-6 A (was %f A).", current);
80 /* Hardware expects current in mA, integer (0..6000). */
81 ma = (int)round(current * 1000);
83 sr_spew("Setting current limit to %f A (%d mA).", current, ma);
85 cmd = g_strdup_printf("set %d\n", ma);
86 if ((ret = send_cmd(sdi, cmd, (char *)&buf, sizeof(buf))) < 0) {
87 sr_err("Error sending current limit command: %d.", ret);
96 SR_PRIV int reloadpro_set_on_off(const struct sr_dev_inst *sdi, gboolean on)
102 cmd = (on) ? "on\n" : "off\n";
103 if ((ret = send_cmd(sdi, cmd, (char *)&buf, sizeof(buf))) < 0) {
104 sr_err("Error sending on/off command: %d.", ret);
111 SR_PRIV int reloadpro_set_under_voltage_threshold(const struct sr_dev_inst *sdi,
118 if (voltage < 0 || voltage > 60) {
119 sr_err("The under voltage threshold must be 0-60 V (was %f V).",
124 /* Hardware expects voltage in mV, integer (0..60000). */
125 mv = (int)round(voltage * 1000);
127 sr_spew("Setting under voltage threshold to %f V (%d mV).", voltage, mv);
129 cmd = g_strdup_printf("uvlo %d\n", mv);
130 if ((ret = send_cmd(sdi, cmd, (char *)&buf, sizeof(buf))) < 0) {
131 sr_err("Error sending under voltage threshold command: %d.", ret);
140 SR_PRIV int reloadpro_get_current_limit(const struct sr_dev_inst *sdi,
145 struct dev_context *devc;
149 if ((ret = send_cmd(sdi, "set\n", (char *)&buf, sizeof(buf))) < 0) {
150 sr_err("Error sending current limit query: %d.", ret);
154 if (!devc->acquisition_running) {
155 /* Hardware sends current in mA, integer (0..6000). */
156 *current = g_ascii_strtod(buf + 4, NULL) / 1000;
162 SR_PRIV int reloadpro_get_under_voltage_threshold(const struct sr_dev_inst *sdi,
167 struct dev_context *devc;
171 if ((ret = send_cmd(sdi, "uvlo\n", (char *)&buf, sizeof(buf))) < 0) {
172 sr_err("Error sending under voltage threshold query: %d.", ret);
176 if (!devc->acquisition_running) {
177 /* Hardware sends voltage in mV, integer (0..60000). */
178 *voltage = g_ascii_strtod(buf + 5, NULL) / 1000;
184 SR_PRIV int reloadpro_get_voltage_current(const struct sr_dev_inst *sdi,
185 float *voltage, float *current)
190 struct dev_context *devc;
194 if ((ret = send_cmd(sdi, "read\n", (char *)&buf, sizeof(buf))) < 0) {
195 sr_err("Error sending voltage/current query: %d.", ret);
199 if (!devc->acquisition_running) {
200 /* Reply: "read <current> <voltage>". */
201 tokens = g_strsplit((const char *)&buf, " ", 3);
203 *voltage = g_ascii_strtod(tokens[2], NULL) / 1000;
205 *current = g_ascii_strtod(tokens[1], NULL) / 1000;
212 static int send_config_update_key(const struct sr_dev_inst *sdi,
213 uint32_t key, GVariant *var)
215 struct sr_config *cfg;
216 struct sr_datafeed_packet packet;
217 struct sr_datafeed_meta meta;
220 cfg = sr_config_new(key, var);
224 memset(&meta, 0, sizeof(meta));
226 packet.type = SR_DF_META;
227 packet.payload = &meta;
229 meta.config = g_slist_append(meta.config, cfg);
231 ret = sr_session_send(sdi, &packet);
237 static void handle_packet(const struct sr_dev_inst *sdi)
239 float voltage, current;
240 struct sr_datafeed_packet packet;
241 struct sr_datafeed_analog analog;
242 struct sr_analog_encoding encoding;
243 struct sr_analog_meaning meaning;
244 struct sr_analog_spec spec;
245 struct dev_context *devc;
251 if (g_str_has_prefix((const char *)devc->buf, "overtemp")) {
252 sr_warn("Overtemperature condition!");
253 devc->otp_active = TRUE;
254 send_config_update_key(sdi, SR_CONF_OVER_TEMPERATURE_PROTECTION_ACTIVE,
255 g_variant_new_boolean(TRUE));
259 if (g_str_has_prefix((const char *)devc->buf, "undervolt")) {
260 sr_warn("Undervoltage condition!");
261 devc->uvc_active = TRUE;
262 send_config_update_key(sdi, SR_CONF_UNDER_VOLTAGE_CONDITION_ACTIVE,
263 g_variant_new_boolean(TRUE));
267 if (g_str_has_prefix((const char *)devc->buf, "err ")) {
268 sr_err("Device replied with an error: '%s'.", devc->buf);
272 if (g_str_has_prefix((const char *)devc->buf, "set ")) {
273 tokens = g_strsplit((const char *)devc->buf, " ", 2);
274 current = g_ascii_strtod(tokens[1], NULL) / 1000;
276 send_config_update_key(sdi, SR_CONF_CURRENT_LIMIT,
277 g_variant_new_double(current));
281 if (g_str_has_prefix((const char *)devc->buf, "uvlo ")) {
282 tokens = g_strsplit((const char *)devc->buf, " ", 2);
283 voltage = g_ascii_strtod(tokens[1], NULL) / 1000;
286 send_config_update_key(sdi, SR_CONF_UNDER_VOLTAGE_CONDITION,
287 g_variant_new_boolean(FALSE));
289 send_config_update_key(sdi, SR_CONF_UNDER_VOLTAGE_CONDITION,
290 g_variant_new_boolean(TRUE));
291 send_config_update_key(sdi,
292 SR_CONF_UNDER_VOLTAGE_CONDITION_THRESHOLD,
293 g_variant_new_double(voltage));
298 if (!g_str_has_prefix((const char *)devc->buf, "read ")) {
299 sr_dbg("Unknown packet: '%s'.", devc->buf);
303 tokens = g_strsplit((const char *)devc->buf, " ", 3);
304 voltage = g_ascii_strtod(tokens[2], NULL) / 1000;
305 current = g_ascii_strtod(tokens[1], NULL) / 1000;
309 packet.type = SR_DF_FRAME_BEGIN;
310 packet.payload = NULL;
311 sr_session_send(sdi, &packet);
313 sr_analog_init(&analog, &encoding, &meaning, &spec, 4);
315 packet.type = SR_DF_ANALOG;
316 packet.payload = &analog;
317 analog.num_samples = 1;
320 l = g_slist_copy(sdi->channels);
321 l = g_slist_remove_link(l, g_slist_nth(l, 1));
322 meaning.channels = l;
323 meaning.mq = SR_MQ_VOLTAGE;
324 meaning.mqflags = SR_MQFLAG_DC;
325 meaning.unit = SR_UNIT_VOLT;
326 analog.data = &voltage;
327 sr_session_send(sdi, &packet);
331 l = g_slist_copy(sdi->channels);
332 l = g_slist_remove_link(l, g_slist_nth(l, 0));
333 meaning.channels = l;
334 meaning.mq = SR_MQ_CURRENT;
335 meaning.mqflags = SR_MQFLAG_DC;
336 meaning.unit = SR_UNIT_AMPERE;
337 analog.data = ¤t;
338 sr_session_send(sdi, &packet);
342 packet.type = SR_DF_FRAME_END;
343 packet.payload = NULL;
344 sr_session_send(sdi, &packet);
346 sr_sw_limits_update_samples_read(&devc->limits, 1);
349 static void handle_new_data(const struct sr_dev_inst *sdi)
352 struct dev_context *devc;
353 struct sr_serial_dev_inst *serial;
359 len = RELOADPRO_BUFSIZE - devc->buflen;
361 if (serial_readline(serial, &buf, &len, 250) != SR_OK) {
367 return; /* No new bytes, nothing to do. */
369 sr_err("Serial port read error: %d.", len);
375 memset(devc->buf, 0, RELOADPRO_BUFSIZE);
379 SR_PRIV int reloadpro_receive_data(int fd, int revents, void *cb_data)
381 struct sr_dev_inst *sdi;
382 struct dev_context *devc;
389 if (revents != G_IO_IN)
392 handle_new_data(sdi);
394 if (sr_sw_limits_check(&devc->limits))
395 sr_dev_acquisition_stop(sdi);