2 * This file is part of the libsigrok project.
4 * Copyright (C) 2015 Martin Lederhilger <martin.lederhilger@gmx.at>
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.
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.
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/>.
24 #define ANALOG_CHANNELS 2
25 #define VERTICAL_DIVISIONS 10
27 static int read_data(struct sr_dev_inst *sdi,
28 struct sr_scpi_dev_inst *scpi, struct dev_context *devc,
33 len = sr_scpi_read_data(scpi,
34 &devc->rcv_buffer[devc->cur_rcv_buffer_position],
35 data_size - devc->cur_rcv_buffer_position);
37 sr_err("Read data error.");
38 sr_dev_acquisition_stop(sdi);
39 devc->cur_rcv_buffer_position = 0;
43 devc->cur_rcv_buffer_position += len;
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++;
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;
57 sr_err("Too many bytes read.");
58 sr_dev_acquisition_stop(sdi);
59 devc->cur_rcv_buffer_position = 0;
64 SR_PRIV int gwinstek_gds_800_receive_data(int fd, int revents, void *cb_data)
66 struct sr_dev_inst *sdi;
67 struct sr_scpi_dev_inst *scpi;
68 struct dev_context *devc;
69 struct sr_datafeed_packet packet;
70 struct sr_datafeed_analog analog;
71 struct sr_analog_encoding encoding;
72 struct sr_analog_meaning meaning;
73 struct sr_analog_spec spec;
76 float volts_per_division;
78 float samples[MAX_SAMPLES];
87 if (!(devc = sdi->priv))
92 if (!(revents == G_IO_IN || revents == 0))
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.");
99 sr_dev_acquisition_stop(sdi);
102 if (sr_scpi_send(scpi, ":STOP") != SR_OK) {
103 sr_err("Failed to put the trigger system into STOP state.");
104 sr_dev_acquisition_stop(sdi);
107 if (sr_scpi_send(scpi, ":RUN") != SR_OK) {
108 sr_err("Failed to put the trigger system into RUN state.");
109 sr_dev_acquisition_stop(sdi);
113 devc->cur_acq_channel = 0;
114 devc->state = START_TRANSFER_OF_CHANNEL_DATA;
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.");
120 sr_dev_acquisition_stop(sdi);
123 if (sr_scpi_read_begin(scpi) != SR_OK) {
124 sr_err("Could not begin reading SCPI response.");
125 sr_dev_acquisition_stop(sdi);
128 devc->state = WAIT_FOR_TRANSFER_OF_BEGIN_TRANSMISSION_COMPLETE;
129 devc->cur_rcv_buffer_position = 0;
131 /* All channels acquired. */
132 if (devc->cur_acq_channel == ANALOG_CHANNELS - 1) {
133 sr_spew("All channels acquired.");
135 if (devc->cur_acq_frame == devc->frame_limit - 1) {
136 /* All frames accquired. */
137 sr_spew("All frames acquired.");
139 sr_dev_acquisition_stop(sdi);
142 /* Start acquiring next frame. */
143 if (devc->df_started) {
144 packet.type = SR_DF_FRAME_END;
145 sr_session_send(sdi, &packet);
147 packet.type = SR_DF_FRAME_BEGIN;
148 sr_session_send(sdi, &packet);
151 devc->cur_acq_frame++;
152 devc->state = START_ACQUISITION;
155 /* Start acquiring next channel. */
156 devc->cur_acq_channel++;
160 case WAIT_FOR_TRANSFER_OF_BEGIN_TRANSMISSION_COMPLETE:
161 if (read_data(sdi, scpi, devc, 1) < 0)
163 if (devc->rcv_buffer[0] == '#')
164 devc->state = WAIT_FOR_TRANSFER_OF_DATA_SIZE_DIGIT_COMPLETE;
166 case WAIT_FOR_TRANSFER_OF_DATA_SIZE_DIGIT_COMPLETE:
167 if (read_data(sdi, scpi, devc, 1) < 0)
169 if (devc->rcv_buffer[0] != '4' &&
170 devc->rcv_buffer[0] != '5' &&
171 devc->rcv_buffer[0] != '6') {
172 sr_err("Data size digits is not 4, 5 or 6 but "
173 "'%c'.", devc->rcv_buffer[0]);
174 sr_dev_acquisition_stop(sdi);
177 devc->data_size_digits = devc->rcv_buffer[0] - '0';
178 devc->state = WAIT_FOR_TRANSFER_OF_DATA_SIZE_COMPLETE;
181 case WAIT_FOR_TRANSFER_OF_DATA_SIZE_COMPLETE:
182 if (read_data(sdi, scpi, devc, devc->data_size_digits) < 0)
184 devc->rcv_buffer[devc->data_size_digits] = 0;
185 if (sr_atoi(devc->rcv_buffer, &devc->data_size) != SR_OK) {
186 sr_err("Could not parse data size '%s'", devc->rcv_buffer);
187 sr_dev_acquisition_stop(sdi);
190 devc->state = WAIT_FOR_TRANSFER_OF_SAMPLE_RATE_COMPLETE;
192 case WAIT_FOR_TRANSFER_OF_SAMPLE_RATE_COMPLETE:
193 if (read_data(sdi, scpi, devc, sizeof(float)) < 0)
196 * Contrary to the documentation, this field is
197 * transfered with most significant byte first!
199 sample_rate = RB32(devc->rcv_buffer);
200 memcpy(&devc->sample_rate, &sample_rate, sizeof(float));
201 devc->state = WAIT_FOR_TRANSFER_OF_CHANNEL_INDICATOR_COMPLETE;
203 if (!devc->df_started) {
204 std_session_send_df_header(sdi);
206 packet.type = SR_DF_FRAME_BEGIN;
207 sr_session_send(sdi, &packet);
209 devc->df_started = TRUE;
212 case WAIT_FOR_TRANSFER_OF_CHANNEL_INDICATOR_COMPLETE:
213 if (read_data(sdi, scpi, devc, 1) == SR_OK)
214 devc->state = WAIT_FOR_TRANSFER_OF_RESERVED_DATA_COMPLETE;
216 case WAIT_FOR_TRANSFER_OF_RESERVED_DATA_COMPLETE:
217 if (read_data(sdi, scpi, devc, 3) == SR_OK)
218 devc->state = WAIT_FOR_TRANSFER_OF_CHANNEL_DATA_COMPLETE;
220 case WAIT_FOR_TRANSFER_OF_CHANNEL_DATA_COMPLETE:
221 if (read_data(sdi, scpi, devc, devc->data_size - 8) < 0)
224 /* Fetch data needed for conversion from device. */
225 snprintf(command, sizeof(command), ":CHAN%d:SCAL?",
226 devc->cur_acq_channel + 1);
227 if (sr_scpi_get_string(scpi, command, &response) != SR_OK) {
228 sr_err("Failed to get volts per division.");
229 sr_dev_acquisition_stop(sdi);
232 volts_per_division = g_ascii_strtod(response, &end_ptr);
233 if (!strcmp(end_ptr, "mV"))
234 volts_per_division *= 1.e-3;
237 num_samples = (devc->data_size - 8) / 2;
238 sr_spew("Received %d number of samples from channel "
239 "%d.", num_samples, devc->cur_acq_channel + 1);
241 float vbit = volts_per_division * VERTICAL_DIVISIONS / 256.0;
242 float vbitlog = log10f(vbit);
243 int digits = -(int)vbitlog + (vbitlog < 0.0);
246 for (i = 0; i < num_samples; i++)
247 samples[i] = ((float) ((int16_t) (RB16(&devc->rcv_buffer[i*2])))) * vbit;
250 sr_analog_init(&analog, &encoding, &meaning, &spec, digits);
251 analog.meaning->channels = g_slist_append(NULL, g_slist_nth_data(sdi->channels, devc->cur_acq_channel));
252 analog.num_samples = num_samples;
253 analog.data = samples;
254 analog.meaning->mq = SR_MQ_VOLTAGE;
255 analog.meaning->unit = SR_UNIT_VOLT;
256 analog.meaning->mqflags = 0;
257 packet.type = SR_DF_ANALOG;
258 packet.payload = &analog;
259 sr_session_send(sdi, &packet);
260 g_slist_free(analog.meaning->channels);
262 /* All channels acquired. */
263 if (devc->cur_acq_channel == ANALOG_CHANNELS - 1) {
264 sr_spew("All channels acquired.");
266 if (devc->cur_acq_frame == devc->frame_limit - 1) {
267 /* All frames acquired. */
268 sr_spew("All frames acquired.");
269 sr_dev_acquisition_stop(sdi);
272 /* Start acquiring next frame. */
273 if (devc->df_started) {
274 packet.type = SR_DF_FRAME_END;
275 sr_session_send(sdi, &packet);
277 packet.type = SR_DF_FRAME_BEGIN;
278 sr_session_send(sdi, &packet);
280 devc->cur_acq_frame++;
281 devc->state = START_ACQUISITION;
284 /* Start acquiring next channel. */
285 devc->state = START_TRANSFER_OF_CHANNEL_DATA;
286 devc->cur_acq_channel++;