-
Notifications
You must be signed in to change notification settings - Fork 0
/
pk2-la
executable file
·1651 lines (1416 loc) · 47.8 KB
/
pk2-la
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
#!/usr/bin/env python
# Logic Analyzer and I/O Probe for the Microchip PICkit2 Programmer (v1.1)
# Copyright (C) 2009, Joseph Jezak <josejx@gentoo.org>
#
# 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 2 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
### For the logic analyzer
import usb
import sys
from threading import Thread
### For the GUI
import pygtk
import pango
pygtk.require('2.0')
import gtk
### This makes threads work
gtk.gdk.threads_init()
###############################################################################
### Logic Analyzer
###############################################################################
### Device IDs
MICROCHIP_VENDOR_ID = 0x04D8
PICKIT2_PRODUCT_ID = 0x0033
### Endpoints
PK2_INT_WRITE = 0x01
PK2_INT_READ = 0x81
INT_PKT_SZ = 0x40
### Commands
PK2_SET_POS = 0xB9
PK2_SETUP = 0xB8
PK2_READ = 0xAC
PK2_PADDING = 0xAD
PK2_RUN_SCRIPT = 0xA6
PK2_CHECK_VOLTAGE = 0xA3
PK2_CHECK_STATUS = 0xA2
PK2_SET_VPP = 0xA1 # Logic Input Level
PK2_SET_VDD = 0xA0 # Supply Level
### Scripting Commands (from pk2cmd source, only the used ones are here)
PK2_SCR_VPP_ON = 0xFB
PK2_SCR_VPP_OFF = 0xFA
PK2_SCR_VPP_PWM_ON = 0xF9
PK2_SCR_MCLR_GND_ON = 0xF7
PK2_SCR_MCLR_GND_OFF = 0xF6
PK2_SCR_SET_ICSP_PINS = 0xF3
PK2_SCR_DELAY_LONG = 0xE8
PK2_SCR_ICSP_STATE_BUF = 0xDC
PK2_SCR_SET_AUX = 0xCF
PK2_SCR_AUX_BUF = 0xCE
### Pin Constants for I/O
PK2_IO_ICSP_1_IN = 0x02
PK2_IO_ICSP_1_SET = 0x08
PK2_IO_ICSP_2_IN = 0x01
PK2_IO_ICSP_2_SET = 0x04
PK2_IO_AUX_1_IN = 0x01
PK2_IO_AUX_1_SET = 0x02
### Banks
PK2_BANK_0 = 0x06
PK2_BANK_1 = 0x07
### Samples taken
PK2_SAMPLES = 1024
### Cursors
PK2_CURSOR1 = 1
PK2_CURSOR2 = 2
PK2_CURSOR_BOTH = 3
class pk2_la:
### Initializes the pickit2 device
def __init__(self, unit, gui_ref):
self.gui_ref = gui_ref
### Find the device
device = self.find_device(unit)
### Connect to the device
self.devhandle = device.open()
dev = self.devhandle
if dev == 0:
sys.stderr.write("Unable to open the PicKit2 USB device!\n")
sys.exit(-1)
### Claim the interface
try:
dev.claimInterface(0)
except:
### Try to detach any other devices using the USB interface
try:
dev.detachKernelDriver(0)
except:
sys.stderr.write("Unable to detach existing driver from the PicKit2 USB device!\n")
sys.exit(-1)
### Now that it's detached, try to claim again
try:
dev.claimInterface(0)
except:
sys.stderr.write("Unable to claim the PicKit2 USB interface!\n")
sys.exit(-1)
###############################################################
### Logic Analyzer default values
###############################################################
### Trigger Enable
self.trig_en = 0
### Trigger on 1/0
self.trig_1 = 0
### Trigger on edge (1/0 says which edge)
self.trig_edge = 0
### Trigger Repetition
self.trig_repetition = 1
### Trigger Window
self.trig_win = "Start"
### Sample Rate
self.set_rate("1MHz")
### Returned channel data for the last run
self.channel_data = [[], [], []]
### Microseconds per tick of the analyzer
self.us_per_tick = 1
###############################################################
### I/O Probe default values
###############################################################
### I/O State
self.chan_io = [ "Output", "Input", "Input", "Input" ]
self.chan_val = [ 0, 0, 0, 0 ]
self.io_running = False
self.io_thread = None
self.last_chan_val = [ -1, -1, -1, -1]
self.last_chan_io = self.chan_io
###############################################################
### Functions for both components
###############################################################
### Unit is the device matched that we want to use
def find_device(self, unit):
devices = []
### Find the PicKit devices plugged in right now
for bus in usb.busses() :
for device in bus.devices :
if device.idVendor == MICROCHIP_VENDOR_ID and device.idProduct == PICKIT2_PRODUCT_ID:
devices.append(device)
### Make sure we have at least one!
if len(devices) < 1:
sys.stderr.write("No PicKit2 USB device found!\n")
sys.exit(-1)
else:
return devices[unit]
### Get the VDD and VPP levels
def check_voltages(self):
done = False
try:
### Send the command
msg = chr(PK2_CHECK_VOLTAGE) + chr(PK2_PADDING) * 63
self.devhandle.interruptWrite(PK2_INT_WRITE, msg)
except:
pass
while not done:
try:
ret = self.devhandle.interruptRead(PK2_INT_READ, INT_PKT_SZ)
### We ignore failures here because we're just polling for
### the return from the device
except:
pass
else:
done = True
### This equation is from the pk2cmd program from microchip
vdd = (float(ret[1] << 8 | ret[0]) / 0x10000) * 5.0
vpp = (float(ret[3] << 8 | ret[2]) / 0x10000) * 13.7
return vdd, vpp
### Set the vpp voltage level
### This function is based on one from pk2cmd
def set_vpp(self, voltage, threshold):
### Message Header
msg = chr(PK2_SET_VPP) + chr(0x40)
### Find ADC threshold
adc = int(float(voltage) * 18.61)
### Find Fault threshold
fault = int(float(voltage * threshold) * 18.61)
### Send the msg to the device
try:
msg = msg + chr(adc) + chr(fault) + chr(PK2_PADDING) * 60
self.devhandle.interruptWrite(PK2_INT_WRITE, msg)
except:
pass
### Set the vdd voltage level
### This function is based on one from pk2cmd
def set_vdd(self, voltage, threshold):
### Voltage must be >= 2.5, setting too low prevents vpp pump from working
if voltage < 2.5:
voltage = 2.5
### Find the capture and compare value
ccp = int(float(voltage * 32) + 10.5) << 6
### Find the fault value
fault = int((float(threshold * voltage) / 5) * 255)
### ~4.12v maximum due to diode droop
if fault > 210:
fault = 210
### Message Header
msg = chr(PK2_SET_VDD)
### Send the msg to the device
try:
msg = msg + chr(ccp & 0xFF) + chr((ccp >> 8) & 0xFF) + chr(fault & 0xFF) + chr(PK2_PADDING) * 60
self.devhandle.interruptWrite(PK2_INT_WRITE, msg)
except:
pass
### Checks the status of the device, a list of the possible responses
### can be found in the IO Probe text file
def check_status(self):
### Check the device status
msg = chr(PK2_CHECK_STATUS) + chr(PK2_PADDING) * 63
try:
### Send status msg
self.devhandle.interruptWrite(PK2_INT_WRITE, msg)
except:
pass
### Get status back
done = False
while not done:
try:
ret = self.devhandle.interruptRead(PK2_INT_READ, INT_PKT_SZ)
### We ignore failures here because we're just polling for
### the return from the device
except:
pass
else:
done = True
return (ret[1] << 8) | ret[0]
#######################################################################
### I/O Probe Components
#######################################################################
### Generate the byte used to set the ICSP Pins
def gen_icsp_byte(self):
icsp_pins = 0
### Channel 1
if self.chan_io[1] == "Input":
icsp_pins = icsp_pins | PK2_IO_ICSP_1_IN
elif self.chan_val[1] == 1:
icsp_pins = icsp_pins | PK2_IO_ICSP_1_SET
### Channel 2
if self.chan_io[2] == "Input":
icsp_pins = icsp_pins | PK2_IO_ICSP_2_IN
elif self.chan_val[2] == 1:
icsp_pins = icsp_pins | PK2_IO_ICSP_2_SET
return icsp_pins
### Generate the byte used to set the AUX Pins
def gen_aux_byte(self):
aux_pins = 0
### Aux pin
if self.chan_io[3] == "Input":
aux_pins = aux_pins | PK2_IO_AUX_1_IN
elif self.chan_val[3] == 1:
aux_pins = aux_pins | PK2_IO_AUX_1_SET
return aux_pins
### Setup the I/O Module
def io_setup(self):
msg = chr(PK2_RUN_SCRIPT)
### 9 Commands in this script
msg = msg + chr(0x09)
### Set Ch. 0 to 0/1
if self.chan_val[0] == 0:
msg = msg + chr(PK2_SCR_VPP_OFF)
msg = msg + chr(PK2_SCR_MCLR_GND_ON)
else:
msg = msg + chr(PK2_SCR_VPP_ON)
msg = msg + chr(PK2_SCR_MCLR_GND_OFF)
### Turn on VPP PWM
msg = msg + chr(PK2_SCR_VPP_PWM_ON)
### Set ICSP Pins
msg = msg + chr(PK2_SCR_SET_ICSP_PINS) + chr(self.gen_icsp_byte())
### Set AUX Pins
msg = msg + chr(PK2_SCR_SET_AUX) + chr(self.gen_aux_byte())
### Delay
msg = msg + chr(PK2_SCR_DELAY_LONG) + chr(0x14)
### Padding
msg = msg + chr(PK2_PADDING) * 53
### Send the msg to the device
try:
self.devhandle.interruptWrite(PK2_INT_WRITE, msg)
except:
pass
### Check device status
### FIXME What are we checking here?
self.check_status()
### Sets the pins direction and value
def io_setup_pins(self):
msg = chr(PK2_RUN_SCRIPT)
### 6 Commands in this script
msg = msg + chr (0x06)
### Set Ch. 0 to 0/1
if self.chan_val[0] == 0:
msg = msg + chr(PK2_SCR_VPP_OFF)
msg = msg + chr(PK2_SCR_MCLR_GND_ON)
else:
msg = msg + chr(PK2_SCR_VPP_ON)
msg = msg + chr(PK2_SCR_MCLR_GND_OFF)
### Set ICSP Pins
msg = msg + chr(PK2_SCR_SET_ICSP_PINS) + chr(self.gen_icsp_byte())
### Set AUX Pins
msg = msg + chr(PK2_SCR_SET_AUX) + chr(self.gen_aux_byte())
### Padding
msg = msg + chr(PK2_PADDING) * 56
### Send the msg to the device
try:
self.devhandle.interruptWrite(PK2_INT_WRITE, msg)
except:
pass
### Save the local copy of the pin value and io state
self.last_chan_val = self.chan_val[:]
self.last_chan_io = self.chan_io[:]
### Check device status
### FIXME What are we checking here?
self.check_status()
### Requests an I/O report
def io_request_pins(self):
### Send the data request
msg = chr(PK2_RUN_SCRIPT)
### 2 Instructions
msg = msg + chr(0x02)
### Request the state buffer?
msg = msg + chr(PK2_SCR_ICSP_STATE_BUF)
### Request the aux state buffer?
msg = msg + chr(PK2_SCR_AUX_BUF)
### Unknown
msg = msg + chr(0xAA)
### Padding
msg = msg + chr(PK2_PADDING) * 59
### Send the msg to the device
try:
self.devhandle.interruptWrite(PK2_INT_WRITE, msg)
except:
pass
done = False
### Get response
while not done:
try:
ret = self.devhandle.interruptRead(PK2_INT_READ, INT_PKT_SZ)
### We ignore failures here because we're just polling for
### the return from the device
except:
pass
else:
done = True
return ret
### Run the I/O Module
def run_io(self):
### Check to make sure we have a voltage on the device
vdd, vpp = self.check_voltages()
### If the input voltage is < 2.5v, it won't work
if vdd < 2.5:
err = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, "You must connect VDD (pin 2) to a source between 2.5v and 5.0v")
err.set_title("Check Source Voltage")
err.run()
err.destroy()
return False
### These values were determined by doing the reverse of the
### equations in these functions using the captured data
### Set VPP
self.set_vpp(vdd, 0.7)
### Set VDD
self.set_vdd(vdd, 0.85)
### Setup the IO Module
self.io_setup()
### Start the io thread running
self.io_running = True
### Start the thread
self.io_thread = io_thread(self)
self.io_thread.start()
return True
#######################################################################
### Logic Analyzer Components
#######################################################################
### Wait for a trigger and retrieve data
def la_acquire(self):
### Get the data
pk2_data = []
if self.la_setup_trigger():
### Read from the banks
pk2_data.append(self.read_bank(PK2_BANK_0, 0x00))
pk2_data.append(self.read_bank(PK2_BANK_0, 0x80))
pk2_data.append(self.read_bank(PK2_BANK_1, 0x00))
pk2_data.append(self.read_bank(PK2_BANK_1, 0x80))
### Convert the input to channel data
self.channel_data = self.la_raw_to_data(pk2_data)
return True
else:
return False
### Takes the raw data banks and converts to a tuple of bits for each channel
def la_raw_to_data(self, bank_data):
channel_data = [[], [], []]
### Loop over the banks
for i in range(0, 4):
for j in bank_data[i]:
for k in j:
### Channel 1
if k & (1 << 6):
channel_data[0].append("1")
else:
channel_data[0].append("0")
if k & (1 << 2):
channel_data[0].append("1")
else:
channel_data[0].append("0")
### Channel 2
if k & (1 << 7):
channel_data[1].append("1")
else:
channel_data[1].append("0")
if k & (1 << 3):
channel_data[1].append("1")
else:
channel_data[1].append("0")
### Channel 3
if k & (1 << 4):
channel_data[2].append("1")
else:
channel_data[2].append("0")
if k & (1 << 0):
channel_data[2].append("1")
else:
channel_data[2].append("0")
return channel_data
### Write a setup pkt to the device
def la_setup_trigger(self):
### Quick check, make sure things are sane
if not self.la_setup_valid():
print "The setup is not valid - please set a trigger"
return False
### Create the setup packet to send to the device
msg = self.la_setup_pkt()
### FIXME better error checking
try:
### Send the setup packet
self.devhandle.interruptWrite(PK2_INT_WRITE, msg)
except:
pass
### Wait for the data or cancelation report
done = False
ret = 0
while not done:
try:
ret = self.devhandle.interruptRead(PK2_INT_READ, INT_PKT_SZ)
### We ignore failures here because we're just polling for
### the return from when the trigger occurs
except:
pass
else:
done = True
### User pressed cancel
if ret[0] == 0x04 and ret[1] == 0x40:
print "The user pressed cancel"
return False
print "Trigger Hit: Aquiring Data"
return True
### Read bank from trigger data
def read_bank(self, bank, offset):
bank_data = []
try:
### Set the position
msg = chr(PK2_SET_POS) + chr(offset) + chr(bank) + chr(PK2_PADDING) * 61
self.devhandle.interruptWrite(PK2_INT_WRITE, msg)
### We read 0x40 bytes out at a time, the bank is 0x80 long
### I'm not sure if this is just a peculiarity of the software, or a real
### limitation of the device, but this works for now
for i in range(0, 2):
### Now request the next bank read
msg = chr(PK2_READ) + chr(PK2_PADDING) * 63
self.devhandle.interruptWrite(PK2_INT_WRITE, msg)
### Okay, now get the data
done = False
while not done:
resp = "No Data"
try:
resp = self.devhandle.interruptRead(PK2_INT_READ, INT_PKT_SZ)
### We ignore failures here because we're just polling for
### the return from when the trigger occurs
except:
pass
else:
done = True
bank_data.append(resp)
except:
pass
raise
return bank_data
### Make sure that the settings are valid for an acquisition
def la_setup_valid(self):
### Make sure that at least one channel is enabled
if self.trig_en == 0:
return False
return True
### Make a setup packet to send to the device
### Settings are taken from the values stored in this object
def la_setup_pkt(self):
### Packet Header
msg = chr(PK2_SETUP) + chr(1)
### Enable bits
msg = msg + chr(self.trig_en)
### Trigger on 1/0
msg = msg + chr(self.trig_1)
### Trigger on edge (1/0 previously set which edge)
msg = msg + chr(self.trig_edge)
### Sample Repetition
msg = msg + chr(self.trig_repetition)
### Trigger Window
if self.trig_win == "Center":
msg = msg + chr(0x0A) + chr(0x00)
elif self.trig_win == "End":
msg = msg + chr(0x48) + chr(0x02)
elif self.trig_win == "Start":
msg = msg + chr(0xCC) + chr(0x3)
elif self.trig_win == "1 Window":
msg = msg + chr(0xB4) + chr(0x07)
elif self.trig_win == "2 Window":
msg = msg + chr(0x9C) + chr(0x0B)
elif self.trig_win == "3 Window":
msg = msg + chr(0x84) + chr(0x0F)
### Sample Rate
msg = msg + chr(self.rate)
### Padding to 64 bytes
msg = msg + chr(PK2_PADDING) * 55
return msg
### Set the internal rate to the constants listed here
def set_rate(self, rate):
if rate == "1MHz":
self.rate = 0x00
self.us_per_tick = 1
elif rate == "500kHz":
self.rate = 0x01
self.us_per_tick = 2
elif rate == "250kHz":
self.rate = 0x03
self.us_per_tick = 4
elif rate == "100kHz":
self.rate = 0x09
self.us_per_tick = 10
elif rate == "50kHz":
self.rate = 0x13
self.us_per_tick = 20
elif rate == "25kHz":
self.rate = 0x27
self.us_per_tick = 40
elif rate == "10kHz":
self.rate = 0x63
self.us_per_tick = 100
elif rate == "5kHz":
self.rate = 0xC7
self.us_per_tick = 200
else:
sys.stder.write("Unsupported rate: " + rate)
sys.exit(-1)
return 0
### Set the trigger window position
def set_trig_win(self, trig_win):
self.trig_win = trig_win
### Set the # of repetitions
def set_sample_repetition(self, reps):
self.trig_repetition = reps
### Set the trigger type
def set_trig_type(self, trigger, channel):
trig_const = 1 << (channel + 1)
### Don't care
if trigger == "*":
self.trig_en = self.trig_en & ~(trig_const)
self.trig_1 = self.trig_1 & ~(trig_const)
self.trig_edge = self.trig_edge & ~(trig_const)
### 1
elif trigger == "1":
self.trig_en = self.trig_en | trig_const
self.trig_1 = self.trig_1 | trig_const
self.trig_edge = self.trig_edge & ~(trig_const)
### 0
elif trigger == "0":
self.trig_en = self.trig_en | trig_const
self.trig_1 = self.trig_1 & ~(trig_const)
self.trig_edge = self.trig_edge & ~(trig_const)
### Rising Edge
elif trigger == "/":
self.trig_en = self.trig_en | trig_const
self.trig_1 = self.trig_1 | trig_const
self.trig_edge = self.trig_edge | trig_const
### Falling Edge
elif trigger == "\\":
self.trig_en = self.trig_en | trig_const
self.trig_1 = self.trig_1 & ~(trig_const)
self.trig_edge = self.trig_edge | trig_const
### Returns the selected channel data
def get_la_data(self, chan):
return self.channel_data[chan - 1]
### Writes data to a file
def write_la_data(self, filename):
### Open the file
df = open(filename, "w")
### Write out the data
for i in range(1,4):
df.write("Channel: " + `i` + "\n")
for bit in self.channel_data[i - 1]:
df.write(bit)
df.write("\n\n")
### Close the file
df.close()
def __del__(self):
try:
self.__devhandle.releaseInterface(self.__intf)
del self.__devhandle
except:
pass
###############################################################################
### Thread for running the I/O Module
###############################################################################
class io_thread(Thread):
def __init__(self, pk2):
Thread.__init__(self)
self.pk2 = pk2
### Runs the loop that sets and releases the pins
def run(self):
while self.pk2.io_running:
### If we need to update the pins, do it now
if not (self.pk2.last_chan_val == self.pk2.chan_val) or not (self.pk2.last_chan_io == self.pk2.chan_io):
### Update the buttons as well
gtk.gdk.threads_enter()
self.pk2.gui_ref.ioFrame.update_btns()
gtk.gdk.threads_leave()
self.pk2.io_setup_pins()
### Request the next report
ret = self.pk2.io_request_pins()
#print ret[0], ret[1], ret[2], ret[3]
if self.pk2.chan_io[1] == "Input":
self.pk2.chan_val[1] = ret[1] & 0x02
if self.pk2.chan_io[2] == "Input":
self.pk2.chan_val[2] = ret[1] & 0x01
if self.pk2.chan_io[3] == "Input":
self.pk2.chan_val[3] = ret[2] & 0x01
### Check device status
### FIXME What are we checking here?
self.pk2.check_status()
###############################################################################
### GUI
###############################################################################
class pk2_triggerFrame:
def __init__(self, parent, pk2):
self.parent = parent
### Logic Analyzer Reference
self.pk2 = pk2
self.frame = gtk.Frame("Triggers")
box = gtk.VBox(False, 0)
### Trigger 1
box1 = gtk.HBox(False, 0)
chan1_label = gtk.Label("Ch 1:")
box1.pack_start(chan1_label, True, False, 0)
self.chan1 = gtk.combo_box_new_text()
self.chan1.append_text("* - Don't Care")
self.chan1.append_text("1 - Logic High")
self.chan1.append_text("0 - Logic Low")
self.chan1.append_text("/ - Rising Edge")
self.chan1.append_text("\ - Falling Edge")
self.chan1.set_active(0)
self.chan1.connect('changed', self.update_trigger, 1)
box1.pack_start(self.chan1, True, False, 0)
box.pack_start(box1, True, False, 0)
### Trigger 2
box2 = gtk.HBox(False, 0)
chan2_label = gtk.Label("Ch 2:")
box2.pack_start(chan2_label, True, False, 0)
self.chan2 = gtk.combo_box_new_text()
self.chan2.append_text("* - Don't Care")
self.chan2.append_text("1 - Logic High")
self.chan2.append_text("0 - Logic Low")
self.chan2.append_text("/ - Rising Edge")
self.chan2.append_text("\ - Falling Edge")
self.chan2.set_active(0)
self.chan2.connect('changed', self.update_trigger, 2)
box2.pack_start(self.chan2, True, False, 0)
box.pack_start(box2, True, False, 0)
### Trigger 3
box3 = gtk.HBox(False, 0)
chan3_label = gtk.Label("Ch 3:")
box3.pack_start(chan3_label, True, False, 0)
self.chan3 = gtk.combo_box_new_text()
self.chan3.append_text("* - Don't Care")
self.chan3.append_text("1 - Logic High")
self.chan3.append_text("0 - Logic Low")
self.chan3.append_text("/ - Rising Edge")
self.chan3.append_text("\ - Falling Edge")
self.chan3.set_active(0)
self.chan3.connect('changed', self.update_trigger, 3)
box3.pack_start(self.chan3, True, False, 0)
box.pack_start(box3, True, False, 0)
### Number of occurrences
box4 = gtk.HBox(False, 0)
occur_label = gtk.Label("# of Times before Trigger: ")
box4.pack_start(occur_label, True, False, 0)
self.occur_adj = gtk.Adjustment(1, 1, 255, 1, 0, 0)
self.occur = gtk.SpinButton(self.occur_adj, 0.0, 0)
self.occur.set_numeric(True)
self.occur.connect('changed', self.update_repetition)
box4.pack_start(self.occur, True, False, 0)
box.pack_start(box4, True, False, 0)
### Display the frame
self.frame.add(box)
def get(self):
return self.frame
def update_repetition(self, adjustment):
self.pk2.set_sample_repetition(adjustment.get_value())
def update_trigger(self, combobox, channel):
pk2 = self.pk2
index = combobox.get_active()
if index == 0:
pk2.set_trig_type("*", channel)
if index == 1:
pk2.set_trig_type("1", channel)
if index == 2:
pk2.set_trig_type("0", channel)
if index == 3:
pk2.set_trig_type("/", channel)
if index == 4:
pk2.set_trig_type("\\", channel)
class pk2_rxFrame:
def __init__(self, parent, pk2):
self.parent = parent
### Logic Analyzer Reference
self.pk2 = pk2
self.frame = gtk.Frame("Acquisition")
box = gtk.VBox(False, 0)
### Bold
bold = pango.AttrList()
bold.insert(pango.AttrWeight(pango.WEIGHT_HEAVY, 0, -1))
### Italic
italic = pango.AttrList()
italic.insert(pango.AttrStyle(pango.STYLE_ITALIC, 0, -1))
### Sample Rate
sample_box = gtk.HBox(False, 0)
rate_label = gtk.Label("Sample Rate:")
rate_label.set_property("attributes", bold)
sample_box.pack_start(rate_label, True, False, 0)
### Sample Rate drop down
self.rate = gtk.combo_box_new_text()
self.rate.append_text("1MHz - 1 ms Window")
self.rate.append_text("500kHz - 2ms Window")
self.rate.append_text("250kHz - 4ms Window")
self.rate.append_text("100kHz - 10ms Window")
self.rate.append_text("50kHz - 20ms Window")
self.rate.append_text("25kHz - 40ms Window")
self.rate.append_text("10kHz - 100ms Window")
self.rate.append_text("5kHz - 200ms Window")
self.rate.set_active(0)
sample_box.pack_start(self.rate, True, False, 0)
box.pack_start(sample_box, True, False, 0)
### Add a callback so the sample rate can change the aliasing warning
self.rate.connect('changed', self.update_rate)
### Add a label for the Aliasing warning
self.note_label = gtk.Label("Signals > 500kHz will alias")
self.note_label.set_property("attributes", italic)
box.pack_start(self.note_label, True, False, 0)
### Trigger Position
trigger_box = gtk.HBox(False, 0)
trig_label = gtk.Label("Trigger Position:")
trig_label.set_property("attributes", bold)
trigger_box.pack_start(trig_label, True, False, 0)
### Trigger Position drop down
self.trig_pos = gtk.combo_box_new_text()
self.trig_pos.append_text("Start of Data")
self.trig_pos.append_text("Center of Data")
self.trig_pos.append_text("End of Data")
self.trig_pos.append_text("Delay 1 Window")
self.trig_pos.append_text("Delay 2 Windows")
self.trig_pos.append_text("Delay 3 Windows")
self.trig_pos.set_active(0)
trigger_box.pack_start(self.trig_pos, True, False, 0)
self.trig_pos.connect('changed', self.update_trig_pos)
box.pack_start(trigger_box, True, False, 0)
### Display the frame
self.frame.add(box)
def update_trig_pos(self, combobox):
pk2 = self.pk2
index = self.trig_pos.get_active()
if index == 0:
pk2.set_trig_win("Start")
elif index == 1:
pk2.set_trig_win("Center")
elif index == 2:
pk2.set_trig_win("End")
elif index == 3:
pk2.set_trig_win("1 Window")
elif index == 4:
pk2.set_trig_win("2 Window")
elif index == 5:
pk2.set_trig_win("3 Window")
def update_rate(self, combobox):
pk2 = self.pk2
### Figure out which one was picked
index = self.rate.get_active()
if index == 0:
self.note_label.set_text("Signals > 500kHz will alias")
pk2.set_rate("1MHz");
elif index == 1:
self.note_label.set_text("Signals > 250kHz will alias")
pk2.set_rate("500kHz");
elif index == 2:
self.note_label.set_text("Signals > 125kHz will alias")
pk2.set_rate("250kHz");
elif index == 3:
self.note_label.set_text("Signals > 50kHz will alias")
pk2.set_rate("100kHz");
elif index == 4:
self.note_label.set_text("Signals > 25kHz will alias")
pk2.set_rate("50kHz");
elif index == 5:
self.note_label.set_text("Signals > 12.5kHz will alias")
pk2.set_rate("25kHz");
elif index == 6:
self.note_label.set_text("Signals > 5kHz will alias")
pk2.set_rate("10kHz");
elif index == 7:
self.note_label.set_text("Signals > 2.5kHz will alias")
pk2.set_rate("5kHz");
### Set the value per tick message
self.parent.la_display.la_ticks_label.set_text(`pk2.us_per_tick` + "us/tick")
### Disable cursors on all of the boxes
self.parent.la_display.clear_cursors()
### Force a redraw
self.parent.window.queue_draw()
def get(self):
return self.frame
class pk2_la_display:
def __init__(self, parent, pk2):
self.parent = parent
### Logic Analyzer Reference
self.pk2 = pk2
### Italic
italic = pango.AttrList()
italic.insert(pango.AttrStyle(pango.STYLE_ITALIC, 0, -1))
### Display window frame
frame = gtk.VBox(False, 0)
self.frame = frame
### Add the information box
infobox = gtk.HBox(False, 0)
frame.pack_start(infobox, True, False, 0)
### Add the legend text
self.la_ticks_label = gtk.Label(`pk2.us_per_tick` + "us/tick")
self.la_ticks_label.set_property("attributes", italic)
infobox.pack_start(self.la_ticks_label, True, False, 0)
### Add Cursor 1
self.la_cur1_label = gtk.Label("Cursor 1: None")
s = self.la_cur1_label.get_style().copy()
cmap = self.la_cur1_label.get_colormap()
s.fg[gtk.STATE_NORMAL] = cmap.alloc_color("red")
self.la_cur1_label.set_style(s)
infobox.pack_start(self.la_cur1_label, True, False, 0)
### Add Cursor 2
self.la_cur2_label = gtk.Label("Cursor 2: None")
s = self.la_cur2_label.get_style().copy()
cmap = self.la_cur2_label.get_colormap()
s.fg[gtk.STATE_NORMAL] = cmap.alloc_color("blue")
self.la_cur2_label.set_style(s)
infobox.pack_start(self.la_cur2_label, True, False, 0)
### Add the difference between cursor 1 and 2
self.la_cur_diff_label = gtk.Label("Distance: N/A")
infobox.pack_start(self.la_cur_diff_label, True, False, 0)
### Display window for the channel displays
dispbox = gtk.HBox(False, 0)
chlbl_box = gtk.VBox(False, 0)
dispbox.pack_start(chlbl_box, False, False, 0)
frame.pack_start(dispbox, True, False, 0)
### Channel Labels
ch1_lbl = gtk.Label("Ch 1: ")
chlbl_box.pack_start(ch1_lbl, True, False, 0)
ch2_lbl = gtk.Label("Ch 2: ")
chlbl_box.pack_start(ch2_lbl, True, False, 0)
ch3_lbl = gtk.Label("Ch 3: ")
chlbl_box.pack_start(ch3_lbl, True, False, 0)