]> sigrok.org Git - libsigrokdecode.git/blob - decoders/arm_tpiu/pd.py
Add PD tags handling and some tags
[libsigrokdecode.git] / decoders / arm_tpiu / pd.py
1 ##
2 ## This file is part of the libsigrokdecode project.
3 ##
4 ## Copyright (C) 2015 Petteri Aimonen <jpa@sigrok.mail.kapsi.fi>
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 = 3
24     id = 'arm_tpiu'
25     name = 'ARM TPIU'
26     longname = 'ARM Trace Port Interface Unit'
27     desc = 'Filter TPIU formatted trace data into separate streams.'
28     license = 'gplv2+'
29     inputs = ['uart']
30     outputs = ['uart'] # Emulate uart output so that arm_itm/arm_etm can stack.
31     tags = ['Logic', 'MCU Debugging']
32     options = (
33         {'id': 'stream', 'desc': 'Stream index', 'default': 1},
34         {'id': 'sync_offset', 'desc': 'Initial sync offset', 'default': 0},
35     )
36     annotations = (
37         ('stream', 'Current stream'),
38         ('data', 'Stream data'),
39     )
40     annotation_rows = (
41         ('stream', 'Current stream', (0,)),
42         ('data', 'Stream data', (1,)),
43     )
44
45     def __init__(self):
46         self.reset()
47
48     def reset(self):
49         self.buf = []
50         self.syncbuf = []
51         self.prevsample = 0
52         self.stream = 0
53         self.ss_stream = None
54         self.bytenum = 0
55
56     def start(self):
57         self.out_ann = self.register(srd.OUTPUT_ANN)
58         self.out_python = self.register(srd.OUTPUT_PYTHON)
59
60     def stream_changed(self, ss, stream):
61         if self.stream != stream:
62             if self.stream != 0:
63                 self.put(self.ss_stream, ss, self.out_ann,
64                          [0, ['Stream %d' % self.stream, 'S%d' % self.stream]])
65             self.stream = stream
66             self.ss_stream = ss
67
68     def emit_byte(self, ss, es, byte):
69         if self.stream == self.options['stream']:
70             self.put(ss, es, self.out_ann, [1, ['0x%02x' % byte]])
71             self.put(ss, es, self.out_python, ['DATA', 0, (byte, [])])
72
73     def process_frame(self, buf):
74         # Byte 15 contains the lowest bits of bytes 0, 2, ... 14.
75         lowbits = buf[15][2]
76
77         for i in range(0, 15, 2):
78             # Odd bytes can be stream ID or data.
79             delayed_stream_change = None
80             lowbit = (lowbits >> (i // 2)) & 0x01
81             if buf[i][2] & 0x01 != 0:
82                 if lowbit:
83                     delayed_stream_change = buf[i][2] >> 1
84                 else:
85                     self.stream_changed(buf[i][0], buf[i][2] >> 1)
86             else:
87                 byte = buf[i][2] | lowbit
88                 self.emit_byte(buf[i][0], buf[i][1], byte)
89
90             # Even bytes are always data.
91             if i < 14:
92                 self.emit_byte(buf[i+1][0], buf[i+1][1], buf[i+1][2])
93
94             # The stream change can be delayed to occur after the data byte.
95             if delayed_stream_change is not None:
96                 self.stream_changed(buf[i+1][1], delayed_stream_change)
97
98     def decode(self, ss, es, data):
99         ptype, rxtx, pdata = data
100
101         if ptype != 'DATA':
102             return
103
104         # Reset packet if there is a long pause between bytes.
105         self.byte_len = es - ss
106         if ss - self.prevsample > self.byte_len:
107             self.buf = []
108         self.prevsample = es
109
110         self.buf.append((ss, es, pdata[0]))
111         self.bytenum += 1
112
113         # Allow skipping N first bytes of the data. By adjusting the sync
114         # value, one can get initial synchronization as soon as the trace
115         # starts.
116         if self.bytenum < self.options['sync_offset']:
117             self.buf = []
118             return
119
120         # Keep separate buffer for detection of sync packets.
121         # Sync packets override everything else, so that we can regain sync
122         # even if some packets are corrupted.
123         self.syncbuf = self.syncbuf[-3:] + [pdata[0]]
124         if self.syncbuf == [0xFF, 0xFF, 0xFF, 0x7F]:
125             self.buf = []
126             self.syncbuf = []
127             return
128
129         if len(self.buf) == 16:
130             self.process_frame(self.buf)
131             self.buf = []