-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbeagle_py.py
More file actions
2481 lines (2095 loc) · 181 KB
/
beagle_py.py
File metadata and controls
2481 lines (2095 loc) · 181 KB
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
#==========================================================================
# Beagle Interface Library
#--------------------------------------------------------------------------
# Copyright (c) 2004-2011 Total Phase, Inc.
# All rights reserved.
# www.totalphase.com
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# - Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# - Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# - Neither the name of Total Phase, Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#--------------------------------------------------------------------------
# To access Total Phase Beagle devices through the API:
#
# 1) Use one of the following shared objects:
# beagle.so -- Linux shared object
# or
# beagle.dll -- Windows dynamic link library
#
# 2) Along with one of the following language modules:
# beagle.c/h -- C/C++ API header file and interface module
# beagle_py.py -- Python API
# beagle.bas -- Visual Basic 6 API
# beagle.cs -- C# .NET source
# beagle_net.dll -- Compiled .NET binding
#==========================================================================
#==========================================================================
# VERSION
#==========================================================================
BG_API_VERSION = 0x050a # v5.10
BG_REQ_SW_VERSION = 0x050a # v5.10
import os
import sys
try:
import beagle as api
except ImportError, ex1:
import imp, platform, struct
# pick system, 32/64, and extension
system = "linux"
architecture = "x86_64"
extension = '.so'
if platform.system() in ['Windows', 'Microsoft']:
system = 'windows'
extension = '.dll'
elif platform.system() in ('Darwin'):
system = 'macosx'
if struct.calcsize('P') * 8 == 32:
architecture = 'i686'
lib_folder = system + '-' + architecture
print("Library folder: %s" % lib_folder)
try:
api = imp.load_dynamic('beagle', lib_folder + '/beagle' + extension)
except ImportError, ex2:
import_err_msg = 'Error importing beagle%s\n' % ext
import_err_msg += ' Architecture of beagle%s may be wrong\n' % ext
import_err_msg += '%s\n%s' % (ex1, ex2)
raise ImportError(import_err_msg)
BG_SW_VERSION = api.py_version() & 0xffff
BG_REQ_API_VERSION = (api.py_version() >> 16) & 0xffff
BG_LIBRARY_LOADED = \
((BG_SW_VERSION >= BG_REQ_SW_VERSION) and \
(BG_API_VERSION >= BG_REQ_API_VERSION))
from array import array, ArrayType
import struct
#==========================================================================
# HELPER FUNCTIONS
#==========================================================================
def array_u08 (n): return array('B', '\0'*n)
def array_u16 (n): return array('H', '\0\0'*n)
def array_u32 (n): return array('I', '\0\0\0\0'*n)
def array_u64 (n): return array('K', '\0\0\0\0\0\0\0\0'*n)
def array_s08 (n): return array('b', '\0'*n)
def array_s16 (n): return array('h', '\0\0'*n)
def array_s32 (n): return array('i', '\0\0\0\0'*n)
def array_s64 (n): return array('L', '\0\0\0\0\0\0\0\0'*n)
def array_f32 (n): return array('f', '\0\0\0\0'*n)
def array_f64 (n): return array('d', '\0\0\0\0\0\0\0\0'*n)
#==========================================================================
# STATUS CODES
#==========================================================================
# All API functions return an integer which is the result of the
# transaction, or a status code if negative. The status codes are
# defined as follows:
# enum BeagleStatus
# General codes (0 to -99)
BG_OK = 0
BG_UNABLE_TO_LOAD_LIBRARY = -1
BG_UNABLE_TO_LOAD_DRIVER = -2
BG_UNABLE_TO_LOAD_FUNCTION = -3
BG_INCOMPATIBLE_LIBRARY = -4
BG_INCOMPATIBLE_DEVICE = -5
BG_INCOMPATIBLE_DRIVER = -6
BG_COMMUNICATION_ERROR = -7
BG_UNABLE_TO_OPEN = -8
BG_UNABLE_TO_CLOSE = -9
BG_INVALID_HANDLE = -10
BG_CONFIG_ERROR = -11
BG_UNKNOWN_PROTOCOL = -12
BG_STILL_ACTIVE = -13
BG_FUNCTION_NOT_AVAILABLE = -14
BG_INVALID_LICENSE = -15
BG_CAPTURE_NOT_TRIGGERED = -16
BG_CAPTURE_NOT_READY_FOR_DOWNLOAD = -17
# COMMTEST codes (-100 to -199)
BG_COMMTEST_NOT_AVAILABLE = -100
BG_COMMTEST_NOT_ENABLED = -101
# I2C codes (-200 to -299)
BG_I2C_NOT_AVAILABLE = -200
BG_I2C_NOT_ENABLED = -201
# SPI codes (-300 to -399)
BG_SPI_NOT_AVAILABLE = -300
BG_SPI_NOT_ENABLED = -301
# USB codes (-400 to -499)
BG_USB_NOT_AVAILABLE = -400
BG_USB_NOT_ENABLED = -401
BG_USB2_NOT_ENABLED = -402
BG_USB3_NOT_ENABLED = -403
# Cross-Analyzer Sync codes (-410 to -413)
BG_CROSS_ANALYZER_SYNC_DISTURBED_RE_ENABLE = -410
BG_CROSS_ANALYZER_SYNC_DISTURBED_RECONNECT = -411
BG_CROSS_ANALYZER_SYNC_UNLICENSED_SELF = -412
BG_CROSS_ANALYZER_SYNC_UNLICENSED_OTHER = -413
# Complex Triggering Config codes (-450 to -469)
BG_COMPLEX_CONFIG_ERROR_NO_STATES = -450
BG_COMPLEX_CONFIG_ERROR_DATA_PACKET_TYPE = -451
BG_COMPLEX_CONFIG_ERROR_DATA_FIELD = -452
BG_COMPLEX_CONFIG_ERROR_ERR_MATCH_FIELD = -453
BG_COMPLEX_CONFIG_ERROR_DATA_RESOURCES = -454
BG_COMPLEX_CONFIG_ERROR_DP_MATCH_TYPE = -455
BG_COMPLEX_CONFIG_ERROR_DP_MATCH_VAL = -456
BG_COMPLEX_CONFIG_ERROR_DP_REQUIRED = -457
BG_COMPLEX_CONFIG_ERROR_DP_RESOURCES = -458
BG_COMPLEX_CONFIG_ERROR_TIMER_UNIT = -459
BG_COMPLEX_CONFIG_ERROR_TIMER_BOUNDS = -460
BG_COMPLEX_CONFIG_ERROR_ASYNC_EVENT = -461
BG_COMPLEX_CONFIG_ERROR_ASYNC_EDGE = -462
BG_COMPLEX_CONFIG_ERROR_ACTION_FILTER = -463
BG_COMPLEX_CONFIG_ERROR_ACTION_GOTO_SEL = -464
BG_COMPLEX_CONFIG_ERROR_ACTION_GOTO_DEST = -465
BG_COMPLEX_CONFIG_ERROR_BAD_VBUS_TRIGGER_TYPE = -466
BG_COMPLEX_CONFIG_ERROR_BAD_VBUS_TRIGGER_THRES = -467
BG_COMPLEX_CONFIG_ERROR_NO_MULTI_VBUS_TRIGGERS = -468
BG_COMPLEX_CONFIG_ERROR_IV_MONITOR_NOT_ENABLED = -469
# MDIO codes (-500 to -599)
BG_MDIO_NOT_AVAILABLE = -500
BG_MDIO_NOT_ENABLED = -501
BG_MDIO_BAD_TURNAROUND = -502
# IV MON codes (-600 to -699)
BG_IV_MON_NULL_PACKET = -600
BG_IV_MON_INVALID_PACKET_LENGTH = -601
#==========================================================================
# GENERAL TYPE DEFINITIONS
#==========================================================================
# Beagle handle type definition
# typedef Beagle => integer
# Beagle version matrix.
#
# This matrix describes the various version dependencies
# of Beagle components. It can be used to determine
# which component caused an incompatibility error.
#
# All version numbers are of the format:
# (major << 8) | minor
#
# ex. v1.20 would be encoded as: 0x0114
class BeagleVersion:
def __init__ (self):
# Software, firmware, and hardware versions.
self.software = 0
self.firmware = 0
self.hardware = 0
# Hardware revisions that are compatible with this software version.
# The top 16 bits gives the maximum accepted hw revision.
# The lower 16 bits gives the minimum accepted hw revision.
self.hw_revs_for_sw = 0
# Firmware revisions that are compatible with this software version.
# The top 16 bits gives the maximum accepted fw revision.
# The lower 16 bits gives the minimum accepted fw revision.
self.fw_revs_for_sw = 0
# Driver revisions that are compatible with this software version.
# The top 16 bits gives the maximum accepted driver revision.
# The lower 16 bits gives the minimum accepted driver revision.
# This version checking is currently only pertinent for WIN32
# platforms.
self.drv_revs_for_sw = 0
# Software requires that the API interface must be >= this version.
self.api_req_by_sw = 0
#==========================================================================
# GENERAL API
#==========================================================================
# Get a list of ports to which Beagle devices are attached.
#
# num_devices = maximum number of elements to return
# devices = array into which the port numbers are returned
#
# Each element of the array is written with the port number.
# Devices that are in-use are ORed with BG_PORT_NOT_FREE
# (0x8000).
#
# ex. devices are attached to ports 0, 1, 2
# ports 0 and 2 are available, and port 1 is in-use.
# array => 0x0000, 0x8001, 0x0002
#
# If the array is NULL, it is not filled with any values.
# If there are more devices than the array size, only the
# first nmemb port numbers will be written into the array.
#
# Returns the number of devices found, regardless of the
# array size.
BG_PORT_NOT_FREE = 0x8000
def bg_find_devices (devices):
"""usage: (int return, u16[] devices) = bg_find_devices(u16[] devices)
All arrays can be passed into the API as an ArrayType object or as
a tuple (array, length), where array is an ArrayType object and
length is an integer. The user-specified length would then serve
as the length argument to the API funtion (please refer to the
product datasheet). If only the array is provided, the array's
intrinsic length is used as the argument to the underlying API
function.
Additionally, for arrays that are filled by the API function, an
integer can be passed in place of the array argument and the API
will automatically create an array of that length. All output
arrays, whether passed in or generated, are passed back in the
returned tuple."""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# devices pre-processing
__devices = isinstance(devices, int)
if __devices:
(devices, num_devices) = (array_u16(devices), devices)
else:
(devices, num_devices) = isinstance(devices, ArrayType) and (devices, len(devices)) or (devices[0], min(len(devices[0]), int(devices[1])))
if devices.typecode != 'H':
raise TypeError("type for 'devices' must be array('H')")
# Call API function
(_ret_) = api.py_bg_find_devices(num_devices, devices)
# devices post-processing
if __devices: del devices[max(0, min(_ret_, len(devices))):]
return (_ret_, devices)
# Get a list of ports to which Beagle devices are attached
#
# This function is the same as bg_find_devices() except that
# it returns the unique IDs of each Beagle device. The IDs
# are guaranteed to be non-zero if valid.
#
# The IDs are the unsigned integer representation of the 10-digit
# serial numbers.
def bg_find_devices_ext (devices, unique_ids):
"""usage: (int return, u16[] devices, u32[] unique_ids) = bg_find_devices_ext(u16[] devices, u32[] unique_ids)
All arrays can be passed into the API as an ArrayType object or as
a tuple (array, length), where array is an ArrayType object and
length is an integer. The user-specified length would then serve
as the length argument to the API funtion (please refer to the
product datasheet). If only the array is provided, the array's
intrinsic length is used as the argument to the underlying API
function.
Additionally, for arrays that are filled by the API function, an
integer can be passed in place of the array argument and the API
will automatically create an array of that length. All output
arrays, whether passed in or generated, are passed back in the
returned tuple."""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# devices pre-processing
__devices = isinstance(devices, int)
if __devices:
(devices, num_devices) = (array_u16(devices), devices)
else:
(devices, num_devices) = isinstance(devices, ArrayType) and (devices, len(devices)) or (devices[0], min(len(devices[0]), int(devices[1])))
if devices.typecode != 'H':
raise TypeError("type for 'devices' must be array('H')")
# unique_ids pre-processing
__unique_ids = isinstance(unique_ids, int)
if __unique_ids:
(unique_ids, num_ids) = (array_u32(unique_ids), unique_ids)
else:
(unique_ids, num_ids) = isinstance(unique_ids, ArrayType) and (unique_ids, len(unique_ids)) or (unique_ids[0], min(len(unique_ids[0]), int(unique_ids[1])))
if unique_ids.typecode != 'I':
raise TypeError("type for 'unique_ids' must be array('I')")
# Call API function
(_ret_) = api.py_bg_find_devices_ext(num_devices, num_ids, devices, unique_ids)
# devices post-processing
if __devices: del devices[max(0, min(_ret_, len(devices))):]
# unique_ids post-processing
if __unique_ids: del unique_ids[max(0, min(_ret_, len(unique_ids))):]
return (_ret_, devices, unique_ids)
# Open the Beagle port.
#
# The port number is a zero-indexed integer.
#
# The port number is the same as that obtained from the
# bg_find_devices() function above.
#
# Returns an Beagle handle, which is guaranteed to be
# greater than zero if it is valid.
#
# This function is recommended for use in simple applications
# where extended information is not required. For more complex
# applications, the use of bg_open_ext() is recommended.
def bg_open (port_number):
"""usage: Beagle return = bg_open(int port_number)"""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# Call API function
return api.py_bg_open(port_number)
# Open the Beagle port, returning extended information
# in the supplied structure. Behavior is otherwise identical
# to bg_open() above. If 0 is passed as the pointer to the
# structure, this function is exactly equivalent to bg_open().
#
# The structure is zeroed before the open is attempted.
# It is filled with whatever information is available.
#
# For example, if the hardware version is not filled, then
# the device could not be queried for its version number.
#
# This function is recommended for use in complex applications
# where extended information is required. For more simple
# applications, the use of bg_open() is recommended.
class BeagleExt:
def __init__ (self):
# Version matrix
self.version = BeagleVersion()
# Features of this device.
self.features = 0
def bg_open_ext (port_number):
"""usage: (Beagle return, BeagleExt bg_ext) = bg_open_ext(int port_number)"""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# Call API function
(_ret_, c_bg_ext) = api.py_bg_open_ext(port_number)
# bg_ext post-processing
bg_ext = BeagleExt()
(bg_ext.version.software, bg_ext.version.firmware, bg_ext.version.hardware, bg_ext.version.hw_revs_for_sw, bg_ext.version.fw_revs_for_sw, bg_ext.version.drv_revs_for_sw, bg_ext.version.api_req_by_sw, bg_ext.features) = c_bg_ext
return (_ret_, bg_ext)
# Close the Beagle port.
def bg_close (beagle):
"""usage: int return = bg_close(Beagle beagle)"""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# Call API function
return api.py_bg_close(beagle)
# Return the port for this Beagle handle.
#
# The port number is a zero-indexed integer.
def bg_port (beagle):
"""usage: int return = bg_port(Beagle beagle)"""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# Call API function
return api.py_bg_port(beagle)
# Return the device features as a bit-mask of values, or
# an error code if the handle is not valid.
BG_FEATURE_NONE = 0x00000000
BG_FEATURE_I2C = 0x00000001
BG_FEATURE_SPI = 0x00000002
BG_FEATURE_USB = 0x00000004
BG_FEATURE_MDIO = 0x00000008
BG_FEATURE_USB_HS = 0x00000010
BG_FEATURE_USB_SS = 0x00000020
def bg_features (beagle):
"""usage: int return = bg_features(Beagle beagle)"""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# Call API function
return api.py_bg_features(beagle)
def bg_unique_id_to_features (unique_id):
"""usage: int return = bg_unique_id_to_features(u32 unique_id)"""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# Call API function
return api.py_bg_unique_id_to_features(unique_id)
# Return the unique ID for this Beagle adapter.
# IDs are guaranteed to be non-zero if valid.
# The ID is the unsigned integer representation of the
# 10-digit serial number.
def bg_unique_id (beagle):
"""usage: u32 return = bg_unique_id(Beagle beagle)"""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# Call API function
return api.py_bg_unique_id(beagle)
# Return the status string for the given status code.
# If the code is not valid or the library function cannot
# be loaded, return a NULL string.
def bg_status_string (status):
"""usage: str return = bg_status_string(int status)"""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# Call API function
return api.py_bg_status_string(status)
# Return the version matrix for the device attached to the
# given handle. If the handle is 0 or invalid, only the
# software and required api versions are set.
def bg_version (beagle):
"""usage: (int return, BeagleVersion version) = bg_version(Beagle beagle)"""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# Call API function
(_ret_, c_version) = api.py_bg_version(beagle)
# version post-processing
version = BeagleVersion()
(version.software, version.firmware, version.hardware, version.hw_revs_for_sw, version.fw_revs_for_sw, version.drv_revs_for_sw, version.api_req_by_sw) = c_version
return (_ret_, version)
# Set the capture latency to the specified number of milliseconds.
# This number determines the minimum time that a read call will
# block if there is no available data. Lower times result in
# faster turnaround at the expense of reduced buffering. Setting
# this parameter too low can cause packets to be dropped.
def bg_latency (beagle, milliseconds):
"""usage: int return = bg_latency(Beagle beagle, u32 milliseconds)"""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# Call API function
return api.py_bg_latency(beagle, milliseconds)
# Set the capture timeout to the specified number of milliseconds.
# If any read call has a longer idle than this value, that call
# will return with a count of 0 bytes.
def bg_timeout (beagle, milliseconds):
"""usage: int return = bg_timeout(Beagle beagle, u32 milliseconds)"""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# Call API function
return api.py_bg_timeout(beagle, milliseconds)
# Sleep for the specified number of milliseconds
# Accuracy depends on the operating system scheduler
# Returns the number of milliseconds slept
def bg_sleep_ms (milliseconds):
"""usage: u32 return = bg_sleep_ms(u32 milliseconds)"""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# Call API function
return api.py_bg_sleep_ms(milliseconds)
# Configure the target power pin.
BG_TARGET_POWER_OFF = 0x00
BG_TARGET_POWER_ON = 0x01
BG_TARGET_POWER_QUERY = 0x80
def bg_target_power (beagle, power_flag):
"""usage: int return = bg_target_power(Beagle beagle, u08 power_flag)"""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# Call API function
return api.py_bg_target_power(beagle, power_flag)
BG_HOST_IFCE_FULL_SPEED = 0x00
BG_HOST_IFCE_HIGH_SPEED = 0x01
BG_HOST_IFCE_SUPER_SPEED = 0x02
def bg_host_ifce_speed (beagle):
"""usage: int return = bg_host_ifce_speed(Beagle beagle)"""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# Call API function
return api.py_bg_host_ifce_speed(beagle)
# Returns the device address that the beagle is attached to.
def bg_dev_addr (beagle):
"""usage: int return = bg_dev_addr(Beagle beagle)"""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# Call API function
return api.py_bg_dev_addr(beagle)
#==========================================================================
# BUFFERING API
#==========================================================================
# Set the amount of buffering that is to be allocated on the PC.
# Pass zero to num_bytes to query the existing buffer size.
def bg_host_buffer_size (beagle, num_bytes):
"""usage: int return = bg_host_buffer_size(Beagle beagle, u32 num_bytes)"""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# Call API function
return api.py_bg_host_buffer_size(beagle, num_bytes)
# Query the amount of buffering that is unused and free for buffering.
def bg_host_buffer_free (beagle):
"""usage: int return = bg_host_buffer_free(Beagle beagle)"""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# Call API function
return api.py_bg_host_buffer_free(beagle)
# Query the amount of buffering that is used and no longer available.
def bg_host_buffer_used (beagle):
"""usage: int return = bg_host_buffer_used(Beagle beagle)"""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# Call API function
return api.py_bg_host_buffer_used(beagle)
# Benchmark the speed of the host to Beagle interface
def bg_commtest (beagle, num_samples, delay_count):
"""usage: int return = bg_commtest(Beagle beagle, int num_samples, int delay_count)"""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# Call API function
return api.py_bg_commtest(beagle, num_samples, delay_count)
#==========================================================================
# MONITORING API
#==========================================================================
# Protocol codes
# enum BeagleProtocol
BG_PROTOCOL_NONE = 0
BG_PROTOCOL_COMMTEST = 1
BG_PROTOCOL_USB = 2
BG_PROTOCOL_I2C = 3
BG_PROTOCOL_SPI = 4
BG_PROTOCOL_MDIO = 5
# Common Beagle read status codes
# PARTIAL_LAST_BYTE Unused by USB 480 and 5000
BG_READ_OK = 0x00000000
BG_READ_TIMEOUT = 0x00000100
BG_READ_ERR_MIDDLE_OF_PACKET = 0x00000200
BG_READ_ERR_SHORT_BUFFER = 0x00000400
BG_READ_ERR_PARTIAL_LAST_BYTE = 0x00000800
BG_READ_ERR_PARTIAL_LAST_BYTE_MASK = 0x0000000f
BG_READ_ERR_UNEXPECTED = 0x00001000
# Enable the Beagle monitor
def bg_enable (beagle, protocol):
"""usage: int return = bg_enable(Beagle beagle, BeagleProtocol protocol)"""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# Call API function
return api.py_bg_enable(beagle, protocol)
# Disable the Beagle monitor
def bg_disable (beagle):
"""usage: int return = bg_disable(Beagle beagle)"""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# Call API function
return api.py_bg_disable(beagle)
# Capture stop function only supported for analyzers with
# on-board triggering capability. For other analyzers, use
# bg_disable to stop the capture and download to PC.
def bg_capture_stop (beagle):
"""usage: int return = bg_capture_stop(Beagle beagle)"""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# Call API function
return api.py_bg_capture_stop(beagle)
def bg_capture_trigger (beagle):
"""usage: int return = bg_capture_trigger(Beagle beagle)"""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# Call API function
return api.py_bg_capture_trigger(beagle)
# Capture status; general across protocols but used in
# protocol-specific capture-status-query functions as well
# enum BeagleCaptureStatus
BG_CAPTURE_STATUS_UNKNOWN = -1
BG_CAPTURE_STATUS_INACTIVE = 0
BG_CAPTURE_STATUS_SYNC_STANDBY = 1
BG_CAPTURE_STATUS_PRE_TRIGGER = 2
BG_CAPTURE_STATUS_PRE_TRIGGER_SYNC = 3
BG_CAPTURE_STATUS_POST_TRIGGER = 4
BG_CAPTURE_STATUS_TRANSFER = 5
BG_CAPTURE_STATUS_COMPLETE = 6
def bg_capture_trigger_wait (beagle, timeout_ms):
"""usage: (int return, BeagleCaptureStatus status) = bg_capture_trigger_wait(Beagle beagle, u32 timeout_ms)"""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# Call API function
return api.py_bg_capture_trigger_wait(beagle, timeout_ms)
# Set the sample rate in kilohertz.
def bg_samplerate (beagle, samplerate_khz):
"""usage: int return = bg_samplerate(Beagle beagle, int samplerate_khz)"""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# Call API function
return api.py_bg_samplerate(beagle, samplerate_khz)
# Get the number of bits for the given number of bytes in the
# given protocol.
# Use this to determine how large a bit_timing array to allocate
# for bg_*_read_bit_timing functions.
def bg_bit_timing_size (protocol, num_data_bytes):
"""usage: int return = bg_bit_timing_size(BeagleProtocol protocol, int num_data_bytes)"""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# Call API function
return api.py_bg_bit_timing_size(protocol, num_data_bytes)
#==========================================================================
# I2C API
#==========================================================================
# Configure the I2C pullup resistors.
BG_I2C_PULLUP_OFF = 0x00
BG_I2C_PULLUP_ON = 0x01
BG_I2C_PULLUP_QUERY = 0x80
def bg_i2c_pullup (beagle, pullup_flag):
"""usage: int return = bg_i2c_pullup(Beagle beagle, u08 pullup_flag)"""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# Call API function
return api.py_bg_i2c_pullup(beagle, pullup_flag)
BG_I2C_MONITOR_DATA = 0x00ff
BG_I2C_MONITOR_NACK = 0x0100
BG_READ_I2C_NO_STOP = 0x00010000
def bg_i2c_read (beagle, data_in):
"""usage: (int return, u32 status, u64 time_sop, u64 time_duration, u32 time_dataoffset, u16[] data_in) = bg_i2c_read(Beagle beagle, u16[] data_in)
All arrays can be passed into the API as an ArrayType object or as
a tuple (array, length), where array is an ArrayType object and
length is an integer. The user-specified length would then serve
as the length argument to the API funtion (please refer to the
product datasheet). If only the array is provided, the array's
intrinsic length is used as the argument to the underlying API
function.
Additionally, for arrays that are filled by the API function, an
integer can be passed in place of the array argument and the API
will automatically create an array of that length. All output
arrays, whether passed in or generated, are passed back in the
returned tuple."""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# data_in pre-processing
__data_in = isinstance(data_in, int)
if __data_in:
(data_in, max_bytes) = (array_u16(data_in), data_in)
else:
(data_in, max_bytes) = isinstance(data_in, ArrayType) and (data_in, len(data_in)) or (data_in[0], min(len(data_in[0]), int(data_in[1])))
if data_in.typecode != 'H':
raise TypeError("type for 'data_in' must be array('H')")
# Call API function
(_ret_, status, time_sop, time_duration, time_dataoffset) = api.py_bg_i2c_read(beagle, max_bytes, data_in)
# data_in post-processing
if __data_in: del data_in[max(0, min(_ret_, len(data_in))):]
return (_ret_, status, time_sop, time_duration, time_dataoffset, data_in)
def bg_i2c_read_data_timing (beagle, data_in, data_timing):
"""usage: (int return, u32 status, u64 time_sop, u64 time_duration, u32 time_dataoffset, u16[] data_in, u32[] data_timing) = bg_i2c_read_data_timing(Beagle beagle, u16[] data_in, u32[] data_timing)
All arrays can be passed into the API as an ArrayType object or as
a tuple (array, length), where array is an ArrayType object and
length is an integer. The user-specified length would then serve
as the length argument to the API funtion (please refer to the
product datasheet). If only the array is provided, the array's
intrinsic length is used as the argument to the underlying API
function.
Additionally, for arrays that are filled by the API function, an
integer can be passed in place of the array argument and the API
will automatically create an array of that length. All output
arrays, whether passed in or generated, are passed back in the
returned tuple."""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# data_in pre-processing
__data_in = isinstance(data_in, int)
if __data_in:
(data_in, max_bytes) = (array_u16(data_in), data_in)
else:
(data_in, max_bytes) = isinstance(data_in, ArrayType) and (data_in, len(data_in)) or (data_in[0], min(len(data_in[0]), int(data_in[1])))
if data_in.typecode != 'H':
raise TypeError("type for 'data_in' must be array('H')")
# data_timing pre-processing
__data_timing = isinstance(data_timing, int)
if __data_timing:
(data_timing, max_timing) = (array_u32(data_timing), data_timing)
else:
(data_timing, max_timing) = isinstance(data_timing, ArrayType) and (data_timing, len(data_timing)) or (data_timing[0], min(len(data_timing[0]), int(data_timing[1])))
if data_timing.typecode != 'I':
raise TypeError("type for 'data_timing' must be array('I')")
# Call API function
(_ret_, status, time_sop, time_duration, time_dataoffset) = api.py_bg_i2c_read_data_timing(beagle, max_bytes, max_timing, data_in, data_timing)
# data_in post-processing
if __data_in: del data_in[max(0, min(_ret_, len(data_in))):]
# data_timing post-processing
if __data_timing: del data_timing[max(0, min(_ret_, len(data_timing))):]
return (_ret_, status, time_sop, time_duration, time_dataoffset, data_in, data_timing)
def bg_i2c_read_bit_timing (beagle, data_in, bit_timing):
"""usage: (int return, u32 status, u64 time_sop, u64 time_duration, u32 time_dataoffset, u16[] data_in, u32[] bit_timing) = bg_i2c_read_bit_timing(Beagle beagle, u16[] data_in, u32[] bit_timing)
All arrays can be passed into the API as an ArrayType object or as
a tuple (array, length), where array is an ArrayType object and
length is an integer. The user-specified length would then serve
as the length argument to the API funtion (please refer to the
product datasheet). If only the array is provided, the array's
intrinsic length is used as the argument to the underlying API
function.
Additionally, for arrays that are filled by the API function, an
integer can be passed in place of the array argument and the API
will automatically create an array of that length. All output
arrays, whether passed in or generated, are passed back in the
returned tuple."""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# data_in pre-processing
__data_in = isinstance(data_in, int)
if __data_in:
(data_in, max_bytes) = (array_u16(data_in), data_in)
else:
(data_in, max_bytes) = isinstance(data_in, ArrayType) and (data_in, len(data_in)) or (data_in[0], min(len(data_in[0]), int(data_in[1])))
if data_in.typecode != 'H':
raise TypeError("type for 'data_in' must be array('H')")
# bit_timing pre-processing
__bit_timing = isinstance(bit_timing, int)
if __bit_timing:
(bit_timing, max_timing) = (array_u32(bit_timing), bit_timing)
else:
(bit_timing, max_timing) = isinstance(bit_timing, ArrayType) and (bit_timing, len(bit_timing)) or (bit_timing[0], min(len(bit_timing[0]), int(bit_timing[1])))
if bit_timing.typecode != 'I':
raise TypeError("type for 'bit_timing' must be array('I')")
# Call API function
(_ret_, status, time_sop, time_duration, time_dataoffset) = api.py_bg_i2c_read_bit_timing(beagle, max_bytes, max_timing, data_in, bit_timing)
# data_in post-processing
if __data_in: del data_in[max(0, min(_ret_, len(data_in))):]
# bit_timing post-processing
if __bit_timing: del bit_timing[max(0, min(bg_bit_timing_size(BG_PROTOCOL_I2C, _ret_), len(bit_timing))):]
return (_ret_, status, time_sop, time_duration, time_dataoffset, data_in, bit_timing)
#==========================================================================
# SPI API
#==========================================================================
# enum BeagleSpiSSPolarity
BG_SPI_SS_ACTIVE_LOW = 0
BG_SPI_SS_ACTIVE_HIGH = 1
# enum BeagleSpiSckSamplingEdge
BG_SPI_SCK_SAMPLING_EDGE_RISING = 0
BG_SPI_SCK_SAMPLING_EDGE_FALLING = 1
# enum BeagleSpiBitorder
BG_SPI_BITORDER_MSB = 0
BG_SPI_BITORDER_LSB = 1
def bg_spi_configure (beagle, ss_polarity, sck_sampling_edge, bitorder):
"""usage: int return = bg_spi_configure(Beagle beagle, BeagleSpiSSPolarity ss_polarity, BeagleSpiSckSamplingEdge sck_sampling_edge, BeagleSpiBitorder bitorder)"""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# Call API function
return api.py_bg_spi_configure(beagle, ss_polarity, sck_sampling_edge, bitorder)
def bg_spi_read (beagle, data_mosi, data_miso):
"""usage: (int return, u32 status, u64 time_sop, u64 time_duration, u32 time_dataoffset, u08[] data_mosi, u08[] data_miso) = bg_spi_read(Beagle beagle, u08[] data_mosi, u08[] data_miso)
All arrays can be passed into the API as an ArrayType object or as
a tuple (array, length), where array is an ArrayType object and
length is an integer. The user-specified length would then serve
as the length argument to the API funtion (please refer to the
product datasheet). If only the array is provided, the array's
intrinsic length is used as the argument to the underlying API
function.
Additionally, for arrays that are filled by the API function, an
integer can be passed in place of the array argument and the API
will automatically create an array of that length. All output
arrays, whether passed in or generated, are passed back in the
returned tuple."""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# data_mosi pre-processing
__data_mosi = isinstance(data_mosi, int)
if __data_mosi:
(data_mosi, mosi_max_bytes) = (array_u08(data_mosi), data_mosi)
else:
(data_mosi, mosi_max_bytes) = isinstance(data_mosi, ArrayType) and (data_mosi, len(data_mosi)) or (data_mosi[0], min(len(data_mosi[0]), int(data_mosi[1])))
if data_mosi.typecode != 'B':
raise TypeError("type for 'data_mosi' must be array('B')")
# data_miso pre-processing
__data_miso = isinstance(data_miso, int)
if __data_miso:
(data_miso, miso_max_bytes) = (array_u08(data_miso), data_miso)
else:
(data_miso, miso_max_bytes) = isinstance(data_miso, ArrayType) and (data_miso, len(data_miso)) or (data_miso[0], min(len(data_miso[0]), int(data_miso[1])))
if data_miso.typecode != 'B':
raise TypeError("type for 'data_miso' must be array('B')")
# Call API function
(_ret_, status, time_sop, time_duration, time_dataoffset) = api.py_bg_spi_read(beagle, mosi_max_bytes, miso_max_bytes, data_mosi, data_miso)
# data_mosi post-processing
if __data_mosi: del data_mosi[max(0, min(_ret_, len(data_mosi))):]
# data_miso post-processing
if __data_miso: del data_miso[max(0, min(_ret_, len(data_miso))):]
return (_ret_, status, time_sop, time_duration, time_dataoffset, data_mosi, data_miso)
def bg_spi_read_data_timing (beagle, data_mosi, data_miso, data_timing):
"""usage: (int return, u32 status, u64 time_sop, u64 time_duration, u32 time_dataoffset, u08[] data_mosi, u08[] data_miso, u32[] data_timing) = bg_spi_read_data_timing(Beagle beagle, u08[] data_mosi, u08[] data_miso, u32[] data_timing)
All arrays can be passed into the API as an ArrayType object or as
a tuple (array, length), where array is an ArrayType object and
length is an integer. The user-specified length would then serve
as the length argument to the API funtion (please refer to the
product datasheet). If only the array is provided, the array's
intrinsic length is used as the argument to the underlying API
function.
Additionally, for arrays that are filled by the API function, an
integer can be passed in place of the array argument and the API
will automatically create an array of that length. All output
arrays, whether passed in or generated, are passed back in the
returned tuple."""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# data_mosi pre-processing
__data_mosi = isinstance(data_mosi, int)
if __data_mosi:
(data_mosi, mosi_max_bytes) = (array_u08(data_mosi), data_mosi)
else:
(data_mosi, mosi_max_bytes) = isinstance(data_mosi, ArrayType) and (data_mosi, len(data_mosi)) or (data_mosi[0], min(len(data_mosi[0]), int(data_mosi[1])))
if data_mosi.typecode != 'B':
raise TypeError("type for 'data_mosi' must be array('B')")
# data_miso pre-processing
__data_miso = isinstance(data_miso, int)
if __data_miso:
(data_miso, miso_max_bytes) = (array_u08(data_miso), data_miso)
else:
(data_miso, miso_max_bytes) = isinstance(data_miso, ArrayType) and (data_miso, len(data_miso)) or (data_miso[0], min(len(data_miso[0]), int(data_miso[1])))
if data_miso.typecode != 'B':
raise TypeError("type for 'data_miso' must be array('B')")
# data_timing pre-processing
__data_timing = isinstance(data_timing, int)
if __data_timing:
(data_timing, max_timing) = (array_u32(data_timing), data_timing)
else:
(data_timing, max_timing) = isinstance(data_timing, ArrayType) and (data_timing, len(data_timing)) or (data_timing[0], min(len(data_timing[0]), int(data_timing[1])))
if data_timing.typecode != 'I':
raise TypeError("type for 'data_timing' must be array('I')")
# Call API function
(_ret_, status, time_sop, time_duration, time_dataoffset) = api.py_bg_spi_read_data_timing(beagle, mosi_max_bytes, miso_max_bytes, max_timing, data_mosi, data_miso, data_timing)
# data_mosi post-processing
if __data_mosi: del data_mosi[max(0, min(_ret_, len(data_mosi))):]
# data_miso post-processing
if __data_miso: del data_miso[max(0, min(_ret_, len(data_miso))):]
# data_timing post-processing
if __data_timing: del data_timing[max(0, min(_ret_, len(data_timing))):]
return (_ret_, status, time_sop, time_duration, time_dataoffset, data_mosi, data_miso, data_timing)
def bg_spi_read_bit_timing (beagle, data_mosi, data_miso, bit_timing):
"""usage: (int return, u32 status, u64 time_sop, u64 time_duration, u32 time_dataoffset, u08[] data_mosi, u08[] data_miso, u32[] bit_timing) = bg_spi_read_bit_timing(Beagle beagle, u08[] data_mosi, u08[] data_miso, u32[] bit_timing)
All arrays can be passed into the API as an ArrayType object or as
a tuple (array, length), where array is an ArrayType object and
length is an integer. The user-specified length would then serve
as the length argument to the API funtion (please refer to the
product datasheet). If only the array is provided, the array's
intrinsic length is used as the argument to the underlying API
function.
Additionally, for arrays that are filled by the API function, an
integer can be passed in place of the array argument and the API
will automatically create an array of that length. All output
arrays, whether passed in or generated, are passed back in the
returned tuple."""
if not BG_LIBRARY_LOADED: return BG_INCOMPATIBLE_LIBRARY
# data_mosi pre-processing
__data_mosi = isinstance(data_mosi, int)
if __data_mosi:
(data_mosi, mosi_max_bytes) = (array_u08(data_mosi), data_mosi)
else:
(data_mosi, mosi_max_bytes) = isinstance(data_mosi, ArrayType) and (data_mosi, len(data_mosi)) or (data_mosi[0], min(len(data_mosi[0]), int(data_mosi[1])))
if data_mosi.typecode != 'B':
raise TypeError("type for 'data_mosi' must be array('B')")
# data_miso pre-processing
__data_miso = isinstance(data_miso, int)
if __data_miso:
(data_miso, miso_max_bytes) = (array_u08(data_miso), data_miso)
else:
(data_miso, miso_max_bytes) = isinstance(data_miso, ArrayType) and (data_miso, len(data_miso)) or (data_miso[0], min(len(data_miso[0]), int(data_miso[1])))
if data_miso.typecode != 'B':
raise TypeError("type for 'data_miso' must be array('B')")
# bit_timing pre-processing
__bit_timing = isinstance(bit_timing, int)
if __bit_timing:
(bit_timing, max_timing) = (array_u32(bit_timing), bit_timing)
else:
(bit_timing, max_timing) = isinstance(bit_timing, ArrayType) and (bit_timing, len(bit_timing)) or (bit_timing[0], min(len(bit_timing[0]), int(bit_timing[1])))
if bit_timing.typecode != 'I':
raise TypeError("type for 'bit_timing' must be array('I')")
# Call API function
(_ret_, status, time_sop, time_duration, time_dataoffset) = api.py_bg_spi_read_bit_timing(beagle, mosi_max_bytes, miso_max_bytes, max_timing, data_mosi, data_miso, bit_timing)
# data_mosi post-processing
if __data_mosi: del data_mosi[max(0, min(_ret_, len(data_mosi))):]
# data_miso post-processing
if __data_miso: del data_miso[max(0, min(_ret_, len(data_miso))):]
# bit_timing post-processing
if __bit_timing: del bit_timing[max(0, min(bg_bit_timing_size(BG_PROTOCOL_SPI, _ret_), len(bit_timing))):]
return (_ret_, status, time_sop, time_duration, time_dataoffset, data_mosi, data_miso, bit_timing)