#include <stdlib.h>
#include "protocol.h"
+SR_PRIV struct sr_dev_driver yokogawa_dlm_driver_info;
+static struct sr_dev_driver *di = &yokogawa_dlm_driver_info;
+
+static char *MANUFACTURER_ID = "YOKOGAWA";
+static char *MANUFACTURER_NAME = "Yokogawa";
+
+enum {
+ CG_INVALID = -1,
+ CG_NONE,
+ CG_ANALOG,
+ CG_DIGITAL,
+};
+
+static int init(struct sr_context *sr_ctx)
+{
+ return std_init(sr_ctx, di, LOG_PREFIX);
+}
+
+static struct sr_dev_inst *probe_usbtmc_device(struct sr_scpi_dev_inst *scpi)
+{
+ struct sr_dev_inst *sdi;
+ struct dev_context *devc;
+ struct sr_scpi_hw_info *hw_info;
+ char *model_name;
+ int model_index;
+
+ 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;
+ }
+
+ if (strcmp(hw_info->manufacturer, MANUFACTURER_ID) != 0)
+ goto fail;
+
+ if (dlm_model_get(hw_info->model, &model_name, &model_index) != SR_OK)
+ goto fail;
+
+ if (!(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, MANUFACTURER_NAME,
+ model_name, NULL)))
+ goto fail;
+
+ sr_scpi_hw_info_free(hw_info);
+ hw_info = NULL;
+
+ if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
+ goto fail;
+
+ sdi->driver = di;
+ sdi->priv = devc;
+ sdi->inst_type = SR_INST_SCPI;
+ sdi->conn = scpi;
+
+ if (dlm_device_init(sdi, model_index) != SR_OK)
+ goto fail;
+
+ sr_scpi_close(sdi->conn);
+
+ sdi->status = SR_ST_INACTIVE;
+ return sdi;
+
+fail:
+ if (hw_info)
+ sr_scpi_hw_info_free(hw_info);
+ if (sdi)
+ sr_dev_inst_free(sdi);
+ if (devc)
+ g_free(devc);
+
+ return NULL;
+}
+
+static GSList *scan(GSList *options)
+{
+ return sr_scpi_scan(di->priv, options, probe_usbtmc_device);
+}
+
+static GSList *dev_list(void)
+{
+ return ((struct drv_context *)(di->priv))->instances;
+}
+
+static void clear_helper(void *priv)
+{
+ struct dev_context *devc;
+
+ devc = priv;
+
+ dlm_scope_state_destroy(devc->model_state);
+
+ g_free(devc->analog_groups);
+ g_free(devc->digital_groups);
+ g_free(devc);
+}
+
+static int dev_clear(void)
+{
+ return std_dev_clear(di, clear_helper);
+}
+
+static int dev_open(struct sr_dev_inst *sdi)
+{
+ if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK)
+ return SR_ERR;
+
+ if (dlm_scope_state_query(sdi) != SR_OK)
+ return SR_ERR;
+
+ sdi->status = SR_ST_ACTIVE;
+
+ return SR_OK;
+}
+
+static int dev_close(struct sr_dev_inst *sdi)
+{
+ if (sdi->status == SR_ST_INACTIVE)
+ return SR_OK;
+
+ sr_scpi_close(sdi->conn);
+
+ sdi->status = SR_ST_INACTIVE;
+
+ return SR_OK;
+}
+
+static int cleanup(void)
+{
+ dev_clear();
+
+ return SR_OK;
+}
+
+/**
+ * Check which category a given channel group belongs to.
+ *
+ * @param devc Our internal device context.
+ * @param cg The channel group to check.
+ *
+ * @retval CG_NONE cg is NULL
+ * @retval CG_ANALOG cg is an analog group
+ * @retval CG_DIGITAL cg is a digital group
+ * @retval CG_INVALID cg is something else
+ */
+static int check_channel_group(struct dev_context *devc,
+ const struct sr_channel_group *cg)
+{
+ unsigned int i;
+ struct scope_config *model;
+
+ model = devc->model_config;
+
+ if (!cg)
+ return CG_NONE;
+
+ for (i = 0; i < model->analog_channels; ++i)
+ if (cg == devc->analog_groups[i])
+ return CG_ANALOG;
+
+ for (i = 0; i < model->pods; ++i)
+ if (cg == devc->digital_groups[i])
+ return CG_DIGITAL;
+
+ sr_err("Invalid channel group specified.");
+ return CG_INVALID;
+}
+
+static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
+ const struct sr_channel_group *cg)
+{
+ int ret, cg_type;
+ unsigned int i;
+ struct dev_context *devc;
+ struct scope_config *model;
+ struct scope_state *state;
+
+ if (!sdi || !(devc = sdi->priv))
+ return SR_ERR_ARG;
+
+ if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
+ return SR_ERR;
+
+ ret = SR_ERR_NA;
+ model = devc->model_config;
+ state = devc->model_state;
+
+ switch (key) {
+ case SR_CONF_NUM_TIMEBASE:
+ *data = g_variant_new_int32(model->num_xdivs);
+ ret = SR_OK;
+ break;
+ case SR_CONF_TIMEBASE:
+ *data = g_variant_new("(tt)",
+ (*model->timebases)[state->timebase][0],
+ (*model->timebases)[state->timebase][1]);
+ ret = SR_OK;
+ break;
+ case SR_CONF_NUM_VDIV:
+ if (cg_type == CG_NONE) {
+ sr_err("No channel group specified.");
+ return SR_ERR_CHANNEL_GROUP;
+ } else if (cg_type == CG_ANALOG) {
+ *data = g_variant_new_int32(model->num_ydivs);
+ ret = SR_OK;
+ break;
+ } else {
+ ret = SR_ERR_NA;
+ }
+ break;
+ case SR_CONF_VDIV:
+ ret = SR_ERR_NA;
+ if (cg_type == CG_NONE) {
+ sr_err("No channel group specified.");
+ return SR_ERR_CHANNEL_GROUP;
+ } else if (cg_type != CG_ANALOG)
+ break;
+
+ for (i = 0; i < model->analog_channels; ++i) {
+ if (cg != devc->analog_groups[i])
+ continue;
+ *data = g_variant_new("(tt)",
+ (*model->vdivs)[state->analog_states[i].vdiv][0],
+ (*model->vdivs)[state->analog_states[i].vdiv][1]);
+ ret = SR_OK;
+ break;
+ }
+ break;
+ case SR_CONF_TRIGGER_SOURCE:
+ *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
+ ret = SR_OK;
+ break;
+ case SR_CONF_TRIGGER_SLOPE:
+ *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
+ ret = SR_OK;
+ break;
+ case SR_CONF_HORIZ_TRIGGERPOS:
+ *data = g_variant_new_double(state->horiz_triggerpos);
+ ret = SR_OK;
+ break;
+ case SR_CONF_COUPLING:
+ ret = SR_ERR_NA;
+ if (cg_type == CG_NONE) {
+ sr_err("No channel group specified.");
+ return SR_ERR_CHANNEL_GROUP;
+ } else if (cg_type != CG_ANALOG)
+ break;
+
+ for (i = 0; i < model->analog_channels; ++i) {
+ if (cg != devc->analog_groups[i])
+ continue;
+ *data = g_variant_new_string((*model->coupling_options)[state->analog_states[i].coupling]);
+ ret = SR_OK;
+ break;
+ }
+ break;
+ case SR_CONF_SAMPLERATE:
+ *data = g_variant_new_uint64(state->sample_rate);
+ ret = SR_OK;
+ break;
+ default:
+ ret = SR_ERR_NA;
+ }
+
+ return ret;
+}
+
+static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
+{
+ unsigned int i;
+ GVariant *rational[2];
+ GVariantBuilder gvb;
+
+ g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
+
+ for (i = 0; i < n; i++) {
+ rational[0] = g_variant_new_uint64((*array)[i][0]);
+ rational[1] = g_variant_new_uint64((*array)[i][1]);
+
+ /* FIXME: Valgrind reports a memory leak here. */
+ g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
+ }
+
+ return g_variant_builder_end(&gvb);
+}
+
+static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
+ const struct sr_channel_group *cg)
+{
+ int ret, cg_type;
+ unsigned int i, j;
+ char float_str[30];
+ struct dev_context *devc;
+ struct scope_config *model;
+ struct scope_state *state;
+ const char *tmp;
+ uint64_t p, q;
+ double tmp_d;
+ gboolean update_sample_rate;
+
+ if (!sdi || !(devc = sdi->priv))
+ return SR_ERR_ARG;
+
+ if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
+ return SR_ERR;
+
+ model = devc->model_config;
+ state = devc->model_state;
+ update_sample_rate = FALSE;
+
+ ret = SR_ERR_NA;
+
+ switch (key) {
+ case SR_CONF_LIMIT_FRAMES:
+ devc->frame_limit = g_variant_get_uint64(data);
+ ret = SR_OK;
+ break;
+ case SR_CONF_TRIGGER_SOURCE:
+ tmp = g_variant_get_string(data, NULL);
+ for (i = 0; (*model->trigger_sources)[i]; i++) {
+ if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
+ continue;
+ state->trigger_source = i;
+ /* TODO: A and B trigger support possible? */
+ ret = dlm_trigger_source_set(sdi->conn, (*model->trigger_sources)[i]);
+ break;
+ }
+ break;
+ case SR_CONF_VDIV:
+ if (cg_type == CG_NONE) {
+ sr_err("No channel group specified.");
+ return SR_ERR_CHANNEL_GROUP;
+ }
+
+ g_variant_get(data, "(tt)", &p, &q);
+
+ for (i = 0; i < model->num_vdivs; i++) {
+ if (p != (*model->vdivs)[i][0] ||
+ q != (*model->vdivs)[i][1])
+ continue;
+ for (j = 1; j <= model->analog_channels; ++j) {
+ if (cg != devc->analog_groups[j - 1])
+ continue;
+ state->analog_states[j - 1].vdiv = i;
+ g_ascii_formatd(float_str, sizeof(float_str),
+ "%E", (float) p / q);
+ if (dlm_analog_chan_vdiv_set(sdi->conn, j, float_str) != SR_OK ||
+ sr_scpi_get_opc(sdi->conn) != SR_OK)
+ return SR_ERR;
+
+ break;
+ }
+
+ ret = SR_OK;
+ break;
+ }
+ break;
+ case SR_CONF_TIMEBASE:
+ g_variant_get(data, "(tt)", &p, &q);
+
+ for (i = 0; i < model->num_timebases; i++) {
+ if (p != (*model->timebases)[i][0] ||
+ q != (*model->timebases)[i][1])
+ continue;
+ state->timebase = i;
+ g_ascii_formatd(float_str, sizeof(float_str),
+ "%E", (float) p / q);
+ ret = dlm_timebase_set(sdi->conn, float_str);
+ update_sample_rate = TRUE;
+ break;
+ }
+ break;
+ case SR_CONF_HORIZ_TRIGGERPOS:
+ tmp_d = g_variant_get_double(data);
+
+ /* TODO: Check if the calculation makes sense for the DLM. */
+ if (tmp_d < 0.0 || tmp_d > 1.0)
+ return SR_ERR;
+
+ state->horiz_triggerpos = tmp_d;
+ tmp_d = -(tmp_d - 0.5) *
+ ((double) (*model->timebases)[state->timebase][0] /
+ (*model->timebases)[state->timebase][1])
+ * model->num_xdivs;
+
+ g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
+ ret = dlm_horiz_trigger_pos_set(sdi->conn, float_str);
+ break;
+ case SR_CONF_TRIGGER_SLOPE:
+ tmp = g_variant_get_string(data, NULL);
+
+ if (!tmp || !(tmp[0] == 'f' || tmp[0] == 'r'))
+ return SR_ERR_ARG;
+
+ /* Note: See dlm_trigger_slopes[] in protocol.c. */
+ state->trigger_slope = (tmp[0] == 'r') ?
+ SLOPE_POSITIVE : SLOPE_NEGATIVE;
+
+ ret = dlm_trigger_slope_set(sdi->conn, state->trigger_slope);
+ break;
+ case SR_CONF_COUPLING:
+ if (cg_type == CG_NONE) {
+ sr_err("No channel group specified.");
+ return SR_ERR_CHANNEL_GROUP;
+ }
+
+ tmp = g_variant_get_string(data, NULL);
+
+ for (i = 0; (*model->coupling_options)[i]; i++) {
+ if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
+ continue;
+ for (j = 1; j <= model->analog_channels; ++j) {
+ if (cg != devc->analog_groups[j - 1])
+ continue;
+ state->analog_states[j-1].coupling = i;
+
+ if (dlm_analog_chan_coupl_set(sdi->conn, j, tmp) != SR_OK ||
+ sr_scpi_get_opc(sdi->conn) != SR_OK)
+ return SR_ERR;
+ break;
+ }
+
+ ret = SR_OK;
+ break;
+ }
+ break;
+ default:
+ ret = SR_ERR_NA;
+ break;
+ }
+
+ if (ret == SR_OK)
+ ret = sr_scpi_get_opc(sdi->conn);
+
+ if (ret == SR_OK && update_sample_rate)
+ ret = dlm_sample_rate_query(sdi);
+
+ return ret;
+}
+
+static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
+ const struct sr_channel_group *cg)
+{
+ int cg_type;
+ struct dev_context *devc;
+ struct scope_config *model;
+
+ if (!sdi || !(devc = sdi->priv))
+ return SR_ERR_ARG;
+
+ if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
+ return SR_ERR;
+
+ model = devc->model_config;
+
+ switch (key) {
+ case SR_CONF_SCAN_OPTIONS:
+ *data = NULL;
+ break;
+ case SR_CONF_DEVICE_OPTIONS:
+ if (cg_type == CG_NONE) {
+ *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
+ model->hw_caps, model->num_hwcaps, sizeof(int32_t));
+ } else if (cg_type == CG_ANALOG) {
+ *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
+ model->analog_hwcaps, model->num_analog_hwcaps, sizeof(int32_t));
+ } else {
+ *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
+ NULL, 0, sizeof(int32_t));
+ }
+ break;
+ case SR_CONF_COUPLING:
+ if (cg_type == CG_NONE)
+ return SR_ERR_CHANNEL_GROUP;
+ *data = g_variant_new_strv(*model->coupling_options,
+ g_strv_length((char **)*model->coupling_options));
+ break;
+ case SR_CONF_TRIGGER_SOURCE:
+ *data = g_variant_new_strv(*model->trigger_sources,
+ g_strv_length((char **)*model->trigger_sources));
+ break;
+ case SR_CONF_TRIGGER_SLOPE:
+ *data = g_variant_new_strv(*model->trigger_slopes,
+ g_strv_length((char **)*model->trigger_slopes));
+ break;
+ case SR_CONF_TIMEBASE:
+ *data = build_tuples(model->timebases, model->num_timebases);
+ break;
+ case SR_CONF_VDIV:
+ if (cg_type == CG_NONE)
+ return SR_ERR_CHANNEL_GROUP;
+ *data = build_tuples(model->vdivs, model->num_vdivs);
+ break;
+ default:
+ return SR_ERR_NA;
+ }
+
+ return SR_OK;
+}
+
+static int dlm_check_channels(GSList *channels)
+{
+ GSList *l;
+ struct sr_channel *ch;
+ gboolean enabled_pod1, enabled_chan4;
+
+ enabled_pod1 = enabled_chan4 = FALSE;
+
+ /* Note: On the DLM2000, CH4 and Logic are shared. */
+ /* TODO Handle non-DLM2000 models. */
+ for (l = channels; l; l = l->next) {
+ ch = l->data;
+ switch (ch->type) {
+ case SR_CHANNEL_ANALOG:
+ if (ch->index == 3)
+ enabled_chan4 = TRUE;
+ break;
+ case SR_CHANNEL_LOGIC:
+ enabled_pod1 = TRUE;
+ break;
+ default:
+ return SR_ERR;
+ }
+ }
+
+ if (enabled_pod1 && enabled_chan4)
+ return SR_ERR;
+
+ return SR_OK;
+}
+
+static int dlm_setup_channels(const struct sr_dev_inst *sdi)
+{
+ GSList *l;
+ unsigned int i;
+ gboolean *pod_enabled, setup_changed;
+ struct scope_state *state;
+ struct scope_config *model;
+ struct sr_channel *ch;
+ struct dev_context *devc;
+ struct sr_scpi_dev_inst *scpi;
+
+ devc = sdi->priv;
+ scpi = sdi->conn;
+ state = devc->model_state;
+ model = devc->model_config;
+ setup_changed = FALSE;
+
+ pod_enabled = g_try_malloc0(sizeof(gboolean) * model->pods);
+
+ for (l = sdi->channels; l; l = l->next) {
+ ch = l->data;
+ switch (ch->type) {
+ case SR_CHANNEL_ANALOG:
+ if (ch->enabled == state->analog_states[ch->index].state)
+ break;
+
+ if (dlm_analog_chan_state_set(scpi, ch->index + 1,
+ ch->enabled) != SR_OK)
+ return SR_ERR;
+
+ state->analog_states[ch->index].state = ch->enabled;
+ setup_changed = TRUE;
+ break;
+ case SR_CHANNEL_LOGIC:
+ if (ch->enabled)
+ pod_enabled[ch->index / 8] = TRUE;
+
+ if (ch->enabled == state->digital_states[ch->index])
+ break;
+
+ if (dlm_digital_chan_state_set(scpi, ch->index + 1,
+ ch->enabled) != SR_OK)
+ return SR_ERR;
+
+ state->digital_states[ch->index] = ch->enabled;
+ setup_changed = TRUE;
+ break;
+ default:
+ return SR_ERR;
+ }
+ }
+
+ for (i = 1; i <= model->pods; ++i) {
+ if (state->pod_states[i - 1] == pod_enabled[i - 1])
+ continue;
+
+ if (dlm_digital_pod_state_set(scpi, i,
+ pod_enabled[i - 1]) != SR_OK)
+ return SR_ERR;
+
+ state->pod_states[i - 1] = pod_enabled[i - 1];
+ setup_changed = TRUE;
+ }
+
+ g_free(pod_enabled);
+
+ if (setup_changed && dlm_sample_rate_query(sdi) != SR_OK)
+ return SR_ERR;
+
+ return SR_OK;
+}
+
+static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
+{
+ GSList *l;
+ gboolean digital_added;
+ struct sr_channel *ch;
+ struct dev_context *devc;
+ struct sr_scpi_dev_inst *scpi;
+
+ (void)cb_data;
+
+ if (sdi->status != SR_ST_ACTIVE) return SR_ERR_DEV_CLOSED;
+
+ scpi = sdi->conn;
+ devc = sdi->priv;
+ digital_added = FALSE;
+
+ g_slist_free(devc->enabled_channels);
+ devc->enabled_channels = NULL;
+
+ for (l = sdi->channels; l; l = l->next) {
+ ch = l->data;
+ if (!ch->enabled)
+ continue;
+ /* Only add a single digital channel. */
+ if (ch->type != SR_CHANNEL_LOGIC || !digital_added) {
+ devc->enabled_channels = g_slist_append(
+ devc->enabled_channels, ch);
+ if (ch->type == SR_CHANNEL_LOGIC)
+ digital_added = TRUE;
+ }
+ }
+
+ if (!devc->enabled_channels)
+ return SR_ERR;
+
+ if (dlm_check_channels(devc->enabled_channels) != SR_OK) {
+ sr_err("Invalid channel configuration specified!");
+ return SR_ERR_NA;
+ }
+
+ if (dlm_setup_channels(sdi) != SR_OK) {
+ sr_err("Failed to setup channel configuration!");
+ return SR_ERR;
+ }
+
+ /* Call our callback when data comes in or after 50ms. */
+ sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
+ dlm_data_receive, (void *)sdi);
+
+ return SR_OK;
+}
+
+static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
+{
+ struct dev_context *devc;
+ struct sr_scpi_dev_inst *scpi;
+ struct sr_datafeed_packet packet;
+
+ (void)cb_data;
+
+ packet.type = SR_DF_END;
+ packet.payload = NULL;
+ sr_session_send(sdi, &packet);
+
+ if (sdi->status != SR_ST_ACTIVE)
+ return SR_ERR_DEV_CLOSED;
+
+ devc = sdi->priv;
+
+ devc->num_frames = 0;
+ g_slist_free(devc->enabled_channels);
+ devc->enabled_channels = NULL;
+ scpi = sdi->conn;
+ sr_scpi_source_remove(sdi->session, scpi);
+
+ return SR_OK;
+}
SR_PRIV struct sr_dev_driver yokogawa_dlm_driver_info = {
.name = "yokogawa-dlm",
.longname = "Yokogawa DL/DLM driver",
.api_version = 1,
- .init = NULL,
- .cleanup = NULL,
- .scan = NULL,
- .dev_list = NULL,
- .dev_clear = NULL,
- .config_get = NULL,
- .config_set = NULL,
- .config_list = NULL,
- .dev_open = NULL,
- .dev_close = NULL,
- .dev_acquisition_start = NULL,
- .dev_acquisition_stop = NULL,
+ .init = init,
+ .cleanup = cleanup,
+ .scan = scan,
+ .dev_list = dev_list,
+ .dev_clear = dev_clear,
+ .config_get = config_get,
+ .config_set = config_set,
+ .config_list = config_list,
+ .dev_open = dev_open,
+ .dev_close = dev_close,
+ .dev_acquisition_start = dev_acquisition_start,
+ .dev_acquisition_stop = dev_acquisition_stop,
.priv = NULL,
};
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+/** @file
+ * <em>Yokogawa DL/DLM series</em> oscilloscope driver
+ * @internal
+ */
+
#include "protocol.h"
+static const int32_t dlm_hwcaps[] = {
+ SR_CONF_LOGIC_ANALYZER,
+ SR_CONF_OSCILLOSCOPE,
+ SR_CONF_TRIGGER_SLOPE,
+ SR_CONF_TRIGGER_SOURCE,
+ SR_CONF_TIMEBASE,
+ SR_CONF_NUM_TIMEBASE,
+ SR_CONF_HORIZ_TRIGGERPOS,
+};
+
+static const int32_t dlm_analog_caps[] = {
+ SR_CONF_VDIV,
+ SR_CONF_COUPLING,
+ SR_CONF_NUM_VDIV,
+};
+
+static const char *dlm_coupling_options[] = {
+ "AC",
+ "DC",
+ "DC50",
+ "GND",
+ NULL,
+};
+
+/* Note: Values must correlate to the trigger_slopes values */
+static const char *dlm_trigger_slopes[] = {
+ "r",
+ "f",
+ NULL,
+};
+
+static const char *dlm_2ch_trigger_sources[] = {
+ "1",
+ "2",
+ "LINE",
+ "EXT",
+ NULL,
+};
+
+/* TODO: Is BITx handled correctly or is Dx required? */
+static const char *dlm_4ch_trigger_sources[] = {
+ "1",
+ "2",
+ "3",
+ "4",
+ "LINE",
+ "EXT",
+ "BIT1",
+ "BIT2",
+ "BIT3",
+ "BIT4",
+ "BIT5",
+ "BIT6",
+ "BIT7",
+ "BIT8",
+ NULL,
+};
+
+static const uint64_t dlm_timebases[][2] = {
+ /* nanoseconds */
+ { 1, 1000000000 },
+ { 2, 1000000000 },
+ { 5, 1000000000 },
+ { 10, 1000000000 },
+ { 20, 1000000000 },
+ { 50, 1000000000 },
+ { 100, 1000000000 },
+ { 200, 1000000000 },
+ { 500, 1000000000 },
+ /* microseconds */
+ { 1, 1000000 },
+ { 2, 1000000 },
+ { 5, 1000000 },
+ { 10, 1000000 },
+ { 20, 1000000 },
+ { 50, 1000000 },
+ { 100, 1000000 },
+ { 200, 1000000 },
+ { 500, 1000000 },
+ /* milliseconds */
+ { 1, 1000 },
+ { 2, 1000 },
+ { 5, 1000 },
+ { 10, 1000 },
+ { 20, 1000 },
+ { 50, 1000 },
+ { 100, 1000 },
+ { 200, 1000 },
+ { 500, 1000 },
+ /* seconds */
+ { 1, 1 },
+ { 2, 1 },
+ { 5, 1 },
+ { 10, 1 },
+ { 20, 1 },
+ { 50, 1 },
+ { 100, 1 },
+ { 200, 1 },
+ { 500, 1 },
+};
+
+static const uint64_t dlm_vdivs[][2] = {
+ /* millivolts */
+ { 2, 1000 },
+ { 5, 1000 },
+ { 10, 1000 },
+ { 20, 1000 },
+ { 50, 1000 },
+ { 100, 1000 },
+ { 200, 1000 },
+ { 500, 1000 },
+ /* volts */
+ { 1, 1 },
+ { 2, 1 },
+ { 5, 1 },
+ { 10, 1 },
+ { 20, 1 },
+ { 50, 1 },
+ { 100, 1 },
+ { 200, 1 },
+ { 500, 1 },
+};
+
+static const char *scope_analog_channel_names[] = {
+ "1",
+ "2",
+ "3",
+ "4"
+};
+
+static const char *scope_digital_channel_names[] = {
+ "D0",
+ "D1",
+ "D2",
+ "D3",
+ "D4",
+ "D5",
+ "D6",
+ "D7"
+};
+
+static struct scope_config scope_models[] = {
+ {
+ .model_id = {"710105", "710115", "710125", NULL},
+ .model_name = {"DLM2022", "DLM2032", "DLM2052", NULL},
+ .analog_channels = 2,
+ .digital_channels = 0,
+ .pods = 0,
+
+ .analog_names = &scope_analog_channel_names,
+ .digital_names = &scope_digital_channel_names,
+
+ .hw_caps = &dlm_hwcaps,
+ .num_hwcaps = ARRAY_SIZE(dlm_hwcaps),
+
+ .analog_hwcaps = &dlm_analog_caps,
+ .num_analog_hwcaps = ARRAY_SIZE(dlm_analog_caps),
+
+ .coupling_options = &dlm_coupling_options,
+ .trigger_sources = &dlm_2ch_trigger_sources,
+ .trigger_slopes = &dlm_trigger_slopes,
+
+ .timebases = &dlm_timebases,
+ .num_timebases = ARRAY_SIZE(dlm_timebases),
+
+ .vdivs = &dlm_vdivs,
+ .num_vdivs = ARRAY_SIZE(dlm_vdivs),
+
+ .num_xdivs = 10,
+ .num_ydivs = 8,
+ },
+ {
+ .model_id = {"710110", "710120", "710130", NULL},
+ .model_name = {"DLM2024", "DLM2034", "DLM2054", NULL},
+ .analog_channels = 4,
+ .digital_channels = 8,
+ .pods = 1,
+
+ .analog_names = &scope_analog_channel_names,
+ .digital_names = &scope_digital_channel_names,
+
+ .hw_caps = &dlm_hwcaps,
+ .num_hwcaps = ARRAY_SIZE(dlm_hwcaps),
+
+ .analog_hwcaps = &dlm_analog_caps,
+ .num_analog_hwcaps = ARRAY_SIZE(dlm_analog_caps),
+
+ .coupling_options = &dlm_coupling_options,
+ .trigger_sources = &dlm_4ch_trigger_sources,
+ .trigger_slopes = &dlm_trigger_slopes,
+
+ .timebases = &dlm_timebases,
+ .num_timebases = ARRAY_SIZE(dlm_timebases),
+
+ .vdivs = &dlm_vdivs,
+ .num_vdivs = ARRAY_SIZE(dlm_vdivs),
+
+ .num_xdivs = 10,
+ .num_ydivs = 8,
+ },
+};
+
+/**
+ * Prints out the state of the device as we currently know it.
+ *
+ * @param config This is the scope configuration.
+ * @param state The current scope state to print.
+ */
+static void scope_state_dump(struct scope_config *config,
+ struct scope_state *state)
+{
+ unsigned int i;
+ char *tmp;
+
+ for (i = 0; i < config->analog_channels; ++i) {
+ tmp = sr_voltage_string((*config->vdivs)[state->analog_states[i].vdiv][0],
+ (*config->vdivs)[state->analog_states[i].vdiv][1]);
+ sr_info("State of analog channel %d -> %s : %s (coupling) %s (vdiv) %2.2e (offset)",
+ i + 1, state->analog_states[i].state ? "On" : "Off",
+ (*config->coupling_options)[state->analog_states[i].coupling],
+ tmp, state->analog_states[i].vertical_offset);
+ }
+
+ for (i = 0; i < config->digital_channels; ++i) {
+ sr_info("State of digital channel %d -> %s", i,
+ state->digital_states[i] ? "On" : "Off");
+ }
+
+ for (i = 0; i < config->pods; ++i) {
+ sr_info("State of digital POD %d -> %s", i,
+ state->pod_states[i] ? "On" : "Off");
+ }
+
+ tmp = sr_period_string((*config->timebases)[state->timebase][0] *
+ (*config->timebases)[state->timebase][1]);
+ sr_info("Current timebase: %s", tmp);
+ g_free(tmp);
+
+ tmp = sr_samplerate_string(state->sample_rate);
+ sr_info("Current samplerate: %s", tmp);
+ g_free(tmp);
+
+ sr_info("Current trigger: %s (source), %s (slope) %.2f (offset)",
+ (*config->trigger_sources)[state->trigger_source],
+ (*config->trigger_slopes)[state->trigger_slope],
+ state->horiz_triggerpos);
+}
+
+/**
+ * Searches through an array of strings and returns the index to the
+ * array where a given string is located.
+ *
+ * @param value The string to search for.
+ * @param array The array of strings.
+ * @param result The index at which value is located in array. -1 on error.
+ *
+ * @return SR_ERR when value couldn't be found, SR_OK otherwise.
+ */
+static int array_option_get(char *value, const char *(*array)[],
+ int *result)
+{
+ unsigned int i;
+
+ *result = -1;
+
+ for (i = 0; (*array)[i]; ++i)
+ if (!g_strcmp0(value, (*array)[i])) {
+ *result = i;
+ break;
+ }
+
+ if (*result == -1)
+ return SR_ERR;
+
+ return SR_OK;
+}
+
+/**
+ * This function takes a value of the form "2.000E-03", converts it to a
+ * significand / factor pair and returns the index of an array where
+ * a matching pair was found.
+ *
+ * It's a bit convoluted because of floating-point issues. The value "10.00E-09"
+ * is parsed by g_ascii_strtod() as 0.000000009999999939, for example.
+ * Therefore it's easier to break the number up into two strings and handle
+ * them separately.
+ *
+ * @param value The string to be parsed.
+ * @param array The array of s/f pairs.
+ * @param array_len The number of pairs in the array.
+ * @param result The index at which a matching pair was found.
+ *
+ * @return SR_ERR on any parsing error, SR_OK otherwise.
+ */
+static int array_float_get(gchar *value, const uint64_t array[][2],
+ int array_len, int *result)
+{
+ int i;
+ uint64_t f;
+ float s;
+ gchar ss[10], es[10];
+
+ memset(ss, 0, sizeof(ss));
+ memset(es, 0, sizeof(es));
+
+ strncpy(ss, value, 5);
+ strncpy(es, &(value[6]), 3);
+
+ if (sr_atof_ascii(ss, &s) != SR_OK)
+ return SR_ERR;
+ if (sr_atoi(es, &i) != SR_OK)
+ return SR_ERR;
+
+ /* Transform e.g. 10^-03 to 1000 as the array stores the inverse. */
+ f = pow(10, abs(i));
+
+ /* Adjust the significand/factor pair to make sure
+ * that f is a multiple of 1000.
+ */
+ while ((int)fmod(log10(f), 3) > 0) { s *= 10; f *= 10; }
+
+ /* Truncate s to circumvent rounding errors. */
+ s = (int)s;
+
+ for (i = 0; i < array_len; i++) {
+ if ( (s == array[i][0]) && (f == array[i][1]) ) {
+ *result = i;
+ return SR_OK;
+ }
+ }
+
+ return SR_ERR;
+}
+
+/**
+ * Obtains information about all analog channels from the oscilloscope.
+ * The internal state information is updated accordingly.
+ *
+ * @param scpi An open SCPI connection.
+ * @param config The device's device configuration.
+ * @param state The device's state information.
+ *
+ * @return SR_ERR on error, SR_OK otherwise.
+ */
+static int analog_channel_state_get(struct sr_scpi_dev_inst *scpi,
+ struct scope_config *config,
+ struct scope_state *state)
+{
+ int i, j;
+ gchar *response;
+
+ for (i = 0; i < config->analog_channels; ++i) {
+
+ if (dlm_analog_chan_state_get(scpi, i + 1,
+ &state->analog_states[i].state) != SR_OK)
+ return SR_ERR;
+
+ if (dlm_analog_chan_vdiv_get(scpi, i + 1, &response) != SR_OK)
+ return SR_ERR;
+
+ if (array_float_get(response, *config->vdivs, config->num_vdivs,
+ &j) != SR_OK) {
+ g_free(response);
+ return SR_ERR;
+ }
+
+ g_free(response);
+ state->analog_states[i].vdiv = j;
+
+ if (dlm_analog_chan_voffs_get(scpi, i + 1,
+ &state->analog_states[i].vertical_offset) != SR_OK)
+ return SR_ERR;
+
+ if (dlm_analog_chan_wrange_get(scpi, i + 1,
+ &state->analog_states[i].waveform_range) != SR_OK)
+ return SR_ERR;
+
+ if (dlm_analog_chan_woffs_get(scpi, i + 1,
+ &state->analog_states[i].waveform_offset) != SR_OK)
+ return SR_ERR;
+
+ if (dlm_analog_chan_coupl_get(scpi, i + 1, &response) != SR_OK) {
+ g_free(response);
+ return SR_ERR;
+ }
+
+ if (array_option_get(response, config->coupling_options,
+ &state->analog_states[i].coupling) != SR_OK) {
+ g_free(response);
+ return SR_ERR;
+ }
+ g_free(response);
+ }
+
+ return SR_OK;
+}
+
+/**
+ * Obtains information about all digital channels from the oscilloscope.
+ * The internal state information is updated accordingly.
+ *
+ * @param scpi An open SCPI connection.
+ * @param config The device's device configuration.
+ * @param state The device's state information.
+ *
+ * @return SR_ERR on error, SR_OK otherwise.
+ */
+static int digital_channel_state_get(struct sr_scpi_dev_inst *scpi,
+ struct scope_config *config,
+ struct scope_state *state)
+{
+ unsigned int i;
+
+ if (!config->digital_channels)
+ {
+ sr_warn("Tried obtaining digital channel states on a " \
+ "model without digital inputs.");
+ return SR_OK;
+ }
+
+ for (i = 0; i < config->digital_channels; ++i) {
+ if (dlm_digital_chan_state_get(scpi, i + 1,
+ &state->digital_states[i]) != SR_OK) {
+ return SR_ERR;
+ }
+ }
+
+ if (!config->pods)
+ {
+ sr_warn("Tried obtaining pod states on a model without pods.");
+ return SR_OK;
+ }
+
+ for (i = 0; i < config->pods; ++i) {
+ if (dlm_digital_pod_state_get(scpi, i + 'A',
+ &state->pod_states[i]) != SR_OK)
+ return SR_ERR;
+ }
+
+ return SR_OK;
+}
+
+/**
+ * Obtains information about the sample rate from the oscilloscope.
+ * The internal state information is updated accordingly.
+ *
+ * @param sdi The device instance.
+ *
+ * @return SR_ERR on error, SR_OK otherwise.
+ */
+SR_PRIV int dlm_sample_rate_query(const struct sr_dev_inst *sdi)
+{
+ struct dev_context *devc;
+ struct scope_state *state;
+ float tmp_float;
+
+ devc = sdi->priv;
+ state = devc->model_state;
+
+ /* No need to find an active channel to query the sample rate:
+ * querying any channel will do, so we use channel 1 all the time.
+ */
+ if (dlm_analog_chan_srate_get(sdi->conn, 1, &tmp_float) != SR_OK)
+ return SR_ERR;
+
+ state->sample_rate = tmp_float;
+
+ return SR_OK;
+}
+
+/**
+ * Obtains information about the current device state from the oscilloscope,
+ * including all analog and digital channel configurations.
+ * The internal state information is updated accordingly.
+ *
+ * @param sdi The device instance.
+ *
+ * @return SR_ERR on error, SR_OK otherwise.
+ */
+SR_PRIV int dlm_scope_state_query(struct sr_dev_inst *sdi)
+{
+ struct dev_context *devc;
+ struct scope_state *state;
+ struct scope_config *config;
+ float tmp_float;
+ gchar *response;
+ int i;
+
+ devc = sdi->priv;
+ config = devc->model_config;
+ state = devc->model_state;
+
+ if (analog_channel_state_get(sdi->conn, config, state) != SR_OK)
+ return SR_ERR;
+
+ if (digital_channel_state_get(sdi->conn, config, state) != SR_OK)
+ return SR_ERR;
+
+ if (dlm_timebase_get(sdi->conn, &response) != SR_OK)
+ return SR_ERR;
+
+ if (array_float_get(response, *config->timebases,
+ config->num_timebases, &i) != SR_OK) {
+ g_free(response);
+ return SR_ERR;
+ }
+
+ g_free(response);
+ state->timebase = i;
+
+ if (dlm_horiz_trigger_pos_get(sdi->conn, &tmp_float) != SR_OK)
+ return SR_ERR;
+
+ /* TODO: Check if the calculation makes sense for the DLM. */
+ state->horiz_triggerpos = tmp_float /
+ (((double)(*config->timebases)[state->timebase][0] /
+ (*config->timebases)[state->timebase][1]) * config->num_xdivs);
+ state->horiz_triggerpos -= 0.5;
+ state->horiz_triggerpos *= -1;
+
+ if (dlm_trigger_source_get(sdi->conn, &response) != SR_OK) {
+ g_free(response);
+ return SR_ERR;
+ }
+
+ if (array_option_get(response, config->trigger_sources,
+ &state->trigger_source) != SR_OK) {
+ g_free(response);
+ return SR_ERR;
+ }
+
+ g_free(response);
+
+ if (dlm_trigger_slope_get(sdi->conn, &i) != SR_OK)
+ return SR_ERR;
+
+ state->trigger_slope = i;
+
+ dlm_sample_rate_query(sdi);
+
+ scope_state_dump(config, state);
+
+ return SR_OK;
+}
+
+/**
+ * Creates a new device state structure.
+ *
+ * @param config The device configuration to use.
+ *
+ * @return The newly allocated scope_state struct or NULL on error.
+ */
+static struct scope_state *dlm_scope_state_new(struct scope_config *config)
+{
+ struct scope_state *state;
+
+ if (!(state = g_try_malloc0(sizeof(struct scope_state))))
+ return NULL;
+
+ state->analog_states = g_malloc0(config->analog_channels *
+ sizeof(struct analog_channel_state));
+
+ state->digital_states = g_malloc0(config->digital_channels *
+ sizeof(gboolean));
+
+ state->pod_states = g_malloc0(config->pods * sizeof(gboolean));
+
+ return state;
+}
+
+/**
+ * Frees the memory that was allocated by a call to dlm_scope_state_new().
+ *
+ * @param state The device state structure whose memory is to be freed.
+ */
+SR_PRIV void dlm_scope_state_destroy(struct scope_state *state)
+{
+ g_free(state->analog_states);
+ g_free(state->digital_states);
+ g_free(state->pod_states);
+ g_free(state);
+}
+
+SR_PRIV int dlm_model_get(char *model_id, char **model_name, int *model_index)
+{
+ unsigned int i, j;
+
+ *model_index = -1;
+ *model_name = NULL;
+
+ for (i = 0; i < ARRAY_SIZE(scope_models); i++) {
+ for (j = 0; scope_models[i].model_id[j]; j++) {
+ if (!strcmp(model_id, scope_models[i].model_id[j])) {
+ *model_index = i;
+ *model_name = (char *)scope_models[i].model_name[j];
+ break;
+ }
+ }
+ if (*model_index != -1)
+ break;
+ }
+
+ if (*model_index == -1) {
+ sr_err("Found unsupported DLM device with model identifier %s.",
+ model_id);
+ return SR_ERR_NA;
+ }
+
+ return SR_OK;
+}
+
+/**
+ * Attempts to initialize a DL/DLM device and prepares internal structures
+ * if a suitable device was found.
+ *
+ * @param sdi The device instance.
+ */
+SR_PRIV int dlm_device_init(struct sr_dev_inst *sdi, int model_index)
+{
+ char tmp[25];
+ int i;
+ struct sr_channel *ch;
+ struct dev_context *devc;
+
+ devc = sdi->priv;
+
+ devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group*) *
+ scope_models[model_index].analog_channels);
+
+ devc->digital_groups = g_malloc0(sizeof(struct sr_channel_group*) *
+ scope_models[model_index].digital_channels);
+
+ /* Add analog channels. */
+ for (i = 0; i < scope_models[model_index].analog_channels; i++) {
+ if (!(ch = sr_channel_new(i, SR_CHANNEL_ANALOG, TRUE,
+ (*scope_models[model_index].analog_names)[i])))
+ return SR_ERR_MALLOC;
+ sdi->channels = g_slist_append(sdi->channels, ch);
+
+ devc->analog_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
+
+ devc->analog_groups[i]->name = g_strdup(
+ (char *)(*scope_models[model_index].analog_names)[i]);
+ devc->analog_groups[i]->channels = g_slist_append(NULL, ch);
+
+ sdi->channel_groups = g_slist_append(sdi->channel_groups,
+ devc->analog_groups[i]);
+ }
+
+ /* Add digital channel groups. */
+ for (i = 0; i < scope_models[model_index].pods; ++i) {
+ g_snprintf(tmp, sizeof(tmp), "POD%d", i);
+
+ devc->digital_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
+ if (!devc->digital_groups[i])
+ return SR_ERR_MALLOC;
+
+ devc->digital_groups[i]->name = g_strdup(tmp);
+ sdi->channel_groups = g_slist_append(sdi->channel_groups,
+ devc->digital_groups[i]);
+ }
+
+ /* Add digital channels. */
+ for (i = 0; i < scope_models[model_index].digital_channels; i++) {
+ if (!(ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE,
+ (*scope_models[model_index].digital_names)[i])))
+ return SR_ERR_MALLOC;
+ sdi->channels = g_slist_append(sdi->channels, ch);
+
+ devc->digital_groups[i / 8]->channels = g_slist_append(
+ devc->digital_groups[i / 8]->channels, ch);
+ }
+ devc->model_config = &scope_models[model_index];
+ devc->frame_limit = 0;
+
+ if (!(devc->model_state = dlm_scope_state_new(devc->model_config)))
+ return SR_ERR_MALLOC;
+
+ /* Disable non-standard response behavior. */
+ if (dlm_response_headers_set(sdi->conn, FALSE) != SR_OK)
+ return SR_ERR;
+
+ return SR_OK;
+}
+
+/**
+ * Send an SCPI command, read the reply and store the result in scpi_response
+ * without performing any processing on it.
+ *
+ * @param scpi Previously initialised SCPI device structure.
+ * @param command The SCPI command to send to the device (can be NULL).
+ * @param scpi_response Pointer where to store the parsed result.
+ *
+ * @return SR_OK on success, SR_ERR on failure.
+ */
+static int dlm_scpi_get_raw(struct sr_scpi_dev_inst *scpi,
+ const char *command, GArray **scpi_response)
+{
+ char buf[256];
+ int len;
+
+ if (command)
+ if (sr_scpi_send(scpi, command) != SR_OK)
+ return SR_ERR;
+
+ if (sr_scpi_read_begin(scpi) != SR_OK)
+ return SR_ERR;
+
+ *scpi_response = g_array_new(FALSE, FALSE, sizeof(uint8_t));
+
+ while (!sr_scpi_read_complete(scpi)) {
+ len = sr_scpi_read_data(scpi, buf, sizeof(buf));
+ if (len < 0) {
+ g_array_free(*scpi_response, TRUE);
+ *scpi_response = NULL;
+ return SR_ERR;
+ }
+ g_array_append_vals(*scpi_response, buf, len);
+ }
+
+ return SR_OK;
+}
+
+/**
+ * Reads and removes the block data header from a given data input.
+ * Format is #ndddd... with n being the number of decimal digits d.
+ * The string dddd... contains the decimal-encoded length of the data.
+ * Example: #9000000013 would yield a length of 13 bytes.
+ *
+ * @param data The input data.
+ * @param len The determined input data length.
+ */
+static int dlm_block_data_header_process(GArray *data, int *len)
+{
+ int i, n;
+ gchar s[20];
+
+ if (g_array_index(data, gchar, 0) != '#')
+ return SR_ERR;
+
+ n = (uint8_t)(g_array_index(data, gchar, 1) - '0');
+
+ for (i = 0; i < n; i++)
+ s[i] = g_array_index(data, gchar, 2 + i);
+ s[i] = 0;
+
+ if (sr_atoi(s, len) != SR_OK)
+ return SR_ERR;
+
+ g_array_remove_range(data, 0, 2 + n);
+
+ return SR_OK;
+}
+
+/**
+ * Turns raw sample data into voltages and sends them off to the session bus.
+ *
+ * @param data The raw sample data.
+ * @samples Number of samples that were acquired.
+ * @ch_state Pointer to the state of the channel whose data we're processing.
+ * @sdi The device instance.
+ *
+ * @return SR_ERR when data is trucated, SR_OK otherwise.
+ */
+static int dlm_analog_samples_send(GArray *data, int samples,
+ struct analog_channel_state *ch_state,
+ struct sr_dev_inst *sdi)
+{
+ int i;
+ float voltage, range, offset;
+ GArray *float_data;
+ struct dev_context *devc;
+ struct sr_channel *ch;
+ struct sr_datafeed_analog analog;
+ struct sr_datafeed_packet packet;
+
+ if (data->len < samples * sizeof(uint8_t)) {
+ sr_err("Truncated waveform data packet received.");
+ return SR_ERR;
+ }
+
+ devc = sdi->priv;
+ ch = devc->current_channel->data;
+
+ range = ch_state->waveform_range;
+ offset = ch_state->waveform_offset;
+
+ /* Convert byte sample to voltage according to
+ * page 269 of the Communication Interface User's Manual.
+ */
+ float_data = g_array_new(FALSE, FALSE, sizeof(float));
+ for (i = 0; i < samples; i++) {
+ voltage = (float)g_array_index(data, int8_t, i);
+ voltage = (range * voltage /
+ DLM_DIVISION_FOR_BYTE_FORMAT) + offset;
+ g_array_append_val(float_data, voltage);
+ }
+
+ analog.channels = g_slist_append(NULL, ch);
+ analog.num_samples = float_data->len;
+ analog.data = (float*)float_data->data;
+ analog.mq = SR_MQ_VOLTAGE;
+ analog.unit = SR_UNIT_VOLT;
+ analog.mqflags = 0;
+ packet.type = SR_DF_ANALOG;
+ packet.payload = &analog;
+ sr_session_send(sdi, &packet);
+ g_slist_free(analog.channels);
+
+ g_array_free(float_data, TRUE);
+ g_array_remove_range(data, 0, samples * sizeof(uint8_t));
+
+ return SR_OK;
+}
+
+/**
+ * Sends logic sample data off to the session bus.
+ *
+ * @param data The raw sample data.
+ * @samples Number of samples that were acquired.
+ * @ch_state Pointer to the state of the channel whose data we're processing.
+ * @sdi The device instance.
+ *
+ * @return SR_ERR when data is trucated, SR_OK otherwise.
+ */
+static int dlm_digital_samples_send(GArray *data, int samples,
+ struct sr_dev_inst *sdi)
+{
+ struct sr_datafeed_logic logic;
+ struct sr_datafeed_packet packet;
+
+ if (data->len < samples * sizeof(uint8_t)) {
+ sr_err("Truncated waveform data packet received.");
+ return SR_ERR;
+ }
+
+ logic.length = samples;
+ logic.unitsize = 1;
+ logic.data = data->data;
+ packet.type = SR_DF_LOGIC;
+ packet.payload = &logic;
+ sr_session_send(sdi, &packet);
+
+ g_array_remove_range(data, 0, samples * sizeof(uint8_t));
+
+ return SR_OK;
+}
+
+/**
+ * Attempts to query sample data from the oscilloscope in order to send it
+ * to the session bus for further processing.
+ *
+ * @param fd The file descriptor used as the event source.
+ * @param revents The received events.
+ * @param cb_data Callback data, in this case our device instance.
+ *
+ * @return TRUE in case of success or a recoverable error,
+ * FALSE when a fatal error was encountered.
+ */
+SR_PRIV int dlm_data_receive(int fd, int revents, void *cb_data)
+{
+ struct sr_channel *ch;
+ struct sr_dev_inst *sdi;
+ struct scope_state *model_state;
+ struct dev_context *devc;
+ struct sr_datafeed_packet packet;
+ GArray *data;
+ int result, num_bytes, samples;
+
+ (void)fd;
+ (void)revents;
+
+ if (!(sdi = cb_data))
+ return FALSE;
+
+ if (!(devc = sdi->priv))
+ return FALSE;
+
+ if (!(model_state = (struct scope_state*)devc->model_state))
+ return FALSE;
+
+ if (dlm_acq_length_get(sdi->conn, &samples) != SR_OK) {
+ sr_err("Failed to query acquisition length.");
+ return TRUE;
+ }
+
+ packet.type = SR_DF_FRAME_BEGIN;
+ sr_session_send(sdi, &packet);
+
+ /* Request data for all active channels. */
+ for (devc->current_channel = devc->enabled_channels;
+ devc->current_channel;
+ devc->current_channel = devc->current_channel->next) {
+ ch = devc->current_channel->data;
+
+ switch (ch->type) {
+ case SR_CHANNEL_ANALOG:
+ result = dlm_analog_data_get(sdi->conn, ch->index + 1);
+ break;
+ case SR_CHANNEL_LOGIC:
+ result = dlm_digital_data_get(sdi->conn);
+ break;
+ default:
+ sr_err("Invalid channel type encountered (%d).",
+ ch->type);
+ continue;
+ }
+
+ if (result != SR_OK) {
+ sr_err("Failed to query aquisition data.");
+ goto fail;
+ }
+
+ data = NULL;
+ if (dlm_scpi_get_raw(sdi->conn, NULL, &data) != SR_OK) {
+ sr_err("Failed to receive waveform data from device.");
+ goto fail;
+ }
+
+ if (dlm_block_data_header_process(data, &num_bytes) != SR_OK) {
+ sr_err("Encountered malformed block data header.");
+ goto fail;
+ }
+
+ if (num_bytes == 0) {
+ sr_warn("Zero-length waveform data packet received. " \
+ "Live mode not supported yet, stopping " \
+ "acquisition and retrying.");
+ /* Don't care about return value here. */
+ dlm_acquisition_stop(sdi->conn);
+ goto fail;
+ }
+
+ switch (ch->type) {
+ case SR_CHANNEL_ANALOG:
+ if (dlm_analog_samples_send(data, samples,
+ &model_state->analog_states[ch->index],
+ sdi) != SR_OK)
+ goto fail;
+ break;
+
+ case SR_CHANNEL_LOGIC:
+ if (dlm_digital_samples_send(data, samples,
+ sdi) != SR_OK)
+ goto fail;
+ break;
+
+ default:
+ sr_err("Invalid channel type encountered.");
+ break;
+ }
+
+ g_array_free(data, TRUE);
+ }
+
+ packet.type = SR_DF_FRAME_END;
+ sr_session_send(sdi, &packet);
+
+ sdi->driver->dev_acquisition_stop(sdi, cb_data);
+
+ return TRUE;
+
+fail:
+ if (data)
+ g_array_free(data, TRUE);
+
+ return TRUE;
+}
#include <glib.h>
#include <stdint.h>
+#include <stdlib.h>
#include <string.h>
+#include <math.h>
#include "libsigrok.h"
#include "libsigrok-internal.h"
+#include "protocol_wrappers.h"
+
+#define LOG_PREFIX "yokogawa-dlm"
+#define MAX_INSTRUMENT_VERSIONS 4
+
+/* See Communication Interface User's Manual on p. 268 (:WAVeform:ALL:SEND?). */
+#define DLM_MAX_FRAME_LENGTH (12500)
+/* See Communication Interface User's Manual on p. 269 (:WAVeform:SEND?). */
+#define DLM_DIVISION_FOR_WORD_FORMAT (3200)
+#define DLM_DIVISION_FOR_BYTE_FORMAT (12.5)
+
+
+enum trigger_slopes {
+ SLOPE_POSITIVE,
+ SLOPE_NEGATIVE
+};
+
+struct scope_config {
+ const char *model_id[MAX_INSTRUMENT_VERSIONS];
+ const char *model_name[MAX_INSTRUMENT_VERSIONS];
+ const uint8_t analog_channels;
+ const uint8_t digital_channels;
+ const uint8_t pods;
+
+ const char *(*analog_names)[];
+ const char *(*digital_names)[];
+
+ const int32_t (*hw_caps)[];
+ const uint8_t num_hwcaps;
+
+ const int32_t (*analog_hwcaps)[];
+ const uint8_t num_analog_hwcaps;
+
+ const char *(*coupling_options)[];
+ const uint8_t num_coupling_options;
+
+ const char *(*trigger_sources)[];
+ const uint8_t num_trigger_sources;
+
+ const char *(*trigger_slopes)[];
+
+ const uint64_t (*timebases)[][2];
+ const uint8_t num_timebases;
+
+ const uint64_t (*vdivs)[][2];
+ const uint8_t num_vdivs;
+
+ const uint8_t num_xdivs;
+ const uint8_t num_ydivs;
+
+ const char *(*scpi_dialect)[];
+};
+
+struct analog_channel_state {
+ int coupling;
+
+ int vdiv;
+ float vertical_offset, waveform_range, waveform_offset;
+
+ gboolean state;
+};
+
+struct scope_state {
+ struct analog_channel_state *analog_states;
+ gboolean *digital_states;
+ gboolean *pod_states;
+
+ int timebase;
+ float horiz_triggerpos;
+
+ int trigger_source;
+ int trigger_slope;
+ uint64_t sample_rate;
+};
+
+/** Private, per-device-instance driver context. */
+struct dev_context {
+ void *model_config;
+ void *model_state;
+
+ struct sr_channel_group **analog_groups;
+ struct sr_channel_group **digital_groups;
+
+ GSList *enabled_channels;
+ GSList *current_channel;
+ uint64_t num_frames;
+
+ uint64_t frame_limit;
+};
+
+/*--- api.c -----------------------------------------------------------------*/
+SR_PRIV int dlm_data_request(const struct sr_dev_inst *sdi);
+
+/*--- protocol.c ------------------------------------------------------------*/
+SR_PRIV int dlm_model_get(char *model_id, char **model_name, int *model_index);
+SR_PRIV int dlm_device_init(struct sr_dev_inst *sdi, int model_index);
+SR_PRIV int dlm_data_receive(int fd, int revents, void *cb_data);
+
+SR_PRIV void dlm_scope_state_destroy(struct scope_state *state);
+SR_PRIV int dlm_scope_state_query(struct sr_dev_inst *sdi);
+SR_PRIV int dlm_sample_rate_query(const struct sr_dev_inst *sdi);
#endif
#include "protocol_wrappers.h"
+#define MAX_COMMAND_SIZE 64
+
+/*
+ * DLM2000 comm spec:
+ * https://www.yokogawa.com/pdf/provide/E/GW/IM/0000022842/0/IM710105-17E.pdf
+ */
+
+int dlm_timebase_get(struct sr_scpi_dev_inst *scpi,
+ gchar **response)
+{
+ return sr_scpi_get_string(scpi, ":TIMEBASE:TDIV?", response);
+}
+
+int dlm_timebase_set(struct sr_scpi_dev_inst *scpi,
+ const gchar *value)
+{
+ gchar cmd[MAX_COMMAND_SIZE];
+ g_snprintf(cmd, sizeof(cmd), ":TIMEBASE:TDIV %s", value);
+ return sr_scpi_send(scpi, cmd);
+}
+
+int dlm_horiz_trigger_pos_get(struct sr_scpi_dev_inst *scpi,
+ float *response)
+{
+ return sr_scpi_get_float(scpi, ":TRIGGER:DELAY:TIME?", response);
+}
+
+int dlm_horiz_trigger_pos_set(struct sr_scpi_dev_inst *scpi,
+ const gchar *value)
+{
+ gchar cmd[MAX_COMMAND_SIZE];
+ g_snprintf(cmd, sizeof(cmd), ":TRIGGER:DELAY:TIME %s", value);
+ return sr_scpi_send(scpi, cmd);
+}
+
+int dlm_trigger_source_get(struct sr_scpi_dev_inst *scpi,
+ gchar **response)
+{
+ return sr_scpi_get_string(scpi, ":TRIGGER:ATRIGGER:SIMPLE:SOURCE?", response);
+}
+
+int dlm_trigger_source_set(struct sr_scpi_dev_inst *scpi,
+ const gchar *value)
+{
+ gchar cmd[MAX_COMMAND_SIZE];
+ g_snprintf(cmd, sizeof(cmd), ":TRIGGER:ATRIGGER:SIMPLE:SOURCE %s", value);
+ return sr_scpi_send(scpi, cmd);
+}
+
+int dlm_trigger_slope_get(struct sr_scpi_dev_inst *scpi,
+ int *response)
+{
+ gchar *resp;
+ int result;
+
+ result = SR_ERR;
+
+ if (sr_scpi_get_string(scpi, ":TRIGGER:ATRIGGER:SIMPLE:SLOPE?", &resp) != SR_OK) {
+ g_free(resp);
+ return SR_ERR;
+ }
+
+ if (strcmp("RISE", resp) == 0) {
+ *response = SLOPE_POSITIVE;
+ result = SR_OK;
+ }
+
+ if (strcmp("FALL", resp) == 0) {
+ *response = SLOPE_NEGATIVE;
+ result = SR_OK;
+ }
+
+ g_free(resp);
+ return result;
+}
+
+int dlm_trigger_slope_set(struct sr_scpi_dev_inst *scpi,
+ const int value)
+{
+ if (value == SLOPE_POSITIVE)
+ return sr_scpi_send(scpi, ":TRIGGER:ATRIGGER:SIMPLE:SLOPE RISE");
+
+ if (value == SLOPE_NEGATIVE)
+ return sr_scpi_send(scpi, ":TRIGGER:ATRIGGER:SIMPLE:SLOPE FALL");
+
+ return SR_ERR_ARG;
+}
+
+int dlm_analog_chan_state_get(struct sr_scpi_dev_inst *scpi, int channel,
+ gboolean *response)
+{
+ gchar cmd[MAX_COMMAND_SIZE];
+ g_snprintf(cmd, sizeof(cmd), ":CHANNEL%d:DISPLAY?", channel);
+ return sr_scpi_get_bool(scpi, cmd, response);
+}
+
+int dlm_analog_chan_state_set(struct sr_scpi_dev_inst *scpi, int channel,
+ const gboolean value)
+{
+ gchar cmd[MAX_COMMAND_SIZE];
+
+ if (value)
+ g_snprintf(cmd, sizeof(cmd), ":CHANNEL%d:DISPLAY ON", channel);
+ else
+ g_snprintf(cmd, sizeof(cmd), ":CHANNEL%d:DISPLAY OFF", channel);
+
+ return sr_scpi_send(scpi, cmd);
+}
+
+int dlm_analog_chan_vdiv_get(struct sr_scpi_dev_inst *scpi, int channel,
+ gchar **response)
+{
+ gchar cmd[MAX_COMMAND_SIZE];
+ g_snprintf(cmd, sizeof(cmd), ":CHANNEL%d:VDIV?", channel);
+ return sr_scpi_get_string(scpi, cmd, response);
+}
+
+int dlm_analog_chan_vdiv_set(struct sr_scpi_dev_inst *scpi, int channel,
+ const gchar *value)
+{
+ gchar cmd[MAX_COMMAND_SIZE];
+ g_snprintf(cmd, sizeof(cmd), ":CHANNEL%d:VDIV %s", channel, value);
+ return sr_scpi_send(scpi, cmd);
+}
+
+int dlm_analog_chan_voffs_get(struct sr_scpi_dev_inst *scpi, int channel,
+ float *response)
+{
+ gchar cmd[MAX_COMMAND_SIZE];
+ g_snprintf(cmd, sizeof(cmd), ":CHANNEL%d:POSITION?", channel);
+ return sr_scpi_get_float(scpi, cmd, response);
+}
+
+int dlm_analog_chan_srate_get(struct sr_scpi_dev_inst *scpi, int channel,
+ float *response)
+{
+ gchar cmd[MAX_COMMAND_SIZE];
+ g_snprintf(cmd, sizeof(cmd), ":WAVEFORM:TRACE %d", channel);
+
+ if (sr_scpi_send(scpi, cmd) != SR_OK)
+ return SR_ERR;
+
+ g_snprintf(cmd, sizeof(cmd), ":WAVEFORM:RECORD 0");
+ if (sr_scpi_send(scpi, cmd) != SR_OK)
+ return SR_ERR;
+
+ return sr_scpi_get_float(scpi, ":WAVEFORM:SRATE?", response);
+}
+
+int dlm_analog_chan_coupl_get(struct sr_scpi_dev_inst *scpi, int channel,
+ gchar **response)
+{
+ gchar cmd[MAX_COMMAND_SIZE];
+ g_snprintf(cmd, sizeof(cmd), ":CHANNEL%d:COUPLING?", channel);
+ return sr_scpi_get_string(scpi, cmd, response);
+}
+
+int dlm_analog_chan_coupl_set(struct sr_scpi_dev_inst *scpi, int channel,
+ const gchar *value)
+{
+ gchar cmd[MAX_COMMAND_SIZE];
+ g_snprintf(cmd, sizeof(cmd), ":CHANNEL%d:COUPLING %s", channel, value);
+ return sr_scpi_send(scpi, cmd);
+}
+
+int dlm_analog_chan_wrange_get(struct sr_scpi_dev_inst *scpi, int channel,
+ float *response)
+{
+ gchar cmd[MAX_COMMAND_SIZE];
+ int result;
+
+ g_snprintf(cmd, sizeof(cmd), ":WAVEFORM:TRACE %d", channel);
+ result = sr_scpi_send(scpi, cmd);
+ result &= sr_scpi_get_float(scpi, ":WAVEFORM:RANGE?", response);
+ return result;
+}
+
+int dlm_analog_chan_woffs_get(struct sr_scpi_dev_inst *scpi, int channel,
+ float *response)
+{
+ gchar cmd[MAX_COMMAND_SIZE];
+ int result;
+
+ g_snprintf(cmd, sizeof(cmd), ":WAVEFORM:TRACE %d", channel);
+ result = sr_scpi_send(scpi, cmd);
+ result &= sr_scpi_get_float(scpi, ":WAVEFORM:OFFSET?", response);
+ return result;
+}
+
+int dlm_digital_chan_state_get(struct sr_scpi_dev_inst *scpi, int channel,
+ gboolean *response)
+{
+ gchar cmd[MAX_COMMAND_SIZE];
+ g_snprintf(cmd, sizeof(cmd), ":LOGIC:PODA:BIT%d:DISPLAY?", channel);
+ return sr_scpi_get_bool(scpi, cmd, response);
+}
+
+int dlm_digital_chan_state_set(struct sr_scpi_dev_inst *scpi, int channel,
+ const gboolean value)
+{
+ gchar cmd[MAX_COMMAND_SIZE];
+
+ if (value)
+ g_snprintf(cmd, sizeof(cmd), ":LOGIC:PODA:BIT%d:DISPLAY ON", channel);
+ else
+ g_snprintf(cmd, sizeof(cmd), ":LOGIC:PODA:BIT%d:DISPLAY OFF", channel);
+
+ return sr_scpi_send(scpi, cmd);
+}
+
+int dlm_digital_pod_state_get(struct sr_scpi_dev_inst *scpi, int pod,
+ gboolean *response)
+{
+ gchar cmd[MAX_COMMAND_SIZE];
+
+ /* TODO: pod currently ignored as DLM2000 only has pod A. */
+ (void)pod;
+
+ g_snprintf(cmd, sizeof(cmd), ":LOGIC:MODE?");
+ return sr_scpi_get_bool(scpi, cmd, response);
+}
+
+int dlm_digital_pod_state_set(struct sr_scpi_dev_inst *scpi, int pod,
+ const gboolean value)
+{
+ /* TODO: pod currently ignored as DLM2000 only has pod A. */
+ (void)pod;
+
+ if (value)
+ return sr_scpi_send(scpi, ":LOGIC:MODE ON");
+ else
+ return sr_scpi_send(scpi, ":LOGIC:MODE OFF");
+}
+
+
+int dlm_response_headers_set(struct sr_scpi_dev_inst *scpi,
+ const gboolean value)
+{
+ if (value)
+ return sr_scpi_send(scpi, ":COMMUNICATE:HEADER ON");
+ else
+ return sr_scpi_send(scpi, ":COMMUNICATE:HEADER OFF");
+}
+
+int dlm_acquisition_stop(struct sr_scpi_dev_inst *scpi)
+{
+ return sr_scpi_send(scpi, ":STOP");
+}
+
+
+int dlm_acq_length_get(struct sr_scpi_dev_inst *scpi,
+ int *response)
+{
+ return sr_scpi_get_int(scpi, ":WAVEFORM:LENGTH?", response);
+}
+
+int dlm_chunks_per_acq_get(struct sr_scpi_dev_inst *scpi, int *response)
+{
+ int result, acq_len;
+
+ /* Data retrieval queries such as :WAVEFORM:SEND? will only return
+ * up to 12500 samples at a time. If the oscilloscope operates in a
+ * mode where more than 12500 samples fit on screen (i.e. in one
+ * acquisition), data needs to be retrieved multiple times.
+ */
+
+ result = sr_scpi_get_int(scpi, ":WAVEFORM:LENGTH?", &acq_len);
+ *response = MAX(acq_len / DLM_MAX_FRAME_LENGTH, 1);
+
+ return result;
+}
+
+int dlm_start_frame_set(struct sr_scpi_dev_inst *scpi, int value)
+{
+ gchar cmd[MAX_COMMAND_SIZE];
+
+ g_snprintf(cmd, sizeof(cmd), ":WAVEFORM:START %d",
+ value * DLM_MAX_FRAME_LENGTH);
+
+ return sr_scpi_send(scpi, cmd);
+}
+
+int dlm_data_get(struct sr_scpi_dev_inst *scpi, int acquisition_num)
+{
+ gchar cmd[MAX_COMMAND_SIZE];
+
+ g_snprintf(cmd, sizeof(cmd), ":WAVEFORM:ALL:SEND? %d", acquisition_num);
+ return sr_scpi_send(scpi, cmd);
+}
+
+int dlm_analog_data_get(struct sr_scpi_dev_inst *scpi, int channel)
+{
+ gchar cmd[MAX_COMMAND_SIZE];
+ int result;
+
+ result = sr_scpi_send(scpi, ":WAVEFORM:FORMAT BYTE");
+ if (result == SR_OK) result = sr_scpi_send(scpi, ":WAVEFORM:RECORD 0");
+ if (result == SR_OK) result = sr_scpi_send(scpi, ":WAVEFORM:START 0");
+ if (result == SR_OK) result = sr_scpi_send(scpi, ":WAVEFORM:END 124999999");
+
+ g_snprintf(cmd, sizeof(cmd), ":WAVEFORM:TRACE %d", channel);
+ if (result == SR_OK) result = sr_scpi_send(scpi, cmd);
+
+ if (result == SR_OK) result = sr_scpi_send(scpi, ":WAVEFORM:SEND? 1");
+
+ return result;
+}
+
+int dlm_digital_data_get(struct sr_scpi_dev_inst *scpi)
+{
+ int result;
+
+ result = sr_scpi_send(scpi, ":WAVEFORM:FORMAT BYTE");
+ if (result == SR_OK) result = sr_scpi_send(scpi, ":WAVEFORM:RECORD 0");
+ if (result == SR_OK) result = sr_scpi_send(scpi, ":WAVEFORM:START 0");
+ if (result == SR_OK) result = sr_scpi_send(scpi, ":WAVEFORM:END 124999999");
+ if (result == SR_OK) result = sr_scpi_send(scpi, ":WAVEFORM:TRACE LOGIC");
+ if (result == SR_OK) result = sr_scpi_send(scpi, ":WAVEFORM:SEND? 1");
+
+ return result;
+}
#include "libsigrok-internal.h"
#include "protocol.h"
+extern int dlm_timebase_get(struct sr_scpi_dev_inst *scpi,
+ gchar **response);
+extern int dlm_timebase_set(struct sr_scpi_dev_inst *scpi,
+ const gchar *value);
+extern int dlm_horiz_trigger_pos_get(struct sr_scpi_dev_inst *scpi,
+ float *response);
+extern int dlm_horiz_trigger_pos_set(struct sr_scpi_dev_inst *scpi,
+ const gchar *value);
+extern int dlm_trigger_source_get(struct sr_scpi_dev_inst *scpi,
+ gchar **response);
+extern int dlm_trigger_source_set(struct sr_scpi_dev_inst *scpi,
+ const gchar *value);
+extern int dlm_trigger_slope_get(struct sr_scpi_dev_inst *scpi,
+ int *value);
+extern int dlm_trigger_slope_set(struct sr_scpi_dev_inst *scpi,
+ const int value);
+
+extern int dlm_analog_chan_state_get(struct sr_scpi_dev_inst *scpi, int channel,
+ gboolean *response);
+extern int dlm_analog_chan_state_set(struct sr_scpi_dev_inst *scpi, int channel,
+ const gboolean value);
+extern int dlm_analog_chan_vdiv_get(struct sr_scpi_dev_inst *scpi, int channel,
+ gchar **response);
+extern int dlm_analog_chan_vdiv_set(struct sr_scpi_dev_inst *scpi, int channel,
+ const gchar *value);
+extern int dlm_analog_chan_voffs_get(struct sr_scpi_dev_inst *scpi, int channel,
+ float *response);
+extern int dlm_analog_chan_srate_get(struct sr_scpi_dev_inst *scpi, int channel,
+ float *response);
+extern int dlm_analog_chan_coupl_get(struct sr_scpi_dev_inst *scpi, int channel,
+ gchar **response);
+extern int dlm_analog_chan_coupl_set(struct sr_scpi_dev_inst *scpi, int channel,
+ const gchar *value);
+extern int dlm_analog_chan_wrange_get(struct sr_scpi_dev_inst *scpi, int channel,
+ float *response);
+extern int dlm_analog_chan_woffs_get(struct sr_scpi_dev_inst *scpi, int channel,
+ float *response);
+
+extern int dlm_digital_chan_state_get(struct sr_scpi_dev_inst *scpi, int channel,
+ gboolean *response);
+extern int dlm_digital_chan_state_set(struct sr_scpi_dev_inst *scpi, int channel,
+ const gboolean value);
+extern int dlm_digital_pod_state_get(struct sr_scpi_dev_inst *scpi, int pod,
+ gboolean *response);
+extern int dlm_digital_pod_state_set(struct sr_scpi_dev_inst *scpi, int pod,
+ const gboolean value);
+
+extern int dlm_response_headers_set(struct sr_scpi_dev_inst *scpi,
+ const gboolean value);
+extern int dlm_acquisition_stop(struct sr_scpi_dev_inst *scpi);
+
+extern int dlm_acq_length_get(struct sr_scpi_dev_inst *scpi,
+ int *response);
+extern int dlm_chunks_per_acq_get(struct sr_scpi_dev_inst *scpi,
+ int *response);
+extern int dlm_start_frame_set(struct sr_scpi_dev_inst *scpi, int value);
+extern int dlm_data_get(struct sr_scpi_dev_inst *scpi, int acquisition_num);
+extern int dlm_analog_data_get(struct sr_scpi_dev_inst *scpi, int channel);
+extern int dlm_digital_data_get(struct sr_scpi_dev_inst *scpi);
+
#endif