2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Matthias Heidbrink <m-sigrok@heidbrink.biz>
5 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com> (code from atten-pps3xxx)
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 SR_PRIV int lps_read_reply(struct sr_serial_dev_inst *serial, char **buf, int *buflen);
28 SR_PRIV int lps_send_va(struct sr_serial_dev_inst *serial, const char *fmt, va_list args);
29 SR_PRIV int lps_cmd_ok(struct sr_serial_dev_inst *serial, const char *fmt, ...);
30 SR_PRIV int lps_cmd_reply(char *reply, struct sr_serial_dev_inst *serial, const char *fmt, ...);
31 SR_PRIV int lps_query_status(struct sr_dev_inst *sdi);
33 #define SERIALCOMM "2400/8n1/dtr=1/rts=1/flow=0"
35 #define VENDOR_MOTECH "Motech"
37 static const uint32_t scanopts[] = {
42 static const uint32_t drvopts[] = {
46 static const uint32_t devopts[] = {
48 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
49 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
50 SR_CONF_CHANNEL_CONFIG | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
53 /** Hardware capabilities channel 1, 2. */
54 static const uint32_t devopts_cg_ch12[] = {
55 SR_CONF_VOLTAGE | SR_CONF_GET,
56 SR_CONF_VOLTAGE_TARGET | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
57 SR_CONF_CURRENT | SR_CONF_GET,
58 SR_CONF_CURRENT_LIMIT | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
59 SR_CONF_ENABLED | SR_CONF_GET | SR_CONF_SET,
62 /** Hardware capabilities channel 3 (LPS-304/305 only). */
63 static const uint32_t devopts_cg_ch3[] = {
64 SR_CONF_VOLTAGE | SR_CONF_GET,
65 SR_CONF_ENABLED | SR_CONF_GET | SR_CONF_SET,
68 static const char *channel_modes[] = {
74 static const struct lps_modelspec models[] = {
75 { LPS_UNKNOWN, "Dummy", 0,
80 { LPS_301, "LPS-301", 1,
83 { { 0, 32, 0.01 }, { 0.005, 2, 0.001 } },
86 { LPS_302, "LPS-302", 1,
89 { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } },
92 { LPS_303, "LPS-303", 1,
95 { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } },
98 { LPS_304, "LPS-304", 3,
101 { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } },
103 { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } },
105 { { 5, 5, 0.0 }, { 0.005, 3, 0.001 } },
108 { LPS_305, "LPS-305", 3,
111 { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } },
113 { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } },
115 { { 3.3, 5, 1.7 }, { 0.005, 3, 0.001 } },
120 /** Send command to device with va_list. */
121 SR_PRIV int lps_send_va(struct sr_serial_dev_inst *serial, const char *fmt, va_list args)
124 char auxfmt[LINELEN_MAX];
125 char buf[LINELEN_MAX];
127 snprintf(auxfmt, sizeof(auxfmt), "%s\r\n", fmt);
128 vsnprintf(buf, sizeof(buf), auxfmt, args);
130 sr_spew("lps_send_va: \"%s\"", buf);
132 retc = serial_write_blocking(serial, buf, strlen(buf),
133 serial_timeout(serial, strlen(buf)));
141 /** Send command to device. */
142 SR_PRIV int lps_send_req(struct sr_serial_dev_inst *serial, const char *fmt, ...)
148 retc = lps_send_va(serial, fmt, args);
154 /** Send command and consume simple OK reply. */
155 SR_PRIV int lps_cmd_ok(struct sr_serial_dev_inst *serial, const char *fmt, ...)
159 char buf[LINELEN_MAX];
165 retc = lps_send_va(serial, fmt, args);
174 buflen = sizeof(buf);
175 retc = lps_read_reply(serial, &bufptr, &buflen);
176 if ((retc == SR_OK) && (buflen == 0))
183 * Send command and read reply string.
184 * @param reply Pointer to buffer of size LINELEN_MAX. Will be NUL-terminated.
186 SR_PRIV int lps_cmd_reply(char *reply, struct sr_serial_dev_inst *serial, const char *fmt, ...)
190 char buf[LINELEN_MAX];
198 retc = lps_send_va(serial, fmt, args);
207 buflen = sizeof(buf);
208 retc = lps_read_reply(serial, &bufptr, &buflen);
209 if ((retc == SR_OK) && (buflen > 0)) {
217 /** Process integer value returned by STATUS command. */
218 SR_PRIV int lps_process_status(struct sr_dev_inst *sdi, int stat)
220 struct dev_context *devc;
225 sr_spew("Status: %d", stat);
226 devc->channel_status[0].cc_mode = (stat & 0x01) != 0;
227 sr_spew("Channel 1 %s mode", devc->channel_status[0].cc_mode?"CC":"CV");
228 if (devc->model->num_channels > 1) {
229 devc->channel_status[1].cc_mode = (stat & 0x02) != 0;
230 sr_spew("Channel 2 %s mode", devc->channel_status[1].cc_mode?"CC":"CV");
232 tracking_mode = (stat & 0x0c) >> 2;
233 switch (tracking_mode) {
234 case 0: devc->tracking_mode = 0;
236 case 2: devc->tracking_mode = 1;
238 case 3: devc->tracking_mode = 2;
241 sr_err("Illegal channel tracking mode %d!", tracking_mode);
242 devc->tracking_mode = 0;
246 sr_spew("Channel tracking: %d", devc->tracking_mode);
248 devc->channel_status[0].output_enabled = devc->channel_status[1].output_enabled = stat&0x040?TRUE:FALSE;
249 sr_spew("Channel 1%s output: %s", devc->model->num_channels > 1?"+2":"", devc->channel_status[0].output_enabled?"ON":"OFF");
250 if (devc->model->num_channels > 2) {
251 devc->channel_status[2].output_enabled = stat&0x010?TRUE:FALSE;
252 devc->channel_status[2].output_voltage_last = stat&0x020?3.3:5;
253 sr_spew("Channel 3 output: %s, U=%02f V, overload=%d",
254 devc->channel_status[2].output_enabled?"ON":"OFF",
255 devc->channel_status[2].output_voltage_last,
258 sr_spew("Fan=%d, beep=%d, CC output compensated=%d", stat&0x0100?1:0, stat&0x0200?1:0, stat&0x0400?1:0);
263 /** Send STATUS commend and process status string. */
264 SR_PRIV int lps_query_status(struct sr_dev_inst *sdi)
266 char buf[LINELEN_MAX];
268 struct dev_context *devc;
272 devc->req_sent_at = g_get_real_time();
274 if ((ret = lps_cmd_reply(buf, sdi->conn, "STATUS")) < 0) {
275 sr_err("%s: Failed to read status: %s.", __func__,
280 if (sr_atoi(buf, &stat) != SR_OK)
283 return lps_process_status(sdi, stat);
286 static gint64 calc_timeout_ms(gint64 start_us)
288 gint64 result = REQ_TIMEOUT_MS - ((g_get_real_time() - start_us) / 1000);
297 * Read message into buf until "OK" received.
299 * @retval SR_OK Msg received; buf and buflen contain result, if any except OK.
300 * @retval SR_ERR Error, including timeout.
302 SR_PRIV int lps_read_reply(struct sr_serial_dev_inst *serial, char **buf, int *buflen)
305 char buf2[LINELEN_MAX];
308 gint64 timeout_start;
312 /* Read one line. It is either a data message or "OK". */
313 timeout_start = g_get_real_time();
315 /* Up to 5 tries because serial_readline() will consume only one CR or LF per
316 * call, but device sends up to 4 in a row. */
317 for (retries = 0; retries < 5; retries++) {
319 if (serial_readline(serial, buf, buflen, calc_timeout_ms(timeout_start)) != SR_OK)
321 if (!strcmp(*buf, "OK")) { /* We got an OK! */
326 if (*buflen > 0) /* We got a msg! */
330 /* A data msg is in buf (possibly ERROR), need to consume "OK". */
333 for (retries = 0; retries < 5; retries++) {
334 buf2len = sizeof(buf2);
335 if (serial_readline(serial, &buf2ptr, &buf2len, calc_timeout_ms(timeout_start)) != SR_OK)
338 if (!strcmp(buf2ptr, "OK")) { /* We got an OK! */
339 if (!strcmp(*buf, "ERROR")) { /* OK came after msg ERROR! */
340 sr_spew("ERROR found!");
349 return SR_ERR; /* Timeout! */
352 /** Scan for LPS-300 series device. */
353 static GSList *do_scan(lps_modelid modelid, struct sr_dev_driver *drv, GSList *options)
355 struct sr_dev_inst *sdi;
356 struct dev_context *devc;
357 struct sr_serial_dev_inst *serial;
358 struct sr_channel *ch;
359 struct sr_channel_group *cg;
360 const char *conn, *serialcomm;
362 gchar buf[LINELEN_MAX];
368 conn = serialcomm = NULL;
370 /* Process and check options. */
371 if (sr_serial_extract_options(options, &conn, &serialcomm) != SR_OK)
374 serialcomm = SERIALCOMM;
376 /* Init serial port. */
377 serial = sr_serial_dev_inst_new(conn, serialcomm);
379 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
382 /* Query and verify model string. */
383 serial_flush(serial);
384 if (lps_cmd_reply(buf, serial, "MODEL") != SR_OK)
387 /* Check model string. */
388 if (strncmp(buf, "LPS-", 4)) {
389 sr_spew("Unknown model code \"%s\"!", buf);
393 /* Bug in device FW 1.17, model number is empty, so this can't work with this FW! */
394 if (modelid == LPS_UNKNOWN) {
396 for (cnt = LPS_301; cnt <= LPS_305; cnt++) {
397 if (!strcmp(buf, models[cnt].modelstr)) {
402 if (modelid == LPS_UNKNOWN) {
403 sr_err("Unable to detect model from model string '%s'!", buf);
410 if ((ret = lps_cmd_reply(buf, serial, "VERSION")) == SR_OK) {
411 if (strncmp(buf, "Ver-", 4)) {
412 sr_spew("Version string %s not recognized.", buf);
419 else /* Bug in device FW 1.17: Querying version string fails while output is active.
420 Therefore just print an error message, but do not exit with error. */
421 sr_err("Failed to query for hardware version: %s.",
424 sdi = g_malloc0(sizeof(struct sr_dev_inst));
425 sdi->status = SR_ST_INACTIVE;
426 sdi->vendor = g_strdup(VENDOR_MOTECH);
427 sdi->model = g_strdup(models[modelid].modelstr);
428 sdi->version = g_strdup(verstr);
429 sdi->inst_type = SR_INST_SERIAL;
432 devc = g_malloc0(sizeof(struct dev_context));
433 sr_sw_limits_init(&devc->limits);
434 devc->model = &models[modelid];
438 /* Setup channels and channel groups. */
439 for (cnt = 0; cnt < models[modelid].num_channels; cnt++) {
440 snprintf(channel, sizeof(channel), "CH%d", cnt + 1);
441 ch = sr_channel_new(sdi, cnt, SR_CHANNEL_ANALOG, TRUE, channel);
443 devc->channel_status[cnt].info = g_slist_append(NULL, ch);
445 cg = g_malloc(sizeof(struct sr_channel_group));
446 snprintf(channel, sizeof(channel), "CG%d", cnt+1);
447 cg->name = g_strdup(channel);
449 cg->channels = g_slist_append(NULL, ch);
451 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
455 if (lps_query_status(sdi) != SR_OK)
458 serial_close(serial);
460 return std_scan_complete(drv, g_slist_append(NULL, sdi));
463 sr_info("%s: Error!", __func__);
466 serial_close(serial);
467 sr_serial_dev_inst_free(serial);
469 sr_dev_inst_free(sdi);
474 /** Scan for LPS-301 device. */
475 static GSList *scan_lps301(struct sr_dev_driver *di, GSList *options)
477 return do_scan(LPS_301, di, options);
480 static void clear_helper(struct dev_context *devc)
484 /* Free channel_status.info (list only, data owned by sdi). */
485 for (ch_idx = 0; ch_idx < devc->model->num_channels; ch_idx++)
486 g_slist_free(devc->channel_status[ch_idx].info);
489 static int dev_clear(const struct sr_dev_driver *di)
491 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
494 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
495 const struct sr_channel_group *cg)
497 struct dev_context *devc;
498 struct sr_channel *ch;
508 case SR_CONF_LIMIT_SAMPLES:
509 case SR_CONF_LIMIT_MSEC:
510 return sr_sw_limits_config_get(&devc->limits, key, data);
511 case SR_CONF_CHANNEL_CONFIG:
512 *data = g_variant_new_string(channel_modes[devc->tracking_mode]);
518 /* We only ever have one channel per channel group in this driver. */
519 ch = cg->channels->data;
523 case SR_CONF_VOLTAGE:
524 *data = g_variant_new_double(devc->channel_status[ch_idx].output_voltage_last);
526 case SR_CONF_VOLTAGE_TARGET:
527 *data = g_variant_new_double(devc->channel_status[ch_idx].output_voltage_max);
529 case SR_CONF_CURRENT:
530 *data = g_variant_new_double(devc->channel_status[ch_idx].output_current_last);
532 case SR_CONF_CURRENT_LIMIT:
533 *data = g_variant_new_double(devc->channel_status[ch_idx].output_current_max);
535 case SR_CONF_ENABLED:
536 *data = g_variant_new_boolean(devc->channel_status[ch_idx].output_enabled);
546 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
547 const struct sr_channel_group *cg)
549 struct dev_context *devc;
550 struct sr_channel *ch;
560 /* Cannot change settings while acquisition active, would cause a mess with commands.
561 * Changing this would be possible, but tricky. */
562 if (devc->acq_running)
567 case SR_CONF_LIMIT_MSEC:
568 case SR_CONF_LIMIT_SAMPLES:
569 return sr_sw_limits_config_set(&devc->limits, key, data);
570 case SR_CONF_CHANNEL_CONFIG:
571 sval = g_variant_get_string(data, NULL);
573 for (idx = 0; idx < (int)ARRAY_SIZE(channel_modes); idx++) {
574 if (!strcmp(sval, channel_modes[idx])) {
576 if (devc->tracking_mode == idx)
577 break; /* Nothing to do! */
578 devc->tracking_mode = idx;
579 if (devc->model->modelid >= LPS_304) /* No use to set anything in the smaller models. */
580 return lps_cmd_ok(sdi->conn, "TRACK%1d", devc->tracking_mode);
582 if (devc->model->modelid <= LPS_303) /* Only first setting possible for smaller models. */
592 /* We only ever have one channel per channel group in this driver. */
593 ch = cg->channels->data;
597 case SR_CONF_VOLTAGE_TARGET:
598 dval = g_variant_get_double(data);
599 if (dval < 0 || dval > devc->model->channels[ch_idx].voltage[1])
602 if (devc->model->modelid < LPS_304)
605 if (fabs(dval - 5.000) <= 0.001)
607 else if ((devc->model->modelid >= LPS_305) && (fabs(dval - 3.300) <= 0.001))
609 else return SR_ERR_ARG;
612 devc->channel_status[ch_idx].output_voltage_max = dval;
614 return lps_cmd_ok(sdi->conn, "VDD%1.0f", trunc(dval));
616 return lps_cmd_ok(sdi->conn, "VSET%d %05.3f", ch_idx+1, dval);
618 case SR_CONF_CURRENT_LIMIT:
619 dval = g_variant_get_double(data);
620 if (dval < 0 || dval > devc->model->channels[ch_idx].current[1])
622 if (ch_idx == 2) /* No current setting for CH3. */
624 devc->channel_status[ch_idx].output_current_max = dval;
625 return lps_cmd_ok(sdi->conn, "ISET%d %05.4f", ch_idx+1, dval);
627 case SR_CONF_ENABLED:
628 bval = g_variant_get_boolean(data);
629 if (bval == devc->channel_status[ch_idx].output_enabled) /* Nothing to do. */
631 devc->channel_status[ch_idx].output_enabled = bval;
632 if (ch_idx != 2) { /* Channels 1,2 can be set only together. */
633 devc->channel_status[ch_idx^1].output_enabled = bval;
634 return lps_cmd_ok(sdi->conn, "OUT%1d", (int)bval);
635 } else { /* Channel 3: No command to disable output, set voltage to 0 instead. */
637 return lps_cmd_ok(sdi->conn, "VDD%1.0f", devc->channel_status[ch_idx].output_voltage_max);
639 return lps_cmd_ok(sdi->conn, "VDD0");
650 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
651 const struct sr_channel_group *cg)
653 struct dev_context *devc;
654 struct sr_channel *ch;
657 devc = (sdi) ? sdi->priv : NULL;
661 case SR_CONF_SCAN_OPTIONS:
662 case SR_CONF_DEVICE_OPTIONS:
663 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
664 case SR_CONF_CHANNEL_CONFIG:
665 if (devc->model->modelid <= LPS_303) {
666 /* The 1-channel models. */
667 *data = g_variant_new_strv(channel_modes, 1);
669 /* The other models support all modes. */
670 *data = g_variant_new_strv(ARRAY_AND_SIZE(channel_modes));
678 /* We only ever have one channel per channel group in this driver. */
679 ch = cg->channels->data;
683 case SR_CONF_DEVICE_OPTIONS:
684 if ((ch_idx == 0) || (ch_idx == 1)) /* CH1, CH2 */
685 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_ch12));
686 else /* Must be CH3 */
687 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_ch3));
689 case SR_CONF_VOLTAGE_TARGET:
690 *data = std_gvar_min_max_step_array(devc->model->channels[ch_idx].voltage);
692 case SR_CONF_CURRENT_LIMIT:
693 *data = std_gvar_min_max_step_array(devc->model->channels[ch_idx].current);
702 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
704 struct dev_context *devc;
705 struct sr_serial_dev_inst *serial;
709 devc->acq_running = TRUE;
712 serial_source_add(sdi->session, serial, G_IO_IN, 50,
713 motech_lps_30x_receive_data, (void *)sdi);
714 std_session_send_df_header(sdi);
716 sr_sw_limits_acquisition_start(&devc->limits);
718 devc->acq_req = AQ_NONE;
719 /* Do not start polling device here, the read function will do it in 50 ms. */
724 static struct sr_dev_driver motech_lps_301_driver_info = {
725 .name = "motech-lps-301",
726 .longname = "Motech LPS-301",
729 .cleanup = std_cleanup,
731 .dev_list = std_dev_list,
732 .dev_clear = dev_clear,
733 .config_get = config_get,
734 .config_set = config_set,
735 .config_list = config_list,
736 .dev_open = std_serial_dev_open,
737 .dev_close = std_serial_dev_close,
738 .dev_acquisition_start = dev_acquisition_start,
739 .dev_acquisition_stop = std_serial_dev_acquisition_stop,
742 SR_REGISTER_DEV_DRIVER(motech_lps_301_driver_info);