X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=decoders%2Fusb_packet%2Fpd.py;h=e262074e0ba7d4bbd6a0fb57d022f22bdfc6159b;hb=6a419ada57b7e219b9aca56253be664cca51865d;hp=61acd4f68154e1f8004f351d0044f52034be21e8;hpb=da9bcbd9f45b0153465c55ec726a0d76f6d7f01e;p=libsigrokdecode.git
diff --git a/decoders/usb_packet/pd.py b/decoders/usb_packet/pd.py
index 61acd4f..e262074 100644
--- a/decoders/usb_packet/pd.py
+++ b/decoders/usb_packet/pd.py
@@ -15,8 +15,7 @@
## 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 St, Fifth Floor, Boston, MA 02110-1301 USA
+## along with this program; if not, see .
##
import sigrokdecode as srd
@@ -33,7 +32,9 @@ Packet:
- 'ADDR',
- 'EP',
- 'CRC5',
+ - 'CRC5 ERROR',
- 'CRC16',
+ - 'CRC16 ERROR',
- 'EOP',
- 'FRAMENUM',
- 'DATABYTE',
@@ -111,7 +112,7 @@ pids = {
# Special
'00111100': ['PRE', 'Host-issued preamble; enables downstream bus traffic to low-speed devices'],
- '00111100': ['ERR', 'Split transaction error handshake'],
+ #'00111100': ['ERR', 'Split transaction error handshake'],
'00011110': ['SPLIT', 'HS split transaction token'],
'00101101': ['PING', 'HS flow control probe for a bulk/control EP'],
'00001111': ['Reserved', 'Reserved PID'],
@@ -142,8 +143,37 @@ def bitstr_to_num(bitstr):
l.reverse()
return int(''.join(l), 2)
+def reverse_number(num, count):
+ out = list(count * '0')
+ for i in range(0, count):
+ if num >> i & 1:
+ out[i] = '1'
+ return int(''.join(out), 2)
+
+def calc_crc5(bitstr):
+ poly5 = 0x25
+ crc5 = 0x1f
+ for bit in bitstr:
+ crc5 <<= 1
+ if int(bit) != (crc5 >> 5):
+ crc5 ^= poly5
+ crc5 &= 0x1f
+ crc5 ^= 0x1f
+ return reverse_number(crc5, 5)
+
+def calc_crc16(bitstr):
+ poly16 = 0x18005
+ crc16 = 0xffff
+ for bit in bitstr:
+ crc16 <<= 1
+ if int(bit) != (crc16 >> 16):
+ crc16 ^= poly16
+ crc16 &= 0xffff
+ crc16 ^= 0xffff
+ return reverse_number(crc16, 16)
+
class Decoder(srd.Decoder):
- api_version = 1
+ api_version = 3
id = 'usb_packet'
name = 'USB packet'
longname = 'Universal Serial Bus (LS/FS) packet'
@@ -151,8 +181,10 @@ class Decoder(srd.Decoder):
license = 'gplv2+'
inputs = ['usb_signalling']
outputs = ['usb_packet']
+ tags = ['PC']
options = (
- {'id': 'signalling', 'desc': 'Signalling', 'default': 'full-speed'},
+ {'id': 'signalling', 'desc': 'Signalling',
+ 'default': 'full-speed', 'values': ('full-speed', 'low-speed')},
)
annotations = (
('sync-ok', 'SYNC'),
@@ -186,12 +218,14 @@ class Decoder(srd.Decoder):
('packet-invalid', 'Packet: Invalid'),
)
annotation_rows = (
- ('fields', 'Packet fields', tuple(range(11 + 1))),
- ('packet', 'Packets', tuple(range(12, 28 + 1))),
+ ('fields', 'Packet fields', tuple(range(10 + 1))),
+ ('packet', 'Packets', tuple(range(11, 28 + 1))),
)
def __init__(self):
- self.samplenum = 0
+ self.reset()
+
+ def reset(self):
self.bits = []
self.packet = []
self.packet_summary = ''
@@ -220,34 +254,47 @@ class Decoder(srd.Decoder):
for (bit, ss, es) in self.bits:
packet += bit
+ if len(packet) < 8:
+ self.putp([28, ['Invalid packet (shorter than 8 bits)']])
+ return
+
# Bits[0:7]: SYNC
sync = packet[:7 + 1]
self.ss, self.es = self.bits[0][1], self.bits[7][2]
# The SYNC pattern for low-speed/full-speed is KJKJKJKK (00000001).
if sync != '00000001':
self.putpb(['SYNC ERROR', sync])
- self.putb([1, ['SYNC ERROR: %s' % sync]])
+ self.putb([1, ['SYNC ERROR: %s' % sync, 'SYNC ERR: %s' % sync,
+ 'SYNC ERR', 'SE', 'S']])
else:
self.putpb(['SYNC', sync])
- self.putb([0, ['SYNC: %s' % sync]])
+ self.putb([0, ['SYNC: %s' % sync, 'SYNC', 'S']])
self.packet.append(sync)
+ if len(packet) < 16:
+ self.putp([28, ['Invalid packet (shorter than 16 bits)']])
+ return
+
# Bits[8:15]: PID
pid = packet[8:15 + 1]
- pidname = pids.get(pid, (pid, ''))[0]
+ pidname = pids.get(pid, ('UNKNOWN', 'Unknown PID'))[0]
self.ss, self.es = self.bits[8][1], self.bits[15][2]
self.putpb(['PID', pidname])
- self.putb([2, ['PID: %s' % pidname]])
+ self.putb([2, ['PID: %s' % pidname, pidname, pidname[0]]])
self.packet.append(pid)
self.packet_summary += pidname
- if pidname in ('OUT', 'IN', 'SOF', 'SETUP', 'PRE', 'PING'):
+ if pidname in ('OUT', 'IN', 'SOF', 'SETUP', 'PING'):
+ if len(packet) < 32:
+ self.putp([28, ['Invalid packet (shorter than 32 bits)']])
+ return
+
if pidname == 'SOF':
# Bits[16:26]: Framenum
framenum = bitstr_to_num(packet[16:26 + 1])
self.ss, self.es = self.bits[16][1], self.bits[26][2]
self.putpb(['FRAMENUM', framenum])
- self.putb([3, ['Frame: %d' % framenum]])
+ self.putb([3, ['Frame: %d' % framenum, 'Frame', 'Fr', 'F']])
self.packet.append(framenum)
self.packet_summary += ' %d' % framenum
else:
@@ -255,7 +302,8 @@ class Decoder(srd.Decoder):
addr = bitstr_to_num(packet[16:22 + 1])
self.ss, self.es = self.bits[16][1], self.bits[22][2]
self.putpb(['ADDR', addr])
- self.putb([4, ['Addr: %d' % addr]])
+ self.putb([4, ['Address: %d' % addr, 'Addr: %d' % addr,
+ 'Addr', 'A']])
self.packet.append(addr)
self.packet_summary += ' ADDR %d' % addr
@@ -263,15 +311,20 @@ class Decoder(srd.Decoder):
ep = bitstr_to_num(packet[23:26 + 1])
self.ss, self.es = self.bits[23][1], self.bits[26][2]
self.putpb(['EP', ep])
- self.putb([5, ['EP: %d' % ep]])
+ self.putb([5, ['Endpoint: %d' % ep, 'EP: %d' % ep, 'EP', 'E']])
self.packet.append(ep)
self.packet_summary += ' EP %d' % ep
# Bits[27:31]: CRC5
crc5 = bitstr_to_num(packet[27:31 + 1])
+ crc5_calc = calc_crc5(packet[16:27])
self.ss, self.es = self.bits[27][1], self.bits[31][2]
- self.putpb(['CRC5', crc5])
- self.putb([6, ['CRC5: 0x%02x' % crc5]])
+ if crc5 == crc5_calc:
+ self.putpb(['CRC5', crc5])
+ self.putb([6, ['CRC5: 0x%02X' % crc5, 'CRC5', 'C']])
+ else:
+ self.putpb(['CRC5 ERROR', crc5])
+ self.putb([7, ['CRC5 ERROR: 0x%02X' % crc5, 'CRC5 ERR', 'CE', 'C']])
self.packet.append(crc5)
elif pidname in ('DATA0', 'DATA1', 'DATA2', 'MDATA'):
# Bits[16:packetlen-16]: Data
@@ -283,25 +336,32 @@ class Decoder(srd.Decoder):
db = bitstr_to_num(data[i:i + 8])
self.ss, self.es = self.bits[16 + i][1], self.bits[23 + i][2]
self.putpb(['DATABYTE', db])
- self.putb([8, ['Databyte: %02x' % db]])
+ self.putb([8, ['Databyte: %02X' % db, 'Data: %02X' % db,
+ 'DB: %02X' % db, '%02X' % db]])
databytes.append(db)
- self.packet_summary += ' %02x' % db
- data = data[8:]
+ self.packet_summary += ' %02X' % db
self.packet_summary += ' ]'
- # Convenience python output (no annotation) for all bytes together.
+ # Convenience Python output (no annotation) for all bytes together.
self.ss, self.es = self.bits[16][1], self.bits[-16][2]
self.putpb(['DATABYTES', databytes])
self.packet.append(databytes)
# Bits[packetlen-16:packetlen]: CRC16
crc16 = bitstr_to_num(packet[-16:])
+ crc16_calc = calc_crc16(packet[16:-16])
self.ss, self.es = self.bits[-16][1], self.bits[-1][2]
- self.putpb(['CRC16', crc16])
- self.putb([9, ['CRC16: 0x%04x' % crc16]])
+ if crc16 == crc16_calc:
+ self.putpb(['CRC16', crc16])
+ self.putb([9, ['CRC16: 0x%04X' % crc16, 'CRC16', 'C']])
+ else:
+ self.putpb(['CRC16 ERROR', crc16])
+ self.putb([10, ['CRC16 ERROR: 0x%04X' % crc16, 'CRC16 ERR', 'CE', 'C']])
self.packet.append(crc16)
elif pidname in ('ACK', 'NAK', 'STALL', 'NYET', 'ERR'):
pass # Nothing to do, these only have SYNC+PID+EOP fields.
+ elif pidname in ('PRE'):
+ pass # Nothing to do, PRE only has SYNC+PID fields.
else:
pass # TODO: Handle 'SPLIT' and possibly 'Reserved' packets.
@@ -316,7 +376,7 @@ class Decoder(srd.Decoder):
(ptype, pdata) = data
# We only care about certain packet types for now.
- if ptype not in ('SOP', 'BIT', 'EOP'):
+ if ptype not in ('SOP', 'BIT', 'EOP', 'ERR'):
return
# State machine.
@@ -328,12 +388,10 @@ class Decoder(srd.Decoder):
elif self.state == 'GET BIT':
if ptype == 'BIT':
self.bits.append([pdata, ss, es])
- elif ptype == 'EOP':
+ elif ptype == 'EOP' or ptype == 'ERR':
self.es_packet = es
self.handle_packet()
+ self.packet, self.packet_summary = [], ''
self.bits, self.state = [], 'WAIT FOR SOP'
else:
pass # TODO: Error
- else:
- raise Exception('Invalid state: %s' % self.state)
-