From: Matthias Heidbrink Date: Mon, 19 May 2014 17:48:28 +0000 (+0200) Subject: motech-lps-30x: Implemented driver. X-Git-Tag: libsigrok-0.4.0~1315 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=1c3d002b68d9fe6fe511c84e4da7f164f32f35ff;p=libsigrok.git motech-lps-30x: Implemented driver. --- diff --git a/hardware/motech-lps-30x/api.c b/hardware/motech-lps-30x/api.c index 0b4f2b2b..d4b6c0d7 100644 --- a/hardware/motech-lps-30x/api.c +++ b/hardware/motech-lps-30x/api.c @@ -2,6 +2,7 @@ * This file is part of the libsigrok project. * * Copyright (C) 2014 Matthias Heidbrink + * Copyright (C) 2014 Bert Vermeulen (code from atten-pps3xxx) * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -17,174 +18,846 @@ * along with this program. If not, see . */ +/** @file + * Motech LPS-30x series power supply driver + * @internal + */ + +#include +#include +#include +#include + #include "protocol.h" +/* Forward declarations */ SR_PRIV struct sr_dev_driver motech_lps_301_driver_info; -static struct sr_dev_driver *di = &motech_lps_301_driver_info; +SR_PRIV int lps_read_reply(struct sr_serial_dev_inst *serial, char **buf, int *buflen); +SR_PRIV int lps_send_va(struct sr_serial_dev_inst *serial, const char* fmt, va_list args); +SR_PRIV int lps_cmd_ok(struct sr_serial_dev_inst *serial, const char* fmt, ...); +SR_PRIV int lps_cmd_reply(char* reply, struct sr_serial_dev_inst *serial, const char* fmt, ...); +SR_PRIV int lps_query_status(struct sr_dev_inst* sdi); + +/* Serial communication parameters */ +#define SERIALCOMM "2400/8n1/dtr=1/rts=1/flow=0" + +#define VENDOR_MOTECH "Motech" + +/** Driver scanning options. */ +static const int32_t hwopts[] = { + SR_CONF_CONN, + SR_CONF_SERIALCOMM, +}; + +/** Hardware capabilities generic. */ +static const int32_t hwcaps[] = { + /* Device class */ + SR_CONF_POWER_SUPPLY, + /* Aquisition modes. */ + SR_CONF_LIMIT_SAMPLES, + SR_CONF_LIMIT_MSEC, + SR_CONF_CONTINUOUS, + /* Device configuration */ + SR_CONF_OUTPUT_CHANNEL, +}; + +/** Hardware capabilities channel 1, 2. */ +static const int32_t hwcaps_ch12[] = { + SR_CONF_OUTPUT_VOLTAGE, + SR_CONF_OUTPUT_VOLTAGE_MAX, + SR_CONF_OUTPUT_CURRENT, + SR_CONF_OUTPUT_CURRENT_MAX, + SR_CONF_OUTPUT_ENABLED, +}; + +/** Hardware capabilities channel 3. (LPS-304/305 only). */ +static const int32_t hwcaps_ch3[] = { + SR_CONF_OUTPUT_VOLTAGE, + SR_CONF_OUTPUT_ENABLED, +}; + +static const char *channel_modes[] = { + "Independent", + "Track1", + "Track2", +}; + +static struct lps_modelspec models[] = { + { LPS_UNKNOWN, "Dummy", 0, + { + + } + }, + { LPS_301, "LPS-301", 1, + { + /* Channel 1 */ + { { 0, 32, 0.01 }, { 0.005, 2, 0.001 } }, + }, + }, + { LPS_302, "LPS-302", 1, + { + /* Channel 1 */ + { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } }, + }, + }, + { LPS_303, "LPS-303", 1, + { + /* Channel 1 */ + { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } }, + }, + }, + { LPS_304, "LPS-304", 3, + { + /* Channel 1 */ + { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } }, + /* Channel 2 */ + { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } }, + /* Channel 3 */ + { { 5, 5, 0.0 }, { 0.005, 3, 0.001 } }, + }, + }, + { LPS_305, "LPS-305", 3, + { + /* Channel 1 */ + { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } }, + /* Channel 2 */ + { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } }, + /* Channel 3 */ + { { 3.3, 5, 1.7 }, { 0.005, 3, 0.001 } }, + }, + }, +}; -static int init(struct sr_context *sr_ctx) +static int init_lps301(struct sr_context *sr_ctx) { - return std_init(sr_ctx, di, LOG_PREFIX); + return std_init(sr_ctx, &motech_lps_301_driver_info, LOG_PREFIX); } -static GSList *scan(GSList *options) +/** Send command to device with va_list. + */ +SR_PRIV int lps_send_va(struct sr_serial_dev_inst *serial, const char* fmt, va_list args) { - struct drv_context *drvc; - GSList *devices; + int retc; + char auxfmt[LINELEN_MAX]; + char buf[LINELEN_MAX]; - (void)options; + snprintf(auxfmt, sizeof(auxfmt), "%s\r\n", fmt); + vsnprintf(buf, sizeof(buf), auxfmt, args); - devices = NULL; - drvc = di->priv; - drvc->instances = NULL; + sr_spew("lps_send_va: \"%s\"", buf); - /* TODO: scan for devices, either based on a SR_CONF_CONN option - * or on a USB scan. */ + retc = serial_write_nonblocking(serial, buf, strlen(buf)); - return devices; + if (retc < 0) + return SR_ERR; + + return SR_OK; } -static GSList *dev_list(void) +/** Send command to device. + */ +SR_PRIV int lps_send_req(struct sr_serial_dev_inst *serial, const char* fmt, ...) { - return ((struct drv_context *)(di->priv))->instances; + int retc; + va_list args; + + va_start(args, fmt); + retc = lps_send_va(serial, fmt, args); + va_end(args); + + return retc; } -static int dev_clear(void) +/** Send command and consume simple OK reply. */ +SR_PRIV int lps_cmd_ok(struct sr_serial_dev_inst *serial, const char* fmt, ...) { - return std_dev_clear(di, NULL); + int retc; + va_list args; + char buf[LINELEN_MAX]; + char* bufptr; + int buflen; + + /* Send command */ + va_start(args, fmt); + retc = lps_send_va(serial, fmt, args); + va_end(args); + + if (retc != SR_OK) + return SR_ERR; + + /* Read reply */ + buf[0] = '\0'; + bufptr = buf; + buflen = sizeof(buf); + retc = lps_read_reply(serial, &bufptr, &buflen); + if ((retc == SR_OK) && (buflen == 0)) + return SR_OK; + + return SR_ERR; } -static int dev_open(struct sr_dev_inst *sdi) +/** Send command and read reply string. + * \param reply Pointer to buffer of size LINELEN_MAX. Will be NUL-terminated. + */ +SR_PRIV int lps_cmd_reply(char* reply, struct sr_serial_dev_inst *serial, const char* fmt, ...) { - (void)sdi; + int retc; + va_list args; + char buf[LINELEN_MAX]; + char* bufptr; + int buflen; + + reply[0] = '\0'; + + /* Send command */ + va_start(args, fmt); + retc = lps_send_va(serial, fmt, args); + va_end(args); + + if (retc != SR_OK) + return SR_ERR; + + /* Read reply */ + buf[0] = '\0'; + bufptr = buf; + buflen = sizeof(buf); + retc = lps_read_reply(serial, &bufptr, &buflen); + if ((retc == SR_OK) && (buflen > 0)) { + strcpy(reply, buf); + return SR_OK; + } - /* TODO: get handle from sdi->conn and open it. */ + return SR_ERR; +} - sdi->status = SR_ST_ACTIVE; +/** Process integer value returned by STATUS command. */ +SR_PRIV int lps_process_status(struct sr_dev_inst* sdi, int stat) +{ + struct dev_context* devc; + int tracking_mode; + + devc = (struct dev_context*)sdi->priv; + + sr_spew("Status: %d", stat); + devc->channel_status[0].cc_mode = (stat & 0x01) != 0; + sr_spew("Channel 1 %s mode", devc->channel_status[0].cc_mode?"CC":"CV"); + if (devc->model->num_channels > 1) { + devc->channel_status[1].cc_mode = (stat & 0x02) != 0; + sr_spew("Channel 2 %s mode", devc->channel_status[1].cc_mode?"CC":"CV"); + + tracking_mode = (stat & 0x0c) >> 2; + switch (tracking_mode) { + case 0: devc->tracking_mode = 0; + break; + case 2: devc->tracking_mode = 1; + break; + case 3: devc->tracking_mode = 2; + break; + default: + sr_err("Illegal channel tracking mode %d!", tracking_mode); + devc->tracking_mode = 0; + break; + } + + sr_spew("Channel tracking: %d", devc->tracking_mode); + } + devc->channel_status[0].output_enabled = devc->channel_status[1].output_enabled = stat&0x040?TRUE:FALSE; + sr_spew("Channel 1%s output: %s", devc->model->num_channels > 1?"+2":"", devc->channel_status[0].output_enabled?"ON":"OFF"); + if (devc->model->num_channels > 2) { + devc->channel_status[2].output_enabled = stat&0x010?TRUE:FALSE; + devc->channel_status[2].output_voltage_last = stat&0x020?3.3:5; + sr_spew("Channel 3 output: %s, U=%02f V, overload=%d", + devc->channel_status[2].output_enabled?"ON":"OFF", + devc->channel_status[2].output_voltage_last, + stat&0x080?1:0); + } + sr_spew("Fan=%d, beep=%d, CC output compensated=%d", stat&0x0100?1:0, stat&0x0200?1:0, stat&0x0400?1:0); return SR_OK; } -static int dev_close(struct sr_dev_inst *sdi) +/** Send STATUS commend and process status string. */ +SR_PRIV int lps_query_status(struct sr_dev_inst* sdi) { - (void)sdi; + char buf[LINELEN_MAX]; + int stat; + const char* ptr; + struct dev_context* devc; - /* TODO: get handle from sdi->conn and close it. */ + devc = (struct dev_context*)sdi->priv; - sdi->status = SR_ST_INACTIVE; + devc->req_sent_at = g_get_real_time(); - return SR_OK; + if (lps_cmd_reply(buf, sdi->conn, "STATUS") < 0) { + sr_err("%s: Failed to read status: %d %s", __func__, errno, strerror(errno)); + return SR_ERR; + } + + /* Jump across leading zeros to prevent sr_atoi() believing this to be octal. */ + for (ptr = buf; isspace(*buf) || (*ptr == '0'); ptr++) + ; + + if (sr_atoi(ptr, &stat) != SR_OK) + return SR_ERR; + + return lps_process_status(sdi, stat); } -static int cleanup(void) +static gint64 calc_timeout_ms(gint64 start_us) { - dev_clear(); + gint64 result = REQ_TIMEOUT_MS - ((g_get_real_time() - start_us) / 1000); - /* TODO: free other driver resources, if any. */ + if (result < 0) + return 0; - return SR_OK; + return result; } -static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi, - const struct sr_channel_group *cg) +/** Read message into buf until "OK" received. + * \retval SR_OK Msg received; buf and buflen contain result, if any except OK. + * \retval SR_ERR Error, including timeout. +*/ +SR_PRIV int lps_read_reply(struct sr_serial_dev_inst *serial, char **buf, int *buflen) { - int ret; + int retries; + char buf2[LINELEN_MAX]; + char *buf2ptr; + int buf2len; + gint64 timeout_start; + + *buf[0] = '\0'; + + /* Read one line. It is either a data message or "OK". */ + timeout_start = g_get_real_time(); + buf2len = *buflen; + /* Up to 5 tries because serial_readline() will consume only one CR or LF per + * call, but device sends up to 4 in a row. */ + for (retries = 0; retries < 5; retries++) { + *buflen = buf2len; + if (serial_readline(serial, buf, buflen, calc_timeout_ms(timeout_start)) != SR_OK) + return SR_ERR; + if (!strcmp(*buf, "OK")) { /* We got an OK! */ + *buf[0] = '\0'; + *buflen = 0; + return SR_OK; + } + if (*buflen > 0) /* We got a msg! */ + break; + } - (void)sdi; - (void)data; - (void)cg; + /* A data msg is in buf (possibly ERROR), need to consume "OK". */ + buf2[0] = '\0'; + buf2ptr = buf2; + for (retries = 0; retries < 5; retries++) { + buf2len = sizeof(buf2); + if (serial_readline(serial, &buf2ptr, &buf2len, calc_timeout_ms(timeout_start)) != SR_OK) + return SR_ERR; + + if (!strcmp(buf2ptr, "OK")) { /* We got an OK! */ + if (!strcmp(*buf, "ERROR")) { /* OK came after msg ERROR! */ + sr_spew("ERROR found!"); + *buf[0] = '\0'; + *buflen = 0; + return SR_ERR; + } + return SR_OK; + } + } - ret = SR_OK; - switch (key) { - /* TODO */ - default: - return SR_ERR_NA; + return SR_ERR; /* Timeout! */ +} + +/** Scan for LPS-300 series device. + */ +static GSList *doScan(lps_modelid modelid, struct sr_dev_driver *drv, GSList *options) +{ + struct sr_dev_inst *sdi; + struct drv_context *drvc; + struct dev_context *devc; + struct sr_serial_dev_inst *serial; + struct sr_channel *ch; + struct sr_channel_group *cg; + GSList *devices; + const char *conn, *serialcomm; + int cnt; + gchar buf[LINELEN_MAX]; + gchar channel[10]; + char* verstr; + + sdi = NULL; + devc = NULL; + conn = serialcomm = NULL; + devices = NULL; + + drvc = drv->priv; + drvc->instances = NULL; + + sr_spew("scan() called!"); + + /* Process and check options. */ + if (sr_serial_extract_options(options, &conn, &serialcomm) != SR_OK) + return NULL; + if (!serialcomm) + serialcomm = SERIALCOMM; + + /* Init serial port. */ + if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) + return NULL; + + if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) + goto exit_err; + + /* Query and verify model string. */ + serial_flush(serial); + if (lps_cmd_reply(buf, serial, "MODEL") != SR_OK) + return NULL; + + /* Check model string. */ + if (strncmp(buf, "LPS-", 4)) { + sr_spew("Unknown model code \"%s\"!", buf); + return NULL; + } + + /* Bug in device FW 1.17, model number is empty, so this can't work with this FW! */ + if (modelid == LPS_UNKNOWN) { + g_strstrip(buf); + for (cnt = LPS_301; cnt <= LPS_305; cnt++) { + if (!strcmp(buf, models[cnt].modelstr)) { + modelid = cnt; + break; + } + } + if (modelid == LPS_UNKNOWN) { + sr_err("Unable to detect model from model string '%s'!", buf); + return NULL; + } } - return ret; + /* Query version */ + verstr = NULL; + if (lps_cmd_reply(buf, serial, "VERSION") == SR_OK) { + if (strncmp(buf, "Ver-", 4)) { + sr_spew("Version string %s not recognized.", buf); + goto exit_err; + } + + + g_strstrip(buf); + verstr = buf + 4; + } + else /* Bug in device FW 1.17: Quering version string fails while output is active. + Therefore just print an error message, but do not exit with error. */ + sr_err("Failed to query for hardware version: %d %s", errno, strerror(errno)); + + sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, VENDOR_MOTECH, models[modelid].modelstr, verstr); + sdi->driver = drv; + sdi->inst_type = SR_INST_SERIAL; + sdi->conn = serial; + + devc = g_malloc0(sizeof(struct dev_context)); + devc->model = &models[modelid]; + devc->limit_samples = 0; + devc->limit_msec = 0; + devc->num_samples = 0; + devc->elapsed_msec = g_timer_new(); + + sdi->priv = devc; + + /* Setup channels and channel groups. */ + for (cnt = 0; cnt < models[modelid].num_channels; cnt++) { + snprintf(channel, sizeof(channel), "CH%d", cnt + 1); + ch = sr_channel_new(cnt, SR_CHANNEL_ANALOG, TRUE, channel); + sdi->channels = g_slist_append(sdi->channels, ch); + + devc->channel_status[cnt].info = g_slist_append(NULL, ch); + + cg = g_malloc(sizeof(struct sr_channel_group)); + snprintf(channel, sizeof(channel), "CG%d", cnt+1); + cg->name = g_strdup(channel); + cg->priv = NULL; + cg->channels = g_slist_append(NULL, ch); + + sdi->channel_groups = g_slist_append(sdi->channel_groups, cg); + } + + drvc->instances = g_slist_append(drvc->instances, sdi); + devices = g_slist_append(devices, sdi); + + /* Query status */ + if (lps_query_status(sdi) != SR_OK) + goto exit_err; + + serial_close(serial); + if (!devices) + sr_serial_dev_inst_free(serial); + + return devices; + +exit_err: + sr_info("%s: Error!", __func__); + + if (serial) { + serial_close(serial); + sr_serial_dev_inst_free(serial); + } + if (devc) + g_free(devc); + if (sdi) + sr_dev_inst_free(sdi); + + return NULL; } -static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi, +/** Scan for LPS-301 device. */ +static GSList *scan_lps301(GSList *options) +{ + return doScan(LPS_301, &motech_lps_301_driver_info, options); +} + +static GSList *doDevList(struct sr_dev_driver *drv) +{ + return ((struct drv_context *)(drv->priv))->instances; +} + +static GSList *dev_list_lps301(void) +{ + return doDevList(&motech_lps_301_driver_info); +} + +static void dev_clear_private(struct dev_context* devc) +{ + int ch_idx; + + /* Free channel_status.info (list only, data owned by sdi). */ + for (ch_idx = 0; ch_idx < devc->model->num_channels; ch_idx++) + g_slist_free(devc->channel_status[ch_idx].info); + + g_timer_destroy(devc->elapsed_msec); +} + +static int dev_clear_lps301(void) +{ + return std_dev_clear(&motech_lps_301_driver_info, (std_dev_clear_callback)dev_clear_private); +} + +static int cleanup(void) +{ + return dev_clear_lps301(); +} + +static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { - int ret; + struct dev_context *devc; + struct sr_channel *ch; + int ch_idx; + + if (!sdi) + return SR_ERR_ARG; + + devc = sdi->priv; + + if (!cg) { + /* No channel group: global options. */ + switch (key) { + case SR_CONF_LIMIT_SAMPLES: + *data = g_variant_new_uint64(devc->limit_samples); + break; + case SR_CONF_LIMIT_MSEC: + *data = g_variant_new_uint64(devc->limit_msec); + break; + case SR_CONF_OUTPUT_CHANNEL: + *data = g_variant_new_string(channel_modes[devc->tracking_mode]); + break; + default: + return SR_ERR_NA; + } + } else { + /* We only ever have one channel per channel group in this driver. */ + ch = cg->channels->data; + ch_idx = ch->index; + switch (key) { + case SR_CONF_OUTPUT_VOLTAGE: + *data = g_variant_new_double(devc->channel_status[ch_idx].output_voltage_last); + break; + case SR_CONF_OUTPUT_VOLTAGE_MAX: + *data = g_variant_new_double(devc->channel_status[ch_idx].output_voltage_max); + break; + case SR_CONF_OUTPUT_CURRENT: + *data = g_variant_new_double(devc->channel_status[ch_idx].output_current_last); + break; + case SR_CONF_OUTPUT_CURRENT_MAX: + *data = g_variant_new_double(devc->channel_status[ch_idx].output_current_max); + break; + case SR_CONF_OUTPUT_ENABLED: + *data = g_variant_new_boolean(devc->channel_status[ch_idx].output_enabled); + break; + default: + return SR_ERR_NA; + } + } - (void)data; - (void)cg; + return SR_OK; +} + +static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi, + const struct sr_channel_group *cg) +{ + struct dev_context *devc; + struct sr_channel *ch; + gdouble dval; + int ch_idx; + const char *sval; + gboolean bval; + int idx; + gboolean found; if (sdi->status != SR_ST_ACTIVE) return SR_ERR_DEV_CLOSED; - ret = SR_OK; - switch (key) { - /* TODO */ - default: - ret = SR_ERR_NA; + devc = sdi->priv; + + /* Cannot change settings while aquisition active, would cause a mess with commands. + * Changing this would be possible, but tricky. */ + if (devc->acq_running) + return SR_ERR_NA; + + if (!cg) { + /* No channel group: global options. */ + switch (key) { + case SR_CONF_LIMIT_MSEC: + if (g_variant_get_uint64(data) == 0) { + sr_err("LIMIT_MSEC can't be 0."); + return SR_ERR; + } + devc->limit_msec = g_variant_get_uint64(data); + sr_dbg("Setting time limit to %" PRIu64 "ms.", + devc->limit_msec); + break; + case SR_CONF_LIMIT_SAMPLES: + devc->limit_samples = g_variant_get_uint64(data); + sr_dbg("Setting sample limit to %" PRIu64 ".", + devc->limit_samples); + break; + case SR_CONF_OUTPUT_CHANNEL: + sval = g_variant_get_string(data, NULL); + found = FALSE; + for (idx = 0; idx < (int)ARRAY_SIZE(channel_modes); idx++) + { + if (!strcmp(sval, channel_modes[idx])) { + found = TRUE; + if (devc->tracking_mode == idx) + break; /* Nothing to do! */ + devc->tracking_mode = idx; + if (devc->model->modelid >= LPS_304) /* No use to set anything in the smaller models. */ + return lps_cmd_ok(sdi->conn, "TRACK%1d", devc->tracking_mode); + } + if (devc->model->modelid <= LPS_303) /* Only first setting possible for smaller models. */ + break; + } + if (!found) { + return SR_ERR_ARG; + } + break; + default: + return SR_ERR_NA; + } + } else { + /* Channel group specified: per-channel options. */ + /* We only ever have one channel per channel group in this driver. */ + ch = cg->channels->data; + ch_idx = ch->index; + + switch (key) { + case SR_CONF_OUTPUT_VOLTAGE_MAX: + dval = g_variant_get_double(data); + if (dval < 0 || dval > devc->model->channels[ch_idx].voltage[1]) + return SR_ERR_ARG; + if (ch_idx == 2) { + if (devc->model->modelid < LPS_304) + return SR_ERR_ARG; + + if (fabs(dval - 5.000) <= 0.001) + dval = 5.0; + else if ((devc->model->modelid >= LPS_305) && (fabs(dval - 3.300) <= 0.001)) + dval = 3.3; + else return SR_ERR_ARG; + } + + devc->channel_status[ch_idx].output_voltage_max = dval; + if (ch_idx == 2) + return lps_cmd_ok(sdi->conn, "VDD%1.0f", trunc(dval)); + else + return lps_cmd_ok(sdi->conn, "VSET%d %05.3f", ch_idx+1, dval); + break; + case SR_CONF_OUTPUT_CURRENT_MAX: + dval = g_variant_get_double(data); + if (dval < 0 || dval > devc->model->channels[ch_idx].current[1]) + return SR_ERR_ARG; + if (ch_idx == 2) /* No current setting for CH3. */ + return SR_ERR_NA; + devc->channel_status[ch_idx].output_current_max = dval; + return lps_cmd_ok(sdi->conn, "ISET%d %05.4f", ch_idx+1, dval); + break; + case SR_CONF_OUTPUT_ENABLED: + bval = g_variant_get_boolean(data); + if (bval == devc->channel_status[ch_idx].output_enabled) /* Nothing to do. */ + break; + devc->channel_status[ch_idx].output_enabled = bval; + if (ch_idx != 2) { /* Channels 1,2 can be set only together. */ + devc->channel_status[ch_idx^1].output_enabled = bval; + return lps_cmd_ok(sdi->conn, "OUT%1d", (int)bval); + } else { /* Channel 3: No command to disable output, set voltage to 0 instead. */ + if (bval) + return lps_cmd_ok(sdi->conn, "VDD%1.0f", devc->channel_status[ch_idx].output_voltage_max); + else + return lps_cmd_ok(sdi->conn, "VDD0"); + } + break; + default: + return SR_ERR_NA; + } } - return ret; + return SR_OK; } static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { - int ret; + struct dev_context *devc; + struct sr_channel *ch; + int ch_idx, i; + GVariant *gvar; + GVariantBuilder gvb; - (void)sdi; (void)data; - (void)cg; - ret = SR_OK; + /* Driver options, no device instance necessary. */ switch (key) { - /* TODO */ + case SR_CONF_SCAN_OPTIONS: + *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, + hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t)); + return SR_OK; + default: + if (sdi == NULL) + return SR_ERR_ARG; + + devc = sdi->priv; + } + + /* Device options, independant from channel groups. */ + if (cg == NULL) { + switch (key) { + case SR_CONF_DEVICE_OPTIONS: + *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, + hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); + return SR_OK; + case SR_CONF_OUTPUT_CHANNEL: + if (devc->model->modelid <= LPS_303) { + /* The 1-channel models. */ + *data = g_variant_new_strv(channel_modes, 1); + } else { + /* The other models support all modes. */ + *data = g_variant_new_strv(channel_modes, ARRAY_SIZE(channel_modes)); + } + return SR_OK; + break; + default: + return SR_ERR_NA; + } + } + + /* Device options, depending on channel groups. */ + ch = cg->channels->data; + ch_idx = ch->index; + switch (key) { + case SR_CONF_DEVICE_OPTIONS: + if ((ch_idx == 0) || (ch_idx == 1)) /* CH1, CH2 */ + *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, + hwcaps_ch12, ARRAY_SIZE(hwcaps_ch12), sizeof(int32_t)); + else /* Must be CH3 */ + *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, + hwcaps_ch3, ARRAY_SIZE(hwcaps_ch3), sizeof(int32_t)); + break; + case SR_CONF_OUTPUT_VOLTAGE_MAX: + g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); + /* Min, max, step. */ + for (i = 0; i < 3; i++) { + gvar = g_variant_new_double(devc->model->channels[ch_idx].voltage[i]); + g_variant_builder_add_value(&gvb, gvar); + } + *data = g_variant_builder_end(&gvb); + break; + case SR_CONF_OUTPUT_CURRENT_MAX: + g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); + /* Min, max, step. */ + for (i = 0; i < 3; i++) { + gvar = g_variant_new_double(devc->model->channels[ch_idx].current[i]); + g_variant_builder_add_value(&gvb, gvar); + } + *data = g_variant_builder_end(&gvb); + break; default: return SR_ERR_NA; } - return ret; + return SR_OK; } static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) { - (void)sdi; - (void)cb_data; + struct dev_context *devc; + struct sr_serial_dev_inst *serial; if (sdi->status != SR_ST_ACTIVE) return SR_ERR_DEV_CLOSED; - /* TODO: configure hardware, reset acquisition state, set up - * callbacks and send header packet. */ + devc = sdi->priv; + + devc->acq_running = TRUE; + + serial = sdi->conn; + serial_source_add(serial, G_IO_IN, 50, motech_lps_30x_receive_data, (void *)sdi); + std_session_send_df_header(cb_data, LOG_PREFIX); + + /* Start timer, if required. */ + if (devc->limit_msec) + g_timer_start(devc->elapsed_msec); + + devc->acq_req = AQ_NONE; + /* Do not start polling device here, the read function will do it in 50 ms. */ return SR_OK; } static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) { - (void)cb_data; + struct dev_context *devc; - if (sdi->status != SR_ST_ACTIVE) - return SR_ERR_DEV_CLOSED; + /* Stop timer, if required. */ + if (sdi && (devc = sdi->priv) && devc->limit_msec) + g_timer_stop(devc->elapsed_msec); - /* TODO: stop acquisition. */ - - return SR_OK; + return std_serial_dev_acquisition_stop(sdi, cb_data, std_serial_dev_close, + sdi->conn, LOG_PREFIX); } SR_PRIV struct sr_dev_driver motech_lps_301_driver_info = { .name = "motech-lps-301", .longname = "Motech LPS-301", .api_version = 1, - .init = init, + .init = init_lps301, .cleanup = cleanup, - .scan = scan, - .dev_list = dev_list, - .dev_clear = dev_clear, + .scan = scan_lps301, + .dev_list = dev_list_lps301, + .dev_clear = dev_clear_lps301, .config_get = config_get, .config_set = config_set, .config_list = config_list, - .dev_open = dev_open, - .dev_close = dev_close, + .dev_open = std_serial_dev_open, + .dev_close = std_serial_dev_close, .dev_acquisition_start = dev_acquisition_start, .dev_acquisition_stop = dev_acquisition_stop, .priv = NULL, diff --git a/hardware/motech-lps-30x/protocol.c b/hardware/motech-lps-30x/protocol.c index 921b8de8..5cffdcce 100644 --- a/hardware/motech-lps-30x/protocol.c +++ b/hardware/motech-lps-30x/protocol.c @@ -2,6 +2,7 @@ * This file is part of the libsigrok project. * * Copyright (C) 2014 Matthias Heidbrink + * Copyright (C) 2014 Bert Vermeulen (code from atten-pps3xxx) * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -17,12 +18,130 @@ * along with this program. If not, see . */ +/** @file + * Motech LPS-30x series power supply driver + * @internal + */ + +#include +#include + #include "protocol.h" +/** Send data packets for current measurements. */ +static void send_data(struct sr_dev_inst *sdi) +{ + struct dev_context *devc; + struct sr_datafeed_packet packet; + struct sr_datafeed_analog analog; + int i; + float data[MAX_CHANNELS]; + + devc = sdi->priv; + packet.type = SR_DF_ANALOG; + packet.payload = &analog; + analog.channels = sdi->channels; + analog.num_samples = 1; + + analog.mq = SR_MQ_VOLTAGE; + analog.unit = SR_UNIT_VOLT; + analog.mqflags = SR_MQFLAG_DC; + analog.data = data; + for (i = 0; i < devc->model->num_channels; i++) + analog.data[i] = devc->channel_status[i].output_voltage_last; /* Value always 3.3 or 5 for channel 3, if present! */ + sr_session_send(sdi, &packet); + + analog.mq = SR_MQ_CURRENT; + analog.unit = SR_UNIT_AMPERE; + analog.mqflags = 0; + analog.data = data; + for (i = 0; i < devc->model->num_channels; i++) + analog.data[i] = devc->channel_status[i].output_current_last; /* Value always 0 for channel 3, if present! */ + sr_session_send(sdi, &packet); + + devc->num_samples++; +} + +/** Process a complete line (without CR/LF) in buf. */ +static void process_line(struct sr_dev_inst *sdi) +{ + struct dev_context *devc; + struct sr_serial_dev_inst *serial; + double dbl; + int auxint; + + devc = sdi->priv; + serial = sdi->conn; + + switch (devc->acq_req_pending) { + case 0: /* Should not happen... */ + break; + case 1: /* Waiting for data reply to request */ + /* Convert numbers */ + switch (devc->acq_req) { + case AQ_U1: case AQ_U2: case AQ_I1: case AQ_I2: + if (sr_atod(devc->buf, &dbl) != SR_OK) { + sr_err("Failed to convert '%s' to double, errno=%d %s", + devc->buf, errno, strerror(errno)); + dbl = 0.0; + } + break; + case AQ_STATUS: + if (sr_atoi(devc->buf, &auxint) != SR_OK) { + sr_err("Failed to convert '%s' to int, errno=%d %s", + devc->buf, errno, strerror(errno)); + auxint = 0; + } + break; + default: + break; + } + + switch (devc->acq_req) { + case AQ_U1: + devc->channel_status[0].output_voltage_last = dbl; + break; + case AQ_I1: + devc->channel_status[0].output_current_last = dbl; + break; + case AQ_U2: + devc->channel_status[1].output_voltage_last = dbl; + break; + case AQ_I2: + devc->channel_status[1].output_current_last = dbl; + break; + case AQ_STATUS: /* Process status and generate data. */ + if (lps_process_status(sdi, auxint) == SR_OK) { + send_data(sdi); + } + break; + default: + break; + } + + devc->acq_req_pending = 2; + break; + case 2: /* Waiting for OK after request */ + if (strcmp(devc->buf, "OK")) { + sr_err("Unexpected reply while waiting for OK: '%s'", devc->buf); + } + devc->acq_req_pending = 0; + break; + } + + devc->buf[0] = '\0'; + devc->buflen = 0; +} + + SR_PRIV int motech_lps_30x_receive_data(int fd, int revents, void *cb_data) { - const struct sr_dev_inst *sdi; + struct sr_dev_inst *sdi; struct dev_context *devc; + struct sr_serial_dev_inst *serial; + // char achar; + int len; + gdouble elapsed_s; (void)fd; @@ -32,8 +151,93 @@ SR_PRIV int motech_lps_30x_receive_data(int fd, int revents, void *cb_data) if (!(devc = sdi->priv)) return TRUE; - if (revents == G_IO_IN) { - /* TODO */ + serial = sdi->conn; + + if (revents == G_IO_IN) { /* Serial data arrived. */ + while (LINELEN_MAX - devc->buflen - 2 > 0) { + len = serial_read(serial, devc->buf + devc->buflen, 1); + if (len < 1) + break; + + /* Eliminate whitespace at beginning of line. */ + if (g_ascii_isspace(devc->buf[0])) { + devc->buf[0] = '\0'; + devc->buflen = 0; + continue; + } + + // achar = *(devc->buf + devc->buflen); + // sr_spew("read 0x%02x/%d/'%c'", achar, achar, achar); + + devc->buflen += len; + devc->buf[devc->buflen] = '\0'; + + /* If line complete, process msg. */ + if ((devc->buflen > 0) && ((devc->buf[devc->buflen-1] == '\r') || devc->buf[devc->buflen-1] == '\n')) { + devc->buflen--; + devc->buf[devc->buflen] = '\0'; + + sr_spew("Line complete: \"%s\"", devc->buf); + process_line(sdi); + } + } + } + + /* If number of samples or time limit reached, stop acquisition. */ + if (devc->limit_samples && (devc->num_samples >= devc->limit_samples)) + sdi->driver->dev_acquisition_stop(sdi, cb_data); + + if (devc->limit_msec) { + elapsed_s = g_timer_elapsed(devc->elapsed_msec, NULL); + if ((elapsed_s * 1000) >= devc->limit_msec) + sdi->driver->dev_acquisition_stop(sdi, cb_data); + } + + /* Request next packet, if required. */ + if ((sdi->status == SR_ST_ACTIVE) && (devc->acq_running)){ + if (devc->acq_req_pending) { + gint64 elapsed_us = g_get_monotonic_time() - devc->req_sent_at; + if (elapsed_us > (REQ_TIMEOUT_MS * 1000)) { + sr_spew("Request timeout: req=%d t=%lldus", (int)devc->acq_req, elapsed_us); + devc->acq_req_pending = 0; + } + } + if (devc->acq_req_pending == 0) { + switch(devc->acq_req) + { + case AQ_NONE: /* Fall through */ + case AQ_STATUS: + devc->acq_req = AQ_U1; + lps_send_req(serial, "VOUT1"); + break; + case AQ_U1: + devc->acq_req = AQ_I1; + lps_send_req(serial, "IOUT1"); + break; + case AQ_I1: + if (devc->model->num_channels == 1) { + devc->acq_req = AQ_STATUS; + lps_send_req(serial, "STATUS"); + } else { + devc->acq_req = AQ_U2; + lps_send_req(serial, "VOUT2"); + } + break; + case AQ_U2: + devc->acq_req = AQ_I2; + lps_send_req(serial, "IOUT2"); + break; + case AQ_I2: + devc->acq_req = AQ_STATUS; + lps_send_req(serial, "STATUS"); + break; + default: + sr_err("Illegal devc->acq_req=%d", devc->acq_req); + return SR_ERR; + } + devc->req_sent_at = g_get_real_time(); + devc->acq_req_pending = 1; + } } return TRUE; diff --git a/hardware/motech-lps-30x/protocol.h b/hardware/motech-lps-30x/protocol.h index 7890654c..76f646d8 100644 --- a/hardware/motech-lps-30x/protocol.h +++ b/hardware/motech-lps-30x/protocol.h @@ -2,6 +2,7 @@ * This file is part of the libsigrok project. * * Copyright (C) 2014 Matthias Heidbrink + * Copyright (C) 2014 Bert Vermeulen (code from atten-pps3xxx) * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -17,28 +18,104 @@ * along with this program. If not, see . */ -#ifndef LIBSIGROK_HARDWARE_MOTECH_LPS30X_PROTOCOL_H -#define LIBSIGROK_HARDWARE_MOTECH_LPS30X_PROTOCOL_H +/** @file + * Motech LPS-30x series power supply driver + * @internal + */ + + +#ifndef LIBSIGROK_HARDWARE_MOTECH_LPS_30X_PROTOCOL_H +#define LIBSIGROK_HARDWARE_MOTECH_LPS_30X_PROTOCOL_H #include #include #include "libsigrok.h" #include "libsigrok-internal.h" -#define LOG_PREFIX "motech-lps30x" + +SR_PRIV int lps_process_status(struct sr_dev_inst* sdi, int stat); +SR_PRIV int lps_send_req(struct sr_serial_dev_inst *serial, const char* fmt, ...); + +#define LOG_PREFIX "motech-lps-30x" + +#define LINELEN_MAX 50 /**< Max. line length for requests */ + +#define REQ_TIMEOUT_MS 250 /**< Timeout [ms] for single request. */ + +#define MAX_CHANNELS 3 + +typedef enum { + LPS_UNKNOWN = 0,/**< Unknown model (used during detection process) */ + LPS_301, /**< Motech/Amrel LPS-301, 1 output */ + LPS_302, /**< Motech/Amrel LPS-302, 1 output */ + LPS_303, /**< Motech/Amrel LPS-303, 1 output */ + LPS_304, /**< Motech/Amrel LPS-304, 3 outputs */ + LPS_305, /**< Motech/Amrel LPS-305, 3 outputs */ +} lps_modelid; + +/** Channel specification */ +struct channel_spec { + /* Min, max, step. */ + gdouble voltage[3]; + gdouble current[3]; +}; + +/** Model properties specification */ +struct lps_modelspec { + lps_modelid modelid; + const char* modelstr; + uint8_t num_channels; + struct channel_spec channels[3]; +}; + +/** Used to implement a little state machine to query all required values in a row. */ +typedef enum { + AQ_NONE, + AQ_U1, + AQ_I1, + AQ_I2, + AQ_U2, + AQ_STATUS, +} acquisition_req; + +/** Status of a single channel. */ +struct channel_status { + /* Channel information (struct channel_info*). data (struct) owned by sdi, just a reference to address a single channel. */ + GSList* info; + /* Received from device. */ + gdouble output_voltage_last; + gdouble output_current_last; + gboolean output_enabled; /**< Also used when set. */ + gboolean cc_mode; /**< Constant current mode. If false, constant voltage mode. */ + /* Set by frontend. */ + gdouble output_voltage_max; + gdouble output_current_max; +}; /** Private, per-device-instance driver context. */ struct dev_context { /* Model-specific information */ + struct lps_modelspec* model; - /* Acquisition settings */ + /* Acquisition status */ + gboolean acq_running; /**< Aquisition is running. */ + uint64_t limit_samples; /**< Target number of samples */ + uint64_t limit_msec; /**< Target sampling time */ + acquisition_req acq_req; /**< Current request. */ + uint8_t acq_req_pending; /**< Request pending. 0=none, 1=reply, 2=OK */ /* Operational state */ + struct channel_status channel_status[MAX_CHANNELS]; + guint8 tracking_mode; /**< 0=off, 1=Tracking from CH1, 2=Tracking from CH2. */ /* Temporary state across callbacks */ - + int64_t req_sent_at; /**< Request sent. */ + uint64_t num_samples; /**< Current #samples for limit_samples */ + GTimer *elapsed_msec; /**< Used for sampling with limit_msec */ + gchar buf[LINELEN_MAX]; /**< Buffer for read callback */ + int buflen; /**< Data len in buf */ }; -SR_PRIV int motech_lps30x_receive_data(int fd, int revents, void *cb_data); +SR_PRIV int motech_lps_30x_receive_data(int fd, int revents, void *cb_data); #endif