2 * This file is part of the libsigrok project.
4 * Copyright (C) 2012 Martin Ling <martin-git@earth.li>
5 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include "libsigrok.h"
29 #include "libsigrok-internal.h"
32 SR_PRIV int rigol_ds1xx2_receive(int fd, int revents, void *cb_data)
34 struct sr_dev_inst *sdi;
35 struct dev_context *devc;
36 struct sr_datafeed_packet packet;
37 struct sr_datafeed_analog analog;
38 struct sr_datafeed_logic logic;
39 unsigned char buf[DIGITAL_WAVEFORM_SIZE];
41 float data[ANALOG_WAVEFORM_SIZE];
42 int len, i, waveform_size;
43 struct sr_probe *probe;
48 if (!(devc = sdi->priv))
51 if (revents == G_IO_IN) {
52 probe = devc->channel_frame;
53 waveform_size = probe->type == SR_PROBE_ANALOG ?
54 ANALOG_WAVEFORM_SIZE : DIGITAL_WAVEFORM_SIZE;
55 len = read(fd, buf, waveform_size - devc->num_frame_bytes);
56 sr_dbg("Received %d bytes.", len);
60 if (devc->num_frame_bytes == 0) {
61 /* Start of a new frame. */
62 packet.type = SR_DF_FRAME_BEGIN;
63 sr_session_send(sdi, &packet);
66 if (probe->type == SR_PROBE_ANALOG) {
67 for (i = 0; i < len; i++) {
68 vdiv = devc->vdiv[probe->index];
69 offset = devc->vert_offset[probe->index];
70 data[i] = vdiv / 25.6 * (128 - buf[i]) - offset;
72 analog.probes = g_slist_append(NULL, probe);
73 analog.num_samples = len;
75 analog.mq = SR_MQ_VOLTAGE;
76 analog.unit = SR_UNIT_VOLT;
78 packet.type = SR_DF_ANALOG;
79 packet.payload = &analog;
80 sr_session_send(cb_data, &packet);
81 g_slist_free(analog.probes);
83 if (len != ANALOG_WAVEFORM_SIZE)
84 /* Don't have the whole frame yet. */
87 logic.length = len - 10;
89 logic.data = buf + 10;
90 packet.type = SR_DF_LOGIC;
91 packet.payload = &logic;
92 sr_session_send(cb_data, &packet);
94 if (len != DIGITAL_WAVEFORM_SIZE)
95 /* Don't have the whole frame yet. */
99 /* End of the frame. */
100 packet.type = SR_DF_FRAME_END;
101 sr_session_send(sdi, &packet);
102 devc->num_frame_bytes = 0;
104 if (devc->enabled_analog_probes
105 && devc->channel_frame == devc->enabled_analog_probes->data
106 && devc->enabled_analog_probes->next != NULL) {
107 /* We got the frame for the first analog channel, but
108 * there's a second analog channel. */
109 devc->channel_frame = devc->enabled_analog_probes->next->data;
110 rigol_ds1xx2_send(devc, ":WAV:DATA? CHAN%c",
111 devc->channel_frame->name[2]);
113 /* Done with both analog channels in this frame. */
114 if (devc->enabled_digital_probes
115 && devc->channel_frame != devc->enabled_digital_probes->data) {
116 /* Now we need to get the digital data. */
117 devc->channel_frame = devc->enabled_digital_probes->data;
118 rigol_ds1xx2_send(devc, ":WAV:DATA? DIG");
119 } else if (++devc->num_frames == devc->limit_frames) {
120 /* End of last frame. */
121 sdi->driver->dev_acquisition_stop(sdi, cb_data);
123 /* Get the next frame, starting with the first analog channel. */
124 if (devc->enabled_analog_probes) {
125 devc->channel_frame = devc->enabled_analog_probes->data;
126 rigol_ds1xx2_send(devc, ":WAV:DATA? CHAN%c",
127 devc->channel_frame->name[2]);
129 devc->channel_frame = devc->enabled_digital_probes->data;
130 rigol_ds1xx2_send(devc, ":WAV:DATA? DIG");
139 SR_PRIV int rigol_ds1xx2_send(struct dev_context *devc, const char *format, ...)
145 va_start(args, format);
146 len = vsnprintf(buf, 255, format, args);
150 out = write(devc->fd, buf, len);
153 sr_dbg("Only sent %d/%d bytes of '%s'.", out, len, buf);
156 sr_spew("Sent '%s'.", buf);
163 static int get_cfg(const struct sr_dev_inst *sdi, char *cmd, char *reply)
165 struct dev_context *devc;
170 if (rigol_ds1xx2_send(devc, cmd) != SR_OK)
173 if ((len = read(devc->fd, reply, 255)) < 0)
176 sr_spew("Received '%s'.", reply);
181 static int get_cfg_float(const struct sr_dev_inst *sdi, char *cmd, float *f)
185 if (get_cfg(sdi, cmd, buf) != SR_OK)
187 *f = strtof(buf, &e);
188 if (e == buf || (fpclassify(*f) & (FP_ZERO | FP_NORMAL)) == 0) {
189 sr_dbg("failed to parse response to '%s': '%s'", cmd, buf);
196 static int get_cfg_string(const struct sr_dev_inst *sdi, char *cmd, char **buf)
199 if (!(*buf = g_try_malloc0(256)))
200 return SR_ERR_MALLOC;
202 if (get_cfg(sdi, cmd, *buf) != SR_OK)
208 SR_PRIV int rigol_ds1xx2_get_dev_cfg(const struct sr_dev_inst *sdi)
210 struct dev_context *devc;
216 /* Analog channel state. */
217 if (get_cfg_string(sdi, ":CHAN1:DISP?", &t_s) != SR_OK)
219 devc->analog_channels[0] = !strcmp(t_s, "ON") ? TRUE : FALSE;
221 if (get_cfg_string(sdi, ":CHAN2:DISP?", &t_s) != SR_OK)
223 devc->analog_channels[1] = !strcmp(t_s, "ON") ? TRUE : FALSE;
225 sr_dbg("Current analog channel state CH1 %s CH2 %s",
226 devc->analog_channels[0] ? "on" : "off",
227 devc->analog_channels[1] ? "on" : "off");
229 /* Digital channel state. */
230 if (devc->has_digital) {
231 sr_dbg("Current digital channel state:");
232 for (i = 0; i < 16; i++) {
233 cmd = g_strdup_printf(":DIG%d:TURN?", i);
234 res = get_cfg_string(sdi, cmd, &t_s);
238 devc->digital_channels[i] = !strcmp(t_s, "ON") ? TRUE : FALSE;
240 sr_dbg("D%d: %s", i, devc->digital_channels[i] ? "on" : "off");
245 if (get_cfg_float(sdi, ":TIM:SCAL?", &devc->timebase) != SR_OK)
247 sr_dbg("Current timebase %f", devc->timebase);
250 if (get_cfg_float(sdi, ":CHAN1:SCAL?", &devc->vdiv[0]) != SR_OK)
252 if (get_cfg_float(sdi, ":CHAN2:SCAL?", &devc->vdiv[1]) != SR_OK)
254 sr_dbg("Current vertical gain CH1 %f CH2 %f", devc->vdiv[0], devc->vdiv[1]);
256 /* Vertical offset. */
257 if (get_cfg_float(sdi, ":CHAN1:OFFS?", &devc->vert_offset[0]) != SR_OK)
259 if (get_cfg_float(sdi, ":CHAN2:OFFS?", &devc->vert_offset[1]) != SR_OK)
261 sr_dbg("Current vertical offset CH1 %f CH2 %f", devc->vert_offset[0],
262 devc->vert_offset[1]);
265 if (get_cfg_string(sdi, ":CHAN1:COUP?", &devc->coupling[0]) != SR_OK)
267 if (get_cfg_string(sdi, ":CHAN2:COUP?", &devc->coupling[1]) != SR_OK)
269 sr_dbg("Current coupling CH1 %s CH2 %s", devc->coupling[0],
272 /* Trigger source. */
273 if (get_cfg_string(sdi, ":TRIG:EDGE:SOUR?", &devc->trigger_source) != SR_OK)
275 sr_dbg("Current trigger source %s", devc->trigger_source);
277 /* Horizontal trigger position. */
278 if (get_cfg_float(sdi, ":TIM:OFFS?", &devc->horiz_triggerpos) != SR_OK)
280 sr_dbg("Current horizontal trigger position %f", devc->horiz_triggerpos);
283 if (get_cfg_string(sdi, ":TRIG:EDGE:SLOP?", &devc->trigger_slope) != SR_OK)
285 sr_dbg("Current trigger slope %s", devc->trigger_slope);