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