From: Timo Boettcher Date: Fri, 27 Sep 2024 22:20:32 +0000 (+0200) Subject: siglent-sdl10x0: Implement Siglent SDL10x0 electronic load driver X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=cd468e3776a108a7e62dbbf79016a3aecb1de516;p=libsigrok.git siglent-sdl10x0: Implement Siglent SDL10x0 electronic load driver --- diff --git a/src/hardware/siglent-sdl10x0/api.c b/src/hardware/siglent-sdl10x0/api.c index 24f85ce7..d45da520 100644 --- a/src/hardware/siglent-sdl10x0/api.c +++ b/src/hardware/siglent-sdl10x0/api.c @@ -18,57 +18,255 @@ */ #include +#include "scpi.h" #include "protocol.h" +static const char *manufacturers[] = { + "Siglent Technologies", +}; + +static const char *models[] = { + "SDL1020X-E", + "SDL1020X", + "SDL1030X-E", + "SDL1030X", +}; + +static const uint32_t scanopts[] = { + SR_CONF_CONN, +}; + +static const uint32_t drvopts[] = { + SR_CONF_ELECTRONIC_LOAD, +}; + +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, +}; + +static const uint32_t devopts_cg[] = { + SR_CONF_ENABLED | SR_CONF_GET | SR_CONF_SET, + SR_CONF_REGULATION | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, + 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, + SR_CONF_CURRENT_LIMIT | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, + SR_CONF_POWER | SR_CONF_GET, + SR_CONF_POWER_TARGET | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, + SR_CONF_RESISTANCE | SR_CONF_GET, + SR_CONF_RESISTANCE_TARGET | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, + SR_CONF_OVER_POWER_PROTECTION_ENABLED | SR_CONF_GET, + SR_CONF_OVER_POWER_PROTECTION_ACTIVE | SR_CONF_GET, + SR_CONF_OVER_POWER_PROTECTION_THRESHOLD | SR_CONF_GET | SR_CONF_SET, + SR_CONF_OVER_CURRENT_PROTECTION_ENABLED | SR_CONF_GET, + SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE | SR_CONF_GET, + SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD | SR_CONF_GET | SR_CONF_SET, +}; + +static const char *regulation[] = { + "CURRENT", + "VOLTAGE", + "POWER", + "RESISTANCE" +}; + static struct sr_dev_driver siglent_sdl10x0_driver_info; -static GSList *scan(struct sr_dev_driver *di, GSList *options) +static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi) { - struct drv_context *drvc; - GSList *devices; + struct sr_dev_inst *sdi; + struct sr_channel_group *cg; + struct sr_channel *ch; + struct dev_context *devc; + struct sr_scpi_hw_info *hw_info; + + sdi = NULL; + devc = NULL; + hw_info = NULL; + + if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) { + sr_info("Couldn't get IDN response."); + goto fail; + } - (void)options; + if (std_str_idx_s(hw_info->manufacturer, ARRAY_AND_SIZE(manufacturers)) < 0) + goto fail; + + if (std_str_idx_s(hw_info->model, ARRAY_AND_SIZE(models)) < 0) + goto fail; + + sdi = g_malloc0(sizeof(struct sr_dev_inst)); + sdi->vendor = g_strdup(hw_info->manufacturer); + sdi->model = g_strdup(hw_info->model); + sdi->version = g_strdup(hw_info->firmware_version); + sdi->serial_num = g_strdup(hw_info->serial_number); + sdi->driver = &siglent_sdl10x0_driver_info; + sdi->inst_type = SR_INST_SCPI; + sdi->conn = scpi; + sdi->channels = NULL; + sdi->channel_groups = NULL; + + cg = sr_channel_group_new(sdi, "1", NULL); + + ch = sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "1"); + cg->channels = g_slist_append(cg->channels, ch); + + sr_scpi_hw_info_free(hw_info); + hw_info = NULL; + + devc = g_malloc0(sizeof(struct dev_context)); + sdi->priv = devc; + + /* + * modelname 'SDL1020X-E': + * 6th character indicates wattage: + * 2 => 200 + * 3 => 300 + */ + devc->maxpower = 200.0; + if (g_ascii_strncasecmp(sdi->model, "SDL1030", strlen("SDL1030")) == 0) { + devc->maxpower = 300.0; + } - devices = NULL; - drvc = di->context; - drvc->instances = NULL; + return sdi; - /* TODO: scan for devices, either based on a SR_CONF_CONN option - * or on a USB scan. */ +fail: + sr_scpi_hw_info_free(hw_info); + sr_dev_inst_free(sdi); + g_free(devc); - return devices; + return NULL; } -static int dev_open(struct sr_dev_inst *sdi) +static GSList *scan(struct sr_dev_driver *di, GSList *options) { - (void)sdi; - - /* TODO: get handle from sdi->conn and open it. */ + return sr_scpi_scan(di->context, options, probe_device); +} - return SR_OK; +static int dev_open(struct sr_dev_inst *sdi) +{ + return sr_scpi_open(sdi->conn); } static int dev_close(struct sr_dev_inst *sdi) { - (void)sdi; - - /* TODO: get handle from sdi->conn and close it. */ - - return SR_OK; + return sr_scpi_close(sdi->conn); } static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { int ret; + float ival; + gboolean bval; + char *mode; - (void)sdi; - (void)data; (void)cg; + struct dev_context *devc; + + devc = sdi->priv; + if (!devc) + return SR_ERR; ret = SR_OK; switch (key) { - /* TODO */ + case SR_CONF_LIMIT_SAMPLES: + case SR_CONF_LIMIT_MSEC: + return sr_sw_limits_config_get(&devc->limits, key, data); + case SR_CONF_ENABLED: + ret = sr_scpi_get_bool(sdi->conn, ":INPUT?", &bval); + if (ret != SR_OK) + return SR_ERR; + *data = g_variant_new_boolean(bval); + break; + case SR_CONF_REGULATION: + ret = sr_scpi_get_string(sdi->conn, ":FUNC?", &mode); + if (ret != SR_OK) + return SR_ERR; + *data = g_variant_new_string(mode); + break; + case SR_CONF_VOLTAGE: + ret = sr_scpi_get_float(sdi->conn, "MEAS:VOLTage?", &ival); + if (ret != SR_OK) + return SR_ERR; + *data = g_variant_new_double((double)ival); + break; + case SR_CONF_VOLTAGE_TARGET: + ret = sr_scpi_get_float(sdi->conn, ":VOLTage:LEVel?", &ival); + if (ret != SR_OK) + return SR_ERR; + *data = g_variant_new_double((double)ival); + break; + case SR_CONF_CURRENT: + ret = sr_scpi_get_float(sdi->conn, "MEAS:CURRent?", &ival); + if (ret != SR_OK) + return SR_ERR; + *data = g_variant_new_double((double)ival); + break; + case SR_CONF_CURRENT_LIMIT: + ret = sr_scpi_get_float(sdi->conn, ":CURRENT:LEVel?", &ival); + if (ret != SR_OK) + return SR_ERR; + *data = g_variant_new_double((double)ival); + break; + case SR_CONF_POWER: + ret = sr_scpi_get_float(sdi->conn, "MEAS:POWer?", &ival); + if (ret != SR_OK) + return SR_ERR; + *data = g_variant_new_double((double)ival); + break; + case SR_CONF_POWER_TARGET: + ret = sr_scpi_get_float(sdi->conn, ":POWer:LEVel?", &ival); + if (ret != SR_OK) + return SR_ERR; + *data = g_variant_new_double((double)ival); + break; + case SR_CONF_RESISTANCE: + ret = sr_scpi_get_float(sdi->conn, "MEAS:RESistance?", &ival); + if (ret != SR_OK) + return SR_ERR; + *data = g_variant_new_double((double)ival); + break; + case SR_CONF_RESISTANCE_TARGET: + ret = sr_scpi_get_float(sdi->conn, ":RESistance:LEVel?", &ival); + if (ret != SR_OK) + return SR_ERR; + *data = g_variant_new_double((double)ival); + break; + case SR_CONF_OVER_POWER_PROTECTION_ENABLED: + /* Always true */ + *data = g_variant_new_boolean(TRUE); + break; + case SR_CONF_OVER_POWER_PROTECTION_ACTIVE: + ret = sr_scpi_get_bool(sdi->conn, ":POWer:PROTection:STATe?", &bval); + if (ret != SR_OK) + return SR_ERR; + *data = g_variant_new_boolean(bval); + break; + case SR_CONF_OVER_POWER_PROTECTION_THRESHOLD: + ret = sr_scpi_get_float(sdi->conn, ":POWer:PROTection:LEVel?", &ival); + if (ret != SR_OK) + return SR_ERR; + *data = g_variant_new_double((double)ival); + break; + case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: + /* Always true */ + *data = g_variant_new_boolean(TRUE); + break; + case SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE: + ret = sr_scpi_get_bool(sdi->conn, ":CURRent:PROTection:STATe?", &bval); + if (ret != SR_OK) + return SR_ERR; + *data = g_variant_new_boolean(bval); + break; + case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD: + ret = sr_scpi_get_bool(sdi->conn, ":CURRent:PROTection:LEVel?", &bval); + if (ret != SR_OK) + return SR_ERR; + *data = g_variant_new_double((double)ival); + break; default: return SR_ERR_NA; } @@ -80,14 +278,76 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { int ret; + double ivalue; + gboolean ena; + char command[64]; + const char *mode_str; + enum siglent_sdl10x0_modes mode; - (void)sdi; - (void)data; (void)cg; + struct dev_context *devc; + + devc = sdi->priv; + if (!devc) + return SR_ERR; ret = SR_OK; switch (key) { - /* TODO */ + case SR_CONF_LIMIT_SAMPLES: + case SR_CONF_LIMIT_MSEC: + return sr_sw_limits_config_set(&devc->limits, key, data); + case SR_CONF_ENABLED: + ena = g_variant_get_boolean(data); + g_snprintf(command, sizeof(command), ":INPUT %s", ena ? "ON" : "OFF"); + sr_spew("Sending '%s'.", command); + ret = sr_scpi_send(sdi->conn, command); + break; + case SR_CONF_REGULATION: + mode_str = g_variant_get_string(data, NULL); + if (siglent_sdl10x0_string_to_mode(mode_str, &mode) != SR_OK) { + ret = SR_ERR_ARG; + break; + } + g_snprintf(command, sizeof(command), ":FUNC %s", siglent_sdl10x0_mode_to_longstring(mode)); + sr_spew("Sending '%s'.", command); + ret = sr_scpi_send(sdi->conn, command); + break; + case SR_CONF_VOLTAGE_TARGET: + ivalue = g_variant_get_double(data); + g_snprintf(command, sizeof(command), ":VOLT:LEV:IMM %.3f", (ivalue)); + sr_spew("Sending '%s'.", command); + ret = sr_scpi_send(sdi->conn, command); + break; + case SR_CONF_CURRENT_LIMIT: + ivalue = g_variant_get_double(data); + g_snprintf(command, sizeof(command), ":CURR:LEV:IMM %.3f", (ivalue)); + sr_spew("Sending '%s'.", command); + ret = sr_scpi_send(sdi->conn, command); + break; + case SR_CONF_POWER_TARGET: + ivalue = g_variant_get_double(data); + g_snprintf(command, sizeof(command), ":POW:LEV:IMM %.3f", (ivalue)); + sr_spew("Sending '%s'.", command); + ret = sr_scpi_send(sdi->conn, command); + break; + case SR_CONF_RESISTANCE_TARGET: + ivalue = g_variant_get_double(data); + g_snprintf(command, sizeof(command), ":RES:LEV:IMM %.3f", (ivalue)); + sr_spew("Sending '%s'.", command); + ret = sr_scpi_send(sdi->conn, command); + break; + case SR_CONF_OVER_POWER_PROTECTION_THRESHOLD: + ivalue = g_variant_get_double(data); + g_snprintf(command, sizeof(command), ":POW:PROT:LEV %.3f", (ivalue)); + sr_spew("Sending '%s'.", command); + ret = sr_scpi_send(sdi->conn, command); + break; + case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD: + ivalue = g_variant_get_double(data); + g_snprintf(command, sizeof(command), ":CURR:PROT:LEV %.3f", (ivalue)); + sr_spew("Sending '%s'.", command); + ret = sr_scpi_send(sdi->conn, command); + break; default: ret = SR_ERR_NA; } @@ -98,37 +358,82 @@ static int config_set(uint32_t key, GVariant *data, static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { - int ret; - (void)sdi; - (void)data; - (void)cg; - - ret = SR_OK; - switch (key) { - /* TODO */ - default: - return SR_ERR_NA; + struct dev_context *devc; + + devc = sdi ? sdi->priv : NULL; + + if (!cg) { + return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); + } else { + switch (key) { + case SR_CONF_DEVICE_OPTIONS: + *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg)); + break; + case SR_CONF_REGULATION: + *data = std_gvar_array_str(ARRAY_AND_SIZE(regulation)); + break; + case SR_CONF_VOLTAGE_TARGET: + *data = std_gvar_min_max_step(0.0, 150.0, 0.001); + break; + case SR_CONF_CURRENT_LIMIT: + *data = std_gvar_min_max_step(0.0, 30.0, 0.001); + break; + case SR_CONF_POWER_TARGET: + if (!devc) { + *data = std_gvar_min_max_step(0.0, 200.0, 0.001); + } else { + *data = std_gvar_min_max_step(0.0, devc->maxpower, 0.001); + } + break; + case SR_CONF_RESISTANCE_TARGET: + *data = std_gvar_min_max_step(0.03, 10000.0, 0.01); + break; + case SR_CONF_OVER_POWER_PROTECTION_THRESHOLD: + if (!devc) { + *data = std_gvar_min_max_step(0.0, 200.0, 0.001); + } else { + *data = std_gvar_min_max_step(0.0, devc->maxpower, 0.001); + } + break; + case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD: + *data = std_gvar_min_max_step(0.0, 30.0, 0.001); + break; + default: + return SR_ERR_NA; + } } - - return ret; + return SR_OK; } static int dev_acquisition_start(const struct sr_dev_inst *sdi) { - /* TODO: configure hardware, reset acquisition state, set up - * callbacks and send header packet. */ - - (void)sdi; - - return SR_OK; + struct sr_scpi_dev_inst *scpi; + struct dev_context *devc; + + scpi = sdi->conn; + devc = sdi->priv; + + sr_sw_limits_acquisition_start(&devc->limits); + std_session_send_df_header(sdi); + + /* + * Start acquisition. The receive routine will continue + * driving the acquisition. + */ + sr_scpi_send(scpi, "MEAS:VOLT?"); + devc->acq_state = ACQ_REQUESTED_VOLTAGE; + return sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 100, siglent_sdl10x0_handle_events, (void *)sdi); } static int dev_acquisition_stop(struct sr_dev_inst *sdi) { - /* TODO: stop acquisition. */ + struct sr_scpi_dev_inst *scpi; + + scpi = sdi->conn; - (void)sdi; + sr_scpi_source_remove(sdi->session, scpi); + std_session_send_df_end(sdi); return SR_OK; } diff --git a/src/hardware/siglent-sdl10x0/protocol.c b/src/hardware/siglent-sdl10x0/protocol.c index c7565e69..640fe183 100644 --- a/src/hardware/siglent-sdl10x0/protocol.c +++ b/src/hardware/siglent-sdl10x0/protocol.c @@ -18,25 +18,162 @@ */ #include +#include "scpi.h" #include "protocol.h" -SR_PRIV int siglent_sdl10x0_receive_data(int fd, int revents, void *cb_data) +SR_PRIV const char *siglent_sdl10x0_mode_to_string(enum siglent_sdl10x0_modes mode) +{ + switch (mode) { + case CC: + return "CC"; + case CV: + return "CV"; + case CP: + return "CP"; + case CR: + return "CR"; + default: + return "Unknown"; + } +} + +SR_PRIV const char *siglent_sdl10x0_mode_to_longstring(enum siglent_sdl10x0_modes mode) +{ + switch (mode) { + case CC: + return "CURRENT"; + case CV: + return "VOLTAGE"; + case CP: + return "POWER"; + case CR: + return "RESISTANCE"; + default: + return "Unknown"; + } +} + + +SR_PRIV int siglent_sdl10x0_string_to_mode(const char *modename, enum siglent_sdl10x0_modes *mode) +{ + size_t i; + const char *s; + + for (i = 0; i < SDL10x0_MODES; i++) { + s = siglent_sdl10x0_mode_to_string(i); + if (strncmp(modename, s, strlen(s)) == 0) { + *mode = i; + return SR_OK; + } + } + + return SR_ERR; +} + + +SR_PRIV void siglent_sdl10x0_send_value(const struct sr_dev_inst *sdi, float value, enum sr_mq mq, enum sr_mqflag mqflags, enum sr_unit unit, int digits) +{ + struct sr_datafeed_packet packet; + struct sr_datafeed_analog analog; + struct sr_analog_encoding encoding; + struct sr_analog_meaning meaning; + struct sr_analog_spec spec; + + sr_analog_init(&analog, &encoding, &meaning, &spec, digits); + analog.meaning->channels = sdi->channels; + analog.num_samples = 1; + analog.data = &value; + analog.encoding->unitsize = sizeof(value); + analog.encoding->is_float = TRUE; + /* Are we on a little or big endian system? */ +#ifdef WORDS_BIGENDIAN + analog.encoding->is_bigendian = TRUE; +#else + analog.encoding->is_bigendian = FALSE; +#endif + analog.meaning->mq = mq; + analog.meaning->unit = unit; + analog.meaning->mqflags = mqflags; + + packet.type = SR_DF_ANALOG; + packet.payload = &analog; + sr_session_send(sdi, &packet); +} + +/* Gets invoked when RX data is available. */ + +SR_PRIV int siglent_sdl10x0_receive_data(struct sr_dev_inst *sdi) { - const struct sr_dev_inst *sdi; struct dev_context *devc; + int ret; + float ival; + + devc = sdi->priv; + + switch (devc->acq_state) { + case ACQ_REQUESTED_VOLTAGE: + /* Voltage was requested, read result */ + ret = sr_scpi_get_float(sdi->conn, NULL, &ival); + if (ret != SR_OK) + return SR_ERR; + devc->voltage = ival; + + /* Now get next Value: Current */ + sr_scpi_send(sdi->conn, "MEAS:CURR?"); + devc->acq_state = ACQ_REQUESTED_CURRENT; + break; + + case ACQ_REQUESTED_CURRENT: + /* Current was requested, read result */ + ret = sr_scpi_get_float(sdi->conn, NULL, &ival); + if (ret != SR_OK) + return SR_ERR; + devc->current = ival; + + /* All values received, now build a frame */ + std_session_send_df_frame_begin(sdi); + siglent_sdl10x0_send_value(sdi, devc->voltage, SR_MQ_VOLTAGE, SR_MQFLAG_DC, SR_UNIT_VOLT, 7); + siglent_sdl10x0_send_value(sdi, devc->current, SR_MQ_CURRENT, SR_MQFLAG_DC, SR_UNIT_AMPERE, 7); + std_session_send_df_frame_end(sdi); + sr_sw_limits_update_samples_read(&devc->limits, 1); + + /* Now get next Value: Voltage */ + sr_scpi_send(sdi->conn, "MEAS:VOLT?"); + devc->acq_state = ACQ_REQUESTED_VOLTAGE; + break; + + default: + return FALSE; + } + +} + +SR_PRIV int siglent_sdl10x0_handle_events(int fd, int revents, void *cb_data) +{ + struct dev_context *devc; + struct sr_dev_inst *sdi; (void)fd; + (void)revents; sdi = cb_data; if (!sdi) return TRUE; devc = sdi->priv; - if (!devc) + if (!devc) { return TRUE; + } + + if (revents & G_IO_IN) { + siglent_sdl10x0_receive_data(sdi); + } else { + return FALSE; + } - if (revents == G_IO_IN) { - /* TODO */ + if (sr_sw_limits_check(&devc->limits)) { + sr_dev_acquisition_stop(sdi); + return FALSE; } return TRUE; diff --git a/src/hardware/siglent-sdl10x0/protocol.h b/src/hardware/siglent-sdl10x0/protocol.h index fdf4f86b..0282fa48 100644 --- a/src/hardware/siglent-sdl10x0/protocol.h +++ b/src/hardware/siglent-sdl10x0/protocol.h @@ -27,9 +27,43 @@ #define LOG_PREFIX "siglent-sdl10x0" +/* + * Operating modes. + */ +enum siglent_sdl10x0_modes { + CC = 0, + CV = 1, + CP = 2, + CR = 3, + LED = 4, + SDL10x0_MODES, /* Total count, for internal use. */ +}; + +/* + * Possible states in an acquisition. + */ +enum acquisition_state { + ACQ_REQUESTED_VOLTAGE, + ACQ_REQUESTED_CURRENT, + ACQ_REQUESTED_POWER, + ACQ_REQUESTED_RESISTANCE, +}; + struct dev_context { + struct sr_sw_limits limits; + enum acquisition_state acq_state; + float voltage; + float current; + double maxpower; }; -SR_PRIV int siglent_sdl10x0_receive_data(int fd, int revents, void *cb_data); +SR_PRIV const char *siglent_sdl10x0_mode_to_string(enum siglent_sdl10x0_modes mode); +SR_PRIV const char *siglent_sdl10x0_mode_to_longstring(enum siglent_sdl10x0_modes mode); +SR_PRIV int siglent_sdl10x0_string_to_mode(const char *modename, enum siglent_sdl10x0_modes *mode); + +SR_PRIV void siglent_sdl10x0_send_value(const struct sr_dev_inst *sdi, float value, enum sr_mq mq, enum sr_mqflag mqflags, enum sr_unit unit, int digits); + +SR_PRIV int siglent_sdl10x0_receive_data(struct sr_dev_inst *sdi); +SR_PRIV int siglent_sdl10x0_handle_events(int fd, int revents, void *cb_data); #endif