]>
Commit | Line | Data |
---|---|---|
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 | 26 | static 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 | |
63 | SR_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; |
5faebab2 | 69 | struct sr_datafeed_analog_old analog; |
b11afbb1 ML |
70 | char command[32]; |
71 | char *response; | |
72 | float volts_per_division; | |
73 | int num_samples, i; | |
74 | float samples[MAX_SAMPLES]; | |
75 | uint32_t sample_rate; | |
76 | char *end_ptr; | |
7c198f96 ML |
77 | |
78 | (void)fd; | |
79 | ||
80 | if (!(sdi = cb_data)) | |
81 | return TRUE; | |
82 | ||
83 | if (!(devc = sdi->priv)) | |
84 | return TRUE; | |
85 | ||
b11afbb1 ML |
86 | scpi = sdi->conn; |
87 | ||
88 | if (!(revents == G_IO_IN || revents == 0)) | |
89 | return TRUE; | |
90 | ||
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."); | |
695dc859 | 95 | sdi->driver->dev_acquisition_stop(sdi); |
b11afbb1 ML |
96 | return TRUE; |
97 | } | |
98 | if (sr_scpi_send(scpi, ":STOP") != SR_OK) { | |
99 | sr_err("Failed to put the trigger system into STOP state."); | |
695dc859 | 100 | sdi->driver->dev_acquisition_stop(sdi); |
b11afbb1 ML |
101 | return TRUE; |
102 | } | |
103 | if (sr_scpi_send(scpi, ":RUN") != SR_OK) { | |
104 | sr_err("Failed to put the trigger system into RUN state."); | |
695dc859 | 105 | sdi->driver->dev_acquisition_stop(sdi); |
b11afbb1 ML |
106 | return TRUE; |
107 | } | |
108 | ||
109 | devc->cur_acq_channel = 0; | |
110 | devc->state = START_TRANSFER_OF_CHANNEL_DATA; | |
111 | break; | |
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."); | |
695dc859 | 116 | sdi->driver->dev_acquisition_stop(sdi); |
b11afbb1 ML |
117 | return TRUE; |
118 | } | |
119 | if (sr_scpi_read_begin(scpi) != SR_OK) { | |
120 | sr_err("Could not begin reading SCPI response."); | |
695dc859 | 121 | sdi->driver->dev_acquisition_stop(sdi); |
b11afbb1 ML |
122 | return TRUE; |
123 | } | |
124 | devc->state = WAIT_FOR_TRANSFER_OF_BEGIN_TRANSMISSION_COMPLETE; | |
125 | devc->cur_rcv_buffer_position = 0; | |
126 | } else { | |
127 | /* All channels acquired. */ | |
128 | if (devc->cur_acq_channel == ANALOG_CHANNELS - 1) { | |
129 | sr_spew("All channels acquired."); | |
130 | ||
131 | if (devc->cur_acq_frame == devc->frame_limit - 1) { | |
132 | /* All frames accquired. */ | |
133 | sr_spew("All frames acquired."); | |
134 | ||
695dc859 | 135 | sdi->driver->dev_acquisition_stop(sdi); |
b11afbb1 ML |
136 | return TRUE; |
137 | } else { | |
138 | /* Start acquiring next frame. */ | |
139 | if (devc->df_started) { | |
140 | packet.type = SR_DF_FRAME_END; | |
141 | sr_session_send(sdi, &packet); | |
142 | ||
143 | packet.type = SR_DF_FRAME_BEGIN; | |
144 | sr_session_send(sdi, &packet); | |
145 | } | |
146 | ||
147 | devc->cur_acq_frame++; | |
148 | devc->state = START_ACQUISITION; | |
149 | } | |
150 | } else { | |
151 | /* Start acquiring next channel. */ | |
152 | devc->cur_acq_channel++; | |
153 | } | |
154 | } | |
155 | break; | |
156 | case WAIT_FOR_TRANSFER_OF_BEGIN_TRANSMISSION_COMPLETE: | |
695dc859 | 157 | if (read_data(sdi, scpi, devc, 1) == SR_OK) { |
b11afbb1 ML |
158 | if (devc->rcv_buffer[0] == '#') |
159 | devc->state = WAIT_FOR_TRANSFER_OF_DATA_SIZE_DIGIT_COMPLETE; | |
160 | } | |
161 | break; | |
162 | case WAIT_FOR_TRANSFER_OF_DATA_SIZE_DIGIT_COMPLETE: | |
695dc859 | 163 | if (read_data(sdi, scpi, devc, 1) == SR_OK) { |
b11afbb1 ML |
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]); | |
695dc859 | 169 | sdi->driver->dev_acquisition_stop(sdi); |
b11afbb1 ML |
170 | return TRUE; |
171 | } else { | |
172 | devc->data_size_digits = devc->rcv_buffer[0] - '0'; | |
173 | devc->state = WAIT_FOR_TRANSFER_OF_DATA_SIZE_COMPLETE; | |
174 | } | |
175 | } | |
176 | break; | |
177 | case WAIT_FOR_TRANSFER_OF_DATA_SIZE_COMPLETE: | |
695dc859 | 178 | if (read_data(sdi, scpi, devc, devc->data_size_digits) == SR_OK) { |
b11afbb1 ML |
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); | |
695dc859 | 182 | sdi->driver->dev_acquisition_stop(sdi); |
b11afbb1 ML |
183 | return TRUE; |
184 | } else | |
185 | devc->state = WAIT_FOR_TRANSFER_OF_SAMPLE_RATE_COMPLETE; | |
186 | } | |
187 | break; | |
188 | case WAIT_FOR_TRANSFER_OF_SAMPLE_RATE_COMPLETE: | |
695dc859 | 189 | if (read_data(sdi, scpi, devc, sizeof(float)) == SR_OK) { |
b11afbb1 ML |
190 | /* |
191 | * Contrary to the documentation, this field is | |
192 | * transfered with most significant byte first! | |
193 | */ | |
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; | |
197 | ||
198 | if (!devc->df_started) { | |
bee2b016 | 199 | std_session_send_df_header(sdi); |
b11afbb1 ML |
200 | |
201 | packet.type = SR_DF_FRAME_BEGIN; | |
202 | sr_session_send(sdi, &packet); | |
203 | ||
204 | devc->df_started = TRUE; | |
205 | } | |
206 | } | |
207 | break; | |
208 | case WAIT_FOR_TRANSFER_OF_CHANNEL_INDICATOR_COMPLETE: | |
695dc859 | 209 | if (read_data(sdi, scpi, devc, 1) == SR_OK) |
b11afbb1 ML |
210 | devc->state = WAIT_FOR_TRANSFER_OF_RESERVED_DATA_COMPLETE; |
211 | break; | |
212 | case WAIT_FOR_TRANSFER_OF_RESERVED_DATA_COMPLETE: | |
695dc859 | 213 | if (read_data(sdi, scpi, devc, 3) == SR_OK) |
b11afbb1 ML |
214 | devc->state = WAIT_FOR_TRANSFER_OF_CHANNEL_DATA_COMPLETE; |
215 | break; | |
216 | case WAIT_FOR_TRANSFER_OF_CHANNEL_DATA_COMPLETE: | |
695dc859 | 217 | if (read_data(sdi, scpi, devc, devc->data_size - 8) == SR_OK) { |
b11afbb1 ML |
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."); | |
695dc859 | 223 | sdi->driver->dev_acquisition_stop(sdi); |
b11afbb1 ML |
224 | return TRUE; |
225 | } | |
226 | volts_per_division = g_ascii_strtod(response, &end_ptr); | |
227 | if (!strcmp(end_ptr, "mV")) | |
228 | volts_per_division *= 1.e-3; | |
229 | g_free(response); | |
230 | ||
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); | |
234 | ||
235 | /* Convert data. */ | |
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; | |
238 | ||
239 | /* Fill frame. */ | |
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; | |
245 | analog.mqflags = 0; | |
5faebab2 | 246 | packet.type = SR_DF_ANALOG_OLD; |
b11afbb1 | 247 | packet.payload = &analog; |
695dc859 | 248 | sr_session_send(sdi, &packet); |
b11afbb1 ML |
249 | g_slist_free(analog.channels); |
250 | ||
251 | /* All channels acquired. */ | |
252 | if (devc->cur_acq_channel == ANALOG_CHANNELS - 1) { | |
253 | sr_spew("All channels acquired."); | |
254 | ||
255 | if (devc->cur_acq_frame == devc->frame_limit - 1) { | |
256 | /* All frames acquired. */ | |
257 | sr_spew("All frames acquired."); | |
695dc859 | 258 | sdi->driver->dev_acquisition_stop(sdi); |
b11afbb1 ML |
259 | return TRUE; |
260 | } else { | |
261 | /* Start acquiring next frame. */ | |
262 | if (devc->df_started) { | |
263 | packet.type = SR_DF_FRAME_END; | |
264 | sr_session_send(sdi, &packet); | |
265 | ||
266 | packet.type = SR_DF_FRAME_BEGIN; | |
267 | sr_session_send(sdi, &packet); | |
268 | } | |
269 | devc->cur_acq_frame++; | |
270 | devc->state = START_ACQUISITION; | |
271 | } | |
272 | } else { | |
273 | /* Start acquiring next channel. */ | |
274 | devc->state = START_TRANSFER_OF_CHANNEL_DATA; | |
275 | devc->cur_acq_channel++; | |
276 | return TRUE; | |
277 | } | |
278 | } | |
279 | break; | |
7c198f96 ML |
280 | } |
281 | ||
282 | return TRUE; | |
283 | } |