]> sigrok.org Git - libsigrok.git/blame - src/hardware/gwinstek-gds-800/protocol.c
gwinstek-gds-800: Convert to SR_DF_ANALOG.
[libsigrok.git] / src / hardware / gwinstek-gds-800 / protocol.c
CommitLineData
7c198f96
ML
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2015 Martin Lederhilger <martin.lederhilger@gmx.at>
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.h"
b11afbb1
ML
21#include <string.h>
22
23#define ANALOG_CHANNELS 2
24#define VERTICAL_DIVISIONS 10
25
695dc859 26static int read_data(struct sr_dev_inst *sdi,
b11afbb1
ML
27 struct sr_scpi_dev_inst *scpi, struct dev_context *devc,
28 int data_size)
29{
30 int len;
31
32 len = sr_scpi_read_data(scpi,
33 &devc->rcv_buffer[devc->cur_rcv_buffer_position],
34 data_size - devc->cur_rcv_buffer_position);
35 if (len < 0) {
36 sr_err("Read data error.");
695dc859 37 sdi->driver->dev_acquisition_stop(sdi);
b11afbb1
ML
38 devc->cur_rcv_buffer_position = 0;
39 return SR_ERR;
40 }
41
42 devc->cur_rcv_buffer_position += len;
43
44 /* Handle the case where sr_scpi_read_data stopped at the newline. */
45 if (len < data_size && sr_scpi_read_complete(scpi)) {
46 devc->rcv_buffer[devc->cur_rcv_buffer_position] = '\n';
47 devc->cur_rcv_buffer_position++;
48 }
49
50 if (devc->cur_rcv_buffer_position < data_size)
51 return SR_ERR; /* Not finished yet. */
52 else if (devc->cur_rcv_buffer_position == data_size) {
53 devc->cur_rcv_buffer_position = 0;
54 return SR_OK;
55 } else {
56 sr_err("Too many bytes read.");
695dc859 57 sdi->driver->dev_acquisition_stop(sdi);
b11afbb1
ML
58 devc->cur_rcv_buffer_position = 0;
59 return SR_ERR;
60 }
61}
7c198f96
ML
62
63SR_PRIV int gwinstek_gds_800_receive_data(int fd, int revents, void *cb_data)
64{
b11afbb1
ML
65 struct sr_dev_inst *sdi;
66 struct sr_scpi_dev_inst *scpi;
7c198f96 67 struct dev_context *devc;
b11afbb1 68 struct sr_datafeed_packet packet;
6cfb9729
UH
69 struct sr_datafeed_analog analog;
70 struct sr_analog_encoding encoding;
71 struct sr_analog_meaning meaning;
72 struct sr_analog_spec spec;
b11afbb1
ML
73 char command[32];
74 char *response;
75 float volts_per_division;
76 int num_samples, i;
77 float samples[MAX_SAMPLES];
78 uint32_t sample_rate;
79 char *end_ptr;
7c198f96
ML
80
81 (void)fd;
82
83 if (!(sdi = cb_data))
84 return TRUE;
85
86 if (!(devc = sdi->priv))
87 return TRUE;
88
b11afbb1
ML
89 scpi = sdi->conn;
90
91 if (!(revents == G_IO_IN || revents == 0))
92 return TRUE;
93
94 switch (devc->state) {
95 case START_ACQUISITION:
96 if (sr_scpi_send(scpi, ":TRIG:MOD 3") != SR_OK) {
97 sr_err("Failed to set trigger mode to SINGLE.");
695dc859 98 sdi->driver->dev_acquisition_stop(sdi);
b11afbb1
ML
99 return TRUE;
100 }
101 if (sr_scpi_send(scpi, ":STOP") != SR_OK) {
102 sr_err("Failed to put the trigger system into STOP state.");
695dc859 103 sdi->driver->dev_acquisition_stop(sdi);
b11afbb1
ML
104 return TRUE;
105 }
106 if (sr_scpi_send(scpi, ":RUN") != SR_OK) {
107 sr_err("Failed to put the trigger system into RUN state.");
695dc859 108 sdi->driver->dev_acquisition_stop(sdi);
b11afbb1
ML
109 return TRUE;
110 }
111
112 devc->cur_acq_channel = 0;
113 devc->state = START_TRANSFER_OF_CHANNEL_DATA;
114 break;
115 case START_TRANSFER_OF_CHANNEL_DATA:
116 if (((struct sr_channel *)g_slist_nth_data(sdi->channels, devc->cur_acq_channel))->enabled) {
117 if (sr_scpi_send(scpi, ":ACQ%d:MEM?", devc->cur_acq_channel+1) != SR_OK) {
118 sr_err("Failed to acquire memory.");
695dc859 119 sdi->driver->dev_acquisition_stop(sdi);
b11afbb1
ML
120 return TRUE;
121 }
122 if (sr_scpi_read_begin(scpi) != SR_OK) {
123 sr_err("Could not begin reading SCPI response.");
695dc859 124 sdi->driver->dev_acquisition_stop(sdi);
b11afbb1
ML
125 return TRUE;
126 }
127 devc->state = WAIT_FOR_TRANSFER_OF_BEGIN_TRANSMISSION_COMPLETE;
128 devc->cur_rcv_buffer_position = 0;
129 } else {
130 /* All channels acquired. */
131 if (devc->cur_acq_channel == ANALOG_CHANNELS - 1) {
132 sr_spew("All channels acquired.");
133
134 if (devc->cur_acq_frame == devc->frame_limit - 1) {
135 /* All frames accquired. */
136 sr_spew("All frames acquired.");
137
695dc859 138 sdi->driver->dev_acquisition_stop(sdi);
b11afbb1
ML
139 return TRUE;
140 } else {
141 /* Start acquiring next frame. */
142 if (devc->df_started) {
143 packet.type = SR_DF_FRAME_END;
144 sr_session_send(sdi, &packet);
145
146 packet.type = SR_DF_FRAME_BEGIN;
147 sr_session_send(sdi, &packet);
148 }
149
150 devc->cur_acq_frame++;
151 devc->state = START_ACQUISITION;
152 }
153 } else {
154 /* Start acquiring next channel. */
155 devc->cur_acq_channel++;
156 }
157 }
158 break;
159 case WAIT_FOR_TRANSFER_OF_BEGIN_TRANSMISSION_COMPLETE:
695dc859 160 if (read_data(sdi, scpi, devc, 1) == SR_OK) {
b11afbb1
ML
161 if (devc->rcv_buffer[0] == '#')
162 devc->state = WAIT_FOR_TRANSFER_OF_DATA_SIZE_DIGIT_COMPLETE;
163 }
164 break;
165 case WAIT_FOR_TRANSFER_OF_DATA_SIZE_DIGIT_COMPLETE:
695dc859 166 if (read_data(sdi, scpi, devc, 1) == SR_OK) {
b11afbb1
ML
167 if (devc->rcv_buffer[0] != '4' &&
168 devc->rcv_buffer[0] != '5' &&
169 devc->rcv_buffer[0] != '6') {
170 sr_err("Data size digits is not 4, 5 or 6 but "
171 "'%c'.", devc->rcv_buffer[0]);
695dc859 172 sdi->driver->dev_acquisition_stop(sdi);
b11afbb1
ML
173 return TRUE;
174 } else {
175 devc->data_size_digits = devc->rcv_buffer[0] - '0';
176 devc->state = WAIT_FOR_TRANSFER_OF_DATA_SIZE_COMPLETE;
177 }
178 }
179 break;
180 case WAIT_FOR_TRANSFER_OF_DATA_SIZE_COMPLETE:
695dc859 181 if (read_data(sdi, scpi, devc, devc->data_size_digits) == SR_OK) {
b11afbb1
ML
182 devc->rcv_buffer[devc->data_size_digits] = 0;
183 if (sr_atoi(devc->rcv_buffer, &devc->data_size) != SR_OK) {
184 sr_err("Could not parse data size '%s'", devc->rcv_buffer);
695dc859 185 sdi->driver->dev_acquisition_stop(sdi);
b11afbb1
ML
186 return TRUE;
187 } else
188 devc->state = WAIT_FOR_TRANSFER_OF_SAMPLE_RATE_COMPLETE;
189 }
190 break;
191 case WAIT_FOR_TRANSFER_OF_SAMPLE_RATE_COMPLETE:
695dc859 192 if (read_data(sdi, scpi, devc, sizeof(float)) == SR_OK) {
b11afbb1
ML
193 /*
194 * Contrary to the documentation, this field is
195 * transfered with most significant byte first!
196 */
197 sample_rate = RB32(devc->rcv_buffer);
198 memcpy(&devc->sample_rate, &sample_rate, sizeof(float));
199 devc->state = WAIT_FOR_TRANSFER_OF_CHANNEL_INDICATOR_COMPLETE;
200
201 if (!devc->df_started) {
bee2b016 202 std_session_send_df_header(sdi);
b11afbb1
ML
203
204 packet.type = SR_DF_FRAME_BEGIN;
205 sr_session_send(sdi, &packet);
206
207 devc->df_started = TRUE;
208 }
209 }
210 break;
211 case WAIT_FOR_TRANSFER_OF_CHANNEL_INDICATOR_COMPLETE:
695dc859 212 if (read_data(sdi, scpi, devc, 1) == SR_OK)
b11afbb1
ML
213 devc->state = WAIT_FOR_TRANSFER_OF_RESERVED_DATA_COMPLETE;
214 break;
215 case WAIT_FOR_TRANSFER_OF_RESERVED_DATA_COMPLETE:
695dc859 216 if (read_data(sdi, scpi, devc, 3) == SR_OK)
b11afbb1
ML
217 devc->state = WAIT_FOR_TRANSFER_OF_CHANNEL_DATA_COMPLETE;
218 break;
219 case WAIT_FOR_TRANSFER_OF_CHANNEL_DATA_COMPLETE:
695dc859 220 if (read_data(sdi, scpi, devc, devc->data_size - 8) == SR_OK) {
b11afbb1
ML
221 /* Fetch data needed for conversion from device. */
222 snprintf(command, sizeof(command), ":CHAN%d:SCAL?",
223 devc->cur_acq_channel + 1);
224 if (sr_scpi_get_string(scpi, command, &response) != SR_OK) {
225 sr_err("Failed to get volts per division.");
695dc859 226 sdi->driver->dev_acquisition_stop(sdi);
b11afbb1
ML
227 return TRUE;
228 }
229 volts_per_division = g_ascii_strtod(response, &end_ptr);
230 if (!strcmp(end_ptr, "mV"))
231 volts_per_division *= 1.e-3;
232 g_free(response);
233
234 num_samples = (devc->data_size - 8) / 2;
235 sr_spew("Received %d number of samples from channel "
236 "%d.", num_samples, devc->cur_acq_channel + 1);
237
238 /* Convert data. */
239 for (i = 0; i < num_samples; i++)
240 samples[i] = ((float) ((int16_t) (RB16(&devc->rcv_buffer[i*2])))) / 256. * VERTICAL_DIVISIONS * volts_per_division;
241
242 /* Fill frame. */
6cfb9729
UH
243 sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
244 analog.meaning->channels = g_slist_append(NULL, g_slist_nth_data(sdi->channels, devc->cur_acq_channel));
b11afbb1
ML
245 analog.num_samples = num_samples;
246 analog.data = samples;
6cfb9729
UH
247 analog.meaning->mq = SR_MQ_VOLTAGE;
248 analog.meaning->unit = SR_UNIT_VOLT;
249 analog.meaning->mqflags = 0;
250 packet.type = SR_DF_ANALOG;
b11afbb1 251 packet.payload = &analog;
695dc859 252 sr_session_send(sdi, &packet);
6cfb9729 253 g_slist_free(analog.meaning->channels);
b11afbb1
ML
254
255 /* All channels acquired. */
256 if (devc->cur_acq_channel == ANALOG_CHANNELS - 1) {
257 sr_spew("All channels acquired.");
258
259 if (devc->cur_acq_frame == devc->frame_limit - 1) {
260 /* All frames acquired. */
261 sr_spew("All frames acquired.");
695dc859 262 sdi->driver->dev_acquisition_stop(sdi);
b11afbb1
ML
263 return TRUE;
264 } else {
265 /* Start acquiring next frame. */
266 if (devc->df_started) {
267 packet.type = SR_DF_FRAME_END;
268 sr_session_send(sdi, &packet);
269
270 packet.type = SR_DF_FRAME_BEGIN;
271 sr_session_send(sdi, &packet);
272 }
273 devc->cur_acq_frame++;
274 devc->state = START_ACQUISITION;
275 }
276 } else {
277 /* Start acquiring next channel. */
278 devc->state = START_TRANSFER_OF_CHANNEL_DATA;
279 devc->cur_acq_channel++;
280 return TRUE;
281 }
282 }
283 break;
7c198f96
ML
284 }
285
286 return TRUE;
287}