X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=hardware%2Frigol-ds1xx2%2Fapi.c;h=83323114fe412a467cdb45a6eafeceb1c979ef81;hb=c50277a6ec09fb35de3a6e0f2ae3401bc72a1526;hp=84273c94bfdf543413073a15742eb83b59340711;hpb=fb6e5ba8c7da4cc16eda03e3ffd0a97780655442;p=libsigrok.git diff --git a/hardware/rigol-ds1xx2/api.c b/hardware/rigol-ds1xx2/api.c index 84273c94..83323114 100644 --- a/hardware/rigol-ds1xx2/api.c +++ b/hardware/rigol-ds1xx2/api.c @@ -26,25 +26,18 @@ #include "libsigrok-internal.h" #include "protocol.h" -#define NUM_PROBES 2 - static const int hwcaps[] = { - SR_HWCAP_OSCILLOSCOPE, - SR_HWCAP_LIMIT_SAMPLES, - SR_HWCAP_TIMEBASE, - SR_HWCAP_TRIGGER_SOURCE, - SR_HWCAP_TRIGGER_SLOPE, - SR_HWCAP_HORIZ_TRIGGERPOS, - SR_HWCAP_VDIV, - SR_HWCAP_COUPLING, + 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, 0, }; -static const char *probe_names[] = { - "CH1", "CH2", - NULL, -}; - static const struct sr_rational timebases[] = { /* nanoseconds */ { 2, 1000000000 }, @@ -117,6 +110,13 @@ static const char *coupling[] = { NULL, }; +static const char *supported_models[] = { + "DS1052E", + "DS1102E", + "DS1052D", + "DS1102D" +}; + SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info; static struct sr_dev_driver *di = &rigol_ds1xx2_driver_info; @@ -138,6 +138,7 @@ static int clear_instances(void) continue; g_free(devc->device); + g_slist_free(devc->enabled_probes); close(devc->fd); sr_dev_inst_free(sdi); @@ -177,17 +178,14 @@ static GSList *hw_scan(GSList *options) const gchar *prefix = "usbtmc"; gchar *device; const gchar *idn_query = "*IDN?"; - gchar *idn_reply; - const gchar *idn_reply_prefix = "*IDN "; - int len; + int len, num_tokens, fd, i; const gchar *delimiter = ","; gchar **tokens; - int num_tokens; - int fd; + const char *manufacturer, *model, *version; + int num_models; + gboolean matched = FALSE; char buf[256]; - int i; - (void)options; devices = NULL; @@ -199,8 +197,7 @@ static GSList *hw_scan(GSList *options) if (dir == NULL) return NULL; - while ((dev_name = g_dir_read_name(dir)) != NULL) - { + while ((dev_name = g_dir_read_name(dir)) != NULL) { if (strncmp(dev_name, prefix, strlen(prefix))) continue; @@ -210,8 +207,7 @@ static GSList *hw_scan(GSList *options) len = write(fd, idn_query, strlen(idn_query)); len = read(fd, buf, sizeof(buf)); close(fd); - if (len == 0) - { + if (len == 0) { g_free(device); return NULL; } @@ -219,20 +215,46 @@ static GSList *hw_scan(GSList *options) buf[len] = 0; tokens = g_strsplit(buf, delimiter, 0); close(fd); + sr_dbg("response: %s %d [%s]", device, len, buf); for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++); - if (!(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, tokens[0], - num_tokens > 1 ? tokens[1] : NULL, num_tokens > 3 ? tokens[3] : NULL))) - { + if (num_tokens < 4) { g_strfreev(tokens); g_free(device); return NULL; } + + manufacturer = tokens[0]; + model = tokens[1]; + version = tokens[3]; + + if (strcmp(manufacturer, "Rigol Technologies")) { + g_strfreev(tokens); + g_free(device); + return NULL; + } + + num_models = sizeof(supported_models) / sizeof(supported_models[0]); + + for (i = 0; i < num_models; i++) { + if (!strcmp(model, supported_models[i])) { + matched = 1; + break; + } + } + + if (!matched || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, + manufacturer, model, version))) { + g_strfreev(tokens); + g_free(device); + return NULL; + } + g_strfreev(tokens); - if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) - { + if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { + sr_err("Device context malloc failed."); g_free(device); return NULL; } @@ -242,9 +264,9 @@ static GSList *hw_scan(GSList *options) sdi->priv = devc; sdi->driver = di; - for (i = 0; i < 2; i++) - { - if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, i == 0 ? "CH1" : "CH2"))) + for (i = 0; i < 2; i++) { + if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, + i == 0 ? "CH1" : "CH2"))) return NULL; sdi->probes = g_slist_append(sdi->probes, probe); } @@ -269,11 +291,12 @@ static GSList *hw_dev_list(void) static int hw_dev_open(struct sr_dev_inst *sdi) { - struct dev_context *devc = sdi->priv; + struct dev_context *devc; + int fd; - int fd = open(devc->device, O_RDWR); + devc = sdi->priv; - if (fd == -1) + if ((fd = open(devc->device, O_RDWR)) == -1) return SR_ERR; devc->fd = fd; @@ -285,7 +308,9 @@ static int hw_dev_open(struct sr_dev_inst *sdi) static int hw_dev_close(struct sr_dev_inst *sdi) { - struct dev_context *devc = sdi->priv; + struct dev_context *devc; + + devc = sdi->priv; close(devc->fd); @@ -299,74 +324,57 @@ static int hw_cleanup(void) return SR_OK; } -static int hw_info_get(int info_id, const void **data, - const struct sr_dev_inst *sdi) +static int config_get(int id, const void **data, const struct sr_dev_inst *sdi) { (void)sdi; - switch (info_id) { + switch (id) { case SR_DI_HWCAPS: *data = hwcaps; break; - case SR_DI_NUM_PROBES: - *data = GINT_TO_POINTER(NUM_PROBES); - break; - case SR_DI_PROBE_NAMES: - *data = probe_names; - break; - case SR_DI_TIMEBASES: - *data = timebases; - break; - case SR_DI_TRIGGER_SOURCES: - *data = trigger_sources; - break; - case SR_DI_VDIVS: - *data = vdivs; - break; - case SR_DI_COUPLING: - *data = coupling; - break; default: - sr_err("Unknown info_id: %d.", info_id); return SR_ERR_ARG; } return SR_OK; } -static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap, - const void *value) +static int config_set(int id, const void *value, const struct sr_dev_inst *sdi) { - struct dev_context *devc = sdi->priv; + struct dev_context *devc; uint64_t tmp_u64; float tmp_float; struct sr_rational tmp_rat; int ret, i, j; char *channel; + devc = sdi->priv; + if (sdi->status != SR_ST_ACTIVE) { sr_err("Device inactive, can't set config options."); return SR_ERR; } ret = SR_OK; - switch (hwcap) { - case SR_HWCAP_LIMIT_FRAMES: + switch (id) { + case SR_CONF_LIMIT_FRAMES: devc->limit_frames = *(const uint64_t *)value; break; - case SR_HWCAP_TRIGGER_SLOPE: + case SR_CONF_TRIGGER_SLOPE: tmp_u64 = *(const int *)value; - rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SLOP %s\n", tmp_u64 ? "POS" : "NEG"); + rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SLOP %s\n", + tmp_u64 ? "POS" : "NEG"); break; - case SR_HWCAP_HORIZ_TRIGGERPOS: + case SR_CONF_HORIZ_TRIGGERPOS: tmp_float = *(const float *)value; rigol_ds1xx2_send_data(devc->fd, ":TIM:OFFS %.9f\n", tmp_float); break; - case SR_HWCAP_TIMEBASE: + case SR_CONF_TIMEBASE: tmp_rat = *(const struct sr_rational *)value; - rigol_ds1xx2_send_data(devc->fd, ":TIM:SCAL %.9f\n", (float) tmp_rat.p / tmp_rat.q); + rigol_ds1xx2_send_data(devc->fd, ":TIM:SCAL %.9f\n", + (float)tmp_rat.p / tmp_rat.q); break; - case SR_HWCAP_TRIGGER_SOURCE: + case SR_CONF_TRIGGER_SOURCE: if (!strcmp(value, "CH1")) channel = "CHAN1"; else if (!strcmp(value, "CH2")) @@ -375,34 +383,35 @@ static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap, channel = "EXT"; else if (!strcmp(value, "AC Line")) channel = "ACL"; - else - { + else { ret = SR_ERR_ARG; break; } rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SOUR %s\n", channel); break; - case SR_HWCAP_VDIV: + case SR_CONF_VDIV: /* TODO: Not supporting vdiv per channel yet. */ tmp_rat = *(const struct sr_rational *)value; for (i = 0; vdivs[i].p && vdivs[i].q; i++) { if (vdivs[i].p == tmp_rat.p && vdivs[i].q == tmp_rat.q) { - devc->scale = (float) tmp_rat.p / tmp_rat.q; + devc->scale = (float)tmp_rat.p / tmp_rat.q; for (j = 0; j < 2; j++) - rigol_ds1xx2_send_data(devc->fd, ":CHAN%d:SCAL %.3f\n", j, devc->scale); + rigol_ds1xx2_send_data(devc->fd, + ":CHAN%d:SCAL %.3f\n", j, devc->scale); break; } } if (vdivs[i].p == 0 && vdivs[i].q == 0) ret = SR_ERR_ARG; break; - case SR_HWCAP_COUPLING: + case SR_CONF_COUPLING: /* TODO: Not supporting coupling per channel yet. */ for (i = 0; coupling[i]; i++) { if (!strcmp(value, coupling[i])) { for (j = 0; j < 2; j++) - rigol_ds1xx2_send_data(devc->fd, ":CHAN%d:COUP %s\n", j, coupling[i]); + rigol_ds1xx2_send_data(devc->fd, + ":CHAN%d:COUP %s\n", j, coupling[i]); break; } } @@ -410,24 +419,52 @@ static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap, ret = SR_ERR_ARG; break; default: - sr_err("Unknown hardware capability: %d.", hwcap); + sr_err("Unknown hardware capability: %d.", id); ret = SR_ERR_ARG; + break; } return ret; } +static int config_list(int key, const void **data, const struct sr_dev_inst *sdi) +{ + + (void)sdi; + + switch (key) { + case SR_CONF_COUPLING: + *data = coupling; + break; + case SR_CONF_VDIV: + *data = vdivs; + break; + case SR_CONF_TIMEBASE: + *data = timebases; + break; + case SR_CONF_TRIGGER_SOURCE: + *data = trigger_sources; + break; + default: + return SR_ERR_ARG; + } + + return SR_OK; +} + static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) { - struct dev_context *devc = sdi->priv; + struct dev_context *devc; struct sr_datafeed_packet packet; struct sr_datafeed_header header; - struct sr_datafeed_meta_analog meta; char buf[256]; int len; + (void)cb_data; + devc = sdi->priv; + devc->num_frames = 0; sr_source_add(devc->fd, G_IO_IN, 50, rigol_ds1xx2_receive_data, (void *)sdi); @@ -439,22 +476,18 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, gettimeofday(&header.starttime, NULL); sr_session_send(cb_data, &packet); - /* Send metadata about the SR_DF_ANALOG packets to come. */ - packet.type = SR_DF_META_ANALOG; - packet.payload = &meta; - meta.num_probes = NUM_PROBES; - sr_session_send(cb_data, &packet); - + /* 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); + 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); + sr_dbg("Offset is %.6f.", devc->offset); rigol_ds1xx2_send_data(devc->fd, ":WAV:DATA?\n"); return SR_OK; @@ -462,9 +495,12 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) { - struct dev_context *devc = sdi->priv; + struct dev_context *devc; + (void)cb_data; + devc = sdi->priv; + if (sdi->status != SR_ST_ACTIVE) { sr_err("Device inactive, can't stop acquisition."); return SR_ERR; @@ -484,10 +520,11 @@ SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info = { .scan = hw_scan, .dev_list = hw_dev_list, .dev_clear = clear_instances, + .config_get = config_get, + .config_set = config_set, + .config_list = config_list, .dev_open = hw_dev_open, .dev_close = hw_dev_close, - .info_get = hw_info_get, - .dev_config_set = hw_dev_config_set, .dev_acquisition_start = hw_dev_acquisition_start, .dev_acquisition_stop = hw_dev_acquisition_stop, .priv = NULL,