]> sigrok.org Git - libsigrok.git/blame - src/hardware/yokogawa-dlm/protocol_wrappers.c
yokogawa-dlm: Do not block when receiving and save frame length in scope state
[libsigrok.git] / src / hardware / yokogawa-dlm / protocol_wrappers.c
CommitLineData
10763937
SA
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 abraxa (Soeren Apel) <soeren@apelpie.net>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "protocol_wrappers.h"
21
8ab929d6
SA
22#define MAX_COMMAND_SIZE 64
23
24/*
25 * DLM2000 comm spec:
26 * https://www.yokogawa.com/pdf/provide/E/GW/IM/0000022842/0/IM710105-17E.pdf
27 */
28
29int dlm_timebase_get(struct sr_scpi_dev_inst *scpi,
30 gchar **response)
31{
32 return sr_scpi_get_string(scpi, ":TIMEBASE:TDIV?", response);
33}
34
35int dlm_timebase_set(struct sr_scpi_dev_inst *scpi,
36 const gchar *value)
37{
38 gchar cmd[MAX_COMMAND_SIZE];
39 g_snprintf(cmd, sizeof(cmd), ":TIMEBASE:TDIV %s", value);
40 return sr_scpi_send(scpi, cmd);
41}
42
43int dlm_horiz_trigger_pos_get(struct sr_scpi_dev_inst *scpi,
44 float *response)
45{
46 return sr_scpi_get_float(scpi, ":TRIGGER:DELAY:TIME?", response);
47}
48
49int dlm_horiz_trigger_pos_set(struct sr_scpi_dev_inst *scpi,
50 const gchar *value)
51{
52 gchar cmd[MAX_COMMAND_SIZE];
53 g_snprintf(cmd, sizeof(cmd), ":TRIGGER:DELAY:TIME %s", value);
54 return sr_scpi_send(scpi, cmd);
55}
56
57int dlm_trigger_source_get(struct sr_scpi_dev_inst *scpi,
58 gchar **response)
59{
60 return sr_scpi_get_string(scpi, ":TRIGGER:ATRIGGER:SIMPLE:SOURCE?", response);
61}
62
63int dlm_trigger_source_set(struct sr_scpi_dev_inst *scpi,
64 const gchar *value)
65{
66 gchar cmd[MAX_COMMAND_SIZE];
67 g_snprintf(cmd, sizeof(cmd), ":TRIGGER:ATRIGGER:SIMPLE:SOURCE %s", value);
68 return sr_scpi_send(scpi, cmd);
69}
70
71int dlm_trigger_slope_get(struct sr_scpi_dev_inst *scpi,
72 int *response)
73{
74 gchar *resp;
75 int result;
76
77 result = SR_ERR;
78
79 if (sr_scpi_get_string(scpi, ":TRIGGER:ATRIGGER:SIMPLE:SLOPE?", &resp) != SR_OK) {
80 g_free(resp);
81 return SR_ERR;
82 }
83
84 if (strcmp("RISE", resp) == 0) {
85 *response = SLOPE_POSITIVE;
86 result = SR_OK;
87 }
88
89 if (strcmp("FALL", resp) == 0) {
90 *response = SLOPE_NEGATIVE;
91 result = SR_OK;
92 }
93
94 g_free(resp);
95 return result;
96}
97
98int dlm_trigger_slope_set(struct sr_scpi_dev_inst *scpi,
99 const int value)
100{
101 if (value == SLOPE_POSITIVE)
102 return sr_scpi_send(scpi, ":TRIGGER:ATRIGGER:SIMPLE:SLOPE RISE");
103
104 if (value == SLOPE_NEGATIVE)
105 return sr_scpi_send(scpi, ":TRIGGER:ATRIGGER:SIMPLE:SLOPE FALL");
106
107 return SR_ERR_ARG;
108}
109
110int dlm_analog_chan_state_get(struct sr_scpi_dev_inst *scpi, int channel,
111 gboolean *response)
112{
113 gchar cmd[MAX_COMMAND_SIZE];
114 g_snprintf(cmd, sizeof(cmd), ":CHANNEL%d:DISPLAY?", channel);
115 return sr_scpi_get_bool(scpi, cmd, response);
116}
117
118int dlm_analog_chan_state_set(struct sr_scpi_dev_inst *scpi, int channel,
119 const gboolean value)
120{
121 gchar cmd[MAX_COMMAND_SIZE];
122
123 if (value)
124 g_snprintf(cmd, sizeof(cmd), ":CHANNEL%d:DISPLAY ON", channel);
125 else
126 g_snprintf(cmd, sizeof(cmd), ":CHANNEL%d:DISPLAY OFF", channel);
127
128 return sr_scpi_send(scpi, cmd);
129}
130
131int dlm_analog_chan_vdiv_get(struct sr_scpi_dev_inst *scpi, int channel,
132 gchar **response)
133{
134 gchar cmd[MAX_COMMAND_SIZE];
135 g_snprintf(cmd, sizeof(cmd), ":CHANNEL%d:VDIV?", channel);
136 return sr_scpi_get_string(scpi, cmd, response);
137}
138
139int dlm_analog_chan_vdiv_set(struct sr_scpi_dev_inst *scpi, int channel,
140 const gchar *value)
141{
142 gchar cmd[MAX_COMMAND_SIZE];
143 g_snprintf(cmd, sizeof(cmd), ":CHANNEL%d:VDIV %s", channel, value);
144 return sr_scpi_send(scpi, cmd);
145}
146
147int dlm_analog_chan_voffs_get(struct sr_scpi_dev_inst *scpi, int channel,
148 float *response)
149{
150 gchar cmd[MAX_COMMAND_SIZE];
151 g_snprintf(cmd, sizeof(cmd), ":CHANNEL%d:POSITION?", channel);
152 return sr_scpi_get_float(scpi, cmd, response);
153}
154
155int dlm_analog_chan_srate_get(struct sr_scpi_dev_inst *scpi, int channel,
156 float *response)
157{
158 gchar cmd[MAX_COMMAND_SIZE];
159 g_snprintf(cmd, sizeof(cmd), ":WAVEFORM:TRACE %d", channel);
160
161 if (sr_scpi_send(scpi, cmd) != SR_OK)
162 return SR_ERR;
163
164 g_snprintf(cmd, sizeof(cmd), ":WAVEFORM:RECORD 0");
165 if (sr_scpi_send(scpi, cmd) != SR_OK)
166 return SR_ERR;
167
168 return sr_scpi_get_float(scpi, ":WAVEFORM:SRATE?", response);
169}
170
171int dlm_analog_chan_coupl_get(struct sr_scpi_dev_inst *scpi, int channel,
172 gchar **response)
173{
174 gchar cmd[MAX_COMMAND_SIZE];
175 g_snprintf(cmd, sizeof(cmd), ":CHANNEL%d:COUPLING?", channel);
176 return sr_scpi_get_string(scpi, cmd, response);
177}
178
179int dlm_analog_chan_coupl_set(struct sr_scpi_dev_inst *scpi, int channel,
180 const gchar *value)
181{
182 gchar cmd[MAX_COMMAND_SIZE];
183 g_snprintf(cmd, sizeof(cmd), ":CHANNEL%d:COUPLING %s", channel, value);
184 return sr_scpi_send(scpi, cmd);
185}
186
187int dlm_analog_chan_wrange_get(struct sr_scpi_dev_inst *scpi, int channel,
188 float *response)
189{
190 gchar cmd[MAX_COMMAND_SIZE];
191 int result;
192
193 g_snprintf(cmd, sizeof(cmd), ":WAVEFORM:TRACE %d", channel);
194 result = sr_scpi_send(scpi, cmd);
195 result &= sr_scpi_get_float(scpi, ":WAVEFORM:RANGE?", response);
196 return result;
197}
198
199int dlm_analog_chan_woffs_get(struct sr_scpi_dev_inst *scpi, int channel,
200 float *response)
201{
202 gchar cmd[MAX_COMMAND_SIZE];
203 int result;
204
205 g_snprintf(cmd, sizeof(cmd), ":WAVEFORM:TRACE %d", channel);
206 result = sr_scpi_send(scpi, cmd);
207 result &= sr_scpi_get_float(scpi, ":WAVEFORM:OFFSET?", response);
208 return result;
209}
210
211int dlm_digital_chan_state_get(struct sr_scpi_dev_inst *scpi, int channel,
212 gboolean *response)
213{
214 gchar cmd[MAX_COMMAND_SIZE];
215 g_snprintf(cmd, sizeof(cmd), ":LOGIC:PODA:BIT%d:DISPLAY?", channel);
216 return sr_scpi_get_bool(scpi, cmd, response);
217}
218
219int dlm_digital_chan_state_set(struct sr_scpi_dev_inst *scpi, int channel,
220 const gboolean value)
221{
222 gchar cmd[MAX_COMMAND_SIZE];
223
224 if (value)
225 g_snprintf(cmd, sizeof(cmd), ":LOGIC:PODA:BIT%d:DISPLAY ON", channel);
226 else
227 g_snprintf(cmd, sizeof(cmd), ":LOGIC:PODA:BIT%d:DISPLAY OFF", channel);
228
229 return sr_scpi_send(scpi, cmd);
230}
231
232int dlm_digital_pod_state_get(struct sr_scpi_dev_inst *scpi, int pod,
233 gboolean *response)
234{
235 gchar cmd[MAX_COMMAND_SIZE];
236
237 /* TODO: pod currently ignored as DLM2000 only has pod A. */
238 (void)pod;
239
240 g_snprintf(cmd, sizeof(cmd), ":LOGIC:MODE?");
241 return sr_scpi_get_bool(scpi, cmd, response);
242}
243
244int dlm_digital_pod_state_set(struct sr_scpi_dev_inst *scpi, int pod,
245 const gboolean value)
246{
247 /* TODO: pod currently ignored as DLM2000 only has pod A. */
248 (void)pod;
249
250 if (value)
251 return sr_scpi_send(scpi, ":LOGIC:MODE ON");
252 else
253 return sr_scpi_send(scpi, ":LOGIC:MODE OFF");
254}
255
256
257int dlm_response_headers_set(struct sr_scpi_dev_inst *scpi,
258 const gboolean value)
259{
260 if (value)
261 return sr_scpi_send(scpi, ":COMMUNICATE:HEADER ON");
262 else
263 return sr_scpi_send(scpi, ":COMMUNICATE:HEADER OFF");
264}
265
266int dlm_acquisition_stop(struct sr_scpi_dev_inst *scpi)
267{
268 return sr_scpi_send(scpi, ":STOP");
269}
270
271
272int dlm_acq_length_get(struct sr_scpi_dev_inst *scpi,
af3487ec 273 uint32_t *response)
8ab929d6 274{
af3487ec
SA
275 int ret;
276 char *s;
277 long tmp;
278
279 if (sr_scpi_get_string(scpi, ":WAVEFORM:LENGTH?", &s) != SR_OK)
280 if (!s)
281 return SR_ERR;
282
283 if (sr_atol(s, &tmp) == SR_OK)
284 ret = SR_OK;
285 else
286 ret = SR_ERR;
287
288 g_free(s);
289 *response = tmp;
290
291 return ret;
8ab929d6
SA
292}
293
294int dlm_chunks_per_acq_get(struct sr_scpi_dev_inst *scpi, int *response)
295{
296 int result, acq_len;
297
298 /* Data retrieval queries such as :WAVEFORM:SEND? will only return
299 * up to 12500 samples at a time. If the oscilloscope operates in a
300 * mode where more than 12500 samples fit on screen (i.e. in one
301 * acquisition), data needs to be retrieved multiple times.
302 */
303
304 result = sr_scpi_get_int(scpi, ":WAVEFORM:LENGTH?", &acq_len);
305 *response = MAX(acq_len / DLM_MAX_FRAME_LENGTH, 1);
306
307 return result;
308}
309
310int dlm_start_frame_set(struct sr_scpi_dev_inst *scpi, int value)
311{
312 gchar cmd[MAX_COMMAND_SIZE];
313
314 g_snprintf(cmd, sizeof(cmd), ":WAVEFORM:START %d",
315 value * DLM_MAX_FRAME_LENGTH);
316
317 return sr_scpi_send(scpi, cmd);
318}
319
320int dlm_data_get(struct sr_scpi_dev_inst *scpi, int acquisition_num)
321{
322 gchar cmd[MAX_COMMAND_SIZE];
323
324 g_snprintf(cmd, sizeof(cmd), ":WAVEFORM:ALL:SEND? %d", acquisition_num);
325 return sr_scpi_send(scpi, cmd);
326}
327
328int dlm_analog_data_get(struct sr_scpi_dev_inst *scpi, int channel)
329{
330 gchar cmd[MAX_COMMAND_SIZE];
331 int result;
332
333 result = sr_scpi_send(scpi, ":WAVEFORM:FORMAT BYTE");
334 if (result == SR_OK) result = sr_scpi_send(scpi, ":WAVEFORM:RECORD 0");
335 if (result == SR_OK) result = sr_scpi_send(scpi, ":WAVEFORM:START 0");
336 if (result == SR_OK) result = sr_scpi_send(scpi, ":WAVEFORM:END 124999999");
337
338 g_snprintf(cmd, sizeof(cmd), ":WAVEFORM:TRACE %d", channel);
339 if (result == SR_OK) result = sr_scpi_send(scpi, cmd);
340
341 if (result == SR_OK) result = sr_scpi_send(scpi, ":WAVEFORM:SEND? 1");
342
343 return result;
344}
345
346int dlm_digital_data_get(struct sr_scpi_dev_inst *scpi)
347{
348 int result;
349
350 result = sr_scpi_send(scpi, ":WAVEFORM:FORMAT BYTE");
351 if (result == SR_OK) result = sr_scpi_send(scpi, ":WAVEFORM:RECORD 0");
352 if (result == SR_OK) result = sr_scpi_send(scpi, ":WAVEFORM:START 0");
353 if (result == SR_OK) result = sr_scpi_send(scpi, ":WAVEFORM:END 124999999");
354 if (result == SR_OK) result = sr_scpi_send(scpi, ":WAVEFORM:TRACE LOGIC");
355 if (result == SR_OK) result = sr_scpi_send(scpi, ":WAVEFORM:SEND? 1");
356
357 return result;
358}