X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fhardware%2Fmanson-hcs-3xxx%2Fapi.c;h=9d3c9238ed35e4d82f57f4bce7e435f85c58a6fc;hb=060df80fb354246cc309ef80927d38e37e922eb2;hp=e79e59dc160ff4d7fddec2b516f005f13391359d;hpb=6e799d0799abb2c8c0ca75238fff902e31453108;p=libsigrok.git
diff --git a/src/hardware/manson-hcs-3xxx/api.c b/src/hardware/manson-hcs-3xxx/api.c
index e79e59dc..9d3c9238 100644
--- a/src/hardware/manson-hcs-3xxx/api.c
+++ b/src/hardware/manson-hcs-3xxx/api.c
@@ -15,36 +15,25 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ * along with this program; if not, see .
*/
-/** @file
- * Manson HCS-3xxx series power supply driver
- * @internal
- */
-
#include
#include "protocol.h"
-static const uint32_t drvopts[] = {
- /* Device class */
- SR_CONF_POWER_SUPPLY,
-};
-
static const uint32_t scanopts[] = {
SR_CONF_CONN,
SR_CONF_SERIALCOMM,
};
-static const uint32_t devopts[] = {
- /* Device class */
+static const uint32_t drvopts[] = {
SR_CONF_POWER_SUPPLY,
- /* Acquisition modes. */
+};
+
+static const uint32_t devopts[] = {
SR_CONF_CONTINUOUS,
SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
- /* Device configuration */
SR_CONF_VOLTAGE | SR_CONF_GET,
SR_CONF_VOLTAGE_TARGET | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
SR_CONF_CURRENT | SR_CONF_GET,
@@ -54,52 +43,52 @@ static const uint32_t devopts[] = {
/* Note: All models have one power supply output only. */
static const struct hcs_model models[] = {
- { MANSON_HCS_3100, "HCS-3100", "3100", { 1, 18, 0.1 }, { 0, 10, 0.10 } },
- { MANSON_HCS_3102, "HCS-3102", "3102", { 1, 36, 0.1 }, { 0, 5, 0.01 } },
- { MANSON_HCS_3104, "HCS-3104", "3104", { 1, 60, 0.1 }, { 0, 2.5, 0.01 } },
- { MANSON_HCS_3150, "HCS-3150", "3150", { 1, 18, 0.1 }, { 0, 15, 0.10 } },
- { MANSON_HCS_3200, "HCS-3200", "3200", { 1, 18, 0.1 }, { 0, 20, 0.10 } },
- { MANSON_HCS_3202, "HCS-3202", "3202", { 1, 36, 0.1 }, { 0, 10, 0.10 } },
- { MANSON_HCS_3204, "HCS-3204", "3204", { 1, 60, 0.1 }, { 0, 5, 0.01 } },
- { MANSON_HCS_3300, "HCS-3300-USB", "3300", { 1, 16, 0.1 }, { 0, 30, 0.10 } },
- { MANSON_HCS_3302, "HCS-3302-USB", "3302", { 1, 32, 0.1 }, { 0, 15, 0.10 } },
- { MANSON_HCS_3304, "HCS-3304-USB", "3304", { 1, 60, 0.1 }, { 0, 8, 0.10 } },
- { MANSON_HCS_3400, "HCS-3400-USB", "3400", { 1, 16, 0.1 }, { 0, 40, 0.10 } },
- { MANSON_HCS_3402, "HCS-3402-USB", "3402", { 1, 32, 0.1 }, { 0, 20, 0.10 } },
- { MANSON_HCS_3404, "HCS-3404-USB", "3404", { 1, 60, 0.1 }, { 0, 10, 0.10 } },
- { MANSON_HCS_3600, "HCS-3600-USB", "3600", { 1, 16, 0.1 }, { 0, 60, 0.10 } },
- { MANSON_HCS_3602, "HCS-3602-USB", "3602", { 1, 32, 0.1 }, { 0, 30, 0.10 } },
- { MANSON_HCS_3604, "HCS-3604-USB", "3604", { 1, 60, 0.1 }, { 0, 15, 0.10 } },
- { 0, NULL, NULL, { 0, 0, 0 }, { 0, 0, 0 }, },
+ { MANSON_HCS_3100, "HCS-3100", "3100", { 1, 18, 0.1 }, { 0, 10, 0.10 } },
+ { MANSON_HCS_3100, "HCS-3100", "HCS-3100", { 1, 18, 0.1 }, { 0, 10, 0.10 } },
+ { MANSON_HCS_3102, "HCS-3102", "3102", { 1, 36, 0.1 }, { 0, 5, 0.01 } },
+ { MANSON_HCS_3102, "HCS-3102", "HCS-3102", { 1, 36, 0.1 }, { 0, 5, 0.01 } },
+ { MANSON_HCS_3104, "HCS-3104", "3104", { 1, 60, 0.1 }, { 0, 2.5, 0.01 } },
+ { MANSON_HCS_3104, "HCS-3104", "HCS-3104", { 1, 60, 0.1 }, { 0, 2.5, 0.01 } },
+ { MANSON_HCS_3150, "HCS-3150", "3150", { 1, 18, 0.1 }, { 0, 15, 0.10 } },
+ { MANSON_HCS_3150, "HCS-3150", "HCS-3150", { 1, 18, 0.1 }, { 0, 15, 0.10 } },
+ { MANSON_HCS_3200, "HCS-3200", "3200", { 1, 18, 0.1 }, { 0, 20, 0.10 } },
+ { MANSON_HCS_3200, "HCS-3200", "HCS-3200", { 1, 18, 0.1 }, { 0, 20, 0.10 } },
+ { MANSON_HCS_3202, "HCS-3202", "3202", { 1, 36, 0.1 }, { 0, 10, 0.10 } },
+ { MANSON_HCS_3202, "HCS-3202", "HCS-3202", { 1, 36, 0.1 }, { 0, 10, 0.10 } },
+ { MANSON_HCS_3204, "HCS-3204", "3204", { 1, 60, 0.1 }, { 0, 5, 0.01 } },
+ { MANSON_HCS_3204, "HCS-3204", "HCS-3204", { 1, 60, 0.1 }, { 0, 5, 0.01 } },
+ { MANSON_HCS_3300, "HCS-3300-USB", "3300", { 1, 16, 0.1 }, { 0, 30, 0.10 } },
+ { MANSON_HCS_3300, "HCS-3300-USB", "HCS-3300", { 1, 16, 0.1 }, { 0, 30, 0.10 } },
+ { MANSON_HCS_3302, "HCS-3302-USB", "3302", { 1, 32, 0.1 }, { 0, 15, 0.10 } },
+ { MANSON_HCS_3302, "HCS-3302-USB", "HCS-3302", { 1, 32, 0.1 }, { 0, 15, 0.10 } },
+ { MANSON_HCS_3304, "HCS-3304-USB", "3304", { 1, 60, 0.1 }, { 0, 8, 0.10 } },
+ { MANSON_HCS_3304, "HCS-3304-USB", "HCS-3304", { 1, 60, 0.1 }, { 0, 8, 0.10 } },
+ { MANSON_HCS_3400, "HCS-3400-USB", "3400", { 1, 16, 0.1 }, { 0, 40, 0.10 } },
+ { MANSON_HCS_3400, "HCS-3400-USB", "HCS-3400", { 1, 16, 0.1 }, { 0, 40, 0.10 } },
+ { MANSON_HCS_3402, "HCS-3402-USB", "3402", { 1, 32, 0.1 }, { 0, 20, 0.10 } },
+ { MANSON_HCS_3402, "HCS-3402-USB", "HCS-3402", { 1, 32, 0.1 }, { 0, 20, 0.10 } },
+ { MANSON_HCS_3404, "HCS-3404-USB", "3404", { 1, 60, 0.1 }, { 0, 10, 0.10 } },
+ { MANSON_HCS_3404, "HCS-3404-USB", "HCS-3404", { 1, 60, 0.1 }, { 0, 10, 0.10 } },
+ { MANSON_HCS_3600, "HCS-3600-USB", "3600", { 1, 16, 0.1 }, { 0, 60, 0.10 } },
+ { MANSON_HCS_3600, "HCS-3600-USB", "HCS-3600", { 1, 16, 0.1 }, { 0, 60, 0.10 } },
+ { MANSON_HCS_3602, "HCS-3602-USB", "3602", { 1, 32, 0.1 }, { 0, 30, 0.10 } },
+ { MANSON_HCS_3602, "HCS-3602-USB", "HCS-3602", { 1, 32, 0.1 }, { 0, 30, 0.10 } },
+ { MANSON_HCS_3604, "HCS-3604-USB", "3604", { 1, 60, 0.1 }, { 0, 15, 0.10 } },
+ { MANSON_HCS_3604, "HCS-3604-USB", "HCS-3604", { 1, 60, 0.1 }, { 0, 15, 0.10 } },
+ ALL_ZERO
};
-SR_PRIV struct sr_dev_driver manson_hcs_3xxx_driver_info;
-
-static int dev_clear(const struct sr_dev_driver *di)
-{
- return std_dev_clear(di, NULL);
-}
-
-static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
-{
- return std_init(sr_ctx, di, LOG_PREFIX);
-}
-
static GSList *scan(struct sr_dev_driver *di, GSList *options)
{
int i, model_id;
- struct drv_context *drvc;
struct dev_context *devc;
struct sr_dev_inst *sdi;
struct sr_config *src;
- GSList *devices, *l;
+ GSList *l;
const char *conn, *serialcomm;
struct sr_serial_dev_inst *serial;
char reply[50], **tokens, *dummy;
- drvc = di->context;
- drvc->instances = NULL;
- devices = NULL;
conn = NULL;
serialcomm = NULL;
devc = NULL;
@@ -129,8 +118,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return NULL;
- serial_flush(serial);
-
sr_info("Probing serial port %s.", conn);
/* Get the device model. */
@@ -152,18 +139,17 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
}
g_strfreev(tokens);
- /* Init device instance, etc. */
sdi = g_malloc0(sizeof(struct sr_dev_inst));
sdi->status = SR_ST_INACTIVE;
sdi->vendor = g_strdup("Manson");
sdi->model = g_strdup(models[model_id].name);
sdi->inst_type = SR_INST_SERIAL;
sdi->conn = serial;
- sdi->driver = di;
sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "CH1");
devc = g_malloc0(sizeof(struct dev_context));
+ sr_sw_limits_init(&devc->limits);
devc->model = &models[model_id];
sdi->priv = devc;
@@ -189,14 +175,9 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
devc->voltage_max_device = g_strtod(tokens[0], &dummy) * devc->model->voltage[2];
g_strfreev(tokens);
- drvc->instances = g_slist_append(drvc->instances, sdi);
- devices = g_slist_append(devices, sdi);
-
serial_close(serial);
- if (!devices)
- sr_serial_dev_inst_free(serial);
- return devices;
+ return std_scan_complete(di, g_slist_append(NULL, sdi));
exit_err:
sr_dev_inst_free(sdi);
@@ -205,18 +186,8 @@ exit_err:
return NULL;
}
-static GSList *dev_list(const struct sr_dev_driver *di)
-{
- return ((struct drv_context *)(di->context))->instances;
-}
-
-static int cleanup(const struct sr_dev_driver *di)
-{
- return dev_clear(di);
-}
-
-static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
- const struct sr_channel_group *cg)
+static int config_get(uint32_t key, GVariant **data,
+ const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
{
struct dev_context *devc;
@@ -229,11 +200,8 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s
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;
+ return sr_sw_limits_config_get(&devc->limits, key, data);
case SR_CONF_VOLTAGE:
*data = g_variant_new_double(devc->voltage);
break;
@@ -256,8 +224,8 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s
return SR_OK;
}
-static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
- const struct sr_channel_group *cg)
+static int config_set(uint32_t key, GVariant *data,
+ const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
{
struct dev_context *devc;
gboolean bval;
@@ -265,22 +233,12 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd
(void)cg;
- if (sdi->status != SR_ST_ACTIVE)
- return SR_ERR_DEV_CLOSED;
-
devc = sdi->priv;
switch (key) {
case SR_CONF_LIMIT_MSEC:
- if (g_variant_get_uint64(data) == 0)
- return SR_ERR_ARG;
- devc->limit_msec = g_variant_get_uint64(data);
- break;
case SR_CONF_LIMIT_SAMPLES:
- if (g_variant_get_uint64(data) == 0)
- return SR_ERR_ARG;
- devc->limit_samples = g_variant_get_uint64(data);
- break;
+ return sr_sw_limits_config_set(&devc->limits, key, data);
case SR_CONF_VOLTAGE_TARGET:
dval = g_variant_get_double(data);
if (dval < devc->model->voltage[0] || dval > devc->voltage_max_device)
@@ -305,11 +263,15 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd
break;
case SR_CONF_ENABLED:
bval = g_variant_get_boolean(data);
- if (bval == devc->output_enabled) /* Nothing to do. */
- break;
- if ((hcs_send_cmd(sdi->conn, "SOUT%1d\r", !bval) < 0) ||
- (hcs_read_reply(sdi->conn, 1, devc->buf, sizeof(devc->buf)) < 0))
+
+ if (hcs_send_cmd(sdi->conn, "SOUT%1d\r", !bval) < 0) {
+ sr_err("Could not send SR_CONF_ENABLED command.");
return SR_ERR;
+ }
+ if (hcs_read_reply(sdi->conn, 1, devc->buf, sizeof(devc->buf)) < 0) {
+ sr_err("Could not read SR_CONF_ENABLED reply.");
+ return SR_ERR;
+ }
devc->output_enabled = bval;
break;
default:
@@ -319,66 +281,29 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd
return SR_OK;
}
-static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
- const struct sr_channel_group *cg)
+static int config_list(uint32_t key, GVariant **data,
+ const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
{
+ const double *a;
struct dev_context *devc;
- GVariant *gvar;
- GVariantBuilder gvb;
- double dval;
- int idx;
-
- (void)cg;
-
- /* Always available (with or without sdi). */
- if (key == SR_CONF_SCAN_OPTIONS) {
- *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
- scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
- return SR_OK;
- }
- /* Return drvopts without sdi (and devopts with sdi, see below). */
- if (key == SR_CONF_DEVICE_OPTIONS && !sdi) {
- *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
- drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
- return SR_OK;
- }
-
- /* Every other key needs an sdi. */
- if (!sdi)
- return SR_ERR_ARG;
- devc = sdi->priv;
+ devc = (sdi) ? sdi->priv : NULL;
switch (key) {
+ case SR_CONF_SCAN_OPTIONS:
case SR_CONF_DEVICE_OPTIONS:
- *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
- devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
- break;
+ return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
case SR_CONF_VOLTAGE_TARGET:
- g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
- /* Min, max, step. */
- for (idx = 0; idx < 3; idx++) {
- if (idx == 1)
- dval = devc->voltage_max_device;
- else
- dval = devc->model->voltage[idx];
- gvar = g_variant_new_double(dval);
- g_variant_builder_add_value(&gvb, gvar);
- }
- *data = g_variant_builder_end(&gvb);
+ if (!devc || !devc->model)
+ return SR_ERR_ARG;
+ a = devc->model->voltage;
+ *data = std_gvar_min_max_step(a[0], devc->voltage_max_device, a[2]);
break;
case SR_CONF_CURRENT_LIMIT:
- g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
- /* Min, max, step. */
- for (idx = 0; idx < 3; idx++) {
- if (idx == 1)
- dval = devc->current_max_device;
- else
- dval = devc->model->current[idx];
- gvar = g_variant_new_double(dval);
- g_variant_builder_add_value(&gvb, gvar);
- }
- *data = g_variant_builder_end(&gvb);
+ if (!devc || !devc->model)
+ return SR_ERR_ARG;
+ a = devc->model->current;
+ *data = std_gvar_min_max_step(a[0], devc->current_max_device, a[2]);
break;
default:
return SR_ERR_NA;
@@ -387,26 +312,19 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *
return SR_OK;
}
-static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
+static int dev_acquisition_start(const struct sr_dev_inst *sdi)
{
struct dev_context *devc;
struct sr_serial_dev_inst *serial;
- if (sdi->status != SR_ST_ACTIVE)
- return SR_ERR_DEV_CLOSED;
-
devc = sdi->priv;
- devc->cb_data = cb_data;
- /* Send header packet to the session bus. */
- std_session_send_df_header(cb_data, LOG_PREFIX);
+ sr_sw_limits_acquisition_start(&devc->limits);
+ std_session_send_df_header(sdi);
- devc->starttime = g_get_monotonic_time();
- devc->num_samples = 0;
devc->reply_pending = FALSE;
devc->req_sent_at = 0;
- /* Poll every 10ms, or whenever some data comes in. */
serial = sdi->conn;
serial_source_add(sdi->session, serial, G_IO_IN, 10,
hcs_receive_data, (void *)sdi);
@@ -414,27 +332,22 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
return SR_OK;
}
-static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
-{
- return std_serial_dev_acquisition_stop(sdi, cb_data,
- std_serial_dev_close, sdi->conn, LOG_PREFIX);
-}
-
-SR_PRIV struct sr_dev_driver manson_hcs_3xxx_driver_info = {
+static struct sr_dev_driver manson_hcs_3xxx_driver_info = {
.name = "manson-hcs-3xxx",
.longname = "Manson HCS-3xxx",
.api_version = 1,
- .init = init,
- .cleanup = cleanup,
+ .init = std_init,
+ .cleanup = std_cleanup,
.scan = scan,
- .dev_list = dev_list,
- .dev_clear = dev_clear,
+ .dev_list = std_dev_list,
+ .dev_clear = std_dev_clear,
.config_get = config_get,
.config_set = config_set,
.config_list = config_list,
.dev_open = std_serial_dev_open,
.dev_close = std_serial_dev_close,
.dev_acquisition_start = dev_acquisition_start,
- .dev_acquisition_stop = dev_acquisition_stop,
+ .dev_acquisition_stop = std_serial_dev_acquisition_stop,
.context = NULL,
};
+SR_REGISTER_DEV_DRIVER(manson_hcs_3xxx_driver_info);