2 * This file is part of the libsigrok project.
4 * Copyright (C) 2015-2016 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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
25 #define SERIALCOMM "115200/8n1"
27 #define CMD_VERSION "version\r\n"
28 #define CMD_MONITOR "monitor 200\r\n"
30 static const uint32_t scanopts[] = {
35 static const uint32_t drvopts[] = {
36 SR_CONF_ELECTRONIC_LOAD,
39 static const uint32_t devopts[] = {
41 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
42 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
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,
59 static GSList *scan(struct sr_dev_driver *di, GSList *options)
61 struct sr_dev_inst *sdi;
62 struct drv_context *drvc;
63 struct dev_context *devc;
64 struct sr_config *src;
65 struct sr_serial_dev_inst *serial;
66 struct sr_channel_group *cg;
67 struct sr_channel *ch;
70 const char *conn, *serialcomm;
77 conn = serialcomm = NULL;
78 for (l = options; l; l = l->next) {
82 conn = g_variant_get_string(src->data, NULL);
84 case SR_CONF_SERIALCOMM:
85 serialcomm = g_variant_get_string(src->data, NULL);
92 serialcomm = SERIALCOMM;
94 serial = sr_serial_dev_inst_new(conn, serialcomm);
96 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
101 if (serial_write_blocking(serial, CMD_VERSION,
102 strlen(CMD_VERSION), serial_timeout(serial,
103 strlen(CMD_VERSION))) < (int)strlen(CMD_VERSION)) {
104 sr_dbg("Unable to write while probing for hardware.");
105 serial_close(serial);
109 memset(buf, 0, sizeof(buf));
112 ret = serial_readline(serial, &bufptr, &len, 3000);
114 if (ret < 0 || len < 9 || strncmp((const char *)&buf, "version ", 8)) {
115 sr_dbg("Unable to probe version number.");
116 serial_close(serial);
120 version = g_ascii_strtod(buf + 8, NULL);
121 if (version < 1.10) {
122 sr_info("Firmware >= 1.10 required (got %1.2f).", version);
123 serial_close(serial);
127 sdi = g_malloc0(sizeof(struct sr_dev_inst));
128 sdi->status = SR_ST_INACTIVE;
129 sdi->vendor = g_strdup("Arachnid Labs");
130 sdi->model = g_strdup("Re:load Pro");
131 sdi->version = g_strdup(buf + 8);
132 sdi->inst_type = SR_INST_SERIAL;
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);
139 ch = sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "V");
140 cg->channels = g_slist_append(cg->channels, ch);
142 ch = sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "I");
143 cg->channels = g_slist_append(cg->channels, ch);
145 devc = g_malloc0(sizeof(struct dev_context));
148 serial_close(serial);
150 return std_scan_complete(di, g_slist_append(NULL, sdi));
153 static int config_list(uint32_t key, GVariant **data,
154 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
159 /* Always available. */
160 if (key == SR_CONF_SCAN_OPTIONS) {
161 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
162 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
166 if (key == SR_CONF_DEVICE_OPTIONS && !sdi) {
167 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
168 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
178 /* No channel group: global options. */
180 case SR_CONF_DEVICE_OPTIONS:
181 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
182 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
189 case SR_CONF_DEVICE_OPTIONS:
190 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
191 devopts_cg, ARRAY_SIZE(devopts_cg), sizeof(uint32_t));
193 case SR_CONF_CURRENT_LIMIT:
194 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
195 /* Min, max, step. */
196 g_variant_builder_add_value(&gvb, g_variant_new_double(0.0));
197 g_variant_builder_add_value(&gvb, g_variant_new_double(6.0));
198 g_variant_builder_add_value(&gvb, g_variant_new_double(0.001)); /* 1mA steps */
199 *data = g_variant_builder_end(&gvb);
209 static int config_get(uint32_t key, GVariant **data,
210 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
212 struct dev_context *devc;
221 * These features/keys are not supported by the hardware:
222 * - SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE
223 * - SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD
224 * - SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE
225 * - SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD
226 * - SR_CONF_ENABLED (state cannot be queried, only set)
231 case SR_CONF_LIMIT_SAMPLES:
232 *data = g_variant_new_uint64(devc->limit_samples);
234 case SR_CONF_LIMIT_MSEC:
235 *data = g_variant_new_uint64(devc->limit_msec);
237 case SR_CONF_REGULATION:
238 *data = g_variant_new_string("CC"); /* Always CC mode. */
240 case SR_CONF_VOLTAGE:
241 if (reloadpro_get_voltage_current(sdi, &fvalue, NULL) < 0)
243 *data = g_variant_new_double(fvalue);
245 case SR_CONF_CURRENT:
246 if (reloadpro_get_voltage_current(sdi, NULL, &fvalue) < 0)
248 *data = g_variant_new_double(fvalue);
250 case SR_CONF_CURRENT_LIMIT:
251 if (reloadpro_get_current_limit(sdi, &fvalue) == SR_OK)
252 *data = g_variant_new_double(fvalue);
254 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
255 *data = g_variant_new_boolean(TRUE); /* Always on. */
257 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
258 *data = g_variant_new_boolean(TRUE); /* Always on. */
260 case SR_CONF_OVER_TEMPERATURE_PROTECTION:
261 *data = g_variant_new_boolean(TRUE); /* Always on. */
263 case SR_CONF_OVER_TEMPERATURE_PROTECTION_ACTIVE:
264 *data = g_variant_new_boolean(devc->otp_active);
266 case SR_CONF_UNDER_VOLTAGE_CONDITION:
267 *data = g_variant_new_boolean(TRUE); /* Always on. */
269 case SR_CONF_UNDER_VOLTAGE_CONDITION_ACTIVE:
270 *data = g_variant_new_boolean(devc->uvc_active);
279 static int config_set(uint32_t key, GVariant *data,
280 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
282 struct dev_context *devc;
287 if (sdi->status != SR_ST_ACTIVE)
288 return SR_ERR_DEV_CLOSED;
294 case SR_CONF_LIMIT_SAMPLES:
295 devc->limit_samples = g_variant_get_uint64(data);
297 case SR_CONF_LIMIT_MSEC:
298 devc->limit_msec = g_variant_get_uint64(data);
300 case SR_CONF_ENABLED:
301 ret = reloadpro_set_on_off(sdi, g_variant_get_boolean(data));
303 case SR_CONF_CURRENT_LIMIT:
304 ret = reloadpro_set_current_limit(sdi,
305 g_variant_get_double(data));
314 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
317 struct dev_context *devc;
318 struct sr_serial_dev_inst *serial;
320 if (sdi->status != SR_ST_ACTIVE)
321 return SR_ERR_DEV_CLOSED;
326 /* Send the 'monitor <ms>' command (doesn't have a reply). */
327 if ((ret = serial_write_blocking(serial, CMD_MONITOR,
328 strlen(CMD_MONITOR), serial_timeout(serial,
329 strlen(CMD_MONITOR)))) < (int)strlen(CMD_MONITOR)) {
330 sr_err("Unable to send 'monitor' command: %d.", ret);
334 /* Poll every 100ms, or whenever some data comes in. */
335 serial_source_add(sdi->session, serial, G_IO_IN, 100,
336 reloadpro_receive_data, (void *)sdi);
338 std_session_send_df_header(sdi, LOG_PREFIX);
340 memset(devc->buf, 0, RELOADPRO_BUFSIZE);
342 devc->num_samples = 0;
343 devc->starttime = g_get_monotonic_time();
348 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
350 return std_serial_dev_acquisition_stop(sdi,
351 std_serial_dev_close, sdi->conn, LOG_PREFIX);
354 static struct sr_dev_driver arachnid_labs_re_load_pro_driver_info = {
355 .name = "arachnid-labs-re-load-pro",
356 .longname = "Arachnid Labs Re:load Pro",
359 .cleanup = std_cleanup,
361 .dev_list = std_dev_list,
362 .config_get = config_get,
363 .config_set = config_set,
364 .config_list = config_list,
365 .dev_open = std_serial_dev_open,
366 .dev_close = std_serial_dev_close,
367 .dev_acquisition_start = dev_acquisition_start,
368 .dev_acquisition_stop = dev_acquisition_stop,
371 SR_REGISTER_DEV_DRIVER(arachnid_labs_re_load_pro_driver_info);