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/>.
23 #define ANALOG_CHANNELS 2
24 #define VERTICAL_DIVISIONS 10
26 static int read_data(struct sr_dev_inst *sdi, void *cb_data,
27 struct sr_scpi_dev_inst *scpi, struct dev_context *devc,
32 len = sr_scpi_read_data(scpi,
33 &devc->rcv_buffer[devc->cur_rcv_buffer_position],
34 data_size - devc->cur_rcv_buffer_position);
36 sr_err("Read data error.");
37 sdi->driver->dev_acquisition_stop(sdi, cb_data);
38 devc->cur_rcv_buffer_position = 0;
42 devc->cur_rcv_buffer_position += len;
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++;
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;
56 sr_err("Too many bytes read.");
57 sdi->driver->dev_acquisition_stop(sdi, cb_data);
58 devc->cur_rcv_buffer_position = 0;
63 SR_PRIV int gwinstek_gds_800_receive_data(int fd, int revents, void *cb_data)
65 struct sr_dev_inst *sdi;
66 struct sr_scpi_dev_inst *scpi;
67 struct dev_context *devc;
68 struct sr_datafeed_packet packet;
69 struct sr_datafeed_analog_old analog;
72 float volts_per_division;
74 float samples[MAX_SAMPLES];
83 if (!(devc = sdi->priv))
88 if (!(revents == G_IO_IN || revents == 0))
91 switch (devc->state) {
92 case START_ACQUISITION:
93 if (sr_scpi_send(scpi, ":TRIG:MOD 3") != SR_OK) {
94 sr_err("Failed to set trigger mode to SINGLE.");
95 sdi->driver->dev_acquisition_stop(sdi, cb_data);
98 if (sr_scpi_send(scpi, ":STOP") != SR_OK) {
99 sr_err("Failed to put the trigger system into STOP state.");
100 sdi->driver->dev_acquisition_stop(sdi, cb_data);
103 if (sr_scpi_send(scpi, ":RUN") != SR_OK) {
104 sr_err("Failed to put the trigger system into RUN state.");
105 sdi->driver->dev_acquisition_stop(sdi, cb_data);
109 devc->cur_acq_channel = 0;
110 devc->state = START_TRANSFER_OF_CHANNEL_DATA;
112 case START_TRANSFER_OF_CHANNEL_DATA:
113 if (((struct sr_channel *)g_slist_nth_data(sdi->channels, devc->cur_acq_channel))->enabled) {
114 if (sr_scpi_send(scpi, ":ACQ%d:MEM?", devc->cur_acq_channel+1) != SR_OK) {
115 sr_err("Failed to acquire memory.");
116 sdi->driver->dev_acquisition_stop(sdi, cb_data);
119 if (sr_scpi_read_begin(scpi) != SR_OK) {
120 sr_err("Could not begin reading SCPI response.");
121 sdi->driver->dev_acquisition_stop(sdi, cb_data);
124 devc->state = WAIT_FOR_TRANSFER_OF_BEGIN_TRANSMISSION_COMPLETE;
125 devc->cur_rcv_buffer_position = 0;
127 /* All channels acquired. */
128 if (devc->cur_acq_channel == ANALOG_CHANNELS - 1) {
129 sr_spew("All channels acquired.");
131 if (devc->cur_acq_frame == devc->frame_limit - 1) {
132 /* All frames accquired. */
133 sr_spew("All frames acquired.");
135 sdi->driver->dev_acquisition_stop(sdi, cb_data);
138 /* Start acquiring next frame. */
139 if (devc->df_started) {
140 packet.type = SR_DF_FRAME_END;
141 sr_session_send(sdi, &packet);
143 packet.type = SR_DF_FRAME_BEGIN;
144 sr_session_send(sdi, &packet);
147 devc->cur_acq_frame++;
148 devc->state = START_ACQUISITION;
151 /* Start acquiring next channel. */
152 devc->cur_acq_channel++;
156 case WAIT_FOR_TRANSFER_OF_BEGIN_TRANSMISSION_COMPLETE:
157 if (read_data(sdi, cb_data, scpi, devc, 1) == SR_OK) {
158 if (devc->rcv_buffer[0] == '#')
159 devc->state = WAIT_FOR_TRANSFER_OF_DATA_SIZE_DIGIT_COMPLETE;
162 case WAIT_FOR_TRANSFER_OF_DATA_SIZE_DIGIT_COMPLETE:
163 if (read_data(sdi, cb_data, scpi, devc, 1) == SR_OK) {
164 if (devc->rcv_buffer[0] != '4' &&
165 devc->rcv_buffer[0] != '5' &&
166 devc->rcv_buffer[0] != '6') {
167 sr_err("Data size digits is not 4, 5 or 6 but "
168 "'%c'.", devc->rcv_buffer[0]);
169 sdi->driver->dev_acquisition_stop(sdi, cb_data);
172 devc->data_size_digits = devc->rcv_buffer[0] - '0';
173 devc->state = WAIT_FOR_TRANSFER_OF_DATA_SIZE_COMPLETE;
177 case WAIT_FOR_TRANSFER_OF_DATA_SIZE_COMPLETE:
178 if (read_data(sdi, cb_data, scpi, devc, devc->data_size_digits) == SR_OK) {
179 devc->rcv_buffer[devc->data_size_digits] = 0;
180 if (sr_atoi(devc->rcv_buffer, &devc->data_size) != SR_OK) {
181 sr_err("Could not parse data size '%s'", devc->rcv_buffer);
182 sdi->driver->dev_acquisition_stop(sdi, cb_data);
185 devc->state = WAIT_FOR_TRANSFER_OF_SAMPLE_RATE_COMPLETE;
188 case WAIT_FOR_TRANSFER_OF_SAMPLE_RATE_COMPLETE:
189 if (read_data(sdi, cb_data, scpi, devc, sizeof(float)) == SR_OK) {
191 * Contrary to the documentation, this field is
192 * transfered with most significant byte first!
194 sample_rate = RB32(devc->rcv_buffer);
195 memcpy(&devc->sample_rate, &sample_rate, sizeof(float));
196 devc->state = WAIT_FOR_TRANSFER_OF_CHANNEL_INDICATOR_COMPLETE;
198 if (!devc->df_started) {
199 std_session_send_df_header(sdi, LOG_PREFIX);
201 packet.type = SR_DF_FRAME_BEGIN;
202 sr_session_send(sdi, &packet);
204 devc->df_started = TRUE;
208 case WAIT_FOR_TRANSFER_OF_CHANNEL_INDICATOR_COMPLETE:
209 if (read_data(sdi, cb_data, scpi, devc, 1) == SR_OK)
210 devc->state = WAIT_FOR_TRANSFER_OF_RESERVED_DATA_COMPLETE;
212 case WAIT_FOR_TRANSFER_OF_RESERVED_DATA_COMPLETE:
213 if (read_data(sdi, cb_data, scpi, devc, 3) == SR_OK)
214 devc->state = WAIT_FOR_TRANSFER_OF_CHANNEL_DATA_COMPLETE;
216 case WAIT_FOR_TRANSFER_OF_CHANNEL_DATA_COMPLETE:
217 if (read_data(sdi, cb_data, scpi, devc, devc->data_size - 8) == SR_OK) {
218 /* Fetch data needed for conversion from device. */
219 snprintf(command, sizeof(command), ":CHAN%d:SCAL?",
220 devc->cur_acq_channel + 1);
221 if (sr_scpi_get_string(scpi, command, &response) != SR_OK) {
222 sr_err("Failed to get volts per division.");
223 sdi->driver->dev_acquisition_stop(sdi, cb_data);
226 volts_per_division = g_ascii_strtod(response, &end_ptr);
227 if (!strcmp(end_ptr, "mV"))
228 volts_per_division *= 1.e-3;
231 num_samples = (devc->data_size - 8) / 2;
232 sr_spew("Received %d number of samples from channel "
233 "%d.", num_samples, devc->cur_acq_channel + 1);
236 for (i = 0; i < num_samples; i++)
237 samples[i] = ((float) ((int16_t) (RB16(&devc->rcv_buffer[i*2])))) / 256. * VERTICAL_DIVISIONS * volts_per_division;
240 analog.channels = g_slist_append(NULL, g_slist_nth_data(sdi->channels, devc->cur_acq_channel));
241 analog.num_samples = num_samples;
242 analog.data = samples;
243 analog.mq = SR_MQ_VOLTAGE;
244 analog.unit = SR_UNIT_VOLT;
246 packet.type = SR_DF_ANALOG_OLD;
247 packet.payload = &analog;
248 sr_session_send(cb_data, &packet);
249 g_slist_free(analog.channels);
251 /* All channels acquired. */
252 if (devc->cur_acq_channel == ANALOG_CHANNELS - 1) {
253 sr_spew("All channels acquired.");
255 if (devc->cur_acq_frame == devc->frame_limit - 1) {
256 /* All frames acquired. */
257 sr_spew("All frames acquired.");
258 sdi->driver->dev_acquisition_stop(sdi, cb_data);
261 /* Start acquiring next frame. */
262 if (devc->df_started) {
263 packet.type = SR_DF_FRAME_END;
264 sr_session_send(sdi, &packet);
266 packet.type = SR_DF_FRAME_BEGIN;
267 sr_session_send(sdi, &packet);
269 devc->cur_acq_frame++;
270 devc->state = START_ACQUISITION;
273 /* Start acquiring next channel. */
274 devc->state = START_TRANSFER_OF_CHANNEL_DATA;
275 devc->cur_acq_channel++;