]> sigrok.org Git - libsigrok.git/blob - src/hardware/arachnid-labs-re-load-pro/protocol.c
arachnid-labs-re-load-pro: Add initial driver.
[libsigrok.git] / src / hardware / arachnid-labs-re-load-pro / protocol.c
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, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include <config.h>
22 #include <string.h>
23 #include "protocol.h"
24
25 #define READ_TIMEOUT_MS 1000
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;
32         struct sr_serial_dev_inst *serial;
33
34         serial = sdi->conn;
35
36         /* Send the command (blocking, with timeout). */
37         if ((ret = serial_write_blocking(serial, cmd,
38                         strlen(cmd), serial_timeout(serial,
39                         strlen(cmd)))) < (int)strlen(cmd)) {
40                 sr_err("Unable to send command.");
41                 return SR_ERR;
42         }
43
44         /* Read the reply (blocking, with timeout). */
45         memset(replybuf, 0, replybufsize);
46         bufptr = replybuf;
47         len = replybufsize;
48         ret = serial_readline(serial, &bufptr, &len, READ_TIMEOUT_MS);
49
50         /* If we got 0 characters (possibly one \r or \n), retry once. */
51         if (len == 0) {
52                 len = replybufsize;
53                 ret = serial_readline(serial, &bufptr, &len, READ_TIMEOUT_MS);
54         }
55
56         if (g_str_has_prefix((const char *)&bufptr, "err ")) {
57                 sr_err("Device replied with an error: '%s'.", bufptr);
58                 return SR_ERR;
59         }
60
61         return ret;
62 }
63
64 SR_PRIV int reloadpro_set_current_limit(const struct sr_dev_inst *sdi,
65                                         float current)
66 {
67         int ret, ma;
68         char buf[100];
69         char *cmd;
70
71         if (current < 0 || current > 6) {
72                 sr_err("The current limit must be 0-6 A (was %f A).", current);
73                 return SR_ERR_ARG;
74         }
75
76         /* Hardware expects current in mA, integer (0..6000). */
77         ma = (int)(current * 1000);
78
79         sr_err("Setting current limit to %f A (%d mA).", current, ma);
80
81         cmd = g_strdup_printf("set %d\n", ma);
82         if ((ret = send_cmd(sdi, cmd, (char *)&buf, sizeof(buf))) < 0) {
83                 sr_err("Error sending current limit command: %d.", ret);
84                 g_free(cmd);
85                 return SR_ERR;
86         }
87         g_free(cmd);
88
89         return SR_OK;
90 }
91
92 SR_PRIV int reloadpro_get_current_limit(const struct sr_dev_inst *sdi,
93                                         float *current)
94 {
95         int ret;
96         char buf[100];
97
98         if ((ret = send_cmd(sdi, "set\n", (char *)&buf, sizeof(buf))) < 0) {
99                 sr_err("Error sending current limit query: %d.", ret);
100                 return SR_ERR;
101         }
102
103         /* Hardware sends current in mA, integer (0..6000). */
104         *current = g_ascii_strtod(buf + 4, NULL) / 1000;
105
106         return SR_OK;
107 }
108
109 SR_PRIV int reloadpro_get_voltage_current(const struct sr_dev_inst *sdi,
110                 float *voltage, float *current)
111 {
112         int ret;
113         char buf[100];
114         char **tokens;
115
116         if ((ret = send_cmd(sdi, "read\n", (char *)&buf, sizeof(buf))) < 0) {
117                 sr_err("Error sending voltage/current query: %d.", ret);
118                 return SR_ERR;
119         }
120
121         /* Reply: "read <current> <voltage>". */
122         tokens = g_strsplit((const char *)&buf, " ", 3);
123         if (voltage)
124                 *voltage = g_ascii_strtod(tokens[2], NULL) / 1000;
125         if (current)
126                 *current = g_ascii_strtod(tokens[1], NULL) / 1000;
127         g_strfreev(tokens);
128
129         return SR_OK;
130 }
131
132 static void handle_packet(const struct sr_dev_inst *sdi)
133 {
134         float voltage, current;
135         struct sr_datafeed_packet packet;
136         struct sr_datafeed_analog_old analog;
137         struct dev_context *devc;
138         char **tokens;
139         GSList *l;
140
141         devc = sdi->priv;
142
143         if (g_str_has_prefix((const char *)devc->buf, "overtemp")) {
144                 sr_dbg("Overtemperature condition!");
145                 devc->otp_active = TRUE;
146                 return;
147         }
148
149         if (!g_str_has_prefix((const char *)devc->buf, "read ")) {
150                 sr_dbg("Unknown packet: '%s'.", devc->buf);
151                 return;
152         }
153
154         tokens = g_strsplit((const char *)devc->buf, " ", 3);
155         voltage = g_ascii_strtod(tokens[2], NULL) / 1000;
156         current = g_ascii_strtod(tokens[1], NULL) / 1000;
157         g_strfreev(tokens);
158
159         memset(&analog, 0, sizeof(struct sr_datafeed_analog_old));
160
161         /* Begin frame. */
162         packet.type = SR_DF_FRAME_BEGIN;
163         sr_session_send(sdi, &packet);
164
165         packet.type = SR_DF_ANALOG_OLD;
166         packet.payload = &analog;
167         analog.num_samples = 1;
168
169         /* Voltage */
170         l = g_slist_copy(sdi->channels);
171         l = g_slist_remove_link(l, g_slist_nth(l, 1));
172         analog.channels = l;
173         analog.mq = SR_MQ_VOLTAGE;
174         analog.mqflags = SR_MQFLAG_DC;
175         analog.unit = SR_UNIT_VOLT;
176         analog.data = &voltage;
177         sr_session_send(sdi, &packet);
178         g_slist_free(l);
179
180         /* Current */
181         l = g_slist_copy(sdi->channels);
182         l = g_slist_remove_link(l, g_slist_nth(l, 0));
183         analog.channels = l;
184         analog.mq = SR_MQ_CURRENT;
185         analog.mqflags = SR_MQFLAG_DC;
186         analog.unit = SR_UNIT_AMPERE;
187         analog.data = &current;
188         sr_session_send(sdi, &packet);
189         g_slist_free(l);
190
191         /* End frame. */
192         packet.type = SR_DF_FRAME_END;
193         sr_session_send(sdi, &packet);
194
195         devc->num_samples++;
196 }
197
198 static void handle_new_data(const struct sr_dev_inst *sdi)
199 {
200         int len;
201         struct dev_context *devc;
202         struct sr_serial_dev_inst *serial;
203
204         devc = sdi->priv;
205         serial = sdi->conn;
206
207         /* Try to get as much data as the buffer can hold. */
208         len = RELOADPRO_BUFSIZE - devc->buflen;
209         len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len);
210         if (len == 0)
211                 return; /* No new bytes, nothing to do. */
212         if (len < 0) {
213                 sr_err("Serial port read error: %d.", len);
214                 return;
215         }
216         devc->buflen += len;
217
218         if (g_str_has_suffix((const char *)devc->buf, "\n")) {
219                 handle_packet(sdi);
220                 memset(devc->buf, 0, RELOADPRO_BUFSIZE);
221                 devc->buflen = 0;
222         }
223 }
224
225 SR_PRIV int reloadpro_receive_data(int fd, int revents, void *cb_data)
226 {
227         struct sr_dev_inst *sdi;
228         struct dev_context *devc;
229         int64_t t;
230
231         (void)fd;
232
233         sdi = cb_data;
234         devc = sdi->priv;
235
236         if (revents != G_IO_IN)
237                 return TRUE;
238
239         handle_new_data(sdi);
240
241         if (devc->limit_samples && (devc->num_samples >= devc->limit_samples)) {
242                 sr_info("Requested number of samples reached.");
243                 sdi->driver->dev_acquisition_stop(sdi, cb_data);
244                 return TRUE;
245         }
246
247         if (devc->limit_msec) {
248                 t = (g_get_monotonic_time() - devc->starttime) / 1000;
249                 if (t > (int64_t)devc->limit_msec) {
250                         sr_info("Requested time limit reached.");
251                         sdi->driver->dev_acquisition_stop(sdi, cb_data);
252                         return TRUE;
253                 }
254         }
255
256         return TRUE;
257 }