]>
Commit | Line | Data |
---|---|---|
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" | |
21 | #include <math.h> | |
22 | #include <string.h> | |
23 | ||
24 | #define ANALOG_CHANNELS 2 | |
25 | #define VERTICAL_DIVISIONS 10 | |
26 | ||
27 | static int read_data(struct sr_dev_inst *sdi, | |
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."); | |
38 | sr_dev_acquisition_stop(sdi); | |
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."); | |
58 | sr_dev_acquisition_stop(sdi); | |
59 | devc->cur_rcv_buffer_position = 0; | |
60 | return SR_ERR; | |
61 | } | |
62 | } | |
63 | ||
64 | SR_PRIV int gwinstek_gds_800_receive_data(int fd, int revents, void *cb_data) | |
65 | { | |
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; | |
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; | |
81 | ||
82 | (void)fd; | |
83 | ||
84 | if (!(sdi = cb_data)) | |
85 | return TRUE; | |
86 | ||
87 | if (!(devc = sdi->priv)) | |
88 | return TRUE; | |
89 | ||
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."); | |
99 | sr_dev_acquisition_stop(sdi); | |
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."); | |
104 | sr_dev_acquisition_stop(sdi); | |
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."); | |
109 | sr_dev_acquisition_stop(sdi); | |
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."); | |
120 | sr_dev_acquisition_stop(sdi); | |
121 | return TRUE; | |
122 | } | |
123 | if (sr_scpi_read_begin(scpi) != SR_OK) { | |
124 | sr_err("Could not begin reading SCPI response."); | |
125 | sr_dev_acquisition_stop(sdi); | |
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."); | |
138 | ||
139 | sr_dev_acquisition_stop(sdi); | |
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); | |
146 | ||
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: | |
161 | if (read_data(sdi, scpi, devc, 1) < 0) | |
162 | break; | |
163 | if (devc->rcv_buffer[0] == '#') | |
164 | devc->state = WAIT_FOR_TRANSFER_OF_DATA_SIZE_DIGIT_COMPLETE; | |
165 | break; | |
166 | case WAIT_FOR_TRANSFER_OF_DATA_SIZE_DIGIT_COMPLETE: | |
167 | if (read_data(sdi, scpi, devc, 1) < 0) | |
168 | break; | |
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); | |
175 | return TRUE; | |
176 | } else { | |
177 | devc->data_size_digits = devc->rcv_buffer[0] - '0'; | |
178 | devc->state = WAIT_FOR_TRANSFER_OF_DATA_SIZE_COMPLETE; | |
179 | } | |
180 | break; | |
181 | case WAIT_FOR_TRANSFER_OF_DATA_SIZE_COMPLETE: | |
182 | if (read_data(sdi, scpi, devc, devc->data_size_digits) < 0) | |
183 | break; | |
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); | |
188 | return TRUE; | |
189 | } else | |
190 | devc->state = WAIT_FOR_TRANSFER_OF_SAMPLE_RATE_COMPLETE; | |
191 | break; | |
192 | case WAIT_FOR_TRANSFER_OF_SAMPLE_RATE_COMPLETE: | |
193 | if (read_data(sdi, scpi, devc, sizeof(float)) < 0) | |
194 | break; | |
195 | /* | |
196 | * Contrary to the documentation, this field is | |
197 | * transfered with most significant byte first! | |
198 | */ | |
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; | |
202 | ||
203 | if (!devc->df_started) { | |
204 | std_session_send_df_header(sdi); | |
205 | ||
206 | packet.type = SR_DF_FRAME_BEGIN; | |
207 | sr_session_send(sdi, &packet); | |
208 | ||
209 | devc->df_started = TRUE; | |
210 | } | |
211 | break; | |
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; | |
215 | break; | |
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; | |
219 | break; | |
220 | case WAIT_FOR_TRANSFER_OF_CHANNEL_DATA_COMPLETE: | |
221 | if (read_data(sdi, scpi, devc, devc->data_size - 8) < 0) | |
222 | break; | |
223 | ||
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); | |
230 | return TRUE; | |
231 | } | |
232 | volts_per_division = g_ascii_strtod(response, &end_ptr); | |
233 | if (!strcmp(end_ptr, "mV")) | |
234 | volts_per_division *= 1.e-3; | |
235 | g_free(response); | |
236 | ||
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); | |
240 | ||
241 | float vbit = volts_per_division * VERTICAL_DIVISIONS / 256.0; | |
242 | float vbitlog = log10f(vbit); | |
243 | int digits = -(int)vbitlog + (vbitlog < 0.0); | |
244 | ||
245 | /* Convert data. */ | |
246 | for (i = 0; i < num_samples; i++) | |
247 | samples[i] = ((float) ((int16_t) (RB16(&devc->rcv_buffer[i*2])))) * vbit; | |
248 | ||
249 | /* Fill frame. */ | |
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); | |
261 | ||
262 | /* All channels acquired. */ | |
263 | if (devc->cur_acq_channel == ANALOG_CHANNELS - 1) { | |
264 | sr_spew("All channels acquired."); | |
265 | ||
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); | |
270 | return TRUE; | |
271 | } else { | |
272 | /* Start acquiring next frame. */ | |
273 | if (devc->df_started) { | |
274 | packet.type = SR_DF_FRAME_END; | |
275 | sr_session_send(sdi, &packet); | |
276 | ||
277 | packet.type = SR_DF_FRAME_BEGIN; | |
278 | sr_session_send(sdi, &packet); | |
279 | } | |
280 | devc->cur_acq_frame++; | |
281 | devc->state = START_ACQUISITION; | |
282 | } | |
283 | } else { | |
284 | /* Start acquiring next channel. */ | |
285 | devc->state = START_TRANSFER_OF_CHANNEL_DATA; | |
286 | devc->cur_acq_channel++; | |
287 | return TRUE; | |
288 | } | |
289 | break; | |
290 | } | |
291 | ||
292 | return TRUE; | |
293 | } |