]> sigrok.org Git - libsigrok.git/blame - src/hardware/arachnid-labs-re-load-pro/api.c
arachnid-labs-re-load-pro: Add SR_CONF_UNDER_VOLTAGE_THRESHOLD.
[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
2ea1fdf1 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
6e8d31d4
UH
18 */
19
20#include <config.h>
803db07a 21#include <string.h>
6e8d31d4
UH
22#include "protocol.h"
23
803db07a
UH
24#define SERIALCOMM "115200/8n1"
25
26#define CMD_VERSION "version\r\n"
27#define CMD_MONITOR "monitor 200\r\n"
b76a414d 28#define CMD_MONITOR_STOP "monitor 0\r\n"
803db07a
UH
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,
3d70d777 57 SR_CONF_UNDER_VOLTAGE_CONDITION_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
803db07a
UH
58};
59
6e8d31d4
UH
60static GSList *scan(struct sr_dev_driver *di, GSList *options)
61{
803db07a 62 struct sr_dev_inst *sdi;
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;
43376f33 68 GSList *l;
803db07a
UH
69 int ret, len;
70 const char *conn, *serialcomm;
71 char buf[100];
72 char *bufptr;
8084e0fa 73 double version;
6e8d31d4 74
803db07a
UH
75 conn = serialcomm = NULL;
76 for (l = options; l; l = l->next) {
77 src = l->data;
78 switch (src->key) {
79 case SR_CONF_CONN:
80 conn = g_variant_get_string(src->data, NULL);
81 break;
82 case SR_CONF_SERIALCOMM:
83 serialcomm = g_variant_get_string(src->data, NULL);
84 break;
85 }
86 }
87 if (!conn)
88 return NULL;
89 if (!serialcomm)
90 serialcomm = SERIALCOMM;
91
92 serial = sr_serial_dev_inst_new(conn, serialcomm);
93
94 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
95 return NULL;
96
97 serial_flush(serial);
98
b76a414d
FS
99 /*
100 * First stop potentially running monitoring and wait for 50ms before
101 * next command can be sent.
102 */
103 if (serial_write_blocking(serial, CMD_MONITOR_STOP,
104 strlen(CMD_MONITOR_STOP), serial_timeout(serial,
105 strlen(CMD_MONITOR_STOP))) < (int)strlen(CMD_MONITOR_STOP)) {
106 sr_dbg("Unable to write while probing for hardware.");
107 serial_close(serial);
108 return NULL;
109 }
110 g_usleep(50 * 1000);
111
803db07a
UH
112 if (serial_write_blocking(serial, CMD_VERSION,
113 strlen(CMD_VERSION), serial_timeout(serial,
114 strlen(CMD_VERSION))) < (int)strlen(CMD_VERSION)) {
115 sr_dbg("Unable to write while probing for hardware.");
116 serial_close(serial);
117 return NULL;
118 }
119
120 memset(buf, 0, sizeof(buf));
121 bufptr = buf;
122 len = sizeof(buf);
123 ret = serial_readline(serial, &bufptr, &len, 3000);
124
125 if (ret < 0 || len < 9 || strncmp((const char *)&buf, "version ", 8)) {
126 sr_dbg("Unable to probe version number.");
127 serial_close(serial);
128 return NULL;
129 }
130
8084e0fa
UH
131 version = g_ascii_strtod(buf + 8, NULL);
132 if (version < 1.10) {
133 sr_info("Firmware >= 1.10 required (got %1.2f).", version);
134 serial_close(serial);
135 return NULL;
136 }
137
803db07a 138 sdi = g_malloc0(sizeof(struct sr_dev_inst));
45884333 139 sdi->status = SR_ST_INACTIVE;
803db07a
UH
140 sdi->vendor = g_strdup("Arachnid Labs");
141 sdi->model = g_strdup("Re:load Pro");
142 sdi->version = g_strdup(buf + 8);
803db07a
UH
143 sdi->inst_type = SR_INST_SERIAL;
144 sdi->conn = serial;
145
146 cg = g_malloc0(sizeof(struct sr_channel_group));
147 cg->name = g_strdup("1");
148 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
149
150 ch = sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "V");
151 cg->channels = g_slist_append(cg->channels, ch);
152
153 ch = sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "I");
154 cg->channels = g_slist_append(cg->channels, ch);
155
156 devc = g_malloc0(sizeof(struct dev_context));
014c7f93 157 sr_sw_limits_init(&devc->limits);
803db07a 158 sdi->priv = devc;
803db07a
UH
159
160 serial_close(serial);
803db07a 161
43376f33 162 return std_scan_complete(di, g_slist_append(NULL, sdi));
6e8d31d4
UH
163}
164
803db07a
UH
165static int config_list(uint32_t key, GVariant **data,
166 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
6e8d31d4 167{
803db07a 168 if (!cg) {
e66d1892 169 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
803db07a
UH
170 } else {
171 switch (key) {
172 case SR_CONF_DEVICE_OPTIONS:
53012da6 173 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg));
803db07a
UH
174 break;
175 case SR_CONF_CURRENT_LIMIT:
54d471f4 176 *data = std_gvar_min_max_step(0.0, 6.0, 0.001);
803db07a 177 break;
3d70d777
FS
178 case SR_CONF_UNDER_VOLTAGE_CONDITION_THRESHOLD:
179 *data = std_gvar_min_max_step(0.0, 60.0, 0.001);
180 break;
803db07a
UH
181 default:
182 return SR_ERR_NA;
183 }
184 }
185
a9010323 186 return SR_OK;
6e8d31d4
UH
187}
188
189static int config_get(uint32_t key, GVariant **data,
190 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
191{
803db07a 192 struct dev_context *devc;
803db07a 193 float fvalue;
6e8d31d4 194
6e8d31d4
UH
195 (void)cg;
196
803db07a
UH
197 devc = sdi->priv;
198
199 /*
200 * These features/keys are not supported by the hardware:
201 * - SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE
202 * - SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD
203 * - SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE
204 * - SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD
8501448c 205 * - SR_CONF_ENABLED (state cannot be queried, only set)
803db07a
UH
206 */
207
6e8d31d4 208 switch (key) {
803db07a 209 case SR_CONF_LIMIT_SAMPLES:
803db07a 210 case SR_CONF_LIMIT_MSEC:
014c7f93 211 return sr_sw_limits_config_get(&devc->limits, key, data);
803db07a
UH
212 case SR_CONF_REGULATION:
213 *data = g_variant_new_string("CC"); /* Always CC mode. */
214 break;
215 case SR_CONF_VOLTAGE:
216 if (reloadpro_get_voltage_current(sdi, &fvalue, NULL) < 0)
217 return SR_ERR;
218 *data = g_variant_new_double(fvalue);
219 break;
220 case SR_CONF_CURRENT:
221 if (reloadpro_get_voltage_current(sdi, NULL, &fvalue) < 0)
222 return SR_ERR;
223 *data = g_variant_new_double(fvalue);
224 break;
225 case SR_CONF_CURRENT_LIMIT:
226 if (reloadpro_get_current_limit(sdi, &fvalue) == SR_OK)
227 *data = g_variant_new_double(fvalue);
228 break;
229 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
230 *data = g_variant_new_boolean(TRUE); /* Always on. */
231 break;
232 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
233 *data = g_variant_new_boolean(TRUE); /* Always on. */
234 break;
235 case SR_CONF_OVER_TEMPERATURE_PROTECTION:
236 *data = g_variant_new_boolean(TRUE); /* Always on. */
237 break;
238 case SR_CONF_OVER_TEMPERATURE_PROTECTION_ACTIVE:
239 *data = g_variant_new_boolean(devc->otp_active);
240 break;
2217be1d 241 case SR_CONF_UNDER_VOLTAGE_CONDITION:
3d70d777
FS
242 if (reloadpro_get_under_voltage_threshold(sdi, &fvalue) == SR_OK) {
243 if (fvalue == .0)
244 *data = g_variant_new_boolean(FALSE);
245 else
246 *data = g_variant_new_boolean(TRUE);
247 }
2217be1d
UH
248 break;
249 case SR_CONF_UNDER_VOLTAGE_CONDITION_ACTIVE:
250 *data = g_variant_new_boolean(devc->uvc_active);
251 break;
3d70d777
FS
252 case SR_CONF_UNDER_VOLTAGE_CONDITION_THRESHOLD:
253 if (reloadpro_get_under_voltage_threshold(sdi, &fvalue) == SR_OK)
254 *data = g_variant_new_double(fvalue);
255 break;
6e8d31d4
UH
256 default:
257 return SR_ERR_NA;
258 }
259
a9010323 260 return SR_OK;
6e8d31d4
UH
261}
262
263static int config_set(uint32_t key, GVariant *data,
264 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
265{
803db07a 266 struct dev_context *devc;
6e8d31d4 267
6e8d31d4
UH
268 (void)cg;
269
803db07a
UH
270 devc = sdi->priv;
271
6e8d31d4 272 switch (key) {
803db07a 273 case SR_CONF_LIMIT_SAMPLES:
cd04f641 274 case SR_CONF_LIMIT_MSEC:
014c7f93 275 return sr_sw_limits_config_set(&devc->limits, key, data);
8501448c 276 case SR_CONF_ENABLED:
758906aa 277 return reloadpro_set_on_off(sdi, g_variant_get_boolean(data));
803db07a 278 case SR_CONF_CURRENT_LIMIT:
758906aa 279 return reloadpro_set_current_limit(sdi, g_variant_get_double(data));
3d70d777
FS
280 case SR_CONF_UNDER_VOLTAGE_CONDITION_THRESHOLD:
281 return reloadpro_set_under_voltage_threshold(sdi,
282 g_variant_get_double(data));
6e8d31d4 283 default:
758906aa 284 return SR_ERR_NA;
6e8d31d4
UH
285 }
286
758906aa 287 return SR_OK;
6e8d31d4
UH
288}
289
b76a414d
FS
290static int dev_close(struct sr_dev_inst *sdi)
291{
292 if (serial_write_blocking(sdi->conn, CMD_MONITOR_STOP,
293 strlen(CMD_MONITOR_STOP), serial_timeout(sdi->conn,
294 strlen(CMD_MONITOR_STOP))) < (int)strlen(CMD_MONITOR_STOP)) {
295 sr_dbg("Unable to stop monitoring.");
296 }
297
298 return std_serial_dev_close(sdi);
299}
300
695dc859 301static int dev_acquisition_start(const struct sr_dev_inst *sdi)
6e8d31d4
UH
302{
303 int ret;
803db07a
UH
304 struct dev_context *devc;
305 struct sr_serial_dev_inst *serial;
6e8d31d4 306
803db07a 307 devc = sdi->priv;
a2172899
FS
308 devc->acquisition_running = TRUE;
309
803db07a
UH
310 serial = sdi->conn;
311
312 /* Send the 'monitor <ms>' command (doesn't have a reply). */
313 if ((ret = serial_write_blocking(serial, CMD_MONITOR,
314 strlen(CMD_MONITOR), serial_timeout(serial,
315 strlen(CMD_MONITOR)))) < (int)strlen(CMD_MONITOR)) {
316 sr_err("Unable to send 'monitor' command: %d.", ret);
317 return SR_ERR;
6e8d31d4
UH
318 }
319
803db07a
UH
320 serial_source_add(sdi->session, serial, G_IO_IN, 100,
321 reloadpro_receive_data, (void *)sdi);
6e8d31d4 322
014c7f93 323 sr_sw_limits_acquisition_start(&devc->limits);
bee2b016 324 std_session_send_df_header(sdi);
6e8d31d4 325
803db07a
UH
326 memset(devc->buf, 0, RELOADPRO_BUFSIZE);
327 devc->buflen = 0;
6e8d31d4
UH
328
329 return SR_OK;
330}
331
a2172899
FS
332static int dev_acquisition_stop(struct sr_dev_inst *sdi)
333{
334 struct dev_context *devc;
335
336 devc = sdi->priv;
337 devc->acquisition_running = FALSE;
338
339 return std_serial_dev_acquisition_stop(sdi);
340}
341
dd5c48a6 342static struct sr_dev_driver arachnid_labs_re_load_pro_driver_info = {
6e8d31d4
UH
343 .name = "arachnid-labs-re-load-pro",
344 .longname = "Arachnid Labs Re:load Pro",
345 .api_version = 1,
c2fdcc25 346 .init = std_init,
700d6b64 347 .cleanup = std_cleanup,
6e8d31d4 348 .scan = scan,
c01bf34c 349 .dev_list = std_dev_list,
f778bf02 350 .dev_clear = std_dev_clear,
6e8d31d4
UH
351 .config_get = config_get,
352 .config_set = config_set,
353 .config_list = config_list,
803db07a 354 .dev_open = std_serial_dev_open,
b76a414d 355 .dev_close = dev_close,
6e8d31d4 356 .dev_acquisition_start = dev_acquisition_start,
a2172899 357 .dev_acquisition_stop = dev_acquisition_stop,
6e8d31d4
UH
358 .context = NULL,
359};
dd5c48a6 360SR_REGISTER_DEV_DRIVER(arachnid_labs_re_load_pro_driver_info);