]>
Commit | Line | Data |
---|---|---|
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: | |
137 | ftdi_usb_close(devc->ftdic); | |
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 | ||
156 | /* Configures the channel mask based on which channels are enabled. */ | |
157 | SR_PRIV void pols_channel_mask(const struct sr_dev_inst *sdi) | |
158 | { | |
159 | struct dev_context *devc; | |
160 | struct sr_channel *channel; | |
161 | const GSList *l; | |
162 | ||
163 | devc = sdi->priv; | |
164 | ||
165 | devc->channel_mask = 0; | |
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; | |
185 | for (i = 0; i < NUM_TRIGGER_STAGES; i++) { | |
186 | devc->trigger_mask[i] = 0; | |
187 | devc->trigger_value[i] = 0; | |
188 | devc->trigger_edge[i] = 0; | |
189 | } | |
190 | ||
191 | if (!(trigger = sr_session_trigger_get(sdi->session))) | |
192 | return SR_OK; | |
193 | ||
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 | } | |
200 | ||
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; | |
213 | } | |
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 | ||
229 | sdi = sr_dev_inst_new(); | |
230 | sdi->status = SR_ST_INACTIVE; | |
231 | sdi->driver = di; | |
232 | sdi->priv = devc; | |
233 | ||
234 | devname = g_string_new(""); | |
235 | version = g_string_new(""); | |
236 | ||
237 | index = 0; | |
238 | while (index < bytes_read) { | |
239 | key = buf[index++]; | |
240 | if (key == 0x00) { | |
241 | sr_dbg("Got metadata key 0x00, metadata ends."); | |
242 | break; | |
243 | } | |
244 | type = key >> 5; | |
245 | token = key & 0x1f; | |
246 | switch (type) { | |
247 | case 0: | |
248 | /* NULL-terminated string */ | |
249 | tmp_str = g_string_new(""); | |
250 | while ((index < bytes_read) && ((tmp_c = buf[index++]) != '\0')) | |
251 | g_string_append_c(tmp_str, tmp_c); | |
252 | sr_dbg("Got metadata key 0x%.2x value '%s'.", | |
253 | key, tmp_str->str); | |
254 | switch (token) { | |
255 | case 0x01: | |
256 | /* Device name */ | |
257 | devname = g_string_append(devname, tmp_str->str); | |
258 | break; | |
259 | case 0x02: | |
260 | /* FPGA firmware version */ | |
261 | if (version->len) | |
262 | g_string_append(version, ", "); | |
263 | g_string_append(version, "FPGA version "); | |
264 | g_string_append(version, tmp_str->str); | |
265 | break; | |
266 | case 0x03: | |
267 | /* Ancillary version */ | |
268 | if (version->len) | |
269 | g_string_append(version, ", "); | |
270 | g_string_append(version, "Ancillary version "); | |
271 | g_string_append(version, tmp_str->str); | |
272 | break; | |
273 | default: | |
274 | sr_info("Unknown token 0x%.2x: '%s'", | |
275 | token, tmp_str->str); | |
276 | break; | |
277 | } | |
278 | g_string_free(tmp_str, TRUE); | |
279 | break; | |
280 | case 1: | |
281 | /* 32-bit unsigned integer */ | |
282 | tmp_int = 0; | |
283 | for (i = 0; i < 4; i++) { | |
284 | tmp_int = (tmp_int << 8) | buf[index++]; | |
285 | } | |
286 | sr_dbg("Got metadata key 0x%.2x value 0x%.8x.", | |
287 | key, tmp_int); | |
288 | switch (token) { | |
289 | case 0x00: | |
290 | /* Number of usable channels */ | |
291 | for (ui = 0; ui < tmp_int; ui++) { | |
292 | if (!(ch = sr_channel_new(ui, SR_CHANNEL_LOGIC, TRUE, | |
293 | p_ols_channel_names[ui]))) | |
294 | return 0; | |
295 | sdi->channels = g_slist_append(sdi->channels, ch); | |
296 | } | |
297 | break; | |
298 | case 0x01: | |
299 | /* Amount of sample memory available (bytes) */ | |
300 | devc->max_samplebytes = tmp_int; | |
301 | break; | |
302 | case 0x02: | |
303 | /* Amount of dynamic memory available (bytes) */ | |
304 | /* what is this for? */ | |
305 | break; | |
306 | case 0x03: | |
307 | /* Maximum sample rate (hz) */ | |
308 | devc->max_samplerate = tmp_int; | |
309 | break; | |
310 | case 0x04: | |
311 | /* protocol version */ | |
312 | devc->protocol_version = tmp_int; | |
313 | break; | |
314 | default: | |
315 | sr_info("Unknown token 0x%.2x: 0x%.8x.", | |
316 | token, tmp_int); | |
317 | break; | |
318 | } | |
319 | break; | |
320 | case 2: | |
321 | /* 8-bit unsigned integer */ | |
322 | tmp_c = buf[index++]; | |
323 | sr_dbg("Got metadata key 0x%.2x value 0x%.2x.", | |
324 | key, tmp_c); | |
325 | switch (token) { | |
326 | case 0x00: | |
327 | /* Number of usable channels */ | |
328 | for (ui = 0; ui < tmp_c; ui++) { | |
329 | if (!(ch = sr_channel_new(ui, SR_CHANNEL_LOGIC, TRUE, | |
330 | p_ols_channel_names[ui]))) | |
331 | return 0; | |
332 | sdi->channels = g_slist_append(sdi->channels, ch); | |
333 | } | |
334 | break; | |
335 | case 0x01: | |
336 | /* protocol version */ | |
337 | devc->protocol_version = tmp_c; | |
338 | break; | |
339 | default: | |
340 | sr_info("Unknown token 0x%.2x: 0x%.2x.", | |
341 | token, tmp_c); | |
342 | break; | |
343 | } | |
344 | break; | |
345 | default: | |
346 | /* unknown type */ | |
347 | break; | |
348 | } | |
349 | } | |
350 | ||
351 | sdi->model = devname->str; | |
352 | sdi->version = version->str; | |
353 | g_string_free(devname, FALSE); | |
354 | g_string_free(version, FALSE); | |
355 | ||
356 | return sdi; | |
357 | } | |
358 | ||
359 | SR_PRIV int p_ols_set_samplerate(const struct sr_dev_inst *sdi, | |
360 | const uint64_t samplerate) | |
361 | { | |
362 | struct dev_context *devc; | |
363 | ||
364 | devc = sdi->priv; | |
365 | if (devc->max_samplerate && samplerate > devc->max_samplerate) | |
366 | return SR_ERR_SAMPLERATE; | |
367 | ||
368 | if (samplerate > CLOCK_RATE) { | |
369 | sr_info("Enabling demux mode."); | |
370 | devc->flag_reg |= FLAG_DEMUX; | |
371 | devc->flag_reg &= ~FLAG_FILTER; | |
372 | devc->max_channels = NUM_CHANNELS / 2; | |
373 | devc->cur_samplerate_divider = (CLOCK_RATE * 2 / samplerate) - 1; | |
374 | } else { | |
375 | sr_info("Disabling demux mode."); | |
376 | devc->flag_reg &= ~FLAG_DEMUX; | |
377 | devc->flag_reg |= FLAG_FILTER; | |
378 | devc->max_channels = NUM_CHANNELS; | |
379 | devc->cur_samplerate_divider = (CLOCK_RATE / samplerate) - 1; | |
380 | } | |
381 | ||
382 | /* Calculate actual samplerate used and complain if it is different | |
383 | * from the requested. | |
384 | */ | |
385 | devc->cur_samplerate = CLOCK_RATE / (devc->cur_samplerate_divider + 1); | |
386 | if (devc->flag_reg & FLAG_DEMUX) | |
387 | devc->cur_samplerate *= 2; | |
388 | if (devc->cur_samplerate != samplerate) | |
389 | sr_info("Can't match samplerate %" PRIu64 ", using %" | |
390 | PRIu64 ".", samplerate, devc->cur_samplerate); | |
391 | ||
392 | return SR_OK; | |
393 | } | |
394 | ||
395 | ||
396 | SR_PRIV int p_ols_receive_data(int fd, int revents, void *cb_data) | |
397 | { | |
398 | struct dev_context *devc; | |
399 | struct sr_dev_inst *sdi; | |
400 | struct sr_datafeed_packet packet; | |
401 | struct sr_datafeed_logic logic; | |
402 | uint32_t sample; | |
403 | int num_channels, offset, j; | |
404 | int bytes_read, index; | |
405 | unsigned int i; | |
406 | unsigned char byte; | |
407 | ||
408 | (void)fd; | |
409 | (void)revents; | |
410 | ||
411 | sdi = cb_data; | |
412 | devc = sdi->priv; | |
413 | ||
414 | if (devc->num_transfers++ == 0) { | |
415 | devc->raw_sample_buf = g_try_malloc(devc->limit_samples * 4); | |
416 | if (!devc->raw_sample_buf) { | |
417 | sr_err("Sample buffer malloc failed."); | |
418 | return FALSE; | |
419 | } | |
420 | /* fill with 1010... for debugging */ | |
421 | memset(devc->raw_sample_buf, 0x82, devc->limit_samples * 4); | |
422 | } | |
423 | ||
424 | if ((devc->num_samples < devc->limit_samples) && (devc->cnt_samples < devc->max_samples)) { | |
425 | ||
426 | num_channels = 0; | |
427 | for (i = NUM_CHANNELS; i > 0x02; i /= 2) { | |
428 | if ((devc->flag_reg & i) == 0) { | |
429 | num_channels++; | |
430 | } | |
431 | } | |
432 | ||
433 | /* Get a block of data. */ | |
434 | bytes_read = ftdi_read_data(devc->ftdic, devc->ftdi_buf, FTDI_BUF_SIZE); | |
435 | if (bytes_read < 0) { | |
436 | sr_err("Failed to read FTDI data (%d): %s.", | |
437 | bytes_read, ftdi_get_error_string(devc->ftdic)); | |
438 | sdi->driver->dev_acquisition_stop(sdi, sdi); | |
439 | return FALSE; | |
440 | } | |
441 | if (bytes_read == 0) { | |
442 | sr_spew("Received 0 bytes, nothing to do."); | |
443 | return TRUE; | |
444 | } | |
445 | ||
446 | sr_dbg("Received %d bytes", bytes_read); | |
447 | ||
448 | index = 0; | |
449 | while (index < bytes_read) { | |
450 | byte = devc->ftdi_buf[index++]; | |
451 | devc->cnt_bytes++; | |
452 | ||
453 | devc->sample[devc->num_bytes++] = byte; | |
454 | sr_spew("Received byte 0x%.2x.", byte); | |
455 | ||
456 | if ((devc->flag_reg & FLAG_DEMUX) && (devc->flag_reg & FLAG_RLE)) { | |
457 | /* RLE in demux mode must be processed differently | |
458 | * since in this case the RLE encoder is operating on pairs of samples. | |
459 | */ | |
460 | if (devc->num_bytes == num_channels * 2) { | |
461 | devc->cnt_samples += 2; | |
462 | devc->cnt_samples_rle += 2; | |
463 | /* | |
464 | * Got a sample pair. Convert from the OLS's little-endian | |
465 | * sample to the local format. | |
466 | */ | |
467 | sample = devc->sample[0] | (devc->sample[1] << 8) \ | |
468 | | (devc->sample[2] << 16) | (devc->sample[3] << 24); | |
469 | sr_spew("Received sample pair 0x%.*x.", devc->num_bytes * 2, sample); | |
470 | ||
471 | /* | |
472 | * In RLE mode the high bit of the sample pair is the | |
473 | * "count" flag, meaning this sample pair is the number | |
474 | * of times the previous sample pair occurred. | |
475 | */ | |
476 | if (devc->sample[devc->num_bytes - 1] & 0x80) { | |
477 | /* Clear the high bit. */ | |
478 | sample &= ~(0x80 << (devc->num_bytes - 1) * 8); | |
479 | devc->rle_count = sample; | |
480 | devc->cnt_samples_rle += devc->rle_count * 2; | |
481 | sr_dbg("RLE count: %u.", devc->rle_count * 2); | |
482 | devc->num_bytes = 0; | |
483 | continue; | |
484 | } | |
485 | devc->num_samples += (devc->rle_count + 1) * 2; | |
486 | if (devc->num_samples > devc->limit_samples) { | |
487 | /* Save us from overrunning the buffer. */ | |
488 | devc->rle_count -= (devc->num_samples - devc->limit_samples) / 2; | |
489 | devc->num_samples = devc->limit_samples; | |
490 | index = bytes_read; | |
491 | } | |
492 | ||
493 | /* | |
494 | * Some channel groups may have been turned | |
495 | * off, to speed up transfer between the | |
496 | * hardware and the PC. Expand that here before | |
497 | * submitting it over the session bus -- | |
498 | * whatever is listening on the bus will be | |
499 | * expecting a full 32-bit sample, based on | |
500 | * the number of channels. | |
501 | */ | |
502 | j = 0; | |
503 | /* expand first sample */ | |
504 | memset(devc->tmp_sample, 0, 4); | |
505 | for (i = 0; i < 2; i++) { | |
506 | if (((devc->flag_reg >> 2) & (1 << i)) == 0) { | |
507 | /* | |
508 | * This channel group was | |
509 | * enabled, copy from received | |
510 | * sample. | |
511 | */ | |
512 | devc->tmp_sample[i] = devc->sample[j++]; | |
513 | } | |
514 | } | |
515 | /* Clear out the most significant bit of the sample */ | |
516 | devc->tmp_sample[devc->num_bytes - 1] &= 0x7f; | |
517 | sr_spew("Expanded sample 1: 0x%.8x.", devc->tmp_sample); | |
518 | ||
519 | /* expand second sample */ | |
520 | memset(devc->tmp_sample2, 0, 4); | |
521 | for (i = 0; i < 2; i++) { | |
522 | if (((devc->flag_reg >> 2) & (1 << i)) == 0) { | |
523 | /* | |
524 | * This channel group was | |
525 | * enabled, copy from received | |
526 | * sample. | |
527 | */ | |
528 | devc->tmp_sample2[i] = devc->sample[j++]; | |
529 | } | |
530 | } | |
531 | /* Clear out the most significant bit of the sample */ | |
532 | devc->tmp_sample2[devc->num_bytes - 1] &= 0x7f; | |
533 | sr_spew("Expanded sample 2: 0x%.8x.", devc->tmp_sample2); | |
534 | ||
535 | /* | |
536 | * OLS sends its sample buffer backwards. | |
537 | * store it in reverse order here, so we can dump | |
538 | * this on the session bus later. | |
539 | */ | |
540 | offset = (devc->limit_samples - devc->num_samples) * 4; | |
541 | for (i = 0; i <= devc->rle_count; i++) { | |
542 | memcpy(devc->raw_sample_buf + offset + (i * 8), | |
543 | devc->tmp_sample2, 4); | |
544 | memcpy(devc->raw_sample_buf + offset + (4 + (i * 8)), | |
545 | devc->tmp_sample, 4); | |
546 | } | |
547 | memset(devc->sample, 0, 4); | |
548 | devc->num_bytes = 0; | |
549 | devc->rle_count = 0; | |
550 | } | |
551 | } | |
552 | else { | |
553 | if (devc->num_bytes == num_channels) { | |
554 | devc->cnt_samples++; | |
555 | devc->cnt_samples_rle++; | |
556 | /* | |
557 | * Got a full sample. Convert from the OLS's little-endian | |
558 | * sample to the local format. | |
559 | */ | |
560 | sample = devc->sample[0] | (devc->sample[1] << 8) \ | |
561 | | (devc->sample[2] << 16) | (devc->sample[3] << 24); | |
562 | sr_spew("Received sample 0x%.*x.", devc->num_bytes * 2, sample); | |
563 | if (devc->flag_reg & FLAG_RLE) { | |
564 | /* | |
565 | * In RLE mode the high bit of the sample is the | |
566 | * "count" flag, meaning this sample is the number | |
567 | * of times the previous sample occurred. | |
568 | */ | |
569 | if (devc->sample[devc->num_bytes - 1] & 0x80) { | |
570 | /* Clear the high bit. */ | |
571 | sample &= ~(0x80 << (devc->num_bytes - 1) * 8); | |
572 | devc->rle_count = sample; | |
573 | devc->cnt_samples_rle += devc->rle_count; | |
574 | sr_dbg("RLE count: %u.", devc->rle_count); | |
575 | devc->num_bytes = 0; | |
576 | continue; | |
577 | } | |
578 | } | |
579 | devc->num_samples += devc->rle_count + 1; | |
580 | if (devc->num_samples > devc->limit_samples) { | |
581 | /* Save us from overrunning the buffer. */ | |
582 | devc->rle_count -= devc->num_samples - devc->limit_samples; | |
583 | devc->num_samples = devc->limit_samples; | |
584 | index = bytes_read; | |
585 | } | |
586 | ||
587 | if (num_channels < 4) { | |
588 | /* | |
589 | * Some channel groups may have been turned | |
590 | * off, to speed up transfer between the | |
591 | * hardware and the PC. Expand that here before | |
592 | * submitting it over the session bus -- | |
593 | * whatever is listening on the bus will be | |
594 | * expecting a full 32-bit sample, based on | |
595 | * the number of channels. | |
596 | */ | |
597 | j = 0; | |
598 | memset(devc->tmp_sample, 0, 4); | |
599 | for (i = 0; i < 4; i++) { | |
600 | if (((devc->flag_reg >> 2) & (1 << i)) == 0) { | |
601 | /* | |
602 | * This channel group was | |
603 | * enabled, copy from received | |
604 | * sample. | |
605 | */ | |
606 | devc->tmp_sample[i] = devc->sample[j++]; | |
607 | } | |
608 | } | |
609 | memcpy(devc->sample, devc->tmp_sample, 4); | |
610 | sr_spew("Expanded sample: 0x%.8x.", sample); | |
611 | } | |
612 | ||
613 | /* | |
614 | * Pipistrello OLS sends its sample buffer backwards. | |
615 | * store it in reverse order here, so we can dump | |
616 | * this on the session bus later. | |
617 | */ | |
618 | offset = (devc->limit_samples - devc->num_samples) * 4; | |
619 | for (i = 0; i <= devc->rle_count; i++) { | |
620 | memcpy(devc->raw_sample_buf + offset + (i * 4), | |
621 | devc->sample, 4); | |
622 | } | |
623 | memset(devc->sample, 0, 4); | |
624 | devc->num_bytes = 0; | |
625 | devc->rle_count = 0; | |
626 | } | |
627 | } | |
628 | } | |
629 | return TRUE; | |
630 | } else { | |
631 | do bytes_read = ftdi_read_data(devc->ftdic, devc->ftdi_buf, FTDI_BUF_SIZE); | |
632 | while (bytes_read > 0); | |
633 | ||
634 | /* | |
635 | * We've acquired all the samples we asked for -- we're done. | |
636 | * Send the (properly-ordered) buffer to the frontend. | |
637 | */ | |
638 | sr_dbg("Received %d bytes, %d samples, %d decompressed samples.", | |
639 | devc->cnt_bytes, devc->cnt_samples, | |
640 | devc->cnt_samples_rle); | |
641 | if (devc->trigger_at != -1) { | |
642 | /* | |
643 | * A trigger was set up, so we need to tell the frontend | |
644 | * about it. | |
645 | */ | |
646 | if (devc->trigger_at > 0) { | |
647 | /* There are pre-trigger samples, send those first. */ | |
648 | packet.type = SR_DF_LOGIC; | |
649 | packet.payload = &logic; | |
650 | logic.length = devc->trigger_at * 4; | |
651 | logic.unitsize = 4; | |
652 | logic.data = devc->raw_sample_buf + | |
653 | (devc->limit_samples - devc->num_samples) * 4; | |
654 | sr_session_send(cb_data, &packet); | |
655 | } | |
656 | ||
657 | /* Send the trigger. */ | |
658 | packet.type = SR_DF_TRIGGER; | |
659 | sr_session_send(cb_data, &packet); | |
660 | ||
661 | /* Send post-trigger samples. */ | |
662 | packet.type = SR_DF_LOGIC; | |
663 | packet.payload = &logic; | |
664 | logic.length = (devc->num_samples * 4) - (devc->trigger_at * 4); | |
665 | logic.unitsize = 4; | |
666 | logic.data = devc->raw_sample_buf + devc->trigger_at * 4 + | |
667 | (devc->limit_samples - devc->num_samples) * 4; | |
668 | sr_session_send(cb_data, &packet); | |
669 | } else { | |
670 | /* no trigger was used */ | |
671 | packet.type = SR_DF_LOGIC; | |
672 | packet.payload = &logic; | |
673 | logic.length = devc->num_samples * 4; | |
674 | logic.unitsize = 4; | |
675 | logic.data = devc->raw_sample_buf + | |
676 | (devc->limit_samples - devc->num_samples) * 4; | |
677 | sr_session_send(cb_data, &packet); | |
678 | } | |
679 | g_free(devc->raw_sample_buf); | |
680 | ||
681 | sdi->driver->dev_acquisition_stop(sdi, cb_data); | |
682 | } | |
683 | ||
684 | return TRUE; | |
685 | } |