X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=hardware%2Frigol-ds1xx2%2Fapi.c;h=3abe0046621ead400878ee129867eceeb6246623;hb=250a78c7d5d049edd8c4f792f9eee56eae773a0b;hp=572df782d0ba2fd54029ca92c7da11de5c95cba2;hpb=f6a0ac9f6217f808d02f1c64a49c7ead3afb06e1;p=libsigrok.git diff --git a/hardware/rigol-ds1xx2/api.c b/hardware/rigol-ds1xx2/api.c index 572df782..3abe0046 100644 --- a/hardware/rigol-ds1xx2/api.c +++ b/hardware/rigol-ds1xx2/api.c @@ -2,6 +2,7 @@ * This file is part of the libsigrok project. * * Copyright (C) 2012 Martin Ling + * Copyright (C) 2013 Bert Vermeulen * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -26,15 +27,19 @@ #include "libsigrok-internal.h" #include "protocol.h" +#define NUM_TIMEBASE 12 +#define NUM_VDIV 8 + static const int32_t hwcaps[] = { SR_CONF_OSCILLOSCOPE, - SR_CONF_LIMIT_SAMPLES, SR_CONF_TIMEBASE, SR_CONF_TRIGGER_SOURCE, SR_CONF_TRIGGER_SLOPE, SR_CONF_HORIZ_TRIGGERPOS, SR_CONF_VDIV, SR_CONF_COUPLING, + SR_CONF_NUM_TIMEBASE, + SR_CONF_NUM_VDIV, }; static const uint64_t timebases[][2] = { @@ -97,6 +102,22 @@ static const char *trigger_sources[] = { "CH2", "EXT", "AC Line", + "D0", + "D1", + "D2", + "D3", + "D4", + "D5", + "D6", + "D7", + "D8", + "D9", + "D10", + "D11", + "D12", + "D13", + "D14", + "D15", }; static const char *coupling[] = { @@ -133,7 +154,10 @@ static int clear_instances(void) continue; g_free(devc->device); - g_slist_free(devc->enabled_probes); + g_free(devc->coupling[0]); + g_free(devc->coupling[1]); + g_free(devc->trigger_source); + g_free(devc->trigger_slope); close(devc->fd); sr_dev_inst_free(sdi); @@ -145,6 +169,28 @@ static int clear_instances(void) return SR_OK; } +static int set_cfg(const struct sr_dev_inst *sdi, const char *format, ...) +{ + struct dev_context *devc; + va_list args; + char buf[256]; + + devc = sdi->priv; + + va_start(args, format); + vsnprintf(buf, 255, format, args); + va_end(args); + if (rigol_ds1xx2_send(devc, buf) != SR_OK) + return SR_ERR; + + /* When setting a bunch of parameters in a row, the DS1052E scrambles + * some of them unless there is at least 100ms delay in between. */ + sr_spew("delay %dms", 100); + g_usleep(100); + + return SR_OK; +} + static int hw_init(struct sr_context *sr_ctx) { return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN); @@ -169,7 +215,9 @@ static GSList *hw_scan(GSList *options) gchar **tokens; const char *manufacturer, *model, *version; gboolean matched = FALSE; + gboolean has_digital = FALSE; char buf[256]; + gchar *channel_name; (void)options; @@ -224,6 +272,7 @@ static GSList *hw_scan(GSList *options) for (i = 0; i < ARRAY_SIZE(supported_models); i++) { if (!strcmp(model, supported_models[i])) { matched = 1; + has_digital = g_str_has_suffix(model, "D"); break; } } @@ -242,19 +291,31 @@ static GSList *hw_scan(GSList *options) g_free(device); return NULL; } - + devc->limit_frames = 0; devc->device = device; - + devc->has_digital = has_digital; sdi->priv = devc; sdi->driver = di; for (i = 0; i < 2; i++) { - if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, + if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE, i == 0 ? "CH1" : "CH2"))) return NULL; sdi->probes = g_slist_append(sdi->probes, probe); } + if (devc->has_digital) { + for (i = 0; i < 16; i++) { + if (!(channel_name = g_strdup_printf("D%d", i))) + return NULL; + probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, channel_name); + g_free(channel_name); + if (!probe) + return NULL; + sdi->probes = g_slist_append(sdi->probes, probe); + } + } + drvc->instances = g_slist_append(drvc->instances, sdi); devices = g_slist_append(devices, sdi); } @@ -278,10 +339,11 @@ static int hw_dev_open(struct sr_dev_inst *sdi) if ((fd = open(devc->device, O_RDWR)) == -1) return SR_ERR; - devc->fd = fd; - devc->scale = 1; + if (rigol_ds1xx2_get_dev_cfg(sdi) != SR_OK) + /* TODO: force configuration? */ + return SR_ERR; return SR_OK; } @@ -304,14 +366,33 @@ static int hw_cleanup(void) return SR_OK; } +static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi) +{ + + (void)sdi; + + switch (id) { + case SR_CONF_NUM_TIMEBASE: + *data = g_variant_new_int32(NUM_TIMEBASE); + break; + case SR_CONF_NUM_VDIV: + *data = g_variant_new_int32(NUM_VDIV); + break; + default: + return SR_ERR_NA; + } + + return SR_OK; +} + static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi) { struct dev_context *devc; uint64_t tmp_u64, p, q; - double tmp_double; + double t_dbl; unsigned int i; - int tmp_int, ret; - const char *tmp_str, *channel; + int ret; + const char *tmp_str; devc = sdi->priv; @@ -327,53 +408,63 @@ static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi) break; case SR_CONF_TRIGGER_SLOPE: tmp_u64 = g_variant_get_uint64(data); - rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SLOP %s\n", - tmp_u64 ? "POS" : "NEG"); + if (tmp_u64 != 0 && tmp_u64 != 1) + return SR_ERR; + g_free(devc->trigger_slope); + devc->trigger_slope = g_strdup(tmp_u64 ? "POS" : "NEG"); + ret = set_cfg(sdi, ":TRIG:EDGE:SLOP %s", devc->trigger_slope); break; case SR_CONF_HORIZ_TRIGGERPOS: - tmp_double = g_variant_get_double(data); - rigol_ds1xx2_send_data(devc->fd, ":TIM:OFFS %.9f\n", tmp_double); + t_dbl = g_variant_get_double(data); + if (t_dbl < 0.0 || t_dbl > 1.0) + return SR_ERR; + devc->horiz_triggerpos = t_dbl; + /* We have the trigger offset as a percentage of the frame, but + * need to express this in seconds. */ + t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * NUM_TIMEBASE; + ret = set_cfg(sdi, ":TIM:OFFS %.6f", t_dbl); break; case SR_CONF_TIMEBASE: g_variant_get(data, "(tt)", &p, &q); - tmp_int = -1; for (i = 0; i < ARRAY_SIZE(timebases); i++) { if (timebases[i][0] == p && timebases[i][1] == q) { - tmp_int = i; + devc->timebase = (float)p / q; + ret = set_cfg(sdi, ":TIM:SCAL %.9f", devc->timebase); break; } } - if (tmp_int >= 0) - rigol_ds1xx2_send_data(devc->fd, ":TIM:SCAL %.9f\n", - (float)timebases[i][0] / timebases[i][1]); + if (i == ARRAY_SIZE(timebases)) + ret = SR_ERR_ARG; break; case SR_CONF_TRIGGER_SOURCE: tmp_str = g_variant_get_string(data, NULL); - if (!strcmp(tmp_str, "CH1")) - channel = "CHAN1"; - else if (!strcmp(tmp_str, "CH2")) - channel = "CHAN2"; - else if (!strcmp(tmp_str, "EXT")) - channel = "EXT"; - else if (!strcmp(tmp_str, "AC Line")) - channel = "ACL"; - else { - ret = SR_ERR_ARG; - break; + for (i = 0; i < ARRAY_SIZE(trigger_sources); i++) { + if (!strcmp(trigger_sources[i], tmp_str)) { + g_free(devc->trigger_source); + devc->trigger_source = g_strdup(trigger_sources[i]); + if (!strcmp(devc->trigger_source, "AC Line")) + tmp_str = "ACL"; + else if (!strcmp(devc->trigger_source, "CH1")) + tmp_str = "CHAN1"; + else if (!strcmp(devc->trigger_source, "CH2")) + tmp_str = "CHAN2"; + else + tmp_str = (char *)devc->trigger_source; + ret = set_cfg(sdi, ":TRIG:EDGE:SOUR %s", tmp_str); + break; + } } - rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SOUR %s\n", channel); + if (i == ARRAY_SIZE(trigger_sources)) + ret = SR_ERR_ARG; break; case SR_CONF_VDIV: g_variant_get(data, "(tt)", &p, &q); - tmp_int = -1; for (i = 0; i < ARRAY_SIZE(vdivs); i++) { if (vdivs[i][0] != p || vdivs[i][1] != q) continue; - devc->scale = (float)vdivs[i][0] / vdivs[i][1]; - rigol_ds1xx2_send_data(devc->fd, ":CHAN0:SCAL %.3f\n", - devc->scale); - rigol_ds1xx2_send_data(devc->fd, ":CHAN1:SCAL %.3f\n", - devc->scale); + devc->vdiv[0] = devc->vdiv[1] = (float)p / q; + set_cfg(sdi, ":CHAN1:SCAL %.3f", devc->vdiv[0]); + ret = set_cfg(sdi, ":CHAN2:SCAL %.3f", devc->vdiv[1]); break; } if (i == ARRAY_SIZE(vdivs)) @@ -384,10 +475,12 @@ static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi) tmp_str = g_variant_get_string(data, NULL); for (i = 0; i < ARRAY_SIZE(coupling); i++) { if (!strcmp(tmp_str, coupling[i])) { - rigol_ds1xx2_send_data(devc->fd, ":CHAN0:COUP %s\n", - coupling[i]); - rigol_ds1xx2_send_data(devc->fd, ":CHAN1:COUP %s\n", - coupling[i]); + g_free(devc->coupling[0]); + g_free(devc->coupling[1]); + devc->coupling[0] = g_strdup(coupling[i]); + devc->coupling[1] = g_strdup(coupling[i]); + set_cfg(sdi, ":CHAN1:COUP %s", devc->coupling[0]); + ret = set_cfg(sdi, ":CHAN2:COUP %s", devc->coupling[1]); break; } } @@ -395,8 +488,7 @@ static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi) ret = SR_ERR_ARG; break; default: - sr_err("Unknown hardware capability: %d.", id); - ret = SR_ERR_ARG; + ret = SR_ERR_NA; break; } @@ -405,8 +497,10 @@ static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi) static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi) { - - (void)sdi; + GVariant *tuple, *rational[2]; + GVariantBuilder gvb; + unsigned int i; + struct dev_context *devc; switch (key) { case SR_CONF_DEVICE_OPTIONS: @@ -417,60 +511,104 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi) *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling)); break; case SR_CONF_VDIV: - *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64, - vdivs, ARRAY_SIZE(vdivs) * 2, sizeof(uint64_t)); + g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); + for (i = 0; i < ARRAY_SIZE(vdivs); i++) { + rational[0] = g_variant_new_uint64(vdivs[i][0]); + rational[1] = g_variant_new_uint64(vdivs[i][1]); + tuple = g_variant_new_tuple(rational, 2); + g_variant_builder_add_value(&gvb, tuple); + } + *data = g_variant_builder_end(&gvb); break; case SR_CONF_TIMEBASE: - *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64, - timebases, ARRAY_SIZE(timebases) * 2, sizeof(uint64_t)); + g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); + for (i = 0; i < ARRAY_SIZE(timebases); i++) { + rational[0] = g_variant_new_uint64(timebases[i][0]); + rational[1] = g_variant_new_uint64(timebases[i][1]); + tuple = g_variant_new_tuple(rational, 2); + g_variant_builder_add_value(&gvb, tuple); + } + *data = g_variant_builder_end(&gvb); break; case SR_CONF_TRIGGER_SOURCE: + if (!sdi || !sdi->priv) + /* Can't know this until we have the exact model. */ + return SR_ERR_ARG; + devc = sdi->priv; *data = g_variant_new_strv(trigger_sources, - ARRAY_SIZE(trigger_sources)); + devc->has_digital ? ARRAY_SIZE(trigger_sources) : 4); break; default: - return SR_ERR_ARG; + return SR_ERR_NA; } return SR_OK; } -static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, - void *cb_data) +static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) { struct dev_context *devc; - char buf[256]; - int len; + struct sr_probe *probe; + GSList *l; + char cmd[256]; (void)cb_data; devc = sdi->priv; - devc->num_frames = 0; + for (l = sdi->probes; l; l = l->next) { + probe = l->data; + sr_dbg("handling probe %s", probe->name); + if (probe->type == SR_PROBE_ANALOG) { + if (probe->enabled) + devc->enabled_analog_probes = g_slist_append( + devc->enabled_analog_probes, probe); + if (probe->enabled != devc->analog_channels[probe->index]) { + /* Enabled channel is currently disabled, or vice versa. */ + sprintf(cmd, ":CHAN%d:DISP %s", probe->index + 1, + probe->enabled ? "ON" : "OFF"); + if (rigol_ds1xx2_send(devc, cmd) != SR_OK) + return SR_ERR; + } + } else if (probe->type == SR_PROBE_LOGIC) { + if (probe->enabled) + devc->enabled_digital_probes = g_slist_append( + devc->enabled_digital_probes, probe); + if (probe->enabled != devc->digital_channels[probe->index]) { + /* Enabled channel is currently disabled, or vice versa. */ + sprintf(cmd, ":DIG%d:TURN %s", probe->index, + probe->enabled ? "ON" : "OFF"); + if (rigol_ds1xx2_send(devc, cmd) != SR_OK) + return SR_ERR; + } + } + } + if (!devc->enabled_analog_probes && !devc->enabled_digital_probes) + return SR_ERR; - sr_source_add(devc->fd, G_IO_IN, 50, rigol_ds1xx2_receive_data, (void *)sdi); + sr_source_add(devc->fd, G_IO_IN, 50, rigol_ds1xx2_receive, (void *)sdi); /* Send header packet to the session bus. */ std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN); - /* Hardcoded to CH1 only. */ - devc->enabled_probes = g_slist_append(NULL, sdi->probes->data); - rigol_ds1xx2_send_data(devc->fd, ":CHAN1:SCAL?\n"); - len = read(devc->fd, buf, sizeof(buf)); - buf[len] = 0; - devc->scale = atof(buf); - sr_dbg("Scale is %.3f.", devc->scale); - rigol_ds1xx2_send_data(devc->fd, ":CHAN1:OFFS?\n"); - len = read(devc->fd, buf, sizeof(buf)); - buf[len] = 0; - devc->offset = atof(buf); - sr_dbg("Offset is %.6f.", devc->offset); - rigol_ds1xx2_send_data(devc->fd, ":WAV:DATA?\n"); + /* Fetch the first frame. */ + if (devc->enabled_analog_probes) { + devc->channel_frame = devc->enabled_analog_probes->data; + if (rigol_ds1xx2_send(devc, ":WAV:DATA? CHAN%d", + devc->channel_frame->index + 1) != SR_OK) + return SR_ERR; + } else { + devc->channel_frame = devc->enabled_digital_probes->data; + if (rigol_ds1xx2_send(devc, ":WAV:DATA? DIG") != SR_OK) + return SR_ERR; + } + + devc->num_frame_bytes = 0; return SR_OK; } -static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) +static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) { struct dev_context *devc; @@ -483,6 +621,10 @@ static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) return SR_ERR; } + g_slist_free(devc->enabled_analog_probes); + g_slist_free(devc->enabled_digital_probes); + devc->enabled_analog_probes = NULL; + devc->enabled_digital_probes = NULL; sr_source_remove(devc->fd); return SR_OK; @@ -497,12 +639,12 @@ SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info = { .scan = hw_scan, .dev_list = hw_dev_list, .dev_clear = clear_instances, - .config_get = NULL, + .config_get = config_get, .config_set = config_set, .config_list = config_list, .dev_open = hw_dev_open, .dev_close = hw_dev_close, - .dev_acquisition_start = hw_dev_acquisition_start, - .dev_acquisition_stop = hw_dev_acquisition_stop, + .dev_acquisition_start = dev_acquisition_start, + .dev_acquisition_stop = dev_acquisition_stop, .priv = NULL, };