Skip to content

Commit a05d3a9

Browse files
committed
i2c_packet: new decoder for forming packets from i2c data
i2c_packet protocol decoder stacks with i2c PD and allows to combine several data bytes from "START" i2c condition until "STOP" or "START REPEAT" and puts this data in a dedicated annotation. Transaction mode allows to combine all packets from START to STOP condition into single transaction packet. Possible data formats for data output are hex, ascii, dec, bin and oct. This decoder also allows logging data packets with the print_sec option enabled.
1 parent e556e11 commit a05d3a9

2 files changed

Lines changed: 239 additions & 0 deletions

File tree

decoders/i2c_packet/__init__.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
##
2+
## This file is part of the libsigrokdecode project.
3+
##
4+
## Copyright (C) 2022 Sergey Spivak <sespivak@yandex.ru>
5+
##
6+
## Permission is hereby granted, free of charge, to any person obtaining a copy
7+
## of this software and associated documentation files (the "Software"), to deal
8+
## in the Software without restriction, including without limitation the rights
9+
## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
## copies of the Software, and to permit persons to whom the Software is
11+
## furnished to do so, subject to the following conditions:
12+
##
13+
## The above copyright notice and this permission notice shall be included in all
14+
## copies or substantial portions of the Software.
15+
##
16+
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
## SOFTWARE.
23+
24+
'''
25+
Make data packets from I²C decoder data.
26+
'''
27+
28+
from .pd import Decoder

