]>
Commit | Line | Data |
---|---|---|
4bd80e12 | 1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 Bert Vermeulen <bert@biot.com> | |
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 | ||
22 | extern SR_PRIV struct sr_dev_driver p_ols_driver_info; | |
23 | static struct sr_dev_driver *di = &p_ols_driver_info; | |
24 | ||
25 | SR_PRIV int write_shortcommand(struct dev_context *devc, uint8_t command) | |
26 | { | |
27 | uint8_t buf[1]; | |
28 | int bytes_written; | |
29 | ||
30 | sr_dbg("Sending cmd 0x%.2x.", command); | |
31 | buf[0] = command; | |
32 | bytes_written = ftdi_write_data(devc->ftdic, buf, 1); | |
33 | if (bytes_written < 0) { | |
34 | sr_err("Failed to write FTDI data (%d): %s.", | |
35 | bytes_written, ftdi_get_error_string(devc->ftdic)); | |
36 | return SR_ERR; | |
37 | } else if (bytes_written != 1) { | |
38 | sr_err("FTDI write error, only %d/%d bytes written: %s.", | |
39 | bytes_written, 1, ftdi_get_error_string(devc->ftdic)); | |
40 | return SR_ERR; | |
41 | } | |
42 | ||
43 | return SR_OK; | |
44 | } | |
45 | ||
46 | SR_PRIV int write_longcommand(struct dev_context *devc, uint8_t command, uint8_t *data) | |
47 | { | |
48 | uint8_t buf[5]; | |
49 | int bytes_written; | |
50 | ||
51 | sr_dbg("Sending cmd 0x%.2x data 0x%.2x%.2x%.2x%.2x.", command, | |
52 | data[0], data[1], data[2], data[3]); | |
53 | buf[0] = command; | |
54 | buf[1] = data[0]; | |
55 | buf[2] = data[1]; | |
56 | buf[3] = data[2]; | |
57 | buf[4] = data[3]; | |
58 | bytes_written = ftdi_write_data(devc->ftdic, buf, 5); | |
59 | if (bytes_written < 0) { | |
60 | sr_err("Failed to write FTDI data (%d): %s.", | |
61 | bytes_written, ftdi_get_error_string(devc->ftdic)); | |
62 | return SR_ERR; | |
63 | } else if (bytes_written != 5) { | |
64 | sr_err("FTDI write error, only %d/%d bytes written: %s.", | |
65 | bytes_written, 1, ftdi_get_error_string(devc->ftdic)); | |
66 | return SR_ERR; | |
67 | } | |
68 | ||
69 | return SR_OK; | |
70 | } | |
71 | ||
72 | SR_PRIV int p_ols_open(struct dev_context *devc) | |
73 | { | |
74 | int ret; | |
75 | ||
76 | /* Note: Caller checks devc and devc->ftdic. */ | |
77 | ||
78 | /* Select interface B, otherwise communication will fail. */ | |
79 | ret = ftdi_set_interface(devc->ftdic, INTERFACE_B); | |
80 | if (ret < 0) { | |
81 | sr_err("Failed to set FTDI interface B (%d): %s", ret, | |
82 | ftdi_get_error_string(devc->ftdic)); | |
83 | return SR_ERR; | |
84 | } | |
85 | sr_dbg("FTDI chip interface B set successfully."); | |
86 | ||
87 | /* Check for the device and temporarily open it. */ | |
88 | ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID, USB_DEVICE_ID, | |
89 | USB_IPRODUCT, NULL); | |
90 | if (ret < 0) { | |
91 | /* Log errors, except for -3 ("device not found"). */ | |
92 | if (ret != -3) | |
93 | sr_err("Failed to open device (%d): %s", ret, | |
94 | ftdi_get_error_string(devc->ftdic)); | |
95 | return SR_ERR; | |
96 | } | |
97 | sr_dbg("FTDI device opened successfully."); | |
98 | ||
99 | /* Purge RX/TX buffers in the FTDI chip. */ | |
100 | if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) { | |
101 | sr_err("Failed to purge FTDI RX/TX buffers (%d): %s.", | |
102 | ret, ftdi_get_error_string(devc->ftdic)); | |
103 | goto err_open_close_ftdic; | |
104 | } | |
105 | sr_dbg("FTDI chip buffers purged successfully."); | |
106 | ||
107 | /* Reset the FTDI bitmode. */ | |
108 | ret = ftdi_set_bitmode(devc->ftdic, 0xff, BITMODE_RESET); | |
109 | if (ret < 0) { | |
110 | sr_err("Failed to reset the FTDI chip bitmode (%d): %s.", | |
111 | ret, ftdi_get_error_string(devc->ftdic)); | |
112 | goto err_open_close_ftdic; | |
113 | } | |
114 | sr_dbg("FTDI chip bitmode reset successfully."); | |
115 | ||
116 | /* Set the FTDI latency timer to 16. */ | |
117 | ret = ftdi_set_latency_timer(devc->ftdic, 16); | |
118 | if (ret < 0) { | |
119 | sr_err("Failed to set FTDI latency timer (%d): %s.", | |
120 | ret, ftdi_get_error_string(devc->ftdic)); | |
121 | goto err_open_close_ftdic; | |
122 | } | |
123 | sr_dbg("FTDI chip latency timer set successfully."); | |
124 | ||
125 | /* Set the FTDI read data chunk size to 64kB. */ | |
126 | ret = ftdi_read_data_set_chunksize(devc->ftdic, 64 * 1024); | |
127 | if (ret < 0) { | |
128 | sr_err("Failed to set FTDI read data chunk size (%d): %s.", | |
129 | ret, ftdi_get_error_string(devc->ftdic)); | |
130 | goto err_open_close_ftdic; | |
131 | } | |
132 | sr_dbg("FTDI chip read data chunk size set successfully."); | |
133 | ||
134 | return SR_OK; | |
135 | ||
136 | err_open_close_ftdic: | |
1f9bcd0f | 137 | ftdi_usb_close(devc->ftdic); |
4bd80e12 | 138 | return SR_ERR; |
139 | } | |
140 | ||
141 | SR_PRIV int p_ols_close(struct dev_context *devc) | |
142 | { | |
143 | int ret; | |
144 | ||
145 | /* Note: Caller checks devc and devc->ftdic. */ | |
146 | ||
147 | if ((ret = ftdi_usb_close(devc->ftdic)) < 0) { | |
148 | sr_err("Failed to close FTDI device (%d): %s.", | |
149 | ret, ftdi_get_error_string(devc->ftdic)); | |
150 | return SR_ERR; | |
151 | } | |
152 | ||
153 | return SR_OK; | |
154 | } | |
155 | ||
acc885c7 BV |
156 | /* Configures the channel mask based on which channels are enabled. */ |
157 | SR_PRIV void pols_channel_mask(const struct sr_dev_inst *sdi) | |
4bd80e12 | 158 | { |
159 | struct dev_context *devc; | |
acc885c7 | 160 | struct sr_channel *channel; |
4bd80e12 | 161 | const GSList *l; |
4bd80e12 | 162 | |
163 | devc = sdi->priv; | |
164 | ||
165 | devc->channel_mask = 0; | |
acc885c7 BV |
166 | for (l = sdi->channels; l; l = l->next) { |
167 | channel = l->data; | |
168 | if (channel->enabled) | |
169 | devc->channel_mask |= 1 << channel->index; | |
170 | } | |
171 | } | |
172 | ||
173 | SR_PRIV int pols_convert_trigger(const struct sr_dev_inst *sdi) | |
174 | { | |
175 | struct dev_context *devc; | |
176 | struct sr_trigger *trigger; | |
177 | struct sr_trigger_stage *stage; | |
178 | struct sr_trigger_match *match; | |
179 | const GSList *l, *m; | |
180 | int i; | |
181 | ||
182 | devc = sdi->priv; | |
183 | ||
184 | devc->num_stages = 0; | |
4bd80e12 | 185 | for (i = 0; i < NUM_TRIGGER_STAGES; i++) { |
186 | devc->trigger_mask[i] = 0; | |
187 | devc->trigger_value[i] = 0; | |
1e0de846 | 188 | devc->trigger_edge[i] = 0; |
4bd80e12 | 189 | } |
190 | ||
acc885c7 BV |
191 | if (!(trigger = sr_session_trigger_get(sdi->session))) |
192 | return SR_OK; | |
4bd80e12 | 193 | |
acc885c7 BV |
194 | devc->num_stages = g_slist_length(trigger->stages); |
195 | if (devc->num_stages > NUM_TRIGGER_STAGES) { | |
196 | sr_err("This device only supports %d trigger stages.", | |
197 | NUM_TRIGGER_STAGES); | |
198 | return SR_ERR; | |
199 | } | |
4bd80e12 | 200 | |
acc885c7 BV |
201 | for (l = trigger->stages; l; l = l->next) { |
202 | stage = l->data; | |
203 | for (m = stage->matches; m; m = m->next) { | |
204 | match = m->data; | |
205 | if (!match->channel->enabled) | |
206 | /* Ignore disabled channels with a trigger. */ | |
207 | continue; | |
208 | devc->trigger_mask[stage->stage] |= 1 << match->channel->index; | |
209 | if (match->match == SR_TRIGGER_ONE || match->match == SR_TRIGGER_RISING) | |
210 | devc->trigger_value[stage->stage] |= 1 << match->channel->index; | |
211 | if (match->match == SR_TRIGGER_RISING || match->match == SR_TRIGGER_FALLING) | |
212 | devc->trigger_edge[stage->stage] |= 1 << match->channel->index; | |
4bd80e12 | 213 | } |
4bd80e12 | 214 | } |
215 | ||
216 | return SR_OK; | |
217 | } | |
218 | ||
219 | SR_PRIV struct sr_dev_inst *p_ols_get_metadata(uint8_t *buf, int bytes_read, struct dev_context *devc) | |
220 | { | |
221 | struct sr_dev_inst *sdi; | |
222 | struct sr_channel *ch; | |
223 | uint32_t tmp_int, ui; | |
224 | uint8_t key, type, token; | |
225 | GString *tmp_str, *devname, *version; | |
226 | guchar tmp_c; | |
227 | int index, i; | |
228 | ||
aed4ad0b | 229 | sdi = sr_dev_inst_new(SR_ST_INACTIVE, NULL, NULL, NULL); |
4bd80e12 | 230 | sdi->driver = di; |
231 | sdi->priv = devc; | |
232 | ||
233 | devname = g_string_new(""); | |
234 | version = g_string_new(""); | |
235 | ||
236 | index = 0; | |
237 | while (index < bytes_read) { | |
238 | key = buf[index++]; | |
239 | if (key == 0x00) { | |
240 | sr_dbg("Got metadata key 0x00, metadata ends."); | |
241 | break; | |
242 | } | |
243 | type = key >> 5; | |
244 | token = key & 0x1f; | |
245 | switch (type) { | |
246 | case 0: | |
247 | /* NULL-terminated string */ | |
248 | tmp_str = g_string_new(""); | |
249 | while ((index < bytes_read) && ((tmp_c = buf[index++]) != '\0')) | |
250 | g_string_append_c(tmp_str, tmp_c); | |
251 | sr_dbg("Got metadata key 0x%.2x value '%s'.", | |
252 | key, tmp_str->str); | |
253 | switch (token) { | |
254 | case 0x01: | |
255 | /* Device name */ | |
256 | devname = g_string_append(devname, tmp_str->str); | |
257 | break; | |
258 | case 0x02: | |
259 | /* FPGA firmware version */ | |
260 | if (version->len) | |
261 | g_string_append(version, ", "); | |
262 | g_string_append(version, "FPGA version "); | |
263 | g_string_append(version, tmp_str->str); | |
264 | break; | |
265 | case 0x03: | |
266 | /* Ancillary version */ | |
267 | if (version->len) | |
268 | g_string_append(version, ", "); | |
269 | g_string_append(version, "Ancillary version "); | |
270 | g_string_append(version, tmp_str->str); | |
271 | break; | |
272 | default: | |
273 | sr_info("Unknown token 0x%.2x: '%s'", | |
274 | token, tmp_str->str); | |
275 | break; | |
276 | } | |
277 | g_string_free(tmp_str, TRUE); | |
278 | break; | |
279 | case 1: | |
280 | /* 32-bit unsigned integer */ | |
281 | tmp_int = 0; | |
282 | for (i = 0; i < 4; i++) { | |
283 | tmp_int = (tmp_int << 8) | buf[index++]; | |
284 | } | |
285 | sr_dbg("Got metadata key 0x%.2x value 0x%.8x.", | |
286 | key, tmp_int); | |
287 | switch (token) { | |
288 | case 0x00: | |
289 | /* Number of usable channels */ | |
290 | for (ui = 0; ui < tmp_int; ui++) { | |
291 | if (!(ch = sr_channel_new(ui, SR_CHANNEL_LOGIC, TRUE, | |
292 | p_ols_channel_names[ui]))) | |
293 | return 0; | |
294 | sdi->channels = g_slist_append(sdi->channels, ch); | |
295 | } | |
296 | break; | |
297 | case 0x01: | |
298 | /* Amount of sample memory available (bytes) */ | |
b94cff40 | 299 | devc->max_samplebytes = tmp_int; |
4bd80e12 | 300 | break; |
301 | case 0x02: | |
302 | /* Amount of dynamic memory available (bytes) */ | |
303 | /* what is this for? */ | |
304 | break; | |
305 | case 0x03: | |
306 | /* Maximum sample rate (hz) */ | |
307 | devc->max_samplerate = tmp_int; | |
308 | break; | |
309 | case 0x04: | |
310 | /* protocol version */ | |
311 | devc->protocol_version = tmp_int; | |
312 | break; | |
313 | default: | |
314 | sr_info("Unknown token 0x%.2x: 0x%.8x.", | |
315 | token, tmp_int); | |
316 | break; | |
317 | } | |
318 | break; | |
319 | case 2: | |
320 | /* 8-bit unsigned integer */ | |
321 | tmp_c = buf[index++]; | |
322 | sr_dbg("Got metadata key 0x%.2x value 0x%.2x.", | |
323 | key, tmp_c); | |
324 | switch (token) { | |
325 | case 0x00: | |
326 | /* Number of usable channels */ | |
327 | for (ui = 0; ui < tmp_c; ui++) { | |
328 | if (!(ch = sr_channel_new(ui, SR_CHANNEL_LOGIC, TRUE, | |
329 | p_ols_channel_names[ui]))) | |
330 | return 0; | |
331 | sdi->channels = g_slist_append(sdi->channels, ch); | |
332 | } | |
333 | break; | |
334 | case 0x01: | |
335 | /* protocol version */ | |
336 | devc->protocol_version = tmp_c; | |
337 | break; | |
338 | default: | |
339 | sr_info("Unknown token 0x%.2x: 0x%.2x.", | |
340 | token, tmp_c); | |
341 | break; | |
342 | } | |
343 | break; | |
344 | default: | |
345 | /* unknown type */ | |
346 | break; | |
347 | } | |
348 | } | |
349 | ||
350 | sdi->model = devname->str; | |
351 | sdi->version = version->str; | |
352 | g_string_free(devname, FALSE); | |
353 | g_string_free(version, FALSE); | |
354 | ||
355 | return sdi; | |
356 | } | |
357 | ||
358 | SR_PRIV int p_ols_set_samplerate(const struct sr_dev_inst *sdi, | |
359 | const uint64_t samplerate) | |
360 | { | |
361 | struct dev_context *devc; | |
362 | ||
363 | devc = sdi->priv; | |
364 | if (devc->max_samplerate && samplerate > devc->max_samplerate) | |
365 | return SR_ERR_SAMPLERATE; | |
366 | ||
367 | if (samplerate > CLOCK_RATE) { | |
368 | sr_info("Enabling demux mode."); | |
369 | devc->flag_reg |= FLAG_DEMUX; | |
370 | devc->flag_reg &= ~FLAG_FILTER; | |
371 | devc->max_channels = NUM_CHANNELS / 2; | |
372 | devc->cur_samplerate_divider = (CLOCK_RATE * 2 / samplerate) - 1; | |
373 | } else { | |
374 | sr_info("Disabling demux mode."); | |
375 | devc->flag_reg &= ~FLAG_DEMUX; | |
376 | devc->flag_reg |= FLAG_FILTER; | |
377 | devc->max_channels = NUM_CHANNELS; | |
378 | devc->cur_samplerate_divider = (CLOCK_RATE / samplerate) - 1; | |
379 | } | |
380 | ||
381 | /* Calculate actual samplerate used and complain if it is different | |
382 | * from the requested. | |
383 | */ | |
384 | devc->cur_samplerate = CLOCK_RATE / (devc->cur_samplerate_divider + 1); | |
385 | if (devc->flag_reg & FLAG_DEMUX) | |
386 | devc->cur_samplerate *= 2; | |
387 | if (devc->cur_samplerate != samplerate) | |
388 | sr_info("Can't match samplerate %" PRIu64 ", using %" | |
389 | PRIu64 ".", samplerate, devc->cur_samplerate); | |
390 | ||
391 | return SR_OK; | |
392 | } | |
393 | ||
394 | ||
395 | SR_PRIV int p_ols_receive_data(int fd, int revents, void *cb_data) | |
396 | { | |
397 | struct dev_context *devc; | |
398 | struct sr_dev_inst *sdi; | |
399 | struct sr_datafeed_packet packet; | |
400 | struct sr_datafeed_logic logic; | |
401 | uint32_t sample; | |
402 | int num_channels, offset, j; | |
403 | int bytes_read, index; | |
404 | unsigned int i; | |
405 | unsigned char byte; | |
406 | ||
407 | (void)fd; | |
408 | (void)revents; | |
409 | ||
410 | sdi = cb_data; | |
411 | devc = sdi->priv; | |
412 | ||
413 | if (devc->num_transfers++ == 0) { | |
414 | devc->raw_sample_buf = g_try_malloc(devc->limit_samples * 4); | |
415 | if (!devc->raw_sample_buf) { | |
416 | sr_err("Sample buffer malloc failed."); | |
417 | return FALSE; | |
418 | } | |
419 | /* fill with 1010... for debugging */ | |
420 | memset(devc->raw_sample_buf, 0x82, devc->limit_samples * 4); | |
421 | } | |
422 | ||
b94cff40 | 423 | if ((devc->num_samples < devc->limit_samples) && (devc->cnt_samples < devc->max_samples)) { |
4bd80e12 | 424 | |
425 | num_channels = 0; | |
426 | for (i = NUM_CHANNELS; i > 0x02; i /= 2) { | |
427 | if ((devc->flag_reg & i) == 0) { | |
428 | num_channels++; | |
429 | } | |
430 | } | |
431 | ||
432 | /* Get a block of data. */ | |
433 | bytes_read = ftdi_read_data(devc->ftdic, devc->ftdi_buf, FTDI_BUF_SIZE); | |
434 | if (bytes_read < 0) { | |
435 | sr_err("Failed to read FTDI data (%d): %s.", | |
436 | bytes_read, ftdi_get_error_string(devc->ftdic)); | |
437 | sdi->driver->dev_acquisition_stop(sdi, sdi); | |
438 | return FALSE; | |
439 | } | |
440 | if (bytes_read == 0) { | |
441 | sr_spew("Received 0 bytes, nothing to do."); | |
442 | return TRUE; | |
443 | } | |
444 | ||
445 | sr_dbg("Received %d bytes", bytes_read); | |
446 | ||
447 | index = 0; | |
448 | while (index < bytes_read) { | |
449 | byte = devc->ftdi_buf[index++]; | |
450 | devc->cnt_bytes++; | |
451 | ||
452 | devc->sample[devc->num_bytes++] = byte; | |
453 | sr_spew("Received byte 0x%.2x.", byte); | |
b94cff40 | 454 | |
455 | if ((devc->flag_reg & FLAG_DEMUX) && (devc->flag_reg & FLAG_RLE)) { | |
456 | /* RLE in demux mode must be processed differently | |
457 | * since in this case the RLE encoder is operating on pairs of samples. | |
458 | */ | |
459 | if (devc->num_bytes == num_channels * 2) { | |
460 | devc->cnt_samples += 2; | |
461 | devc->cnt_samples_rle += 2; | |
4bd80e12 | 462 | /* |
b94cff40 | 463 | * Got a sample pair. Convert from the OLS's little-endian |
464 | * sample to the local format. | |
465 | */ | |
466 | sample = devc->sample[0] | (devc->sample[1] << 8) \ | |
467 | | (devc->sample[2] << 16) | (devc->sample[3] << 24); | |
468 | sr_spew("Received sample pair 0x%.*x.", devc->num_bytes * 2, sample); | |
469 | ||
470 | /* | |
471 | * In RLE mode the high bit of the sample pair is the | |
472 | * "count" flag, meaning this sample pair is the number | |
473 | * of times the previous sample pair occurred. | |
4bd80e12 | 474 | */ |
475 | if (devc->sample[devc->num_bytes - 1] & 0x80) { | |
476 | /* Clear the high bit. */ | |
477 | sample &= ~(0x80 << (devc->num_bytes - 1) * 8); | |
478 | devc->rle_count = sample; | |
b94cff40 | 479 | devc->cnt_samples_rle += devc->rle_count * 2; |
480 | sr_dbg("RLE count: %u.", devc->rle_count * 2); | |
4bd80e12 | 481 | devc->num_bytes = 0; |
482 | continue; | |
483 | } | |
b94cff40 | 484 | devc->num_samples += (devc->rle_count + 1) * 2; |
485 | if (devc->num_samples > devc->limit_samples) { | |
486 | /* Save us from overrunning the buffer. */ | |
487 | devc->rle_count -= (devc->num_samples - devc->limit_samples) / 2; | |
488 | devc->num_samples = devc->limit_samples; | |
489 | index = bytes_read; | |
490 | } | |
4bd80e12 | 491 | |
4bd80e12 | 492 | /* |
493 | * Some channel groups may have been turned | |
494 | * off, to speed up transfer between the | |
495 | * hardware and the PC. Expand that here before | |
496 | * submitting it over the session bus -- | |
497 | * whatever is listening on the bus will be | |
498 | * expecting a full 32-bit sample, based on | |
499 | * the number of channels. | |
500 | */ | |
501 | j = 0; | |
b94cff40 | 502 | /* expand first sample */ |
4bd80e12 | 503 | memset(devc->tmp_sample, 0, 4); |
b94cff40 | 504 | for (i = 0; i < 2; i++) { |
4bd80e12 | 505 | if (((devc->flag_reg >> 2) & (1 << i)) == 0) { |
506 | /* | |
507 | * This channel group was | |
508 | * enabled, copy from received | |
509 | * sample. | |
510 | */ | |
511 | devc->tmp_sample[i] = devc->sample[j++]; | |
b94cff40 | 512 | } |
4bd80e12 | 513 | } |
b94cff40 | 514 | /* Clear out the most significant bit of the sample */ |
515 | devc->tmp_sample[devc->num_bytes - 1] &= 0x7f; | |
516 | sr_spew("Expanded sample 1: 0x%.8x.", devc->tmp_sample); | |
517 | ||
518 | /* expand second sample */ | |
519 | memset(devc->tmp_sample2, 0, 4); | |
520 | for (i = 0; i < 2; i++) { | |
521 | if (((devc->flag_reg >> 2) & (1 << i)) == 0) { | |
522 | /* | |
523 | * This channel group was | |
524 | * enabled, copy from received | |
525 | * sample. | |
526 | */ | |
527 | devc->tmp_sample2[i] = devc->sample[j++]; | |
528 | } | |
529 | } | |
530 | /* Clear out the most significant bit of the sample */ | |
531 | devc->tmp_sample2[devc->num_bytes - 1] &= 0x7f; | |
532 | sr_spew("Expanded sample 2: 0x%.8x.", devc->tmp_sample2); | |
533 | ||
534 | /* | |
535 | * OLS sends its sample buffer backwards. | |
536 | * store it in reverse order here, so we can dump | |
537 | * this on the session bus later. | |
538 | */ | |
539 | offset = (devc->limit_samples - devc->num_samples) * 4; | |
540 | for (i = 0; i <= devc->rle_count; i++) { | |
541 | memcpy(devc->raw_sample_buf + offset + (i * 8), | |
542 | devc->tmp_sample2, 4); | |
543 | memcpy(devc->raw_sample_buf + offset + (4 + (i * 8)), | |
544 | devc->tmp_sample, 4); | |
545 | } | |
546 | memset(devc->sample, 0, 4); | |
547 | devc->num_bytes = 0; | |
548 | devc->rle_count = 0; | |
4bd80e12 | 549 | } |
b94cff40 | 550 | } |
551 | else { | |
552 | if (devc->num_bytes == num_channels) { | |
553 | devc->cnt_samples++; | |
554 | devc->cnt_samples_rle++; | |
555 | /* | |
556 | * Got a full sample. Convert from the OLS's little-endian | |
557 | * sample to the local format. | |
558 | */ | |
559 | sample = devc->sample[0] | (devc->sample[1] << 8) \ | |
560 | | (devc->sample[2] << 16) | (devc->sample[3] << 24); | |
561 | sr_spew("Received sample 0x%.*x.", devc->num_bytes * 2, sample); | |
562 | if (devc->flag_reg & FLAG_RLE) { | |
563 | /* | |
564 | * In RLE mode the high bit of the sample is the | |
565 | * "count" flag, meaning this sample is the number | |
566 | * of times the previous sample occurred. | |
567 | */ | |
568 | if (devc->sample[devc->num_bytes - 1] & 0x80) { | |
569 | /* Clear the high bit. */ | |
570 | sample &= ~(0x80 << (devc->num_bytes - 1) * 8); | |
571 | devc->rle_count = sample; | |
572 | devc->cnt_samples_rle += devc->rle_count; | |
573 | sr_dbg("RLE count: %u.", devc->rle_count); | |
574 | devc->num_bytes = 0; | |
575 | continue; | |
576 | } | |
577 | } | |
578 | devc->num_samples += devc->rle_count + 1; | |
579 | if (devc->num_samples > devc->limit_samples) { | |
580 | /* Save us from overrunning the buffer. */ | |
581 | devc->rle_count -= devc->num_samples - devc->limit_samples; | |
582 | devc->num_samples = devc->limit_samples; | |
583 | index = bytes_read; | |
584 | } | |
585 | ||
586 | if (num_channels < 4) { | |
587 | /* | |
588 | * Some channel groups may have been turned | |
589 | * off, to speed up transfer between the | |
590 | * hardware and the PC. Expand that here before | |
591 | * submitting it over the session bus -- | |
592 | * whatever is listening on the bus will be | |
593 | * expecting a full 32-bit sample, based on | |
594 | * the number of channels. | |
595 | */ | |
596 | j = 0; | |
597 | memset(devc->tmp_sample, 0, 4); | |
598 | for (i = 0; i < 4; i++) { | |
599 | if (((devc->flag_reg >> 2) & (1 << i)) == 0) { | |
600 | /* | |
601 | * This channel group was | |
602 | * enabled, copy from received | |
603 | * sample. | |
604 | */ | |
605 | devc->tmp_sample[i] = devc->sample[j++]; | |
606 | } | |
607 | } | |
608 | memcpy(devc->sample, devc->tmp_sample, 4); | |
609 | sr_spew("Expanded sample: 0x%.8x.", sample); | |
610 | } | |
4bd80e12 | 611 | |
b94cff40 | 612 | /* |
613 | * Pipistrello OLS sends its sample buffer backwards. | |
614 | * store it in reverse order here, so we can dump | |
615 | * this on the session bus later. | |
616 | */ | |
617 | offset = (devc->limit_samples - devc->num_samples) * 4; | |
618 | for (i = 0; i <= devc->rle_count; i++) { | |
619 | memcpy(devc->raw_sample_buf + offset + (i * 4), | |
620 | devc->sample, 4); | |
621 | } | |
622 | memset(devc->sample, 0, 4); | |
623 | devc->num_bytes = 0; | |
624 | devc->rle_count = 0; | |
4bd80e12 | 625 | } |
4bd80e12 | 626 | } |
627 | } | |
628 | return TRUE; | |
629 | } else { | |
b94cff40 | 630 | do bytes_read = ftdi_read_data(devc->ftdic, devc->ftdi_buf, FTDI_BUF_SIZE); |
631 | while (bytes_read > 0); | |
632 | ||
4bd80e12 | 633 | /* |
634 | * We've acquired all the samples we asked for -- we're done. | |
635 | * Send the (properly-ordered) buffer to the frontend. | |
636 | */ | |
637 | sr_dbg("Received %d bytes, %d samples, %d decompressed samples.", | |
638 | devc->cnt_bytes, devc->cnt_samples, | |
639 | devc->cnt_samples_rle); | |
640 | if (devc->trigger_at != -1) { | |
641 | /* | |
642 | * A trigger was set up, so we need to tell the frontend | |
643 | * about it. | |
644 | */ | |
645 | if (devc->trigger_at > 0) { | |
646 | /* There are pre-trigger samples, send those first. */ | |
647 | packet.type = SR_DF_LOGIC; | |
648 | packet.payload = &logic; | |
649 | logic.length = devc->trigger_at * 4; | |
650 | logic.unitsize = 4; | |
651 | logic.data = devc->raw_sample_buf + | |
652 | (devc->limit_samples - devc->num_samples) * 4; | |
653 | sr_session_send(cb_data, &packet); | |
654 | } | |
655 | ||
656 | /* Send the trigger. */ | |
657 | packet.type = SR_DF_TRIGGER; | |
658 | sr_session_send(cb_data, &packet); | |
659 | ||
660 | /* Send post-trigger samples. */ | |
661 | packet.type = SR_DF_LOGIC; | |
662 | packet.payload = &logic; | |
663 | logic.length = (devc->num_samples * 4) - (devc->trigger_at * 4); | |
664 | logic.unitsize = 4; | |
665 | logic.data = devc->raw_sample_buf + devc->trigger_at * 4 + | |
666 | (devc->limit_samples - devc->num_samples) * 4; | |
667 | sr_session_send(cb_data, &packet); | |
668 | } else { | |
669 | /* no trigger was used */ | |
670 | packet.type = SR_DF_LOGIC; | |
671 | packet.payload = &logic; | |
672 | logic.length = devc->num_samples * 4; | |
673 | logic.unitsize = 4; | |
674 | logic.data = devc->raw_sample_buf + | |
675 | (devc->limit_samples - devc->num_samples) * 4; | |
676 | sr_session_send(cb_data, &packet); | |
677 | } | |
678 | g_free(devc->raw_sample_buf); | |
679 | ||
680 | sdi->driver->dev_acquisition_stop(sdi, cb_data); | |
681 | } | |
682 | ||
683 | return TRUE; | |
684 | } |