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