]> sigrok.org Git - libsigrok.git/blob - src/hardware/yokogawa-dlm/protocol_wrappers.c
419420d14b83a508ca0dce5d505ad3f1b1ed91e6
[libsigrok.git] / src / hardware / yokogawa-dlm / protocol_wrappers.c
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
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
29 int 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
35 int 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
43 int 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
49 int 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
57 int 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
63 int 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
71 int 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
98 int 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
110 int 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
118 int 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
131 int 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
139 int 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
147 int 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
155 int 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
171 int 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
179 int 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
187 int 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
199 int 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
211 int 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
219 int 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
232 int 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
244 int 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
257 int 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
266 int dlm_acquisition_stop(struct sr_scpi_dev_inst *scpi)
267 {
268         return sr_scpi_send(scpi, ":STOP");
269 }
270
271
272 int dlm_acq_length_get(struct sr_scpi_dev_inst *scpi,
273                 int *response)
274 {
275         return sr_scpi_get_int(scpi, ":WAVEFORM:LENGTH?", response);
276 }
277
278 int dlm_chunks_per_acq_get(struct sr_scpi_dev_inst *scpi, int *response)
279 {
280         int result, acq_len;
281
282         /* Data retrieval queries such as :WAVEFORM:SEND? will only return
283          * up to 12500 samples at a time. If the oscilloscope operates in a
284          * mode where more than 12500 samples fit on screen (i.e. in one
285          * acquisition), data needs to be retrieved multiple times.
286          */
287
288         result = sr_scpi_get_int(scpi, ":WAVEFORM:LENGTH?", &acq_len);
289         *response = MAX(acq_len / DLM_MAX_FRAME_LENGTH, 1);
290
291         return result;
292 }
293
294 int dlm_start_frame_set(struct sr_scpi_dev_inst *scpi, int value)
295 {
296         gchar cmd[MAX_COMMAND_SIZE];
297
298         g_snprintf(cmd, sizeof(cmd), ":WAVEFORM:START %d",
299                         value * DLM_MAX_FRAME_LENGTH);
300
301         return sr_scpi_send(scpi, cmd);
302 }
303
304 int dlm_data_get(struct sr_scpi_dev_inst *scpi, int acquisition_num)
305 {
306         gchar cmd[MAX_COMMAND_SIZE];
307
308         g_snprintf(cmd, sizeof(cmd), ":WAVEFORM:ALL:SEND? %d", acquisition_num);
309         return sr_scpi_send(scpi, cmd);
310 }
311
312 int dlm_analog_data_get(struct sr_scpi_dev_inst *scpi, int channel)
313 {
314         gchar cmd[MAX_COMMAND_SIZE];
315         int result;
316
317         result = sr_scpi_send(scpi, ":WAVEFORM:FORMAT BYTE");
318         if (result == SR_OK) result = sr_scpi_send(scpi, ":WAVEFORM:RECORD 0");
319         if (result == SR_OK) result = sr_scpi_send(scpi, ":WAVEFORM:START 0");
320         if (result == SR_OK) result = sr_scpi_send(scpi, ":WAVEFORM:END 124999999");
321
322         g_snprintf(cmd, sizeof(cmd), ":WAVEFORM:TRACE %d", channel);
323         if (result == SR_OK) result = sr_scpi_send(scpi, cmd);
324
325         if (result == SR_OK) result = sr_scpi_send(scpi, ":WAVEFORM:SEND? 1");
326
327         return result;
328 }
329
330 int dlm_digital_data_get(struct sr_scpi_dev_inst *scpi)
331 {
332         int result;
333
334         result = sr_scpi_send(scpi, ":WAVEFORM:FORMAT BYTE");
335         if (result == SR_OK) result = sr_scpi_send(scpi, ":WAVEFORM:RECORD 0");
336         if (result == SR_OK) result = sr_scpi_send(scpi, ":WAVEFORM:START 0");
337         if (result == SR_OK) result = sr_scpi_send(scpi, ":WAVEFORM:END 124999999");
338         if (result == SR_OK) result = sr_scpi_send(scpi, ":WAVEFORM:TRACE LOGIC");
339         if (result == SR_OK) result = sr_scpi_send(scpi, ":WAVEFORM:SEND? 1");
340
341         return result;
342 }