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