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