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