2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2015 Petteri Aimonen <jpa@sigrok.mail.kapsi.fi>
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.
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.
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
21 import sigrokdecode as srd
23 class Decoder(srd.Decoder):
27 longname = 'ARM Trace Port Interface Unit'
28 desc = 'Filter TPIU formatted trace data into separate streams.'
31 outputs = ['uart'] # Emulate uart output so that arm_itm/arm_etm can stack.
33 {'id': 'stream', 'desc': 'Stream index', 'default': 1},
34 {'id': 'sync_offset', 'desc': 'Initial sync offset', 'default': 0},
37 ('stream', 'Current stream'),
38 ('data', 'Stream data'),
41 ('stream', 'Current stream', (0,)),
42 ('data', 'Stream data', (1,)),
45 def __init__(self, **kwargs):
54 self.out_ann = self.register(srd.OUTPUT_ANN)
55 self.out_python = self.register(srd.OUTPUT_PYTHON)
57 def stream_changed(self, ss, stream):
58 if self.stream != stream:
60 self.put(self.stream_ss, ss, self.out_ann,
61 [0, ["Stream %d" % self.stream, "S%d" % self.stream]])
65 def emit_byte(self, ss, es, byte):
66 if self.stream == self.options['stream']:
67 self.put(ss, es, self.out_ann, [1, ["0x%02x" % byte]])
68 self.put(ss, es, self.out_python, ['DATA', 0, (byte, [])])
70 def process_frame(self, buf):
71 # Byte 15 contains the lowest bits of bytes 0, 2, ... 14.
74 for i in range(0, 15, 2):
75 # Odd bytes can be stream ID or data.
76 delayed_stream_change = None
77 lowbit = (lowbits >> (i // 2)) & 0x01
78 if buf[i][2] & 0x01 != 0:
80 delayed_stream_change = buf[i][2] >> 1
82 self.stream_changed(buf[i][0], buf[i][2] >> 1)
84 byte = buf[i][2] | lowbit
85 self.emit_byte(buf[i][0], buf[i][1], byte)
87 # Even bytes are always data.
89 self.emit_byte(buf[i+1][0], buf[i+1][1], buf[i+1][2])
91 # The stream change can be delayed to occur after the data byte.
92 if delayed_stream_change is not None:
93 self.stream_changed(buf[i+1][1], delayed_stream_change)
95 def decode(self, ss, es, data):
96 ptype, rxtx, pdata = data
101 # Reset packet if there is a long pause between bytes.
102 self.byte_len = es - ss
103 if ss - self.prevsample > self.byte_len:
107 self.buf.append((ss, es, pdata[0]))
110 # Allow skipping N first bytes of the data. By adjusting the sync
111 # value, one can get initial synchronization as soon as the trace
113 if self.bytenum < self.options['sync_offset']:
117 # Keep separate buffer for detection of sync packets.
118 # Sync packets override everything else, so that we can regain sync
119 # even if some packets are corrupted.
120 self.syncbuf = self.syncbuf[-3:] + [pdata[0]]
121 if self.syncbuf == [0xFF, 0xFF, 0xFF, 0x7F]:
126 if len(self.buf) == 16:
127 self.process_frame(self.buf)