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