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, see <http://www.gnu.org/licenses/>.
24 #define SERIALCOMM "115200/8n1"
26 #define CMD_VERSION "version\r\n"
27 #define CMD_MONITOR "monitor 200\r\n"
28 #define CMD_MONITOR_STOP "monitor 0\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 | SR_CONF_LIST,
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 SR_CONF_UNDER_VOLTAGE_CONDITION_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
60 static const char *regulation[] = {
65 static GSList *scan(struct sr_dev_driver *di, GSList *options)
67 struct sr_dev_inst *sdi;
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;
75 const char *conn, *serialcomm;
80 conn = serialcomm = NULL;
81 for (l = options; l; l = l->next) {
85 conn = g_variant_get_string(src->data, NULL);
87 case SR_CONF_SERIALCOMM:
88 serialcomm = g_variant_get_string(src->data, NULL);
95 serialcomm = SERIALCOMM;
97 serial = sr_serial_dev_inst_new(conn, serialcomm);
99 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
103 * First stop potentially running monitoring and wait for 50ms before
104 * next command can be sent.
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);
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);
123 memset(buf, 0, sizeof(buf));
126 ret = serial_readline(serial, &bufptr, &len, 3000);
128 if (ret < 0 || len < 9 || strncmp((const char *)&buf, "version ", 8)) {
129 sr_dbg("Unable to probe version number.");
130 serial_close(serial);
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);
141 sdi = g_malloc0(sizeof(struct sr_dev_inst));
142 sdi->status = SR_ST_INACTIVE;
143 sdi->vendor = g_strdup("Arachnid Labs");
144 sdi->model = g_strdup("Re:load Pro");
145 sdi->version = g_strdup(buf + 8);
146 sdi->inst_type = SR_INST_SERIAL;
149 cg = sr_channel_group_new(sdi, "1", NULL);
151 ch = sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "V");
152 cg->channels = g_slist_append(cg->channels, ch);
154 ch = sr_channel_new(sdi, 1, SR_CHANNEL_ANALOG, TRUE, "I");
155 cg->channels = g_slist_append(cg->channels, ch);
157 devc = g_malloc0(sizeof(struct dev_context));
158 sr_sw_limits_init(&devc->limits);
161 serial_close(serial);
163 return std_scan_complete(di, g_slist_append(NULL, sdi));
166 static int config_list(uint32_t key, GVariant **data,
167 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
170 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
173 case SR_CONF_DEVICE_OPTIONS:
174 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg));
176 case SR_CONF_REGULATION:
177 *data = std_gvar_array_str(ARRAY_AND_SIZE(regulation));
179 case SR_CONF_CURRENT_LIMIT:
180 *data = std_gvar_min_max_step(0.0, 6.0, 0.001);
182 case SR_CONF_UNDER_VOLTAGE_CONDITION_THRESHOLD:
183 *data = std_gvar_min_max_step(0.0, 60.0, 0.001);
193 static int config_get(uint32_t key, GVariant **data,
194 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
196 struct dev_context *devc;
204 * These features/keys are not supported by the hardware:
205 * - SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE
206 * - SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD
207 * - SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE
208 * - SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD
209 * - SR_CONF_ENABLED (state cannot be queried, only set)
213 case SR_CONF_LIMIT_SAMPLES:
214 case SR_CONF_LIMIT_MSEC:
215 return sr_sw_limits_config_get(&devc->limits, key, data);
216 case SR_CONF_REGULATION:
217 *data = g_variant_new_string("CC"); /* Always CC mode. */
219 case SR_CONF_VOLTAGE:
220 if (reloadpro_get_voltage_current(sdi, &fvalue, NULL) < 0)
222 *data = g_variant_new_double(fvalue);
224 case SR_CONF_CURRENT:
225 if (reloadpro_get_voltage_current(sdi, NULL, &fvalue) < 0)
227 *data = g_variant_new_double(fvalue);
229 case SR_CONF_CURRENT_LIMIT:
230 if (reloadpro_get_current_limit(sdi, &fvalue) == SR_OK)
231 *data = g_variant_new_double(fvalue);
233 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
234 *data = g_variant_new_boolean(TRUE); /* Always on. */
236 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
237 *data = g_variant_new_boolean(TRUE); /* Always on. */
239 case SR_CONF_OVER_TEMPERATURE_PROTECTION:
240 *data = g_variant_new_boolean(TRUE); /* Always on. */
242 case SR_CONF_OVER_TEMPERATURE_PROTECTION_ACTIVE:
243 *data = g_variant_new_boolean(devc->otp_active);
245 case SR_CONF_UNDER_VOLTAGE_CONDITION:
246 if (reloadpro_get_under_voltage_threshold(sdi, &fvalue) == SR_OK)
247 *data = g_variant_new_boolean(fvalue != 0.0);
249 case SR_CONF_UNDER_VOLTAGE_CONDITION_ACTIVE:
250 *data = g_variant_new_boolean(devc->uvc_active);
252 case SR_CONF_UNDER_VOLTAGE_CONDITION_THRESHOLD:
253 if (reloadpro_get_under_voltage_threshold(sdi, &fvalue) == SR_OK)
254 *data = g_variant_new_double(fvalue);
263 static int config_set(uint32_t key, GVariant *data,
264 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
266 struct dev_context *devc;
273 case SR_CONF_LIMIT_SAMPLES:
274 case SR_CONF_LIMIT_MSEC:
275 return sr_sw_limits_config_set(&devc->limits, key, data);
276 case SR_CONF_ENABLED:
277 return reloadpro_set_on_off(sdi, g_variant_get_boolean(data));
278 case SR_CONF_CURRENT_LIMIT:
279 return reloadpro_set_current_limit(sdi, g_variant_get_double(data));
280 case SR_CONF_UNDER_VOLTAGE_CONDITION_THRESHOLD:
281 return reloadpro_set_under_voltage_threshold(sdi,
282 g_variant_get_double(data));
290 static int dev_close(struct sr_dev_inst *sdi)
292 if (serial_write_blocking(sdi->conn, CMD_MONITOR_STOP,
293 strlen(CMD_MONITOR_STOP), serial_timeout(sdi->conn,
294 strlen(CMD_MONITOR_STOP))) < (int)strlen(CMD_MONITOR_STOP)) {
295 sr_dbg("Unable to stop monitoring.");
298 return std_serial_dev_close(sdi);
301 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
304 struct dev_context *devc;
305 struct sr_serial_dev_inst *serial;
308 devc->acquisition_running = TRUE;
312 /* Send the 'monitor <ms>' command (doesn't have a reply). */
313 if ((ret = serial_write_blocking(serial, CMD_MONITOR,
314 strlen(CMD_MONITOR), serial_timeout(serial,
315 strlen(CMD_MONITOR)))) < (int)strlen(CMD_MONITOR)) {
316 sr_err("Unable to send 'monitor' command: %d.", ret);
320 sr_sw_limits_acquisition_start(&devc->limits);
321 std_session_send_df_header(sdi);
323 memset(devc->buf, 0, RELOADPRO_BUFSIZE);
326 g_mutex_init(&devc->acquisition_mutex);
328 serial_source_add(sdi->session, serial, G_IO_IN, 100,
329 reloadpro_receive_data, (void *)sdi);
334 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
336 struct dev_context *devc;
340 devc->acquisition_running = FALSE;
342 ret = std_serial_dev_acquisition_stop(sdi);
343 g_mutex_clear(&devc->acquisition_mutex);
348 static struct sr_dev_driver arachnid_labs_re_load_pro_driver_info = {
349 .name = "arachnid-labs-re-load-pro",
350 .longname = "Arachnid Labs Re:load Pro",
353 .cleanup = std_cleanup,
355 .dev_list = std_dev_list,
356 .dev_clear = std_dev_clear,
357 .config_get = config_get,
358 .config_set = config_set,
359 .config_list = config_list,
360 .dev_open = std_serial_dev_open,
361 .dev_close = dev_close,
362 .dev_acquisition_start = dev_acquisition_start,
363 .dev_acquisition_stop = dev_acquisition_stop,
366 SR_REGISTER_DEV_DRIVER(arachnid_labs_re_load_pro_driver_info);