]>
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 | |
2ea1fdf1 | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
6e8d31d4 UH |
18 | */ |
19 | ||
20 | #include <config.h> | |
803db07a | 21 | #include <string.h> |
6e8d31d4 UH |
22 | #include "protocol.h" |
23 | ||
803db07a UH |
24 | #define SERIALCOMM "115200/8n1" |
25 | ||
26 | #define CMD_VERSION "version\r\n" | |
27 | #define CMD_MONITOR "monitor 200\r\n" | |
b76a414d | 28 | #define CMD_MONITOR_STOP "monitor 0\r\n" |
803db07a UH |
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, |
70635036 | 47 | SR_CONF_REGULATION | SR_CONF_GET | SR_CONF_LIST, |
803db07a UH |
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, | |
3d70d777 | 57 | SR_CONF_UNDER_VOLTAGE_CONDITION_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, |
803db07a UH |
58 | }; |
59 | ||
70635036 FS |
60 | static const char *regulation[] = { |
61 | /* CC mode only. */ | |
62 | "CC", | |
63 | }; | |
64 | ||
6e8d31d4 UH |
65 | static GSList *scan(struct sr_dev_driver *di, GSList *options) |
66 | { | |
803db07a | 67 | struct sr_dev_inst *sdi; |
803db07a UH |
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; | |
43376f33 | 73 | GSList *l; |
803db07a UH |
74 | int ret, len; |
75 | const char *conn, *serialcomm; | |
76 | char buf[100]; | |
77 | char *bufptr; | |
8084e0fa | 78 | double version; |
6e8d31d4 | 79 | |
803db07a UH |
80 | conn = serialcomm = NULL; |
81 | for (l = options; l; l = l->next) { | |
82 | src = l->data; | |
83 | switch (src->key) { | |
84 | case SR_CONF_CONN: | |
85 | conn = g_variant_get_string(src->data, NULL); | |
86 | break; | |
87 | case SR_CONF_SERIALCOMM: | |
88 | serialcomm = g_variant_get_string(src->data, NULL); | |
89 | break; | |
90 | } | |
91 | } | |
92 | if (!conn) | |
93 | return NULL; | |
94 | if (!serialcomm) | |
95 | serialcomm = SERIALCOMM; | |
96 | ||
97 | serial = sr_serial_dev_inst_new(conn, serialcomm); | |
98 | ||
99 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) | |
100 | return NULL; | |
101 | ||
b76a414d FS |
102 | /* |
103 | * First stop potentially running monitoring and wait for 50ms before | |
104 | * next command can be sent. | |
105 | */ | |
106 | if (serial_write_blocking(serial, CMD_MONITOR_STOP, | |
107 | strlen(CMD_MONITOR_STOP), serial_timeout(serial, | |
108 | strlen(CMD_MONITOR_STOP))) < (int)strlen(CMD_MONITOR_STOP)) { | |
109 | sr_dbg("Unable to write while probing for hardware."); | |
110 | serial_close(serial); | |
111 | return NULL; | |
112 | } | |
113 | g_usleep(50 * 1000); | |
114 | ||
803db07a UH |
115 | if (serial_write_blocking(serial, CMD_VERSION, |
116 | strlen(CMD_VERSION), serial_timeout(serial, | |
117 | strlen(CMD_VERSION))) < (int)strlen(CMD_VERSION)) { | |
118 | sr_dbg("Unable to write while probing for hardware."); | |
119 | serial_close(serial); | |
120 | return NULL; | |
121 | } | |
122 | ||
123 | memset(buf, 0, sizeof(buf)); | |
124 | bufptr = buf; | |
125 | len = sizeof(buf); | |
126 | ret = serial_readline(serial, &bufptr, &len, 3000); | |
127 | ||
128 | if (ret < 0 || len < 9 || strncmp((const char *)&buf, "version ", 8)) { | |
129 | sr_dbg("Unable to probe version number."); | |
130 | serial_close(serial); | |
131 | return NULL; | |
132 | } | |
133 | ||
8084e0fa UH |
134 | version = g_ascii_strtod(buf + 8, NULL); |
135 | if (version < 1.10) { | |
136 | sr_info("Firmware >= 1.10 required (got %1.2f).", version); | |
137 | serial_close(serial); | |
138 | return NULL; | |
139 | } | |
140 | ||
803db07a | 141 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
45884333 | 142 | sdi->status = SR_ST_INACTIVE; |
803db07a UH |
143 | sdi->vendor = g_strdup("Arachnid Labs"); |
144 | sdi->model = g_strdup("Re:load Pro"); | |
145 | sdi->version = g_strdup(buf + 8); | |
803db07a UH |
146 | sdi->inst_type = SR_INST_SERIAL; |
147 | sdi->conn = serial; | |
148 | ||
149 | cg = g_malloc0(sizeof(struct sr_channel_group)); | |
150 | cg->name = g_strdup("1"); | |
151 | sdi->channel_groups = g_slist_append(sdi->channel_groups, cg); | |
152 | ||
153 | ch = sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "V"); | |
154 | cg->channels = g_slist_append(cg->channels, ch); | |
155 | ||
21530939 | 156 | ch = sr_channel_new(sdi, 1, SR_CHANNEL_ANALOG, TRUE, "I"); |
803db07a UH |
157 | cg->channels = g_slist_append(cg->channels, ch); |
158 | ||
159 | devc = g_malloc0(sizeof(struct dev_context)); | |
014c7f93 | 160 | sr_sw_limits_init(&devc->limits); |
803db07a | 161 | sdi->priv = devc; |
803db07a UH |
162 | |
163 | serial_close(serial); | |
803db07a | 164 | |
43376f33 | 165 | return std_scan_complete(di, g_slist_append(NULL, sdi)); |
6e8d31d4 UH |
166 | } |
167 | ||
803db07a UH |
168 | static int config_list(uint32_t key, GVariant **data, |
169 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
6e8d31d4 | 170 | { |
803db07a | 171 | if (!cg) { |
e66d1892 | 172 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); |
803db07a UH |
173 | } else { |
174 | switch (key) { | |
175 | case SR_CONF_DEVICE_OPTIONS: | |
53012da6 | 176 | *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg)); |
803db07a | 177 | break; |
70635036 FS |
178 | case SR_CONF_REGULATION: |
179 | *data = std_gvar_array_str(ARRAY_AND_SIZE(regulation)); | |
180 | break; | |
803db07a | 181 | case SR_CONF_CURRENT_LIMIT: |
54d471f4 | 182 | *data = std_gvar_min_max_step(0.0, 6.0, 0.001); |
803db07a | 183 | break; |
3d70d777 FS |
184 | case SR_CONF_UNDER_VOLTAGE_CONDITION_THRESHOLD: |
185 | *data = std_gvar_min_max_step(0.0, 60.0, 0.001); | |
186 | break; | |
803db07a UH |
187 | default: |
188 | return SR_ERR_NA; | |
189 | } | |
190 | } | |
191 | ||
a9010323 | 192 | return SR_OK; |
6e8d31d4 UH |
193 | } |
194 | ||
195 | static int config_get(uint32_t key, GVariant **data, | |
196 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
197 | { | |
803db07a | 198 | struct dev_context *devc; |
803db07a | 199 | float fvalue; |
6e8d31d4 | 200 | |
6e8d31d4 UH |
201 | (void)cg; |
202 | ||
803db07a UH |
203 | devc = sdi->priv; |
204 | ||
205 | /* | |
206 | * These features/keys are not supported by the hardware: | |
207 | * - SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE | |
208 | * - SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD | |
209 | * - SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE | |
210 | * - SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD | |
8501448c | 211 | * - SR_CONF_ENABLED (state cannot be queried, only set) |
803db07a UH |
212 | */ |
213 | ||
6e8d31d4 | 214 | switch (key) { |
803db07a | 215 | case SR_CONF_LIMIT_SAMPLES: |
803db07a | 216 | case SR_CONF_LIMIT_MSEC: |
014c7f93 | 217 | return sr_sw_limits_config_get(&devc->limits, key, data); |
803db07a UH |
218 | case SR_CONF_REGULATION: |
219 | *data = g_variant_new_string("CC"); /* Always CC mode. */ | |
220 | break; | |
221 | case SR_CONF_VOLTAGE: | |
222 | if (reloadpro_get_voltage_current(sdi, &fvalue, NULL) < 0) | |
223 | return SR_ERR; | |
224 | *data = g_variant_new_double(fvalue); | |
225 | break; | |
226 | case SR_CONF_CURRENT: | |
227 | if (reloadpro_get_voltage_current(sdi, NULL, &fvalue) < 0) | |
228 | return SR_ERR; | |
229 | *data = g_variant_new_double(fvalue); | |
230 | break; | |
231 | case SR_CONF_CURRENT_LIMIT: | |
232 | if (reloadpro_get_current_limit(sdi, &fvalue) == SR_OK) | |
233 | *data = g_variant_new_double(fvalue); | |
234 | break; | |
235 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: | |
236 | *data = g_variant_new_boolean(TRUE); /* Always on. */ | |
237 | break; | |
238 | case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: | |
239 | *data = g_variant_new_boolean(TRUE); /* Always on. */ | |
240 | break; | |
241 | case SR_CONF_OVER_TEMPERATURE_PROTECTION: | |
242 | *data = g_variant_new_boolean(TRUE); /* Always on. */ | |
243 | break; | |
244 | case SR_CONF_OVER_TEMPERATURE_PROTECTION_ACTIVE: | |
245 | *data = g_variant_new_boolean(devc->otp_active); | |
246 | break; | |
2217be1d | 247 | case SR_CONF_UNDER_VOLTAGE_CONDITION: |
0aaaee2d UH |
248 | if (reloadpro_get_under_voltage_threshold(sdi, &fvalue) == SR_OK) |
249 | *data = g_variant_new_boolean(fvalue != 0.0); | |
2217be1d UH |
250 | break; |
251 | case SR_CONF_UNDER_VOLTAGE_CONDITION_ACTIVE: | |
252 | *data = g_variant_new_boolean(devc->uvc_active); | |
253 | break; | |
3d70d777 FS |
254 | case SR_CONF_UNDER_VOLTAGE_CONDITION_THRESHOLD: |
255 | if (reloadpro_get_under_voltage_threshold(sdi, &fvalue) == SR_OK) | |
256 | *data = g_variant_new_double(fvalue); | |
257 | break; | |
6e8d31d4 UH |
258 | default: |
259 | return SR_ERR_NA; | |
260 | } | |
261 | ||
a9010323 | 262 | return SR_OK; |
6e8d31d4 UH |
263 | } |
264 | ||
265 | static int config_set(uint32_t key, GVariant *data, | |
266 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
267 | { | |
803db07a | 268 | struct dev_context *devc; |
6e8d31d4 | 269 | |
6e8d31d4 UH |
270 | (void)cg; |
271 | ||
803db07a UH |
272 | devc = sdi->priv; |
273 | ||
6e8d31d4 | 274 | switch (key) { |
803db07a | 275 | case SR_CONF_LIMIT_SAMPLES: |
cd04f641 | 276 | case SR_CONF_LIMIT_MSEC: |
014c7f93 | 277 | return sr_sw_limits_config_set(&devc->limits, key, data); |
8501448c | 278 | case SR_CONF_ENABLED: |
758906aa | 279 | return reloadpro_set_on_off(sdi, g_variant_get_boolean(data)); |
803db07a | 280 | case SR_CONF_CURRENT_LIMIT: |
758906aa | 281 | return reloadpro_set_current_limit(sdi, g_variant_get_double(data)); |
3d70d777 FS |
282 | case SR_CONF_UNDER_VOLTAGE_CONDITION_THRESHOLD: |
283 | return reloadpro_set_under_voltage_threshold(sdi, | |
284 | g_variant_get_double(data)); | |
6e8d31d4 | 285 | default: |
758906aa | 286 | return SR_ERR_NA; |
6e8d31d4 UH |
287 | } |
288 | ||
758906aa | 289 | return SR_OK; |
6e8d31d4 UH |
290 | } |
291 | ||
b76a414d FS |
292 | static int dev_close(struct sr_dev_inst *sdi) |
293 | { | |
294 | if (serial_write_blocking(sdi->conn, CMD_MONITOR_STOP, | |
295 | strlen(CMD_MONITOR_STOP), serial_timeout(sdi->conn, | |
296 | strlen(CMD_MONITOR_STOP))) < (int)strlen(CMD_MONITOR_STOP)) { | |
297 | sr_dbg("Unable to stop monitoring."); | |
298 | } | |
299 | ||
300 | return std_serial_dev_close(sdi); | |
301 | } | |
302 | ||
695dc859 | 303 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) |
6e8d31d4 UH |
304 | { |
305 | int ret; | |
803db07a UH |
306 | struct dev_context *devc; |
307 | struct sr_serial_dev_inst *serial; | |
6e8d31d4 | 308 | |
803db07a | 309 | devc = sdi->priv; |
a2172899 FS |
310 | devc->acquisition_running = TRUE; |
311 | ||
803db07a UH |
312 | serial = sdi->conn; |
313 | ||
314 | /* Send the 'monitor <ms>' command (doesn't have a reply). */ | |
315 | if ((ret = serial_write_blocking(serial, CMD_MONITOR, | |
316 | strlen(CMD_MONITOR), serial_timeout(serial, | |
317 | strlen(CMD_MONITOR)))) < (int)strlen(CMD_MONITOR)) { | |
318 | sr_err("Unable to send 'monitor' command: %d.", ret); | |
319 | return SR_ERR; | |
6e8d31d4 UH |
320 | } |
321 | ||
014c7f93 | 322 | sr_sw_limits_acquisition_start(&devc->limits); |
bee2b016 | 323 | std_session_send_df_header(sdi); |
6e8d31d4 | 324 | |
803db07a UH |
325 | memset(devc->buf, 0, RELOADPRO_BUFSIZE); |
326 | devc->buflen = 0; | |
6e8d31d4 | 327 | |
b3e715e5 FS |
328 | g_mutex_init(&devc->acquisition_mutex); |
329 | ||
330 | serial_source_add(sdi->session, serial, G_IO_IN, 100, | |
331 | reloadpro_receive_data, (void *)sdi); | |
332 | ||
6e8d31d4 UH |
333 | return SR_OK; |
334 | } | |
335 | ||
a2172899 FS |
336 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) |
337 | { | |
338 | struct dev_context *devc; | |
b3e715e5 | 339 | int ret; |
a2172899 FS |
340 | |
341 | devc = sdi->priv; | |
342 | devc->acquisition_running = FALSE; | |
343 | ||
b3e715e5 FS |
344 | ret = std_serial_dev_acquisition_stop(sdi); |
345 | g_mutex_clear(&devc->acquisition_mutex); | |
346 | ||
347 | return ret; | |
a2172899 FS |
348 | } |
349 | ||
dd5c48a6 | 350 | static struct sr_dev_driver arachnid_labs_re_load_pro_driver_info = { |
6e8d31d4 UH |
351 | .name = "arachnid-labs-re-load-pro", |
352 | .longname = "Arachnid Labs Re:load Pro", | |
353 | .api_version = 1, | |
c2fdcc25 | 354 | .init = std_init, |
700d6b64 | 355 | .cleanup = std_cleanup, |
6e8d31d4 | 356 | .scan = scan, |
c01bf34c | 357 | .dev_list = std_dev_list, |
f778bf02 | 358 | .dev_clear = std_dev_clear, |
6e8d31d4 UH |
359 | .config_get = config_get, |
360 | .config_set = config_set, | |
361 | .config_list = config_list, | |
803db07a | 362 | .dev_open = std_serial_dev_open, |
b76a414d | 363 | .dev_close = dev_close, |
6e8d31d4 | 364 | .dev_acquisition_start = dev_acquisition_start, |
a2172899 | 365 | .dev_acquisition_stop = dev_acquisition_stop, |
6e8d31d4 UH |
366 | .context = NULL, |
367 | }; | |
dd5c48a6 | 368 | SR_REGISTER_DEV_DRIVER(arachnid_labs_re_load_pro_driver_info); |