]>
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, | |
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_SET, | |
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 | SR_CONF_UNDER_VOLTAGE_CONDITION | SR_CONF_GET, | |
56 | SR_CONF_UNDER_VOLTAGE_CONDITION_ACTIVE | SR_CONF_GET, | |
57 | }; | |
58 | ||
59 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
60 | { | |
61 | struct sr_dev_inst *sdi; | |
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; | |
67 | GSList *l; | |
68 | int ret, len; | |
69 | const char *conn, *serialcomm; | |
70 | char buf[100]; | |
71 | char *bufptr; | |
72 | double version; | |
73 | ||
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 | ||
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 | ||
124 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
125 | sdi->status = SR_ST_INACTIVE; | |
126 | sdi->vendor = g_strdup("Arachnid Labs"); | |
127 | sdi->model = g_strdup("Re:load Pro"); | |
128 | sdi->version = g_strdup(buf + 8); | |
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 | sr_sw_limits_init(&devc->limits); | |
144 | sdi->priv = devc; | |
145 | ||
146 | serial_close(serial); | |
147 | ||
148 | return std_scan_complete(di, g_slist_append(NULL, sdi)); | |
149 | } | |
150 | ||
151 | static int config_list(uint32_t key, GVariant **data, | |
152 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
153 | { | |
154 | GVariantBuilder gvb; | |
155 | int ret; | |
156 | ||
157 | /* Always available. */ | |
158 | if (key == SR_CONF_SCAN_OPTIONS) { | |
159 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
160 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); | |
161 | return SR_OK; | |
162 | } | |
163 | ||
164 | if (key == SR_CONF_DEVICE_OPTIONS && !sdi) { | |
165 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
166 | drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t)); | |
167 | return SR_OK; | |
168 | } | |
169 | ||
170 | if (!sdi) | |
171 | return SR_ERR_ARG; | |
172 | ||
173 | ret = SR_OK; | |
174 | ||
175 | if (!cg) { | |
176 | /* No channel group: global options. */ | |
177 | switch (key) { | |
178 | case SR_CONF_DEVICE_OPTIONS: | |
179 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
180 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); | |
181 | break; | |
182 | default: | |
183 | return SR_ERR_NA; | |
184 | } | |
185 | } else { | |
186 | switch (key) { | |
187 | case SR_CONF_DEVICE_OPTIONS: | |
188 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
189 | devopts_cg, ARRAY_SIZE(devopts_cg), sizeof(uint32_t)); | |
190 | break; | |
191 | case SR_CONF_CURRENT_LIMIT: | |
192 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
193 | /* Min, max, step. */ | |
194 | g_variant_builder_add_value(&gvb, g_variant_new_double(0.0)); | |
195 | g_variant_builder_add_value(&gvb, g_variant_new_double(6.0)); | |
196 | g_variant_builder_add_value(&gvb, g_variant_new_double(0.001)); /* 1mA steps */ | |
197 | *data = g_variant_builder_end(&gvb); | |
198 | break; | |
199 | default: | |
200 | return SR_ERR_NA; | |
201 | } | |
202 | } | |
203 | ||
204 | return ret; | |
205 | } | |
206 | ||
207 | static int config_get(uint32_t key, GVariant **data, | |
208 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
209 | { | |
210 | struct dev_context *devc; | |
211 | int ret; | |
212 | float fvalue; | |
213 | ||
214 | (void)cg; | |
215 | ||
216 | devc = sdi->priv; | |
217 | ||
218 | /* | |
219 | * These features/keys are not supported by the hardware: | |
220 | * - SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE | |
221 | * - SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD | |
222 | * - SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE | |
223 | * - SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD | |
224 | * - SR_CONF_ENABLED (state cannot be queried, only set) | |
225 | */ | |
226 | ||
227 | ret = SR_OK; | |
228 | switch (key) { | |
229 | case SR_CONF_LIMIT_SAMPLES: | |
230 | case SR_CONF_LIMIT_MSEC: | |
231 | return sr_sw_limits_config_get(&devc->limits, key, data); | |
232 | case SR_CONF_REGULATION: | |
233 | *data = g_variant_new_string("CC"); /* Always CC mode. */ | |
234 | break; | |
235 | case SR_CONF_VOLTAGE: | |
236 | if (reloadpro_get_voltage_current(sdi, &fvalue, NULL) < 0) | |
237 | return SR_ERR; | |
238 | *data = g_variant_new_double(fvalue); | |
239 | break; | |
240 | case SR_CONF_CURRENT: | |
241 | if (reloadpro_get_voltage_current(sdi, NULL, &fvalue) < 0) | |
242 | return SR_ERR; | |
243 | *data = g_variant_new_double(fvalue); | |
244 | break; | |
245 | case SR_CONF_CURRENT_LIMIT: | |
246 | if (reloadpro_get_current_limit(sdi, &fvalue) == SR_OK) | |
247 | *data = g_variant_new_double(fvalue); | |
248 | break; | |
249 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: | |
250 | *data = g_variant_new_boolean(TRUE); /* Always on. */ | |
251 | break; | |
252 | case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: | |
253 | *data = g_variant_new_boolean(TRUE); /* Always on. */ | |
254 | break; | |
255 | case SR_CONF_OVER_TEMPERATURE_PROTECTION: | |
256 | *data = g_variant_new_boolean(TRUE); /* Always on. */ | |
257 | break; | |
258 | case SR_CONF_OVER_TEMPERATURE_PROTECTION_ACTIVE: | |
259 | *data = g_variant_new_boolean(devc->otp_active); | |
260 | break; | |
261 | case SR_CONF_UNDER_VOLTAGE_CONDITION: | |
262 | *data = g_variant_new_boolean(TRUE); /* Always on. */ | |
263 | break; | |
264 | case SR_CONF_UNDER_VOLTAGE_CONDITION_ACTIVE: | |
265 | *data = g_variant_new_boolean(devc->uvc_active); | |
266 | break; | |
267 | default: | |
268 | return SR_ERR_NA; | |
269 | } | |
270 | ||
271 | return ret; | |
272 | } | |
273 | ||
274 | static int config_set(uint32_t key, GVariant *data, | |
275 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
276 | { | |
277 | struct dev_context *devc; | |
278 | int ret; | |
279 | ||
280 | (void)cg; | |
281 | ||
282 | if (sdi->status != SR_ST_ACTIVE) | |
283 | return SR_ERR_DEV_CLOSED; | |
284 | ||
285 | devc = sdi->priv; | |
286 | ||
287 | ret = SR_OK; | |
288 | switch (key) { | |
289 | case SR_CONF_LIMIT_SAMPLES: | |
290 | return sr_sw_limits_config_set(&devc->limits, key, data); | |
291 | case SR_CONF_ENABLED: | |
292 | ret = reloadpro_set_on_off(sdi, g_variant_get_boolean(data)); | |
293 | break; | |
294 | case SR_CONF_CURRENT_LIMIT: | |
295 | ret = reloadpro_set_current_limit(sdi, | |
296 | g_variant_get_double(data)); | |
297 | break; | |
298 | default: | |
299 | ret = SR_ERR_NA; | |
300 | } | |
301 | ||
302 | return ret; | |
303 | } | |
304 | ||
305 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
306 | { | |
307 | int ret; | |
308 | struct dev_context *devc; | |
309 | struct sr_serial_dev_inst *serial; | |
310 | ||
311 | if (sdi->status != SR_ST_ACTIVE) | |
312 | return SR_ERR_DEV_CLOSED; | |
313 | ||
314 | devc = sdi->priv; | |
315 | serial = sdi->conn; | |
316 | ||
317 | /* Send the 'monitor <ms>' command (doesn't have a reply). */ | |
318 | if ((ret = serial_write_blocking(serial, CMD_MONITOR, | |
319 | strlen(CMD_MONITOR), serial_timeout(serial, | |
320 | strlen(CMD_MONITOR)))) < (int)strlen(CMD_MONITOR)) { | |
321 | sr_err("Unable to send 'monitor' command: %d.", ret); | |
322 | return SR_ERR; | |
323 | } | |
324 | ||
325 | /* Poll every 100ms, or whenever some data comes in. */ | |
326 | serial_source_add(sdi->session, serial, G_IO_IN, 100, | |
327 | reloadpro_receive_data, (void *)sdi); | |
328 | ||
329 | sr_sw_limits_acquisition_start(&devc->limits); | |
330 | std_session_send_df_header(sdi); | |
331 | ||
332 | memset(devc->buf, 0, RELOADPRO_BUFSIZE); | |
333 | devc->buflen = 0; | |
334 | ||
335 | return SR_OK; | |
336 | } | |
337 | ||
338 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
339 | { | |
340 | return std_serial_dev_acquisition_stop(sdi, | |
341 | std_serial_dev_close); | |
342 | } | |
343 | ||
344 | static struct sr_dev_driver arachnid_labs_re_load_pro_driver_info = { | |
345 | .name = "arachnid-labs-re-load-pro", | |
346 | .longname = "Arachnid Labs Re:load Pro", | |
347 | .api_version = 1, | |
348 | .init = std_init, | |
349 | .cleanup = std_cleanup, | |
350 | .scan = scan, | |
351 | .dev_list = std_dev_list, | |
352 | .config_get = config_get, | |
353 | .config_set = config_set, | |
354 | .config_list = config_list, | |
355 | .dev_open = std_serial_dev_open, | |
356 | .dev_close = std_serial_dev_close, | |
357 | .dev_acquisition_start = dev_acquisition_start, | |
358 | .dev_acquisition_stop = dev_acquisition_stop, | |
359 | .context = NULL, | |
360 | }; | |
361 | SR_REGISTER_DEV_DRIVER(arachnid_labs_re_load_pro_driver_info); |