#include <unistd.h>
#include <stdlib.h>
#include <string.h>
+#include <time.h>
#include <glib.h>
#include "libsigrok.h"
#include "libsigrok-internal.h"
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);
return SR_OK;
}
+static int set_cfg(const struct sr_dev_inst *sdi, const char *format, ...)
+{
+ struct dev_context *devc;
+ struct timespec delay;
+ 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. */
+ delay.tv_sec = 0;
+ delay.tv_nsec = 100000000L;
+ sr_spew("delay %dms", delay.tv_nsec / 1000000);
+ nanosleep(&delay, NULL);
+
+ return SR_OK;
+}
+
static int hw_init(struct sr_context *sr_ctx)
{
return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN);
g_free(device);
return NULL;
}
-
+ devc->limit_frames = 0;
devc->device = device;
-
sdi->priv = devc;
sdi->driver = di;
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;
}
{
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;
break;
case SR_CONF_TRIGGER_SLOPE:
tmp_u64 = g_variant_get_uint64(data);
- rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SLOP %s",
- 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", 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",
- (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", 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, ":CHAN1:SCAL %.3f",
- 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))
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, ":CHAN1:COUP %s",
- coupling[i]);
- rigol_ds1xx2_send_data(devc->fd, ":CHAN2:COUP %s",
- 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;
}
}
hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
break;
case SR_CONF_COUPLING:
- *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
- break;
+ *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
+ break;
case SR_CONF_VDIV:
g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
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;
+ int probenum;
+ char cmd[256];
(void)cb_data;
devc = sdi->priv;
- devc->num_frames = devc->num_frame_samples = 0;
+ for (l = sdi->probes; l; l = l->next) {
+ probe = l->data;
+ probenum = probe->name[2] == '1' ? 0 : 1;
+ if (probe->enabled)
+ devc->enabled_probes = g_slist_append(devc->enabled_probes, probe);
+
+ if (probe->enabled != devc->channels[probenum]) {
+ /* Enabled channel is currently disabled, or vice versa. */
+ sprintf(cmd, ":CHAN%d:DISP %s", probenum + 1,
+ probe->enabled ? "ON" : "OFF");
+ if (rigol_ds1xx2_send(devc, cmd) != SR_OK)
+ return SR_ERR;
+ }
+ }
+ if (!devc->enabled_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?");
- 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?");
- 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?");
+ /* Fetch the first frame. */
+ devc->channel_frame = devc->enabled_probes->data;
+ if (rigol_ds1xx2_send(devc, ":WAV:DATA? CHAN%c",
+ devc->channel_frame->name[2]) != SR_OK)
+ return SR_ERR;
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;
return SR_ERR;
}
+ g_slist_free(devc->enabled_probes);
+ devc->enabled_probes = NULL;
sr_source_remove(devc->fd);
return SR_OK;
.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,
};
#include <unistd.h>
#include <errno.h>
#include <string.h>
+#include <math.h>
#include <glib.h>
#include "libsigrok.h"
#include "libsigrok-internal.h"
#include "protocol.h"
-SR_PRIV int rigol_ds1xx2_receive_data(int fd, int revents, void *cb_data)
+SR_PRIV int rigol_ds1xx2_receive(int fd, int revents, void *cb_data)
{
struct sr_dev_inst *sdi;
struct dev_context *devc;
struct sr_datafeed_packet packet;
struct sr_datafeed_analog analog;
unsigned char buf[WAVEFORM_SIZE];
+ double vdiv, offset;
float data[WAVEFORM_SIZE];
- int len, i;
+ int probenum, len, i;
if (!(sdi = cb_data))
return TRUE;
sr_session_send(sdi, &packet);
}
- for (i = 0; i < len; i++)
- data[i] = devc->scale / 25.6 * (128 - buf[i]) - devc->offset;
- analog.probes = devc->enabled_probes;
+ probenum = devc->channel_frame->name[2] == '1' ? 0 : 1;
+ for (i = 0; i < len; i++) {
+ vdiv = devc->vdiv[probenum];
+ offset = devc->vert_offset[probenum];
+ data[i] = vdiv / 25.6 * (128 - buf[i]) - offset;
+ }
+ analog.probes = g_slist_append(NULL, devc->channel_frame);
analog.num_samples = len;
analog.data = data;
analog.mq = SR_MQ_VOLTAGE;
packet.type = SR_DF_ANALOG;
packet.payload = &analog;
sr_session_send(cb_data, &packet);
+ g_slist_free(analog.probes);
- if (len == WAVEFORM_SIZE) {
- /* End of the frame. */
- packet.type = SR_DF_FRAME_END;
- sr_session_send(sdi, &packet);
+ if (len != WAVEFORM_SIZE)
+ /* Don't have the whole frame yet. */
+ return TRUE;
- if (++devc->num_frames == devc->limit_frames)
+ /* End of the frame. */
+ packet.type = SR_DF_FRAME_END;
+ sr_session_send(sdi, &packet);
+
+ if (devc->channel_frame == devc->enabled_probes->data
+ && devc->enabled_probes->next != NULL) {
+ /* We got the frame for the first channel, but
+ * there's a second channel. */
+ devc->channel_frame = devc->enabled_probes->next->data;
+ rigol_ds1xx2_send(devc, ":WAV:DATA? CHAN%c",
+ devc->channel_frame->name[2]);
+ } else {
+ /* Done with both channels in this frame. */
+ if (++devc->num_frames == devc->limit_frames) {
sdi->driver->dev_acquisition_stop(sdi, cb_data);
- else
- rigol_ds1xx2_send_data(fd, ":WAV:DATA?");
+ } else {
+ /* Get the next frame, starting with the first channel. */
+ devc->channel_frame = devc->enabled_probes->data;
+ rigol_ds1xx2_send(devc, ":WAV:DATA? CHAN%c",
+ devc->channel_frame->name[2]);
+ }
}
}
return TRUE;
}
-SR_PRIV int rigol_ds1xx2_send_data(int fd, const char *format, ...)
+SR_PRIV int rigol_ds1xx2_send(struct dev_context *devc, const char *format, ...)
{
va_list args;
char buf[256];
int len, out, ret;
va_start(args, format);
- len = vsprintf(buf, format, args);
+ len = vsnprintf(buf, 255, format, args);
va_end(args);
strcat(buf, "\n");
len++;
- out = write(fd, buf, len);
+ out = write(devc->fd, buf, len);
buf[len - 1] = '\0';
if (out != len) {
sr_dbg("Only sent %d/%d bytes of '%s'.", out, len, buf);
ret = SR_ERR;
} else {
- sr_dbg("Sent '%s'.", buf);
+ sr_spew("Sent '%s'.", buf);
ret = SR_OK;
}
return ret;
}
+
+static int get_cfg(const struct sr_dev_inst *sdi, char *cmd, char *reply)
+{
+ struct dev_context *devc;
+ int len;
+
+ devc = sdi->priv;
+
+ if (rigol_ds1xx2_send(devc, cmd) != SR_OK)
+ return SR_ERR;
+
+ if ((len = read(devc->fd, reply, 255)) < 0)
+ return SR_ERR;
+ reply[len] = '\0';
+ sr_spew("Received '%s'.", reply);
+
+ return SR_OK;
+}
+
+static int get_cfg_float(const struct sr_dev_inst *sdi, char *cmd, float *f)
+{
+ char buf[256], *e;
+
+ if (get_cfg(sdi, cmd, buf) != SR_OK)
+ return SR_ERR;
+ *f = strtof(buf, &e);
+ if (e == buf || (fpclassify(*f) & (FP_ZERO|FP_NORMAL)) == 0) {
+ sr_dbg("failed to parse response to '%s': '%s'", cmd, buf);
+ return SR_ERR;
+ }
+
+ return SR_OK;
+}
+
+static int get_cfg_string(const struct sr_dev_inst *sdi, char *cmd, char **buf)
+{
+
+ if (!(*buf = g_try_malloc0(256)))
+ return SR_ERR;
+
+ if (get_cfg(sdi, cmd, *buf) != SR_OK)
+ return SR_ERR;
+
+ return SR_OK;
+}
+
+SR_PRIV int rigol_ds1xx2_get_dev_cfg(const struct sr_dev_inst *sdi)
+{
+ struct dev_context *devc;
+ char *t_s;
+
+ devc = sdi->priv;
+
+ /* Channel state. */
+ if (get_cfg_string(sdi, ":CHAN1:DISP?", &t_s) != SR_OK)
+ return SR_ERR;
+ devc->channels[0] = !strcmp(t_s, "ON") ? TRUE : FALSE;
+ g_free(t_s);
+ if (get_cfg_string(sdi, ":CHAN2:DISP?", &t_s) != SR_OK)
+ return SR_ERR;
+ devc->channels[1] = !strcmp(t_s, "ON") ? TRUE : FALSE;
+ g_free(t_s);
+ sr_dbg("Current channel state CH1 %s CH2 %s",
+ devc->channels[0] ? "on" : "off",
+ devc->channels[1] ? "on" : "off");
+
+ /* Timebase. */
+ if (get_cfg_float(sdi, ":TIM:SCAL?", &devc->timebase) != SR_OK)
+ return SR_ERR;
+ sr_dbg("Current timebase %f", devc->timebase);
+
+ /* Vertical gain. */
+ if (get_cfg_float(sdi, ":CHAN1:SCAL?", &devc->vdiv[0]) != SR_OK)
+ return SR_ERR;
+ if (get_cfg_float(sdi, ":CHAN2:SCAL?", &devc->vdiv[1]) != SR_OK)
+ return SR_ERR;
+ sr_dbg("Current vertical gain CH1 %f CH2 %f", devc->vdiv[0], devc->vdiv[1]);
+
+ /* Vertical offset. */
+ if (get_cfg_float(sdi, ":CHAN1:OFFS?", &devc->vert_offset[0]) != SR_OK)
+ return SR_ERR;
+ if (get_cfg_float(sdi, ":CHAN2:OFFS?", &devc->vert_offset[1]) != SR_OK)
+ return SR_ERR;
+ sr_dbg("Current vertical offset CH1 %f CH2 %f", devc->vert_offset[0],
+ devc->vert_offset[1]);
+
+ /* Coupling. */
+ if (get_cfg_string(sdi, ":CHAN1:COUP?", &devc->coupling[0]) != SR_OK)
+ return SR_ERR;
+ if (get_cfg_string(sdi, ":CHAN2:COUP?", &devc->coupling[1]) != SR_OK)
+ return SR_ERR;
+ sr_dbg("Current coupling CH1 %s CH2 %s", devc->coupling[0],
+ devc->coupling[1]);
+
+ /* Trigger source. */
+ if (get_cfg_string(sdi, ":TRIG:EDGE:SOUR?", &devc->trigger_source) != SR_OK)
+ return SR_ERR;
+ sr_dbg("Current trigger source %s", devc->trigger_source);
+
+ /* Horizontal trigger position. */
+ if (get_cfg_float(sdi, ":TIM:OFFS?", &devc->horiz_triggerpos) != SR_OK)
+ return SR_ERR;
+ sr_dbg("Current horizontal trigger position %f", devc->horiz_triggerpos);
+
+ /* Trigger slope. */
+ if (get_cfg_string(sdi, ":TRIG:EDGE:SLOP?", &devc->trigger_slope) != SR_OK)
+ return SR_ERR;
+ sr_dbg("Current trigger slope %s", devc->trigger_slope);
+
+ return SR_OK;
+}
+