]> sigrok.org Git - libsigrokdecode.git/blob - decoders/gpib/pd.py
license: remove FSF postal address from boiler plate license text
[libsigrokdecode.git] / decoders / gpib / pd.py
1 ##
2 ## This file is part of the libsigrokdecode project.
3 ##
4 ## Copyright (C) 2016 Rudolf Reuter <reuterru@arcor.de>
5 ##
6 ## This program is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 2 of the License, or
9 ## (at your option) any later version.
10 ##
11 ## This program is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ## GNU General Public License for more details.
15 ##
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
18 ##
19
20 import sigrokdecode as srd
21
22 class Decoder(srd.Decoder):
23     api_version = 2
24     id = 'gpib'
25     name = 'GPIB'
26     longname = 'General Purpose Interface Bus'
27     desc = 'IEEE-488 GPIB / HPIB protocol.'
28     license = 'gplv2+'
29     inputs = ['logic']
30     outputs = ['gpib']
31     channels = (
32         {'id': 'dio1' , 'name': 'DIO1', 'desc': 'Data I/O bit 1'},
33         {'id': 'dio2' , 'name': 'DIO2', 'desc': 'Data I/O bit 2'},
34         {'id': 'dio3' , 'name': 'DIO3', 'desc': 'Data I/O bit 3'},
35         {'id': 'dio4' , 'name': 'DIO4', 'desc': 'Data I/O bit 4'},
36         {'id': 'dio5' , 'name': 'DIO5', 'desc': 'Data I/O bit 5'},
37         {'id': 'dio6' , 'name': 'DIO6', 'desc': 'Data I/O bit 6'},
38         {'id': 'dio7' , 'name': 'DIO7', 'desc': 'Data I/O bit 7'},
39         {'id': 'dio8' , 'name': 'DIO8', 'desc': 'Data I/O bit 8'},
40         {'id': 'eoi', 'name': 'EOI', 'desc': 'End or identify'},
41         {'id': 'dav', 'name': 'DAV', 'desc': 'Data valid'},
42         {'id': 'nrfd', 'name': 'NRFD', 'desc': 'Not ready for data'},
43         {'id': 'ndac', 'name': 'NDAC', 'desc': 'Not data accepted'},
44         {'id': 'ifc', 'name': 'IFC', 'desc': 'Interface clear'},
45         {'id': 'srq', 'name': 'SRQ', 'desc': 'Service request'},
46         {'id': 'atn', 'name': 'ATN', 'desc': 'Attention'},
47         {'id': 'ren', 'name': 'REN', 'desc': 'Remote enable'},
48     )
49     options = (
50         {'id': 'sample_total', 'desc': 'Total number of samples', 'default': 0},
51     )
52     annotations = (
53         ('items', 'Items'),
54         ('gpib', 'DAT/CMD'),
55         ('eoi', 'EOI'),
56     )
57     annotation_rows = (
58         ('bytes', 'Bytes', (0,)),
59         ('gpib', 'DAT/CMD', (1,)),
60         ('eoi', 'EOI', (2,)),
61     )
62
63     def __init__(self):
64         self.olddav = None
65         self.items = []
66         self.itemcount = 0
67         self.saved_item = None
68         self.saved_ATN = False
69         self.saved_EOI = False
70         self.samplenum = 0
71         self.oldpins = None
72         self.ss_item = self.es_item = None
73         self.first = True
74
75     def start(self):
76         self.out_ann = self.register(srd.OUTPUT_ANN)
77
78     def putb(self, data):
79         self.put(self.ss_item, self.es_item, self.out_ann, data)
80
81     def handle_bits(self, datapins):
82         dbyte = 0x20
83         dATN = False
84         item2 = False
85         dEOI = False
86         item3 = False
87         # If this is the first item in a word, save its sample number.
88         if self.itemcount == 0:
89             self.ss_word = self.samplenum
90
91         # Get the bits for this item.
92         item = 0
93         for i in range(8):
94             item |= datapins[i] << i
95
96         item = item ^ 0xff # Invert data byte.
97         self.items.append(item)
98         self.itemcount += 1
99
100         if datapins[14] == 0:
101             item2 = True
102         if datapins[8] == 0:
103             item3 = True
104
105         if self.first:
106             # Save the start sample and item for later (no output yet).
107             self.ss_item = self.samplenum
108             self.first = False
109             self.saved_item = item
110             self.saved_ATN = item2
111             self.saved_EOI = item3
112         else:
113             # Output the saved item.
114             dbyte = self.saved_item
115             dATN = self.saved_ATN
116             dEOI = self.saved_EOI
117             self.es_item = self.samplenum
118             self.putb([0, ['%02X' % self.saved_item]])
119
120             # Encode item byte to GPIB convention.
121             self.strgpib = ' '
122             if dATN: # ATN, decode commands.
123                 if dbyte == 0x01: self.strgpib = 'GTL'
124                 if dbyte == 0x04: self.strgpib = 'SDC'
125                 if dbyte == 0x05: self.strgpib = 'PPC'
126                 if dbyte == 0x08: self.strgpib = 'GET'
127                 if dbyte == 0x09: self.strgpib = 'TCT'
128                 if dbyte == 0x11: self.strgpib = 'LLO'
129                 if dbyte == 0x14: self.strgpib = 'DCL'
130                 if dbyte == 0x15: self.strgpib = 'PPU'
131                 if dbyte == 0x18: self.strgpib = 'SPE'
132                 if dbyte == 0x19: self.strgpib = 'SPD'
133                 if dbyte == 0x3f: self.strgpib = 'UNL'
134                 if dbyte == 0x5f: self.strgpib = 'UNT'
135                 if dbyte > 0x1f and dbyte < 0x3f: # Address Listener.
136                     self.strgpib = 'L' + chr(dbyte + 0x10)
137                 if dbyte > 0x3f and dbyte < 0x5f: # Address Talker
138                     self.strgpib = 'T' + chr(dbyte - 0x10)
139             else:
140                 if dbyte > 0x1f and dbyte < 0x7f:
141                     self.strgpib = chr(dbyte)
142                 if dbyte == 0x0a:
143                     self.strgpib = 'LF'
144                 if dbyte == 0x0d:
145                     self.strgpib = 'CR'
146
147             self.putb([1, [self.strgpib]])
148             self.strEOI = ' '
149             if dEOI:
150                 self.strEOI = 'EOI'
151             self.putb([2, [self.strEOI]])
152
153             self.ss_item = self.samplenum
154             self.saved_item = item
155             self.saved_ATN = item2
156             self.saved_EOI = item3
157
158         if self.itemcount < 16:
159             return
160
161         self.itemcount, self.items = 0, []
162
163     def find_falling_dav_edge(self, dav, datapins):
164         # Ignore sample if the DAV pin hasn't changed.
165         if dav == self.olddav:
166             return
167         self.olddav = dav
168         # Sample on falling DAV edge.
169         if dav == 1:
170             return
171
172         # Found the correct DAV edge, now get the bits.
173         self.handle_bits(datapins)
174
175     def decode(self, ss, es, data):
176         lsn = self.options['sample_total']
177
178         for (self.samplenum, pins) in data:
179             if lsn > 0:
180                 if (lsn - self.samplenum) == 1: # Show the last data word.
181                     self.handle_bits(pins)
182
183             # Ignore identical samples early on (for performance reasons).
184             if self.oldpins == pins:
185                 continue
186             self.oldpins = pins
187
188             self.find_falling_dav_edge(pins[9], pins)