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/>.
24 #define READ_TIMEOUT_MS 1000
26 static int send_cmd(const struct sr_dev_inst *sdi, const char *cmd,
27 char *replybuf, int replybufsize)
31 struct sr_serial_dev_inst *serial;
35 /* Send the command (blocking, with timeout). */
36 if ((ret = serial_write_blocking(serial, cmd,
37 strlen(cmd), serial_timeout(serial,
38 strlen(cmd)))) < (int)strlen(cmd)) {
39 sr_err("Unable to send command.");
43 /* Read the reply (blocking, with timeout). */
44 memset(replybuf, 0, replybufsize);
47 ret = serial_readline(serial, &bufptr, &len, READ_TIMEOUT_MS);
49 /* If we got 0 characters (possibly one \r or \n), retry once. */
52 ret = serial_readline(serial, &bufptr, &len, READ_TIMEOUT_MS);
55 if (g_str_has_prefix((const char *)&bufptr, "err ")) {
56 sr_err("Device replied with an error: '%s'.", bufptr);
63 SR_PRIV int reloadpro_set_current_limit(const struct sr_dev_inst *sdi,
70 if (current < 0 || current > 6) {
71 sr_err("The current limit must be 0-6 A (was %f A).", current);
75 /* Hardware expects current in mA, integer (0..6000). */
76 ma = (int)(current * 1000);
78 sr_err("Setting current limit to %f A (%d mA).", current, ma);
80 cmd = g_strdup_printf("set %d\n", ma);
81 if ((ret = send_cmd(sdi, cmd, (char *)&buf, sizeof(buf))) < 0) {
82 sr_err("Error sending current limit command: %d.", ret);
91 SR_PRIV int reloadpro_set_on_off(const struct sr_dev_inst *sdi, gboolean on)
97 cmd = (on) ? "on\n" : "off\n";
98 if ((ret = send_cmd(sdi, cmd, (char *)&buf, sizeof(buf))) < 0) {
99 sr_err("Error sending on/off command: %d.", ret);
106 SR_PRIV int reloadpro_get_current_limit(const struct sr_dev_inst *sdi,
112 if ((ret = send_cmd(sdi, "set\n", (char *)&buf, sizeof(buf))) < 0) {
113 sr_err("Error sending current limit query: %d.", ret);
117 /* Hardware sends current in mA, integer (0..6000). */
118 *current = g_ascii_strtod(buf + 4, NULL) / 1000;
123 SR_PRIV int reloadpro_get_voltage_current(const struct sr_dev_inst *sdi,
124 float *voltage, float *current)
130 if ((ret = send_cmd(sdi, "read\n", (char *)&buf, sizeof(buf))) < 0) {
131 sr_err("Error sending voltage/current query: %d.", ret);
135 /* Reply: "read <current> <voltage>". */
136 tokens = g_strsplit((const char *)&buf, " ", 3);
138 *voltage = g_ascii_strtod(tokens[2], NULL) / 1000;
140 *current = g_ascii_strtod(tokens[1], NULL) / 1000;
146 static void handle_packet(const struct sr_dev_inst *sdi)
148 float voltage, current;
149 struct sr_datafeed_packet packet;
150 struct sr_datafeed_analog analog;
151 struct sr_analog_encoding encoding;
152 struct sr_analog_meaning meaning;
153 struct sr_analog_spec spec;
154 struct dev_context *devc;
160 if (g_str_has_prefix((const char *)devc->buf, "overtemp")) {
161 sr_warn("Overtemperature condition!");
162 devc->otp_active = TRUE;
166 if (g_str_has_prefix((const char *)devc->buf, "undervolt")) {
167 sr_warn("Undervoltage condition!");
168 devc->uvc_active = TRUE;
172 if (!g_str_has_prefix((const char *)devc->buf, "read ")) {
173 sr_dbg("Unknown packet: '%s'.", devc->buf);
177 tokens = g_strsplit((const char *)devc->buf, " ", 3);
178 voltage = g_ascii_strtod(tokens[2], NULL) / 1000;
179 current = g_ascii_strtod(tokens[1], NULL) / 1000;
183 packet.type = SR_DF_FRAME_BEGIN;
184 packet.payload = NULL;
185 sr_session_send(sdi, &packet);
187 sr_analog_init(&analog, &encoding, &meaning, &spec, 4);
189 packet.type = SR_DF_ANALOG;
190 packet.payload = &analog;
191 analog.num_samples = 1;
194 l = g_slist_copy(sdi->channels);
195 l = g_slist_remove_link(l, g_slist_nth(l, 1));
196 meaning.channels = l;
197 meaning.mq = SR_MQ_VOLTAGE;
198 meaning.mqflags = SR_MQFLAG_DC;
199 meaning.unit = SR_UNIT_VOLT;
200 analog.data = &voltage;
201 sr_session_send(sdi, &packet);
205 l = g_slist_copy(sdi->channels);
206 l = g_slist_remove_link(l, g_slist_nth(l, 0));
207 meaning.channels = l;
208 meaning.mq = SR_MQ_CURRENT;
209 meaning.mqflags = SR_MQFLAG_DC;
210 meaning.unit = SR_UNIT_AMPERE;
211 analog.data = ¤t;
212 sr_session_send(sdi, &packet);
216 packet.type = SR_DF_FRAME_END;
217 packet.payload = NULL;
218 sr_session_send(sdi, &packet);
220 sr_sw_limits_update_samples_read(&devc->limits, 1);
223 static void handle_new_data(const struct sr_dev_inst *sdi)
226 struct dev_context *devc;
227 struct sr_serial_dev_inst *serial;
232 /* Try to get as much data as the buffer can hold. */
233 len = RELOADPRO_BUFSIZE - devc->buflen;
234 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len);
236 return; /* No new bytes, nothing to do. */
238 sr_err("Serial port read error: %d.", len);
243 if (g_str_has_suffix((const char *)devc->buf, "\n")) {
245 memset(devc->buf, 0, RELOADPRO_BUFSIZE);
250 SR_PRIV int reloadpro_receive_data(int fd, int revents, void *cb_data)
252 struct sr_dev_inst *sdi;
253 struct dev_context *devc;
260 if (revents != G_IO_IN)
263 handle_new_data(sdi);
265 if (sr_sw_limits_check(&devc->limits))
266 sdi->driver->dev_acquisition_stop(sdi);