decoders/i2c_packet/pd.py

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
##
2+
## This file is part of the libsigrokdecode project.
3+
##
4+
## Copyright (C) 2022 Sergey Spivak <sespivak@yandex.ru>
5+
##
6+
## Permission is hereby granted, free of charge, to any person obtaining a copy
7+
## of this software and associated documentation files (the "Software"), to deal
8+
## in the Software without restriction, including without limitation the rights
9+
## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
## copies of the Software, and to permit persons to whom the Software is
11+
## furnished to do so, subject to the following conditions:
12+
##
13+
## The above copyright notice and this permission notice shall be included in all
14+
## copies or substantial portions of the Software.
15+
##
16+
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
## SOFTWARE.
23+
24+
from collections import deque
25+
import sigrokdecode as srd
26+
27+
'''
28+
OUTPUT_PYTHON format:
29+
30+
Packet:
31+
[<ptype>, <pdata>]
32+
33+
<ptype>:
34+
- 'PACKET READ' (ADDRESS READ followed by one or several
35+
DATA bytes until STOP or START REPEAT)
36+
- 'PACKET WRITE' (ADDRESS WRITE followed by one or several
37+
DATA bytes until STOP or START REPEAT)
38+
- 'TRANSACTION END' (End of transaction, due toi STOP bus condition)
39+
40+
<pdata> is the tuple with slave address byte and tuple with data bytes
41+
if ptype is 'PACKET READ' or 'PACKET WRITE'.
42+
Slave addresses do not include bit 0 (the READ/WRITE indication bit).
43+
<pdata is None if ptype is 'TRANSACTION END'.
44+
'''
45+
46+
class Ann:
47+
PACKET_READ, \
48+
PACKET_WRITE, \
49+
TRANSACTION, \
50+
= range(3)
51+
52+
class Decoder(srd.Decoder):
53+
api_version = 3
54+
id = 'i2c_packet'
55+
name = 'I²C packet'
56+
longname = 'I²C packet builder'
57+
desc = 'Concatenate I²C data to packets'
58+
license = 'mit'
59+
inputs = ['i2c']
60+
outputs = []
61+
tags = ['Embedded/industrial']
62+
options = (
63+
{'id': 'format', 'desc': 'Data format', 'default': 'hex',
64+
'values': ('ascii', 'dec', 'hex', 'oct', 'bin')},
65+
{'id': 'transaction', 'desc': 'Transaction mode', 'default': 'no', 'values': ('yes', 'no')},
66+
)
67+
annotations = (
68+
('rd', 'Read'),
69+
('wr', 'Write'),
70+
('data', 'Data'),
71+
)
72+
annotation_rows = (
73+
('rd', 'Read', (Ann.PACKET_READ, )),
74+
('wr', 'Write', (Ann.PACKET_WRITE, )),
75+
('data', 'Data', (Ann.TRANSACTION, )),
76+
)
77+
78+
def __init__(self):
79+
self.out_py = None
80+
self.out_ann = None
81+
self.packet_data = deque()
82+
self.packet_str = ''
83+
self.packet_str_short = ''
84+
self.packet_ss = 0
85+
self.packet_part_ss = 0
86+
self.packet_es = 0
87+
self.transaction = False
88+
self.read_sign = False
89+
self.address = 0
90+
self.fmt = None
91+
self.reset()
92+
93+
def start(self):
94+
self.out_ann = self.register(srd.OUTPUT_ANN)
95+
self.out_py = self.register(srd.OUTPUT_PYTHON)
96+
97+
format_name = self.options['format']
98+
if format_name == 'hex':
99+
self.fmt = '{:02X}'
100+
elif format_name == 'dec':
101+
self.fmt = '{:d}'
102+
elif format_name == 'bin':
103+
self.fmt = '{:08b}'
104+
elif format_name == 'oct':
105+
self.fmt = '{:03o}'
106+
else:
107+
self.fmt = None
108+
self.transaction = self.options['transaction'] == 'yes'
109+
self.packet_data = deque()
110+
111+
def reset(self):
112+
self.packet_data.clear()
113+
self.packet_str = ''
114+
self.packet_str_short = ''
115+
self.packet_ss = 0
116+
self.packet_part_ss = 0
117+
self.packet_es = 0
118+
self.address = 0
119+
120+
def putg(self, ss, es, data):
121+
"""Put a graphical annotation."""
122+
self.put(ss, es, self.out_ann, data)
123+
124+
def putp(self, ss, es, data):
125+
"""Put a python annotation."""
126+
self.put(ss, es, self.out_py, data)
127+
128+
def format_data_value(self, v):
129+
# Assume "is printable" for values from 32 to including 126,
130+
# below 32 is "control" and thus not printable, above 127 is
131+
# "not ASCII" in its strict sense, 127 (DEL) is not printable,
132+
# fall back to hex representation for non-printables.
133+
if self.fmt is None:
134+
if 32 <= v <= 126:
135+
return chr(v)
136+
return "[{:02X}]".format(v)
137+
else:
138+
return self.fmt.format(v)
139+
140+
def data_array_to_str(self, data_array):
141+
if self.fmt:
142+
str_array = [self.fmt.format(value) for value in data_array]
143+
return ' '.join(str_array)
144+
else:
145+
str_array = [self.format_data_value(value) for value in data_array]
146+
return ''.join(str_array)
147+
148+
def format_packet(self):
149+
packet_str = "0x{:02X} {:}: ".format(
150+
self.address,
151+
'RD' if self.read_sign else 'WR',
152+
) + self.data_array_to_str(self.packet_data)
153+
154+
packet_str_short = packet_str[2:]
155+
156+
if self.transaction and self.packet_str:
157+
packet_str = self.packet_str + ' [SR] ' + packet_str
158+
packet_str_short = self.packet_str_short + ' [SR] ' + packet_str_short
159+
160+
return packet_str, packet_str_short
161+
162+
def handle_packet(self, start_repeat=False):
163+
if not len(self.packet_data):
164+
if not start_repeat:
165+
self.reset()
166+
return
167+
168+
packet_str, packet_str_short = self.format_packet()
169+
170+
ptype = 'PACKET READ' if self.read_sign else 'PACKET WRITE'
171+
self.putp(self.packet_part_ss, self.packet_es, (ptype, (self.address, tuple(self.packet_data))))
172+
if not start_repeat:
173+
self.putp(self.packet_es, self.packet_es, ('TRANSACTION END', None))
174+
175+
if start_repeat and self.transaction:
176+
self.packet_data.clear()
177+
self.packet_str = packet_str
178+
self.packet_str_short = packet_str_short
179+
else:
180+
if self.transaction:
181+
ann = Ann.TRANSACTION
182+
elif self.read_sign:
183+
ann = Ann.PACKET_READ
184+
else:
185+
ann = Ann.PACKET_WRITE
186+
187+
packet_ss = self.packet_ss if self.transaction else self.packet_part_ss
188+
self.putg(packet_ss, self.packet_es, [ann, [packet_str, packet_str_short]])
189+
self.reset()
190+
191+
def decode(self, ss, es, data):
192+
ptype = data[0]
193+
if ptype.startswith('DATA'):
194+
self.packet_data.append(data[1])
195+
self.packet_es = es
196+
elif ptype.startswith('START'):
197+
# If there is still data in the reception buffer, put the packet annotation
198+
start_repeat = 'REPEAT' in ptype
199+
self.handle_packet(start_repeat=start_repeat)
200+
self.packet_part_ss = ss
201+
if not start_repeat:
202+
self.packet_ss = ss
203+
elif ptype.startswith('ADDRESS'):
204+
self.address = data[1]
205+
self.read_sign = 'READ' in ptype
206+
self.packet_es = es
207+
elif ptype.endswith('ACK'):
208+
self.packet_es = es
209+
elif ptype == 'STOP':
210+
self.packet_es = es
211+
self.handle_packet()

0 commit comments

Comments
 (0)