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