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