]> sigrok.org Git - libsigrok.git/blob - src/hardware/arachnid-labs-re-load-pro/api.c
Don't reset instance list in scan() callback
[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->driver = di;
134         sdi->inst_type = SR_INST_SERIAL;
135         sdi->conn = serial;
136
137         cg = g_malloc0(sizeof(struct sr_channel_group));
138         cg->name = g_strdup("1");
139         sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
140
141         ch = sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "V");
142         cg->channels = g_slist_append(cg->channels, ch);
143
144         ch = sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "I");
145         cg->channels = g_slist_append(cg->channels, ch);
146
147         devc = g_malloc0(sizeof(struct dev_context));
148         sdi->priv = devc;
149         drvc->instances = g_slist_append(drvc->instances, sdi);
150         devices = g_slist_append(devices, sdi);
151
152         serial_close(serial);
153         if (!devices)
154                 sr_serial_dev_inst_free(serial);
155
156         return devices;
157 }
158
159 static int config_list(uint32_t key, GVariant **data,
160         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
161 {
162         GVariantBuilder gvb;
163         int ret;
164
165         /* Always available. */
166         if (key == SR_CONF_SCAN_OPTIONS) {
167                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
168                         scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
169                 return SR_OK;
170         }
171
172         if (key == SR_CONF_DEVICE_OPTIONS && !sdi) {
173                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
174                         drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
175                 return SR_OK;
176         }
177
178         if (!sdi)
179                 return SR_ERR_ARG;
180
181         ret = SR_OK;
182
183         if (!cg) {
184                 /* No channel group: global options. */
185                 switch (key) {
186                 case SR_CONF_DEVICE_OPTIONS:
187                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
188                                 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
189                         break;
190                 default:
191                         return SR_ERR_NA;
192                 }
193         } else {
194                 switch (key) {
195                 case SR_CONF_DEVICE_OPTIONS:
196                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
197                                 devopts_cg, ARRAY_SIZE(devopts_cg), sizeof(uint32_t));
198                         break;
199                 case SR_CONF_CURRENT_LIMIT:
200                         g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
201                         /* Min, max, step. */
202                         g_variant_builder_add_value(&gvb, g_variant_new_double(0.0));
203                         g_variant_builder_add_value(&gvb, g_variant_new_double(6.0));
204                         g_variant_builder_add_value(&gvb, g_variant_new_double(0.001)); /* 1mA steps */
205                         *data = g_variant_builder_end(&gvb);
206                         break;
207                 default:
208                         return SR_ERR_NA;
209                 }
210         }
211
212         return ret;
213 }
214
215 static int config_get(uint32_t key, GVariant **data,
216         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
217 {
218         struct dev_context *devc;
219         int ret;
220         float fvalue;
221
222         (void)cg;
223
224         devc = sdi->priv;
225
226         /*
227          * These features/keys are not supported by the hardware:
228          *  - SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE
229          *  - SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD
230          *  - SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE
231          *  - SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD
232          *  - SR_CONF_ENABLED (state cannot be queried, only set)
233          */
234
235         ret = SR_OK;
236         switch (key) {
237         case SR_CONF_LIMIT_SAMPLES:
238                 *data = g_variant_new_uint64(devc->limit_samples);
239                 break;
240         case SR_CONF_LIMIT_MSEC:
241                 *data = g_variant_new_uint64(devc->limit_msec);
242                 break;
243         case SR_CONF_REGULATION:
244                 *data = g_variant_new_string("CC"); /* Always CC mode. */
245                 break;
246         case SR_CONF_VOLTAGE:
247                 if (reloadpro_get_voltage_current(sdi, &fvalue, NULL) < 0)
248                         return SR_ERR;
249                 *data = g_variant_new_double(fvalue);
250                 break;
251         case SR_CONF_CURRENT:
252                 if (reloadpro_get_voltage_current(sdi, NULL, &fvalue) < 0)
253                         return SR_ERR;
254                 *data = g_variant_new_double(fvalue);
255                 break;
256         case SR_CONF_CURRENT_LIMIT:
257                 if (reloadpro_get_current_limit(sdi, &fvalue) == SR_OK)
258                         *data = g_variant_new_double(fvalue);
259                 break;
260         case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
261                 *data = g_variant_new_boolean(TRUE); /* Always on. */
262                 break;
263         case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
264                 *data = g_variant_new_boolean(TRUE); /* Always on. */
265                 break;
266         case SR_CONF_OVER_TEMPERATURE_PROTECTION:
267                 *data = g_variant_new_boolean(TRUE); /* Always on. */
268                 break;
269         case SR_CONF_OVER_TEMPERATURE_PROTECTION_ACTIVE:
270                 *data = g_variant_new_boolean(devc->otp_active);
271                 break;
272         case SR_CONF_UNDER_VOLTAGE_CONDITION:
273                 *data = g_variant_new_boolean(TRUE); /* Always on. */
274                 break;
275         case SR_CONF_UNDER_VOLTAGE_CONDITION_ACTIVE:
276                 *data = g_variant_new_boolean(devc->uvc_active);
277                 break;
278         default:
279                 return SR_ERR_NA;
280         }
281
282         return ret;
283 }
284
285 static int config_set(uint32_t key, GVariant *data,
286         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
287 {
288         struct dev_context *devc;
289         int ret;
290
291         (void)cg;
292
293         if (sdi->status != SR_ST_ACTIVE)
294                 return SR_ERR_DEV_CLOSED;
295
296         devc = sdi->priv;
297
298         ret = SR_OK;
299         switch (key) {
300         case SR_CONF_LIMIT_SAMPLES:
301                 devc->limit_samples = g_variant_get_uint64(data);
302                 break;
303         case SR_CONF_LIMIT_MSEC:
304                 devc->limit_msec = g_variant_get_uint64(data);
305                 break;
306         case SR_CONF_ENABLED:
307                 ret = reloadpro_set_on_off(sdi, g_variant_get_boolean(data));
308                 break;
309         case SR_CONF_CURRENT_LIMIT:
310                 ret = reloadpro_set_current_limit(sdi,
311                         g_variant_get_double(data));
312                 break;
313         default:
314                 ret = SR_ERR_NA;
315         }
316
317         return ret;
318 }
319
320 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
321 {
322         int ret;
323         struct dev_context *devc;
324         struct sr_serial_dev_inst *serial;
325
326         if (sdi->status != SR_ST_ACTIVE)
327                 return SR_ERR_DEV_CLOSED;
328
329         devc = sdi->priv;
330         serial = sdi->conn;
331
332         /* Send the 'monitor <ms>' command (doesn't have a reply). */
333         if ((ret = serial_write_blocking(serial, CMD_MONITOR,
334                         strlen(CMD_MONITOR), serial_timeout(serial,
335                         strlen(CMD_MONITOR)))) < (int)strlen(CMD_MONITOR)) {
336                 sr_err("Unable to send 'monitor' command: %d.", ret);
337                 return SR_ERR;
338         }
339
340         /* Poll every 100ms, or whenever some data comes in. */
341         serial_source_add(sdi->session, serial, G_IO_IN, 100,
342                           reloadpro_receive_data, (void *)sdi);
343
344         std_session_send_df_header(sdi, LOG_PREFIX);
345
346         memset(devc->buf, 0, RELOADPRO_BUFSIZE);
347         devc->buflen = 0;
348         devc->num_samples = 0;
349         devc->starttime = g_get_monotonic_time();
350
351         return SR_OK;
352 }
353
354 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
355 {
356         return std_serial_dev_acquisition_stop(sdi,
357                 std_serial_dev_close, sdi->conn, LOG_PREFIX);
358 }
359
360 static struct sr_dev_driver arachnid_labs_re_load_pro_driver_info = {
361         .name = "arachnid-labs-re-load-pro",
362         .longname = "Arachnid Labs Re:load Pro",
363         .api_version = 1,
364         .init = std_init,
365         .cleanup = std_cleanup,
366         .scan = scan,
367         .dev_list = std_dev_list,
368         .config_get = config_get,
369         .config_set = config_set,
370         .config_list = config_list,
371         .dev_open = std_serial_dev_open,
372         .dev_close = std_serial_dev_close,
373         .dev_acquisition_start = dev_acquisition_start,
374         .dev_acquisition_stop = dev_acquisition_stop,
375         .context = NULL,
376 };
377 SR_REGISTER_DEV_DRIVER(arachnid_labs_re_load_pro_driver_info);