X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=blobdiff_plain;f=src%2Fhardware%2Flecroy-xstream%2Fapi.c;h=8551351a11a2994270879e9b461b566e08881e70;hp=281a3871bf0050d74414fdc381770bf63d7bb15d;hb=HEAD;hpb=e66d1892d0dd16ed166fc8f13493f95de0607362 diff --git a/src/hardware/lecroy-xstream/api.c b/src/hardware/lecroy-xstream/api.c index 281a3871..8551351a 100644 --- a/src/hardware/lecroy-xstream/api.c +++ b/src/hardware/lecroy-xstream/api.c @@ -46,24 +46,13 @@ static const uint32_t devopts[] = { SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, }; -static const uint32_t analog_devopts[] = { +static const uint32_t devopts_cg_analog[] = { SR_CONF_NUM_VDIV | SR_CONF_GET, SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, }; -static int check_manufacturer(const char *manufacturer) -{ - unsigned int i; - - for (i = 0; i < ARRAY_SIZE(manufacturers); i++) - if (!strcmp(manufacturer, manufacturers[i])) - return SR_OK; - - return SR_ERR; -} - -static struct sr_dev_inst *probe_serial_device(struct sr_scpi_dev_inst *scpi) +static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi) { struct sr_dev_inst *sdi; struct dev_context *devc; @@ -78,7 +67,7 @@ static struct sr_dev_inst *probe_serial_device(struct sr_scpi_dev_inst *scpi) goto fail; } - if (check_manufacturer(hw_info->manufacturer) != SR_OK) + if (std_str_idx_s(hw_info->manufacturer, ARRAY_AND_SIZE(manufacturers)) < 0) goto fail; sdi = g_malloc0(sizeof(struct sr_dev_inst)); @@ -94,7 +83,6 @@ static struct sr_dev_inst *probe_serial_device(struct sr_scpi_dev_inst *scpi) hw_info = NULL; devc = g_malloc0(sizeof(struct dev_context)); - sdi->priv = devc; if (lecroy_xstream_init_device(sdi) != SR_OK) @@ -112,7 +100,7 @@ fail: static GSList *scan(struct sr_dev_driver *di, GSList *options) { - return sr_scpi_scan(di->context, options, probe_serial_device); + return sr_scpi_scan(di->context, options, probe_device); } static void clear_helper(struct dev_context *devc) @@ -143,9 +131,9 @@ static int dev_close(struct sr_dev_inst *sdi) } static int config_get(uint32_t key, GVariant **data, - const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) + const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { - unsigned int i; + int idx; struct dev_context *devc; const struct scope_config *model; struct scope_state *state; @@ -165,24 +153,20 @@ static int config_get(uint32_t key, GVariant **data, break; case SR_CONF_TIMEBASE: *data = g_variant_new("(tt)", - model->timebases[state->timebase].p, - model->timebases[state->timebase].q); + (*model->timebases)[state->timebase][0], + (*model->timebases)[state->timebase][1]); break; case SR_CONF_NUM_VDIV: - for (i = 0; i < model->analog_channels; i++) { - if (cg != devc->analog_groups[i]) - continue; - *data = g_variant_new_int32(model->num_ydivs); - } + if (std_cg_idx(cg, devc->analog_groups, model->analog_channels) < 0) + return SR_ERR_ARG; + *data = g_variant_new_int32(model->num_ydivs); break; case SR_CONF_VDIV: - for (i = 0; i < model->analog_channels; i++) { - if (cg != devc->analog_groups[i]) - continue; - *data = g_variant_new("(tt)", - model->vdivs[state->analog_channels[i].vdiv].p, - model->vdivs[state->analog_channels[i].vdiv].q); - } + if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0) + return SR_ERR_ARG; + *data = g_variant_new("(tt)", + (*model->vdivs)[state->analog_channels[idx].vdiv][0], + (*model->vdivs)[state->analog_channels[idx].vdiv][1]); break; case SR_CONF_TRIGGER_SOURCE: *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]); @@ -194,11 +178,9 @@ static int config_get(uint32_t key, GVariant **data, *data = g_variant_new_double(state->horiz_triggerpos); break; case SR_CONF_COUPLING: - 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_channels[i].coupling]); - } + if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0) + return SR_ERR_ARG; + *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[idx].coupling]); break; case SR_CONF_SAMPLERATE: *data = g_variant_new_uint64(state->sample_rate); @@ -213,39 +195,15 @@ static int config_get(uint32_t key, GVariant **data, return SR_OK; } -static GVariant *build_tuples(const struct sr_rational *array, 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].p); - rational[1] = g_variant_new_uint64(array[i].q); - - /* 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(uint32_t key, GVariant *data, - const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) + const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { - int ret; - unsigned int i, j; + int ret, idx, j; char command[MAX_COMMAND_SIZE]; struct dev_context *devc; const struct scope_config *model; struct scope_state *state; - const char *tmp; - int64_t p; - uint64_t q; double tmp_d; - gboolean update_sample_rate; if (!sdi) return SR_ERR_ARG; @@ -254,9 +212,6 @@ static int config_set(uint32_t key, GVariant *data, model = devc->model_config; state = devc->model_state; - update_sample_rate = FALSE; - - ret = SR_ERR_NA; switch (key) { case SR_CONF_LIMIT_FRAMES: @@ -264,58 +219,32 @@ static int config_set(uint32_t key, GVariant *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; - g_snprintf(command, sizeof(command), - "SET TRIGGER SOURCE %s", - (*model->trigger_sources)[i]); - - ret = sr_scpi_send(sdi->conn, command); - break; - } + if ((idx = std_str_idx(data, *model->trigger_sources, model->num_trigger_sources)) < 0) + return SR_ERR_ARG; + state->trigger_source = idx; + g_snprintf(command, sizeof(command), + "TRIG_SELECT EDGE,SR,%s", (*model->trigger_sources)[idx]); + ret = sr_scpi_send(sdi->conn, command); break; case SR_CONF_VDIV: - g_variant_get(data, "(tt)", &p, &q); - - for (i = 0; i < model->num_vdivs; i++) { - if (p != model->vdivs[i].p || q != model->vdivs[i].q) - continue; - for (j = 1; j <= model->analog_channels; j++) { - if (cg != devc->analog_groups[j - 1]) - continue; - state->analog_channels[j - 1].vdiv = i; - g_snprintf(command, sizeof(command), - "C%d:VDIV %E", j, (float)p/q); - - if (sr_scpi_send(sdi->conn, command) != SR_OK || - sr_scpi_get_opc(sdi->conn) != SR_OK) - return SR_ERR; - - break; - } - - ret = SR_OK; - break; - } + if ((idx = std_u64_tuple_idx(data, *model->vdivs, model->num_vdivs)) < 0) + return SR_ERR_ARG; + if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0) + return SR_ERR_ARG; + state->analog_channels[j].vdiv = idx; + g_snprintf(command, sizeof(command), + "C%d:VDIV %E", j + 1, (float) (*model->vdivs)[idx][0] / (*model->vdivs)[idx][1]); + if (sr_scpi_send(sdi->conn, command) != SR_OK || sr_scpi_get_opc(sdi->conn) != SR_OK) + return SR_ERR; + ret = SR_OK; 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].p || - q != model->timebases[i].q) - continue; - state->timebase = i; - g_snprintf(command, sizeof(command), - "TIME_DIV %E", (float)p/q); - - ret = sr_scpi_send(sdi->conn, command); - update_sample_rate = TRUE; - break; - } + if ((idx = std_u64_tuple_idx(data, *model->timebases, model->num_timebases)) < 0) + return SR_ERR_ARG; + state->timebase = idx; + g_snprintf(command, sizeof(command), + "TIME_DIV %E", (float) (*model->timebases)[idx][0] / (*model->timebases)[idx][1]); + ret = sr_scpi_send(sdi->conn, command); break; case SR_CONF_HORIZ_TRIGGERPOS: tmp_d = g_variant_get_double(data); @@ -325,51 +254,34 @@ static int config_set(uint32_t key, GVariant *data, state->horiz_triggerpos = tmp_d; tmp_d = -(tmp_d - 0.5) * - ((double)model->timebases[state->timebase].p / - model->timebases[state->timebase].q) - * model->num_xdivs; + ((double)(*model->timebases)[state->timebase][0] / + (*model->timebases)[state->timebase][1]) + * model->num_xdivs; g_snprintf(command, sizeof(command), "TRIG POS %e S", tmp_d); ret = sr_scpi_send(sdi->conn, command); break; case SR_CONF_TRIGGER_SLOPE: - tmp = g_variant_get_string(data, NULL); - for (i = 0; (*model->trigger_slopes)[i]; i++) { - if (g_strcmp0(tmp, (*model->trigger_slopes)[i]) != 0) - continue; - state->trigger_slope = i; - g_snprintf(command, sizeof(command), - "SET TRIGGER SLOPE %s", - (*model->trigger_slopes)[i]); - - ret = sr_scpi_send(sdi->conn, command); - break; - } + if ((idx = std_str_idx(data, *model->trigger_slopes, model->num_trigger_slopes)) < 0) + return SR_ERR_ARG; + state->trigger_slope = idx; + g_snprintf(command, sizeof(command), + "%s:TRIG_SLOPE %s", (*model->trigger_sources)[state->trigger_source], + (*model->trigger_slopes)[idx]); + ret = sr_scpi_send(sdi->conn, command); break; case SR_CONF_COUPLING: - 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_channels[j - 1].coupling = i; - - g_snprintf(command, sizeof(command), - "C%d:COUPLING %s", j, tmp); - - if (sr_scpi_send(sdi->conn, command) != SR_OK || - sr_scpi_get_opc(sdi->conn) != SR_OK) - return SR_ERR; - break; - } - - ret = SR_OK; - break; - } + if ((idx = std_str_idx(data, *model->coupling_options, model->num_coupling_options)) < 0) + return SR_ERR_ARG; + if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0) + return SR_ERR_ARG; + state->analog_channels[j].coupling = idx; + g_snprintf(command, sizeof(command), "C%d:COUPLING %s", + j + 1, (*model->coupling_options)[idx]); + if (sr_scpi_send(sdi->conn, command) != SR_OK || sr_scpi_get_opc(sdi->conn) != SR_OK) + return SR_ERR; + ret = SR_OK; break; default: ret = SR_ERR_NA; @@ -379,14 +291,21 @@ static int config_set(uint32_t key, GVariant *data, if (ret == SR_OK) ret = sr_scpi_get_opc(sdi->conn); - if (ret == SR_OK && update_sample_rate) - ret = lecroy_xstream_update_sample_rate(sdi); - return ret; } +static int config_channel_set(const struct sr_dev_inst *sdi, + struct sr_channel *ch, unsigned int changes) +{ + /* Currently we only handle SR_CHANNEL_SET_ENABLED. */ + if (changes != SR_CHANNEL_SET_ENABLED) + return SR_ERR_NA; + + return lecroy_xstream_channel_state_set(sdi, ch->index, ch->enabled); +} + static int config_list(uint32_t key, GVariant **data, - const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) + const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { struct dev_context *devc; const struct scope_config *model; @@ -396,43 +315,41 @@ static int config_list(uint32_t key, GVariant **data, switch (key) { case SR_CONF_SCAN_OPTIONS: - return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, NULL, NULL); + return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, NO_OPTS, NO_OPTS); case SR_CONF_DEVICE_OPTIONS: if (!cg) - return STD_CONFIG_LIST(key, data, sdi, cg, NULL, drvopts, devopts); - *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, - analog_devopts, ARRAY_SIZE(analog_devopts), - sizeof(uint32_t)); + return STD_CONFIG_LIST(key, data, sdi, cg, NO_OPTS, drvopts, devopts); + *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog)); break; case SR_CONF_COUPLING: - *data = g_variant_new_strv(*model->coupling_options, - g_strv_length((char **)*model->coupling_options)); + if (!model) + return SR_ERR_ARG; + *data = g_variant_new_strv(*model->coupling_options, model->num_coupling_options); break; case SR_CONF_TRIGGER_SOURCE: if (!model) return SR_ERR_ARG; - *data = g_variant_new_strv(*model->trigger_sources, - g_strv_length((char **)*model->trigger_sources)); + *data = g_variant_new_strv(*model->trigger_sources, model->num_trigger_sources); break; case SR_CONF_TRIGGER_SLOPE: if (!model) return SR_ERR_ARG; - *data = g_variant_new_strv(*model->trigger_slopes, - g_strv_length((char **)*model->trigger_slopes)); + *data = g_variant_new_strv(*model->trigger_slopes, model->num_trigger_slopes); break; case SR_CONF_TIMEBASE: if (!model) return SR_ERR_ARG; - *data = build_tuples(model->timebases, model->num_timebases); + *data = std_gvar_tuple_array(*model->timebases, model->num_timebases); break; case SR_CONF_VDIV: if (!model) return SR_ERR_ARG; - *data = build_tuples(model->vdivs, model->num_vdivs); + *data = std_gvar_tuple_array(*model->vdivs, model->num_vdivs); break; default: return SR_ERR_NA; } + return SR_OK; } @@ -444,20 +361,25 @@ SR_PRIV int lecroy_xstream_request_data(const struct sr_dev_inst *sdi) devc = sdi->priv; + /* + * We may be left with an invalid current_channel if acquisition was + * already stopped but we are processing the last pending events. + */ + if (!devc->current_channel) + return SR_ERR_NA; + ch = devc->current_channel->data; if (ch->type != SR_CHANNEL_ANALOG) return SR_ERR; - g_snprintf(command, sizeof(command), - "COMM_FORMAT DEF9,WORD,BIN;C%d:WAVEFORM?", ch->index + 1); + g_snprintf(command, sizeof(command), "C%d:WAVEFORM?", ch->index + 1); return sr_scpi_send(sdi->conn, command); } static int setup_channels(const struct sr_dev_inst *sdi) { GSList *l; - gboolean setup_changed; char command[MAX_COMMAND_SIZE]; struct scope_state *state; struct sr_channel *ch; @@ -467,7 +389,6 @@ static int setup_channels(const struct sr_dev_inst *sdi) devc = sdi->priv; scpi = sdi->conn; state = devc->model_state; - setup_changed = FALSE; for (l = sdi->channels; l; l = l->next) { ch = l->data; @@ -476,21 +397,18 @@ static int setup_channels(const struct sr_dev_inst *sdi) if (ch->enabled == state->analog_channels[ch->index].state) break; g_snprintf(command, sizeof(command), "C%d:TRACE %s", - ch->index + 1, ch->enabled ? "ON" : "OFF"); + ch->index + 1, ch->enabled ? "ON" : "OFF"); if (sr_scpi_send(scpi, command) != SR_OK) return SR_ERR; + state->analog_channels[ch->index].state = ch->enabled; - setup_changed = TRUE; break; default: return SR_ERR; } } - if (setup_changed && lecroy_xstream_update_sample_rate(sdi) != SR_OK) - return SR_ERR; - return SR_OK; } @@ -499,36 +417,32 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi) GSList *l; struct sr_channel *ch; struct dev_context *devc; + struct scope_state *state; int ret; struct sr_scpi_dev_inst *scpi; devc = sdi->priv; scpi = sdi->conn; + /* Preset empty results. */ g_slist_free(devc->enabled_channels); devc->enabled_channels = NULL; + state = devc->model_state; + state->sample_rate = 0; - /* - * Contruct the list of enabled channels. Determine the highest - * number of digital pods involved in the acquisition. - */ - + /* Contruct the list of enabled channels. */ for (l = sdi->channels; l; l = l->next) { ch = l->data; if (!ch->enabled) continue; - /* Only add a single digital channel per group (pod). */ - devc->enabled_channels = g_slist_append( - devc->enabled_channels, ch); + + devc->enabled_channels = g_slist_append(devc->enabled_channels, ch); } if (!devc->enabled_channels) return SR_ERR; - /* - * Configure the analog channels and the - * corresponding digital pods. - */ + /* Configure the analog channels. */ if (setup_channels(sdi) != SR_OK) { sr_err("Failed to setup channel configuration!"); ret = SR_ERR; @@ -584,6 +498,7 @@ static struct sr_dev_driver lecroy_xstream_driver_info = { .dev_clear = dev_clear, .config_get = config_get, .config_set = config_set, + .config_channel_set = config_channel_set, .config_list = config_list, .dev_open = dev_open, .dev_close = dev_close,