]> sigrok.org Git - libsigrok.git/blame - hardware/fx2lafw/protocol.c
Add new software trigger as a library-wide facility.
[libsigrok.git] / hardware / fx2lafw / protocol.c
CommitLineData
2f937611
UH
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
5 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include "protocol.h"
22
23/* Protocol commands */
24#define CMD_GET_FW_VERSION 0xb0
25#define CMD_START 0xb1
26#define CMD_GET_REVID_VERSION 0xb2
27
28#define CMD_START_FLAGS_WIDE_POS 5
29#define CMD_START_FLAGS_CLK_SRC_POS 6
30
31#define CMD_START_FLAGS_SAMPLE_8BIT (0 << CMD_START_FLAGS_WIDE_POS)
32#define CMD_START_FLAGS_SAMPLE_16BIT (1 << CMD_START_FLAGS_WIDE_POS)
33
34#define CMD_START_FLAGS_CLK_30MHZ (0 << CMD_START_FLAGS_CLK_SRC_POS)
35#define CMD_START_FLAGS_CLK_48MHZ (1 << CMD_START_FLAGS_CLK_SRC_POS)
36
37#pragma pack(push, 1)
38
39struct version_info {
40 uint8_t major;
41 uint8_t minor;
42};
43
44struct cmd_start_acquisition {
45 uint8_t flags;
46 uint8_t sample_delay_h;
47 uint8_t sample_delay_l;
48};
49
50#pragma pack(pop)
51
52static int command_get_fw_version(libusb_device_handle *devhdl,
53 struct version_info *vi)
54{
55 int ret;
56
57 ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
58 LIBUSB_ENDPOINT_IN, CMD_GET_FW_VERSION, 0x0000, 0x0000,
59 (unsigned char *)vi, sizeof(struct version_info), 100);
60
61 if (ret < 0) {
62 sr_err("Unable to get version info: %s.",
63 libusb_error_name(ret));
64 return SR_ERR;
65 }
66
67 return SR_OK;
68}
69
a54edb1d 70static int command_get_revid_version(struct sr_dev_inst *sdi, uint8_t *revid)
2f937611 71{
a54edb1d
ML
72 struct sr_usb_dev_inst *usb = sdi->conn;
73 libusb_device_handle *devhdl = usb->devhdl;
2f937611
UH
74 int ret;
75
76 ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
77 LIBUSB_ENDPOINT_IN, CMD_GET_REVID_VERSION, 0x0000, 0x0000,
78 revid, 1, 100);
79
80 if (ret < 0) {
81 sr_err("Unable to get REVID: %s.", libusb_error_name(ret));
82 return SR_ERR;
83 }
84
85 return SR_OK;
86}
87
a54edb1d 88SR_PRIV int fx2lafw_command_start_acquisition(const struct sr_dev_inst *sdi)
2f937611 89{
a54edb1d
ML
90 struct dev_context *devc = sdi->priv;
91 struct sr_usb_dev_inst *usb = sdi->conn;
92 libusb_device_handle *devhdl = usb->devhdl;
93 uint64_t samplerate = devc->cur_samplerate;
94 gboolean samplewide = devc->sample_wide;
eb28f1b7 95 struct cmd_start_acquisition cmd = { 0 };
2f937611
UH
96 int delay = 0, ret;
97
98 /* Compute the sample rate. */
99 if (samplewide && samplerate > MAX_16BIT_SAMPLE_RATE) {
100 sr_err("Unable to sample at %" PRIu64 "Hz "
101 "when collecting 16-bit samples.", samplerate);
102 return SR_ERR;
103 }
104
105 if ((SR_MHZ(48) % samplerate) == 0) {
106 cmd.flags = CMD_START_FLAGS_CLK_48MHZ;
107 delay = SR_MHZ(48) / samplerate - 1;
108 if (delay > MAX_SAMPLE_DELAY)
109 delay = 0;
110 }
111
112 if (delay == 0 && (SR_MHZ(30) % samplerate) == 0) {
113 cmd.flags = CMD_START_FLAGS_CLK_30MHZ;
114 delay = SR_MHZ(30) / samplerate - 1;
115 }
116
117 sr_info("GPIF delay = %d, clocksource = %sMHz.", delay,
118 (cmd.flags & CMD_START_FLAGS_CLK_48MHZ) ? "48" : "30");
119
120 if (delay <= 0 || delay > MAX_SAMPLE_DELAY) {
121 sr_err("Unable to sample at %" PRIu64 "Hz.", samplerate);
122 return SR_ERR;
123 }
124
125 cmd.sample_delay_h = (delay >> 8) & 0xff;
126 cmd.sample_delay_l = delay & 0xff;
127
128 /* Select the sampling width. */
129 cmd.flags |= samplewide ? CMD_START_FLAGS_SAMPLE_16BIT :
130 CMD_START_FLAGS_SAMPLE_8BIT;
131
132 /* Send the control message. */
133 ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
134 LIBUSB_ENDPOINT_OUT, CMD_START, 0x0000, 0x0000,
135 (unsigned char *)&cmd, sizeof(cmd), 100);
136 if (ret < 0) {
137 sr_err("Unable to send start command: %s.",
138 libusb_error_name(ret));
139 return SR_ERR;
140 }
141
142 return SR_OK;
143}
144
145/**
146 * Check the USB configuration to determine if this is an fx2lafw device.
147 *
148 * @return TRUE if the device's configuration profile match fx2lafw
149 * configuration, FALSE otherwise.
150 */
151SR_PRIV gboolean fx2lafw_check_conf_profile(libusb_device *dev)
152{
153 struct libusb_device_descriptor des;
154 struct libusb_device_handle *hdl;
155 gboolean ret;
156 unsigned char strdesc[64];
157
158 hdl = NULL;
159 ret = FALSE;
160 while (!ret) {
161 /* Assume the FW has not been loaded, unless proven wrong. */
162 if (libusb_get_device_descriptor(dev, &des) != 0)
163 break;
164
165 if (libusb_open(dev, &hdl) != 0)
166 break;
167
168 if (libusb_get_string_descriptor_ascii(hdl,
169 des.iManufacturer, strdesc, sizeof(strdesc)) < 0)
170 break;
171 if (strncmp((const char *)strdesc, "sigrok", 6))
172 break;
173
174 if (libusb_get_string_descriptor_ascii(hdl,
175 des.iProduct, strdesc, sizeof(strdesc)) < 0)
176 break;
177 if (strncmp((const char *)strdesc, "fx2lafw", 7))
178 break;
179
180 /* If we made it here, it must be an fx2lafw. */
181 ret = TRUE;
182 }
183 if (hdl)
184 libusb_close(hdl);
185
186 return ret;
187}
188
189SR_PRIV int fx2lafw_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di)
190{
191 libusb_device **devlist;
192 struct sr_usb_dev_inst *usb;
193 struct libusb_device_descriptor des;
194 struct dev_context *devc;
195 struct drv_context *drvc;
196 struct version_info vi;
197 int ret, skip, i, device_count;
198 uint8_t revid;
199
200 drvc = di->priv;
201 devc = sdi->priv;
202 usb = sdi->conn;
203
204 if (sdi->status == SR_ST_ACTIVE)
205 /* Device is already in use. */
206 return SR_ERR;
207
208 skip = 0;
209 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
210 if (device_count < 0) {
211 sr_err("Failed to get device list: %s.",
212 libusb_error_name(device_count));
213 return SR_ERR;
214 }
215
216 for (i = 0; i < device_count; i++) {
217 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
218 sr_err("Failed to get device descriptor: %s.",
219 libusb_error_name(ret));
220 continue;
221 }
222
223 if (des.idVendor != devc->profile->vid
224 || des.idProduct != devc->profile->pid)
225 continue;
226
227 if (sdi->status == SR_ST_INITIALIZING) {
228 if (skip != sdi->index) {
229 /* Skip devices of this type that aren't the one we want. */
230 skip += 1;
231 continue;
232 }
233 } else if (sdi->status == SR_ST_INACTIVE) {
234 /*
235 * This device is fully enumerated, so we need to find
236 * this device by vendor, product, bus and address.
237 */
238 if (libusb_get_bus_number(devlist[i]) != usb->bus
239 || libusb_get_device_address(devlist[i]) != usb->address)
240 /* This is not the one. */
241 continue;
242 }
243
244 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
245 if (usb->address == 0xff)
246 /*
247 * First time we touch this device after FW
248 * upload, so we don't know the address yet.
249 */
250 usb->address = libusb_get_device_address(devlist[i]);
251 } else {
252 sr_err("Failed to open device: %s.",
253 libusb_error_name(ret));
254 break;
255 }
256
257 ret = command_get_fw_version(usb->devhdl, &vi);
258 if (ret != SR_OK) {
259 sr_err("Failed to get firmware version.");
260 break;
261 }
262
a54edb1d 263 ret = command_get_revid_version(sdi, &revid);
2f937611
UH
264 if (ret != SR_OK) {
265 sr_err("Failed to get REVID.");
266 break;
267 }
268
269 /*
270 * Changes in major version mean incompatible/API changes, so
271 * bail out if we encounter an incompatible version.
272 * Different minor versions are OK, they should be compatible.
273 */
274 if (vi.major != FX2LAFW_REQUIRED_VERSION_MAJOR) {
275 sr_err("Expected firmware version %d.x, "
276 "got %d.%d.", FX2LAFW_REQUIRED_VERSION_MAJOR,
277 vi.major, vi.minor);
278 break;
279 }
280
281 sdi->status = SR_ST_ACTIVE;
282 sr_info("Opened device %d on %d.%d, "
283 "interface %d, firmware %d.%d.",
284 sdi->index, usb->bus, usb->address,
285 USB_INTERFACE, vi.major, vi.minor);
286
287 sr_info("Detected REVID=%d, it's a Cypress CY7C68013%s.",
288 revid, (revid != 1) ? " (FX2)" : "A (FX2LP)");
289
290 break;
291 }
292 libusb_free_device_list(devlist, 1);
293
294 if (sdi->status != SR_ST_ACTIVE)
295 return SR_ERR;
296
297 return SR_OK;
298}
299
2f937611
UH
300SR_PRIV struct dev_context *fx2lafw_dev_new(void)
301{
302 struct dev_context *devc;
303
304 if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
305 sr_err("Device context malloc failed.");
306 return NULL;
307 }
308
309 devc->profile = NULL;
310 devc->fw_updated = 0;
311 devc->cur_samplerate = 0;
312 devc->limit_samples = 0;
87b545fb 313 devc->sample_wide = FALSE;
2f937611
UH
314
315 return devc;
316}
317
318SR_PRIV void fx2lafw_abort_acquisition(struct dev_context *devc)
319{
320 int i;
321
b0ccd64d 322 devc->acq_aborted = TRUE;
2f937611
UH
323
324 for (i = devc->num_transfers - 1; i >= 0; i--) {
325 if (devc->transfers[i])
326 libusb_cancel_transfer(devc->transfers[i]);
327 }
328}
329
330static void finish_acquisition(struct dev_context *devc)
331{
332 struct sr_datafeed_packet packet;
2f937611
UH
333
334 /* Terminate session. */
335 packet.type = SR_DF_END;
336 sr_session_send(devc->cb_data, &packet);
337
338 /* Remove fds from polling. */
6c60facc 339 usb_source_remove(devc->ctx);
2f937611
UH
340
341 devc->num_transfers = 0;
342 g_free(devc->transfers);
343}
344
345static void free_transfer(struct libusb_transfer *transfer)
346{
9615eeb5 347 struct sr_dev_inst *sdi;
2f937611
UH
348 struct dev_context *devc;
349 unsigned int i;
350
9615eeb5
BV
351 sdi = transfer->user_data;
352 devc = sdi->priv;
2f937611
UH
353
354 g_free(transfer->buffer);
355 transfer->buffer = NULL;
356 libusb_free_transfer(transfer);
357
358 for (i = 0; i < devc->num_transfers; i++) {
359 if (devc->transfers[i] == transfer) {
360 devc->transfers[i] = NULL;
361 break;
362 }
363 }
364
365 devc->submitted_transfers--;
366 if (devc->submitted_transfers == 0)
367 finish_acquisition(devc);
368}
369
370static void resubmit_transfer(struct libusb_transfer *transfer)
371{
372 int ret;
373
374 if ((ret = libusb_submit_transfer(transfer)) == LIBUSB_SUCCESS)
375 return;
376
9615eeb5 377 sr_err("%s: %s", __func__, libusb_error_name(ret));
2f937611 378 free_transfer(transfer);
2f937611 379
9615eeb5
BV
380}
381
382static gboolean check_match(uint16_t sample, struct sr_trigger_match *match)
383{
384 gboolean bit, result;
385
386 result = FALSE;
387 bit = sample & (1 << match->channel->index);
388 if (match->match == SR_TRIGGER_ZERO && !bit)
389 result = TRUE;
390 else if (match->match == SR_TRIGGER_ONE && bit)
391 result = TRUE;
392
393 return result;
394}
395
396/* Returns the offset (in samples) within buf of where the trigger
397 * occurred, or -1 if not triggered. */
398static int soft_trigger(struct sr_dev_inst *sdi, uint8_t *buf,
399 int len)
400{
401 struct dev_context *devc;
402 struct sr_datafeed_packet packet;
403 struct sr_trigger *trigger;
404 struct sr_trigger_stage *stage;
405 struct sr_trigger_match *match;
406 GSList *l, *lst;;
407 int offset;
408 uint16_t sample;
409 int unitsize, i;
410 gboolean match_found;
411
412 devc = sdi->priv;
413 offset = -1;
414 trigger = sr_session_trigger_get();
415 unitsize = devc->sample_wide ? 2 : 1;
416 for (i = 0; i < len; i += unitsize) {
417 memcpy(&sample, buf + i, unitsize);
418
419 lst = g_slist_nth(trigger->stages, devc->cur_trigger_stage);
420 stage = lst->data;
421 if (!stage->matches)
422 /* No matches supplied, client error. */
423 return SR_ERR_ARG;
424
425 match_found = TRUE;
426 for (l = stage->matches; l; l = l->next) {
427 match = l->data;
428 if (!match->channel->enabled)
429 /* Ignore disabled channels with a trigger. */
430 continue;
431 if (!check_match(sample, match)) {
432 match_found = FALSE;
433 break;
434 }
435 }
436 if (match_found) {
437 /* Matched on the current stage. */
438 if (lst->next) {
439 /* Advance to next stage. */
440 devc->cur_trigger_stage++;
441 } else {
442 /* Matched on last stage, fire trigger. */
443 offset = i / unitsize;
444
445 packet.type = SR_DF_TRIGGER;
446 packet.payload = NULL;
447 sr_session_send(devc->cb_data, &packet);
448 break;
449 }
450 } else if (devc->cur_trigger_stage > 0) {
451 /*
452 * We had a match at an earlier stage, but failed on the
453 * current stage. However, we may have a match on this
454 * stage in the next bit -- trigger on 0001 will fail on
455 * seeing 00001, so we need to go back to stage 0 -- but
456 * at the next sample from the one that matched originally,
457 * which the counter increment at the end of the loop
458 * takes care of.
459 */
460 i -= devc->cur_trigger_stage * unitsize;
461 if (i < -1)
462 i = -1; /* Oops, went back past this buffer. */
463 /* Reset trigger stage. */
464 devc->cur_trigger_stage = 0;
465 }
466 }
467
468 return offset;
2f937611
UH
469}
470
471SR_PRIV void fx2lafw_receive_transfer(struct libusb_transfer *transfer)
472{
9615eeb5
BV
473 struct sr_dev_inst *sdi;
474 struct dev_context *devc;
2f937611
UH
475 gboolean packet_has_error = FALSE;
476 struct sr_datafeed_packet packet;
477 struct sr_datafeed_logic logic;
9615eeb5
BV
478 unsigned int num_samples;
479 int trigger_offset, cur_sample_count, unitsize;
2f937611 480
9615eeb5
BV
481 sdi = transfer->user_data;
482 devc = sdi->priv;
2f937611
UH
483
484 /*
485 * If acquisition has already ended, just free any queued up
486 * transfer that come in.
487 */
b0ccd64d 488 if (devc->acq_aborted) {
2f937611
UH
489 free_transfer(transfer);
490 return;
491 }
492
493 sr_info("receive_transfer(): status %d received %d bytes.",
494 transfer->status, transfer->actual_length);
495
496 /* Save incoming transfer before reusing the transfer struct. */
9615eeb5
BV
497 unitsize = devc->sample_wide ? 2 : 1;
498 cur_sample_count = transfer->actual_length / unitsize;
2f937611
UH
499
500 switch (transfer->status) {
501 case LIBUSB_TRANSFER_NO_DEVICE:
502 fx2lafw_abort_acquisition(devc);
503 free_transfer(transfer);
504 return;
505 case LIBUSB_TRANSFER_COMPLETED:
506 case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though. */
507 break;
508 default:
509 packet_has_error = TRUE;
510 break;
511 }
512
513 if (transfer->actual_length == 0 || packet_has_error) {
514 devc->empty_transfer_count++;
515 if (devc->empty_transfer_count > MAX_EMPTY_TRANSFERS) {
516 /*
517 * The FX2 gave up. End the acquisition, the frontend
518 * will work out that the samplecount is short.
519 */
520 fx2lafw_abort_acquisition(devc);
521 free_transfer(transfer);
522 } else {
523 resubmit_transfer(transfer);
524 }
525 return;
526 } else {
527 devc->empty_transfer_count = 0;
528 }
529
9615eeb5
BV
530 if (devc->trigger_fired) {
531 if (devc->sent_samples < devc->limit_samples) {
532 /* Send the incoming transfer to the session bus. */
533 packet.type = SR_DF_LOGIC;
534 packet.payload = &logic;
535 if (devc->sent_samples + cur_sample_count > devc->limit_samples)
536 num_samples = devc->limit_samples - devc->sent_samples;
537 else
538 num_samples = cur_sample_count;
539 logic.length = num_samples * unitsize;
540 logic.unitsize = unitsize;
541 logic.data = transfer->buffer;
542 sr_session_send(devc->cb_data, &packet);
543 devc->sent_samples += cur_sample_count;
544 }
545 } else {
546 trigger_offset = soft_trigger(sdi, transfer->buffer, transfer->actual_length);
547 if (trigger_offset > -1) {
548 packet.type = SR_DF_LOGIC;
549 packet.payload = &logic;
550 num_samples = cur_sample_count - trigger_offset;
551 if (devc->limit_samples &&
552 num_samples > devc->limit_samples - devc->sent_samples)
553 num_samples = devc->limit_samples - devc->sent_samples;
554 logic.length = num_samples * unitsize;
555 logic.unitsize = unitsize;
556 logic.data = transfer->buffer + trigger_offset * unitsize;
557 sr_session_send(devc->cb_data, &packet);
558 devc->sent_samples += num_samples;
559
560 devc->trigger_fired = TRUE;
2f937611 561 }
06b5d7f7
BV
562 }
563
564 if (devc->limit_samples && devc->sent_samples >= devc->limit_samples) {
565 fx2lafw_abort_acquisition(devc);
566 free_transfer(transfer);
9615eeb5
BV
567 } else
568 resubmit_transfer(transfer);
2f937611
UH
569}
570
571static unsigned int to_bytes_per_ms(unsigned int samplerate)
572{
573 return samplerate / 1000;
574}
575
576SR_PRIV size_t fx2lafw_get_buffer_size(struct dev_context *devc)
577{
578 size_t s;
579
580 /*
581 * The buffer should be large enough to hold 10ms of data and
582 * a multiple of 512.
583 */
584 s = 10 * to_bytes_per_ms(devc->cur_samplerate);
585 return (s + 511) & ~511;
586}
587
588SR_PRIV unsigned int fx2lafw_get_number_of_transfers(struct dev_context *devc)
589{
590 unsigned int n;
591
592 /* Total buffer size should be able to hold about 500ms of data. */
593 n = (500 * to_bytes_per_ms(devc->cur_samplerate) /
594 fx2lafw_get_buffer_size(devc));
595
596 if (n > NUM_SIMUL_TRANSFERS)
597 return NUM_SIMUL_TRANSFERS;
598
599 return n;
600}
601
602SR_PRIV unsigned int fx2lafw_get_timeout(struct dev_context *devc)
603{
604 size_t total_size;
605 unsigned int timeout;
606
607 total_size = fx2lafw_get_buffer_size(devc) *
608 fx2lafw_get_number_of_transfers(devc);
609 timeout = total_size / to_bytes_per_ms(devc->cur_samplerate);
610 return timeout + timeout / 4; /* Leave a headroom of 25% percent. */
611}