-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibsigrok-pr-148-add-cypress-fx3.patch
1595 lines (1591 loc) · 43.3 KB
/
libsigrok-pr-148-add-cypress-fx3.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
diff --git a/Makefile.am b/Makefile.am
index 62aca8ac..4fd3c26c 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -329,6 +329,12 @@ src_libdrivers_la_SOURCES += \
src/hardware/dcttech-usbrelay/protocol.c \
src/hardware/dcttech-usbrelay/api.c
endif
+if HW_CYPRESS_FX3
+src_libdrivers_la_SOURCES += \
+ src/hardware/cypress-fx3/protocol.h \
+ src/hardware/cypress-fx3/protocol.c \
+ src/hardware/cypress-fx3/api.c
+endif
if HW_DEMO
src_libdrivers_la_SOURCES += \
src/hardware/demo/protocol.h \
diff --git a/configure.ac b/configure.ac
index 5c30a816..c1b19933 100644
--- a/configure.ac
+++ b/configure.ac
@@ -319,6 +319,7 @@ SR_DRIVER([ChronoVu LA], [chronovu-la], [libusb libftdi])
SR_DRIVER([Colead SLM], [colead-slm], [serial_comm])
SR_DRIVER([Conrad DIGI 35 CPU], [conrad-digi-35-cpu], [serial_comm])
SR_DRIVER([dcttech usbrelay], [dcttech-usbrelay], [libhidapi])
+SR_DRIVER([cypress-fx3], [cypress-fx3])
SR_DRIVER([demo], [demo])
SR_DRIVER([Devantech ETH008], [devantech-eth008], [serial_comm])
SR_DRIVER([DreamSourceLab DSLogic], [dreamsourcelab-dslogic], [libusb])
diff --git a/src/ezusb.c b/src/ezusb.c
index c2d270c4..570922c7 100644
--- a/src/ezusb.c
+++ b/src/ezusb.c
@@ -35,6 +35,9 @@
#define FW_CHUNKSIZE (4 * 1024)
+#define GET_LSW(v) ((unsigned short)((v) & 0xFFFF)) // Get Least Significant Word part of an integer.
+#define GET_MSW(v) ((unsigned short)((v) >> 16)) // Get Most Significant Word part of an integer.
+
SR_PRIV int ezusb_reset(struct libusb_device_handle *hdl, int set_clear)
{
int ret;
@@ -142,3 +145,129 @@ SR_PRIV int ezusb_upload_firmware(struct sr_context *ctx, libusb_device *dev,
return SR_OK;
}
+
+SR_PRIV int ezusb_fx3_ram_write (libusb_device_handle *hdl,
+ unsigned char *buf,
+ unsigned int ramAddress,
+ int len)
+{
+ int r;
+ int index = 0;
+ int size;
+
+ while (len > 0) {
+ size = MIN(len ,FW_CHUNKSIZE);
+
+ r = libusb_control_transfer (hdl, 0x40, 0xA0, GET_LSW(ramAddress), GET_MSW(ramAddress),
+ &buf[index], size, 100);
+
+ if (r != size) {
+ sr_err ("Error: Vendor write to FX3 RAM failed\n");
+ return SR_ERR;
+ }
+ ramAddress += size;
+ index += size;
+ len -= size;
+ }
+
+ return SR_OK;
+}
+
+SR_PRIV int ezusb_install_firmware_fx3(struct sr_context *ctx,
+ libusb_device_handle *hdl,
+ const char *name)
+{
+ unsigned char *firmware;
+ size_t length, offset, chunksize,filesize;
+ int ret, result;
+ unsigned int *data_p;
+ unsigned int i, checksum;
+ unsigned int address;
+ int r, index;
+
+ /* Max size of FX3 fw file is 256 KB = 1 << 18*/
+ firmware = sr_resource_load(ctx, SR_RESOURCE_FIRMWARE,
+ name, &filesize, 1 << 18);
+ if (!firmware)
+ return SR_ERR;
+
+ sr_info("Uploading FX3 firmware '%s'.", name);
+
+ result = SR_OK;
+ /* Run through each section of code, and use vendor commands to download them to RAM.*/
+ index = 4;
+ checksum = 0;
+ while (index < filesize) {
+ data_p = (unsigned int *)(firmware + index);
+ length = data_p[0];
+ address = data_p[1];
+ if (length != 0) {
+ for (i = 0; i < length; i++)
+ checksum += data_p[2 + i];
+ r = ezusb_fx3_ram_write (hdl, firmware + index + 8, address, length * 4);
+ if (r != 0) {
+ sr_err ("Error: Failed to download data to FX3 RAM\n");
+ free (firmware);
+ return SR_ERR;
+ }
+ } else {
+ if (checksum != data_p[2]) {
+ sr_err ("Error: Checksum error in firmware binary\n");
+ free (firmware);
+ return SR_ERR;
+ }
+ r = libusb_control_transfer (hdl, 0x40, 0xA0, GET_LSW(address), GET_MSW(address), NULL,
+ 0, 100);
+ if (r != 0)
+ printf ("Info: Ignored error in control transfer: %d\n", r);
+ break;
+ }
+ index += (8 + length * 4);
+ }
+ g_free(firmware);
+
+ sr_info("Firmware upload done.");
+ return result;
+}
+
+SR_PRIV int ezusb_upload_firmware_fx3(struct sr_context *ctx, libusb_device *dev,
+ int configuration, const char *name)
+{
+ struct libusb_device_handle *hdl;
+ int ret;
+
+ sr_info("uploading firmware to FX3 device on %d.%d",
+ libusb_get_bus_number(dev), libusb_get_device_address(dev));
+
+ if ((ret = libusb_open(dev, &hdl)) < 0) {
+ sr_err("failed to open FX3 device: %s.", libusb_error_name(ret));
+ return SR_ERR;
+ }
+
+/*
+ * The libusb Darwin backend is broken: it can report a kernel driver being
+ * active, but detaching it always returns an error.
+ */
+#if !defined(__APPLE__)
+ if (libusb_kernel_driver_active(hdl, 0) == 1) {
+ if ((ret = libusb_detach_kernel_driver(hdl, 0)) < 0) {
+ sr_err("failed to detach kernel driver: %s",
+ libusb_error_name(ret));
+ return SR_ERR;
+ }
+ }
+#endif
+
+ if ((ret = libusb_set_configuration(hdl, configuration)) < 0) {
+ sr_err("Unable to set configuration: %s",
+ libusb_error_name(ret));
+ return SR_ERR;
+ }
+
+ if (ezusb_install_firmware_fx3(ctx, hdl, name) < 0)
+ return SR_ERR;
+
+ libusb_close(hdl);
+
+ return SR_OK;
+}
diff --git a/src/hardware/cypress-fx3/api.c b/src/hardware/cypress-fx3/api.c
new file mode 100644
index 00000000..81b8eee8
--- /dev/null
+++ b/src/hardware/cypress-fx3/api.c
@@ -0,0 +1,519 @@
+/*
+ * This file is part of the libsigrok project.
+ *
+ * Copyright (C) 2021 Ashwin Nair <[email protected]>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#include <config.h>
+#include "protocol.h"
+#include <math.h>
+
+static const struct cypress_fx3_profile supported_fx3[] = {
+ /*
+ * Cypress FX3
+ */
+ { 0x04b4, 0x1234, "Cypress", "FX3", NULL,
+ "cypress-fx3.fw",
+ DEV_CAPS_16BIT, NULL, NULL},
+
+ ALL_ZERO
+};
+
+static const uint32_t scanopts[] = {
+ SR_CONF_CONN,
+};
+
+static const uint32_t drvopts[] = {
+ SR_CONF_LOGIC_ANALYZER,
+};
+
+static const uint32_t devopts[] = {
+ SR_CONF_CONTINUOUS,
+ SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET,
+ SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
+ SR_CONF_CONN | SR_CONF_GET,
+ SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
+ SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
+ SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
+};
+
+static const int32_t trigger_matches[] = {
+ SR_TRIGGER_ZERO,
+ SR_TRIGGER_ONE,
+ SR_TRIGGER_RISING,
+ SR_TRIGGER_FALLING,
+ SR_TRIGGER_EDGE,
+};
+
+static const uint64_t samplerates[] = {
+ SR_KHZ(500),
+ SR_MHZ(1),
+ SR_MHZ(5),
+ SR_MHZ(10),
+ SR_MHZ(25),
+ SR_MHZ(50),
+ SR_MHZ(100),
+};
+
+static gboolean is_plausible(const struct libusb_device_descriptor *des)
+{
+ int i;
+
+ for (i = 0; supported_fx3[i].vid; i++) {
+ if (des->idVendor != supported_fx3[i].vid)
+ continue;
+ if (des->idProduct == supported_fx3[i].pid)
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+static GSList *scan(struct sr_dev_driver *di, GSList *options)
+{
+ struct drv_context *drvc;
+ struct dev_context *devc;
+ struct sr_dev_inst *sdi;
+ struct sr_usb_dev_inst *usb;
+ struct sr_channel *ch;
+ struct sr_channel_group *cg;
+ struct sr_config *src;
+ const struct cypress_fx3_profile *prof;
+ GSList *l, *devices, *conn_devices;
+ gboolean has_firmware;
+ struct libusb_device_descriptor des;
+ libusb_device **devlist;
+ struct libusb_device_handle *hdl;
+ int ret, i, j;
+ int num_logic_channels = 0, num_analog_channels = 0;
+ const char *conn;
+ char manufacturer[64], product[64], serial_num[64], connection_id[64];
+ char channel_name[16];
+
+ drvc = di->context;
+
+ conn = NULL;
+ for (l = options; l; l = l->next) {
+ src = l->data;
+ switch (src->key) {
+ case SR_CONF_CONN:
+ conn = g_variant_get_string(src->data, NULL);
+ break;
+ }
+ }
+ if (conn)
+ conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
+ else
+ conn_devices = NULL;
+
+ /* Find all cypress_fx3 compatible devices and upload firmware to them. */
+ devices = NULL;
+ libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
+ for (i = 0; devlist[i]; i++) {
+ if (conn) {
+ usb = NULL;
+ for (l = conn_devices; l; l = l->next) {
+ usb = l->data;
+ if (usb->bus == libusb_get_bus_number(devlist[i])
+ && usb->address == libusb_get_device_address(devlist[i]))
+ break;
+ }
+ if (!l)
+ /* This device matched none of the ones that
+ * matched the conn specification. */
+ continue;
+ }
+
+ libusb_get_device_descriptor( devlist[i], &des);
+
+ if (!is_plausible(&des))
+ continue;
+
+ if ((ret = libusb_open(devlist[i], &hdl)) < 0) {
+ sr_warn("Failed to open potential device with "
+ "VID:PID %04x:%04x: %s.", des.idVendor,
+ des.idProduct, libusb_error_name(ret));
+ continue;
+ }
+
+ if (des.iManufacturer == 0) {
+ manufacturer[0] = '\0';
+ } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
+ des.iManufacturer, (unsigned char *) manufacturer,
+ sizeof(manufacturer))) < 0) {
+ sr_warn("Failed to get manufacturer string descriptor: %s.",
+ libusb_error_name(ret));
+ continue;
+ }
+
+ if (des.iProduct == 0) {
+ product[0] = '\0';
+ } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
+ des.iProduct, (unsigned char *) product,
+ sizeof(product))) < 0) {
+ sr_warn("Failed to get product string descriptor: %s.",
+ libusb_error_name(ret));
+ continue;
+ }
+
+ if (des.iSerialNumber == 0) {
+ serial_num[0] = '\0';
+ } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
+ des.iSerialNumber, (unsigned char *) serial_num,
+ sizeof(serial_num))) < 0) {
+ sr_warn("Failed to get serial number string descriptor: %s.",
+ libusb_error_name(ret));
+ continue;
+ }
+
+ libusb_close(hdl);
+
+ if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
+ continue;
+
+ prof = NULL;
+ for (j = 0; supported_fx3[j].vid; j++) {
+ if (des.idVendor == supported_fx3[j].vid &&
+ des.idProduct == supported_fx3[j].pid &&
+ (!supported_fx3[j].usb_manufacturer ||
+ !strcmp(manufacturer, supported_fx3[j].usb_manufacturer)) &&
+ (!supported_fx3[j].usb_product ||
+ !strcmp(product, supported_fx3[j].usb_product))) {
+ prof = &supported_fx3[j];
+ break;
+ }
+ }
+
+ if (!prof)
+ continue;
+
+ sdi = g_malloc0(sizeof(struct sr_dev_inst));
+ sdi->status = SR_ST_INITIALIZING;
+ sdi->vendor = g_strdup(prof->vendor);
+ sdi->model = g_strdup(prof->model);
+ sdi->version = g_strdup(prof->model_version);
+ sdi->serial_num = g_strdup(serial_num);
+ sdi->connection_id = g_strdup(connection_id);
+
+ /* Fill in channellist according to this device's profile. */
+ num_logic_channels = prof->dev_caps & DEV_CAPS_16BIT ? 16 : 8;
+ num_analog_channels = prof->dev_caps & DEV_CAPS_AX_ANALOG ? 1 : 0;
+
+ /* Logic channels, all in one channel group. */
+ cg = g_malloc0(sizeof(struct sr_channel_group));
+ cg->name = g_strdup("Logic");
+ for (j = 0; j < num_logic_channels; j++) {
+ sprintf(channel_name, "D%d", j);
+ ch = sr_channel_new(sdi, j, SR_CHANNEL_LOGIC,
+ TRUE, channel_name);
+ cg->channels = g_slist_append(cg->channels, ch);
+ }
+ sdi->channel_groups = g_slist_append(NULL, cg);
+
+ for (j = 0; j < num_analog_channels; j++) {
+ snprintf(channel_name, 16, "A%d", j);
+ ch = sr_channel_new(sdi, j + num_logic_channels,
+ SR_CHANNEL_ANALOG, TRUE, channel_name);
+
+ /* Every analog channel gets its own channel group. */
+ cg = g_malloc0(sizeof(struct sr_channel_group));
+ cg->name = g_strdup(channel_name);
+ cg->channels = g_slist_append(NULL, ch);
+ sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
+ }
+
+ devc = cypress_fx3_dev_new();
+ devc->profile = prof;
+ sdi->priv = devc;
+ devices = g_slist_append(devices, sdi);
+
+ devc->samplerates = samplerates;
+ devc->num_samplerates = ARRAY_SIZE(samplerates);
+ has_firmware = usb_match_manuf_prod(devlist[i],
+ "sigrok", "cypress-fx3");
+ if (has_firmware) {
+ /* Already has the firmware, so fix the new address. */
+ sr_dbg("Found an Cypress FX3 device.");
+ sdi->status = SR_ST_INACTIVE;
+ sdi->inst_type = SR_INST_USB;
+ sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
+ libusb_get_device_address(devlist[i]), NULL);
+ } else {
+ if (ezusb_upload_firmware_fx3(drvc->sr_ctx, devlist[i],
+ USB_CONFIGURATION, prof->firmware) == SR_OK) {
+ /* Store when this device's FW was updated. */
+ devc->fw_updated = g_get_monotonic_time();
+ /*Add delay for the device to re-enumerat in SuperSpeed*/
+ g_usleep(1000 * 1000);
+ devices = g_slist_remove(devices, sdi);
+ /* Rescan and refresh the device list.
+ This is needed as the device renumerates as a SuperSpeed device */
+ libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
+ /* Restart the iteration */
+ i = 0;
+ continue;
+ } else {
+ sr_err("Firmware upload failed for "
+ "device %d.%d (logical), name %s.",
+ libusb_get_bus_number(devlist[i]),
+ libusb_get_device_address(devlist[i]),
+ prof->firmware);
+ }
+ sdi->inst_type = SR_INST_USB;
+ sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
+ 0xff, NULL);
+ }
+ }
+ libusb_free_device_list(devlist, 1);
+ g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
+
+ return std_scan_complete(di, devices);
+}
+
+static void clear_helper(struct dev_context *devc)
+{
+ g_slist_free(devc->enabled_analog_channels);
+}
+
+static int dev_clear(const struct sr_dev_driver *di)
+{
+ return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
+}
+
+static int dev_open(struct sr_dev_inst *sdi)
+{
+ struct sr_dev_driver *di = sdi->driver;
+ struct sr_usb_dev_inst *usb;
+ struct dev_context *devc;
+ int ret;
+ int64_t timediff_us, timediff_ms;
+
+ devc = sdi->priv;
+ usb = sdi->conn;
+
+ /*
+ * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
+ * milliseconds for the fx3 to renumerate.
+ */
+ ret = SR_ERR;
+
+ if (devc->fw_updated > 0) {
+ sr_info("Waiting for device to reset.");
+ /* Takes >= 300ms for the fx3 to be gone from the USB bus. */
+ g_usleep(300 * 1000);
+ timediff_ms = 0;
+ while (timediff_ms < MAX_RENUM_DELAY_MS) {
+ if ((ret = cypress_fx3_dev_open(sdi, di)) == SR_OK)
+ break;
+ g_usleep(100 * 1000);
+
+ timediff_us = g_get_monotonic_time() - devc->fw_updated;
+ timediff_ms = timediff_us / 1000;
+ sr_spew("Waited %" PRIi64 "ms.", timediff_ms);
+ }
+ if (ret != SR_OK) {
+ sr_err("Device failed to renumerate.");
+ return SR_ERR;
+ }
+ sr_info("Device came back after %" PRIi64 "ms.", timediff_ms);
+ } else {
+ sr_info("Firmware upload was not needed.");
+ ret = cypress_fx3_dev_open(sdi, di);
+ }
+
+ if (ret != SR_OK) {
+ sr_err("Unable to open device.");
+ return SR_ERR;
+ }
+
+ ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
+ if (ret != 0) {
+ switch (ret) {
+ case LIBUSB_ERROR_BUSY:
+ sr_err("Unable to claim USB interface. Another "
+ "program or driver has already claimed it.");
+ break;
+ case LIBUSB_ERROR_NO_DEVICE:
+ sr_err("Device has been disconnected.");
+ break;
+ default:
+ sr_err("Unable to claim interface: %s.",
+ libusb_error_name(ret));
+ break;
+ }
+
+ return SR_ERR;
+ }
+
+ if (devc->cur_samplerate == 0) {
+ /* Samplerate hasn't been set; default to the slowest one. */
+ devc->cur_samplerate = devc->samplerates[0];
+ }
+
+ return SR_OK;
+}
+
+static int dev_close(struct sr_dev_inst *sdi)
+{
+ struct sr_usb_dev_inst *usb;
+
+ usb = sdi->conn;
+
+ if (!usb->devhdl)
+ return SR_ERR_BUG;
+
+ sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
+ usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
+ libusb_release_interface(usb->devhdl, USB_INTERFACE);
+ libusb_close(usb->devhdl);
+ usb->devhdl = NULL;
+
+ return SR_OK;
+}
+
+static int config_get(uint32_t key, GVariant **data,
+ const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
+{
+ struct dev_context *devc;
+ struct sr_usb_dev_inst *usb;
+
+ (void)cg;
+
+ if (!sdi)
+ return SR_ERR_ARG;
+
+ devc = sdi->priv;
+
+ switch (key) {
+ case SR_CONF_CONN:
+ if (!sdi->conn)
+ return SR_ERR_ARG;
+ usb = sdi->conn;
+ if (usb->address == 255)
+ /* Device still needs to re-enumerate after firmware
+ * upload, so we don't know its (future) address. */
+ return SR_ERR;
+ *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
+ break;
+ case SR_CONF_LIMIT_FRAMES:
+ *data = g_variant_new_uint64(devc->limit_frames);
+ break;
+ case SR_CONF_LIMIT_SAMPLES:
+ *data = g_variant_new_uint64(devc->limit_samples);
+ break;
+ case SR_CONF_SAMPLERATE:
+ *data = g_variant_new_uint64(devc->cur_samplerate);
+ break;
+ case SR_CONF_CAPTURE_RATIO:
+ *data = g_variant_new_uint64(devc->capture_ratio);
+ break;
+ default:
+ return SR_ERR_NA;
+ }
+
+ return SR_OK;
+}
+
+static int config_set(uint32_t key, GVariant *data,
+ const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
+{
+ struct dev_context *devc;
+ int idx;
+
+ (void)cg;
+
+ if (!sdi)
+ return SR_ERR_ARG;
+
+ devc = sdi->priv;
+
+ switch (key) {
+ case SR_CONF_SAMPLERATE:
+ if ((idx = std_u64_idx(data, devc->samplerates, devc->num_samplerates)) < 0)
+ return SR_ERR_ARG;
+ devc->cur_samplerate = devc->samplerates[idx];
+ break;
+ case SR_CONF_LIMIT_FRAMES:
+ devc->limit_frames = g_variant_get_uint64(data);
+ break;
+ case SR_CONF_LIMIT_SAMPLES:
+ devc->limit_samples = g_variant_get_uint64(data);
+ break;
+ case SR_CONF_CAPTURE_RATIO:
+ devc->capture_ratio = g_variant_get_uint64(data);
+ break;
+ default:
+ return SR_ERR_NA;
+ }
+
+ return SR_OK;
+}
+
+static int config_list(uint32_t key, GVariant **data,
+ const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
+{
+ struct dev_context *devc;
+
+ devc = (sdi) ? sdi->priv : NULL;
+
+ switch (key) {
+ case SR_CONF_SCAN_OPTIONS:
+ case SR_CONF_DEVICE_OPTIONS:
+ if (cg)
+ return SR_ERR_NA;
+ return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
+ case SR_CONF_SAMPLERATE:
+ if (!devc)
+ return SR_ERR_NA;
+ *data = std_gvar_samplerates(devc->samplerates, devc->num_samplerates);
+ break;
+ case SR_CONF_TRIGGER_MATCH:
+ *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
+ break;
+ default:
+ return SR_ERR_NA;
+ }
+
+ return SR_OK;
+}
+
+static int cypress_fx3_acquisition_stop(struct sr_dev_inst *sdi)
+{
+ cypress_fx3_abort_acquisition(sdi->priv);
+
+ return SR_OK;
+}
+
+static struct sr_dev_driver cypress_fx3_driver_info = {
+ .name = "cypress-fx3",
+ .longname = "cypress-fx3 (generic driver for FX3 based LAs)",
+ .api_version = 1,
+ .init = std_init,
+ .cleanup = std_cleanup,
+ .scan = scan,
+ .dev_list = std_dev_list,
+ .dev_clear = dev_clear,
+ .config_get = config_get,
+ .config_set = config_set,
+ .config_list = config_list,
+ .dev_open = dev_open,
+ .dev_close = dev_close,
+ .dev_acquisition_start = cypress_fx3_start_acquisition,
+ .dev_acquisition_stop = cypress_fx3_acquisition_stop,
+ .context = NULL,
+};
+SR_REGISTER_DEV_DRIVER(cypress_fx3_driver_info);
\ No newline at end of file
diff --git a/src/hardware/cypress-fx3/protocol.c b/src/hardware/cypress-fx3/protocol.c
new file mode 100644
index 00000000..21aacf40
--- /dev/null
+++ b/src/hardware/cypress-fx3/protocol.c
@@ -0,0 +1,730 @@
+/*
+ * This file is part of the libsigrok project.
+ *
+ * Copyright (C) 2021 Ashwin Nair <[email protected]>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <glib.h>
+#include <glib/gstdio.h>
+#include "protocol.h"
+
+#pragma pack(push, 1)
+
+struct version_info {
+ uint8_t major;
+ uint8_t minor;
+};
+
+struct cmd_start_acquisition {
+ uint16_t sampling_factor;
+};
+
+#pragma pack(pop)
+
+#define USB_TIMEOUT 100
+
+static int command_get_fw_version(libusb_device_handle *devhdl,
+ struct version_info *vi)
+{
+ int ret;
+
+ ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
+ LIBUSB_ENDPOINT_IN, CMD_GET_FW_VERSION, 0x0000, 0x0000,
+ (unsigned char *)vi, sizeof(struct version_info), USB_TIMEOUT);
+
+ if (ret < 0) {
+ sr_err("Unable to get version info: %s.",
+ libusb_error_name(ret));
+ return SR_ERR;
+ }
+
+ return SR_OK;
+}
+
+static int command_get_revid_version(struct sr_dev_inst *sdi, uint8_t *revid)
+{
+ struct sr_usb_dev_inst *usb = sdi->conn;
+ libusb_device_handle *devhdl = usb->devhdl;
+ int ret;
+
+ ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
+ LIBUSB_ENDPOINT_IN, CMD_GET_REVID_VERSION, 0x0000, 0x0000,
+ revid, 1, USB_TIMEOUT);
+
+ if (ret < 0) {
+ sr_err("Unable to get REVID: %s.", libusb_error_name(ret));
+ return SR_ERR;
+ }
+
+ return SR_OK;
+}
+
+static int command_start_acquisition(const struct sr_dev_inst *sdi)
+{
+ struct dev_context *devc;
+ struct sr_usb_dev_inst *usb;
+ uint64_t samplerate;
+ struct cmd_start_acquisition cmd;
+ int ret;
+
+ devc = sdi->priv;
+ usb = sdi->conn;
+ samplerate = devc->cur_samplerate;
+
+ /* Compute the sample rate. */
+ if (devc->sample_wide && samplerate > MAX_16BIT_SAMPLE_RATE) {
+ sr_err("Unable to sample at %" PRIu64 "Hz "
+ "when collecting 16-bit samples.", samplerate);
+ return SR_ERR;
+ }
+
+ cmd.sampling_factor = (FX3_PIB_CLOCK)/(samplerate);
+
+ sr_spew("cmd.sampling_factor = %d",cmd.sampling_factor);
+
+ /* Send the control message. */
+ ret = libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
+ LIBUSB_ENDPOINT_OUT, CMD_START, 0x0000, 0x0000,
+ (unsigned char *)&cmd, sizeof(cmd), USB_TIMEOUT);
+ if (ret < 0) {
+ sr_err("Unable to send start command: %s.",
+ libusb_error_name(ret));
+ return SR_ERR;
+ }else{
+ sr_info("CMD_START vendor command sent successfully");
+ }
+
+ return SR_OK;
+}
+
+SR_PRIV int cypress_fx3_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di)
+{
+ libusb_device **devlist;
+ struct sr_usb_dev_inst *usb;
+ struct libusb_device_descriptor des;
+ struct dev_context *devc;
+ struct drv_context *drvc;
+ struct version_info vi;
+ int ret = SR_ERR, i, device_count;
+ uint8_t revid;
+ char connection_id[64];
+
+ drvc = di->context;
+ devc = sdi->priv;
+ usb = sdi->conn;
+
+ device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
+ if (device_count < 0) {
+ sr_err("Failed to get device list: %s.",
+ libusb_error_name(device_count));
+ return SR_ERR;
+ }
+
+ for (i = 0; i < device_count; i++) {
+ libusb_get_device_descriptor(devlist[i], &des);
+
+ if (des.idVendor != devc->profile->vid
+ || des.idProduct != devc->profile->pid)
+ continue;
+
+ if ((sdi->status == SR_ST_INITIALIZING) ||
+ (sdi->status == SR_ST_INACTIVE)) {
+
+ /*
+ * Check device by its physical USB bus/port address.
+ */
+ if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
+ {
+ continue;
+ }
+
+ if (strcmp(sdi->connection_id, connection_id))
+ {
+ /* This is not the one. */
+ continue;
+ }
+ }
+
+ if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
+ if (usb->address == 0xff)
+ /*
+ * First time we touch this device after FW
+ * upload, so we don't know the address yet.
+ */
+ usb->address = libusb_get_device_address(devlist[i]);
+ } else {
+ sr_err("Failed to open device: %s.",
+ libusb_error_name(ret));
+ ret = SR_ERR;
+ break;
+ }
+
+ if (libusb_has_capability(LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER)) {
+ if (libusb_kernel_driver_active(usb->devhdl, USB_INTERFACE) == 1) {
+ if ((ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE)) < 0) {
+ sr_err("Failed to detach kernel driver: %s.",
+ libusb_error_name(ret));
+ ret = SR_ERR;
+ break;
+ }
+ }
+ }
+
+ ret = command_get_fw_version(usb->devhdl, &vi);
+ if (ret != SR_OK) {
+ sr_err("Failed to get firmware version.");
+ break;
+ }
+
+ ret = command_get_revid_version(sdi, &revid);
+ if (ret != SR_OK) {
+ sr_err("Failed to get REVID.");
+ break;
+ }
+
+ /*
+ * Changes in major version mean incompatible/API changes, so
+ * bail out if we encounter an incompatible version.
+ * Different minor versions are OK, they should be compatible.
+ */
+ if (vi.major != FX3_REQUIRED_VERSION_MAJOR) {
+ sr_err("Expected firmware version %d.x, "
+ "got %d.%d.", FX3_REQUIRED_VERSION_MAJOR,
+ vi.major, vi.minor);
+ break;
+ }
+
+ sr_info("Opened device on %d.%d (logical) / %s (physical), "
+ "interface %d, firmware %d.%d.",
+ usb->bus, usb->address, connection_id,
+ USB_INTERFACE, vi.major, vi.minor);
+
+ sr_info("Detected REVID, it's a Cypress FX3!\n");
+
+ ret = SR_OK;
+
+ break;
+ }
+ libusb_free_device_list(devlist, 1);
+
+ return ret;
+}
+
+SR_PRIV struct dev_context *cypress_fx3_dev_new(void)
+{
+ struct dev_context *devc;
+
+ devc = g_malloc0(sizeof(struct dev_context));
+ devc->profile = NULL;
+ devc->fw_updated = 0;
+ devc->cur_samplerate = 0;
+ devc->limit_frames = 1;
+ devc->limit_samples = 0;
+ devc->capture_ratio = 0;
+ devc->sample_wide = FALSE;
+ devc->num_frames = 0;
+ devc->stl = NULL;
+
+ return devc;
+}
+
+SR_PRIV void cypress_fx3_abort_acquisition(struct dev_context *devc)
+{
+ int i;
+
+ devc->acq_aborted = TRUE;
+
+ for (i = devc->num_transfers - 1; i >= 0; i--) {
+ if (devc->transfers[i])
+ libusb_cancel_transfer(devc->transfers[i]);
+ }
+}
+
+static void finish_acquisition(struct sr_dev_inst *sdi)
+{
+ struct dev_context *devc;
+
+ devc = sdi->priv;
+
+ std_session_send_df_end(sdi);
+
+ usb_source_remove(sdi->session, devc->ctx);
+
+ devc->num_transfers = 0;
+ g_free(devc->transfers);
+
+ /* Free the deinterlace buffers if we had them. */
+ if (g_slist_length(devc->enabled_analog_channels) > 0) {
+ g_free(devc->logic_buffer);
+ g_free(devc->analog_buffer);
+ }
+
+ if (devc->stl) {
+ soft_trigger_logic_free(devc->stl);
+ devc->stl = NULL;
+ }
+}
+
+static void free_transfer(struct libusb_transfer *transfer)
+{
+ struct sr_dev_inst *sdi;
+ struct dev_context *devc;
+ unsigned int i;
+
+ sdi = transfer->user_data;
+ devc = sdi->priv;
+
+ g_free(transfer->buffer);
+ transfer->buffer = NULL;
+ libusb_free_transfer(transfer);
+
+ for (i = 0; i < devc->num_transfers; i++) {