]> sigrok.org Git - libsigrok.git/blob - src/hardware/arachnid-labs-re-load-pro/api.c
923f6b01e68bff16c095e6c38430520ed32fed7d
[libsigrok.git] / src / hardware / arachnid-labs-re-load-pro / api.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 SERIALCOMM "115200/8n1"
26
27 #define CMD_VERSION "version\r\n"
28 #define CMD_MONITOR "monitor 200\r\n"
29
30 static const uint32_t scanopts[] = {
31         SR_CONF_CONN,
32         SR_CONF_SERIALCOMM,
33 };
34
35 static const uint32_t drvopts[] = {
36         SR_CONF_ELECTRONIC_LOAD,
37 };
38
39 static const uint32_t devopts[] = {
40         SR_CONF_CONTINUOUS,
41         SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
42         SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
43 };
44
45 static const uint32_t devopts_cg[] = {
46         SR_CONF_ENABLED | SR_CONF_SET,
47         SR_CONF_REGULATION | SR_CONF_GET,
48         SR_CONF_VOLTAGE | SR_CONF_GET,
49         SR_CONF_CURRENT | SR_CONF_GET,
50         SR_CONF_CURRENT_LIMIT | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
51         SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED | SR_CONF_GET,
52         SR_CONF_OVER_CURRENT_PROTECTION_ENABLED | SR_CONF_GET,
53         SR_CONF_OVER_TEMPERATURE_PROTECTION | SR_CONF_GET,
54         SR_CONF_OVER_TEMPERATURE_PROTECTION_ACTIVE | SR_CONF_GET,
55         SR_CONF_UNDER_VOLTAGE_CONDITION | SR_CONF_GET,
56         SR_CONF_UNDER_VOLTAGE_CONDITION_ACTIVE | SR_CONF_GET,
57 };
58
59 static GSList *scan(struct sr_dev_driver *di, GSList *options)
60 {
61         struct sr_dev_inst *sdi;
62         struct drv_context *drvc;
63         struct dev_context *devc;
64         struct sr_config *src;
65         struct sr_serial_dev_inst *serial;
66         struct sr_channel_group *cg;
67         struct sr_channel *ch;
68         GSList *l, *devices;
69         int ret, len;
70         const char *conn, *serialcomm;
71         char buf[100];
72         char *bufptr;
73         double version;
74
75         devices = NULL;
76         drvc = di->context;
77
78         conn = serialcomm = NULL;
79         for (l = options; l; l = l->next) {
80                 src = l->data;
81                 switch (src->key) {
82                 case SR_CONF_CONN:
83                         conn = g_variant_get_string(src->data, NULL);
84                         break;
85                 case SR_CONF_SERIALCOMM:
86                         serialcomm = g_variant_get_string(src->data, NULL);
87                         break;
88                 }
89         }
90         if (!conn)
91                 return NULL;
92         if (!serialcomm)
93                 serialcomm = SERIALCOMM;
94
95         serial = sr_serial_dev_inst_new(conn, serialcomm);
96
97         if (serial_open(serial, SERIAL_RDWR) != SR_OK)
98                 return NULL;
99
100         serial_flush(serial);
101
102         if (serial_write_blocking(serial, CMD_VERSION,
103                         strlen(CMD_VERSION), serial_timeout(serial,
104                         strlen(CMD_VERSION))) < (int)strlen(CMD_VERSION)) {
105                 sr_dbg("Unable to write while probing for hardware.");
106                 serial_close(serial);
107                 return NULL;
108         }
109
110         memset(buf, 0, sizeof(buf));
111         bufptr = buf;
112         len = sizeof(buf);
113         ret = serial_readline(serial, &bufptr, &len, 3000);
114
115         if (ret < 0 || len < 9 || strncmp((const char *)&buf, "version ", 8)) {
116                 sr_dbg("Unable to probe version number.");
117                 serial_close(serial);
118                 return NULL;
119         }
120
121         version = g_ascii_strtod(buf + 8, NULL);
122         if (version < 1.10) {
123                 sr_info("Firmware >= 1.10 required (got %1.2f).", version);
124                 serial_close(serial);
125                 return NULL;
126         }
127
128         sdi = g_malloc0(sizeof(struct sr_dev_inst));
129         sdi->status = SR_ST_INACTIVE;
130         sdi->vendor = g_strdup("Arachnid Labs");
131         sdi->model = g_strdup("Re:load Pro");
132         sdi->version = g_strdup(buf + 8);
133         sdi->inst_type = SR_INST_SERIAL;
134         sdi->conn = serial;
135
136         cg = g_malloc0(sizeof(struct sr_channel_group));
137         cg->name = g_strdup("1");
138         sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
139
140         ch = sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "V");
141         cg->channels = g_slist_append(cg->channels, ch);
142
143         ch = sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "I");
144         cg->channels = g_slist_append(cg->channels, ch);
145
146         devc = g_malloc0(sizeof(struct dev_context));
147         sdi->priv = devc;
148         devices = g_slist_append(devices, sdi);
149
150         serial_close(serial);
151         if (!devices)
152                 sr_serial_dev_inst_free(serial);
153
154         return std_scan_complete(di, devices);
155 }
156
157 static int config_list(uint32_t key, GVariant **data,
158         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
159 {
160         GVariantBuilder gvb;
161         int ret;
162
163         /* Always available. */
164         if (key == SR_CONF_SCAN_OPTIONS) {
165                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
166                         scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
167                 return SR_OK;
168         }
169
170         if (key == SR_CONF_DEVICE_OPTIONS && !sdi) {
171                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
172                         drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
173                 return SR_OK;
174         }
175
176         if (!sdi)
177                 return SR_ERR_ARG;
178
179         ret = SR_OK;
180
181         if (!cg) {
182                 /* No channel group: global options. */
183                 switch (key) {
184                 case SR_CONF_DEVICE_OPTIONS:
185                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
186                                 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
187                         break;
188                 default:
189                         return SR_ERR_NA;
190                 }
191         } else {
192                 switch (key) {
193                 case SR_CONF_DEVICE_OPTIONS:
194                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
195                                 devopts_cg, ARRAY_SIZE(devopts_cg), sizeof(uint32_t));
196                         break;
197                 case SR_CONF_CURRENT_LIMIT:
198                         g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
199                         /* Min, max, step. */
200                         g_variant_builder_add_value(&gvb, g_variant_new_double(0.0));
201                         g_variant_builder_add_value(&gvb, g_variant_new_double(6.0));
202                         g_variant_builder_add_value(&gvb, g_variant_new_double(0.001)); /* 1mA steps */
203                         *data = g_variant_builder_end(&gvb);
204                         break;
205                 default:
206                         return SR_ERR_NA;
207                 }
208         }
209
210         return ret;
211 }
212
213 static int config_get(uint32_t key, GVariant **data,
214         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
215 {
216         struct dev_context *devc;
217         int ret;
218         float fvalue;
219
220         (void)cg;
221
222         devc = sdi->priv;
223
224         /*
225          * These features/keys are not supported by the hardware:
226          *  - SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE
227          *  - SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD
228          *  - SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE
229          *  - SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD
230          *  - SR_CONF_ENABLED (state cannot be queried, only set)
231          */
232
233         ret = SR_OK;
234         switch (key) {
235         case SR_CONF_LIMIT_SAMPLES:
236                 *data = g_variant_new_uint64(devc->limit_samples);
237                 break;
238         case SR_CONF_LIMIT_MSEC:
239                 *data = g_variant_new_uint64(devc->limit_msec);
240                 break;
241         case SR_CONF_REGULATION:
242                 *data = g_variant_new_string("CC"); /* Always CC mode. */
243                 break;
244         case SR_CONF_VOLTAGE:
245                 if (reloadpro_get_voltage_current(sdi, &fvalue, NULL) < 0)
246                         return SR_ERR;
247                 *data = g_variant_new_double(fvalue);
248                 break;
249         case SR_CONF_CURRENT:
250                 if (reloadpro_get_voltage_current(sdi, NULL, &fvalue) < 0)
251                         return SR_ERR;
252                 *data = g_variant_new_double(fvalue);
253                 break;
254         case SR_CONF_CURRENT_LIMIT:
255                 if (reloadpro_get_current_limit(sdi, &fvalue) == SR_OK)
256                         *data = g_variant_new_double(fvalue);
257                 break;
258         case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
259                 *data = g_variant_new_boolean(TRUE); /* Always on. */
260                 break;
261         case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
262                 *data = g_variant_new_boolean(TRUE); /* Always on. */
263                 break;
264         case SR_CONF_OVER_TEMPERATURE_PROTECTION:
265                 *data = g_variant_new_boolean(TRUE); /* Always on. */
266                 break;
267         case SR_CONF_OVER_TEMPERATURE_PROTECTION_ACTIVE:
268                 *data = g_variant_new_boolean(devc->otp_active);
269                 break;
270         case SR_CONF_UNDER_VOLTAGE_CONDITION:
271                 *data = g_variant_new_boolean(TRUE); /* Always on. */
272                 break;
273         case SR_CONF_UNDER_VOLTAGE_CONDITION_ACTIVE:
274                 *data = g_variant_new_boolean(devc->uvc_active);
275                 break;
276         default:
277                 return SR_ERR_NA;
278         }
279
280         return ret;
281 }
282
283 static int config_set(uint32_t key, GVariant *data,
284         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
285 {
286         struct dev_context *devc;
287         int ret;
288
289         (void)cg;
290
291         if (sdi->status != SR_ST_ACTIVE)
292                 return SR_ERR_DEV_CLOSED;
293
294         devc = sdi->priv;
295
296         ret = SR_OK;
297         switch (key) {
298         case SR_CONF_LIMIT_SAMPLES:
299                 devc->limit_samples = g_variant_get_uint64(data);
300                 break;
301         case SR_CONF_LIMIT_MSEC:
302                 devc->limit_msec = g_variant_get_uint64(data);
303                 break;
304         case SR_CONF_ENABLED:
305                 ret = reloadpro_set_on_off(sdi, g_variant_get_boolean(data));
306                 break;
307         case SR_CONF_CURRENT_LIMIT:
308                 ret = reloadpro_set_current_limit(sdi,
309                         g_variant_get_double(data));
310                 break;
311         default:
312                 ret = SR_ERR_NA;
313         }
314
315         return ret;
316 }
317
318 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
319 {
320         int ret;
321         struct dev_context *devc;
322         struct sr_serial_dev_inst *serial;
323
324         if (sdi->status != SR_ST_ACTIVE)
325                 return SR_ERR_DEV_CLOSED;
326
327         devc = sdi->priv;
328         serial = sdi->conn;
329
330         /* Send the 'monitor <ms>' command (doesn't have a reply). */
331         if ((ret = serial_write_blocking(serial, CMD_MONITOR,
332                         strlen(CMD_MONITOR), serial_timeout(serial,
333                         strlen(CMD_MONITOR)))) < (int)strlen(CMD_MONITOR)) {
334                 sr_err("Unable to send 'monitor' command: %d.", ret);
335                 return SR_ERR;
336         }
337
338         /* Poll every 100ms, or whenever some data comes in. */
339         serial_source_add(sdi->session, serial, G_IO_IN, 100,
340                           reloadpro_receive_data, (void *)sdi);
341
342         std_session_send_df_header(sdi, LOG_PREFIX);
343
344         memset(devc->buf, 0, RELOADPRO_BUFSIZE);
345         devc->buflen = 0;
346         devc->num_samples = 0;
347         devc->starttime = g_get_monotonic_time();
348
349         return SR_OK;
350 }
351
352 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
353 {
354         return std_serial_dev_acquisition_stop(sdi,
355                 std_serial_dev_close, sdi->conn, LOG_PREFIX);
356 }
357
358 static struct sr_dev_driver arachnid_labs_re_load_pro_driver_info = {
359         .name = "arachnid-labs-re-load-pro",
360         .longname = "Arachnid Labs Re:load Pro",
361         .api_version = 1,
362         .init = std_init,
363         .cleanup = std_cleanup,
364         .scan = scan,
365         .dev_list = std_dev_list,
366         .config_get = config_get,
367         .config_set = config_set,
368         .config_list = config_list,
369         .dev_open = std_serial_dev_open,
370         .dev_close = std_serial_dev_close,
371         .dev_acquisition_start = dev_acquisition_start,
372         .dev_acquisition_stop = dev_acquisition_stop,
373         .context = NULL,
374 };
375 SR_REGISTER_DEV_DRIVER(arachnid_labs_re_load_pro_driver_info);