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