]> sigrok.org Git - libsigrok.git/blame_incremental - src/hardware/arachnid-labs-re-load-pro/protocol.c
arachnid-labs-re-load-pro: Setting correct current limit.
[libsigrok.git] / src / hardware / arachnid-labs-re-load-pro / protocol.c
... / ...
CommitLineData
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2015-2016 Uwe Hermann <uwe@hermann-uwe.de>
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
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <config.h>
21#include <math.h>
22#include <string.h>
23#include "protocol.h"
24
25#define READ_TIMEOUT_MS 1000
26
27static 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;
32 struct dev_context *devc;
33 struct sr_serial_dev_inst *serial;
34
35 devc = sdi->priv;
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
46 if (!devc->acquisition_running) {
47 /* Read the reply (blocking, with timeout). */
48 memset(replybuf, 0, replybufsize);
49 bufptr = replybuf;
50 len = replybufsize;
51 ret = serial_readline(serial, &bufptr, &len, READ_TIMEOUT_MS);
52
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 }
63 }
64
65 return ret;
66}
67
68SR_PRIV int reloadpro_set_current_limit(const struct sr_dev_inst *sdi,
69 float current)
70{
71 int ret, ma;
72 char buf[100];
73 char *cmd;
74
75 if (current < 0 || current > 6) {
76 sr_err("The current limit must be 0-6 A (was %f A).", current);
77 return SR_ERR_ARG;
78 }
79
80 /* Hardware expects current in mA, integer (0..6000). */
81 ma = (int)round(current * 1000);
82
83 sr_err("Setting current limit to %f A (%d mA).", current, ma);
84
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);
88 g_free(cmd);
89 return SR_ERR;
90 }
91 g_free(cmd);
92
93 return SR_OK;
94}
95
96SR_PRIV int reloadpro_set_on_off(const struct sr_dev_inst *sdi, gboolean on)
97{
98 int ret;
99 char buf[100];
100 const char *cmd;
101
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);
105 return SR_ERR;
106 }
107
108 return SR_OK;
109}
110
111SR_PRIV int reloadpro_get_current_limit(const struct sr_dev_inst *sdi,
112 float *current)
113{
114 int ret;
115 char buf[100];
116 struct dev_context *devc;
117
118 devc = sdi->priv;
119
120 if ((ret = send_cmd(sdi, "set\n", (char *)&buf, sizeof(buf))) < 0) {
121 sr_err("Error sending current limit query: %d.", ret);
122 return SR_ERR;
123 }
124
125 if (!devc->acquisition_running) {
126 /* Hardware sends current in mA, integer (0..6000). */
127 *current = g_ascii_strtod(buf + 4, NULL) / 1000;
128 }
129
130 return SR_OK;
131}
132
133SR_PRIV int reloadpro_get_voltage_current(const struct sr_dev_inst *sdi,
134 float *voltage, float *current)
135{
136 int ret;
137 char buf[100];
138 char **tokens;
139 struct dev_context *devc;
140
141 devc = sdi->priv;
142
143 if ((ret = send_cmd(sdi, "read\n", (char *)&buf, sizeof(buf))) < 0) {
144 sr_err("Error sending voltage/current query: %d.", ret);
145 return SR_ERR;
146 }
147
148 if (!devc->acquisition_running) {
149 /* Reply: "read <current> <voltage>". */
150 tokens = g_strsplit((const char *)&buf, " ", 3);
151 if (voltage)
152 *voltage = g_ascii_strtod(tokens[2], NULL) / 1000;
153 if (current)
154 *current = g_ascii_strtod(tokens[1], NULL) / 1000;
155 g_strfreev(tokens);
156 }
157
158 return SR_OK;
159}
160
161static int send_config_update_key(const struct sr_dev_inst *sdi,
162 uint32_t key, GVariant *var)
163{
164 struct sr_config *cfg;
165 struct sr_datafeed_packet packet;
166 struct sr_datafeed_meta meta;
167 int ret;
168
169 cfg = sr_config_new(key, var);
170 if (!cfg)
171 return SR_ERR;
172
173 memset(&meta, 0, sizeof(meta));
174
175 packet.type = SR_DF_META;
176 packet.payload = &meta;
177
178 meta.config = g_slist_append(meta.config, cfg);
179
180 ret = sr_session_send(sdi, &packet);
181 sr_config_free(cfg);
182
183 return ret;
184}
185
186static void handle_packet(const struct sr_dev_inst *sdi)
187{
188 float voltage, current;
189 struct sr_datafeed_packet packet;
190 struct sr_datafeed_analog analog;
191 struct sr_analog_encoding encoding;
192 struct sr_analog_meaning meaning;
193 struct sr_analog_spec spec;
194 struct dev_context *devc;
195 char **tokens;
196 GSList *l;
197
198 devc = sdi->priv;
199
200 if (g_str_has_prefix((const char *)devc->buf, "overtemp")) {
201 sr_warn("Overtemperature condition!");
202 devc->otp_active = TRUE;
203 send_config_update_key(sdi, SR_CONF_OVER_TEMPERATURE_PROTECTION_ACTIVE,
204 g_variant_new_boolean(TRUE));
205 return;
206 }
207
208 if (g_str_has_prefix((const char *)devc->buf, "undervolt")) {
209 sr_warn("Undervoltage condition!");
210 devc->uvc_active = TRUE;
211 send_config_update_key(sdi, SR_CONF_UNDER_VOLTAGE_CONDITION_ACTIVE,
212 g_variant_new_boolean(TRUE));
213 return;
214 }
215
216 if (g_str_has_prefix((const char *)devc->buf, "err ")) {
217 sr_err("Device replied with an error: '%s'.", devc->buf);
218 return;
219 }
220
221 if (g_str_has_prefix((const char *)devc->buf, "set ")) {
222 tokens = g_strsplit((const char *)devc->buf, " ", 2);
223 current = g_ascii_strtod(tokens[1], NULL) / 1000;
224 g_strfreev(tokens);
225 send_config_update_key(sdi, SR_CONF_CURRENT_LIMIT,
226 g_variant_new_double(current));
227 return;
228 }
229
230 if (!g_str_has_prefix((const char *)devc->buf, "read ")) {
231 sr_dbg("Unknown packet: '%s'.", devc->buf);
232 return;
233 }
234
235 tokens = g_strsplit((const char *)devc->buf, " ", 3);
236 voltage = g_ascii_strtod(tokens[2], NULL) / 1000;
237 current = g_ascii_strtod(tokens[1], NULL) / 1000;
238 g_strfreev(tokens);
239
240 /* Begin frame. */
241 packet.type = SR_DF_FRAME_BEGIN;
242 packet.payload = NULL;
243 sr_session_send(sdi, &packet);
244
245 sr_analog_init(&analog, &encoding, &meaning, &spec, 4);
246
247 packet.type = SR_DF_ANALOG;
248 packet.payload = &analog;
249 analog.num_samples = 1;
250
251 /* Voltage */
252 l = g_slist_copy(sdi->channels);
253 l = g_slist_remove_link(l, g_slist_nth(l, 1));
254 meaning.channels = l;
255 meaning.mq = SR_MQ_VOLTAGE;
256 meaning.mqflags = SR_MQFLAG_DC;
257 meaning.unit = SR_UNIT_VOLT;
258 analog.data = &voltage;
259 sr_session_send(sdi, &packet);
260 g_slist_free(l);
261
262 /* Current */
263 l = g_slist_copy(sdi->channels);
264 l = g_slist_remove_link(l, g_slist_nth(l, 0));
265 meaning.channels = l;
266 meaning.mq = SR_MQ_CURRENT;
267 meaning.mqflags = SR_MQFLAG_DC;
268 meaning.unit = SR_UNIT_AMPERE;
269 analog.data = &current;
270 sr_session_send(sdi, &packet);
271 g_slist_free(l);
272
273 /* End frame. */
274 packet.type = SR_DF_FRAME_END;
275 packet.payload = NULL;
276 sr_session_send(sdi, &packet);
277
278 sr_sw_limits_update_samples_read(&devc->limits, 1);
279}
280
281static void handle_new_data(const struct sr_dev_inst *sdi)
282{
283 int len;
284 struct dev_context *devc;
285 struct sr_serial_dev_inst *serial;
286 char *buf;
287
288 devc = sdi->priv;
289 serial = sdi->conn;
290
291 len = RELOADPRO_BUFSIZE - devc->buflen;
292 buf = devc->buf;
293 if (serial_readline(serial, &buf, &len, 250) != SR_OK) {
294 sr_err("Err: ");
295 return;
296 }
297
298 if (len == 0)
299 return; /* No new bytes, nothing to do. */
300 if (len < 0) {
301 sr_err("Serial port read error: %d.", len);
302 return;
303 }
304 devc->buflen += len;
305
306 handle_packet(sdi);
307 memset(devc->buf, 0, RELOADPRO_BUFSIZE);
308 devc->buflen = 0;
309}
310
311SR_PRIV int reloadpro_receive_data(int fd, int revents, void *cb_data)
312{
313 struct sr_dev_inst *sdi;
314 struct dev_context *devc;
315
316 (void)fd;
317
318 sdi = cb_data;
319 devc = sdi->priv;
320
321 if (revents != G_IO_IN)
322 return TRUE;
323
324 handle_new_data(sdi);
325
326 if (sr_sw_limits_check(&devc->limits))
327 sr_dev_acquisition_stop(sdi);
328
329 return TRUE;
330}