]>
Commit | Line | Data |
---|---|---|
f4816ac6 ML |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2012 Martin Ling <martin-git@earth.li> | |
88e429c9 | 5 | * Copyright (C) 2013 Bert Vermeulen <bert@biot.com> |
f4816ac6 ML |
6 | * |
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. | |
11 | * | |
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. | |
16 | * | |
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/>. | |
19 | */ | |
20 | ||
21 | #include <stdlib.h> | |
e0b7d23c ML |
22 | #include <stdarg.h> |
23 | #include <unistd.h> | |
24 | #include <errno.h> | |
a3df166f | 25 | #include <string.h> |
254dd102 | 26 | #include <math.h> |
f4816ac6 ML |
27 | #include <glib.h> |
28 | #include "libsigrok.h" | |
29 | #include "libsigrok-internal.h" | |
30 | #include "protocol.h" | |
31 | ||
254dd102 | 32 | SR_PRIV int rigol_ds1xx2_receive(int fd, int revents, void *cb_data) |
f4816ac6 | 33 | { |
e0b7d23c | 34 | struct sr_dev_inst *sdi; |
9bd4c956 | 35 | struct sr_serial_dev_inst *serial; |
f4816ac6 | 36 | struct dev_context *devc; |
e0b7d23c ML |
37 | struct sr_datafeed_packet packet; |
38 | struct sr_datafeed_analog analog; | |
6bb192bc ML |
39 | struct sr_datafeed_logic logic; |
40 | unsigned char buf[DIGITAL_WAVEFORM_SIZE]; | |
254dd102 | 41 | double vdiv, offset; |
6bb192bc ML |
42 | float data[ANALOG_WAVEFORM_SIZE]; |
43 | int len, i, waveform_size; | |
44 | struct sr_probe *probe; | |
f4816ac6 | 45 | |
9bd4c956 ML |
46 | (void) fd; |
47 | ||
f4816ac6 ML |
48 | if (!(sdi = cb_data)) |
49 | return TRUE; | |
50 | ||
51 | if (!(devc = sdi->priv)) | |
52 | return TRUE; | |
53 | ||
9bd4c956 ML |
54 | serial = sdi->conn; |
55 | ||
f4816ac6 | 56 | if (revents == G_IO_IN) { |
6bb192bc ML |
57 | probe = devc->channel_frame; |
58 | waveform_size = probe->type == SR_PROBE_ANALOG ? | |
59 | ANALOG_WAVEFORM_SIZE : DIGITAL_WAVEFORM_SIZE; | |
9bd4c956 | 60 | len = serial_read(serial, buf, waveform_size - devc->num_frame_bytes); |
29d957ce | 61 | sr_dbg("Received %d bytes.", len); |
e0b7d23c ML |
62 | if (len == -1) |
63 | return TRUE; | |
75d8a4e5 | 64 | |
ee7e9bee | 65 | if (devc->num_frame_bytes == 0) { |
75d8a4e5 BV |
66 | /* Start of a new frame. */ |
67 | packet.type = SR_DF_FRAME_BEGIN; | |
68 | sr_session_send(sdi, &packet); | |
69 | } | |
70 | ||
6bb192bc ML |
71 | if (probe->type == SR_PROBE_ANALOG) { |
72 | for (i = 0; i < len; i++) { | |
73 | vdiv = devc->vdiv[probe->index]; | |
74 | offset = devc->vert_offset[probe->index]; | |
75 | data[i] = vdiv / 25.6 * (128 - buf[i]) - offset; | |
76 | } | |
77 | analog.probes = g_slist_append(NULL, probe); | |
78 | analog.num_samples = len; | |
79 | analog.data = data; | |
80 | analog.mq = SR_MQ_VOLTAGE; | |
81 | analog.unit = SR_UNIT_VOLT; | |
82 | analog.mqflags = 0; | |
83 | packet.type = SR_DF_ANALOG; | |
84 | packet.payload = &analog; | |
85 | sr_session_send(cb_data, &packet); | |
86 | g_slist_free(analog.probes); | |
87 | ||
88 | if (len != ANALOG_WAVEFORM_SIZE) | |
89 | /* Don't have the whole frame yet. */ | |
90 | return TRUE; | |
91 | } else { | |
92 | logic.length = len - 10; | |
93 | logic.unitsize = 2; | |
94 | logic.data = buf + 10; | |
95 | packet.type = SR_DF_LOGIC; | |
96 | packet.payload = &logic; | |
97 | sr_session_send(cb_data, &packet); | |
98 | ||
99 | if (len != DIGITAL_WAVEFORM_SIZE) | |
100 | /* Don't have the whole frame yet. */ | |
101 | return TRUE; | |
ee7e9bee | 102 | } |
75d8a4e5 | 103 | |
254dd102 BV |
104 | /* End of the frame. */ |
105 | packet.type = SR_DF_FRAME_END; | |
106 | sr_session_send(sdi, &packet); | |
ee7e9bee | 107 | devc->num_frame_bytes = 0; |
254dd102 | 108 | |
6bb192bc ML |
109 | if (devc->enabled_analog_probes |
110 | && devc->channel_frame == devc->enabled_analog_probes->data | |
111 | && devc->enabled_analog_probes->next != NULL) { | |
112 | /* We got the frame for the first analog channel, but | |
113 | * there's a second analog channel. */ | |
114 | devc->channel_frame = devc->enabled_analog_probes->next->data; | |
9bd4c956 | 115 | rigol_ds1xx2_send(sdi, ":WAV:DATA? CHAN%c", |
254dd102 BV |
116 | devc->channel_frame->name[2]); |
117 | } else { | |
6bb192bc ML |
118 | /* Done with both analog channels in this frame. */ |
119 | if (devc->enabled_digital_probes | |
120 | && devc->channel_frame != devc->enabled_digital_probes->data) { | |
121 | /* Now we need to get the digital data. */ | |
122 | devc->channel_frame = devc->enabled_digital_probes->data; | |
9bd4c956 | 123 | rigol_ds1xx2_send(sdi, ":WAV:DATA? DIG"); |
6bb192bc ML |
124 | } else if (++devc->num_frames == devc->limit_frames) { |
125 | /* End of last frame. */ | |
75d8a4e5 | 126 | sdi->driver->dev_acquisition_stop(sdi, cb_data); |
254dd102 | 127 | } else { |
6bb192bc ML |
128 | /* Get the next frame, starting with the first analog channel. */ |
129 | if (devc->enabled_analog_probes) { | |
130 | devc->channel_frame = devc->enabled_analog_probes->data; | |
9bd4c956 | 131 | rigol_ds1xx2_send(sdi, ":WAV:DATA? CHAN%c", |
6bb192bc ML |
132 | devc->channel_frame->name[2]); |
133 | } else { | |
134 | devc->channel_frame = devc->enabled_digital_probes->data; | |
9bd4c956 | 135 | rigol_ds1xx2_send(sdi, ":WAV:DATA? DIG"); |
6bb192bc | 136 | } |
254dd102 | 137 | } |
75d8a4e5 | 138 | } |
f4816ac6 ML |
139 | } |
140 | ||
141 | return TRUE; | |
142 | } | |
e0b7d23c | 143 | |
9bd4c956 | 144 | SR_PRIV int rigol_ds1xx2_send(const struct sr_dev_inst *sdi, const char *format, ...) |
e0b7d23c ML |
145 | { |
146 | va_list args; | |
147 | char buf[256]; | |
a3df166f | 148 | int len, out, ret; |
29d957ce | 149 | |
e0b7d23c | 150 | va_start(args, format); |
254dd102 | 151 | len = vsnprintf(buf, 255, format, args); |
e0b7d23c | 152 | va_end(args); |
a3df166f BV |
153 | strcat(buf, "\n"); |
154 | len++; | |
9bd4c956 | 155 | out = serial_write(sdi->conn, buf, len); |
a3df166f BV |
156 | buf[len - 1] = '\0'; |
157 | if (out != len) { | |
158 | sr_dbg("Only sent %d/%d bytes of '%s'.", out, len, buf); | |
159 | ret = SR_ERR; | |
160 | } else { | |
254dd102 | 161 | sr_spew("Sent '%s'.", buf); |
a3df166f BV |
162 | ret = SR_OK; |
163 | } | |
29d957ce | 164 | |
a3df166f | 165 | return ret; |
e0b7d23c | 166 | } |
254dd102 BV |
167 | |
168 | static int get_cfg(const struct sr_dev_inst *sdi, char *cmd, char *reply) | |
169 | { | |
254dd102 BV |
170 | int len; |
171 | ||
9bd4c956 | 172 | if (rigol_ds1xx2_send(sdi, cmd) != SR_OK) |
254dd102 BV |
173 | return SR_ERR; |
174 | ||
9bd4c956 | 175 | if ((len = serial_read(sdi->conn, reply, 255)) < 0) |
254dd102 BV |
176 | return SR_ERR; |
177 | reply[len] = '\0'; | |
178 | sr_spew("Received '%s'.", reply); | |
179 | ||
180 | return SR_OK; | |
181 | } | |
182 | ||
183 | static int get_cfg_float(const struct sr_dev_inst *sdi, char *cmd, float *f) | |
184 | { | |
185 | char buf[256], *e; | |
186 | ||
187 | if (get_cfg(sdi, cmd, buf) != SR_OK) | |
188 | return SR_ERR; | |
189 | *f = strtof(buf, &e); | |
169dbe85 | 190 | if (e == buf || (fpclassify(*f) & (FP_ZERO | FP_NORMAL)) == 0) { |
254dd102 BV |
191 | sr_dbg("failed to parse response to '%s': '%s'", cmd, buf); |
192 | return SR_ERR; | |
193 | } | |
194 | ||
195 | return SR_OK; | |
196 | } | |
197 | ||
198 | static int get_cfg_string(const struct sr_dev_inst *sdi, char *cmd, char **buf) | |
199 | { | |
200 | ||
201 | if (!(*buf = g_try_malloc0(256))) | |
169dbe85 | 202 | return SR_ERR_MALLOC; |
254dd102 BV |
203 | |
204 | if (get_cfg(sdi, cmd, *buf) != SR_OK) | |
205 | return SR_ERR; | |
206 | ||
207 | return SR_OK; | |
208 | } | |
209 | ||
210 | SR_PRIV int rigol_ds1xx2_get_dev_cfg(const struct sr_dev_inst *sdi) | |
211 | { | |
212 | struct dev_context *devc; | |
6bb192bc ML |
213 | char *t_s, *cmd; |
214 | int i, res; | |
254dd102 BV |
215 | |
216 | devc = sdi->priv; | |
217 | ||
6bb192bc | 218 | /* Analog channel state. */ |
254dd102 BV |
219 | if (get_cfg_string(sdi, ":CHAN1:DISP?", &t_s) != SR_OK) |
220 | return SR_ERR; | |
6bb192bc | 221 | devc->analog_channels[0] = !strcmp(t_s, "ON") ? TRUE : FALSE; |
254dd102 BV |
222 | g_free(t_s); |
223 | if (get_cfg_string(sdi, ":CHAN2:DISP?", &t_s) != SR_OK) | |
224 | return SR_ERR; | |
6bb192bc | 225 | devc->analog_channels[1] = !strcmp(t_s, "ON") ? TRUE : FALSE; |
254dd102 | 226 | g_free(t_s); |
6bb192bc ML |
227 | sr_dbg("Current analog channel state CH1 %s CH2 %s", |
228 | devc->analog_channels[0] ? "on" : "off", | |
229 | devc->analog_channels[1] ? "on" : "off"); | |
230 | ||
231 | /* Digital channel state. */ | |
232 | if (devc->has_digital) { | |
233 | sr_dbg("Current digital channel state:"); | |
234 | for (i = 0; i < 16; i++) { | |
235 | cmd = g_strdup_printf(":DIG%d:TURN?", i); | |
236 | res = get_cfg_string(sdi, cmd, &t_s); | |
237 | g_free(cmd); | |
238 | if (res != SR_OK) | |
239 | return SR_ERR; | |
240 | devc->digital_channels[i] = !strcmp(t_s, "ON") ? TRUE : FALSE; | |
241 | g_free(t_s); | |
242 | sr_dbg("D%d: %s", i, devc->digital_channels[i] ? "on" : "off"); | |
243 | } | |
244 | } | |
254dd102 BV |
245 | |
246 | /* Timebase. */ | |
247 | if (get_cfg_float(sdi, ":TIM:SCAL?", &devc->timebase) != SR_OK) | |
248 | return SR_ERR; | |
249 | sr_dbg("Current timebase %f", devc->timebase); | |
250 | ||
251 | /* Vertical gain. */ | |
252 | if (get_cfg_float(sdi, ":CHAN1:SCAL?", &devc->vdiv[0]) != SR_OK) | |
253 | return SR_ERR; | |
254 | if (get_cfg_float(sdi, ":CHAN2:SCAL?", &devc->vdiv[1]) != SR_OK) | |
255 | return SR_ERR; | |
256 | sr_dbg("Current vertical gain CH1 %f CH2 %f", devc->vdiv[0], devc->vdiv[1]); | |
257 | ||
258 | /* Vertical offset. */ | |
259 | if (get_cfg_float(sdi, ":CHAN1:OFFS?", &devc->vert_offset[0]) != SR_OK) | |
260 | return SR_ERR; | |
261 | if (get_cfg_float(sdi, ":CHAN2:OFFS?", &devc->vert_offset[1]) != SR_OK) | |
262 | return SR_ERR; | |
263 | sr_dbg("Current vertical offset CH1 %f CH2 %f", devc->vert_offset[0], | |
264 | devc->vert_offset[1]); | |
265 | ||
266 | /* Coupling. */ | |
267 | if (get_cfg_string(sdi, ":CHAN1:COUP?", &devc->coupling[0]) != SR_OK) | |
268 | return SR_ERR; | |
269 | if (get_cfg_string(sdi, ":CHAN2:COUP?", &devc->coupling[1]) != SR_OK) | |
270 | return SR_ERR; | |
271 | sr_dbg("Current coupling CH1 %s CH2 %s", devc->coupling[0], | |
272 | devc->coupling[1]); | |
273 | ||
274 | /* Trigger source. */ | |
275 | if (get_cfg_string(sdi, ":TRIG:EDGE:SOUR?", &devc->trigger_source) != SR_OK) | |
276 | return SR_ERR; | |
277 | sr_dbg("Current trigger source %s", devc->trigger_source); | |
278 | ||
279 | /* Horizontal trigger position. */ | |
280 | if (get_cfg_float(sdi, ":TIM:OFFS?", &devc->horiz_triggerpos) != SR_OK) | |
281 | return SR_ERR; | |
282 | sr_dbg("Current horizontal trigger position %f", devc->horiz_triggerpos); | |
283 | ||
284 | /* Trigger slope. */ | |
285 | if (get_cfg_string(sdi, ":TRIG:EDGE:SLOP?", &devc->trigger_slope) != SR_OK) | |
286 | return SR_ERR; | |
287 | sr_dbg("Current trigger slope %s", devc->trigger_slope); | |
288 | ||
289 | return SR_OK; | |
290 | } | |
291 |