From: Martin Ling Date: Sun, 30 Dec 2012 03:17:56 +0000 (+0100) Subject: rigol-ds1xx2: First working version. X-Git-Tag: dsupstream~379 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=e0b7d23ce884f03ecb693943c5bd822879c68d65;p=libsigrok.git rigol-ds1xx2: First working version. Currently hardcoded to use /dev/usbtmc1. Analog data readout works. --- diff --git a/hardware/rigol-ds1xx2/api.c b/hardware/rigol-ds1xx2/api.c index 97d66256..ea2df6a3 100644 --- a/hardware/rigol-ds1xx2/api.c +++ b/hardware/rigol-ds1xx2/api.c @@ -17,11 +17,106 @@ * along with this program. If not, see . */ +#include +#include +#include +#include #include #include "libsigrok.h" #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, + 0, +}; + +static const char *probe_names[] = { + "CH1", "CH2", + NULL, +}; + +static const struct sr_rational timebases[] = { + /* nanoseconds */ + { 2, 1000000000 }, + { 5, 1000000000 }, + { 10, 1000000000 }, + { 20, 1000000000 }, + { 50, 1000000000 }, + { 100, 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 }, + { 0, 0}, +}; + +static const struct sr_rational vdivs[] = { + /* 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 }, + { 0, 0 }, +}; + +static const char *trigger_sources[] = { + "CH1", + "CH2", + "EXT", + "AC Line", + NULL, +}; + +static const char *coupling[] = { + "AC", + "DC", + "GND", + NULL, +}; + SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info; static struct sr_dev_driver *di = &rigol_ds1xx2_driver_info; @@ -42,7 +137,7 @@ static int clear_instances(void) if (!(devc = sdi->priv)) continue; - /* TODO */ + close(devc->fd); sr_dev_inst_free(sdi); } @@ -53,17 +148,16 @@ static int clear_instances(void) return SR_OK; } -static int hw_init(void) +static int hw_init(struct sr_context *sr_ctx) { struct drv_context *drvc; + (void)sr_ctx; if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) { sr_err("Driver context malloc failed."); return SR_ERR_MALLOC; } - /* TODO */ - di->priv = drvc; return SR_OK; @@ -72,7 +166,11 @@ static int hw_init(void) static GSList *hw_scan(GSList *options) { struct drv_context *drvc; + struct sr_dev_inst *sdi; + struct dev_context *devc; + struct sr_probe *probe; GSList *devices; + int i; (void)options; @@ -80,7 +178,22 @@ static GSList *hw_scan(GSList *options) drvc = di->priv; drvc->instances = NULL; - /* TODO */ + if (!(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, "Rigol", "DS1xx2", NULL))) + return NULL; + if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) + return NULL; + + 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"))) + return NULL; + sdi->probes = g_slist_append(sdi->probes, probe); + } + drvc->instances = g_slist_append(drvc->instances, sdi); + devices = g_slist_append(devices, sdi); return devices; } @@ -96,14 +209,24 @@ static GSList *hw_dev_list(void) static int hw_dev_open(struct sr_dev_inst *sdi) { - /* TODO */ + int fd = open("/dev/usbtmc1", O_RDWR); + + if (fd == -1) + return SR_ERR; + + struct dev_context *devc = sdi->priv; + devc->fd = fd; + + devc->scale = 1; return SR_OK; } static int hw_dev_close(struct sr_dev_inst *sdi) { - /* TODO */ + struct dev_context *devc = sdi->priv; + + close(devc->fd); return SR_OK; } @@ -112,16 +235,36 @@ static int hw_cleanup(void) { clear_instances(); - /* TODO */ - return SR_OK; } static int hw_info_get(int info_id, const void **data, const struct sr_dev_inst *sdi) { + (void)sdi; + switch (info_id) { - /* TODO */ + 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; @@ -133,7 +276,12 @@ static int hw_info_get(int info_id, const void **data, static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap, const void *value) { - int ret; + struct dev_context *devc = sdi->priv; + uint64_t tmp_u64; + float tmp_float; + struct sr_rational tmp_rat; + int ret, i, j; + char *channel; if (sdi->status != SR_ST_ACTIVE) { sr_err("Device inactive, can't set config options."); @@ -142,7 +290,62 @@ static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap, ret = SR_OK; switch (hwcap) { - /* TODO */ + case SR_HWCAP_LIMIT_FRAMES: + devc->limit_frames = *(const uint64_t *)value; + break; + case SR_HWCAP_TRIGGER_SLOPE: + tmp_u64 = *(const int *)value; + rigol_ds1xx2_send_data(devc->fd, "TRIG:EDGE:%s\n", tmp_u64 ? "POS" : "NEG"); + break; + case SR_HWCAP_HORIZ_TRIGGERPOS: + tmp_float = *(const float *)value; + rigol_ds1xx2_send_data(devc->fd, "TIM:OFFS %f\n", tmp_float); + break; + case SR_HWCAP_TIMEBASE: + tmp_rat = *(const struct sr_rational *)value; + rigol_ds1xx2_send_data(devc->fd, "TIM:SCAL %f\n", (float) tmp_rat.p / tmp_rat.q); + break; + case SR_HWCAP_TRIGGER_SOURCE: + if (!strcmp(value, "CH1")) + channel = "CHAN1"; + else if (!strcmp(value, "CH2")) + channel = "CHAN2"; + else if (!strcmp(value, "EXT")) + channel = "EXT"; + else if (!strcmp(value, "AC Line")) + channel = "ACL"; + else + ret = SR_ERR_ARG; + break; + rigol_ds1xx2_send_data(devc->fd, "TRIG:SOUR %s\n", channel); + break; + case SR_HWCAP_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; + for (j = 0; j < 2; j++) + rigol_ds1xx2_send_data(devc->fd, "CHAN%d:SCAL %f\n", j, devc->scale); + break; + } + } + if (vdivs[i].p == 0 && vdivs[i].q == 0) + ret = SR_ERR_ARG; + break; + case SR_HWCAP_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]); + break; + } + } + if (coupling[i] == 0) + ret = SR_ERR_ARG; + break; default: sr_err("Unknown hardware capability: %d.", hwcap); ret = SR_ERR_ARG; @@ -154,13 +357,49 @@ static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap, static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) { - /* TODO */ + struct dev_context *devc = sdi->priv; + 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->num_frames = 0; + + sr_source_add(devc->fd, G_IO_IN, 50, rigol_ds1xx2_receive_data, (void *)sdi); + + /* Send header packet to the session bus. */ + packet.type = SR_DF_HEADER; + packet.payload = (unsigned char *)&header; + header.feed_version = 1; + 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); + + 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 %f", 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 %f", devc->offset); + rigol_ds1xx2_send_data(devc->fd, ":WAV:DATA?\n"); return SR_OK; } static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) { + struct dev_context *devc = sdi->priv; (void)cb_data; if (sdi->status != SR_ST_ACTIVE) { @@ -168,7 +407,7 @@ static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) return SR_ERR; } - /* TODO */ + sr_source_remove(devc->fd); return SR_OK; } diff --git a/hardware/rigol-ds1xx2/protocol.c b/hardware/rigol-ds1xx2/protocol.c index e03bc1fc..d8b0de72 100644 --- a/hardware/rigol-ds1xx2/protocol.c +++ b/hardware/rigol-ds1xx2/protocol.c @@ -18,6 +18,9 @@ */ #include +#include +#include +#include #include #include "libsigrok.h" #include "libsigrok-internal.h" @@ -25,8 +28,13 @@ SR_PRIV int rigol_ds1xx2_receive_data(int fd, int revents, void *cb_data) { - const struct sr_dev_inst *sdi; + struct sr_dev_inst *sdi; struct dev_context *devc; + struct sr_datafeed_packet packet; + struct sr_datafeed_analog analog; + unsigned char buf[WAVEFORM_SIZE]; + float data[WAVEFORM_SIZE]; + int len, i; if (!(sdi = cb_data)) return TRUE; @@ -35,8 +43,37 @@ SR_PRIV int rigol_ds1xx2_receive_data(int fd, int revents, void *cb_data) return TRUE; if (revents == G_IO_IN) { - /* TODO */ + len = read(fd, buf, WAVEFORM_SIZE); + sr_dbg("received %d", len); + if (len == -1) + return TRUE; + for (i = 0; i < len; i++) + data[i] = devc->scale / 25.6 * (128 - buf[i]) - devc->offset; + analog.num_samples = len; + analog.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(cb_data, &packet); + if (++devc->num_frames == devc->limit_frames) + sdi->driver->dev_acquisition_stop(sdi, cb_data); + else + rigol_ds1xx2_send_data(fd, ":WAV:DATA?\n"); } return TRUE; } + +SR_PRIV int rigol_ds1xx2_send_data(int fd, const char *format, ...) +{ + va_list args; + char buf[256]; + va_start(args, format); + int len = vsprintf(buf, format, args); + va_end(args); + len = write(fd, buf, len); + sr_dbg("sent %s", buf); + return len; +} diff --git a/hardware/rigol-ds1xx2/protocol.h b/hardware/rigol-ds1xx2/protocol.h index 0a5f14c3..547dfc46 100644 --- a/hardware/rigol-ds1xx2/protocol.h +++ b/hardware/rigol-ds1xx2/protocol.h @@ -33,8 +33,13 @@ #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args) #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args) +#define WAVEFORM_SIZE 600 + /** Private, per-device-instance driver context. */ struct dev_context { + /** The current frame limit */ + uint64_t limit_frames; + /** The current sampling limit (in number of samples). */ uint64_t limit_samples; @@ -44,10 +49,24 @@ struct dev_context { /** Opaque pointer passed in by the frontend. */ void *cb_data; + /** The current number of already received frames. */ + uint64_t num_frames; + /** The current number of already received samples. */ uint64_t num_samples; + + /** Current scale setting. */ + float scale; + + /** Current offset setting. */ + float offset; + + /** USBTMC character device file descriptor. */ + int fd; }; SR_PRIV int rigol_ds1xx2_receive_data(int fd, int revents, void *cb_data); +SR_PRIV int rigol_ds1xx2_send_data(int fd, const char *format, ...); + #endif