]> sigrok.org Git - libsigrok.git/blob - src/hardware/gwinstek-gds-800/protocol.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / src / hardware / gwinstek-gds-800 / protocol.c
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                                                 std_session_send_df_frame_end(sdi);
145                                                 std_session_send_df_frame_begin(sdi);
146                                         }
147
148                                         devc->cur_acq_frame++;
149                                         devc->state = START_ACQUISITION;
150                                 }
151                         } else {
152                                 /* Start acquiring next channel. */
153                                 devc->cur_acq_channel++;
154                         }
155                 }
156                 break;
157         case WAIT_FOR_TRANSFER_OF_BEGIN_TRANSMISSION_COMPLETE:
158                 if (read_data(sdi, scpi, devc, 1) < 0)
159                         break;
160                 if (devc->rcv_buffer[0] == '#')
161                         devc->state = WAIT_FOR_TRANSFER_OF_DATA_SIZE_DIGIT_COMPLETE;
162                 break;
163         case WAIT_FOR_TRANSFER_OF_DATA_SIZE_DIGIT_COMPLETE:
164                 if (read_data(sdi, scpi, devc, 1) < 0)
165                         break;
166                 if (devc->rcv_buffer[0] != '4' &&
167                         devc->rcv_buffer[0] != '5' &&
168                         devc->rcv_buffer[0] != '6') {
169                         sr_err("Data size digits is not 4, 5 or 6 but "
170                                "'%c'.", devc->rcv_buffer[0]);
171                         sr_dev_acquisition_stop(sdi);
172                         return TRUE;
173                 } else {
174                         devc->data_size_digits = devc->rcv_buffer[0] - '0';
175                         devc->state = WAIT_FOR_TRANSFER_OF_DATA_SIZE_COMPLETE;
176                 }
177                 break;
178         case WAIT_FOR_TRANSFER_OF_DATA_SIZE_COMPLETE:
179                 if (read_data(sdi, scpi, devc, devc->data_size_digits) < 0)
180                         break;
181                 devc->rcv_buffer[devc->data_size_digits] = 0;
182                 if (sr_atoi(devc->rcv_buffer, &devc->data_size) != SR_OK) {
183                         sr_err("Could not parse data size '%s'", devc->rcv_buffer);
184                         sr_dev_acquisition_stop(sdi);
185                         return TRUE;
186                 } else
187                         devc->state = WAIT_FOR_TRANSFER_OF_SAMPLE_RATE_COMPLETE;
188                 break;
189         case WAIT_FOR_TRANSFER_OF_SAMPLE_RATE_COMPLETE:
190                 if (read_data(sdi, scpi, devc, sizeof(float)) < 0)
191                         break;
192                 /*
193                  * Contrary to the documentation, this field is
194                  * transfered with most significant byte first!
195                  */
196                 sample_rate = RB32(devc->rcv_buffer);
197                 memcpy(&devc->sample_rate, &sample_rate, sizeof(float));
198                 devc->state = WAIT_FOR_TRANSFER_OF_CHANNEL_INDICATOR_COMPLETE;
199
200                 if (!devc->df_started) {
201                         std_session_send_df_header(sdi);
202                         std_session_send_df_frame_begin(sdi);
203                         devc->df_started = TRUE;
204                 }
205                 break;
206         case WAIT_FOR_TRANSFER_OF_CHANNEL_INDICATOR_COMPLETE:
207                 if (read_data(sdi, scpi, devc, 1) == SR_OK)
208                         devc->state = WAIT_FOR_TRANSFER_OF_RESERVED_DATA_COMPLETE;
209                 break;
210         case WAIT_FOR_TRANSFER_OF_RESERVED_DATA_COMPLETE:
211                 if (read_data(sdi, scpi, devc, 3) == SR_OK)
212                         devc->state = WAIT_FOR_TRANSFER_OF_CHANNEL_DATA_COMPLETE;
213                 break;
214         case WAIT_FOR_TRANSFER_OF_CHANNEL_DATA_COMPLETE:
215                 if (read_data(sdi, scpi, devc, devc->data_size - 8) < 0)
216                         break;
217
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                         sr_dev_acquisition_stop(sdi);
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                 float vbit = volts_per_division * VERTICAL_DIVISIONS / 256.0;
236                 float vbitlog = log10f(vbit);
237                 int digits = -(int)vbitlog + (vbitlog < 0.0);
238
239                 /* Convert data. */
240                 for (i = 0; i < num_samples; i++)
241                         samples[i] = ((float) ((int16_t) (RB16(&devc->rcv_buffer[i*2])))) * vbit;
242
243                 /* Fill frame. */
244                 sr_analog_init(&analog, &encoding, &meaning, &spec, digits);
245                 analog.meaning->channels = g_slist_append(NULL, g_slist_nth_data(sdi->channels, devc->cur_acq_channel));
246                 analog.num_samples = num_samples;
247                 analog.data = samples;
248                 analog.meaning->mq = SR_MQ_VOLTAGE;
249                 analog.meaning->unit = SR_UNIT_VOLT;
250                 analog.meaning->mqflags = 0;
251                 packet.type = SR_DF_ANALOG;
252                 packet.payload = &analog;
253                 sr_session_send(sdi, &packet);
254                 g_slist_free(analog.meaning->channels);
255
256                 /* All channels acquired. */
257                 if (devc->cur_acq_channel == ANALOG_CHANNELS - 1) {
258                         sr_spew("All channels acquired.");
259
260                         if (devc->cur_acq_frame == devc->frame_limit - 1) {
261                                 /* All frames acquired. */
262                                 sr_spew("All frames acquired.");
263                                 sr_dev_acquisition_stop(sdi);
264                                 return TRUE;
265                         } else {
266                                 /* Start acquiring next frame. */
267                                 if (devc->df_started) {
268                                         std_session_send_df_frame_end(sdi);
269                                         std_session_send_df_frame_begin(sdi);
270                                 }
271                                 devc->cur_acq_frame++;
272                                 devc->state = START_ACQUISITION;
273                         }
274                 } else {
275                         /* Start acquiring next channel. */
276                         devc->state = START_TRANSFER_OF_CHANNEL_DATA;
277                         devc->cur_acq_channel++;
278                         return TRUE;
279                 }
280                 break;
281         }
282
283         return TRUE;
284 }