]> sigrok.org Git - libsigrok.git/blame_incremental - src/hardware/arachnid-labs-re-load-pro/api.c
drivers: Factor out std_gvar_min_max_step{,_array}().
[libsigrok.git] / src / hardware / arachnid-labs-re-load-pro / api.c
... / ...
CommitLineData
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2015-2016 Uwe Hermann <uwe@hermann-uwe.de>
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, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <config.h>
21#include <string.h>
22#include "protocol.h"
23
24#define SERIALCOMM "115200/8n1"
25
26#define CMD_VERSION "version\r\n"
27#define CMD_MONITOR "monitor 200\r\n"
28
29static const uint32_t scanopts[] = {
30 SR_CONF_CONN,
31 SR_CONF_SERIALCOMM,
32};
33
34static const uint32_t drvopts[] = {
35 SR_CONF_ELECTRONIC_LOAD,
36};
37
38static const uint32_t devopts[] = {
39 SR_CONF_CONTINUOUS,
40 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
41 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
42};
43
44static const uint32_t devopts_cg[] = {
45 SR_CONF_ENABLED | SR_CONF_SET,
46 SR_CONF_REGULATION | SR_CONF_GET,
47 SR_CONF_VOLTAGE | SR_CONF_GET,
48 SR_CONF_CURRENT | SR_CONF_GET,
49 SR_CONF_CURRENT_LIMIT | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
50 SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED | SR_CONF_GET,
51 SR_CONF_OVER_CURRENT_PROTECTION_ENABLED | SR_CONF_GET,
52 SR_CONF_OVER_TEMPERATURE_PROTECTION | SR_CONF_GET,
53 SR_CONF_OVER_TEMPERATURE_PROTECTION_ACTIVE | SR_CONF_GET,
54 SR_CONF_UNDER_VOLTAGE_CONDITION | SR_CONF_GET,
55 SR_CONF_UNDER_VOLTAGE_CONDITION_ACTIVE | SR_CONF_GET,
56};
57
58static GSList *scan(struct sr_dev_driver *di, GSList *options)
59{
60 struct sr_dev_inst *sdi;
61 struct dev_context *devc;
62 struct sr_config *src;
63 struct sr_serial_dev_inst *serial;
64 struct sr_channel_group *cg;
65 struct sr_channel *ch;
66 GSList *l;
67 int ret, len;
68 const char *conn, *serialcomm;
69 char buf[100];
70 char *bufptr;
71 double version;
72
73 conn = serialcomm = NULL;
74 for (l = options; l; l = l->next) {
75 src = l->data;
76 switch (src->key) {
77 case SR_CONF_CONN:
78 conn = g_variant_get_string(src->data, NULL);
79 break;
80 case SR_CONF_SERIALCOMM:
81 serialcomm = g_variant_get_string(src->data, NULL);
82 break;
83 }
84 }
85 if (!conn)
86 return NULL;
87 if (!serialcomm)
88 serialcomm = SERIALCOMM;
89
90 serial = sr_serial_dev_inst_new(conn, serialcomm);
91
92 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
93 return NULL;
94
95 serial_flush(serial);
96
97 if (serial_write_blocking(serial, CMD_VERSION,
98 strlen(CMD_VERSION), serial_timeout(serial,
99 strlen(CMD_VERSION))) < (int)strlen(CMD_VERSION)) {
100 sr_dbg("Unable to write while probing for hardware.");
101 serial_close(serial);
102 return NULL;
103 }
104
105 memset(buf, 0, sizeof(buf));
106 bufptr = buf;
107 len = sizeof(buf);
108 ret = serial_readline(serial, &bufptr, &len, 3000);
109
110 if (ret < 0 || len < 9 || strncmp((const char *)&buf, "version ", 8)) {
111 sr_dbg("Unable to probe version number.");
112 serial_close(serial);
113 return NULL;
114 }
115
116 version = g_ascii_strtod(buf + 8, NULL);
117 if (version < 1.10) {
118 sr_info("Firmware >= 1.10 required (got %1.2f).", version);
119 serial_close(serial);
120 return NULL;
121 }
122
123 sdi = g_malloc0(sizeof(struct sr_dev_inst));
124 sdi->status = SR_ST_INACTIVE;
125 sdi->vendor = g_strdup("Arachnid Labs");
126 sdi->model = g_strdup("Re:load Pro");
127 sdi->version = g_strdup(buf + 8);
128 sdi->inst_type = SR_INST_SERIAL;
129 sdi->conn = serial;
130
131 cg = g_malloc0(sizeof(struct sr_channel_group));
132 cg->name = g_strdup("1");
133 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
134
135 ch = sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "V");
136 cg->channels = g_slist_append(cg->channels, ch);
137
138 ch = sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "I");
139 cg->channels = g_slist_append(cg->channels, ch);
140
141 devc = g_malloc0(sizeof(struct dev_context));
142 sr_sw_limits_init(&devc->limits);
143 sdi->priv = devc;
144
145 serial_close(serial);
146
147 return std_scan_complete(di, g_slist_append(NULL, sdi));
148}
149
150static int config_list(uint32_t key, GVariant **data,
151 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
152{
153 if (!cg) {
154 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
155 } else {
156 switch (key) {
157 case SR_CONF_DEVICE_OPTIONS:
158 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
159 devopts_cg, ARRAY_SIZE(devopts_cg), sizeof(uint32_t));
160 break;
161 case SR_CONF_CURRENT_LIMIT:
162 *data = std_gvar_min_max_step(0.0, 6.0, 0.001);
163 break;
164 default:
165 return SR_ERR_NA;
166 }
167 }
168
169 return SR_OK;
170}
171
172static int config_get(uint32_t key, GVariant **data,
173 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
174{
175 struct dev_context *devc;
176 float fvalue;
177
178 (void)cg;
179
180 devc = sdi->priv;
181
182 /*
183 * These features/keys are not supported by the hardware:
184 * - SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE
185 * - SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD
186 * - SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE
187 * - SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD
188 * - SR_CONF_ENABLED (state cannot be queried, only set)
189 */
190
191 switch (key) {
192 case SR_CONF_LIMIT_SAMPLES:
193 case SR_CONF_LIMIT_MSEC:
194 return sr_sw_limits_config_get(&devc->limits, key, data);
195 case SR_CONF_REGULATION:
196 *data = g_variant_new_string("CC"); /* Always CC mode. */
197 break;
198 case SR_CONF_VOLTAGE:
199 if (reloadpro_get_voltage_current(sdi, &fvalue, NULL) < 0)
200 return SR_ERR;
201 *data = g_variant_new_double(fvalue);
202 break;
203 case SR_CONF_CURRENT:
204 if (reloadpro_get_voltage_current(sdi, NULL, &fvalue) < 0)
205 return SR_ERR;
206 *data = g_variant_new_double(fvalue);
207 break;
208 case SR_CONF_CURRENT_LIMIT:
209 if (reloadpro_get_current_limit(sdi, &fvalue) == SR_OK)
210 *data = g_variant_new_double(fvalue);
211 break;
212 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
213 *data = g_variant_new_boolean(TRUE); /* Always on. */
214 break;
215 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
216 *data = g_variant_new_boolean(TRUE); /* Always on. */
217 break;
218 case SR_CONF_OVER_TEMPERATURE_PROTECTION:
219 *data = g_variant_new_boolean(TRUE); /* Always on. */
220 break;
221 case SR_CONF_OVER_TEMPERATURE_PROTECTION_ACTIVE:
222 *data = g_variant_new_boolean(devc->otp_active);
223 break;
224 case SR_CONF_UNDER_VOLTAGE_CONDITION:
225 *data = g_variant_new_boolean(TRUE); /* Always on. */
226 break;
227 case SR_CONF_UNDER_VOLTAGE_CONDITION_ACTIVE:
228 *data = g_variant_new_boolean(devc->uvc_active);
229 break;
230 default:
231 return SR_ERR_NA;
232 }
233
234 return SR_OK;
235}
236
237static int config_set(uint32_t key, GVariant *data,
238 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
239{
240 struct dev_context *devc;
241 int ret;
242
243 (void)cg;
244
245 devc = sdi->priv;
246
247 ret = SR_OK;
248 switch (key) {
249 case SR_CONF_LIMIT_SAMPLES:
250 case SR_CONF_LIMIT_MSEC:
251 return sr_sw_limits_config_set(&devc->limits, key, data);
252 case SR_CONF_ENABLED:
253 ret = reloadpro_set_on_off(sdi, g_variant_get_boolean(data));
254 break;
255 case SR_CONF_CURRENT_LIMIT:
256 ret = reloadpro_set_current_limit(sdi,
257 g_variant_get_double(data));
258 break;
259 default:
260 ret = SR_ERR_NA;
261 }
262
263 return ret;
264}
265
266static int dev_acquisition_start(const struct sr_dev_inst *sdi)
267{
268 int ret;
269 struct dev_context *devc;
270 struct sr_serial_dev_inst *serial;
271
272 devc = sdi->priv;
273 serial = sdi->conn;
274
275 /* Send the 'monitor <ms>' command (doesn't have a reply). */
276 if ((ret = serial_write_blocking(serial, CMD_MONITOR,
277 strlen(CMD_MONITOR), serial_timeout(serial,
278 strlen(CMD_MONITOR)))) < (int)strlen(CMD_MONITOR)) {
279 sr_err("Unable to send 'monitor' command: %d.", ret);
280 return SR_ERR;
281 }
282
283 serial_source_add(sdi->session, serial, G_IO_IN, 100,
284 reloadpro_receive_data, (void *)sdi);
285
286 sr_sw_limits_acquisition_start(&devc->limits);
287 std_session_send_df_header(sdi);
288
289 memset(devc->buf, 0, RELOADPRO_BUFSIZE);
290 devc->buflen = 0;
291
292 return SR_OK;
293}
294
295static struct sr_dev_driver arachnid_labs_re_load_pro_driver_info = {
296 .name = "arachnid-labs-re-load-pro",
297 .longname = "Arachnid Labs Re:load Pro",
298 .api_version = 1,
299 .init = std_init,
300 .cleanup = std_cleanup,
301 .scan = scan,
302 .dev_list = std_dev_list,
303 .dev_clear = std_dev_clear,
304 .config_get = config_get,
305 .config_set = config_set,
306 .config_list = config_list,
307 .dev_open = std_serial_dev_open,
308 .dev_close = std_serial_dev_close,
309 .dev_acquisition_start = dev_acquisition_start,
310 .dev_acquisition_stop = std_serial_dev_acquisition_stop,
311 .context = NULL,
312};
313SR_REGISTER_DEV_DRIVER(arachnid_labs_re_load_pro_driver_info);