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